blob: 39e90b130bb0a1a60956b4dc9b10fbbbb6038be6 [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 Burkc0c70e32017-02-09 13:18:38 -080025#include "binding/IAAudioService.h"
26#include "binding/AAudioServiceMessage.h"
27#include "utility/AudioClock.h"
28
Phil Burk39f02dd2017-08-04 09:13:31 -070029#include "AAudioEndpointManager.h"
30#include "AAudioService.h"
31#include "AAudioServiceEndpoint.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080032#include "AAudioServiceStreamBase.h"
33#include "TimestampScheduler.h"
34
35using namespace android; // TODO just import names needed
36using namespace aaudio; // TODO just import names needed
Phil Burk2355edb2016-12-26 13:54:02 -080037
38/**
Phil Burkc0c70e32017-02-09 13:18:38 -080039 * Base class for streams in the service.
40 * @return
Phil Burk2355edb2016-12-26 13:54:02 -080041 */
42
Phil Burk39f02dd2017-08-04 09:13:31 -070043AAudioServiceStreamBase::AAudioServiceStreamBase(AAudioService &audioService)
Phil Burk2355edb2016-12-26 13:54:02 -080044 : mUpMessageQueue(nullptr)
Phil Burk55978892018-01-11 14:56:09 -080045 , mTimestampThread("AATime")
Phil Burka53ffa62018-10-10 16:21:37 -070046 , mAtomicStreamTimestamp()
Phil Burk39f02dd2017-08-04 09:13:31 -070047 , mAudioService(audioService) {
Eric Laurentcb4dae22017-07-01 19:39:32 -070048 mMmapClient.clientUid = -1;
49 mMmapClient.clientPid = -1;
50 mMmapClient.packageName = String16("");
Phil Burk2355edb2016-12-26 13:54:02 -080051}
52
Phil Burk5ed503c2017-02-01 09:38:15 -080053AAudioServiceStreamBase::~AAudioServiceStreamBase() {
Phil Burk5a26e662017-07-07 12:44:48 -070054 // If the stream is deleted when OPEN or in use then audio resources will leak.
55 // This would indicate an internal error. So we want to find this ASAP.
Phil Burkbcc36742017-08-31 17:24:51 -070056 LOG_ALWAYS_FATAL_IF(!(getState() == AAUDIO_STREAM_STATE_CLOSED
57 || getState() == AAUDIO_STREAM_STATE_UNINITIALIZED
58 || getState() == AAUDIO_STREAM_STATE_DISCONNECTED),
Phil Burk8b4e05e2019-12-17 12:12:09 -080059 "service stream %p still open, state = %d",
60 this, getState());
Phil Burk2355edb2016-12-26 13:54:02 -080061}
62
Phil Burka5222e22017-07-28 13:31:14 -070063std::string AAudioServiceStreamBase::dumpHeader() {
Phil Burkbbd52862018-04-13 11:37:42 -070064 return std::string(" T Handle UId Port Run State Format Burst Chan Capacity");
Phil Burka5222e22017-07-28 13:31:14 -070065}
66
Phil Burk4501b352017-06-29 18:12:36 -070067std::string AAudioServiceStreamBase::dump() const {
68 std::stringstream result;
69
Phil Burka5222e22017-07-28 13:31:14 -070070 result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle
71 << std::dec << std::setfill(' ') ;
72 result << std::setw(6) << mMmapClient.clientUid;
Phil Burkbbd52862018-04-13 11:37:42 -070073 result << std::setw(7) << mClientHandle;
Phil Burka5222e22017-07-28 13:31:14 -070074 result << std::setw(4) << (isRunning() ? "yes" : " no");
Phil Burkbcc36742017-08-31 17:24:51 -070075 result << std::setw(6) << getState();
Phil Burk39f02dd2017-08-04 09:13:31 -070076 result << std::setw(7) << getFormat();
Phil Burka5222e22017-07-28 13:31:14 -070077 result << std::setw(6) << mFramesPerBurst;
Phil Burk39f02dd2017-08-04 09:13:31 -070078 result << std::setw(5) << getSamplesPerFrame();
79 result << std::setw(9) << getBufferCapacity();
Phil Burk4501b352017-06-29 18:12:36 -070080
81 return result.str();
82}
83
Phil Burk15f97c92018-09-04 14:06:27 -070084aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request) {
Phil Burk39f02dd2017-08-04 09:13:31 -070085 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
86 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -070087
88 mMmapClient.clientUid = request.getUserId();
89 mMmapClient.clientPid = request.getProcessId();
Phil Burk39f02dd2017-08-04 09:13:31 -070090 mMmapClient.packageName.setTo(String16("")); // TODO What should we do here?
Eric Laurentcb4dae22017-07-01 19:39:32 -070091
Phil Burk39f02dd2017-08-04 09:13:31 -070092 // Limit scope of lock to avoid recursive lock in close().
93 {
94 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
95 if (mUpMessageQueue != nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -070096 ALOGE("%s() called twice", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -070097 return AAUDIO_ERROR_INVALID_STATE;
98 }
99
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 mUpMessageQueue = new SharedRingBuffer();
Phil Burk39f02dd2017-08-04 09:13:31 -0700101 result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage),
102 QUEUE_UP_CAPACITY_COMMANDS);
103 if (result != AAUDIO_OK) {
104 goto error;
105 }
106
Phil Burk6e2770e2018-05-01 13:03:52 -0700107 // This is not protected by a lock because the stream cannot be
108 // referenced until the service returns a handle to the client.
109 // So only one thread can open a stream.
Phil Burk39f02dd2017-08-04 09:13:31 -0700110 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService,
Phil Burk15f97c92018-09-04 14:06:27 -0700111 request);
Phil Burk39f02dd2017-08-04 09:13:31 -0700112 if (mServiceEndpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700113 result = AAUDIO_ERROR_UNAVAILABLE;
114 goto error;
115 }
Phil Burk6e2770e2018-05-01 13:03:52 -0700116 // Save a weak pointer that we will use to access the endpoint.
117 mServiceEndpointWeak = mServiceEndpoint;
118
Phil Burk39f02dd2017-08-04 09:13:31 -0700119 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
120 copyFrom(*mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800121 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700122 return result;
123
124error:
125 close();
126 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800127}
Phil Burkdec33ab2017-01-17 14:48:16 -0800128
Phil Burkc0c70e32017-02-09 13:18:38 -0800129aaudio_result_t AAudioServiceStreamBase::close() {
Phil Burkbcc36742017-08-31 17:24:51 -0700130 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700131 return AAUDIO_OK;
132 }
133
134 stop();
135
Phil Burk8b4e05e2019-12-17 12:12:09 -0800136 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700137 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
138 if (endpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700139 result = AAUDIO_ERROR_INVALID_STATE;
140 } else {
Phil Burk6e2770e2018-05-01 13:03:52 -0700141 endpoint->unregisterStream(this);
142 AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance();
143 endpointManager.closeEndpoint(endpoint);
144
145 // AAudioService::closeStream() prevents two threads from closing at the same time.
146 mServiceEndpoint.clear(); // endpoint will hold the pointer until this method returns.
Phil Burk39f02dd2017-08-04 09:13:31 -0700147 }
148
149 {
150 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk98d6d922017-07-06 11:52:45 -0700151 stopTimestampThread();
Phil Burk98d6d922017-07-06 11:52:45 -0700152 delete mUpMessageQueue;
153 mUpMessageQueue = nullptr;
Phil Burk98d6d922017-07-06 11:52:45 -0700154 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700155
Phil Burkbcc36742017-08-31 17:24:51 -0700156 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800158}
159
Phil Burkbcc36742017-08-31 17:24:51 -0700160aaudio_result_t AAudioServiceStreamBase::startDevice() {
161 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burk6e2770e2018-05-01 13:03:52 -0700162 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
163 if (endpoint == nullptr) {
164 ALOGE("%s() has no endpoint", __func__);
165 return AAUDIO_ERROR_INVALID_STATE;
166 }
167 return endpoint->startStream(this, &mClientHandle);
Phil Burkbcc36742017-08-31 17:24:51 -0700168}
169
Phil Burk39f02dd2017-08-04 09:13:31 -0700170/**
171 * Start the flow of audio data.
172 *
173 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
174 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800175aaudio_result_t AAudioServiceStreamBase::start() {
Phil Burkbcc36742017-08-31 17:24:51 -0700176 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700177
Eric Laurentcb4dae22017-07-01 19:39:32 -0700178 if (isRunning()) {
179 return AAUDIO_OK;
180 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700181
Phil Burk23296382017-11-20 15:45:11 -0800182 setFlowing(false);
Phil Burk762365c2018-12-10 16:02:16 -0800183 setSuspended(false);
Phil Burk23296382017-11-20 15:45:11 -0800184
Phil Burkbcc36742017-08-31 17:24:51 -0700185 // Start with fresh presentation timestamps.
Phil Burka53ffa62018-10-10 16:21:37 -0700186 mAtomicStreamTimestamp.clear();
Phil Burkbcc36742017-08-31 17:24:51 -0700187
Phil Burk39f02dd2017-08-04 09:13:31 -0700188 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burkbcc36742017-08-31 17:24:51 -0700189 result = startDevice();
190 if (result != AAUDIO_OK) goto error;
191
192 // This should happen at the end of the start.
193 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
194 setState(AAUDIO_STREAM_STATE_STARTED);
195 mThreadEnabled.store(true);
196 result = mTimestampThread.start(this);
197 if (result != AAUDIO_OK) goto error;
198
199 return result;
200
201error:
202 disconnect();
Phil Burk39f02dd2017-08-04 09:13:31 -0700203 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800204}
205
206aaudio_result_t AAudioServiceStreamBase::pause() {
Phil Burk11e8d332017-05-24 09:59:02 -0700207 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700208 if (!isRunning()) {
209 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800210 }
Phil Burk73af62a2017-10-26 12:11:47 -0700211
212 // Send it now because the timestamp gets rounded up when stopStream() is called below.
213 // Also we don't need the timestamps while we are shutting down.
214 sendCurrentTimestamp();
215
216 result = stopTimestampThread();
217 if (result != AAUDIO_OK) {
218 disconnect();
219 return result;
220 }
221
Phil Burk6e2770e2018-05-01 13:03:52 -0700222 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
223 if (endpoint == nullptr) {
224 ALOGE("%s() has no endpoint", __func__);
225 return AAUDIO_ERROR_INVALID_STATE;
226 }
227 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700228 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700229 ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700230 disconnect(); // TODO should we return or pause Base first?
231 }
232
Eric Laurentcb4dae22017-07-01 19:39:32 -0700233 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkbcc36742017-08-31 17:24:51 -0700234 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800235 return result;
236}
237
Phil Burk71f35bb2017-04-13 16:05:07 -0700238aaudio_result_t AAudioServiceStreamBase::stop() {
Phil Burk11e8d332017-05-24 09:59:02 -0700239 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700240 if (!isRunning()) {
241 return result;
Phil Burk71f35bb2017-04-13 16:05:07 -0700242 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700243
Phil Burk83fb8442017-10-05 16:55:17 -0700244 setState(AAUDIO_STREAM_STATE_STOPPING);
245
Phil Burk73af62a2017-10-26 12:11:47 -0700246 // Send it now because the timestamp gets rounded up when stopStream() is called below.
247 // Also we don't need the timestamps while we are shutting down.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700248 sendCurrentTimestamp(); // warning - this calls a virtual function
249 result = stopTimestampThread();
250 if (result != AAUDIO_OK) {
251 disconnect();
252 return result;
253 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700254
Phil Burk6e2770e2018-05-01 13:03:52 -0700255 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
256 if (endpoint == nullptr) {
257 ALOGE("%s() has no endpoint", __func__);
258 return AAUDIO_ERROR_INVALID_STATE;
259 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700260 // TODO wait for data to be played out
Phil Burk6e2770e2018-05-01 13:03:52 -0700261 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700262 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700263 ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700264 disconnect();
265 // TODO what to do with result here?
266 }
267
Eric Laurentcb4dae22017-07-01 19:39:32 -0700268 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burkbcc36742017-08-31 17:24:51 -0700269 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700270 return result;
271}
272
Phil Burk98d6d922017-07-06 11:52:45 -0700273aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() {
274 aaudio_result_t result = AAUDIO_OK;
275 // clear flag that tells thread to loop
276 if (mThreadEnabled.exchange(false)) {
Phil Burkbcc36742017-08-31 17:24:51 -0700277 result = mTimestampThread.stop();
Phil Burk98d6d922017-07-06 11:52:45 -0700278 }
279 return result;
280}
281
Phil Burk71f35bb2017-04-13 16:05:07 -0700282aaudio_result_t AAudioServiceStreamBase::flush() {
Phil Burk5cc83c32017-11-28 15:43:18 -0800283 aaudio_result_t result = AAudio_isFlushAllowed(getState());
284 if (result != AAUDIO_OK) {
285 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700286 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800287
Phil Burk39f02dd2017-08-04 09:13:31 -0700288 // Data will get flushed when the client receives the FLUSHED event.
Phil Burk71f35bb2017-04-13 16:05:07 -0700289 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
Phil Burkbcc36742017-08-31 17:24:51 -0700290 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700291 return AAUDIO_OK;
292}
293
Phil Burkcf5f6d22017-05-26 12:35:07 -0700294// implement Runnable, periodically send timestamps to client
Phil Burka53ffa62018-10-10 16:21:37 -0700295__attribute__((no_sanitize("integer")))
Phil Burkc0c70e32017-02-09 13:18:38 -0800296void AAudioServiceStreamBase::run() {
Phil Burk19e990e2018-03-22 13:59:34 -0700297 ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800298 TimestampScheduler timestampScheduler;
Phil Burk39f02dd2017-08-04 09:13:31 -0700299 timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate());
Phil Burkc0c70e32017-02-09 13:18:38 -0800300 timestampScheduler.start(AudioClock::getNanoseconds());
301 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
Phil Burka53ffa62018-10-10 16:21:37 -0700302 int32_t loopCount = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800303 while(mThreadEnabled.load()) {
Phil Burka53ffa62018-10-10 16:21:37 -0700304 loopCount++;
Phil Burkc0c70e32017-02-09 13:18:38 -0800305 if (AudioClock::getNanoseconds() >= nextTime) {
306 aaudio_result_t result = sendCurrentTimestamp();
307 if (result != AAUDIO_OK) {
Phil Burka53ffa62018-10-10 16:21:37 -0700308 ALOGE("%s() timestamp thread got result = %d", __func__, result);
Phil Burkc0c70e32017-02-09 13:18:38 -0800309 break;
310 }
311 nextTime = timestampScheduler.nextAbsoluteTime();
312 } else {
313 // Sleep until it is time to send the next timestamp.
Phil Burk98d6d922017-07-06 11:52:45 -0700314 // TODO Wait for a signal with a timeout so that we can stop more quickly.
Phil Burkc0c70e32017-02-09 13:18:38 -0800315 AudioClock::sleepUntilNanoTime(nextTime);
316 }
317 }
Phil Burka53ffa62018-10-10 16:21:37 -0700318 ALOGD("%s() %s exiting after %d loops <<<<<<<<<<<<<< TIMESTAMPS",
319 __func__, getTypeText(), loopCount);
Phil Burkc0c70e32017-02-09 13:18:38 -0800320}
321
Phil Burk5ef003b2017-06-30 11:43:37 -0700322void AAudioServiceStreamBase::disconnect() {
Phil Burkbcc36742017-08-31 17:24:51 -0700323 if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700324 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkbcc36742017-08-31 17:24:51 -0700325 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burk5ef003b2017-06-30 11:43:37 -0700326 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800327}
328
329aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800330 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800331 AAudioServiceMessage command;
332 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800333 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800334 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800335 return writeUpMessageQueue(&command);
336}
337
338aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
339 int64_t dataLong) {
340 AAudioServiceMessage command;
341 command.what = AAudioServiceMessage::code::EVENT;
342 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800343 command.event.dataLong = dataLong;
344 return writeUpMessageQueue(&command);
345}
346
Phil Burkf878a8d2019-03-29 17:23:00 -0700347bool AAudioServiceStreamBase::isUpMessageQueueBusy() {
348 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
349 if (mUpMessageQueue == nullptr) {
350 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
351 return true;
352 }
353 int32_t framesAvailable = mUpMessageQueue->getFifoBuffer()
354 ->getFullFramesAvailable();
355 int32_t capacity = mUpMessageQueue->getFifoBuffer()
356 ->getBufferCapacityInFrames();
357 // Is it half full or more
358 return framesAvailable >= (capacity / 2);
359}
360
Phil Burkc0c70e32017-02-09 13:18:38 -0800361aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700362 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700363 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700364 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700365 return AAUDIO_ERROR_NULL;
366 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800367 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
368 if (count != 1) {
Phil Burk762365c2018-12-10 16:02:16 -0800369 ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s",
370 __func__, command->what, getTypeText());
371 setSuspended(true);
Phil Burkc0c70e32017-02-09 13:18:38 -0800372 return AAUDIO_ERROR_WOULD_BLOCK;
373 } else {
374 return AAUDIO_OK;
375 }
376}
377
Phil Burk23296382017-11-20 15:45:11 -0800378aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
379 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
380}
381
Phil Burkc0c70e32017-02-09 13:18:38 -0800382aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
383 AAudioServiceMessage command;
Phil Burkf878a8d2019-03-29 17:23:00 -0700384 // It is not worth filling up the queue with timestamps.
385 // That can cause the stream to get suspended.
386 // So just drop the timestamp if the queue is getting full.
387 if (isUpMessageQueueBusy()) {
388 return AAUDIO_OK;
389 }
390
Phil Burk97350f92017-07-21 15:59:44 -0700391 // Send a timestamp for the clock model.
Phil Burkc0c70e32017-02-09 13:18:38 -0800392 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
393 &command.timestamp.timestamp);
394 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700395 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700396 (long long) command.timestamp.position,
397 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700398 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800399 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700400
401 if (result == AAUDIO_OK) {
402 // Send a hardware timestamp for presentation time.
403 result = getHardwareTimestamp(&command.timestamp.position,
404 &command.timestamp.timestamp);
405 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700406 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700407 (long long) command.timestamp.position,
408 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700409 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
410 result = writeUpMessageQueue(&command);
411 }
412 }
413 }
414
Phil Burkbcc36742017-08-31 17:24:51 -0700415 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700416 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800417 }
418 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800419}
420
Phil Burkc0c70e32017-02-09 13:18:38 -0800421/**
422 * Get an immutable description of the in-memory queues
423 * used to communicate with the underlying HAL or Service.
424 */
425aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700426 {
427 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
428 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700429 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700430 return AAUDIO_ERROR_NULL;
431 }
432 // Gather information on the message queue.
433 mUpMessageQueue->fillParcelable(parcelable,
434 parcelable.mUpMessageQueueParcelable);
435 }
436 return getAudioDataDescription(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700437}
Phil Burk39f02dd2017-08-04 09:13:31 -0700438
439void AAudioServiceStreamBase::onVolumeChanged(float volume) {
440 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
441}
Phil Burk94862522017-09-13 21:31:36 -0700442
Phil Burk2fe718b2018-05-14 12:28:32 -0700443int32_t AAudioServiceStreamBase::incrementServiceReferenceCount_l() {
Phil Burk94862522017-09-13 21:31:36 -0700444 return ++mCallingCount;
445}
446
Phil Burk2fe718b2018-05-14 12:28:32 -0700447int32_t AAudioServiceStreamBase::decrementServiceReferenceCount_l() {
448 int32_t count = --mCallingCount;
449 // Each call to increment should be balanced with one call to decrement.
450 assert(count >= 0);
451 return count;
Phil Burk94862522017-09-13 21:31:36 -0700452}