blob: 48d8002bfc7e9155bc22d60eda906cb265302c78 [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
Phil Burk6e2770e2018-05-01 13:03:52 -0700108 // This is not protected by a lock because the stream cannot be
109 // referenced until the service returns a handle to the client.
110 // So only one thread can open a stream.
Phil Burk39f02dd2017-08-04 09:13:31 -0700111 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService,
112 request,
113 sharingMode);
114 if (mServiceEndpoint == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700115 ALOGE("%s() openEndpoint() failed", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700116 result = AAUDIO_ERROR_UNAVAILABLE;
117 goto error;
118 }
Phil Burk6e2770e2018-05-01 13:03:52 -0700119 // Save a weak pointer that we will use to access the endpoint.
120 mServiceEndpointWeak = mServiceEndpoint;
121
Phil Burk39f02dd2017-08-04 09:13:31 -0700122 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
123 copyFrom(*mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800124 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700125 return result;
126
127error:
128 close();
129 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800130}
Phil Burkdec33ab2017-01-17 14:48:16 -0800131
Phil Burkc0c70e32017-02-09 13:18:38 -0800132aaudio_result_t AAudioServiceStreamBase::close() {
Phil Burk39f02dd2017-08-04 09:13:31 -0700133 aaudio_result_t result = AAUDIO_OK;
Phil Burkbcc36742017-08-31 17:24:51 -0700134 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700135 return AAUDIO_OK;
136 }
137
138 stop();
139
Phil Burk6e2770e2018-05-01 13:03:52 -0700140 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
141 if (endpoint == nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700142 result = AAUDIO_ERROR_INVALID_STATE;
143 } else {
Phil Burk6e2770e2018-05-01 13:03:52 -0700144 endpoint->unregisterStream(this);
145 AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance();
146 endpointManager.closeEndpoint(endpoint);
147
148 // AAudioService::closeStream() prevents two threads from closing at the same time.
149 mServiceEndpoint.clear(); // endpoint will hold the pointer until this method returns.
Phil Burk39f02dd2017-08-04 09:13:31 -0700150 }
151
152 {
153 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk98d6d922017-07-06 11:52:45 -0700154 stopTimestampThread();
Phil Burk98d6d922017-07-06 11:52:45 -0700155 delete mUpMessageQueue;
156 mUpMessageQueue = nullptr;
Phil Burk98d6d922017-07-06 11:52:45 -0700157 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700158
Phil Burkbcc36742017-08-31 17:24:51 -0700159 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burk39f02dd2017-08-04 09:13:31 -0700160 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800161}
162
Phil Burkbcc36742017-08-31 17:24:51 -0700163aaudio_result_t AAudioServiceStreamBase::startDevice() {
164 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burk6e2770e2018-05-01 13:03:52 -0700165 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
166 if (endpoint == nullptr) {
167 ALOGE("%s() has no endpoint", __func__);
168 return AAUDIO_ERROR_INVALID_STATE;
169 }
170 return endpoint->startStream(this, &mClientHandle);
Phil Burkbcc36742017-08-31 17:24:51 -0700171}
172
Phil Burk39f02dd2017-08-04 09:13:31 -0700173/**
174 * Start the flow of audio data.
175 *
176 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
177 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800178aaudio_result_t AAudioServiceStreamBase::start() {
Phil Burkbcc36742017-08-31 17:24:51 -0700179 aaudio_result_t result = AAUDIO_OK;
Phil Burk6e2770e2018-05-01 13:03:52 -0700180
Eric Laurentcb4dae22017-07-01 19:39:32 -0700181 if (isRunning()) {
182 return AAUDIO_OK;
183 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700184
Phil Burk23296382017-11-20 15:45:11 -0800185 setFlowing(false);
186
Phil Burkbcc36742017-08-31 17:24:51 -0700187 // Start with fresh presentation timestamps.
188 mAtomicTimestamp.clear();
189
Phil Burk39f02dd2017-08-04 09:13:31 -0700190 mClientHandle = AUDIO_PORT_HANDLE_NONE;
Phil Burkbcc36742017-08-31 17:24:51 -0700191 result = startDevice();
192 if (result != AAUDIO_OK) goto error;
193
194 // This should happen at the end of the start.
195 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
196 setState(AAUDIO_STREAM_STATE_STARTED);
197 mThreadEnabled.store(true);
198 result = mTimestampThread.start(this);
199 if (result != AAUDIO_OK) goto error;
200
201 return result;
202
203error:
204 disconnect();
Phil Burk39f02dd2017-08-04 09:13:31 -0700205 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800206}
207
208aaudio_result_t AAudioServiceStreamBase::pause() {
Phil Burk11e8d332017-05-24 09:59:02 -0700209 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700210 if (!isRunning()) {
211 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800212 }
Phil Burk73af62a2017-10-26 12:11:47 -0700213
214 // Send it now because the timestamp gets rounded up when stopStream() is called below.
215 // Also we don't need the timestamps while we are shutting down.
216 sendCurrentTimestamp();
217
218 result = stopTimestampThread();
219 if (result != AAUDIO_OK) {
220 disconnect();
221 return result;
222 }
223
Phil Burk6e2770e2018-05-01 13:03:52 -0700224 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
225 if (endpoint == nullptr) {
226 ALOGE("%s() has no endpoint", __func__);
227 return AAUDIO_ERROR_INVALID_STATE;
228 }
229 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700230 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700231 ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700232 disconnect(); // TODO should we return or pause Base first?
233 }
234
Eric Laurentcb4dae22017-07-01 19:39:32 -0700235 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkbcc36742017-08-31 17:24:51 -0700236 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800237 return result;
238}
239
Phil Burk71f35bb2017-04-13 16:05:07 -0700240aaudio_result_t AAudioServiceStreamBase::stop() {
Phil Burk11e8d332017-05-24 09:59:02 -0700241 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700242 if (!isRunning()) {
243 return result;
Phil Burk71f35bb2017-04-13 16:05:07 -0700244 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700245
Phil Burk83fb8442017-10-05 16:55:17 -0700246 setState(AAUDIO_STREAM_STATE_STOPPING);
247
Phil Burk73af62a2017-10-26 12:11:47 -0700248 // Send it now because the timestamp gets rounded up when stopStream() is called below.
249 // Also we don't need the timestamps while we are shutting down.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700250 sendCurrentTimestamp(); // warning - this calls a virtual function
251 result = stopTimestampThread();
252 if (result != AAUDIO_OK) {
253 disconnect();
254 return result;
255 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700256
Phil Burk6e2770e2018-05-01 13:03:52 -0700257 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
258 if (endpoint == nullptr) {
259 ALOGE("%s() has no endpoint", __func__);
260 return AAUDIO_ERROR_INVALID_STATE;
261 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700262 // TODO wait for data to be played out
Phil Burk6e2770e2018-05-01 13:03:52 -0700263 result = endpoint->stopStream(this, mClientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700264 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700265 ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText());
Phil Burk39f02dd2017-08-04 09:13:31 -0700266 disconnect();
267 // TODO what to do with result here?
268 }
269
Eric Laurentcb4dae22017-07-01 19:39:32 -0700270 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burkbcc36742017-08-31 17:24:51 -0700271 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700272 return result;
273}
274
Phil Burk98d6d922017-07-06 11:52:45 -0700275aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() {
276 aaudio_result_t result = AAUDIO_OK;
277 // clear flag that tells thread to loop
278 if (mThreadEnabled.exchange(false)) {
Phil Burkbcc36742017-08-31 17:24:51 -0700279 result = mTimestampThread.stop();
Phil Burk98d6d922017-07-06 11:52:45 -0700280 }
281 return result;
282}
283
Phil Burk71f35bb2017-04-13 16:05:07 -0700284aaudio_result_t AAudioServiceStreamBase::flush() {
Phil Burk5cc83c32017-11-28 15:43:18 -0800285 aaudio_result_t result = AAudio_isFlushAllowed(getState());
286 if (result != AAUDIO_OK) {
287 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700288 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800289
Phil Burk39f02dd2017-08-04 09:13:31 -0700290 // Data will get flushed when the client receives the FLUSHED event.
Phil Burk71f35bb2017-04-13 16:05:07 -0700291 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
Phil Burkbcc36742017-08-31 17:24:51 -0700292 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700293 return AAUDIO_OK;
294}
295
Phil Burkcf5f6d22017-05-26 12:35:07 -0700296// implement Runnable, periodically send timestamps to client
Phil Burkc0c70e32017-02-09 13:18:38 -0800297void AAudioServiceStreamBase::run() {
Phil Burk19e990e2018-03-22 13:59:34 -0700298 ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800299 TimestampScheduler timestampScheduler;
Phil Burk39f02dd2017-08-04 09:13:31 -0700300 timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate());
Phil Burkc0c70e32017-02-09 13:18:38 -0800301 timestampScheduler.start(AudioClock::getNanoseconds());
302 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
303 while(mThreadEnabled.load()) {
304 if (AudioClock::getNanoseconds() >= nextTime) {
305 aaudio_result_t result = sendCurrentTimestamp();
306 if (result != AAUDIO_OK) {
307 break;
308 }
309 nextTime = timestampScheduler.nextAbsoluteTime();
310 } else {
311 // Sleep until it is time to send the next timestamp.
Phil Burk98d6d922017-07-06 11:52:45 -0700312 // TODO Wait for a signal with a timeout so that we can stop more quickly.
Phil Burkc0c70e32017-02-09 13:18:38 -0800313 AudioClock::sleepUntilNanoTime(nextTime);
314 }
315 }
Phil Burk19e990e2018-03-22 13:59:34 -0700316 ALOGD("%s() %s exiting <<<<<<<<<<<<<< TIMESTAMPS", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800317}
318
Phil Burk5ef003b2017-06-30 11:43:37 -0700319void AAudioServiceStreamBase::disconnect() {
Phil Burkbcc36742017-08-31 17:24:51 -0700320 if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700321 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
Phil Burkbcc36742017-08-31 17:24:51 -0700322 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burk5ef003b2017-06-30 11:43:37 -0700323 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800324}
325
326aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
Phil Burk23296382017-11-20 15:45:11 -0800327 double dataDouble) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800328 AAudioServiceMessage command;
329 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800330 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800331 command.event.dataDouble = dataDouble;
Phil Burk23296382017-11-20 15:45:11 -0800332 return writeUpMessageQueue(&command);
333}
334
335aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
336 int64_t dataLong) {
337 AAudioServiceMessage command;
338 command.what = AAudioServiceMessage::code::EVENT;
339 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800340 command.event.dataLong = dataLong;
341 return writeUpMessageQueue(&command);
342}
343
344aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700345 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700346 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700347 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk71f35bb2017-04-13 16:05:07 -0700348 return AAUDIO_ERROR_NULL;
349 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800350 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
351 if (count != 1) {
Phil Burk19e990e2018-03-22 13:59:34 -0700352 ALOGE("%s(): Queue full. Did client die? %s", __func__, getTypeText());
Phil Burkc0c70e32017-02-09 13:18:38 -0800353 return AAUDIO_ERROR_WOULD_BLOCK;
354 } else {
355 return AAUDIO_OK;
356 }
357}
358
Phil Burk23296382017-11-20 15:45:11 -0800359aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {
360 return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount);
361}
362
Phil Burkc0c70e32017-02-09 13:18:38 -0800363aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
364 AAudioServiceMessage command;
Phil Burk97350f92017-07-21 15:59:44 -0700365 // Send a timestamp for the clock model.
Phil Burkc0c70e32017-02-09 13:18:38 -0800366 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
367 &command.timestamp.timestamp);
368 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700369 ALOGV("%s() SERVICE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700370 (long long) command.timestamp.position,
371 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700372 command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800373 result = writeUpMessageQueue(&command);
Phil Burk97350f92017-07-21 15:59:44 -0700374
375 if (result == AAUDIO_OK) {
376 // Send a hardware timestamp for presentation time.
377 result = getHardwareTimestamp(&command.timestamp.position,
378 &command.timestamp.timestamp);
379 if (result == AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700380 ALOGV("%s() HARDWARE %8lld at %lld", __func__,
Phil Burkbcc36742017-08-31 17:24:51 -0700381 (long long) command.timestamp.position,
382 (long long) command.timestamp.timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700383 command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE;
384 result = writeUpMessageQueue(&command);
385 }
386 }
387 }
388
Phil Burkbcc36742017-08-31 17:24:51 -0700389 if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code
Phil Burk940083c2017-07-17 17:00:02 -0700390 result = AAUDIO_OK; // just not available yet, try again later
Phil Burkc0c70e32017-02-09 13:18:38 -0800391 }
392 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800393}
394
Phil Burkc0c70e32017-02-09 13:18:38 -0800395/**
396 * Get an immutable description of the in-memory queues
397 * used to communicate with the underlying HAL or Service.
398 */
399aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700400 {
401 std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
402 if (mUpMessageQueue == nullptr) {
Phil Burk19e990e2018-03-22 13:59:34 -0700403 ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700404 return AAUDIO_ERROR_NULL;
405 }
406 // Gather information on the message queue.
407 mUpMessageQueue->fillParcelable(parcelable,
408 parcelable.mUpMessageQueueParcelable);
409 }
410 return getAudioDataDescription(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700411}
Phil Burk39f02dd2017-08-04 09:13:31 -0700412
413void AAudioServiceStreamBase::onVolumeChanged(float volume) {
414 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
415}
Phil Burk94862522017-09-13 21:31:36 -0700416
417int32_t AAudioServiceStreamBase::incrementServiceReferenceCount() {
418 std::lock_guard<std::mutex> lock(mCallingCountLock);
419 return ++mCallingCount;
420}
421
422int32_t AAudioServiceStreamBase::decrementServiceReferenceCount() {
423 std::lock_guard<std::mutex> lock(mCallingCountLock);
424 return --mCallingCount;
425}