blob: 17e64ee3700bfcd4992340351451acfa825e8813 [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.
285 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
286 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) {
442 standby_l();
443 }
jiabin2a594622021-10-14 00:32:25 +0000444
445 if (command != nullptr) {
446 std::scoped_lock<std::mutex> _commandLock(command->lock);
447 switch (command->operationCode) {
448 case START:
449 command->result = start_l();
450 timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate());
451 timestampScheduler.start(AudioClock::getNanoseconds());
452 nextTime = timestampScheduler.nextAbsoluteTime();
453 break;
454 case PAUSE:
455 command->result = pause_l();
jiabinf7f06152021-11-22 18:10:14 +0000456 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
jiabin2a594622021-10-14 00:32:25 +0000457 break;
458 case STOP:
459 command->result = stop_l();
jiabinf7f06152021-11-22 18:10:14 +0000460 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
jiabin2a594622021-10-14 00:32:25 +0000461 break;
462 case FLUSH:
463 command->result = flush_l();
464 break;
465 case CLOSE:
466 command->result = close_l();
467 break;
468 case DISCONNECT:
469 disconnect_l();
470 break;
471 case REGISTER_AUDIO_THREAD: {
472 RegisterAudioThreadParam *param =
473 (RegisterAudioThreadParam *) command->parameter.get();
474 command->result =
475 param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
476 : registerAudioThread_l(param->mOwnerPid,
477 param->mClientThreadId,
478 param->mPriority);
479 }
480 break;
481 case UNREGISTER_AUDIO_THREAD: {
482 UnregisterAudioThreadParam *param =
483 (UnregisterAudioThreadParam *) command->parameter.get();
484 command->result =
485 param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
486 : unregisterAudioThread_l(param->mClientThreadId);
487 }
488 break;
489 case GET_DESCRIPTION: {
490 GetDescriptionParam *param = (GetDescriptionParam *) command->parameter.get();
491 command->result = param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
492 : getDescription_l(param->mParcelable);
493 }
494 break;
jiabinf7f06152021-11-22 18:10:14 +0000495 case EXIT_STANDBY: {
496 ExitStandbyParam *param = (ExitStandbyParam *) command->parameter.get();
497 command->result = param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
498 : exitStandby_l(param->mParcelable);
499 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
500 } break;
jiabin2a594622021-10-14 00:32:25 +0000501 default:
502 ALOGE("Invalid command op code: %d", command->operationCode);
503 break;
504 }
505 if (command->isWaitingForReply) {
506 command->isWaitingForReply = false;
507 command->conditionVariable.notify_one();
508 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800509 }
510 }
jiabin2a594622021-10-14 00:32:25 +0000511 ALOGD("%s() %s exiting after %d loops <<<<<<<<<<<<<< COMMANDS",
Phil Burka53ffa62018-10-10 16:21:37 -0700512 __func__, getTypeText(), loopCount);
Phil Burkc0c70e32017-02-09 13:18:38 -0800513}
514
Phil Burk5ef003b2017-06-30 11:43:37 -0700515void AAudioServiceStreamBase::disconnect() {
jiabinba75f212021-12-07 20:06:30 +0000516 sendCommand(DISCONNECT);
Phil Burk7ebbc112020-05-13 15:55:17 -0700517}
518
519void AAudioServiceStreamBase::disconnect_l() {
Phil Burkdb466142021-04-16 16:50:00 +0000520 if (!isDisconnected_l() && getState() != AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700521
Phil Burka9876702020-04-20 18:16:15 -0700522 mediametrics::LogItem(mMetricsId)
523 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DISCONNECT)
524 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
525 .record();
Phil Burk7ebbc112020-05-13 15:55:17 -0700526
Phil Burk5ef003b2017-06-30 11:43:37 -0700527 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkdb466142021-04-16 16:50:00 +0000528 setDisconnected_l(true);
Phil Burk5ef003b2017-06-30 11:43:37 -0700529 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800530}
531
jiabin2a594622021-10-14 00:32:25 +0000532aaudio_result_t AAudioServiceStreamBase::registerAudioThread(pid_t clientThreadId, int priority) {
533 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
jiabinba75f212021-12-07 20:06:30 +0000534 return sendCommand(REGISTER_AUDIO_THREAD,
jiabin2a594622021-10-14 00:32:25 +0000535 std::make_shared<RegisterAudioThreadParam>(ownerPid, clientThreadId, priority),
536 true /*waitForReply*/,
537 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000538}
539
540aaudio_result_t AAudioServiceStreamBase::registerAudioThread_l(
541 pid_t ownerPid, pid_t clientThreadId, int priority) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700542 aaudio_result_t result = AAUDIO_OK;
543 if (getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
544 ALOGE("AAudioService::registerAudioThread(), thread already registered");
545 result = AAUDIO_ERROR_INVALID_STATE;
546 } else {
Phil Burk7ebbc112020-05-13 15:55:17 -0700547 setRegisteredThread(clientThreadId);
548 int err = android::requestPriority(ownerPid, clientThreadId,
549 priority, true /* isForApp */);
550 if (err != 0) {
551 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
552 clientThreadId, errno, priority);
553 result = AAUDIO_ERROR_INTERNAL;
554 }
555 }
556 return result;
557}
558
559aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread(pid_t clientThreadId) {
jiabinba75f212021-12-07 20:06:30 +0000560 return sendCommand(UNREGISTER_AUDIO_THREAD,
jiabin2a594622021-10-14 00:32:25 +0000561 std::make_shared<UnregisterAudioThreadParam>(clientThreadId),
562 true /*waitForReply*/,
563 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000564}
565
566aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread_l(pid_t clientThreadId) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700567 aaudio_result_t result = AAUDIO_OK;
568 if (getRegisteredThread() != clientThreadId) {
569 ALOGE("%s(), wrong thread", __func__);
570 result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
571 } else {
572 setRegisteredThread(0);
573 }
574 return result;
575}
576
577void AAudioServiceStreamBase::setState(aaudio_stream_state_t state) {
578 // CLOSED is a final state.
579 if (mState != AAUDIO_STREAM_STATE_CLOSED) {
580 mState = state;
581 } else {
582 ALOGW_IF(mState != state, "%s(%d) when already CLOSED", __func__, state);
583 }
584}
585
Phil Burkc0c70e32017-02-09 13:18:38 -0800586aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800587 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800588 AAudioServiceMessage command;
589 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800590 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800591 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800592 return writeUpMessageQueue(&command);
593}
594
595aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
596 int64_t dataLong) {
597 AAudioServiceMessage command;
598 command.what = AAudioServiceMessage::code::EVENT;
599 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800600 command.event.dataLong = dataLong;
601 return writeUpMessageQueue(&command);
602}
603
Phil Burkf878a8d2019-03-29 17:23:00 -0700604bool AAudioServiceStreamBase::isUpMessageQueueBusy() {
605 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
606 if (mUpMessageQueue == nullptr) {
607 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
608 return true;
609 }
Phil Burkf878a8d2019-03-29 17:23:00 -0700610 // Is it half full or more
Phil Burk8f4fe502020-07-15 23:54:50 +0000611 return mUpMessageQueue->getFractionalFullness() >= 0.5;
Phil Burkf878a8d2019-03-29 17:23:00 -0700612}
613
Phil Burkc0c70e32017-02-09 13:18:38 -0800614aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700615 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700616 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700617 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700618 return AAUDIO_ERROR_NULL;
619 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800620 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
621 if (count != 1) {
Phil Burk762365c2018-12-10 16:02:16 -0800622 ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s",
623 __func__, command->what, getTypeText());
624 setSuspended(true);
Phil Burkc0c70e32017-02-09 13:18:38 -0800625 return AAUDIO_ERROR_WOULD_BLOCK;
626 } else {
627 return AAUDIO_OK;
628 }
629}
630
Phil Burk23296382017-11-20 15:45:11 -0800631aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
632 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
633}
634
jiabin2a594622021-10-14 00:32:25 +0000635aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800636 AAudioServiceMessage command;
Phil Burkf878a8d2019-03-29 17:23:00 -0700637 // It is not worth filling up the queue with timestamps.
638 // That can cause the stream to get suspended.
639 // So just drop the timestamp if the queue is getting full.
640 if (isUpMessageQueueBusy()) {
641 return AAUDIO_OK;
642 }
643
Phil Burk97350f92017-07-21 15:59:44 -0700644 // Send a timestamp for the clock model.
jiabin2a594622021-10-14 00:32:25 +0000645 aaudio_result_t result = getFreeRunningPosition_l(&command.timestamp.position,
646 &command.timestamp.timestamp);
Phil Burkc0c70e32017-02-09 13:18:38 -0800647 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700648 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700649 (long long) command.timestamp.position,
650 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700651 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800652 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700653
654 if (result == AAUDIO_OK) {
655 // Send a hardware timestamp for presentation time.
jiabin2a594622021-10-14 00:32:25 +0000656 result = getHardwareTimestamp_l(&command.timestamp.position,
657 &command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700658 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700659 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700660 (long long) command.timestamp.position,
661 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700662 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
663 result = writeUpMessageQueue(&command);
664 }
665 }
666 }
667
Phil Burkbcc36742017-08-31 17:24:51 -0700668 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700669 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800670 }
671 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800672}
673
Phil Burkc0c70e32017-02-09 13:18:38 -0800674/**
675 * Get an immutable description of the in-memory queues
676 * used to communicate with the underlying HAL or Service.
677 */
678aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
jiabinba75f212021-12-07 20:06:30 +0000679 return sendCommand(
jiabin2a594622021-10-14 00:32:25 +0000680 GET_DESCRIPTION,
681 std::make_shared<GetDescriptionParam>(&parcelable),
682 true /*waitForReply*/,
683 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000684}
685
686aaudio_result_t AAudioServiceStreamBase::getDescription_l(AudioEndpointParcelable* parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700687 {
688 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
689 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700690 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700691 return AAUDIO_ERROR_NULL;
692 }
693 // Gather information on the message queue.
694 mUpMessageQueue->fillParcelable(parcelable,
jiabin2a594622021-10-14 00:32:25 +0000695 parcelable->mUpMessageQueueParcelable);
Phil Burk523b3042017-09-13 13:03:08 -0700696 }
jiabin2a594622021-10-14 00:32:25 +0000697 return getAudioDataDescription_l(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700698}
Phil Burk39f02dd2017-08-04 09:13:31 -0700699
jiabinf7f06152021-11-22 18:10:14 +0000700aaudio_result_t AAudioServiceStreamBase::exitStandby(AudioEndpointParcelable *parcelable) {
701 auto command = std::make_shared<AAudioCommand>(
702 EXIT_STANDBY,
703 std::make_shared<ExitStandbyParam>(parcelable),
704 true /*waitForReply*/,
705 TIMEOUT_NANOS);
706 return mCommandQueue.sendCommand(command);
707}
708
Phil Burk39f02dd2017-08-04 09:13:31 -0700709void AAudioServiceStreamBase::onVolumeChanged(float volume) {
710 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
711}
jiabinba75f212021-12-07 20:06:30 +0000712
713aaudio_result_t AAudioServiceStreamBase::sendCommand(aaudio_command_opcode opCode,
714 std::shared_ptr<AAudioCommandParam> param,
715 bool waitForReply,
716 int64_t timeoutNanos) {
717 return mCommandQueue.sendCommand(std::make_shared<AAudioCommand>(
718 opCode, param, waitForReply, timeoutNanos));
719}
720
721aaudio_result_t AAudioServiceStreamBase::closeAndClear() {
722 aaudio_result_t result = AAUDIO_OK;
723 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
724 if (endpoint == nullptr) {
725 result = AAUDIO_ERROR_INVALID_STATE;
726 } else {
727 endpoint->unregisterStream(this);
728 AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance();
729 endpointManager.closeEndpoint(endpoint);
730
731 // AAudioService::closeStream() prevents two threads from closing at the same time.
732 mServiceEndpoint.clear(); // endpoint will hold the pointer after this method returns.
733 }
734
735 setState(AAUDIO_STREAM_STATE_CLOSED);
736
737 mediametrics::LogItem(mMetricsId)
738 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CLOSE)
739 .record();
740 return result;
741}