Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceStreamBase" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 21 | #include <iomanip> |
| 22 | #include <iostream> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 23 | #include <mutex> |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 24 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 25 | #include <media/MediaMetricsItem.h> |
| 26 | #include <media/TypeConverter.h> |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 27 | #include <mediautils/SchedulingPolicyService.h> |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 28 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 29 | #include "binding/AAudioServiceMessage.h" |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 30 | #include "core/AudioGlobal.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 31 | #include "utility/AudioClock.h" |
| 32 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 33 | #include "AAudioEndpointManager.h" |
| 34 | #include "AAudioService.h" |
| 35 | #include "AAudioServiceEndpoint.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 36 | #include "AAudioServiceStreamBase.h" |
| 37 | #include "TimestampScheduler.h" |
| 38 | |
| 39 | using namespace android; // TODO just import names needed |
| 40 | using namespace aaudio; // TODO just import names needed |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 41 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame^] | 42 | using content::AttributionSourceState; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 43 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 44 | /** |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 45 | * Base class for streams in the service. |
| 46 | * @return |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 47 | */ |
| 48 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 49 | AAudioServiceStreamBase::AAudioServiceStreamBase(AAudioService &audioService) |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 50 | : mTimestampThread("AATime") |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 51 | , mAtomicStreamTimestamp() |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 52 | , mAudioService(audioService) { |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame^] | 53 | mMmapClient.attributionSource = AttributionSourceState(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 56 | AAudioServiceStreamBase::~AAudioServiceStreamBase() { |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 57 | ALOGD("%s() called", __func__); |
| 58 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 59 | // May not be set if open failed. |
| 60 | if (mMetricsId.size() > 0) { |
| 61 | mediametrics::LogItem(mMetricsId) |
| 62 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DTOR) |
| 63 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 64 | .record(); |
| 65 | } |
| 66 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 67 | // If the stream is deleted when OPEN or in use then audio resources will leak. |
| 68 | // This would indicate an internal error. So we want to find this ASAP. |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 69 | LOG_ALWAYS_FATAL_IF(!(getState() == AAUDIO_STREAM_STATE_CLOSED |
Phil Burk | db46614 | 2021-04-16 16:50:00 +0000 | [diff] [blame] | 70 | || getState() == AAUDIO_STREAM_STATE_UNINITIALIZED), |
Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 71 | "service stream %p still open, state = %d", |
| 72 | this, getState()); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 75 | std::string AAudioServiceStreamBase::dumpHeader() { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 76 | return std::string(" T Handle UId Port Run State Format Burst Chan Capacity"); |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 79 | std::string AAudioServiceStreamBase::dump() const { |
| 80 | std::stringstream result; |
| 81 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 82 | result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle |
| 83 | << std::dec << std::setfill(' ') ; |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame^] | 84 | result << std::setw(6) << mMmapClient.attributionSource.uid; |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 85 | result << std::setw(7) << mClientHandle; |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 86 | result << std::setw(4) << (isRunning() ? "yes" : " no"); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 87 | result << std::setw(6) << getState(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 88 | result << std::setw(7) << getFormat(); |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 89 | result << std::setw(6) << mFramesPerBurst; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 90 | result << std::setw(5) << getSamplesPerFrame(); |
| 91 | result << std::setw(9) << getBufferCapacity(); |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 92 | |
| 93 | return result.str(); |
| 94 | } |
| 95 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 96 | void AAudioServiceStreamBase::logOpen(aaudio_handle_t streamHandle) { |
| 97 | // This is the first log sent from the AAudio Service for a stream. |
| 98 | mMetricsId = std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_STREAM) |
| 99 | + std::to_string(streamHandle); |
| 100 | |
| 101 | audio_attributes_t attributes = AAudioServiceEndpoint::getAudioAttributesFrom(this); |
| 102 | |
| 103 | // Once this item is logged by the server, the client with the same PID, UID |
| 104 | // can also log properties. |
| 105 | mediametrics::LogItem(mMetricsId) |
| 106 | .setPid(getOwnerProcessId()) |
| 107 | .setUid(getOwnerUserId()) |
Andy Hung | d203eb6 | 2020-04-27 09:12:46 -0700 | [diff] [blame] | 108 | .set(AMEDIAMETRICS_PROP_ALLOWUID, (int32_t)getOwnerUserId()) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 109 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_OPEN) |
| 110 | // the following are immutable |
| 111 | .set(AMEDIAMETRICS_PROP_BUFFERCAPACITYFRAMES, (int32_t)getBufferCapacity()) |
| 112 | .set(AMEDIAMETRICS_PROP_BURSTFRAMES, (int32_t)getFramesPerBurst()) |
| 113 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)getSamplesPerFrame()) |
| 114 | .set(AMEDIAMETRICS_PROP_CONTENTTYPE, toString(attributes.content_type).c_str()) |
| 115 | .set(AMEDIAMETRICS_PROP_DIRECTION, |
| 116 | AudioGlobal_convertDirectionToText(getDirection())) |
| 117 | .set(AMEDIAMETRICS_PROP_ENCODING, toString(getFormat()).c_str()) |
| 118 | .set(AMEDIAMETRICS_PROP_ROUTEDDEVICEID, (int32_t)getDeviceId()) |
| 119 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)getSampleRate()) |
| 120 | .set(AMEDIAMETRICS_PROP_SESSIONID, (int32_t)getSessionId()) |
| 121 | .set(AMEDIAMETRICS_PROP_SOURCE, toString(attributes.source).c_str()) |
| 122 | .set(AMEDIAMETRICS_PROP_USAGE, toString(attributes.usage).c_str()) |
| 123 | .record(); |
| 124 | } |
| 125 | |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 126 | aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 127 | AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance(); |
| 128 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 129 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame^] | 130 | mMmapClient.attributionSource = request.getAttributionSource(); |
| 131 | // TODO b/182392769: use attribution source util |
| 132 | mMmapClient.attributionSource.uid = VALUE_OR_FATAL( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 133 | legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid())); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame^] | 134 | mMmapClient.attributionSource.pid = VALUE_OR_FATAL( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 135 | legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid())); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 136 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 137 | // Limit scope of lock to avoid recursive lock in close(). |
| 138 | { |
| 139 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 140 | if (mUpMessageQueue != nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 141 | ALOGE("%s() called twice", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 142 | return AAUDIO_ERROR_INVALID_STATE; |
| 143 | } |
| 144 | |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 145 | mUpMessageQueue = std::make_shared<SharedRingBuffer>(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 146 | result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), |
| 147 | QUEUE_UP_CAPACITY_COMMANDS); |
| 148 | if (result != AAUDIO_OK) { |
| 149 | goto error; |
| 150 | } |
| 151 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 152 | // This is not protected by a lock because the stream cannot be |
| 153 | // referenced until the service returns a handle to the client. |
| 154 | // So only one thread can open a stream. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 155 | mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 156 | request); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 157 | if (mServiceEndpoint == nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 158 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 159 | goto error; |
| 160 | } |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 161 | // Save a weak pointer that we will use to access the endpoint. |
| 162 | mServiceEndpointWeak = mServiceEndpoint; |
| 163 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 164 | mFramesPerBurst = mServiceEndpoint->getFramesPerBurst(); |
| 165 | copyFrom(*mServiceEndpoint); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 166 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 167 | return result; |
| 168 | |
| 169 | error: |
| 170 | close(); |
| 171 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 172 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 173 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 174 | aaudio_result_t AAudioServiceStreamBase::close() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 175 | std::lock_guard<std::mutex> lock(mLock); |
| 176 | return close_l(); |
| 177 | } |
| 178 | |
| 179 | aaudio_result_t AAudioServiceStreamBase::close_l() { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 180 | if (getState() == AAUDIO_STREAM_STATE_CLOSED) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 181 | return AAUDIO_OK; |
| 182 | } |
| 183 | |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 184 | // This will call stopTimestampThread() and also stop the stream, |
| 185 | // just in case it was not already stopped. |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 186 | stop_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 187 | |
Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 188 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 189 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 190 | if (endpoint == nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 191 | result = AAUDIO_ERROR_INVALID_STATE; |
| 192 | } else { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 193 | endpoint->unregisterStream(this); |
| 194 | AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance(); |
| 195 | endpointManager.closeEndpoint(endpoint); |
| 196 | |
| 197 | // AAudioService::closeStream() prevents two threads from closing at the same time. |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 198 | mServiceEndpoint.clear(); // endpoint will hold the pointer after this method returns. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 201 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 202 | |
| 203 | mediametrics::LogItem(mMetricsId) |
| 204 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CLOSE) |
| 205 | .record(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 206 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 209 | aaudio_result_t AAudioServiceStreamBase::startDevice() { |
| 210 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 211 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 212 | if (endpoint == nullptr) { |
| 213 | ALOGE("%s() has no endpoint", __func__); |
| 214 | return AAUDIO_ERROR_INVALID_STATE; |
| 215 | } |
| 216 | return endpoint->startStream(this, &mClientHandle); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Start the flow of audio data. |
| 221 | * |
| 222 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
| 223 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 224 | aaudio_result_t AAudioServiceStreamBase::start() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 225 | std::lock_guard<std::mutex> lock(mLock); |
| 226 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 227 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 228 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 229 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 230 | if (auto state = getState(); |
Phil Burk | db46614 | 2021-04-16 16:50:00 +0000 | [diff] [blame] | 231 | state == AAUDIO_STREAM_STATE_CLOSED || isDisconnected_l()) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 232 | ALOGW("%s() already CLOSED, returns INVALID_STATE, handle = %d", |
| 233 | __func__, getHandle()); |
| 234 | return AAUDIO_ERROR_INVALID_STATE; |
| 235 | } |
| 236 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 237 | mediametrics::Defer defer([&] { |
| 238 | mediametrics::LogItem(mMetricsId) |
| 239 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 240 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 241 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 242 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 243 | .record(); }); |
| 244 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 245 | if (isRunning()) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 246 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 247 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 248 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 249 | setFlowing(false); |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 250 | setSuspended(false); |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 251 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 252 | // Start with fresh presentation timestamps. |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 253 | mAtomicStreamTimestamp.clear(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 254 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 255 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 256 | result = startDevice(); |
| 257 | if (result != AAUDIO_OK) goto error; |
| 258 | |
| 259 | // This should happen at the end of the start. |
| 260 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED); |
| 261 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 262 | mThreadEnabled.store(true); |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 263 | // Make sure this object does not get deleted before the run() method |
| 264 | // can protect it by making a strong pointer. |
| 265 | incStrong(nullptr); // See run() method. |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 266 | result = mTimestampThread.start(this); |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 267 | if (result != AAUDIO_OK) { |
| 268 | decStrong(nullptr); // run() can't do it so we have to do it here. |
| 269 | goto error; |
| 270 | } |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 271 | |
| 272 | return result; |
| 273 | |
| 274 | error: |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 275 | disconnect_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 276 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | aaudio_result_t AAudioServiceStreamBase::pause() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 280 | std::lock_guard<std::mutex> lock(mLock); |
| 281 | return pause_l(); |
| 282 | } |
| 283 | |
| 284 | aaudio_result_t AAudioServiceStreamBase::pause_l() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 285 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 286 | if (!isRunning()) { |
| 287 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 288 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 289 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 290 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 291 | mediametrics::Defer defer([&] { |
| 292 | mediametrics::LogItem(mMetricsId) |
| 293 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_PAUSE) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 294 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 295 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 296 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 297 | .record(); }); |
| 298 | |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 299 | result = stopTimestampThread(); |
| 300 | if (result != AAUDIO_OK) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 301 | disconnect_l(); |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 302 | return result; |
| 303 | } |
| 304 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 305 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 306 | if (endpoint == nullptr) { |
| 307 | ALOGE("%s() has no endpoint", __func__); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 308 | result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking |
| 309 | return result; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 310 | } |
| 311 | result = endpoint->stopStream(this, mClientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 312 | if (result != AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 313 | ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 314 | disconnect_l(); // TODO should we return or pause Base first? |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 317 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 318 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 319 | return result; |
| 320 | } |
| 321 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 322 | aaudio_result_t AAudioServiceStreamBase::stop() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 323 | std::lock_guard<std::mutex> lock(mLock); |
| 324 | return stop_l(); |
| 325 | } |
| 326 | |
| 327 | aaudio_result_t AAudioServiceStreamBase::stop_l() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 328 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 329 | if (!isRunning()) { |
| 330 | return result; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 331 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 332 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 333 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 334 | mediametrics::Defer defer([&] { |
| 335 | mediametrics::LogItem(mMetricsId) |
| 336 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 337 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 338 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 339 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 340 | .record(); }); |
| 341 | |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame] | 342 | setState(AAUDIO_STREAM_STATE_STOPPING); |
| 343 | |
Phil Burk | d50ae62 | 2021-04-15 23:40:25 +0000 | [diff] [blame] | 344 | // Temporarily unlock because we are joining the timestamp thread and it may try |
| 345 | // to acquire mLock. |
| 346 | mLock.unlock(); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 347 | result = stopTimestampThread(); |
Phil Burk | d50ae62 | 2021-04-15 23:40:25 +0000 | [diff] [blame] | 348 | mLock.lock(); |
| 349 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 350 | if (result != AAUDIO_OK) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 351 | disconnect_l(); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 352 | return result; |
| 353 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 354 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 355 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 356 | if (endpoint == nullptr) { |
| 357 | ALOGE("%s() has no endpoint", __func__); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 358 | result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking |
| 359 | return result; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 360 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 361 | // TODO wait for data to be played out |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 362 | result = endpoint->stopStream(this, mClientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 363 | if (result != AAUDIO_OK) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 364 | ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 365 | disconnect_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 366 | // TODO what to do with result here? |
| 367 | } |
| 368 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 369 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 370 | setState(AAUDIO_STREAM_STATE_STOPPED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 371 | return result; |
| 372 | } |
| 373 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 374 | aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() { |
| 375 | aaudio_result_t result = AAUDIO_OK; |
| 376 | // clear flag that tells thread to loop |
| 377 | if (mThreadEnabled.exchange(false)) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 378 | result = mTimestampThread.stop(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 379 | } |
| 380 | return result; |
| 381 | } |
| 382 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 383 | aaudio_result_t AAudioServiceStreamBase::flush() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 384 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 385 | aaudio_result_t result = AAudio_isFlushAllowed(getState()); |
| 386 | if (result != AAUDIO_OK) { |
| 387 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 388 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 389 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 390 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 391 | mediametrics::Defer defer([&] { |
| 392 | mediametrics::LogItem(mMetricsId) |
| 393 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_FLUSH) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 394 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 395 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 396 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 397 | .record(); }); |
| 398 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 399 | // Data will get flushed when the client receives the FLUSHED event. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 400 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 401 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 402 | return AAUDIO_OK; |
| 403 | } |
| 404 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 405 | // implement Runnable, periodically send timestamps to client |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 406 | __attribute__((no_sanitize("integer"))) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 407 | void AAudioServiceStreamBase::run() { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 408 | ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText()); |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 409 | // Hold onto the ref counted stream until the end. |
| 410 | android::sp<AAudioServiceStreamBase> holdStream(this); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 411 | TimestampScheduler timestampScheduler; |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 412 | // Balance the incStrong from when the thread was launched. |
| 413 | holdStream->decStrong(nullptr); |
| 414 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 415 | timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 416 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 417 | int64_t nextTime = timestampScheduler.nextAbsoluteTime(); |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 418 | int32_t loopCount = 0; |
Phil Burk | 45da1b7 | 2021-04-07 06:30:50 +0000 | [diff] [blame] | 419 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 420 | while(mThreadEnabled.load()) { |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 421 | loopCount++; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 422 | if (AudioClock::getNanoseconds() >= nextTime) { |
Phil Burk | 45da1b7 | 2021-04-07 06:30:50 +0000 | [diff] [blame] | 423 | result = sendCurrentTimestamp(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 424 | if (result != AAUDIO_OK) { |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 425 | ALOGE("%s() timestamp thread got result = %d", __func__, result); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 426 | break; |
| 427 | } |
| 428 | nextTime = timestampScheduler.nextAbsoluteTime(); |
| 429 | } else { |
| 430 | // Sleep until it is time to send the next timestamp. |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 431 | // TODO Wait for a signal with a timeout so that we can stop more quickly. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 432 | AudioClock::sleepUntilNanoTime(nextTime); |
| 433 | } |
| 434 | } |
Phil Burk | 45da1b7 | 2021-04-07 06:30:50 +0000 | [diff] [blame] | 435 | // This was moved from the calls in stop_l() and pause_l(), which could cause a deadlock |
| 436 | // if it resulted in a call to disconnect. |
| 437 | if (result == AAUDIO_OK) { |
| 438 | (void) sendCurrentTimestamp(); |
| 439 | } |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 440 | ALOGD("%s() %s exiting after %d loops <<<<<<<<<<<<<< TIMESTAMPS", |
| 441 | __func__, getTypeText(), loopCount); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 444 | void AAudioServiceStreamBase::disconnect() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 445 | std::lock_guard<std::mutex> lock(mLock); |
| 446 | disconnect_l(); |
| 447 | } |
| 448 | |
| 449 | void AAudioServiceStreamBase::disconnect_l() { |
Phil Burk | db46614 | 2021-04-16 16:50:00 +0000 | [diff] [blame] | 450 | if (!isDisconnected_l() && getState() != AAUDIO_STREAM_STATE_CLOSED) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 451 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 452 | mediametrics::LogItem(mMetricsId) |
| 453 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DISCONNECT) |
| 454 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 455 | .record(); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 456 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 457 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
Phil Burk | db46614 | 2021-04-16 16:50:00 +0000 | [diff] [blame] | 458 | setDisconnected_l(true); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 459 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 462 | aaudio_result_t AAudioServiceStreamBase::registerAudioThread(pid_t clientThreadId, |
| 463 | int priority) { |
| 464 | std::lock_guard<std::mutex> lock(mLock); |
| 465 | aaudio_result_t result = AAUDIO_OK; |
| 466 | if (getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 467 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
| 468 | result = AAUDIO_ERROR_INVALID_STATE; |
| 469 | } else { |
| 470 | const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review |
| 471 | setRegisteredThread(clientThreadId); |
| 472 | int err = android::requestPriority(ownerPid, clientThreadId, |
| 473 | priority, true /* isForApp */); |
| 474 | if (err != 0) { |
| 475 | ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d", |
| 476 | clientThreadId, errno, priority); |
| 477 | result = AAUDIO_ERROR_INTERNAL; |
| 478 | } |
| 479 | } |
| 480 | return result; |
| 481 | } |
| 482 | |
| 483 | aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread(pid_t clientThreadId) { |
| 484 | std::lock_guard<std::mutex> lock(mLock); |
| 485 | aaudio_result_t result = AAUDIO_OK; |
| 486 | if (getRegisteredThread() != clientThreadId) { |
| 487 | ALOGE("%s(), wrong thread", __func__); |
| 488 | result = AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 489 | } else { |
| 490 | setRegisteredThread(0); |
| 491 | } |
| 492 | return result; |
| 493 | } |
| 494 | |
| 495 | void AAudioServiceStreamBase::setState(aaudio_stream_state_t state) { |
| 496 | // CLOSED is a final state. |
| 497 | if (mState != AAUDIO_STREAM_STATE_CLOSED) { |
| 498 | mState = state; |
| 499 | } else { |
| 500 | ALOGW_IF(mState != state, "%s(%d) when already CLOSED", __func__, state); |
| 501 | } |
| 502 | } |
| 503 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 504 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 505 | double dataDouble) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 506 | AAudioServiceMessage command; |
| 507 | command.what = AAudioServiceMessage::code::EVENT; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 508 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 509 | command.event.dataDouble = dataDouble; |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 510 | return writeUpMessageQueue(&command); |
| 511 | } |
| 512 | |
| 513 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
| 514 | int64_t dataLong) { |
| 515 | AAudioServiceMessage command; |
| 516 | command.what = AAudioServiceMessage::code::EVENT; |
| 517 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 518 | command.event.dataLong = dataLong; |
| 519 | return writeUpMessageQueue(&command); |
| 520 | } |
| 521 | |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 522 | bool AAudioServiceStreamBase::isUpMessageQueueBusy() { |
| 523 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 524 | if (mUpMessageQueue == nullptr) { |
| 525 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
| 526 | return true; |
| 527 | } |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 528 | // Is it half full or more |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 529 | return mUpMessageQueue->getFractionalFullness() >= 0.5; |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 532 | aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 533 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 534 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 535 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 536 | return AAUDIO_ERROR_NULL; |
| 537 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 538 | int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1); |
| 539 | if (count != 1) { |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 540 | ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s", |
| 541 | __func__, command->what, getTypeText()); |
| 542 | setSuspended(true); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 543 | return AAUDIO_ERROR_WOULD_BLOCK; |
| 544 | } else { |
| 545 | return AAUDIO_OK; |
| 546 | } |
| 547 | } |
| 548 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 549 | aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) { |
| 550 | return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount); |
| 551 | } |
| 552 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 553 | aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() { |
| 554 | AAudioServiceMessage command; |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 555 | // It is not worth filling up the queue with timestamps. |
| 556 | // That can cause the stream to get suspended. |
| 557 | // So just drop the timestamp if the queue is getting full. |
| 558 | if (isUpMessageQueueBusy()) { |
| 559 | return AAUDIO_OK; |
| 560 | } |
| 561 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 562 | // Send a timestamp for the clock model. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 563 | aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position, |
| 564 | &command.timestamp.timestamp); |
| 565 | if (result == AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 566 | ALOGV("%s() SERVICE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 567 | (long long) command.timestamp.position, |
| 568 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 569 | command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 570 | result = writeUpMessageQueue(&command); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 571 | |
| 572 | if (result == AAUDIO_OK) { |
| 573 | // Send a hardware timestamp for presentation time. |
| 574 | result = getHardwareTimestamp(&command.timestamp.position, |
| 575 | &command.timestamp.timestamp); |
| 576 | if (result == AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 577 | ALOGV("%s() HARDWARE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 578 | (long long) command.timestamp.position, |
| 579 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 580 | command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE; |
| 581 | result = writeUpMessageQueue(&command); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 586 | if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 587 | result = AAUDIO_OK; // just not available yet, try again later |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 588 | } |
| 589 | return result; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 592 | /** |
| 593 | * Get an immutable description of the in-memory queues |
| 594 | * used to communicate with the underlying HAL or Service. |
| 595 | */ |
| 596 | aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 597 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 598 | { |
| 599 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 600 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 601 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 602 | return AAUDIO_ERROR_NULL; |
| 603 | } |
| 604 | // Gather information on the message queue. |
| 605 | mUpMessageQueue->fillParcelable(parcelable, |
| 606 | parcelable.mUpMessageQueueParcelable); |
| 607 | } |
| 608 | return getAudioDataDescription(parcelable); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 609 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 610 | |
| 611 | void AAudioServiceStreamBase::onVolumeChanged(float volume) { |
| 612 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 613 | } |