blob: 8e1e4972d7b2f9cd81de4bfcbd4e6af08a3b97a7 [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() {
jiabin613e6ae2022-12-21 20:20:11 +000086 return {" T Handle UId Port Run State Format Burst Chan Mask Capacity"
87 " HwFormat HwChan HwRate"};
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();
Robert Wu310037a2022-09-06 21:48:18 +0000104 result << std::setw(9) << getHardwareFormat();
105 result << std::setw(7) << getHardwareSamplesPerFrame();
106 result << std::setw(7) << getHardwareSampleRate();
Phil Burk4501b352017-06-29 18:12:36 -0700107
108 return result.str();
109}
110
Phil Burka9876702020-04-20 18:16:15 -0700111void AAudioServiceStreamBase::logOpen(aaudio_handle_t streamHandle) {
112 // This is the first log sent from the AAudio Service for a stream.
113 mMetricsId = std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_STREAM)
114 + std::to_string(streamHandle);
115
116 audio_attributes_t attributes = AAudioServiceEndpoint::getAudioAttributesFrom(this);
117
118 // Once this item is logged by the server, the client with the same PID, UID
119 // can also log properties.
120 mediametrics::LogItem(mMetricsId)
121 .setPid(getOwnerProcessId())
122 .setUid(getOwnerUserId())
Andy Hungd203eb62020-04-27 09:12:46 -0700123 .set(AMEDIAMETRICS_PROP_ALLOWUID, (int32_t)getOwnerUserId())
Phil Burka9876702020-04-20 18:16:15 -0700124 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_OPEN)
125 // the following are immutable
126 .set(AMEDIAMETRICS_PROP_BUFFERCAPACITYFRAMES, (int32_t)getBufferCapacity())
127 .set(AMEDIAMETRICS_PROP_BURSTFRAMES, (int32_t)getFramesPerBurst())
128 .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)getSamplesPerFrame())
129 .set(AMEDIAMETRICS_PROP_CONTENTTYPE, toString(attributes.content_type).c_str())
130 .set(AMEDIAMETRICS_PROP_DIRECTION,
131 AudioGlobal_convertDirectionToText(getDirection()))
132 .set(AMEDIAMETRICS_PROP_ENCODING, toString(getFormat()).c_str())
133 .set(AMEDIAMETRICS_PROP_ROUTEDDEVICEID, (int32_t)getDeviceId())
134 .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)getSampleRate())
135 .set(AMEDIAMETRICS_PROP_SESSIONID, (int32_t)getSessionId())
136 .set(AMEDIAMETRICS_PROP_SOURCE, toString(attributes.source).c_str())
137 .set(AMEDIAMETRICS_PROP_USAGE, toString(attributes.usage).c_str())
138 .record();
139}
140
Phil Burk15f97c92018-09-04 14:06:27 -0700141aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700142 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
143 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700144
Svet Ganov33761132021-05-13 22:51:08 +0000145 mMmapClient.attributionSource = request.getAttributionSource();
146 // TODO b/182392769: use attribution source util
147 mMmapClient.attributionSource.uid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700148 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
Svet Ganov33761132021-05-13 22:51:08 +0000149 mMmapClient.attributionSource.pid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700150 legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
Eric Laurentcb4dae22017-07-01 19:39:32 -0700151
Phil Burk39f02dd2017-08-04 09:13:31 -0700152 // Limit scope of lock to avoid recursive lock in close().
153 {
154 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
155 if (mUpMessageQueue != nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700156 ALOGE("%s() called twice", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 return AAUDIO_ERROR_INVALID_STATE;
158 }
159
Phil Burk8f4fe502020-07-15 23:54:50 +0000160 mUpMessageQueue = std::make_shared<SharedRingBuffer>();
Phil Burk39f02dd2017-08-04 09:13:31 -0700161 result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage),
162 QUEUE_UP_CAPACITY_COMMANDS);
163 if (result != AAUDIO_OK) {
164 goto error;
165 }
166
Phil Burk6e2770e2018-05-01 13:03:52 -0700167 // This is not protected by a lock because the stream cannot be
168 // referenced until the service returns a handle to the client.
169 // So only one thread can open a stream.
Phil Burk39f02dd2017-08-04 09:13:31 -0700170 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService,
Phil Burk15f97c92018-09-04 14:06:27 -0700171 request);
Phil Burk39f02dd2017-08-04 09:13:31 -0700172 if (mServiceEndpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700173 result = AAUDIO_ERROR_UNAVAILABLE;
174 goto error;
175 }
Phil Burk6e2770e2018-05-01 13:03:52 -0700176 // Save a weak pointer that we will use to access the endpoint.
177 mServiceEndpointWeak = mServiceEndpoint;
178
Phil Burk39f02dd2017-08-04 09:13:31 -0700179 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
180 copyFrom(*mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800181 }
jiabin2a594622021-10-14 00:32:25 +0000182
183 // Make sure this object does not get deleted before the run() method
184 // can protect it by making a strong pointer.
jiabinba75f212021-12-07 20:06:30 +0000185 mCommandQueue.startWaiting();
jiabin2a594622021-10-14 00:32:25 +0000186 mThreadEnabled = true;
187 incStrong(nullptr); // See run() method.
188 result = mCommandThread.start(this);
189 if (result != AAUDIO_OK) {
190 decStrong(nullptr); // run() can't do it so we have to do it here.
191 goto error;
192 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700193 return result;
194
195error:
jiabinba75f212021-12-07 20:06:30 +0000196 closeAndClear();
197 mThreadEnabled = false;
198 mCommandQueue.stopWaiting();
199 mCommandThread.stop();
Phil Burk39f02dd2017-08-04 09:13:31 -0700200 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800201}
Phil Burkdec33ab2017-01-17 14:48:16 -0800202
Phil Burkc0c70e32017-02-09 13:18:38 -0800203aaudio_result_t AAudioServiceStreamBase::close() {
jiabinba75f212021-12-07 20:06:30 +0000204 aaudio_result_t result = sendCommand(CLOSE, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000205
206 // Stop the command thread as the stream is closed.
207 mThreadEnabled = false;
208 mCommandQueue.stopWaiting();
209 mCommandThread.stop();
210
211 return result;
Phil Burk7ebbc112020-05-13 15:55:17 -0700212}
213
214aaudio_result_t AAudioServiceStreamBase::close_l() {
Phil Burkbcc36742017-08-31 17:24:51 -0700215 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700216 return AAUDIO_OK;
217 }
218
jiabin2a594622021-10-14 00:32:25 +0000219 // This will stop the stream, just in case it was not already stopped.
Phil Burk7ebbc112020-05-13 15:55:17 -0700220 stop_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700221
jiabinba75f212021-12-07 20:06:30 +0000222 return closeAndClear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800223}
224
Phil Burkbcc36742017-08-31 17:24:51 -0700225aaudio_result_t AAudioServiceStreamBase::startDevice() {
226 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burk6e2770e2018-05-01 13:03:52 -0700227 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
228 if (endpoint == nullptr) {
229 ALOGE("%s() has no endpoint", __func__);
230 return AAUDIO_ERROR_INVALID_STATE;
231 }
232 return endpoint->startStream(this, &mClientHandle);
Phil Burkbcc36742017-08-31 17:24:51 -0700233}
234
Phil Burk39f02dd2017-08-04 09:13:31 -0700235/**
236 * Start the flow of audio data.
237 *
238 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
239 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800240aaudio_result_t AAudioServiceStreamBase::start() {
jiabinba75f212021-12-07 20:06:30 +0000241 return sendCommand(START, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000242}
Phil Burk7ebbc112020-05-13 15:55:17 -0700243
jiabin2a594622021-10-14 00:32:25 +0000244aaudio_result_t AAudioServiceStreamBase::start_l() {
Phil Burka9876702020-04-20 18:16:15 -0700245 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burkbcc36742017-08-31 17:24:51 -0700246 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700247
Phil Burk7ebbc112020-05-13 15:55:17 -0700248 if (auto state = getState();
Phil Burkdb466142021-04-16 16:50:00 +0000249 state == AAUDIO_STREAM_STATE_CLOSED || isDisconnected_l()) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700250 ALOGW("%s() already CLOSED, returns INVALID_STATE, handle = %d",
251 __func__, getHandle());
252 return AAUDIO_ERROR_INVALID_STATE;
253 }
254
jiabinf7f06152021-11-22 18:10:14 +0000255 if (mStandby) {
256 ALOGW("%s() the stream is standby, return ERROR_STANDBY, "
257 "expecting the client call exitStandby before start", __func__);
258 return AAUDIO_ERROR_STANDBY;
259 }
260
Phil Burka9876702020-04-20 18:16:15 -0700261 mediametrics::Defer defer([&] {
262 mediametrics::LogItem(mMetricsId)
263 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START)
Andy Hungea840382020-05-05 21:50:17 -0700264 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700265 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
266 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
267 .record(); });
268
Eric Laurentcb4dae22017-07-01 19:39:32 -0700269 if (isRunning()) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700270 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700271 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700272
Phil Burk23296382017-11-20 15:45:11 -0800273 setFlowing(false);
Phil Burk762365c2018-12-10 16:02:16 -0800274 setSuspended(false);
Phil Burk23296382017-11-20 15:45:11 -0800275
Phil Burkbcc36742017-08-31 17:24:51 -0700276 // Start with fresh presentation timestamps.
Phil Burka53ffa62018-10-10 16:21:37 -0700277 mAtomicStreamTimestamp.clear();
Phil Burkbcc36742017-08-31 17:24:51 -0700278
Phil Burk39f02dd2017-08-04 09:13:31 -0700279 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burkbcc36742017-08-31 17:24:51 -0700280 result = startDevice();
281 if (result != AAUDIO_OK) goto error;
282
283 // This should happen at the end of the start.
Vlad Popaec1788e2022-08-04 11:23:30 +0200284 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED, static_cast<int64_t>(mClientHandle));
Phil Burkbcc36742017-08-31 17:24:51 -0700285 setState(AAUDIO_STREAM_STATE_STARTED);
Phil Burkbcc36742017-08-31 17:24:51 -0700286
287 return result;
288
289error:
Phil Burk7ebbc112020-05-13 15:55:17 -0700290 disconnect_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700291 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800292}
293
294aaudio_result_t AAudioServiceStreamBase::pause() {
jiabinba75f212021-12-07 20:06:30 +0000295 return sendCommand(PAUSE, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
Phil Burk7ebbc112020-05-13 15:55:17 -0700296}
297
298aaudio_result_t AAudioServiceStreamBase::pause_l() {
Phil Burk11e8d332017-05-24 09:59:02 -0700299 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700300 if (!isRunning()) {
301 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800302 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700303 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk73af62a2017-10-26 12:11:47 -0700304
Phil Burka9876702020-04-20 18:16:15 -0700305 mediametrics::Defer defer([&] {
306 mediametrics::LogItem(mMetricsId)
307 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_PAUSE)
Andy Hungea840382020-05-05 21:50:17 -0700308 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700309 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
310 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
311 .record(); });
312
Phil Burk6e2770e2018-05-01 13:03:52 -0700313 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
314 if (endpoint == nullptr) {
315 ALOGE("%s() has no endpoint", __func__);
Phil Burka9876702020-04-20 18:16:15 -0700316 result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking
317 return result;
Phil Burk6e2770e2018-05-01 13:03:52 -0700318 }
319 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700320 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700321 ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText());
Phil Burk7ebbc112020-05-13 15:55:17 -0700322 disconnect_l(); // TODO should we return or pause Base first?
Phil Burk39f02dd2017-08-04 09:13:31 -0700323 }
324
Eric Laurentcb4dae22017-07-01 19:39:32 -0700325 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkbcc36742017-08-31 17:24:51 -0700326 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800327 return result;
328}
329
Phil Burk71f35bb2017-04-13 16:05:07 -0700330aaudio_result_t AAudioServiceStreamBase::stop() {
jiabinba75f212021-12-07 20:06:30 +0000331 return sendCommand(STOP, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
Phil Burk7ebbc112020-05-13 15:55:17 -0700332}
333
334aaudio_result_t AAudioServiceStreamBase::stop_l() {
Phil Burk11e8d332017-05-24 09:59:02 -0700335 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700336 if (!isRunning()) {
337 return result;
Phil Burk71f35bb2017-04-13 16:05:07 -0700338 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700339 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700340
Phil Burka9876702020-04-20 18:16:15 -0700341 mediametrics::Defer defer([&] {
342 mediametrics::LogItem(mMetricsId)
343 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP)
Andy Hungea840382020-05-05 21:50:17 -0700344 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700345 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
346 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
347 .record(); });
348
Phil Burk83fb8442017-10-05 16:55:17 -0700349 setState(AAUDIO_STREAM_STATE_STOPPING);
350
Eric Laurentcb4dae22017-07-01 19:39:32 -0700351 if (result != AAUDIO_OK) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700352 disconnect_l();
Eric Laurentcb4dae22017-07-01 19:39:32 -0700353 return result;
354 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700355
Phil Burk6e2770e2018-05-01 13:03:52 -0700356 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
357 if (endpoint == nullptr) {
358 ALOGE("%s() has no endpoint", __func__);
Phil Burka9876702020-04-20 18:16:15 -0700359 result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking
360 return result;
Phil Burk6e2770e2018-05-01 13:03:52 -0700361 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700362 // TODO wait for data to be played out
Phil Burk6e2770e2018-05-01 13:03:52 -0700363 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700364 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700365 ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText());
Phil Burk7ebbc112020-05-13 15:55:17 -0700366 disconnect_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700367 // TODO what to do with result here?
368 }
369
Eric Laurentcb4dae22017-07-01 19:39:32 -0700370 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burkbcc36742017-08-31 17:24:51 -0700371 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700372 return result;
373}
374
jiabin2a594622021-10-14 00:32:25 +0000375aaudio_result_t AAudioServiceStreamBase::flush() {
jiabinba75f212021-12-07 20:06:30 +0000376 return sendCommand(FLUSH, nullptr, true /*waitForReply*/, TIMEOUT_NANOS);
Phil Burk98d6d922017-07-06 11:52:45 -0700377}
378
jiabin2a594622021-10-14 00:32:25 +0000379aaudio_result_t AAudioServiceStreamBase::flush_l() {
Phil Burk5cc83c32017-11-28 15:43:18 -0800380 aaudio_result_t result = AAudio_isFlushAllowed(getState());
381 if (result != AAUDIO_OK) {
382 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700383 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700384 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk5cc83c32017-11-28 15:43:18 -0800385
Phil Burka9876702020-04-20 18:16:15 -0700386 mediametrics::Defer defer([&] {
387 mediametrics::LogItem(mMetricsId)
388 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_FLUSH)
Andy Hungea840382020-05-05 21:50:17 -0700389 .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
Phil Burka9876702020-04-20 18:16:15 -0700390 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
391 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
392 .record(); });
393
Phil Burk39f02dd2017-08-04 09:13:31 -0700394 // Data will get flushed when the client receives the FLUSHED event.
Phil Burk71f35bb2017-04-13 16:05:07 -0700395 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
Phil Burkbcc36742017-08-31 17:24:51 -0700396 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700397 return AAUDIO_OK;
398}
399
jiabin2a594622021-10-14 00:32:25 +0000400// implement Runnable, periodically send timestamps to client and process commands from queue.
Phil Burka53ffa62018-10-10 16:21:37 -0700401__attribute__((no_sanitize("integer")))
Phil Burkc0c70e32017-02-09 13:18:38 -0800402void AAudioServiceStreamBase::run() {
jiabin2a594622021-10-14 00:32:25 +0000403 ALOGD("%s() %s entering >>>>>>>>>>>>>> COMMANDS", __func__, getTypeText());
Phil Burk3d201942021-04-08 23:27:04 +0000404 // Hold onto the ref counted stream until the end.
405 android::sp<AAudioServiceStreamBase> holdStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800406 TimestampScheduler timestampScheduler;
jiabin2a594622021-10-14 00:32:25 +0000407 int64_t nextTime;
jiabinf7f06152021-11-22 18:10:14 +0000408 int64_t standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
Phil Burk3d201942021-04-08 23:27:04 +0000409 // Balance the incStrong from when the thread was launched.
410 holdStream->decStrong(nullptr);
411
jiabin2a594622021-10-14 00:32:25 +0000412 // Taking mLock while starting the thread. All the operation must be able to
413 // run with holding the lock.
414 std::scoped_lock<std::mutex> _l(mLock);
415
Phil Burka53ffa62018-10-10 16:21:37 -0700416 int32_t loopCount = 0;
jiabin2a594622021-10-14 00:32:25 +0000417 while (mThreadEnabled.load()) {
Phil Burka53ffa62018-10-10 16:21:37 -0700418 loopCount++;
jiabin2a594622021-10-14 00:32:25 +0000419 int64_t timeoutNanos = -1;
jiabinf7f06152021-11-22 18:10:14 +0000420 if (isRunning() || (isIdle_l() && !isStandby_l())) {
421 timeoutNanos = (isRunning() ? nextTime : standbyTime) - AudioClock::getNanoseconds();
jiabin2a594622021-10-14 00:32:25 +0000422 timeoutNanos = std::max<int64_t>(0, timeoutNanos);
423 }
424
425 auto command = mCommandQueue.waitForCommand(timeoutNanos);
426 if (!mThreadEnabled) {
427 // Break the loop if the thread is disabled.
428 break;
429 }
430
431 if (isRunning() && AudioClock::getNanoseconds() >= nextTime) {
432 // It is time to update timestamp.
433 if (sendCurrentTimestamp_l() != AAUDIO_OK) {
434 ALOGE("Failed to send current timestamp, stop updating timestamp");
435 disconnect_l();
436 } else {
437 nextTime = timestampScheduler.nextAbsoluteTime();
Phil Burkc0c70e32017-02-09 13:18:38 -0800438 }
jiabin2a594622021-10-14 00:32:25 +0000439 }
jiabinf7f06152021-11-22 18:10:14 +0000440 if (isIdle_l() && AudioClock::getNanoseconds() >= standbyTime) {
Jason Lin20d04e32022-11-22 13:39:49 +0800441 aaudio_result_t result = standby_l();
442 if (result != AAUDIO_OK) {
443 // If standby failed because of the function is not implemented, there is no
444 // need to retry. Otherwise, retry standby later.
445 ALOGW("Failed to enter standby, error=%d", result);
446 standbyTime = result == AAUDIO_ERROR_UNIMPLEMENTED
447 ? std::numeric_limits<int64_t>::max()
448 : AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
449 }
jiabinf7f06152021-11-22 18:10:14 +0000450 }
jiabin2a594622021-10-14 00:32:25 +0000451
452 if (command != nullptr) {
453 std::scoped_lock<std::mutex> _commandLock(command->lock);
454 switch (command->operationCode) {
455 case START:
456 command->result = start_l();
457 timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate());
458 timestampScheduler.start(AudioClock::getNanoseconds());
459 nextTime = timestampScheduler.nextAbsoluteTime();
460 break;
461 case PAUSE:
462 command->result = pause_l();
jiabinf7f06152021-11-22 18:10:14 +0000463 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
jiabin2a594622021-10-14 00:32:25 +0000464 break;
465 case STOP:
466 command->result = stop_l();
jiabinf7f06152021-11-22 18:10:14 +0000467 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
jiabin2a594622021-10-14 00:32:25 +0000468 break;
469 case FLUSH:
470 command->result = flush_l();
471 break;
472 case CLOSE:
473 command->result = close_l();
474 break;
475 case DISCONNECT:
476 disconnect_l();
477 break;
478 case REGISTER_AUDIO_THREAD: {
jiabin613e6ae2022-12-21 20:20:11 +0000479 auto param = (RegisterAudioThreadParam *) command->parameter.get();
jiabin2a594622021-10-14 00:32:25 +0000480 command->result =
481 param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
482 : registerAudioThread_l(param->mOwnerPid,
483 param->mClientThreadId,
484 param->mPriority);
485 }
486 break;
487 case UNREGISTER_AUDIO_THREAD: {
jiabin613e6ae2022-12-21 20:20:11 +0000488 auto param = (UnregisterAudioThreadParam *) command->parameter.get();
jiabin2a594622021-10-14 00:32:25 +0000489 command->result =
490 param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
491 : unregisterAudioThread_l(param->mClientThreadId);
492 }
493 break;
494 case GET_DESCRIPTION: {
jiabin613e6ae2022-12-21 20:20:11 +0000495 auto param = (GetDescriptionParam *) command->parameter.get();
jiabin2a594622021-10-14 00:32:25 +0000496 command->result = param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
497 : getDescription_l(param->mParcelable);
498 }
499 break;
jiabinf7f06152021-11-22 18:10:14 +0000500 case EXIT_STANDBY: {
jiabin613e6ae2022-12-21 20:20:11 +0000501 auto param = (ExitStandbyParam *) command->parameter.get();
jiabinf7f06152021-11-22 18:10:14 +0000502 command->result = param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
503 : exitStandby_l(param->mParcelable);
504 standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
505 } break;
jiabin2a594622021-10-14 00:32:25 +0000506 default:
507 ALOGE("Invalid command op code: %d", command->operationCode);
508 break;
509 }
510 if (command->isWaitingForReply) {
511 command->isWaitingForReply = false;
512 command->conditionVariable.notify_one();
513 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800514 }
515 }
jiabin2a594622021-10-14 00:32:25 +0000516 ALOGD("%s() %s exiting after %d loops <<<<<<<<<<<<<< COMMANDS",
Phil Burka53ffa62018-10-10 16:21:37 -0700517 __func__, getTypeText(), loopCount);
Phil Burkc0c70e32017-02-09 13:18:38 -0800518}
519
Phil Burk5ef003b2017-06-30 11:43:37 -0700520void AAudioServiceStreamBase::disconnect() {
jiabinba75f212021-12-07 20:06:30 +0000521 sendCommand(DISCONNECT);
Phil Burk7ebbc112020-05-13 15:55:17 -0700522}
523
524void AAudioServiceStreamBase::disconnect_l() {
Phil Burkdb466142021-04-16 16:50:00 +0000525 if (!isDisconnected_l() && getState() != AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700526
Phil Burka9876702020-04-20 18:16:15 -0700527 mediametrics::LogItem(mMetricsId)
528 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DISCONNECT)
529 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
530 .record();
Phil Burk7ebbc112020-05-13 15:55:17 -0700531
Phil Burk5ef003b2017-06-30 11:43:37 -0700532 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkdb466142021-04-16 16:50:00 +0000533 setDisconnected_l(true);
Phil Burk5ef003b2017-06-30 11:43:37 -0700534 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800535}
536
jiabin2a594622021-10-14 00:32:25 +0000537aaudio_result_t AAudioServiceStreamBase::registerAudioThread(pid_t clientThreadId, int priority) {
538 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
jiabinba75f212021-12-07 20:06:30 +0000539 return sendCommand(REGISTER_AUDIO_THREAD,
jiabin2a594622021-10-14 00:32:25 +0000540 std::make_shared<RegisterAudioThreadParam>(ownerPid, clientThreadId, priority),
541 true /*waitForReply*/,
542 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000543}
544
545aaudio_result_t AAudioServiceStreamBase::registerAudioThread_l(
546 pid_t ownerPid, pid_t clientThreadId, int priority) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700547 aaudio_result_t result = AAUDIO_OK;
548 if (getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
549 ALOGE("AAudioService::registerAudioThread(), thread already registered");
550 result = AAUDIO_ERROR_INVALID_STATE;
551 } else {
Phil Burk7ebbc112020-05-13 15:55:17 -0700552 setRegisteredThread(clientThreadId);
553 int err = android::requestPriority(ownerPid, clientThreadId,
554 priority, true /* isForApp */);
555 if (err != 0) {
556 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
557 clientThreadId, errno, priority);
558 result = AAUDIO_ERROR_INTERNAL;
559 }
560 }
561 return result;
562}
563
564aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread(pid_t clientThreadId) {
jiabinba75f212021-12-07 20:06:30 +0000565 return sendCommand(UNREGISTER_AUDIO_THREAD,
jiabin2a594622021-10-14 00:32:25 +0000566 std::make_shared<UnregisterAudioThreadParam>(clientThreadId),
567 true /*waitForReply*/,
568 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000569}
570
571aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread_l(pid_t clientThreadId) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700572 aaudio_result_t result = AAUDIO_OK;
573 if (getRegisteredThread() != clientThreadId) {
574 ALOGE("%s(), wrong thread", __func__);
575 result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
576 } else {
577 setRegisteredThread(0);
578 }
579 return result;
580}
581
582void AAudioServiceStreamBase::setState(aaudio_stream_state_t state) {
583 // CLOSED is a final state.
584 if (mState != AAUDIO_STREAM_STATE_CLOSED) {
585 mState = state;
586 } else {
587 ALOGW_IF(mState != state, "%s(%d) when already CLOSED", __func__, state);
588 }
589}
590
Phil Burkc0c70e32017-02-09 13:18:38 -0800591aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800592 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800593 AAudioServiceMessage command;
594 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800595 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800596 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800597 return writeUpMessageQueue(&command);
598}
599
600aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
601 int64_t dataLong) {
602 AAudioServiceMessage command;
603 command.what = AAudioServiceMessage::code::EVENT;
604 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800605 command.event.dataLong = dataLong;
606 return writeUpMessageQueue(&command);
607}
608
Phil Burkf878a8d2019-03-29 17:23:00 -0700609bool AAudioServiceStreamBase::isUpMessageQueueBusy() {
610 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
611 if (mUpMessageQueue == nullptr) {
612 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
613 return true;
614 }
Phil Burkf878a8d2019-03-29 17:23:00 -0700615 // Is it half full or more
Phil Burk8f4fe502020-07-15 23:54:50 +0000616 return mUpMessageQueue->getFractionalFullness() >= 0.5;
Phil Burkf878a8d2019-03-29 17:23:00 -0700617}
618
Phil Burkc0c70e32017-02-09 13:18:38 -0800619aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700620 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700621 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700622 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700623 return AAUDIO_ERROR_NULL;
624 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800625 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
626 if (count != 1) {
Phil Burk762365c2018-12-10 16:02:16 -0800627 ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s",
628 __func__, command->what, getTypeText());
629 setSuspended(true);
Phil Burkc0c70e32017-02-09 13:18:38 -0800630 return AAUDIO_ERROR_WOULD_BLOCK;
631 } else {
632 return AAUDIO_OK;
633 }
634}
635
Phil Burk23296382017-11-20 15:45:11 -0800636aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
637 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
638}
639
jiabin2a594622021-10-14 00:32:25 +0000640aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800641 AAudioServiceMessage command;
Phil Burkf878a8d2019-03-29 17:23:00 -0700642 // It is not worth filling up the queue with timestamps.
643 // That can cause the stream to get suspended.
644 // So just drop the timestamp if the queue is getting full.
645 if (isUpMessageQueueBusy()) {
646 return AAUDIO_OK;
647 }
648
Phil Burk97350f92017-07-21 15:59:44 -0700649 // Send a timestamp for the clock model.
jiabin2a594622021-10-14 00:32:25 +0000650 aaudio_result_t result = getFreeRunningPosition_l(&command.timestamp.position,
651 &command.timestamp.timestamp);
Phil Burkc0c70e32017-02-09 13:18:38 -0800652 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700653 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700654 (long long) command.timestamp.position,
655 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700656 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800657 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700658
659 if (result == AAUDIO_OK) {
660 // Send a hardware timestamp for presentation time.
jiabin2a594622021-10-14 00:32:25 +0000661 result = getHardwareTimestamp_l(&command.timestamp.position,
662 &command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700663 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700664 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700665 (long long) command.timestamp.position,
666 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700667 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
668 result = writeUpMessageQueue(&command);
669 }
670 }
671 }
672
Phil Burkbcc36742017-08-31 17:24:51 -0700673 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700674 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800675 }
676 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800677}
678
Phil Burkc0c70e32017-02-09 13:18:38 -0800679/**
680 * Get an immutable description of the in-memory queues
681 * used to communicate with the underlying HAL or Service.
682 */
683aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
jiabinba75f212021-12-07 20:06:30 +0000684 return sendCommand(
jiabin2a594622021-10-14 00:32:25 +0000685 GET_DESCRIPTION,
686 std::make_shared<GetDescriptionParam>(&parcelable),
687 true /*waitForReply*/,
688 TIMEOUT_NANOS);
jiabin2a594622021-10-14 00:32:25 +0000689}
690
691aaudio_result_t AAudioServiceStreamBase::getDescription_l(AudioEndpointParcelable* parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700692 {
693 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
694 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700695 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700696 return AAUDIO_ERROR_NULL;
697 }
698 // Gather information on the message queue.
699 mUpMessageQueue->fillParcelable(parcelable,
jiabin2a594622021-10-14 00:32:25 +0000700 parcelable->mUpMessageQueueParcelable);
Phil Burk523b3042017-09-13 13:03:08 -0700701 }
jiabin2a594622021-10-14 00:32:25 +0000702 return getAudioDataDescription_l(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700703}
Phil Burk39f02dd2017-08-04 09:13:31 -0700704
jiabinf7f06152021-11-22 18:10:14 +0000705aaudio_result_t AAudioServiceStreamBase::exitStandby(AudioEndpointParcelable *parcelable) {
706 auto command = std::make_shared<AAudioCommand>(
707 EXIT_STANDBY,
708 std::make_shared<ExitStandbyParam>(parcelable),
709 true /*waitForReply*/,
710 TIMEOUT_NANOS);
711 return mCommandQueue.sendCommand(command);
712}
713
Phil Burk39f02dd2017-08-04 09:13:31 -0700714void AAudioServiceStreamBase::onVolumeChanged(float volume) {
715 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
716}
jiabinba75f212021-12-07 20:06:30 +0000717
718aaudio_result_t AAudioServiceStreamBase::sendCommand(aaudio_command_opcode opCode,
719 std::shared_ptr<AAudioCommandParam> param,
720 bool waitForReply,
721 int64_t timeoutNanos) {
722 return mCommandQueue.sendCommand(std::make_shared<AAudioCommand>(
723 opCode, param, waitForReply, timeoutNanos));
724}
725
726aaudio_result_t AAudioServiceStreamBase::closeAndClear() {
727 aaudio_result_t result = AAUDIO_OK;
728 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
729 if (endpoint == nullptr) {
730 result = AAUDIO_ERROR_INVALID_STATE;
731 } else {
732 endpoint->unregisterStream(this);
733 AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance();
734 endpointManager.closeEndpoint(endpoint);
735
736 // AAudioService::closeStream() prevents two threads from closing at the same time.
737 mServiceEndpoint.clear(); // endpoint will hold the pointer after this method returns.
738 }
739
740 setState(AAUDIO_STREAM_STATE_CLOSED);
741
742 mediametrics::LogItem(mMetricsId)
743 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CLOSE)
744 .record();
745 return result;
746}