blob: c943008c8240abbf4f2a19e28a6fc3bbb6b6404a [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 Burk39f02dd2017-08-04 09:13:31 -070046 , mAtomicTimestamp()
47 , 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 Burkfbf031e2017-10-12 15:58:31 -070054 ALOGD("~AAudioServiceStreamBase() destroying %p", this);
Phil Burk5a26e662017-07-07 12:44:48 -070055 // If the stream is deleted when OPEN or in use then audio resources will leak.
56 // This would indicate an internal error. So we want to find this ASAP.
Phil Burkbcc36742017-08-31 17:24:51 -070057 LOG_ALWAYS_FATAL_IF(!(getState() == AAUDIO_STREAM_STATE_CLOSED
58 || getState() == AAUDIO_STREAM_STATE_UNINITIALIZED
59 || getState() == AAUDIO_STREAM_STATE_DISCONNECTED),
60 "service stream still open, state = %d", 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 Burkc0c70e32017-02-09 13:18:38 -080084aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request,
Phil Burk39f02dd2017-08-04 09:13:31 -070085 aaudio_sharing_mode_t sharingMode) {
86 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
87 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -070088
89 mMmapClient.clientUid = request.getUserId();
90 mMmapClient.clientPid = request.getProcessId();
Phil Burk39f02dd2017-08-04 09:13:31 -070091 mMmapClient.packageName.setTo(String16("")); // TODO What should we do here?
Eric Laurentcb4dae22017-07-01 19:39:32 -070092
Phil Burk39f02dd2017-08-04 09:13:31 -070093 // Limit scope of lock to avoid recursive lock in close().
94 {
95 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
96 if (mUpMessageQueue != nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -070097 ALOGE("%s() called twice", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -070098 return AAUDIO_ERROR_INVALID_STATE;
99 }
100
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 mUpMessageQueue = new SharedRingBuffer();
Phil Burk39f02dd2017-08-04 09:13:31 -0700102 result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage),
103 QUEUE_UP_CAPACITY_COMMANDS);
104 if (result != AAUDIO_OK) {
105 goto error;
106 }
107
108 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService,
109 request,
110 sharingMode);
111 if (mServiceEndpoint == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700112 ALOGE("%s() openEndpoint() failed", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700113 result = AAUDIO_ERROR_UNAVAILABLE;
114 goto error;
115 }
116 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
117 copyFrom(*mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800118 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700119 return result;
120
121error:
122 close();
123 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800124}
Phil Burkdec33ab2017-01-17 14:48:16 -0800125
Phil Burkc0c70e32017-02-09 13:18:38 -0800126aaudio_result_t AAudioServiceStreamBase::close() {
Phil Burk39f02dd2017-08-04 09:13:31 -0700127 aaudio_result_t result = AAUDIO_OK;
Phil Burkbcc36742017-08-31 17:24:51 -0700128 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700129 return AAUDIO_OK;
130 }
131
132 stop();
133
134 if (mServiceEndpoint == nullptr) {
135 result = AAUDIO_ERROR_INVALID_STATE;
136 } else {
137 mServiceEndpoint->unregisterStream(this);
138 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
139 mEndpointManager.closeEndpoint(mServiceEndpoint);
140 mServiceEndpoint.clear();
141 }
142
143 {
144 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk98d6d922017-07-06 11:52:45 -0700145 stopTimestampThread();
Phil Burk98d6d922017-07-06 11:52:45 -0700146 delete mUpMessageQueue;
147 mUpMessageQueue = nullptr;
Phil Burk98d6d922017-07-06 11:52:45 -0700148 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700149
Phil Burkbcc36742017-08-31 17:24:51 -0700150 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burk39f02dd2017-08-04 09:13:31 -0700151 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800152}
153
Phil Burkbcc36742017-08-31 17:24:51 -0700154aaudio_result_t AAudioServiceStreamBase::startDevice() {
155 mClientHandle = AUDIO_PORT_HANDLE_NONE;
156 return mServiceEndpoint->startStream(this, &mClientHandle);
157}
158
Phil Burk39f02dd2017-08-04 09:13:31 -0700159/**
160 * Start the flow of audio data.
161 *
162 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
163 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800164aaudio_result_t AAudioServiceStreamBase::start() {
Phil Burkbcc36742017-08-31 17:24:51 -0700165 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700166 if (isRunning()) {
167 return AAUDIO_OK;
168 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700169
170 if (mServiceEndpoint == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700171 ALOGE("%s() missing endpoint", __func__);
Phil Burkbcc36742017-08-31 17:24:51 -0700172 result = AAUDIO_ERROR_INVALID_STATE;
173 goto error;
Phil Burk39f02dd2017-08-04 09:13:31 -0700174 }
Phil Burkbcc36742017-08-31 17:24:51 -0700175
Phil Burk23296382017-11-20 15:45:11 -0800176 setFlowing(false);
177
Phil Burkbcc36742017-08-31 17:24:51 -0700178 // Start with fresh presentation timestamps.
179 mAtomicTimestamp.clear();
180
Phil Burk39f02dd2017-08-04 09:13:31 -0700181 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burkbcc36742017-08-31 17:24:51 -0700182 result = startDevice();
183 if (result != AAUDIO_OK) goto error;
184
185 // This should happen at the end of the start.
186 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
187 setState(AAUDIO_STREAM_STATE_STARTED);
188 mThreadEnabled.store(true);
189 result = mTimestampThread.start(this);
190 if (result != AAUDIO_OK) goto error;
191
192 return result;
193
194error:
195 disconnect();
Phil Burk39f02dd2017-08-04 09:13:31 -0700196 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800197}
198
199aaudio_result_t AAudioServiceStreamBase::pause() {
Phil Burk11e8d332017-05-24 09:59:02 -0700200 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700201 if (!isRunning()) {
202 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700204 if (mServiceEndpoint == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700205 ALOGE("%s() missing endpoint", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700206 return AAUDIO_ERROR_INVALID_STATE;
207 }
Phil Burk73af62a2017-10-26 12:11:47 -0700208
209 // Send it now because the timestamp gets rounded up when stopStream() is called below.
210 // Also we don't need the timestamps while we are shutting down.
211 sendCurrentTimestamp();
212
213 result = stopTimestampThread();
214 if (result != AAUDIO_OK) {
215 disconnect();
216 return result;
217 }
218
Phil Burk39f02dd2017-08-04 09:13:31 -0700219 result = mServiceEndpoint->stopStream(this, mClientHandle);
220 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700221 ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700222 disconnect(); // TODO should we return or pause Base first?
223 }
224
Eric Laurentcb4dae22017-07-01 19:39:32 -0700225 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkbcc36742017-08-31 17:24:51 -0700226 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800227 return result;
228}
229
Phil Burk71f35bb2017-04-13 16:05:07 -0700230aaudio_result_t AAudioServiceStreamBase::stop() {
Phil Burk11e8d332017-05-24 09:59:02 -0700231 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700232 if (!isRunning()) {
233 return result;
Phil Burk71f35bb2017-04-13 16:05:07 -0700234 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700235
236 if (mServiceEndpoint == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700237 ALOGE("%s() missing endpoint", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700238 return AAUDIO_ERROR_INVALID_STATE;
239 }
240
Phil Burk83fb8442017-10-05 16:55:17 -0700241 setState(AAUDIO_STREAM_STATE_STOPPING);
242
Phil Burk73af62a2017-10-26 12:11:47 -0700243 // Send it now because the timestamp gets rounded up when stopStream() is called below.
244 // Also we don't need the timestamps while we are shutting down.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700245 sendCurrentTimestamp(); // warning - this calls a virtual function
246 result = stopTimestampThread();
247 if (result != AAUDIO_OK) {
248 disconnect();
249 return result;
250 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700251
252 // TODO wait for data to be played out
253 result = mServiceEndpoint->stopStream(this, mClientHandle);
254 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700255 ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700256 disconnect();
257 // TODO what to do with result here?
258 }
259
Eric Laurentcb4dae22017-07-01 19:39:32 -0700260 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burkbcc36742017-08-31 17:24:51 -0700261 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700262 return result;
263}
264
Phil Burk98d6d922017-07-06 11:52:45 -0700265aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() {
266 aaudio_result_t result = AAUDIO_OK;
267 // clear flag that tells thread to loop
268 if (mThreadEnabled.exchange(false)) {
Phil Burkbcc36742017-08-31 17:24:51 -0700269 result = mTimestampThread.stop();
Phil Burk98d6d922017-07-06 11:52:45 -0700270 }
271 return result;
272}
273
Phil Burk71f35bb2017-04-13 16:05:07 -0700274aaudio_result_t AAudioServiceStreamBase::flush() {
Phil Burk5cc83c32017-11-28 15:43:18 -0800275 aaudio_result_t result = AAudio_isFlushAllowed(getState());
276 if (result != AAUDIO_OK) {
277 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700278 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800279
Phil Burk39f02dd2017-08-04 09:13:31 -0700280 // Data will get flushed when the client receives the FLUSHED event.
Phil Burk71f35bb2017-04-13 16:05:07 -0700281 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
Phil Burkbcc36742017-08-31 17:24:51 -0700282 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700283 return AAUDIO_OK;
284}
285
Phil Burkcf5f6d22017-05-26 12:35:07 -0700286// implement Runnable, periodically send timestamps to client
Phil Burkc0c70e32017-02-09 13:18:38 -0800287void AAudioServiceStreamBase::run() {
Phil Burk19e990e2018-03-22 13:59:34 -0700288 ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800289 TimestampScheduler timestampScheduler;
Phil Burk39f02dd2017-08-04 09:13:31 -0700290 timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate());
Phil Burkc0c70e32017-02-09 13:18:38 -0800291 timestampScheduler.start(AudioClock::getNanoseconds());
292 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
293 while(mThreadEnabled.load()) {
294 if (AudioClock::getNanoseconds() >= nextTime) {
295 aaudio_result_t result = sendCurrentTimestamp();
296 if (result != AAUDIO_OK) {
297 break;
298 }
299 nextTime = timestampScheduler.nextAbsoluteTime();
300 } else {
301 // Sleep until it is time to send the next timestamp.
Phil Burk98d6d922017-07-06 11:52:45 -0700302 // TODO Wait for a signal with a timeout so that we can stop more quickly.
Phil Burkc0c70e32017-02-09 13:18:38 -0800303 AudioClock::sleepUntilNanoTime(nextTime);
304 }
305 }
Phil Burk19e990e2018-03-22 13:59:34 -0700306 ALOGD("%s() %s exiting <<<<<<<<<<<<<< TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800307}
308
Phil Burk5ef003b2017-06-30 11:43:37 -0700309void AAudioServiceStreamBase::disconnect() {
Phil Burkbcc36742017-08-31 17:24:51 -0700310 if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700311 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkbcc36742017-08-31 17:24:51 -0700312 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burk5ef003b2017-06-30 11:43:37 -0700313 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800314}
315
316aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800317 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800318 AAudioServiceMessage command;
319 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800320 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800321 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800322 return writeUpMessageQueue(&command);
323}
324
325aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
326 int64_t dataLong) {
327 AAudioServiceMessage command;
328 command.what = AAudioServiceMessage::code::EVENT;
329 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800330 command.event.dataLong = dataLong;
331 return writeUpMessageQueue(&command);
332}
333
334aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700335 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700336 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700337 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700338 return AAUDIO_ERROR_NULL;
339 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800340 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
341 if (count != 1) {
Phil Burk19e990e2018-03-22 13:59:34 -0700342 ALOGE("%s(): Queue full. Did client die? %s", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800343 return AAUDIO_ERROR_WOULD_BLOCK;
344 } else {
345 return AAUDIO_OK;
346 }
347}
348
Phil Burk23296382017-11-20 15:45:11 -0800349aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
350 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
351}
352
Phil Burkc0c70e32017-02-09 13:18:38 -0800353aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
354 AAudioServiceMessage command;
Phil Burk97350f92017-07-21 15:59:44 -0700355 // Send a timestamp for the clock model.
Phil Burkc0c70e32017-02-09 13:18:38 -0800356 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
357 &command.timestamp.timestamp);
358 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700359 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700360 (long long) command.timestamp.position,
361 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700362 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800363 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700364
365 if (result == AAUDIO_OK) {
366 // Send a hardware timestamp for presentation time.
367 result = getHardwareTimestamp(&command.timestamp.position,
368 &command.timestamp.timestamp);
369 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700370 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700371 (long long) command.timestamp.position,
372 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700373 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
374 result = writeUpMessageQueue(&command);
375 }
376 }
377 }
378
Phil Burkbcc36742017-08-31 17:24:51 -0700379 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700380 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800381 }
382 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800383}
384
Phil Burkc0c70e32017-02-09 13:18:38 -0800385/**
386 * Get an immutable description of the in-memory queues
387 * used to communicate with the underlying HAL or Service.
388 */
389aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700390 {
391 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
392 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700393 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700394 return AAUDIO_ERROR_NULL;
395 }
396 // Gather information on the message queue.
397 mUpMessageQueue->fillParcelable(parcelable,
398 parcelable.mUpMessageQueueParcelable);
399 }
400 return getAudioDataDescription(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700401}
Phil Burk39f02dd2017-08-04 09:13:31 -0700402
403void AAudioServiceStreamBase::onVolumeChanged(float volume) {
404 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
405}
Phil Burk94862522017-09-13 21:31:36 -0700406
407int32_t AAudioServiceStreamBase::incrementServiceReferenceCount() {
408 std::lock_guard<std::mutex> lock(mCallingCountLock);
409 return ++mCallingCount;
410}
411
412int32_t AAudioServiceStreamBase::decrementServiceReferenceCount() {
413 std::lock_guard<std::mutex> lock(mCallingCountLock);
414 return --mCallingCount;
415}