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 | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 45 | , mTimestampThread("AATime") |
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 | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 54 | // 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 Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 56 | 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 Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 62 | std::string AAudioServiceStreamBase::dumpHeader() { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 63 | return std::string(" T Handle UId Port Run State Format Burst Chan Capacity"); |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 66 | std::string AAudioServiceStreamBase::dump() const { |
| 67 | std::stringstream result; |
| 68 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 69 | result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle |
| 70 | << std::dec << std::setfill(' ') ; |
| 71 | result << std::setw(6) << mMmapClient.clientUid; |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 72 | result << std::setw(7) << mClientHandle; |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 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 | 15f97c9 | 2018-09-04 14:06:27 -0700 | [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 | AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance(); |
| 85 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 86 | |
| 87 | mMmapClient.clientUid = request.getUserId(); |
| 88 | mMmapClient.clientPid = request.getProcessId(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 89 | mMmapClient.packageName.setTo(String16("")); // TODO What should we do here? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 90 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 91 | // Limit scope of lock to avoid recursive lock in close(). |
| 92 | { |
| 93 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 94 | if (mUpMessageQueue != nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 95 | ALOGE("%s() called twice", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 96 | return AAUDIO_ERROR_INVALID_STATE; |
| 97 | } |
| 98 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 99 | mUpMessageQueue = new SharedRingBuffer(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 100 | result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), |
| 101 | QUEUE_UP_CAPACITY_COMMANDS); |
| 102 | if (result != AAUDIO_OK) { |
| 103 | goto error; |
| 104 | } |
| 105 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 106 | // 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 Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 109 | mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 110 | request); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 111 | if (mServiceEndpoint == nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 112 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 113 | goto error; |
| 114 | } |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 115 | // Save a weak pointer that we will use to access the endpoint. |
| 116 | mServiceEndpointWeak = mServiceEndpoint; |
| 117 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 118 | mFramesPerBurst = mServiceEndpoint->getFramesPerBurst(); |
| 119 | copyFrom(*mServiceEndpoint); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 120 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 121 | return result; |
| 122 | |
| 123 | error: |
| 124 | close(); |
| 125 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 127 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 128 | aaudio_result_t AAudioServiceStreamBase::close() { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 129 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 130 | if (getState() == AAUDIO_STREAM_STATE_CLOSED) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 131 | return AAUDIO_OK; |
| 132 | } |
| 133 | |
| 134 | stop(); |
| 135 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 136 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 137 | if (endpoint == nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 138 | result = AAUDIO_ERROR_INVALID_STATE; |
| 139 | } else { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 140 | 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 Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | { |
| 149 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 150 | stopTimestampThread(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 151 | delete mUpMessageQueue; |
| 152 | mUpMessageQueue = nullptr; |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 153 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 154 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 155 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 156 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 159 | aaudio_result_t AAudioServiceStreamBase::startDevice() { |
| 160 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 161 | 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 Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Start the flow of audio data. |
| 171 | * |
| 172 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
| 173 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 174 | aaudio_result_t AAudioServiceStreamBase::start() { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 175 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 176 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 177 | if (isRunning()) { |
| 178 | return AAUDIO_OK; |
| 179 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 180 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 181 | setFlowing(false); |
| 182 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 183 | // Start with fresh presentation timestamps. |
| 184 | mAtomicTimestamp.clear(); |
| 185 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 186 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 187 | result = startDevice(); |
| 188 | if (result != AAUDIO_OK) goto error; |
| 189 | |
| 190 | // This should happen at the end of the start. |
| 191 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED); |
| 192 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 193 | mThreadEnabled.store(true); |
| 194 | result = mTimestampThread.start(this); |
| 195 | if (result != AAUDIO_OK) goto error; |
| 196 | |
| 197 | return result; |
| 198 | |
| 199 | error: |
| 200 | disconnect(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 201 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | aaudio_result_t AAudioServiceStreamBase::pause() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 205 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 206 | if (!isRunning()) { |
| 207 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 208 | } |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 209 | |
| 210 | // Send it now because the timestamp gets rounded up when stopStream() is called below. |
| 211 | // Also we don't need the timestamps while we are shutting down. |
| 212 | sendCurrentTimestamp(); |
| 213 | |
| 214 | result = stopTimestampThread(); |
| 215 | if (result != AAUDIO_OK) { |
| 216 | disconnect(); |
| 217 | return result; |
| 218 | } |
| 219 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 220 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 221 | if (endpoint == nullptr) { |
| 222 | ALOGE("%s() has no endpoint", __func__); |
| 223 | return AAUDIO_ERROR_INVALID_STATE; |
| 224 | } |
| 225 | result = endpoint->stopStream(this, mClientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 226 | if (result != AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 227 | ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 228 | disconnect(); // TODO should we return or pause Base first? |
| 229 | } |
| 230 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 231 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 232 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 233 | return result; |
| 234 | } |
| 235 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 236 | aaudio_result_t AAudioServiceStreamBase::stop() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 237 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 238 | if (!isRunning()) { |
| 239 | return result; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 240 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 241 | |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame] | 242 | setState(AAUDIO_STREAM_STATE_STOPPING); |
| 243 | |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 244 | // Send it now because the timestamp gets rounded up when stopStream() is called below. |
| 245 | // Also we don't need the timestamps while we are shutting down. |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 246 | sendCurrentTimestamp(); // warning - this calls a virtual function |
| 247 | result = stopTimestampThread(); |
| 248 | if (result != AAUDIO_OK) { |
| 249 | disconnect(); |
| 250 | return result; |
| 251 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 252 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 253 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 254 | if (endpoint == nullptr) { |
| 255 | ALOGE("%s() has no endpoint", __func__); |
| 256 | return AAUDIO_ERROR_INVALID_STATE; |
| 257 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 258 | // TODO wait for data to be played out |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 259 | result = endpoint->stopStream(this, mClientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 260 | if (result != AAUDIO_OK) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 261 | ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 262 | disconnect(); |
| 263 | // TODO what to do with result here? |
| 264 | } |
| 265 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 266 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 267 | setState(AAUDIO_STREAM_STATE_STOPPED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 268 | return result; |
| 269 | } |
| 270 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 271 | aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() { |
| 272 | aaudio_result_t result = AAUDIO_OK; |
| 273 | // clear flag that tells thread to loop |
| 274 | if (mThreadEnabled.exchange(false)) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 275 | result = mTimestampThread.stop(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 276 | } |
| 277 | return result; |
| 278 | } |
| 279 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 280 | aaudio_result_t AAudioServiceStreamBase::flush() { |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 281 | aaudio_result_t result = AAudio_isFlushAllowed(getState()); |
| 282 | if (result != AAUDIO_OK) { |
| 283 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 284 | } |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 285 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 286 | // Data will get flushed when the client receives the FLUSHED event. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 287 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 288 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 289 | return AAUDIO_OK; |
| 290 | } |
| 291 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 292 | // implement Runnable, periodically send timestamps to client |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 293 | void AAudioServiceStreamBase::run() { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 294 | ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 295 | TimestampScheduler timestampScheduler; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 296 | timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 297 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 298 | int64_t nextTime = timestampScheduler.nextAbsoluteTime(); |
| 299 | while(mThreadEnabled.load()) { |
| 300 | if (AudioClock::getNanoseconds() >= nextTime) { |
| 301 | aaudio_result_t result = sendCurrentTimestamp(); |
| 302 | if (result != AAUDIO_OK) { |
| 303 | break; |
| 304 | } |
| 305 | nextTime = timestampScheduler.nextAbsoluteTime(); |
| 306 | } else { |
| 307 | // Sleep until it is time to send the next timestamp. |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 308 | // 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] | 309 | AudioClock::sleepUntilNanoTime(nextTime); |
| 310 | } |
| 311 | } |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 312 | ALOGD("%s() %s exiting <<<<<<<<<<<<<< TIMESTAMPS", __func__, getTypeText()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 315 | void AAudioServiceStreamBase::disconnect() { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 316 | if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 317 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 318 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 319 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 323 | double dataDouble) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 324 | AAudioServiceMessage command; |
| 325 | command.what = AAudioServiceMessage::code::EVENT; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 326 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 327 | command.event.dataDouble = dataDouble; |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 328 | return writeUpMessageQueue(&command); |
| 329 | } |
| 330 | |
| 331 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
| 332 | int64_t dataLong) { |
| 333 | AAudioServiceMessage command; |
| 334 | command.what = AAudioServiceMessage::code::EVENT; |
| 335 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 336 | command.event.dataLong = dataLong; |
| 337 | return writeUpMessageQueue(&command); |
| 338 | } |
| 339 | |
| 340 | aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 341 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 342 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 343 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 344 | return AAUDIO_ERROR_NULL; |
| 345 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 346 | int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1); |
| 347 | if (count != 1) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 348 | ALOGE("%s(): Queue full. Did client die? %s", __func__, getTypeText()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 349 | return AAUDIO_ERROR_WOULD_BLOCK; |
| 350 | } else { |
| 351 | return AAUDIO_OK; |
| 352 | } |
| 353 | } |
| 354 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 355 | aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) { |
| 356 | return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount); |
| 357 | } |
| 358 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 359 | aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() { |
| 360 | AAudioServiceMessage command; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 361 | // Send a timestamp for the clock model. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 362 | aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position, |
| 363 | &command.timestamp.timestamp); |
| 364 | if (result == AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 365 | ALOGV("%s() SERVICE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 366 | (long long) command.timestamp.position, |
| 367 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 368 | command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 369 | result = writeUpMessageQueue(&command); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 370 | |
| 371 | if (result == AAUDIO_OK) { |
| 372 | // Send a hardware timestamp for presentation time. |
| 373 | result = getHardwareTimestamp(&command.timestamp.position, |
| 374 | &command.timestamp.timestamp); |
| 375 | if (result == AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 376 | ALOGV("%s() HARDWARE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 377 | (long long) command.timestamp.position, |
| 378 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 379 | command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE; |
| 380 | result = writeUpMessageQueue(&command); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 385 | if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 386 | result = AAUDIO_OK; // just not available yet, try again later |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 387 | } |
| 388 | return result; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 391 | /** |
| 392 | * Get an immutable description of the in-memory queues |
| 393 | * used to communicate with the underlying HAL or Service. |
| 394 | */ |
| 395 | aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 396 | { |
| 397 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 398 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 399 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 400 | return AAUDIO_ERROR_NULL; |
| 401 | } |
| 402 | // Gather information on the message queue. |
| 403 | mUpMessageQueue->fillParcelable(parcelable, |
| 404 | parcelable.mUpMessageQueueParcelable); |
| 405 | } |
| 406 | return getAudioDataDescription(parcelable); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 407 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 408 | |
| 409 | void AAudioServiceStreamBase::onVolumeChanged(float volume) { |
| 410 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 411 | } |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 412 | |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 413 | int32_t AAudioServiceStreamBase::incrementServiceReferenceCount_l() { |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 414 | return ++mCallingCount; |
| 415 | } |
| 416 | |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 417 | int32_t AAudioServiceStreamBase::decrementServiceReferenceCount_l() { |
| 418 | int32_t count = --mCallingCount; |
| 419 | // Each call to increment should be balanced with one call to decrement. |
| 420 | assert(count >= 0); |
| 421 | return count; |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 422 | } |