blob: 35d712c2e85ca0292a2de410359f29e356744750 [file] [log] [blame]
Phil Burk2355edb2016-12-26 13:54:02 -08001/*
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 Laurentcb4dae22017-07-01 19:39:32 -070017#define LOG_TAG "AAudioServiceStreamBase"
Phil Burk2355edb2016-12-26 13:54:02 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burka5222e22017-07-28 13:31:14 -070021#include <iomanip>
22#include <iostream>
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include <mutex>
Phil Burk2355edb2016-12-26 13:54:02 -080024
Phil Burka9876702020-04-20 18:16:15 -070025#include <media/MediaMetricsItem.h>
26#include <media/TypeConverter.h>
Phil Burk7ebbc112020-05-13 15:55:17 -070027#include <mediautils/SchedulingPolicyService.h>
Phil Burka9876702020-04-20 18:16:15 -070028
Phil Burkc0c70e32017-02-09 13:18:38 -080029#include "binding/AAudioServiceMessage.h"
Phil Burka9876702020-04-20 18:16:15 -070030#include "core/AudioGlobal.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080031#include "utility/AudioClock.h"
32
Phil Burk39f02dd2017-08-04 09:13:31 -070033#include "AAudioEndpointManager.h"
34#include "AAudioService.h"
35#include "AAudioServiceEndpoint.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080036#include "AAudioServiceStreamBase.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080037
38using namespace android; // TODO just import names needed
39using namespace aaudio; // TODO just import names needed
Phil Burk2355edb2016-12-26 13:54:02 -080040
Svet Ganov33761132021-05-13 22:51:08 +000041using content::AttributionSourceState;
Philip P. Moltmannbda45752020-07-17 16:41:18 -070042
jiabin2a594622021-10-14 00:32:25 +000043static const int64_t TIMEOUT_NANOS = 3LL * 1000 * 1000 * 1000;
jiabinf7f06152021-11-22 18:10:14 +000044// If the stream is idle for more than `IDLE_TIMEOUT_NANOS`, the stream will be put into standby.
45static const int64_t IDLE_TIMEOUT_NANOS = 3e9;
jiabin2a594622021-10-14 00:32:25 +000046
Phil Burk2355edb2016-12-26 13:54:02 -080047/**
Phil Burkc0c70e32017-02-09 13:18:38 -080048 * Base class for streams in the service.
49 * @return
Phil Burk2355edb2016-12-26 13:54:02 -080050 */
51
Phil Burk39f02dd2017-08-04 09:13:31 -070052AAudioServiceStreamBase::AAudioServiceStreamBase(AAudioService &audioService)
jiabin2a594622021-10-14 00:32:25 +000053 : mCommandThread("AACommand")
Phil Burka53ffa62018-10-10 16:21:37 -070054 , mAtomicStreamTimestamp()
Phil Burk39f02dd2017-08-04 09:13:31 -070055 , mAudioService(audioService) {
Svet Ganov33761132021-05-13 22:51:08 +000056 mMmapClient.attributionSource = AttributionSourceState();
Phil Burk2355edb2016-12-26 13:54:02 -080057}
58
Phil Burk5ed503c2017-02-01 09:38:15 -080059AAudioServiceStreamBase::~AAudioServiceStreamBase() {
Phil Burk8f4fe502020-07-15 23:54:50 +000060 ALOGD("%s() called", __func__);
61
Phil Burka9876702020-04-20 18:16:15 -070062 // 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 Burk5a26e662017-07-07 12:44:48 -070070 // 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 Burkbcc36742017-08-31 17:24:51 -070072 LOG_ALWAYS_FATAL_IF(!(getState() == AAUDIO_STREAM_STATE_CLOSED
Phil Burkdb466142021-04-16 16:50:00 +000073 || getState() == AAUDIO_STREAM_STATE_UNINITIALIZED),
Phil Burk8b4e05e2019-12-17 12:12:09 -080074 "service stream %p still open, state = %d",
75 this, getState());
jiabin2a594622021-10-14 00:32:25 +000076
77 // Stop the command thread before destroying.
78 if (mThreadEnabled) {
79 mThreadEnabled = false;
80 mCommandQueue.stopWaiting();
81 mCommandThread.stop();
82 }
Phil Burk2355edb2016-12-26 13:54:02 -080083}
84
Phil Burka5222e22017-07-28 13:31:14 -070085std::string AAudioServiceStreamBase::dumpHeader() {
jiabina9094092021-06-28 20:36:45 +000086 return std::string(
Robert Wu310037a2022-09-06 21:48:18 +000087 " T Handle UId Port Run State Format Burst Chan Mask Capacity"
88 " HwFormat HwChan HwRate");
Phil Burka5222e22017-07-28 13:31:14 -070089}
90
Phil Burk4501b352017-06-29 18:12:36 -070091std::string AAudioServiceStreamBase::dump() const {
92 std::stringstream result;
93
Phil Burka5222e22017-07-28 13:31:14 -070094 result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle
95 << std::dec << std::setfill(' ') ;
Svet Ganov33761132021-05-13 22:51:08 +000096 result << std::setw(6) << mMmapClient.attributionSource.uid;
Phil Burkbbd52862018-04-13 11:37:42 -070097 result << std::setw(7) << mClientHandle;
Phil Burka5222e22017-07-28 13:31:14 -070098 result << std::setw(4) << (isRunning() ? "yes" : " no");
Phil Burkbcc36742017-08-31 17:24:51 -070099 result << std::setw(6) << getState();
Phil Burk39f02dd2017-08-04 09:13:31 -0700100 result << std::setw(7) << getFormat();
Phil Burka5222e22017-07-28 13:31:14 -0700101 result << std::setw(6) << mFramesPerBurst;
Phil Burk39f02dd2017-08-04 09:13:31 -0700102 result << std::setw(5) << getSamplesPerFrame();
jiabina9094092021-06-28 20:36:45 +0000103 result << std::setw(8) << std::hex << getChannelMask() << std::dec;
Phil Burk39f02dd2017-08-04 09:13:31 -0700104 result << std::setw(9) << getBufferCapacity();
Robert Wu310037a2022-09-06 21:48:18 +0000105 result << std::setw(9) << getHardwareFormat();
106 result << std::setw(7) << getHardwareSamplesPerFrame();
107 result << std::setw(7) << getHardwareSampleRate();
Phil Burk4501b352017-06-29 18:12:36 -0700108
109 return result.str();
110}
111
Phil Burka9876702020-04-20 18:16:15 -0700112void AAudioServiceStreamBase::logOpen(aaudio_handle_t streamHandle) {
113 // This is the first log sent from the AAudio Service for a stream.
114 mMetricsId = std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_STREAM)
115 + std::to_string(streamHandle);
116
117 audio_attributes_t attributes = AAudioServiceEndpoint::getAudioAttributesFrom(this);
118
119 // Once this item is logged by the server, the client with the same PID, UID
120 // can also log properties.
121 mediametrics::LogItem(mMetricsId)
122 .setPid(getOwnerProcessId())
123 .setUid(getOwnerUserId())
Andy Hungd203eb62020-04-27 09:12:46 -0700124 .set(AMEDIAMETRICS_PROP_ALLOWUID, (int32_t)getOwnerUserId())
Phil Burka9876702020-04-20 18:16:15 -0700125 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_OPEN)
126 // the following are immutable
127 .set(AMEDIAMETRICS_PROP_BUFFERCAPACITYFRAMES, (int32_t)getBufferCapacity())
128 .set(AMEDIAMETRICS_PROP_BURSTFRAMES, (int32_t)getFramesPerBurst())
129 .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)getSamplesPerFrame())
130 .set(AMEDIAMETRICS_PROP_CONTENTTYPE, toString(attributes.content_type).c_str())
131 .set(AMEDIAMETRICS_PROP_DIRECTION,
132 AudioGlobal_convertDirectionToText(getDirection()))
133 .set(AMEDIAMETRICS_PROP_ENCODING, toString(getFormat()).c_str())
134 .set(AMEDIAMETRICS_PROP_ROUTEDDEVICEID, (int32_t)getDeviceId())
135 .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)getSampleRate())
136 .set(AMEDIAMETRICS_PROP_SESSIONID, (int32_t)getSessionId())
137 .set(AMEDIAMETRICS_PROP_SOURCE, toString(attributes.source).c_str())
138 .set(AMEDIAMETRICS_PROP_USAGE, toString(attributes.usage).c_str())
139 .record();
140}
141
Phil Burk15f97c92018-09-04 14:06:27 -0700142aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700143 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
144 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700145
Svet Ganov33761132021-05-13 22:51:08 +0000146 mMmapClient.attributionSource = request.getAttributionSource();
147 // TODO b/182392769: use attribution source util
148 mMmapClient.attributionSource.uid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700149 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
Svet Ganov33761132021-05-13 22:51:08 +0000150 mMmapClient.attributionSource.pid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700151 legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
Eric Laurentcb4dae22017-07-01 19:39:32 -0700152
Phil Burk39f02dd2017-08-04 09:13:31 -0700153 // Limit scope of lock to avoid recursive lock in close().
154 {
155 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
156 if (mUpMessageQueue != nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700157 ALOGE("%s() called twice", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700158 return AAUDIO_ERROR_INVALID_STATE;
159 }
160
Phil Burk8f4fe502020-07-15 23:54:50 +0000161 mUpMessageQueue = std::make_shared<SharedRingBuffer>();
Phil Burk39f02dd2017-08-04 09:13:31 -0700162 result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage),
163 QUEUE_UP_CAPACITY_COMMANDS);
164 if (result != AAUDIO_OK) {
165 goto error;
166 }
167
Phil Burk6e2770e2018-05-01 13:03:52 -0700168 // This is not protected by a lock because the stream cannot be
169 // referenced until the service returns a handle to the client.
170 // So only one thread can open a stream.
Phil Burk39f02dd2017-08-04 09:13:31 -0700171 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService,
Phil Burk15f97c92018-09-04 14:06:27 -0700172 request);
Phil Burk39f02dd2017-08-04 09:13:31 -0700173 if (mServiceEndpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700174 result = AAUDIO_ERROR_UNAVAILABLE;
175 goto error;
176 }
Phil Burk6e2770e2018-05-01 13:03:52 -0700177 // Save a weak pointer that we will use to access the endpoint.
178 mServiceEndpointWeak = mServiceEndpoint;
179
Phil Burk39f02dd2017-08-04 09:13:31 -0700180 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
181 copyFrom(*mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800182 }
jiabin2a594622021-10-14 00:32:25 +0000183
184 // Make sure this object does not get deleted before the run() method
185 // can protect it by making a strong pointer.
jiabinba75f212021-12-07 20:06:30 +0000186 mCommandQueue.startWaiting();
jiabin2a594622021-10-14 00:32:25 +0000187 mThreadEnabled = true;
188 incStrong(nullptr); // See run() method.
189 result = mCommandThread.start(this);
190 if (result != AAUDIO_OK) {
191 decStrong(nullptr); // run() can't do it so we have to do it here.
192 goto error;
193 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700194 return result;
195
196error:
jiabinba75f212021-12-07 20:06:30 +0000197 closeAndClear();
198 mThreadEnabled = false;
199 mCommandQueue.stopWaiting();
200 mCommandThread.stop();
Phil Burk39f02dd2017-08-04 09:13:31 -0700201 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800202}
Phil Burkdec33ab2017-01-17 14:48:16 -0800203
Phil Burkc0c70e32017-02-09 13:18:38 -0800204aaudio_result_t AAudioServiceStreamBase::close() {
jiabinba75f212021-12-07 20:06:30 +0000205 aaudio_result_t result = sendCommand(CLOSE, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000206
207 // Stop the command thread as the stream is closed.
208 mThreadEnabled = false;
209 mCommandQueue.stopWaiting();
210 mCommandThread.stop();
211
212 return result;
Phil Burk7ebbc112020-05-13 15:55:17 -0700213}
214
215aaudio_result_t AAudioServiceStreamBase::close_l() {
Phil Burkbcc36742017-08-31 17:24:51 -0700216 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700217 return AAUDIO_OK;
218 }
219
jiabin2a594622021-10-14 00:32:25 +0000220 // This will stop the stream, just in case it was not already stopped.
Phil Burk7ebbc112020-05-13 15:55:17 -0700221 stop_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700222
jiabinba75f212021-12-07 20:06:30 +0000223 return closeAndClear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800224}
225
Phil Burkbcc36742017-08-31 17:24:51 -0700226aaudio_result_t AAudioServiceStreamBase::startDevice() {
227 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burk6e2770e2018-05-01 13:03:52 -0700228 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
229 if (endpoint == nullptr) {
230 ALOGE("%s() has no endpoint", __func__);
231 return AAUDIO_ERROR_INVALID_STATE;
232 }
233 return endpoint->startStream(this, &mClientHandle);
Phil Burkbcc36742017-08-31 17:24:51 -0700234}
235
Phil Burk39f02dd2017-08-04 09:13:31 -0700236/**
237 * Start the flow of audio data.
238 *
239 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
240 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800241aaudio_result_t AAudioServiceStreamBase::start() {
jiabinba75f212021-12-07 20:06:30 +0000242 return sendCommand(START, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000243}
Phil Burk7ebbc112020-05-13 15:55:17 -0700244
jiabin2a594622021-10-14 00:32:25 +0000245aaudio_result_t AAudioServiceStreamBase::start_l() {
Phil Burka9876702020-04-20 18:16:15 -0700246 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burkbcc36742017-08-31 17:24:51 -0700247 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700248
Phil Burk7ebbc112020-05-13 15:55:17 -0700249 if (auto state = getState();
Phil Burkdb466142021-04-16 16:50:00 +0000250 state == AAUDIO_STREAM_STATE_CLOSED || isDisconnected_l()) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700251 ALOGW("%s() already CLOSED, returns INVALID_STATE, handle = %d",
252 __func__, getHandle());
253 return AAUDIO_ERROR_INVALID_STATE;
254 }
255
jiabinf7f06152021-11-22 18:10:14 +0000256 if (mStandby) {
257 ALOGW("%s() the stream is standby, return ERROR_STANDBY, "
258 "expecting the client call exitStandby before start", __func__);
259 return AAUDIO_ERROR_STANDBY;
260 }
261
Phil Burka9876702020-04-20 18:16:15 -0700262 mediametrics::Defer defer([&] {
263 mediametrics::LogItem(mMetricsId)
264 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START)
Andy Hungea840382020-05-05 21:50:17 -0700265 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700266 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
267 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
268 .record(); });
269
Eric Laurentcb4dae22017-07-01 19:39:32 -0700270 if (isRunning()) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700271 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700272 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700273
Phil Burk23296382017-11-20 15:45:11 -0800274 setFlowing(false);
Phil Burk762365c2018-12-10 16:02:16 -0800275 setSuspended(false);
Phil Burk23296382017-11-20 15:45:11 -0800276
Phil Burkbcc36742017-08-31 17:24:51 -0700277 // Start with fresh presentation timestamps.
Phil Burka53ffa62018-10-10 16:21:37 -0700278 mAtomicStreamTimestamp.clear();
Phil Burkbcc36742017-08-31 17:24:51 -0700279
Phil Burk39f02dd2017-08-04 09:13:31 -0700280 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burkbcc36742017-08-31 17:24:51 -0700281 result = startDevice();
282 if (result != AAUDIO_OK) goto error;
283
284 // This should happen at the end of the start.
Vlad Popaec1788e2022-08-04 11:23:30 +0200285 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED, static_cast<int64_t>(mClientHandle));
Phil Burkbcc36742017-08-31 17:24:51 -0700286 setState(AAUDIO_STREAM_STATE_STARTED);
Phil Burkbcc36742017-08-31 17:24:51 -0700287
288 return result;
289
290error:
Phil Burk7ebbc112020-05-13 15:55:17 -0700291 disconnect_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700292 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800293}
294
295aaudio_result_t AAudioServiceStreamBase::pause() {
jiabinba75f212021-12-07 20:06:30 +0000296 return sendCommand(PAUSE, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
Phil Burk7ebbc112020-05-13 15:55:17 -0700297}
298
299aaudio_result_t AAudioServiceStreamBase::pause_l() {
Phil Burk11e8d332017-05-24 09:59:02 -0700300 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700301 if (!isRunning()) {
302 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800303 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700304 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk73af62a2017-10-26 12:11:47 -0700305
Phil Burka9876702020-04-20 18:16:15 -0700306 mediametrics::Defer defer([&] {
307 mediametrics::LogItem(mMetricsId)
308 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_PAUSE)
Andy Hungea840382020-05-05 21:50:17 -0700309 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700310 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
311 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
312 .record(); });
313
Phil Burk6e2770e2018-05-01 13:03:52 -0700314 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
315 if (endpoint == nullptr) {
316 ALOGE("%s() has no endpoint", __func__);
Phil Burka9876702020-04-20 18:16:15 -0700317 result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking
318 return result;
Phil Burk6e2770e2018-05-01 13:03:52 -0700319 }
320 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700321 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700322 ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText());
Phil Burk7ebbc112020-05-13 15:55:17 -0700323 disconnect_l(); // TODO should we return or pause Base first?
Phil Burk39f02dd2017-08-04 09:13:31 -0700324 }
325
Eric Laurentcb4dae22017-07-01 19:39:32 -0700326 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkbcc36742017-08-31 17:24:51 -0700327 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800328 return result;
329}
330
Phil Burk71f35bb2017-04-13 16:05:07 -0700331aaudio_result_t AAudioServiceStreamBase::stop() {
jiabinba75f212021-12-07 20:06:30 +0000332 return sendCommand(STOP, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
Phil Burk7ebbc112020-05-13 15:55:17 -0700333}
334
335aaudio_result_t AAudioServiceStreamBase::stop_l() {
Phil Burk11e8d332017-05-24 09:59:02 -0700336 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700337 if (!isRunning()) {
338 return result;
Phil Burk71f35bb2017-04-13 16:05:07 -0700339 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700340 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700341
Phil Burka9876702020-04-20 18:16:15 -0700342 mediametrics::Defer defer([&] {
343 mediametrics::LogItem(mMetricsId)
344 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP)
Andy Hungea840382020-05-05 21:50:17 -0700345 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700346 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
347 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
348 .record(); });
349
Phil Burk83fb8442017-10-05 16:55:17 -0700350 setState(AAUDIO_STREAM_STATE_STOPPING);
351
Eric Laurentcb4dae22017-07-01 19:39:32 -0700352 if (result != AAUDIO_OK) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700353 disconnect_l();
Eric Laurentcb4dae22017-07-01 19:39:32 -0700354 return result;
355 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700356
Phil Burk6e2770e2018-05-01 13:03:52 -0700357 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
358 if (endpoint == nullptr) {
359 ALOGE("%s() has no endpoint", __func__);
Phil Burka9876702020-04-20 18:16:15 -0700360 result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking
361 return result;
Phil Burk6e2770e2018-05-01 13:03:52 -0700362 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700363 // TODO wait for data to be played out
Phil Burk6e2770e2018-05-01 13:03:52 -0700364 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700365 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700366 ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText());
Phil Burk7ebbc112020-05-13 15:55:17 -0700367 disconnect_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700368 // TODO what to do with result here?
369 }
370
Eric Laurentcb4dae22017-07-01 19:39:32 -0700371 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burkbcc36742017-08-31 17:24:51 -0700372 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700373 return result;
374}
375
jiabin2a594622021-10-14 00:32:25 +0000376aaudio_result_t AAudioServiceStreamBase::flush() {
jiabinba75f212021-12-07 20:06:30 +0000377 return sendCommand(FLUSH, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
Phil Burk98d6d922017-07-06 11:52:45 -0700378}
379
jiabin2a594622021-10-14 00:32:25 +0000380aaudio_result_t AAudioServiceStreamBase::flush_l() {
Phil Burk5cc83c32017-11-28 15:43:18 -0800381 aaudio_result_t result = AAudio_isFlushAllowed(getState());
382 if (result != AAUDIO_OK) {
383 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700384 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700385 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk5cc83c32017-11-28 15:43:18 -0800386
Phil Burka9876702020-04-20 18:16:15 -0700387 mediametrics::Defer defer([&] {
388 mediametrics::LogItem(mMetricsId)
389 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_FLUSH)
Andy Hungea840382020-05-05 21:50:17 -0700390 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700391 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
392 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
393 .record(); });
394
Phil Burk39f02dd2017-08-04 09:13:31 -0700395 // Data will get flushed when the client receives the FLUSHED event.
Phil Burk71f35bb2017-04-13 16:05:07 -0700396 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
Phil Burkbcc36742017-08-31 17:24:51 -0700397 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700398 return AAUDIO_OK;
399}
400
jiabin2a594622021-10-14 00:32:25 +0000401// implement Runnable, periodically send timestamps to client and process commands from queue.
Phil Burka53ffa62018-10-10 16:21:37 -0700402__attribute__((no_sanitize("integer")))
Phil Burkc0c70e32017-02-09 13:18:38 -0800403void AAudioServiceStreamBase::run() {
jiabin2a594622021-10-14 00:32:25 +0000404 ALOGD("%s() %s entering >>>>>>>>>>>>>> COMMANDS", __func__, getTypeText());
Phil Burk3d201942021-04-08 23:27:04 +0000405 // Hold onto the ref counted stream until the end.
406 android::sp<AAudioServiceStreamBase> holdStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800407 TimestampScheduler timestampScheduler;
jiabin2a594622021-10-14 00:32:25 +0000408 int64_t nextTime;
jiabinf7f06152021-11-22 18:10:14 +0000409 int64_t standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
Phil Burk3d201942021-04-08 23:27:04 +0000410 // Balance the incStrong from when the thread was launched.
411 holdStream->decStrong(nullptr);
412
jiabin2a594622021-10-14 00:32:25 +0000413 // Taking mLock while starting the thread. All the operation must be able to
414 // run with holding the lock.
415 std::scoped_lock<std::mutex> _l(mLock);
416
Phil Burka53ffa62018-10-10 16:21:37 -0700417 int32_t loopCount = 0;
jiabin2a594622021-10-14 00:32:25 +0000418 while (mThreadEnabled.load()) {
Phil Burka53ffa62018-10-10 16:21:37 -0700419 loopCount++;
jiabin2a594622021-10-14 00:32:25 +0000420 int64_t timeoutNanos = -1;
jiabinf7f06152021-11-22 18:10:14 +0000421 if (isRunning() || (isIdle_l() && !isStandby_l())) {
422 timeoutNanos = (isRunning() ? nextTime : standbyTime) - AudioClock::getNanoseconds();
jiabin2a594622021-10-14 00:32:25 +0000423 timeoutNanos = std::max<int64_t>(0, timeoutNanos);
424 }
425
426 auto command = mCommandQueue.waitForCommand(timeoutNanos);
427 if (!mThreadEnabled) {
428 // Break the loop if the thread is disabled.
429 break;
430 }
431
432 if (isRunning() && AudioClock::getNanoseconds() >= nextTime) {
433 // It is time to update timestamp.
434 if (sendCurrentTimestamp_l() != AAUDIO_OK) {
435 ALOGE("Failed to send current timestamp, stop updating timestamp");
436 disconnect_l();
437 } else {
438 nextTime = timestampScheduler.nextAbsoluteTime();
Phil Burkc0c70e32017-02-09 13:18:38 -0800439 }
jiabin2a594622021-10-14 00:32:25 +0000440 }
jiabinf7f06152021-11-22 18:10:14 +0000441 if (isIdle_l() && AudioClock::getNanoseconds() >= standbyTime) {
Jason Lin20d04e32022-11-22 13:39:49 +0800442 aaudio_result_t result = standby_l();
443 if (result != AAUDIO_OK) {
444 // If standby failed because of the function is not implemented, there is no
445 // need to retry. Otherwise, retry standby later.
446 ALOGW("Failed to enter standby, error=%d", result);
447 standbyTime = result == AAUDIO_ERROR_UNIMPLEMENTED
448 ? std::numeric_limits<int64_t>::max()
449 : AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
450 }
jiabinf7f06152021-11-22 18:10:14 +0000451 }
jiabin2a594622021-10-14 00:32:25 +0000452
453 if (command != nullptr) {
454 std::scoped_lock<std::mutex> _commandLock(command->lock);
455 switch (command->operationCode) {
456 case START:
457 command->result = start_l();
458 timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate());
459 timestampScheduler.start(AudioClock::getNanoseconds());
460 nextTime = timestampScheduler.nextAbsoluteTime();
461 break;
462 case PAUSE:
463 command->result = pause_l();
jiabinf7f06152021-11-22 18:10:14 +0000464 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
jiabin2a594622021-10-14 00:32:25 +0000465 break;
466 case STOP:
467 command->result = stop_l();
jiabinf7f06152021-11-22 18:10:14 +0000468 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
jiabin2a594622021-10-14 00:32:25 +0000469 break;
470 case FLUSH:
471 command->result = flush_l();
472 break;
473 case CLOSE:
474 command->result = close_l();
475 break;
476 case DISCONNECT:
477 disconnect_l();
478 break;
479 case REGISTER_AUDIO_THREAD: {
480 RegisterAudioThreadParam *param =
481 (RegisterAudioThreadParam *) command->parameter.get();
482 command->result =
483 param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
484 : registerAudioThread_l(param->mOwnerPid,
485 param->mClientThreadId,
486 param->mPriority);
487 }
488 break;
489 case UNREGISTER_AUDIO_THREAD: {
490 UnregisterAudioThreadParam *param =
491 (UnregisterAudioThreadParam *) command->parameter.get();
492 command->result =
493 param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
494 : unregisterAudioThread_l(param->mClientThreadId);
495 }
496 break;
497 case GET_DESCRIPTION: {
498 GetDescriptionParam *param = (GetDescriptionParam *) command->parameter.get();
499 command->result = param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
500 : getDescription_l(param->mParcelable);
501 }
502 break;
jiabinf7f06152021-11-22 18:10:14 +0000503 case EXIT_STANDBY: {
504 ExitStandbyParam *param = (ExitStandbyParam *) command->parameter.get();
505 command->result = param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
506 : exitStandby_l(param->mParcelable);
507 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
508 } break;
jiabin2a594622021-10-14 00:32:25 +0000509 default:
510 ALOGE("Invalid command op code: %d", command->operationCode);
511 break;
512 }
513 if (command->isWaitingForReply) {
514 command->isWaitingForReply = false;
515 command->conditionVariable.notify_one();
516 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800517 }
518 }
jiabin2a594622021-10-14 00:32:25 +0000519 ALOGD("%s() %s exiting after %d loops <<<<<<<<<<<<<< COMMANDS",
Phil Burka53ffa62018-10-10 16:21:37 -0700520 __func__, getTypeText(), loopCount);
Phil Burkc0c70e32017-02-09 13:18:38 -0800521}
522
Phil Burk5ef003b2017-06-30 11:43:37 -0700523void AAudioServiceStreamBase::disconnect() {
jiabinba75f212021-12-07 20:06:30 +0000524 sendCommand(DISCONNECT);
Phil Burk7ebbc112020-05-13 15:55:17 -0700525}
526
527void AAudioServiceStreamBase::disconnect_l() {
Phil Burkdb466142021-04-16 16:50:00 +0000528 if (!isDisconnected_l() && getState() != AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700529
Phil Burka9876702020-04-20 18:16:15 -0700530 mediametrics::LogItem(mMetricsId)
531 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DISCONNECT)
532 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
533 .record();
Phil Burk7ebbc112020-05-13 15:55:17 -0700534
Phil Burk5ef003b2017-06-30 11:43:37 -0700535 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkdb466142021-04-16 16:50:00 +0000536 setDisconnected_l(true);
Phil Burk5ef003b2017-06-30 11:43:37 -0700537 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800538}
539
jiabin2a594622021-10-14 00:32:25 +0000540aaudio_result_t AAudioServiceStreamBase::registerAudioThread(pid_t clientThreadId, int priority) {
541 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
jiabinba75f212021-12-07 20:06:30 +0000542 return sendCommand(REGISTER_AUDIO_THREAD,
jiabin2a594622021-10-14 00:32:25 +0000543 std::make_shared<RegisterAudioThreadParam>(ownerPid, clientThreadId, priority),
544 true /*waitForReply*/,
545 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000546}
547
548aaudio_result_t AAudioServiceStreamBase::registerAudioThread_l(
549 pid_t ownerPid, pid_t clientThreadId, int priority) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700550 aaudio_result_t result = AAUDIO_OK;
551 if (getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
552 ALOGE("AAudioService::registerAudioThread(), thread already registered");
553 result = AAUDIO_ERROR_INVALID_STATE;
554 } else {
Phil Burk7ebbc112020-05-13 15:55:17 -0700555 setRegisteredThread(clientThreadId);
556 int err = android::requestPriority(ownerPid, clientThreadId,
557 priority, true /* isForApp */);
558 if (err != 0) {
559 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
560 clientThreadId, errno, priority);
561 result = AAUDIO_ERROR_INTERNAL;
562 }
563 }
564 return result;
565}
566
567aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread(pid_t clientThreadId) {
jiabinba75f212021-12-07 20:06:30 +0000568 return sendCommand(UNREGISTER_AUDIO_THREAD,
jiabin2a594622021-10-14 00:32:25 +0000569 std::make_shared<UnregisterAudioThreadParam>(clientThreadId),
570 true /*waitForReply*/,
571 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000572}
573
574aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread_l(pid_t clientThreadId) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700575 aaudio_result_t result = AAUDIO_OK;
576 if (getRegisteredThread() != clientThreadId) {
577 ALOGE("%s(), wrong thread", __func__);
578 result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
579 } else {
580 setRegisteredThread(0);
581 }
582 return result;
583}
584
585void AAudioServiceStreamBase::setState(aaudio_stream_state_t state) {
586 // CLOSED is a final state.
587 if (mState != AAUDIO_STREAM_STATE_CLOSED) {
588 mState = state;
589 } else {
590 ALOGW_IF(mState != state, "%s(%d) when already CLOSED", __func__, state);
591 }
592}
593
Phil Burkc0c70e32017-02-09 13:18:38 -0800594aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800595 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800596 AAudioServiceMessage command;
597 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800598 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800599 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800600 return writeUpMessageQueue(&command);
601}
602
603aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
604 int64_t dataLong) {
605 AAudioServiceMessage command;
606 command.what = AAudioServiceMessage::code::EVENT;
607 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800608 command.event.dataLong = dataLong;
609 return writeUpMessageQueue(&command);
610}
611
Phil Burkf878a8d2019-03-29 17:23:00 -0700612bool AAudioServiceStreamBase::isUpMessageQueueBusy() {
613 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
614 if (mUpMessageQueue == nullptr) {
615 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
616 return true;
617 }
Phil Burkf878a8d2019-03-29 17:23:00 -0700618 // Is it half full or more
Phil Burk8f4fe502020-07-15 23:54:50 +0000619 return mUpMessageQueue->getFractionalFullness() >= 0.5;
Phil Burkf878a8d2019-03-29 17:23:00 -0700620}
621
Phil Burkc0c70e32017-02-09 13:18:38 -0800622aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700623 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700624 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700625 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700626 return AAUDIO_ERROR_NULL;
627 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800628 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
629 if (count != 1) {
Phil Burk762365c2018-12-10 16:02:16 -0800630 ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s",
631 __func__, command->what, getTypeText());
632 setSuspended(true);
Phil Burkc0c70e32017-02-09 13:18:38 -0800633 return AAUDIO_ERROR_WOULD_BLOCK;
634 } else {
635 return AAUDIO_OK;
636 }
637}
638
Phil Burk23296382017-11-20 15:45:11 -0800639aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
640 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
641}
642
jiabin2a594622021-10-14 00:32:25 +0000643aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800644 AAudioServiceMessage command;
Phil Burkf878a8d2019-03-29 17:23:00 -0700645 // It is not worth filling up the queue with timestamps.
646 // That can cause the stream to get suspended.
647 // So just drop the timestamp if the queue is getting full.
648 if (isUpMessageQueueBusy()) {
649 return AAUDIO_OK;
650 }
651
Phil Burk97350f92017-07-21 15:59:44 -0700652 // Send a timestamp for the clock model.
jiabin2a594622021-10-14 00:32:25 +0000653 aaudio_result_t result = getFreeRunningPosition_l(&command.timestamp.position,
654 &command.timestamp.timestamp);
Phil Burkc0c70e32017-02-09 13:18:38 -0800655 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700656 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700657 (long long) command.timestamp.position,
658 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700659 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800660 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700661
662 if (result == AAUDIO_OK) {
663 // Send a hardware timestamp for presentation time.
jiabin2a594622021-10-14 00:32:25 +0000664 result = getHardwareTimestamp_l(&command.timestamp.position,
665 &command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700666 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700667 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700668 (long long) command.timestamp.position,
669 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700670 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
671 result = writeUpMessageQueue(&command);
672 }
673 }
674 }
675
Phil Burkbcc36742017-08-31 17:24:51 -0700676 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700677 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800678 }
679 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800680}
681
Phil Burkc0c70e32017-02-09 13:18:38 -0800682/**
683 * Get an immutable description of the in-memory queues
684 * used to communicate with the underlying HAL or Service.
685 */
686aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
jiabinba75f212021-12-07 20:06:30 +0000687 return sendCommand(
jiabin2a594622021-10-14 00:32:25 +0000688 GET_DESCRIPTION,
689 std::make_shared<GetDescriptionParam>(&parcelable),
690 true /*waitForReply*/,
691 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000692}
693
694aaudio_result_t AAudioServiceStreamBase::getDescription_l(AudioEndpointParcelable* parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700695 {
696 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
697 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700698 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700699 return AAUDIO_ERROR_NULL;
700 }
701 // Gather information on the message queue.
702 mUpMessageQueue->fillParcelable(parcelable,
jiabin2a594622021-10-14 00:32:25 +0000703 parcelable->mUpMessageQueueParcelable);
Phil Burk523b3042017-09-13 13:03:08 -0700704 }
jiabin2a594622021-10-14 00:32:25 +0000705 return getAudioDataDescription_l(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700706}
Phil Burk39f02dd2017-08-04 09:13:31 -0700707
jiabinf7f06152021-11-22 18:10:14 +0000708aaudio_result_t AAudioServiceStreamBase::exitStandby(AudioEndpointParcelable *parcelable) {
709 auto command = std::make_shared<AAudioCommand>(
710 EXIT_STANDBY,
711 std::make_shared<ExitStandbyParam>(parcelable),
712 true /*waitForReply*/,
713 TIMEOUT_NANOS);
714 return mCommandQueue.sendCommand(command);
715}
716
Phil Burk39f02dd2017-08-04 09:13:31 -0700717void AAudioServiceStreamBase::onVolumeChanged(float volume) {
718 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
719}
jiabinba75f212021-12-07 20:06:30 +0000720
721aaudio_result_t AAudioServiceStreamBase::sendCommand(aaudio_command_opcode opCode,
722 std::shared_ptr<AAudioCommandParam> param,
723 bool waitForReply,
724 int64_t timeoutNanos) {
725 return mCommandQueue.sendCommand(std::make_shared<AAudioCommand>(
726 opCode, param, waitForReply, timeoutNanos));
727}
728
729aaudio_result_t AAudioServiceStreamBase::closeAndClear() {
730 aaudio_result_t result = AAUDIO_OK;
731 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
732 if (endpoint == nullptr) {
733 result = AAUDIO_ERROR_INVALID_STATE;
734 } else {
735 endpoint->unregisterStream(this);
736 AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance();
737 endpointManager.closeEndpoint(endpoint);
738
739 // AAudioService::closeStream() prevents two threads from closing at the same time.
740 mServiceEndpoint.clear(); // endpoint will hold the pointer after this method returns.
741 }
742
743 setState(AAUDIO_STREAM_STATE_CLOSED);
744
745 mediametrics::LogItem(mMetricsId)
746 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CLOSE)
747 .record();
748 return result;
749}