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 | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 45 | , mTimestampThread() |
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. |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 57 | 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 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"); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 74 | result << std::setw(6) << getState(); |
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; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 127 | if (getState() == AAUDIO_STREAM_STATE_CLOSED) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 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 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 149 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 150 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 153 | aaudio_result_t AAudioServiceStreamBase::startDevice() { |
| 154 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
| 155 | return mServiceEndpoint->startStream(this, &mClientHandle); |
| 156 | } |
| 157 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Start the flow of audio data. |
| 160 | * |
| 161 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
| 162 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 163 | aaudio_result_t AAudioServiceStreamBase::start() { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 164 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 165 | if (isRunning()) { |
| 166 | return AAUDIO_OK; |
| 167 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 168 | |
| 169 | if (mServiceEndpoint == nullptr) { |
| 170 | ALOGE("AAudioServiceStreamBase::start() missing endpoint"); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 171 | result = AAUDIO_ERROR_INVALID_STATE; |
| 172 | goto error; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 173 | } |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 174 | |
| 175 | // Start with fresh presentation timestamps. |
| 176 | mAtomicTimestamp.clear(); |
| 177 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 178 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 179 | result = startDevice(); |
| 180 | if (result != AAUDIO_OK) goto error; |
| 181 | |
| 182 | // This should happen at the end of the start. |
| 183 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED); |
| 184 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 185 | mThreadEnabled.store(true); |
| 186 | result = mTimestampThread.start(this); |
| 187 | if (result != AAUDIO_OK) goto error; |
| 188 | |
| 189 | return result; |
| 190 | |
| 191 | error: |
| 192 | disconnect(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 193 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | aaudio_result_t AAudioServiceStreamBase::pause() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 197 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 198 | if (!isRunning()) { |
| 199 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 200 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 201 | if (mServiceEndpoint == nullptr) { |
| 202 | ALOGE("AAudioServiceStreamShared::pause() missing endpoint"); |
| 203 | return AAUDIO_ERROR_INVALID_STATE; |
| 204 | } |
| 205 | result = mServiceEndpoint->stopStream(this, mClientHandle); |
| 206 | if (result != AAUDIO_OK) { |
| 207 | ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result); |
| 208 | disconnect(); // TODO should we return or pause Base first? |
| 209 | } |
| 210 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 211 | sendCurrentTimestamp(); |
| 212 | mThreadEnabled.store(false); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 213 | result = mTimestampThread.stop(); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 214 | if (result != AAUDIO_OK) { |
| 215 | disconnect(); |
| 216 | return result; |
| 217 | } |
| 218 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 219 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 220 | return result; |
| 221 | } |
| 222 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 223 | aaudio_result_t AAudioServiceStreamBase::stop() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 224 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 225 | if (!isRunning()) { |
| 226 | return result; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 227 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 228 | |
| 229 | if (mServiceEndpoint == nullptr) { |
| 230 | ALOGE("AAudioServiceStreamShared::stop() missing endpoint"); |
| 231 | return AAUDIO_ERROR_INVALID_STATE; |
| 232 | } |
| 233 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 234 | sendCurrentTimestamp(); // warning - this calls a virtual function |
| 235 | result = stopTimestampThread(); |
| 236 | if (result != AAUDIO_OK) { |
| 237 | disconnect(); |
| 238 | return result; |
| 239 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 240 | |
| 241 | // TODO wait for data to be played out |
| 242 | result = mServiceEndpoint->stopStream(this, mClientHandle); |
| 243 | if (result != AAUDIO_OK) { |
| 244 | ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result); |
| 245 | disconnect(); |
| 246 | // TODO what to do with result here? |
| 247 | } |
| 248 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 249 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 250 | setState(AAUDIO_STREAM_STATE_STOPPED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 251 | return result; |
| 252 | } |
| 253 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 254 | aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() { |
| 255 | aaudio_result_t result = AAUDIO_OK; |
| 256 | // clear flag that tells thread to loop |
| 257 | if (mThreadEnabled.exchange(false)) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 258 | result = mTimestampThread.stop(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 259 | } |
| 260 | return result; |
| 261 | } |
| 262 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 263 | aaudio_result_t AAudioServiceStreamBase::flush() { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 264 | if (getState() != AAUDIO_STREAM_STATE_PAUSED) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 265 | ALOGE("AAudioServiceStreamBase::flush() stream not paused, state = %s", |
| 266 | AAudio_convertStreamStateToText(mState)); |
| 267 | return AAUDIO_ERROR_INVALID_STATE; |
| 268 | } |
| 269 | // Data will get flushed when the client receives the FLUSHED event. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 270 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 271 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 272 | return AAUDIO_OK; |
| 273 | } |
| 274 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 275 | // implement Runnable, periodically send timestamps to client |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 276 | void AAudioServiceStreamBase::run() { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 277 | ALOGD("AAudioServiceStreamBase::run() entering ----------------"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 278 | TimestampScheduler timestampScheduler; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 279 | timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 280 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 281 | int64_t nextTime = timestampScheduler.nextAbsoluteTime(); |
| 282 | while(mThreadEnabled.load()) { |
| 283 | if (AudioClock::getNanoseconds() >= nextTime) { |
| 284 | aaudio_result_t result = sendCurrentTimestamp(); |
| 285 | if (result != AAUDIO_OK) { |
| 286 | break; |
| 287 | } |
| 288 | nextTime = timestampScheduler.nextAbsoluteTime(); |
| 289 | } else { |
| 290 | // Sleep until it is time to send the next timestamp. |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 291 | // 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] | 292 | AudioClock::sleepUntilNanoTime(nextTime); |
| 293 | } |
| 294 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 295 | ALOGD("AAudioServiceStreamBase::run() exiting ----------------"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 298 | void AAudioServiceStreamBase::disconnect() { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 299 | if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 300 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 301 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 302 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
| 306 | double dataDouble, |
| 307 | int64_t dataLong) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 308 | AAudioServiceMessage command; |
| 309 | command.what = AAudioServiceMessage::code::EVENT; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 310 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 311 | command.event.dataDouble = dataDouble; |
| 312 | command.event.dataLong = dataLong; |
| 313 | return writeUpMessageQueue(&command); |
| 314 | } |
| 315 | |
| 316 | aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 317 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 318 | if (mUpMessageQueue == nullptr) { |
| 319 | ALOGE("writeUpMessageQueue(): mUpMessageQueue null! - stream not open"); |
| 320 | return AAUDIO_ERROR_NULL; |
| 321 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 322 | int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1); |
| 323 | if (count != 1) { |
| 324 | ALOGE("writeUpMessageQueue(): Queue full. Did client die?"); |
| 325 | return AAUDIO_ERROR_WOULD_BLOCK; |
| 326 | } else { |
| 327 | return AAUDIO_OK; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() { |
| 332 | AAudioServiceMessage command; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 333 | // Send a timestamp for the clock model. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 334 | aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position, |
| 335 | &command.timestamp.timestamp); |
| 336 | if (result == AAUDIO_OK) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 337 | ALOGV("sendCurrentTimestamp() SERVICE %8lld at %lld", |
| 338 | (long long) command.timestamp.position, |
| 339 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 340 | command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 341 | result = writeUpMessageQueue(&command); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 342 | |
| 343 | if (result == AAUDIO_OK) { |
| 344 | // Send a hardware timestamp for presentation time. |
| 345 | result = getHardwareTimestamp(&command.timestamp.position, |
| 346 | &command.timestamp.timestamp); |
| 347 | if (result == AAUDIO_OK) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 348 | ALOGV("sendCurrentTimestamp() HARDWARE %8lld at %lld", |
| 349 | (long long) command.timestamp.position, |
| 350 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 351 | command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE; |
| 352 | result = writeUpMessageQueue(&command); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 357 | if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 358 | result = AAUDIO_OK; // just not available yet, try again later |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 359 | } |
| 360 | return result; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 363 | /** |
| 364 | * Get an immutable description of the in-memory queues |
| 365 | * used to communicate with the underlying HAL or Service. |
| 366 | */ |
| 367 | aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) { |
| 368 | // Gather information on the message queue. |
| 369 | mUpMessageQueue->fillParcelable(parcelable, |
| 370 | parcelable.mUpMessageQueueParcelable); |
| 371 | return getDownDataDescription(parcelable); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 372 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 373 | |
| 374 | void AAudioServiceStreamBase::onVolumeChanged(float volume) { |
| 375 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 376 | } |