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 | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 54 | ALOGD("~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() { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 64 | 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] | 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; |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 73 | result << std::setw(7) << mClientHandle; |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 74 | result << std::setw(4) << (isRunning() ? "yes" : " no"); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 75 | result << std::setw(6) << getState(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 76 | result << std::setw(7) << getFormat(); |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 77 | result << std::setw(6) << mFramesPerBurst; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 78 | result << std::setw(5) << getSamplesPerFrame(); |
| 79 | result << std::setw(9) << getBufferCapacity(); |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 80 | |
| 81 | return result.str(); |
| 82 | } |
| 83 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 84 | aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 85 | aaudio_sharing_mode_t sharingMode) { |
| 86 | AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance(); |
| 87 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 88 | |
| 89 | mMmapClient.clientUid = request.getUserId(); |
| 90 | mMmapClient.clientPid = request.getProcessId(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 91 | mMmapClient.packageName.setTo(String16("")); // TODO What should we do here? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 92 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 93 | // Limit scope of lock to avoid recursive lock in close(). |
| 94 | { |
| 95 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 96 | if (mUpMessageQueue != nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 97 | ALOGE("%s() called twice", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 98 | return AAUDIO_ERROR_INVALID_STATE; |
| 99 | } |
| 100 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 101 | mUpMessageQueue = new SharedRingBuffer(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 102 | result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), |
| 103 | QUEUE_UP_CAPACITY_COMMANDS); |
| 104 | if (result != AAUDIO_OK) { |
| 105 | goto error; |
| 106 | } |
| 107 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 108 | // 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 Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 111 | mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, |
| 112 | request, |
| 113 | sharingMode); |
| 114 | if (mServiceEndpoint == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 115 | ALOGE("%s() openEndpoint() failed", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 116 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 117 | goto error; |
| 118 | } |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 119 | // Save a weak pointer that we will use to access the endpoint. |
| 120 | mServiceEndpointWeak = mServiceEndpoint; |
| 121 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 122 | mFramesPerBurst = mServiceEndpoint->getFramesPerBurst(); |
| 123 | copyFrom(*mServiceEndpoint); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 124 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 125 | return result; |
| 126 | |
| 127 | error: |
| 128 | close(); |
| 129 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 130 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 131 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 132 | aaudio_result_t AAudioServiceStreamBase::close() { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 133 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 134 | if (getState() == AAUDIO_STREAM_STATE_CLOSED) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 135 | return AAUDIO_OK; |
| 136 | } |
| 137 | |
| 138 | stop(); |
| 139 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 140 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 141 | if (endpoint == nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 142 | result = AAUDIO_ERROR_INVALID_STATE; |
| 143 | } else { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 144 | 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 Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | { |
| 153 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 154 | stopTimestampThread(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 155 | delete mUpMessageQueue; |
| 156 | mUpMessageQueue = nullptr; |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 157 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 158 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 159 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 160 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 163 | aaudio_result_t AAudioServiceStreamBase::startDevice() { |
| 164 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 165 | 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 Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Start the flow of audio data. |
| 175 | * |
| 176 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
| 177 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 178 | aaudio_result_t AAudioServiceStreamBase::start() { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 179 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 180 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 181 | if (isRunning()) { |
| 182 | return AAUDIO_OK; |
| 183 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 184 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 185 | setFlowing(false); |
| 186 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 187 | // Start with fresh presentation timestamps. |
| 188 | mAtomicTimestamp.clear(); |
| 189 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 190 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 191 | 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 | |
| 203 | error: |
| 204 | disconnect(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 205 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | aaudio_result_t AAudioServiceStreamBase::pause() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 209 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 210 | if (!isRunning()) { |
| 211 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 212 | } |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 213 | |
| 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 Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 224 | 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 Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 230 | if (result != AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 231 | ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 232 | disconnect(); // TODO should we return or pause Base first? |
| 233 | } |
| 234 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 235 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 236 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 237 | return result; |
| 238 | } |
| 239 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 240 | aaudio_result_t AAudioServiceStreamBase::stop() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 241 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 242 | if (!isRunning()) { |
| 243 | return result; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 244 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 245 | |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame] | 246 | setState(AAUDIO_STREAM_STATE_STOPPING); |
| 247 | |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 248 | // 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 Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 250 | sendCurrentTimestamp(); // warning - this calls a virtual function |
| 251 | result = stopTimestampThread(); |
| 252 | if (result != AAUDIO_OK) { |
| 253 | disconnect(); |
| 254 | return result; |
| 255 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 256 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 257 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 258 | if (endpoint == nullptr) { |
| 259 | ALOGE("%s() has no endpoint", __func__); |
| 260 | return AAUDIO_ERROR_INVALID_STATE; |
| 261 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 262 | // TODO wait for data to be played out |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 263 | result = endpoint->stopStream(this, mClientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 264 | if (result != AAUDIO_OK) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 265 | ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 266 | disconnect(); |
| 267 | // TODO what to do with result here? |
| 268 | } |
| 269 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 270 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 271 | setState(AAUDIO_STREAM_STATE_STOPPED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 272 | return result; |
| 273 | } |
| 274 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 275 | aaudio_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 Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 279 | result = mTimestampThread.stop(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 280 | } |
| 281 | return result; |
| 282 | } |
| 283 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 284 | aaudio_result_t AAudioServiceStreamBase::flush() { |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 285 | aaudio_result_t result = AAudio_isFlushAllowed(getState()); |
| 286 | if (result != AAUDIO_OK) { |
| 287 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 288 | } |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 289 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 290 | // Data will get flushed when the client receives the FLUSHED event. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 291 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 292 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 293 | return AAUDIO_OK; |
| 294 | } |
| 295 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 296 | // implement Runnable, periodically send timestamps to client |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 297 | void AAudioServiceStreamBase::run() { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 298 | ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 299 | TimestampScheduler timestampScheduler; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 300 | timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 301 | 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 Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 312 | // 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] | 313 | AudioClock::sleepUntilNanoTime(nextTime); |
| 314 | } |
| 315 | } |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 316 | ALOGD("%s() %s exiting <<<<<<<<<<<<<< TIMESTAMPS", __func__, getTypeText()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 319 | void AAudioServiceStreamBase::disconnect() { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 320 | if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 321 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 322 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 323 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 327 | double dataDouble) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 328 | AAudioServiceMessage command; |
| 329 | command.what = AAudioServiceMessage::code::EVENT; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 330 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 331 | command.event.dataDouble = dataDouble; |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 332 | return writeUpMessageQueue(&command); |
| 333 | } |
| 334 | |
| 335 | aaudio_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 Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 340 | command.event.dataLong = dataLong; |
| 341 | return writeUpMessageQueue(&command); |
| 342 | } |
| 343 | |
| 344 | aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 345 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 346 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 347 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 348 | return AAUDIO_ERROR_NULL; |
| 349 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 350 | int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1); |
| 351 | if (count != 1) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 352 | ALOGE("%s(): Queue full. Did client die? %s", __func__, getTypeText()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 353 | return AAUDIO_ERROR_WOULD_BLOCK; |
| 354 | } else { |
| 355 | return AAUDIO_OK; |
| 356 | } |
| 357 | } |
| 358 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 359 | aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) { |
| 360 | return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount); |
| 361 | } |
| 362 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 363 | aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() { |
| 364 | AAudioServiceMessage command; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 365 | // Send a timestamp for the clock model. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 366 | aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position, |
| 367 | &command.timestamp.timestamp); |
| 368 | if (result == AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 369 | ALOGV("%s() SERVICE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 370 | (long long) command.timestamp.position, |
| 371 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 372 | command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 373 | result = writeUpMessageQueue(&command); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 374 | |
| 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 Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 380 | ALOGV("%s() HARDWARE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 381 | (long long) command.timestamp.position, |
| 382 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 383 | command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE; |
| 384 | result = writeUpMessageQueue(&command); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 389 | if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 390 | result = AAUDIO_OK; // just not available yet, try again later |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 391 | } |
| 392 | return result; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 395 | /** |
| 396 | * Get an immutable description of the in-memory queues |
| 397 | * used to communicate with the underlying HAL or Service. |
| 398 | */ |
| 399 | aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 400 | { |
| 401 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 402 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 403 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 404 | 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 Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 411 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 412 | |
| 413 | void AAudioServiceStreamBase::onVolumeChanged(float volume) { |
| 414 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 415 | } |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 416 | |
| 417 | int32_t AAudioServiceStreamBase::incrementServiceReferenceCount() { |
| 418 | std::lock_guard<std::mutex> lock(mCallingCountLock); |
| 419 | return ++mCallingCount; |
| 420 | } |
| 421 | |
| 422 | int32_t AAudioServiceStreamBase::decrementServiceReferenceCount() { |
| 423 | std::lock_guard<std::mutex> lock(mCallingCountLock); |
| 424 | return --mCallingCount; |
| 425 | } |