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