blob: 044c361d121477d44f07cf6d107eae2dd039b441 [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>
27
Phil Burkc0c70e32017-02-09 13:18:38 -080028#include "binding/IAAudioService.h"
29#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"
37#include "TimestampScheduler.h"
38
39using namespace android; // TODO just import names needed
40using namespace aaudio; // TODO just import names needed
Phil Burk2355edb2016-12-26 13:54:02 -080041
42/**
Phil Burkc0c70e32017-02-09 13:18:38 -080043 * Base class for streams in the service.
44 * @return
Phil Burk2355edb2016-12-26 13:54:02 -080045 */
46
Phil Burk39f02dd2017-08-04 09:13:31 -070047AAudioServiceStreamBase::AAudioServiceStreamBase(AAudioService &audioService)
Phil Burk2355edb2016-12-26 13:54:02 -080048 : mUpMessageQueue(nullptr)
Phil Burk55978892018-01-11 14:56:09 -080049 , mTimestampThread("AATime")
Phil Burka53ffa62018-10-10 16:21:37 -070050 , mAtomicStreamTimestamp()
Phil Burk39f02dd2017-08-04 09:13:31 -070051 , mAudioService(audioService) {
Eric Laurentcb4dae22017-07-01 19:39:32 -070052 mMmapClient.clientUid = -1;
53 mMmapClient.clientPid = -1;
54 mMmapClient.packageName = String16("");
Phil Burk2355edb2016-12-26 13:54:02 -080055}
56
Phil Burk5ed503c2017-02-01 09:38:15 -080057AAudioServiceStreamBase::~AAudioServiceStreamBase() {
Phil Burka9876702020-04-20 18:16:15 -070058 // May not be set if open failed.
59 if (mMetricsId.size() > 0) {
60 mediametrics::LogItem(mMetricsId)
61 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DTOR)
62 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
63 .record();
64 }
65
Phil Burk5a26e662017-07-07 12:44:48 -070066 // If the stream is deleted when OPEN or in use then audio resources will leak.
67 // This would indicate an internal error. So we want to find this ASAP.
Phil Burkbcc36742017-08-31 17:24:51 -070068 LOG_ALWAYS_FATAL_IF(!(getState() == AAUDIO_STREAM_STATE_CLOSED
69 || getState() == AAUDIO_STREAM_STATE_UNINITIALIZED
70 || getState() == AAUDIO_STREAM_STATE_DISCONNECTED),
Phil Burk8b4e05e2019-12-17 12:12:09 -080071 "service stream %p still open, state = %d",
72 this, getState());
Phil Burk2355edb2016-12-26 13:54:02 -080073}
74
Phil Burka5222e22017-07-28 13:31:14 -070075std::string AAudioServiceStreamBase::dumpHeader() {
Phil Burkbbd52862018-04-13 11:37:42 -070076 return std::string(" T Handle UId Port Run State Format Burst Chan Capacity");
Phil Burka5222e22017-07-28 13:31:14 -070077}
78
Phil Burk4501b352017-06-29 18:12:36 -070079std::string AAudioServiceStreamBase::dump() const {
80 std::stringstream result;
81
Phil Burka5222e22017-07-28 13:31:14 -070082 result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle
83 << std::dec << std::setfill(' ') ;
84 result << std::setw(6) << mMmapClient.clientUid;
Phil Burkbbd52862018-04-13 11:37:42 -070085 result << std::setw(7) << mClientHandle;
Phil Burka5222e22017-07-28 13:31:14 -070086 result << std::setw(4) << (isRunning() ? "yes" : " no");
Phil Burkbcc36742017-08-31 17:24:51 -070087 result << std::setw(6) << getState();
Phil Burk39f02dd2017-08-04 09:13:31 -070088 result << std::setw(7) << getFormat();
Phil Burka5222e22017-07-28 13:31:14 -070089 result << std::setw(6) << mFramesPerBurst;
Phil Burk39f02dd2017-08-04 09:13:31 -070090 result << std::setw(5) << getSamplesPerFrame();
91 result << std::setw(9) << getBufferCapacity();
Phil Burk4501b352017-06-29 18:12:36 -070092
93 return result.str();
94}
95
Phil Burka9876702020-04-20 18:16:15 -070096void AAudioServiceStreamBase::logOpen(aaudio_handle_t streamHandle) {
97 // This is the first log sent from the AAudio Service for a stream.
98 mMetricsId = std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_STREAM)
99 + std::to_string(streamHandle);
100
101 audio_attributes_t attributes = AAudioServiceEndpoint::getAudioAttributesFrom(this);
102
103 // Once this item is logged by the server, the client with the same PID, UID
104 // can also log properties.
105 mediametrics::LogItem(mMetricsId)
106 .setPid(getOwnerProcessId())
107 .setUid(getOwnerUserId())
108 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_OPEN)
109 // the following are immutable
110 .set(AMEDIAMETRICS_PROP_BUFFERCAPACITYFRAMES, (int32_t)getBufferCapacity())
111 .set(AMEDIAMETRICS_PROP_BURSTFRAMES, (int32_t)getFramesPerBurst())
112 .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)getSamplesPerFrame())
113 .set(AMEDIAMETRICS_PROP_CONTENTTYPE, toString(attributes.content_type).c_str())
114 .set(AMEDIAMETRICS_PROP_DIRECTION,
115 AudioGlobal_convertDirectionToText(getDirection()))
116 .set(AMEDIAMETRICS_PROP_ENCODING, toString(getFormat()).c_str())
117 .set(AMEDIAMETRICS_PROP_ROUTEDDEVICEID, (int32_t)getDeviceId())
118 .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)getSampleRate())
119 .set(AMEDIAMETRICS_PROP_SESSIONID, (int32_t)getSessionId())
120 .set(AMEDIAMETRICS_PROP_SOURCE, toString(attributes.source).c_str())
121 .set(AMEDIAMETRICS_PROP_USAGE, toString(attributes.usage).c_str())
122 .record();
123}
124
Phil Burk15f97c92018-09-04 14:06:27 -0700125aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700126 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
127 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700128
129 mMmapClient.clientUid = request.getUserId();
130 mMmapClient.clientPid = request.getProcessId();
Phil Burk39f02dd2017-08-04 09:13:31 -0700131 mMmapClient.packageName.setTo(String16("")); // TODO What should we do here?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700132
Phil Burk39f02dd2017-08-04 09:13:31 -0700133 // Limit scope of lock to avoid recursive lock in close().
134 {
135 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
136 if (mUpMessageQueue != nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700137 ALOGE("%s() called twice", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700138 return AAUDIO_ERROR_INVALID_STATE;
139 }
140
Phil Burkc0c70e32017-02-09 13:18:38 -0800141 mUpMessageQueue = new SharedRingBuffer();
Phil Burk39f02dd2017-08-04 09:13:31 -0700142 result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage),
143 QUEUE_UP_CAPACITY_COMMANDS);
144 if (result != AAUDIO_OK) {
145 goto error;
146 }
147
Phil Burk6e2770e2018-05-01 13:03:52 -0700148 // This is not protected by a lock because the stream cannot be
149 // referenced until the service returns a handle to the client.
150 // So only one thread can open a stream.
Phil Burk39f02dd2017-08-04 09:13:31 -0700151 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService,
Phil Burk15f97c92018-09-04 14:06:27 -0700152 request);
Phil Burk39f02dd2017-08-04 09:13:31 -0700153 if (mServiceEndpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700154 result = AAUDIO_ERROR_UNAVAILABLE;
155 goto error;
156 }
Phil Burk6e2770e2018-05-01 13:03:52 -0700157 // Save a weak pointer that we will use to access the endpoint.
158 mServiceEndpointWeak = mServiceEndpoint;
159
Phil Burk39f02dd2017-08-04 09:13:31 -0700160 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
161 copyFrom(*mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800162 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700163 return result;
164
165error:
166 close();
167 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800168}
Phil Burkdec33ab2017-01-17 14:48:16 -0800169
Phil Burkc0c70e32017-02-09 13:18:38 -0800170aaudio_result_t AAudioServiceStreamBase::close() {
Phil Burkbcc36742017-08-31 17:24:51 -0700171 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700172 return AAUDIO_OK;
173 }
174
175 stop();
176
Phil Burk8b4e05e2019-12-17 12:12:09 -0800177 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700178 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
179 if (endpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700180 result = AAUDIO_ERROR_INVALID_STATE;
181 } else {
Phil Burk6e2770e2018-05-01 13:03:52 -0700182 endpoint->unregisterStream(this);
183 AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance();
184 endpointManager.closeEndpoint(endpoint);
185
186 // AAudioService::closeStream() prevents two threads from closing at the same time.
187 mServiceEndpoint.clear(); // endpoint will hold the pointer until this method returns.
Phil Burk39f02dd2017-08-04 09:13:31 -0700188 }
189
190 {
191 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk98d6d922017-07-06 11:52:45 -0700192 stopTimestampThread();
Phil Burk98d6d922017-07-06 11:52:45 -0700193 delete mUpMessageQueue;
194 mUpMessageQueue = nullptr;
Phil Burk98d6d922017-07-06 11:52:45 -0700195 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700196
Phil Burkbcc36742017-08-31 17:24:51 -0700197 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burka9876702020-04-20 18:16:15 -0700198
199 mediametrics::LogItem(mMetricsId)
200 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CLOSE)
201 .record();
Phil Burk39f02dd2017-08-04 09:13:31 -0700202 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800203}
204
Phil Burkbcc36742017-08-31 17:24:51 -0700205aaudio_result_t AAudioServiceStreamBase::startDevice() {
206 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burk6e2770e2018-05-01 13:03:52 -0700207 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
208 if (endpoint == nullptr) {
209 ALOGE("%s() has no endpoint", __func__);
210 return AAUDIO_ERROR_INVALID_STATE;
211 }
212 return endpoint->startStream(this, &mClientHandle);
Phil Burkbcc36742017-08-31 17:24:51 -0700213}
214
Phil Burk39f02dd2017-08-04 09:13:31 -0700215/**
216 * Start the flow of audio data.
217 *
218 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
219 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800220aaudio_result_t AAudioServiceStreamBase::start() {
Phil Burka9876702020-04-20 18:16:15 -0700221 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burkbcc36742017-08-31 17:24:51 -0700222 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700223
Phil Burka9876702020-04-20 18:16:15 -0700224 mediametrics::Defer defer([&] {
225 mediametrics::LogItem(mMetricsId)
226 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START)
227 .set(AMEDIAMETRICS_PROP_DURATIONNS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
228 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
229 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
230 .record(); });
231
Eric Laurentcb4dae22017-07-01 19:39:32 -0700232 if (isRunning()) {
233 return AAUDIO_OK;
234 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700235
Phil Burk23296382017-11-20 15:45:11 -0800236 setFlowing(false);
Phil Burk762365c2018-12-10 16:02:16 -0800237 setSuspended(false);
Phil Burk23296382017-11-20 15:45:11 -0800238
Phil Burkbcc36742017-08-31 17:24:51 -0700239 // Start with fresh presentation timestamps.
Phil Burka53ffa62018-10-10 16:21:37 -0700240 mAtomicStreamTimestamp.clear();
Phil Burkbcc36742017-08-31 17:24:51 -0700241
Phil Burk39f02dd2017-08-04 09:13:31 -0700242 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burkbcc36742017-08-31 17:24:51 -0700243 result = startDevice();
244 if (result != AAUDIO_OK) goto error;
245
246 // This should happen at the end of the start.
247 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
248 setState(AAUDIO_STREAM_STATE_STARTED);
249 mThreadEnabled.store(true);
250 result = mTimestampThread.start(this);
251 if (result != AAUDIO_OK) goto error;
252
253 return result;
254
255error:
256 disconnect();
Phil Burk39f02dd2017-08-04 09:13:31 -0700257 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800258}
259
260aaudio_result_t AAudioServiceStreamBase::pause() {
Phil Burka9876702020-04-20 18:16:15 -0700261 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk11e8d332017-05-24 09:59:02 -0700262 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700263 if (!isRunning()) {
264 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800265 }
Phil Burk73af62a2017-10-26 12:11:47 -0700266
Phil Burka9876702020-04-20 18:16:15 -0700267 mediametrics::Defer defer([&] {
268 mediametrics::LogItem(mMetricsId)
269 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_PAUSE)
270 .set(AMEDIAMETRICS_PROP_DURATIONNS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
271 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
272 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
273 .record(); });
274
Phil Burk73af62a2017-10-26 12:11:47 -0700275 // Send it now because the timestamp gets rounded up when stopStream() is called below.
276 // Also we don't need the timestamps while we are shutting down.
277 sendCurrentTimestamp();
278
279 result = stopTimestampThread();
280 if (result != AAUDIO_OK) {
281 disconnect();
282 return result;
283 }
284
Phil Burk6e2770e2018-05-01 13:03:52 -0700285 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
286 if (endpoint == nullptr) {
287 ALOGE("%s() has no endpoint", __func__);
Phil Burka9876702020-04-20 18:16:15 -0700288 result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking
289 return result;
Phil Burk6e2770e2018-05-01 13:03:52 -0700290 }
291 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700292 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700293 ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700294 disconnect(); // TODO should we return or pause Base first?
295 }
296
Eric Laurentcb4dae22017-07-01 19:39:32 -0700297 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkbcc36742017-08-31 17:24:51 -0700298 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800299 return result;
300}
301
Phil Burk71f35bb2017-04-13 16:05:07 -0700302aaudio_result_t AAudioServiceStreamBase::stop() {
Phil Burka9876702020-04-20 18:16:15 -0700303 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk11e8d332017-05-24 09:59:02 -0700304 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700305 if (!isRunning()) {
306 return result;
Phil Burk71f35bb2017-04-13 16:05:07 -0700307 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700308
Phil Burka9876702020-04-20 18:16:15 -0700309 mediametrics::Defer defer([&] {
310 mediametrics::LogItem(mMetricsId)
311 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP)
312 .set(AMEDIAMETRICS_PROP_DURATIONNS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
313 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
314 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
315 .record(); });
316
Phil Burk83fb8442017-10-05 16:55:17 -0700317 setState(AAUDIO_STREAM_STATE_STOPPING);
318
Phil Burk73af62a2017-10-26 12:11:47 -0700319 // Send it now because the timestamp gets rounded up when stopStream() is called below.
320 // Also we don't need the timestamps while we are shutting down.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700321 sendCurrentTimestamp(); // warning - this calls a virtual function
322 result = stopTimestampThread();
323 if (result != AAUDIO_OK) {
324 disconnect();
325 return result;
326 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700327
Phil Burk6e2770e2018-05-01 13:03:52 -0700328 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
329 if (endpoint == nullptr) {
330 ALOGE("%s() has no endpoint", __func__);
Phil Burka9876702020-04-20 18:16:15 -0700331 result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking
332 return result;
Phil Burk6e2770e2018-05-01 13:03:52 -0700333 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700334 // TODO wait for data to be played out
Phil Burk6e2770e2018-05-01 13:03:52 -0700335 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700336 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700337 ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700338 disconnect();
339 // TODO what to do with result here?
340 }
341
Eric Laurentcb4dae22017-07-01 19:39:32 -0700342 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burkbcc36742017-08-31 17:24:51 -0700343 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700344 return result;
345}
346
Phil Burk98d6d922017-07-06 11:52:45 -0700347aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() {
348 aaudio_result_t result = AAUDIO_OK;
349 // clear flag that tells thread to loop
350 if (mThreadEnabled.exchange(false)) {
Phil Burkbcc36742017-08-31 17:24:51 -0700351 result = mTimestampThread.stop();
Phil Burk98d6d922017-07-06 11:52:45 -0700352 }
353 return result;
354}
355
Phil Burk71f35bb2017-04-13 16:05:07 -0700356aaudio_result_t AAudioServiceStreamBase::flush() {
Phil Burka9876702020-04-20 18:16:15 -0700357 const int64_t beginNs = AudioClock::getNanoseconds();
Phil Burk5cc83c32017-11-28 15:43:18 -0800358 aaudio_result_t result = AAudio_isFlushAllowed(getState());
359 if (result != AAUDIO_OK) {
360 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700361 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800362
Phil Burka9876702020-04-20 18:16:15 -0700363 mediametrics::Defer defer([&] {
364 mediametrics::LogItem(mMetricsId)
365 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_FLUSH)
366 .set(AMEDIAMETRICS_PROP_DURATIONNS, (int64_t)(AudioClock::getNanoseconds() - beginNs))
367 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
368 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
369 .record(); });
370
Phil Burk39f02dd2017-08-04 09:13:31 -0700371 // Data will get flushed when the client receives the FLUSHED event.
Phil Burk71f35bb2017-04-13 16:05:07 -0700372 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
Phil Burkbcc36742017-08-31 17:24:51 -0700373 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700374 return AAUDIO_OK;
375}
376
Phil Burkcf5f6d22017-05-26 12:35:07 -0700377// implement Runnable, periodically send timestamps to client
Phil Burka53ffa62018-10-10 16:21:37 -0700378__attribute__((no_sanitize("integer")))
Phil Burkc0c70e32017-02-09 13:18:38 -0800379void AAudioServiceStreamBase::run() {
Phil Burk19e990e2018-03-22 13:59:34 -0700380 ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800381 TimestampScheduler timestampScheduler;
Phil Burk39f02dd2017-08-04 09:13:31 -0700382 timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate());
Phil Burkc0c70e32017-02-09 13:18:38 -0800383 timestampScheduler.start(AudioClock::getNanoseconds());
384 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
Phil Burka53ffa62018-10-10 16:21:37 -0700385 int32_t loopCount = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800386 while(mThreadEnabled.load()) {
Phil Burka53ffa62018-10-10 16:21:37 -0700387 loopCount++;
Phil Burkc0c70e32017-02-09 13:18:38 -0800388 if (AudioClock::getNanoseconds() >= nextTime) {
389 aaudio_result_t result = sendCurrentTimestamp();
390 if (result != AAUDIO_OK) {
Phil Burka53ffa62018-10-10 16:21:37 -0700391 ALOGE("%s() timestamp thread got result = %d", __func__, result);
Phil Burkc0c70e32017-02-09 13:18:38 -0800392 break;
393 }
394 nextTime = timestampScheduler.nextAbsoluteTime();
395 } else {
396 // Sleep until it is time to send the next timestamp.
Phil Burk98d6d922017-07-06 11:52:45 -0700397 // TODO Wait for a signal with a timeout so that we can stop more quickly.
Phil Burkc0c70e32017-02-09 13:18:38 -0800398 AudioClock::sleepUntilNanoTime(nextTime);
399 }
400 }
Phil Burka53ffa62018-10-10 16:21:37 -0700401 ALOGD("%s() %s exiting after %d loops <<<<<<<<<<<<<< TIMESTAMPS",
402 __func__, getTypeText(), loopCount);
Phil Burkc0c70e32017-02-09 13:18:38 -0800403}
404
Phil Burk5ef003b2017-06-30 11:43:37 -0700405void AAudioServiceStreamBase::disconnect() {
Phil Burkbcc36742017-08-31 17:24:51 -0700406 if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burka9876702020-04-20 18:16:15 -0700407 mediametrics::LogItem(mMetricsId)
408 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DISCONNECT)
409 .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState()))
410 .record();
Phil Burk5ef003b2017-06-30 11:43:37 -0700411 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkbcc36742017-08-31 17:24:51 -0700412 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burk5ef003b2017-06-30 11:43:37 -0700413 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800414}
415
416aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800417 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800418 AAudioServiceMessage command;
419 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800420 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800421 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800422 return writeUpMessageQueue(&command);
423}
424
425aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
426 int64_t dataLong) {
427 AAudioServiceMessage command;
428 command.what = AAudioServiceMessage::code::EVENT;
429 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800430 command.event.dataLong = dataLong;
431 return writeUpMessageQueue(&command);
432}
433
Phil Burkf878a8d2019-03-29 17:23:00 -0700434bool AAudioServiceStreamBase::isUpMessageQueueBusy() {
435 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
436 if (mUpMessageQueue == nullptr) {
437 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
438 return true;
439 }
440 int32_t framesAvailable = mUpMessageQueue->getFifoBuffer()
441 ->getFullFramesAvailable();
442 int32_t capacity = mUpMessageQueue->getFifoBuffer()
443 ->getBufferCapacityInFrames();
444 // Is it half full or more
445 return framesAvailable >= (capacity / 2);
446}
447
Phil Burkc0c70e32017-02-09 13:18:38 -0800448aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700449 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700450 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700451 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700452 return AAUDIO_ERROR_NULL;
453 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800454 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
455 if (count != 1) {
Phil Burk762365c2018-12-10 16:02:16 -0800456 ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s",
457 __func__, command->what, getTypeText());
458 setSuspended(true);
Phil Burkc0c70e32017-02-09 13:18:38 -0800459 return AAUDIO_ERROR_WOULD_BLOCK;
460 } else {
461 return AAUDIO_OK;
462 }
463}
464
Phil Burk23296382017-11-20 15:45:11 -0800465aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
466 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
467}
468
Phil Burkc0c70e32017-02-09 13:18:38 -0800469aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
470 AAudioServiceMessage command;
Phil Burkf878a8d2019-03-29 17:23:00 -0700471 // It is not worth filling up the queue with timestamps.
472 // That can cause the stream to get suspended.
473 // So just drop the timestamp if the queue is getting full.
474 if (isUpMessageQueueBusy()) {
475 return AAUDIO_OK;
476 }
477
Phil Burk97350f92017-07-21 15:59:44 -0700478 // Send a timestamp for the clock model.
Phil Burkc0c70e32017-02-09 13:18:38 -0800479 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
480 &command.timestamp.timestamp);
481 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700482 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700483 (long long) command.timestamp.position,
484 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700485 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800486 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700487
488 if (result == AAUDIO_OK) {
489 // Send a hardware timestamp for presentation time.
490 result = getHardwareTimestamp(&command.timestamp.position,
491 &command.timestamp.timestamp);
492 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700493 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700494 (long long) command.timestamp.position,
495 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700496 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
497 result = writeUpMessageQueue(&command);
498 }
499 }
500 }
501
Phil Burkbcc36742017-08-31 17:24:51 -0700502 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700503 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800504 }
505 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800506}
507
Phil Burkc0c70e32017-02-09 13:18:38 -0800508/**
509 * Get an immutable description of the in-memory queues
510 * used to communicate with the underlying HAL or Service.
511 */
512aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700513 {
514 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
515 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700516 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700517 return AAUDIO_ERROR_NULL;
518 }
519 // Gather information on the message queue.
520 mUpMessageQueue->fillParcelable(parcelable,
521 parcelable.mUpMessageQueueParcelable);
522 }
523 return getAudioDataDescription(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700524}
Phil Burk39f02dd2017-08-04 09:13:31 -0700525
526void AAudioServiceStreamBase::onVolumeChanged(float volume) {
527 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
528}
Phil Burk94862522017-09-13 21:31:36 -0700529
Phil Burk2fe718b2018-05-14 12:28:32 -0700530int32_t AAudioServiceStreamBase::incrementServiceReferenceCount_l() {
Phil Burk94862522017-09-13 21:31:36 -0700531 return ++mCallingCount;
532}
533
Phil Burk2fe718b2018-05-14 12:28:32 -0700534int32_t AAudioServiceStreamBase::decrementServiceReferenceCount_l() {
535 int32_t count = --mCallingCount;
536 // Each call to increment should be balanced with one call to decrement.
537 assert(count >= 0);
538 return count;
Phil Burk94862522017-09-13 21:31:36 -0700539}