blob: d395c253007fad3a969e473436b28bf4e1dca9ca [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(
87 " T Handle UId Port Run State Format Burst Chan Mask Capacity");
Phil Burka5222e22017-07-28 13:31:14 -070088}
89
Phil Burk4501b352017-06-29 18:12:36 -070090std::string AAudioServiceStreamBase::dump() const {
91 std::stringstream result;
92
Phil Burka5222e22017-07-28 13:31:14 -070093 result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle
94 << std::dec << std::setfill(' ') ;
Svet Ganov33761132021-05-13 22:51:08 +000095 result << std::setw(6) << mMmapClient.attributionSource.uid;
Phil Burkbbd52862018-04-13 11:37:42 -070096 result << std::setw(7) << mClientHandle;
Phil Burka5222e22017-07-28 13:31:14 -070097 result << std::setw(4) << (isRunning() ? "yes" : " no");
Phil Burkbcc36742017-08-31 17:24:51 -070098 result << std::setw(6) << getState();
Phil Burk39f02dd2017-08-04 09:13:31 -070099 result << std::setw(7) << getFormat();
Phil Burka5222e22017-07-28 13:31:14 -0700100 result << std::setw(6) << mFramesPerBurst;
Phil Burk39f02dd2017-08-04 09:13:31 -0700101 result << std::setw(5) << getSamplesPerFrame();
jiabina9094092021-06-28 20:36:45 +0000102 result << std::setw(8) << std::hex << getChannelMask() << std::dec;
Phil Burk39f02dd2017-08-04 09:13:31 -0700103 result << std::setw(9) << getBufferCapacity();
Phil Burk4501b352017-06-29 18:12:36 -0700104
105 return result.str();
106}
107
Phil Burka9876702020-04-20 18:16:15 -0700108void 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 Hungd203eb62020-04-27 09:12:46 -0700120 .set(AMEDIAMETRICS_PROP_ALLOWUID, (int32_t)getOwnerUserId())
Phil Burka9876702020-04-20 18:16:15 -0700121 .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 Burk15f97c92018-09-04 14:06:27 -0700138aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700139 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
140 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700141
Svet Ganov33761132021-05-13 22:51:08 +0000142 mMmapClient.attributionSource = request.getAttributionSource();
143 // TODO b/182392769: use attribution source util
144 mMmapClient.attributionSource.uid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700145 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
Svet Ganov33761132021-05-13 22:51:08 +0000146 mMmapClient.attributionSource.pid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700147 legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
Eric Laurentcb4dae22017-07-01 19:39:32 -0700148
Phil Burk39f02dd2017-08-04 09:13:31 -0700149 // Limit scope of lock to avoid recursive lock in close().
150 {
151 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
152 if (mUpMessageQueue != nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700153 ALOGE("%s() called twice", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700154 return AAUDIO_ERROR_INVALID_STATE;
155 }
156
Phil Burk8f4fe502020-07-15 23:54:50 +0000157 mUpMessageQueue = std::make_shared<SharedRingBuffer>();
Phil Burk39f02dd2017-08-04 09:13:31 -0700158 result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage),
159 QUEUE_UP_CAPACITY_COMMANDS);
160 if (result != AAUDIO_OK) {
161 goto error;
162 }
163
Phil Burk6e2770e2018-05-01 13:03:52 -0700164 // 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 Burk39f02dd2017-08-04 09:13:31 -0700167 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService,
Phil Burk15f97c92018-09-04 14:06:27 -0700168 request);
Phil Burk39f02dd2017-08-04 09:13:31 -0700169 if (mServiceEndpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700170 result = AAUDIO_ERROR_UNAVAILABLE;
171 goto error;
172 }
Phil Burk6e2770e2018-05-01 13:03:52 -0700173 // Save a weak pointer that we will use to access the endpoint.
174 mServiceEndpointWeak = mServiceEndpoint;
175
Phil Burk39f02dd2017-08-04 09:13:31 -0700176 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
177 copyFrom(*mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 }
jiabin2a594622021-10-14 00:32:25 +0000179
180 // Make sure this object does not get deleted before the run() method
181 // can protect it by making a strong pointer.
jiabinba75f212021-12-07 20:06:30 +0000182 mCommandQueue.startWaiting();
jiabin2a594622021-10-14 00:32:25 +0000183 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 Burk39f02dd2017-08-04 09:13:31 -0700190 return result;
191
192error:
jiabinba75f212021-12-07 20:06:30 +0000193 closeAndClear();
194 mThreadEnabled = false;
195 mCommandQueue.stopWaiting();
196 mCommandThread.stop();
Phil Burk39f02dd2017-08-04 09:13:31 -0700197 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800198}
Phil Burkdec33ab2017-01-17 14:48:16 -0800199
Phil Burkc0c70e32017-02-09 13:18:38 -0800200aaudio_result_t AAudioServiceStreamBase::close() {
jiabinba75f212021-12-07 20:06:30 +0000201 aaudio_result_t result = sendCommand(CLOSE, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000202
203 // Stop the command thread as the stream is closed.
204 mThreadEnabled = false;
205 mCommandQueue.stopWaiting();
206 mCommandThread.stop();
207
208 return result;
Phil Burk7ebbc112020-05-13 15:55:17 -0700209}
210
211aaudio_result_t AAudioServiceStreamBase::close_l() {
Phil Burkbcc36742017-08-31 17:24:51 -0700212 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700213 return AAUDIO_OK;
214 }
215
jiabin2a594622021-10-14 00:32:25 +0000216 // This will stop the stream, just in case it was not already stopped.
Phil Burk7ebbc112020-05-13 15:55:17 -0700217 stop_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700218
jiabinba75f212021-12-07 20:06:30 +0000219 return closeAndClear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800220}
221
Phil Burkbcc36742017-08-31 17:24:51 -0700222aaudio_result_t AAudioServiceStreamBase::startDevice() {
223 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burk6e2770e2018-05-01 13:03:52 -0700224 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 Burkbcc36742017-08-31 17:24:51 -0700230}
231
Phil Burk39f02dd2017-08-04 09:13:31 -0700232/**
233 * Start the flow of audio data.
234 *
235 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
236 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800237aaudio_result_t AAudioServiceStreamBase::start() {
jiabinba75f212021-12-07 20:06:30 +0000238 return sendCommand(START, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000239}
Phil Burk7ebbc112020-05-13 15:55:17 -0700240
jiabin2a594622021-10-14 00:32:25 +0000241aaudio_result_t AAudioServiceStreamBase::start_l() {
Phil Burka9876702020-04-20 18:16:15 -0700242 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burkbcc36742017-08-31 17:24:51 -0700243 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700244
Phil Burk7ebbc112020-05-13 15:55:17 -0700245 if (auto state = getState();
Phil Burkdb466142021-04-16 16:50:00 +0000246 state == AAUDIO_STREAM_STATE_CLOSED || isDisconnected_l()) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700247 ALOGW("%s() already CLOSED, returns INVALID_STATE, handle = %d",
248 __func__, getHandle());
249 return AAUDIO_ERROR_INVALID_STATE;
250 }
251
jiabinf7f06152021-11-22 18:10:14 +0000252 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 Burka9876702020-04-20 18:16:15 -0700258 mediametrics::Defer defer([&] {
259 mediametrics::LogItem(mMetricsId)
260 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START)
Andy Hungea840382020-05-05 21:50:17 -0700261 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700262 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
263 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
264 .record(); });
265
Eric Laurentcb4dae22017-07-01 19:39:32 -0700266 if (isRunning()) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700267 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700268 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700269
Phil Burk23296382017-11-20 15:45:11 -0800270 setFlowing(false);
Phil Burk762365c2018-12-10 16:02:16 -0800271 setSuspended(false);
Phil Burk23296382017-11-20 15:45:11 -0800272
Phil Burkbcc36742017-08-31 17:24:51 -0700273 // Start with fresh presentation timestamps.
Phil Burka53ffa62018-10-10 16:21:37 -0700274 mAtomicStreamTimestamp.clear();
Phil Burkbcc36742017-08-31 17:24:51 -0700275
Phil Burk39f02dd2017-08-04 09:13:31 -0700276 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burkbcc36742017-08-31 17:24:51 -0700277 result = startDevice();
278 if (result != AAUDIO_OK) goto error;
279
280 // This should happen at the end of the start.
Vlad Popaec1788e2022-08-04 11:23:30 +0200281 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED, static_cast<int64_t>(mClientHandle));
Phil Burkbcc36742017-08-31 17:24:51 -0700282 setState(AAUDIO_STREAM_STATE_STARTED);
Phil Burkbcc36742017-08-31 17:24:51 -0700283
284 return result;
285
286error:
Phil Burk7ebbc112020-05-13 15:55:17 -0700287 disconnect_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700288 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800289}
290
291aaudio_result_t AAudioServiceStreamBase::pause() {
jiabinba75f212021-12-07 20:06:30 +0000292 return sendCommand(PAUSE, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
Phil Burk7ebbc112020-05-13 15:55:17 -0700293}
294
295aaudio_result_t AAudioServiceStreamBase::pause_l() {
Phil Burk11e8d332017-05-24 09:59:02 -0700296 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700297 if (!isRunning()) {
298 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800299 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700300 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk73af62a2017-10-26 12:11:47 -0700301
Phil Burka9876702020-04-20 18:16:15 -0700302 mediametrics::Defer defer([&] {
303 mediametrics::LogItem(mMetricsId)
304 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_PAUSE)
Andy Hungea840382020-05-05 21:50:17 -0700305 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700306 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
307 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
308 .record(); });
309
Phil Burk6e2770e2018-05-01 13:03:52 -0700310 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
311 if (endpoint == nullptr) {
312 ALOGE("%s() has no endpoint", __func__);
Phil Burka9876702020-04-20 18:16:15 -0700313 result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking
314 return result;
Phil Burk6e2770e2018-05-01 13:03:52 -0700315 }
316 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700317 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700318 ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText());
Phil Burk7ebbc112020-05-13 15:55:17 -0700319 disconnect_l(); // TODO should we return or pause Base first?
Phil Burk39f02dd2017-08-04 09:13:31 -0700320 }
321
Eric Laurentcb4dae22017-07-01 19:39:32 -0700322 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkbcc36742017-08-31 17:24:51 -0700323 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800324 return result;
325}
326
Phil Burk71f35bb2017-04-13 16:05:07 -0700327aaudio_result_t AAudioServiceStreamBase::stop() {
jiabinba75f212021-12-07 20:06:30 +0000328 return sendCommand(STOP, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
Phil Burk7ebbc112020-05-13 15:55:17 -0700329}
330
331aaudio_result_t AAudioServiceStreamBase::stop_l() {
Phil Burk11e8d332017-05-24 09:59:02 -0700332 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700333 if (!isRunning()) {
334 return result;
Phil Burk71f35bb2017-04-13 16:05:07 -0700335 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700336 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700337
Phil Burka9876702020-04-20 18:16:15 -0700338 mediametrics::Defer defer([&] {
339 mediametrics::LogItem(mMetricsId)
340 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP)
Andy Hungea840382020-05-05 21:50:17 -0700341 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700342 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
343 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
344 .record(); });
345
Phil Burk83fb8442017-10-05 16:55:17 -0700346 setState(AAUDIO_STREAM_STATE_STOPPING);
347
Eric Laurentcb4dae22017-07-01 19:39:32 -0700348 if (result != AAUDIO_OK) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700349 disconnect_l();
Eric Laurentcb4dae22017-07-01 19:39:32 -0700350 return result;
351 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700352
Phil Burk6e2770e2018-05-01 13:03:52 -0700353 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
354 if (endpoint == nullptr) {
355 ALOGE("%s() has no endpoint", __func__);
Phil Burka9876702020-04-20 18:16:15 -0700356 result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking
357 return result;
Phil Burk6e2770e2018-05-01 13:03:52 -0700358 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700359 // TODO wait for data to be played out
Phil Burk6e2770e2018-05-01 13:03:52 -0700360 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700361 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700362 ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText());
Phil Burk7ebbc112020-05-13 15:55:17 -0700363 disconnect_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700364 // TODO what to do with result here?
365 }
366
Eric Laurentcb4dae22017-07-01 19:39:32 -0700367 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burkbcc36742017-08-31 17:24:51 -0700368 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700369 return result;
370}
371
jiabin2a594622021-10-14 00:32:25 +0000372aaudio_result_t AAudioServiceStreamBase::flush() {
jiabinba75f212021-12-07 20:06:30 +0000373 return sendCommand(FLUSH, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
Phil Burk98d6d922017-07-06 11:52:45 -0700374}
375
jiabin2a594622021-10-14 00:32:25 +0000376aaudio_result_t AAudioServiceStreamBase::flush_l() {
Phil Burk5cc83c32017-11-28 15:43:18 -0800377 aaudio_result_t result = AAudio_isFlushAllowed(getState());
378 if (result != AAUDIO_OK) {
379 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700380 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700381 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk5cc83c32017-11-28 15:43:18 -0800382
Phil Burka9876702020-04-20 18:16:15 -0700383 mediametrics::Defer defer([&] {
384 mediametrics::LogItem(mMetricsId)
385 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_FLUSH)
Andy Hungea840382020-05-05 21:50:17 -0700386 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700387 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
388 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
389 .record(); });
390
Phil Burk39f02dd2017-08-04 09:13:31 -0700391 // Data will get flushed when the client receives the FLUSHED event.
Phil Burk71f35bb2017-04-13 16:05:07 -0700392 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
Phil Burkbcc36742017-08-31 17:24:51 -0700393 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700394 return AAUDIO_OK;
395}
396
jiabin2a594622021-10-14 00:32:25 +0000397// implement Runnable, periodically send timestamps to client and process commands from queue.
Phil Burka53ffa62018-10-10 16:21:37 -0700398__attribute__((no_sanitize("integer")))
Phil Burkc0c70e32017-02-09 13:18:38 -0800399void AAudioServiceStreamBase::run() {
jiabin2a594622021-10-14 00:32:25 +0000400 ALOGD("%s() %s entering >>>>>>>>>>>>>> COMMANDS", __func__, getTypeText());
Phil Burk3d201942021-04-08 23:27:04 +0000401 // Hold onto the ref counted stream until the end.
402 android::sp<AAudioServiceStreamBase> holdStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800403 TimestampScheduler timestampScheduler;
jiabin2a594622021-10-14 00:32:25 +0000404 int64_t nextTime;
jiabinf7f06152021-11-22 18:10:14 +0000405 int64_t standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
Phil Burk3d201942021-04-08 23:27:04 +0000406 // Balance the incStrong from when the thread was launched.
407 holdStream->decStrong(nullptr);
408
jiabin2a594622021-10-14 00:32:25 +0000409 // 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 Burka53ffa62018-10-10 16:21:37 -0700413 int32_t loopCount = 0;
jiabin2a594622021-10-14 00:32:25 +0000414 while (mThreadEnabled.load()) {
Phil Burka53ffa62018-10-10 16:21:37 -0700415 loopCount++;
jiabin2a594622021-10-14 00:32:25 +0000416 int64_t timeoutNanos = -1;
jiabinf7f06152021-11-22 18:10:14 +0000417 if (isRunning() || (isIdle_l() && !isStandby_l())) {
418 timeoutNanos = (isRunning() ? nextTime : standbyTime) - AudioClock::getNanoseconds();
jiabin2a594622021-10-14 00:32:25 +0000419 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 Burkc0c70e32017-02-09 13:18:38 -0800435 }
jiabin2a594622021-10-14 00:32:25 +0000436 }
jiabinf7f06152021-11-22 18:10:14 +0000437 if (isIdle_l() && AudioClock::getNanoseconds() >= standbyTime) {
438 standby_l();
439 }
jiabin2a594622021-10-14 00:32:25 +0000440
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();
jiabinf7f06152021-11-22 18:10:14 +0000452 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
jiabin2a594622021-10-14 00:32:25 +0000453 break;
454 case STOP:
455 command->result = stop_l();
jiabinf7f06152021-11-22 18:10:14 +0000456 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
jiabin2a594622021-10-14 00:32:25 +0000457 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;
jiabinf7f06152021-11-22 18:10:14 +0000491 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;
jiabin2a594622021-10-14 00:32:25 +0000497 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 Burkc0c70e32017-02-09 13:18:38 -0800505 }
506 }
jiabin2a594622021-10-14 00:32:25 +0000507 ALOGD("%s() %s exiting after %d loops <<<<<<<<<<<<<< COMMANDS",
Phil Burka53ffa62018-10-10 16:21:37 -0700508 __func__, getTypeText(), loopCount);
Phil Burkc0c70e32017-02-09 13:18:38 -0800509}
510
Phil Burk5ef003b2017-06-30 11:43:37 -0700511void AAudioServiceStreamBase::disconnect() {
jiabinba75f212021-12-07 20:06:30 +0000512 sendCommand(DISCONNECT);
Phil Burk7ebbc112020-05-13 15:55:17 -0700513}
514
515void AAudioServiceStreamBase::disconnect_l() {
Phil Burkdb466142021-04-16 16:50:00 +0000516 if (!isDisconnected_l() && getState() != AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700517
Phil Burka9876702020-04-20 18:16:15 -0700518 mediametrics::LogItem(mMetricsId)
519 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DISCONNECT)
520 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
521 .record();
Phil Burk7ebbc112020-05-13 15:55:17 -0700522
Phil Burk5ef003b2017-06-30 11:43:37 -0700523 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkdb466142021-04-16 16:50:00 +0000524 setDisconnected_l(true);
Phil Burk5ef003b2017-06-30 11:43:37 -0700525 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800526}
527
jiabin2a594622021-10-14 00:32:25 +0000528aaudio_result_t AAudioServiceStreamBase::registerAudioThread(pid_t clientThreadId, int priority) {
529 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
jiabinba75f212021-12-07 20:06:30 +0000530 return sendCommand(REGISTER_AUDIO_THREAD,
jiabin2a594622021-10-14 00:32:25 +0000531 std::make_shared<RegisterAudioThreadParam>(ownerPid, clientThreadId, priority),
532 true /*waitForReply*/,
533 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000534}
535
536aaudio_result_t AAudioServiceStreamBase::registerAudioThread_l(
537 pid_t ownerPid, pid_t clientThreadId, int priority) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700538 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 Burk7ebbc112020-05-13 15:55:17 -0700543 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
555aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread(pid_t clientThreadId) {
jiabinba75f212021-12-07 20:06:30 +0000556 return sendCommand(UNREGISTER_AUDIO_THREAD,
jiabin2a594622021-10-14 00:32:25 +0000557 std::make_shared<UnregisterAudioThreadParam>(clientThreadId),
558 true /*waitForReply*/,
559 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000560}
561
562aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread_l(pid_t clientThreadId) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700563 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
573void 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 Burkc0c70e32017-02-09 13:18:38 -0800582aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800583 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800584 AAudioServiceMessage command;
585 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800586 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800587 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800588 return writeUpMessageQueue(&command);
589}
590
591aaudio_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 Burkc0c70e32017-02-09 13:18:38 -0800596 command.event.dataLong = dataLong;
597 return writeUpMessageQueue(&command);
598}
599
Phil Burkf878a8d2019-03-29 17:23:00 -0700600bool 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 Burkf878a8d2019-03-29 17:23:00 -0700606 // Is it half full or more
Phil Burk8f4fe502020-07-15 23:54:50 +0000607 return mUpMessageQueue->getFractionalFullness() >= 0.5;
Phil Burkf878a8d2019-03-29 17:23:00 -0700608}
609
Phil Burkc0c70e32017-02-09 13:18:38 -0800610aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700611 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700612 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700613 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700614 return AAUDIO_ERROR_NULL;
615 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800616 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
617 if (count != 1) {
Phil Burk762365c2018-12-10 16:02:16 -0800618 ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s",
619 __func__, command->what, getTypeText());
620 setSuspended(true);
Phil Burkc0c70e32017-02-09 13:18:38 -0800621 return AAUDIO_ERROR_WOULD_BLOCK;
622 } else {
623 return AAUDIO_OK;
624 }
625}
626
Phil Burk23296382017-11-20 15:45:11 -0800627aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
628 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
629}
630
jiabin2a594622021-10-14 00:32:25 +0000631aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800632 AAudioServiceMessage command;
Phil Burkf878a8d2019-03-29 17:23:00 -0700633 // 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 Burk97350f92017-07-21 15:59:44 -0700640 // Send a timestamp for the clock model.
jiabin2a594622021-10-14 00:32:25 +0000641 aaudio_result_t result = getFreeRunningPosition_l(&command.timestamp.position,
642 &command.timestamp.timestamp);
Phil Burkc0c70e32017-02-09 13:18:38 -0800643 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700644 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700645 (long long) command.timestamp.position,
646 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700647 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800648 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700649
650 if (result == AAUDIO_OK) {
651 // Send a hardware timestamp for presentation time.
jiabin2a594622021-10-14 00:32:25 +0000652 result = getHardwareTimestamp_l(&command.timestamp.position,
653 &command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700654 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700655 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700656 (long long) command.timestamp.position,
657 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700658 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
659 result = writeUpMessageQueue(&command);
660 }
661 }
662 }
663
Phil Burkbcc36742017-08-31 17:24:51 -0700664 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700665 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800666 }
667 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800668}
669
Phil Burkc0c70e32017-02-09 13:18:38 -0800670/**
671 * Get an immutable description of the in-memory queues
672 * used to communicate with the underlying HAL or Service.
673 */
674aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
jiabinba75f212021-12-07 20:06:30 +0000675 return sendCommand(
jiabin2a594622021-10-14 00:32:25 +0000676 GET_DESCRIPTION,
677 std::make_shared<GetDescriptionParam>(&parcelable),
678 true /*waitForReply*/,
679 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000680}
681
682aaudio_result_t AAudioServiceStreamBase::getDescription_l(AudioEndpointParcelable* parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700683 {
684 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
685 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700686 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700687 return AAUDIO_ERROR_NULL;
688 }
689 // Gather information on the message queue.
690 mUpMessageQueue->fillParcelable(parcelable,
jiabin2a594622021-10-14 00:32:25 +0000691 parcelable->mUpMessageQueueParcelable);
Phil Burk523b3042017-09-13 13:03:08 -0700692 }
jiabin2a594622021-10-14 00:32:25 +0000693 return getAudioDataDescription_l(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700694}
Phil Burk39f02dd2017-08-04 09:13:31 -0700695
jiabinf7f06152021-11-22 18:10:14 +0000696aaudio_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 Burk39f02dd2017-08-04 09:13:31 -0700705void AAudioServiceStreamBase::onVolumeChanged(float volume) {
706 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
707}
jiabinba75f212021-12-07 20:06:30 +0000708
709aaudio_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
717aaudio_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}