blob: 12be4a33e4697717f3f48ea27b7322ffdeed4dd1 [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 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 Burk19e990e2018-03-22 13:59:34 -0700113 ALOGE("%s() openEndpoint() failed", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700114 result = AAUDIO_ERROR_UNAVAILABLE;
115 goto error;
116 }
Phil Burk6e2770e2018-05-01 13:03:52 -0700117 // Save a weak pointer that we will use to access the endpoint.
118 mServiceEndpointWeak = mServiceEndpoint;
119
Phil Burk39f02dd2017-08-04 09:13:31 -0700120 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
121 copyFrom(*mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800122 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700123 return result;
124
125error:
126 close();
127 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800128}
Phil Burkdec33ab2017-01-17 14:48:16 -0800129
Phil Burkc0c70e32017-02-09 13:18:38 -0800130aaudio_result_t AAudioServiceStreamBase::close() {
Phil Burk39f02dd2017-08-04 09:13:31 -0700131 aaudio_result_t result = AAUDIO_OK;
Phil Burkbcc36742017-08-31 17:24:51 -0700132 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700133 return AAUDIO_OK;
134 }
135
136 stop();
137
Phil Burk6e2770e2018-05-01 13:03:52 -0700138 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
139 if (endpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700140 result = AAUDIO_ERROR_INVALID_STATE;
141 } else {
Phil Burk6e2770e2018-05-01 13:03:52 -0700142 endpoint->unregisterStream(this);
143 AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance();
144 endpointManager.closeEndpoint(endpoint);
145
146 // AAudioService::closeStream() prevents two threads from closing at the same time.
147 mServiceEndpoint.clear(); // endpoint will hold the pointer until this method returns.
Phil Burk39f02dd2017-08-04 09:13:31 -0700148 }
149
150 {
151 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk98d6d922017-07-06 11:52:45 -0700152 stopTimestampThread();
Phil Burk98d6d922017-07-06 11:52:45 -0700153 delete mUpMessageQueue;
154 mUpMessageQueue = nullptr;
Phil Burk98d6d922017-07-06 11:52:45 -0700155 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700156
Phil Burkbcc36742017-08-31 17:24:51 -0700157 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burk39f02dd2017-08-04 09:13:31 -0700158 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800159}
160
Phil Burkbcc36742017-08-31 17:24:51 -0700161aaudio_result_t AAudioServiceStreamBase::startDevice() {
162 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burk6e2770e2018-05-01 13:03:52 -0700163 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
164 if (endpoint == nullptr) {
165 ALOGE("%s() has no endpoint", __func__);
166 return AAUDIO_ERROR_INVALID_STATE;
167 }
168 return endpoint->startStream(this, &mClientHandle);
Phil Burkbcc36742017-08-31 17:24:51 -0700169}
170
Phil Burk39f02dd2017-08-04 09:13:31 -0700171/**
172 * Start the flow of audio data.
173 *
174 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
175 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800176aaudio_result_t AAudioServiceStreamBase::start() {
Phil Burkbcc36742017-08-31 17:24:51 -0700177 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700178
Eric Laurentcb4dae22017-07-01 19:39:32 -0700179 if (isRunning()) {
180 return AAUDIO_OK;
181 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700182
Phil Burk23296382017-11-20 15:45:11 -0800183 setFlowing(false);
184
Phil Burkbcc36742017-08-31 17:24:51 -0700185 // Start with fresh presentation timestamps.
186 mAtomicTimestamp.clear();
187
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 Burkc0c70e32017-02-09 13:18:38 -0800295void AAudioServiceStreamBase::run() {
Phil Burk19e990e2018-03-22 13:59:34 -0700296 ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800297 TimestampScheduler timestampScheduler;
Phil Burk39f02dd2017-08-04 09:13:31 -0700298 timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate());
Phil Burkc0c70e32017-02-09 13:18:38 -0800299 timestampScheduler.start(AudioClock::getNanoseconds());
300 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
301 while(mThreadEnabled.load()) {
302 if (AudioClock::getNanoseconds() >= nextTime) {
303 aaudio_result_t result = sendCurrentTimestamp();
304 if (result != AAUDIO_OK) {
305 break;
306 }
307 nextTime = timestampScheduler.nextAbsoluteTime();
308 } else {
309 // Sleep until it is time to send the next timestamp.
Phil Burk98d6d922017-07-06 11:52:45 -0700310 // TODO Wait for a signal with a timeout so that we can stop more quickly.
Phil Burkc0c70e32017-02-09 13:18:38 -0800311 AudioClock::sleepUntilNanoTime(nextTime);
312 }
313 }
Phil Burk19e990e2018-03-22 13:59:34 -0700314 ALOGD("%s() %s exiting <<<<<<<<<<<<<< TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800315}
316
Phil Burk5ef003b2017-06-30 11:43:37 -0700317void AAudioServiceStreamBase::disconnect() {
Phil Burkbcc36742017-08-31 17:24:51 -0700318 if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700319 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkbcc36742017-08-31 17:24:51 -0700320 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burk5ef003b2017-06-30 11:43:37 -0700321 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800322}
323
324aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800325 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800326 AAudioServiceMessage command;
327 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800328 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800329 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800330 return writeUpMessageQueue(&command);
331}
332
333aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
334 int64_t dataLong) {
335 AAudioServiceMessage command;
336 command.what = AAudioServiceMessage::code::EVENT;
337 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800338 command.event.dataLong = dataLong;
339 return writeUpMessageQueue(&command);
340}
341
342aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700343 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700344 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700345 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700346 return AAUDIO_ERROR_NULL;
347 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800348 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
349 if (count != 1) {
Phil Burk19e990e2018-03-22 13:59:34 -0700350 ALOGE("%s(): Queue full. Did client die? %s", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800351 return AAUDIO_ERROR_WOULD_BLOCK;
352 } else {
353 return AAUDIO_OK;
354 }
355}
356
Phil Burk23296382017-11-20 15:45:11 -0800357aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
358 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
359}
360
Phil Burkc0c70e32017-02-09 13:18:38 -0800361aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
362 AAudioServiceMessage command;
Phil Burk97350f92017-07-21 15:59:44 -0700363 // Send a timestamp for the clock model.
Phil Burkc0c70e32017-02-09 13:18:38 -0800364 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
365 &command.timestamp.timestamp);
366 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700367 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700368 (long long) command.timestamp.position,
369 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700370 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800371 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700372
373 if (result == AAUDIO_OK) {
374 // Send a hardware timestamp for presentation time.
375 result = getHardwareTimestamp(&command.timestamp.position,
376 &command.timestamp.timestamp);
377 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700378 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700379 (long long) command.timestamp.position,
380 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700381 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
382 result = writeUpMessageQueue(&command);
383 }
384 }
385 }
386
Phil Burkbcc36742017-08-31 17:24:51 -0700387 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700388 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800389 }
390 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800391}
392
Phil Burkc0c70e32017-02-09 13:18:38 -0800393/**
394 * Get an immutable description of the in-memory queues
395 * used to communicate with the underlying HAL or Service.
396 */
397aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700398 {
399 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
400 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700401 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700402 return AAUDIO_ERROR_NULL;
403 }
404 // Gather information on the message queue.
405 mUpMessageQueue->fillParcelable(parcelable,
406 parcelable.mUpMessageQueueParcelable);
407 }
408 return getAudioDataDescription(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700409}
Phil Burk39f02dd2017-08-04 09:13:31 -0700410
411void AAudioServiceStreamBase::onVolumeChanged(float volume) {
412 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
413}
Phil Burk94862522017-09-13 21:31:36 -0700414
Phil Burk2fe718b2018-05-14 12:28:32 -0700415int32_t AAudioServiceStreamBase::incrementServiceReferenceCount_l() {
Phil Burk94862522017-09-13 21:31:36 -0700416 return ++mCallingCount;
417}
418
Phil Burk2fe718b2018-05-14 12:28:32 -0700419int32_t AAudioServiceStreamBase::decrementServiceReferenceCount_l() {
420 int32_t count = --mCallingCount;
421 // Each call to increment should be balanced with one call to decrement.
422 assert(count >= 0);
423 return count;
Phil Burk94862522017-09-13 21:31:36 -0700424}