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