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