Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 1 | /* |
| 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 Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceStreamBase" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 21 | #include <iomanip> |
| 22 | #include <iostream> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 23 | #include <mutex> |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 24 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include "binding/IAAudioService.h" |
| 26 | #include "binding/AAudioServiceMessage.h" |
| 27 | #include "utility/AudioClock.h" |
| 28 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 29 | #include "AAudioEndpointManager.h" |
| 30 | #include "AAudioService.h" |
| 31 | #include "AAudioServiceEndpoint.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 32 | #include "AAudioServiceStreamBase.h" |
| 33 | #include "TimestampScheduler.h" |
| 34 | |
| 35 | using namespace android; // TODO just import names needed |
| 36 | using namespace aaudio; // TODO just import names needed |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 39 | * Base class for streams in the service. |
| 40 | * @return |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 41 | */ |
| 42 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 43 | AAudioServiceStreamBase::AAudioServiceStreamBase(AAudioService &audioService) |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 44 | : mUpMessageQueue(nullptr) |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 45 | , mAAudioThread() |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 46 | , mAtomicTimestamp() |
| 47 | , mAudioService(audioService) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 48 | mMmapClient.clientUid = -1; |
| 49 | mMmapClient.clientPid = -1; |
| 50 | mMmapClient.packageName = String16(""); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 53 | AAudioServiceStreamBase::~AAudioServiceStreamBase() { |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 54 | ALOGD("AAudioServiceStreamBase::~AAudioServiceStreamBase() destroying %p", this); |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 55 | // 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. |
| 57 | LOG_ALWAYS_FATAL_IF(!(mState == AAUDIO_STREAM_STATE_CLOSED |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 58 | || mState == AAUDIO_STREAM_STATE_UNINITIALIZED |
| 59 | || mState == AAUDIO_STREAM_STATE_DISCONNECTED), |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 60 | "service stream still open, state = %d", mState); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 63 | std::string AAudioServiceStreamBase::dumpHeader() { |
| 64 | return std::string(" T Handle UId Run State Format Burst Chan Capacity"); |
| 65 | } |
| 66 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 67 | std::string AAudioServiceStreamBase::dump() const { |
| 68 | std::stringstream result; |
| 69 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 70 | result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle |
| 71 | << std::dec << std::setfill(' ') ; |
| 72 | result << std::setw(6) << mMmapClient.clientUid; |
| 73 | result << std::setw(4) << (isRunning() ? "yes" : " no"); |
| 74 | result << std::setw(6) << mState; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 75 | result << std::setw(7) << getFormat(); |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 76 | result << std::setw(6) << mFramesPerBurst; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 77 | result << std::setw(5) << getSamplesPerFrame(); |
| 78 | result << std::setw(9) << getBufferCapacity(); |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 79 | |
| 80 | return result.str(); |
| 81 | } |
| 82 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 83 | aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 84 | aaudio_sharing_mode_t sharingMode) { |
| 85 | AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance(); |
| 86 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 87 | |
| 88 | mMmapClient.clientUid = request.getUserId(); |
| 89 | mMmapClient.clientPid = request.getProcessId(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 90 | mMmapClient.packageName.setTo(String16("")); // TODO What should we do here? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 91 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 92 | // Limit scope of lock to avoid recursive lock in close(). |
| 93 | { |
| 94 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 95 | if (mUpMessageQueue != nullptr) { |
| 96 | ALOGE("AAudioServiceStreamBase::open() called twice"); |
| 97 | return AAUDIO_ERROR_INVALID_STATE; |
| 98 | } |
| 99 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 100 | mUpMessageQueue = new SharedRingBuffer(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 101 | result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), |
| 102 | QUEUE_UP_CAPACITY_COMMANDS); |
| 103 | if (result != AAUDIO_OK) { |
| 104 | goto error; |
| 105 | } |
| 106 | |
| 107 | mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, |
| 108 | request, |
| 109 | sharingMode); |
| 110 | if (mServiceEndpoint == nullptr) { |
| 111 | ALOGE("AAudioServiceStreamBase::open() openEndpoint() failed"); |
| 112 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 113 | goto error; |
| 114 | } |
| 115 | mFramesPerBurst = mServiceEndpoint->getFramesPerBurst(); |
| 116 | copyFrom(*mServiceEndpoint); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 117 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 118 | return result; |
| 119 | |
| 120 | error: |
| 121 | close(); |
| 122 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 123 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 124 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 125 | aaudio_result_t AAudioServiceStreamBase::close() { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 126 | aaudio_result_t result = AAUDIO_OK; |
| 127 | if (mState == AAUDIO_STREAM_STATE_CLOSED) { |
| 128 | return AAUDIO_OK; |
| 129 | } |
| 130 | |
| 131 | stop(); |
| 132 | |
| 133 | if (mServiceEndpoint == nullptr) { |
| 134 | result = AAUDIO_ERROR_INVALID_STATE; |
| 135 | } else { |
| 136 | mServiceEndpoint->unregisterStream(this); |
| 137 | AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance(); |
| 138 | mEndpointManager.closeEndpoint(mServiceEndpoint); |
| 139 | mServiceEndpoint.clear(); |
| 140 | } |
| 141 | |
| 142 | { |
| 143 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 144 | stopTimestampThread(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 145 | delete mUpMessageQueue; |
| 146 | mUpMessageQueue = nullptr; |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 147 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 148 | |
| 149 | mState = AAUDIO_STREAM_STATE_CLOSED; |
| 150 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 153 | /** |
| 154 | * Start the flow of audio data. |
| 155 | * |
| 156 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
| 157 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 158 | aaudio_result_t AAudioServiceStreamBase::start() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 159 | if (isRunning()) { |
| 160 | return AAUDIO_OK; |
| 161 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 162 | |
| 163 | if (mServiceEndpoint == nullptr) { |
| 164 | ALOGE("AAudioServiceStreamBase::start() missing endpoint"); |
| 165 | return AAUDIO_ERROR_INVALID_STATE; |
| 166 | } |
| 167 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
| 168 | aaudio_result_t result = mServiceEndpoint->startStream(this, &mClientHandle); |
| 169 | if (result != AAUDIO_OK) { |
| 170 | ALOGE("AAudioServiceStreamBase::start() mServiceEndpoint returned %d", result); |
| 171 | disconnect(); |
| 172 | } else { |
| 173 | if (result == AAUDIO_OK) { |
| 174 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED); |
| 175 | mState = AAUDIO_STREAM_STATE_STARTED; |
| 176 | mThreadEnabled.store(true); |
| 177 | result = mAAudioThread.start(this); |
| 178 | } |
| 179 | } |
| 180 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | aaudio_result_t AAudioServiceStreamBase::pause() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 184 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 185 | if (!isRunning()) { |
| 186 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 187 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 188 | if (mServiceEndpoint == nullptr) { |
| 189 | ALOGE("AAudioServiceStreamShared::pause() missing endpoint"); |
| 190 | return AAUDIO_ERROR_INVALID_STATE; |
| 191 | } |
| 192 | result = mServiceEndpoint->stopStream(this, mClientHandle); |
| 193 | if (result != AAUDIO_OK) { |
| 194 | ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result); |
| 195 | disconnect(); // TODO should we return or pause Base first? |
| 196 | } |
| 197 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 198 | sendCurrentTimestamp(); |
| 199 | mThreadEnabled.store(false); |
| 200 | result = mAAudioThread.stop(); |
| 201 | if (result != AAUDIO_OK) { |
| 202 | disconnect(); |
| 203 | return result; |
| 204 | } |
| 205 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | mState = AAUDIO_STREAM_STATE_PAUSED; |
| 207 | return result; |
| 208 | } |
| 209 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 210 | aaudio_result_t AAudioServiceStreamBase::stop() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 211 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 212 | if (!isRunning()) { |
| 213 | return result; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 214 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 215 | |
| 216 | if (mServiceEndpoint == nullptr) { |
| 217 | ALOGE("AAudioServiceStreamShared::stop() missing endpoint"); |
| 218 | return AAUDIO_ERROR_INVALID_STATE; |
| 219 | } |
| 220 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 221 | sendCurrentTimestamp(); // warning - this calls a virtual function |
| 222 | result = stopTimestampThread(); |
| 223 | if (result != AAUDIO_OK) { |
| 224 | disconnect(); |
| 225 | return result; |
| 226 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 227 | |
| 228 | // TODO wait for data to be played out |
| 229 | result = mServiceEndpoint->stopStream(this, mClientHandle); |
| 230 | if (result != AAUDIO_OK) { |
| 231 | ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result); |
| 232 | disconnect(); |
| 233 | // TODO what to do with result here? |
| 234 | } |
| 235 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 236 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 237 | mState = AAUDIO_STREAM_STATE_STOPPED; |
| 238 | return result; |
| 239 | } |
| 240 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 241 | aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() { |
| 242 | aaudio_result_t result = AAUDIO_OK; |
| 243 | // clear flag that tells thread to loop |
| 244 | if (mThreadEnabled.exchange(false)) { |
| 245 | result = mAAudioThread.stop(); |
| 246 | } |
| 247 | return result; |
| 248 | } |
| 249 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 250 | aaudio_result_t AAudioServiceStreamBase::flush() { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 251 | if (mState != AAUDIO_STREAM_STATE_PAUSED) { |
| 252 | ALOGE("AAudioServiceStreamBase::flush() stream not paused, state = %s", |
| 253 | AAudio_convertStreamStateToText(mState)); |
| 254 | return AAUDIO_ERROR_INVALID_STATE; |
| 255 | } |
| 256 | // Data will get flushed when the client receives the FLUSHED event. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 257 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
| 258 | mState = AAUDIO_STREAM_STATE_FLUSHED; |
| 259 | return AAUDIO_OK; |
| 260 | } |
| 261 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 262 | // implement Runnable, periodically send timestamps to client |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 263 | void AAudioServiceStreamBase::run() { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 264 | ALOGD("AAudioServiceStreamBase::run() entering ----------------"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 265 | TimestampScheduler timestampScheduler; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 266 | timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 267 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 268 | int64_t nextTime = timestampScheduler.nextAbsoluteTime(); |
| 269 | while(mThreadEnabled.load()) { |
| 270 | if (AudioClock::getNanoseconds() >= nextTime) { |
| 271 | aaudio_result_t result = sendCurrentTimestamp(); |
| 272 | if (result != AAUDIO_OK) { |
| 273 | break; |
| 274 | } |
| 275 | nextTime = timestampScheduler.nextAbsoluteTime(); |
| 276 | } else { |
| 277 | // Sleep until it is time to send the next timestamp. |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 278 | // TODO Wait for a signal with a timeout so that we can stop more quickly. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 279 | AudioClock::sleepUntilNanoTime(nextTime); |
| 280 | } |
| 281 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 282 | ALOGD("AAudioServiceStreamBase::run() exiting ----------------"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 285 | void AAudioServiceStreamBase::disconnect() { |
| 286 | if (mState != AAUDIO_STREAM_STATE_DISCONNECTED) { |
| 287 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
| 288 | mState = AAUDIO_STREAM_STATE_DISCONNECTED; |
| 289 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
| 293 | double dataDouble, |
| 294 | int64_t dataLong) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 295 | AAudioServiceMessage command; |
| 296 | command.what = AAudioServiceMessage::code::EVENT; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 297 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 298 | command.event.dataDouble = dataDouble; |
| 299 | command.event.dataLong = dataLong; |
| 300 | return writeUpMessageQueue(&command); |
| 301 | } |
| 302 | |
| 303 | aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 304 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 305 | if (mUpMessageQueue == nullptr) { |
| 306 | ALOGE("writeUpMessageQueue(): mUpMessageQueue null! - stream not open"); |
| 307 | return AAUDIO_ERROR_NULL; |
| 308 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 309 | int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1); |
| 310 | if (count != 1) { |
| 311 | ALOGE("writeUpMessageQueue(): Queue full. Did client die?"); |
| 312 | return AAUDIO_ERROR_WOULD_BLOCK; |
| 313 | } else { |
| 314 | return AAUDIO_OK; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() { |
| 319 | AAudioServiceMessage command; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 320 | // Send a timestamp for the clock model. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 321 | aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position, |
| 322 | &command.timestamp.timestamp); |
| 323 | if (result == AAUDIO_OK) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 324 | command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 325 | result = writeUpMessageQueue(&command); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 326 | |
| 327 | if (result == AAUDIO_OK) { |
| 328 | // Send a hardware timestamp for presentation time. |
| 329 | result = getHardwareTimestamp(&command.timestamp.position, |
| 330 | &command.timestamp.timestamp); |
| 331 | if (result == AAUDIO_OK) { |
| 332 | command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE; |
| 333 | result = writeUpMessageQueue(&command); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (result == AAUDIO_ERROR_UNAVAILABLE) { |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 339 | result = AAUDIO_OK; // just not available yet, try again later |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 340 | } |
| 341 | return result; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 344 | /** |
| 345 | * Get an immutable description of the in-memory queues |
| 346 | * used to communicate with the underlying HAL or Service. |
| 347 | */ |
| 348 | aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) { |
| 349 | // Gather information on the message queue. |
| 350 | mUpMessageQueue->fillParcelable(parcelable, |
| 351 | parcelable.mUpMessageQueueParcelable); |
| 352 | return getDownDataDescription(parcelable); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 353 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 354 | |
| 355 | void AAudioServiceStreamBase::onVolumeChanged(float volume) { |
| 356 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 357 | } |