blob: defbb7b2ef735e713a6178056de9032328601224 [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 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),
59 "service stream still open, state = %d", getState());
Phil Burk2355edb2016-12-26 13:54:02 -080060}
61
Phil Burka5222e22017-07-28 13:31:14 -070062std::string AAudioServiceStreamBase::dumpHeader() {
Phil Burkbbd52862018-04-13 11:37:42 -070063 return std::string(" T Handle UId Port Run State Format Burst Chan Capacity");
Phil Burka5222e22017-07-28 13:31:14 -070064}
65
Phil Burk4501b352017-06-29 18:12:36 -070066std::string AAudioServiceStreamBase::dump() const {
67 std::stringstream result;
68
Phil Burka5222e22017-07-28 13:31:14 -070069 result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle
70 << std::dec << std::setfill(' ') ;
71 result << std::setw(6) << mMmapClient.clientUid;
Phil Burkbbd52862018-04-13 11:37:42 -070072 result << std::setw(7) << mClientHandle;
Phil Burka5222e22017-07-28 13:31:14 -070073 result << std::setw(4) << (isRunning() ? "yes" : " no");
Phil Burkbcc36742017-08-31 17:24:51 -070074 result << std::setw(6) << getState();
Phil Burk39f02dd2017-08-04 09:13:31 -070075 result << std::setw(7) << getFormat();
Phil Burka5222e22017-07-28 13:31:14 -070076 result << std::setw(6) << mFramesPerBurst;
Phil Burk39f02dd2017-08-04 09:13:31 -070077 result << std::setw(5) << getSamplesPerFrame();
78 result << std::setw(9) << getBufferCapacity();
Phil Burk4501b352017-06-29 18:12:36 -070079
80 return result.str();
81}
82
Phil Burk15f97c92018-09-04 14:06:27 -070083aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request) {
Phil Burk39f02dd2017-08-04 09:13:31 -070084 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
85 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -070086
87 mMmapClient.clientUid = request.getUserId();
88 mMmapClient.clientPid = request.getProcessId();
Phil Burk39f02dd2017-08-04 09:13:31 -070089 mMmapClient.packageName.setTo(String16("")); // TODO What should we do here?
Eric Laurentcb4dae22017-07-01 19:39:32 -070090
Phil Burk39f02dd2017-08-04 09:13:31 -070091 // Limit scope of lock to avoid recursive lock in close().
92 {
93 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
94 if (mUpMessageQueue != nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -070095 ALOGE("%s() called twice", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -070096 return AAUDIO_ERROR_INVALID_STATE;
97 }
98
Phil Burkc0c70e32017-02-09 13:18:38 -080099 mUpMessageQueue = new SharedRingBuffer();
Phil Burk39f02dd2017-08-04 09:13:31 -0700100 result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage),
101 QUEUE_UP_CAPACITY_COMMANDS);
102 if (result != AAUDIO_OK) {
103 goto error;
104 }
105
Phil Burk6e2770e2018-05-01 13:03:52 -0700106 // This is not protected by a lock because the stream cannot be
107 // referenced until the service returns a handle to the client.
108 // So only one thread can open a stream.
Phil Burk39f02dd2017-08-04 09:13:31 -0700109 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService,
Phil Burk15f97c92018-09-04 14:06:27 -0700110 request);
Phil Burk39f02dd2017-08-04 09:13:31 -0700111 if (mServiceEndpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700112 result = AAUDIO_ERROR_UNAVAILABLE;
113 goto error;
114 }
Phil Burk6e2770e2018-05-01 13:03:52 -0700115 // Save a weak pointer that we will use to access the endpoint.
116 mServiceEndpointWeak = mServiceEndpoint;
117
Phil Burk39f02dd2017-08-04 09:13:31 -0700118 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
119 copyFrom(*mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800120 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700121 return result;
122
123error:
124 close();
125 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800126}
Phil Burkdec33ab2017-01-17 14:48:16 -0800127
Phil Burkc0c70e32017-02-09 13:18:38 -0800128aaudio_result_t AAudioServiceStreamBase::close() {
Phil Burk39f02dd2017-08-04 09:13:31 -0700129 aaudio_result_t result = AAUDIO_OK;
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 Burk6e2770e2018-05-01 13:03:52 -0700136 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
137 if (endpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700138 result = AAUDIO_ERROR_INVALID_STATE;
139 } else {
Phil Burk6e2770e2018-05-01 13:03:52 -0700140 endpoint->unregisterStream(this);
141 AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance();
142 endpointManager.closeEndpoint(endpoint);
143
144 // AAudioService::closeStream() prevents two threads from closing at the same time.
145 mServiceEndpoint.clear(); // endpoint will hold the pointer until this method returns.
Phil Burk39f02dd2017-08-04 09:13:31 -0700146 }
147
148 {
149 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk98d6d922017-07-06 11:52:45 -0700150 stopTimestampThread();
Phil Burk98d6d922017-07-06 11:52:45 -0700151 delete mUpMessageQueue;
152 mUpMessageQueue = nullptr;
Phil Burk98d6d922017-07-06 11:52:45 -0700153 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700154
Phil Burkbcc36742017-08-31 17:24:51 -0700155 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burk39f02dd2017-08-04 09:13:31 -0700156 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800157}
158
Phil Burkbcc36742017-08-31 17:24:51 -0700159aaudio_result_t AAudioServiceStreamBase::startDevice() {
160 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burk6e2770e2018-05-01 13:03:52 -0700161 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
162 if (endpoint == nullptr) {
163 ALOGE("%s() has no endpoint", __func__);
164 return AAUDIO_ERROR_INVALID_STATE;
165 }
166 return endpoint->startStream(this, &mClientHandle);
Phil Burkbcc36742017-08-31 17:24:51 -0700167}
168
Phil Burk39f02dd2017-08-04 09:13:31 -0700169/**
170 * Start the flow of audio data.
171 *
172 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
173 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800174aaudio_result_t AAudioServiceStreamBase::start() {
Phil Burkbcc36742017-08-31 17:24:51 -0700175 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700176
Eric Laurentcb4dae22017-07-01 19:39:32 -0700177 if (isRunning()) {
178 return AAUDIO_OK;
179 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700180
Phil Burk23296382017-11-20 15:45:11 -0800181 setFlowing(false);
Phil Burk762365c2018-12-10 16:02:16 -0800182 setSuspended(false);
Phil Burk23296382017-11-20 15:45:11 -0800183
Phil Burkbcc36742017-08-31 17:24:51 -0700184 // Start with fresh presentation timestamps.
185 mAtomicTimestamp.clear();
186
Phil Burk39f02dd2017-08-04 09:13:31 -0700187 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burkbcc36742017-08-31 17:24:51 -0700188 result = startDevice();
189 if (result != AAUDIO_OK) goto error;
190
191 // This should happen at the end of the start.
192 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
193 setState(AAUDIO_STREAM_STATE_STARTED);
194 mThreadEnabled.store(true);
195 result = mTimestampThread.start(this);
196 if (result != AAUDIO_OK) goto error;
197
198 return result;
199
200error:
201 disconnect();
Phil Burk39f02dd2017-08-04 09:13:31 -0700202 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800203}
204
205aaudio_result_t AAudioServiceStreamBase::pause() {
Phil Burk11e8d332017-05-24 09:59:02 -0700206 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700207 if (!isRunning()) {
208 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800209 }
Phil Burk73af62a2017-10-26 12:11:47 -0700210
211 // Send it now because the timestamp gets rounded up when stopStream() is called below.
212 // Also we don't need the timestamps while we are shutting down.
213 sendCurrentTimestamp();
214
215 result = stopTimestampThread();
216 if (result != AAUDIO_OK) {
217 disconnect();
218 return result;
219 }
220
Phil Burk6e2770e2018-05-01 13:03:52 -0700221 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
222 if (endpoint == nullptr) {
223 ALOGE("%s() has no endpoint", __func__);
224 return AAUDIO_ERROR_INVALID_STATE;
225 }
226 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700227 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700228 ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700229 disconnect(); // TODO should we return or pause Base first?
230 }
231
Eric Laurentcb4dae22017-07-01 19:39:32 -0700232 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkbcc36742017-08-31 17:24:51 -0700233 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800234 return result;
235}
236
Phil Burk71f35bb2017-04-13 16:05:07 -0700237aaudio_result_t AAudioServiceStreamBase::stop() {
Phil Burk11e8d332017-05-24 09:59:02 -0700238 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700239 if (!isRunning()) {
240 return result;
Phil Burk71f35bb2017-04-13 16:05:07 -0700241 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700242
Phil Burk83fb8442017-10-05 16:55:17 -0700243 setState(AAUDIO_STREAM_STATE_STOPPING);
244
Phil Burk73af62a2017-10-26 12:11:47 -0700245 // Send it now because the timestamp gets rounded up when stopStream() is called below.
246 // Also we don't need the timestamps while we are shutting down.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700247 sendCurrentTimestamp(); // warning - this calls a virtual function
248 result = stopTimestampThread();
249 if (result != AAUDIO_OK) {
250 disconnect();
251 return result;
252 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700253
Phil Burk6e2770e2018-05-01 13:03:52 -0700254 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
255 if (endpoint == nullptr) {
256 ALOGE("%s() has no endpoint", __func__);
257 return AAUDIO_ERROR_INVALID_STATE;
258 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700259 // TODO wait for data to be played out
Phil Burk6e2770e2018-05-01 13:03:52 -0700260 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700261 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700262 ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700263 disconnect();
264 // TODO what to do with result here?
265 }
266
Eric Laurentcb4dae22017-07-01 19:39:32 -0700267 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burkbcc36742017-08-31 17:24:51 -0700268 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700269 return result;
270}
271
Phil Burk98d6d922017-07-06 11:52:45 -0700272aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() {
273 aaudio_result_t result = AAUDIO_OK;
274 // clear flag that tells thread to loop
275 if (mThreadEnabled.exchange(false)) {
Phil Burkbcc36742017-08-31 17:24:51 -0700276 result = mTimestampThread.stop();
Phil Burk98d6d922017-07-06 11:52:45 -0700277 }
278 return result;
279}
280
Phil Burk71f35bb2017-04-13 16:05:07 -0700281aaudio_result_t AAudioServiceStreamBase::flush() {
Phil Burk5cc83c32017-11-28 15:43:18 -0800282 aaudio_result_t result = AAudio_isFlushAllowed(getState());
283 if (result != AAUDIO_OK) {
284 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700285 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800286
Phil Burk39f02dd2017-08-04 09:13:31 -0700287 // Data will get flushed when the client receives the FLUSHED event.
Phil Burk71f35bb2017-04-13 16:05:07 -0700288 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
Phil Burkbcc36742017-08-31 17:24:51 -0700289 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700290 return AAUDIO_OK;
291}
292
Phil Burkcf5f6d22017-05-26 12:35:07 -0700293// implement Runnable, periodically send timestamps to client
Phil Burkc0c70e32017-02-09 13:18:38 -0800294void AAudioServiceStreamBase::run() {
Phil Burk19e990e2018-03-22 13:59:34 -0700295 ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800296 TimestampScheduler timestampScheduler;
Phil Burk39f02dd2017-08-04 09:13:31 -0700297 timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate());
Phil Burkc0c70e32017-02-09 13:18:38 -0800298 timestampScheduler.start(AudioClock::getNanoseconds());
299 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
300 while(mThreadEnabled.load()) {
301 if (AudioClock::getNanoseconds() >= nextTime) {
302 aaudio_result_t result = sendCurrentTimestamp();
303 if (result != AAUDIO_OK) {
304 break;
305 }
306 nextTime = timestampScheduler.nextAbsoluteTime();
307 } else {
308 // Sleep until it is time to send the next timestamp.
Phil Burk98d6d922017-07-06 11:52:45 -0700309 // TODO Wait for a signal with a timeout so that we can stop more quickly.
Phil Burkc0c70e32017-02-09 13:18:38 -0800310 AudioClock::sleepUntilNanoTime(nextTime);
311 }
312 }
Phil Burk19e990e2018-03-22 13:59:34 -0700313 ALOGD("%s() %s exiting <<<<<<<<<<<<<< TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800314}
315
Phil Burk5ef003b2017-06-30 11:43:37 -0700316void AAudioServiceStreamBase::disconnect() {
Phil Burkbcc36742017-08-31 17:24:51 -0700317 if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700318 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkbcc36742017-08-31 17:24:51 -0700319 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burk5ef003b2017-06-30 11:43:37 -0700320 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800321}
322
323aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800324 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800325 AAudioServiceMessage command;
326 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800327 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800328 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800329 return writeUpMessageQueue(&command);
330}
331
332aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
333 int64_t dataLong) {
334 AAudioServiceMessage command;
335 command.what = AAudioServiceMessage::code::EVENT;
336 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800337 command.event.dataLong = dataLong;
338 return writeUpMessageQueue(&command);
339}
340
341aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700342 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700343 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700344 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700345 return AAUDIO_ERROR_NULL;
346 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800347 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
348 if (count != 1) {
Phil Burk762365c2018-12-10 16:02:16 -0800349 ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s",
350 __func__, command->what, getTypeText());
351 setSuspended(true);
Phil Burkc0c70e32017-02-09 13:18:38 -0800352 return AAUDIO_ERROR_WOULD_BLOCK;
353 } else {
354 return AAUDIO_OK;
355 }
356}
357
Phil Burk23296382017-11-20 15:45:11 -0800358aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
359 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
360}
361
Phil Burkc0c70e32017-02-09 13:18:38 -0800362aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
363 AAudioServiceMessage command;
Phil Burk97350f92017-07-21 15:59:44 -0700364 // Send a timestamp for the clock model.
Phil Burkc0c70e32017-02-09 13:18:38 -0800365 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
366 &command.timestamp.timestamp);
367 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700368 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700369 (long long) command.timestamp.position,
370 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700371 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800372 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700373
374 if (result == AAUDIO_OK) {
375 // Send a hardware timestamp for presentation time.
376 result = getHardwareTimestamp(&command.timestamp.position,
377 &command.timestamp.timestamp);
378 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700379 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700380 (long long) command.timestamp.position,
381 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700382 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
383 result = writeUpMessageQueue(&command);
384 }
385 }
386 }
387
Phil Burkbcc36742017-08-31 17:24:51 -0700388 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700389 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800390 }
391 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800392}
393
Phil Burkc0c70e32017-02-09 13:18:38 -0800394/**
395 * Get an immutable description of the in-memory queues
396 * used to communicate with the underlying HAL or Service.
397 */
398aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700399 {
400 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
401 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700402 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700403 return AAUDIO_ERROR_NULL;
404 }
405 // Gather information on the message queue.
406 mUpMessageQueue->fillParcelable(parcelable,
407 parcelable.mUpMessageQueueParcelable);
408 }
409 return getAudioDataDescription(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700410}
Phil Burk39f02dd2017-08-04 09:13:31 -0700411
412void AAudioServiceStreamBase::onVolumeChanged(float volume) {
413 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
414}
Phil Burk94862522017-09-13 21:31:36 -0700415
Phil Burk2fe718b2018-05-14 12:28:32 -0700416int32_t AAudioServiceStreamBase::incrementServiceReferenceCount_l() {
Phil Burk94862522017-09-13 21:31:36 -0700417 return ++mCallingCount;
418}
419
Phil Burk2fe718b2018-05-14 12:28:32 -0700420int32_t AAudioServiceStreamBase::decrementServiceReferenceCount_l() {
421 int32_t count = --mCallingCount;
422 // Each call to increment should be balanced with one call to decrement.
423 assert(count >= 0);
424 return count;
Phil Burk94862522017-09-13 21:31:36 -0700425}