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