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 | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 25 | #include <media/MediaMetricsItem.h> |
| 26 | #include <media/TypeConverter.h> |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 27 | #include <mediautils/SchedulingPolicyService.h> |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 28 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 29 | #include "binding/AAudioServiceMessage.h" |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 30 | #include "core/AudioGlobal.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 31 | #include "utility/AudioClock.h" |
| 32 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 33 | #include "AAudioEndpointManager.h" |
| 34 | #include "AAudioService.h" |
| 35 | #include "AAudioServiceEndpoint.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 36 | #include "AAudioServiceStreamBase.h" |
| 37 | #include "TimestampScheduler.h" |
| 38 | |
| 39 | using namespace android; // TODO just import names needed |
| 40 | using namespace aaudio; // TODO just import names needed |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 43 | * Base class for streams in the service. |
| 44 | * @return |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 45 | */ |
| 46 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 47 | AAudioServiceStreamBase::AAudioServiceStreamBase(AAudioService &audioService) |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 48 | : mTimestampThread("AATime") |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 49 | , mAtomicStreamTimestamp() |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 50 | , mAudioService(audioService) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 51 | mMmapClient.clientUid = -1; |
| 52 | mMmapClient.clientPid = -1; |
| 53 | mMmapClient.packageName = String16(""); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 56 | AAudioServiceStreamBase::~AAudioServiceStreamBase() { |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 57 | ALOGD("%s() called", __func__); |
| 58 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 59 | // May not be set if open failed. |
| 60 | if (mMetricsId.size() > 0) { |
| 61 | mediametrics::LogItem(mMetricsId) |
| 62 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DTOR) |
| 63 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 64 | .record(); |
| 65 | } |
| 66 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 67 | // If the stream is deleted when OPEN or in use then audio resources will leak. |
| 68 | // 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] | 69 | LOG_ALWAYS_FATAL_IF(!(getState() == AAUDIO_STREAM_STATE_CLOSED |
| 70 | || getState() == AAUDIO_STREAM_STATE_UNINITIALIZED |
| 71 | || getState() == AAUDIO_STREAM_STATE_DISCONNECTED), |
Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 72 | "service stream %p still open, state = %d", |
| 73 | this, getState()); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 76 | std::string AAudioServiceStreamBase::dumpHeader() { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 77 | 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] | 78 | } |
| 79 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 80 | std::string AAudioServiceStreamBase::dump() const { |
| 81 | std::stringstream result; |
| 82 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 83 | result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle |
| 84 | << std::dec << std::setfill(' ') ; |
| 85 | result << std::setw(6) << mMmapClient.clientUid; |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 86 | result << std::setw(7) << mClientHandle; |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 87 | result << std::setw(4) << (isRunning() ? "yes" : " no"); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 88 | result << std::setw(6) << getState(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 89 | result << std::setw(7) << getFormat(); |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 90 | result << std::setw(6) << mFramesPerBurst; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 91 | result << std::setw(5) << getSamplesPerFrame(); |
| 92 | result << std::setw(9) << getBufferCapacity(); |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 93 | |
| 94 | return result.str(); |
| 95 | } |
| 96 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 97 | void AAudioServiceStreamBase::logOpen(aaudio_handle_t streamHandle) { |
| 98 | // This is the first log sent from the AAudio Service for a stream. |
| 99 | mMetricsId = std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_STREAM) |
| 100 | + std::to_string(streamHandle); |
| 101 | |
| 102 | audio_attributes_t attributes = AAudioServiceEndpoint::getAudioAttributesFrom(this); |
| 103 | |
| 104 | // Once this item is logged by the server, the client with the same PID, UID |
| 105 | // can also log properties. |
| 106 | mediametrics::LogItem(mMetricsId) |
| 107 | .setPid(getOwnerProcessId()) |
| 108 | .setUid(getOwnerUserId()) |
Andy Hung | d203eb6 | 2020-04-27 09:12:46 -0700 | [diff] [blame] | 109 | .set(AMEDIAMETRICS_PROP_ALLOWUID, (int32_t)getOwnerUserId()) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 110 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_OPEN) |
| 111 | // the following are immutable |
| 112 | .set(AMEDIAMETRICS_PROP_BUFFERCAPACITYFRAMES, (int32_t)getBufferCapacity()) |
| 113 | .set(AMEDIAMETRICS_PROP_BURSTFRAMES, (int32_t)getFramesPerBurst()) |
| 114 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)getSamplesPerFrame()) |
| 115 | .set(AMEDIAMETRICS_PROP_CONTENTTYPE, toString(attributes.content_type).c_str()) |
| 116 | .set(AMEDIAMETRICS_PROP_DIRECTION, |
| 117 | AudioGlobal_convertDirectionToText(getDirection())) |
| 118 | .set(AMEDIAMETRICS_PROP_ENCODING, toString(getFormat()).c_str()) |
| 119 | .set(AMEDIAMETRICS_PROP_ROUTEDDEVICEID, (int32_t)getDeviceId()) |
| 120 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)getSampleRate()) |
| 121 | .set(AMEDIAMETRICS_PROP_SESSIONID, (int32_t)getSessionId()) |
| 122 | .set(AMEDIAMETRICS_PROP_SOURCE, toString(attributes.source).c_str()) |
| 123 | .set(AMEDIAMETRICS_PROP_USAGE, toString(attributes.usage).c_str()) |
| 124 | .record(); |
| 125 | } |
| 126 | |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 127 | aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 128 | AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance(); |
| 129 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 130 | |
| 131 | mMmapClient.clientUid = request.getUserId(); |
| 132 | mMmapClient.clientPid = request.getProcessId(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 133 | mMmapClient.packageName.setTo(String16("")); // TODO What should we do here? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 134 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 135 | // Limit scope of lock to avoid recursive lock in close(). |
| 136 | { |
| 137 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 138 | if (mUpMessageQueue != nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 139 | ALOGE("%s() called twice", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 140 | return AAUDIO_ERROR_INVALID_STATE; |
| 141 | } |
| 142 | |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 143 | mUpMessageQueue = std::make_shared<SharedRingBuffer>(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 144 | result = mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), |
| 145 | QUEUE_UP_CAPACITY_COMMANDS); |
| 146 | if (result != AAUDIO_OK) { |
| 147 | goto error; |
| 148 | } |
| 149 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 150 | // This is not protected by a lock because the stream cannot be |
| 151 | // referenced until the service returns a handle to the client. |
| 152 | // So only one thread can open a stream. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 153 | mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 154 | request); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 155 | if (mServiceEndpoint == nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 156 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 157 | goto error; |
| 158 | } |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 159 | // Save a weak pointer that we will use to access the endpoint. |
| 160 | mServiceEndpointWeak = mServiceEndpoint; |
| 161 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 162 | mFramesPerBurst = mServiceEndpoint->getFramesPerBurst(); |
| 163 | copyFrom(*mServiceEndpoint); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 164 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 165 | return result; |
| 166 | |
| 167 | error: |
| 168 | close(); |
| 169 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 170 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 171 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 172 | aaudio_result_t AAudioServiceStreamBase::close() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 173 | std::lock_guard<std::mutex> lock(mLock); |
| 174 | return close_l(); |
| 175 | } |
| 176 | |
| 177 | aaudio_result_t AAudioServiceStreamBase::close_l() { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 178 | if (getState() == AAUDIO_STREAM_STATE_CLOSED) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 179 | return AAUDIO_OK; |
| 180 | } |
| 181 | |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 182 | // This will call stopTimestampThread() and also stop the stream, |
| 183 | // just in case it was not already stopped. |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 184 | stop_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 185 | |
Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 186 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 187 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 188 | if (endpoint == nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 189 | result = AAUDIO_ERROR_INVALID_STATE; |
| 190 | } else { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 191 | endpoint->unregisterStream(this); |
| 192 | AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance(); |
| 193 | endpointManager.closeEndpoint(endpoint); |
| 194 | |
| 195 | // AAudioService::closeStream() prevents two threads from closing at the same time. |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 196 | mServiceEndpoint.clear(); // endpoint will hold the pointer after this method returns. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 199 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 200 | |
| 201 | mediametrics::LogItem(mMetricsId) |
| 202 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CLOSE) |
| 203 | .record(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 204 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 207 | aaudio_result_t AAudioServiceStreamBase::startDevice() { |
| 208 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 209 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 210 | if (endpoint == nullptr) { |
| 211 | ALOGE("%s() has no endpoint", __func__); |
| 212 | return AAUDIO_ERROR_INVALID_STATE; |
| 213 | } |
| 214 | return endpoint->startStream(this, &mClientHandle); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Start the flow of audio data. |
| 219 | * |
| 220 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
| 221 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 222 | aaudio_result_t AAudioServiceStreamBase::start() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 223 | std::lock_guard<std::mutex> lock(mLock); |
| 224 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 225 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 226 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 227 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 228 | if (auto state = getState(); |
| 229 | state == AAUDIO_STREAM_STATE_CLOSED || state == AAUDIO_STREAM_STATE_DISCONNECTED) { |
| 230 | ALOGW("%s() already CLOSED, returns INVALID_STATE, handle = %d", |
| 231 | __func__, getHandle()); |
| 232 | return AAUDIO_ERROR_INVALID_STATE; |
| 233 | } |
| 234 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 235 | mediametrics::Defer defer([&] { |
| 236 | mediametrics::LogItem(mMetricsId) |
| 237 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 238 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 239 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 240 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 241 | .record(); }); |
| 242 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 243 | if (isRunning()) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 244 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 245 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 246 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 247 | setFlowing(false); |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 248 | setSuspended(false); |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 249 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 250 | // Start with fresh presentation timestamps. |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 251 | mAtomicStreamTimestamp.clear(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 252 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 253 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 254 | result = startDevice(); |
| 255 | if (result != AAUDIO_OK) goto error; |
| 256 | |
| 257 | // This should happen at the end of the start. |
| 258 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED); |
| 259 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 260 | mThreadEnabled.store(true); |
| 261 | result = mTimestampThread.start(this); |
| 262 | if (result != AAUDIO_OK) goto error; |
| 263 | |
| 264 | return result; |
| 265 | |
| 266 | error: |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 267 | disconnect_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 268 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | aaudio_result_t AAudioServiceStreamBase::pause() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 272 | std::lock_guard<std::mutex> lock(mLock); |
| 273 | return pause_l(); |
| 274 | } |
| 275 | |
| 276 | aaudio_result_t AAudioServiceStreamBase::pause_l() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 277 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 278 | if (!isRunning()) { |
| 279 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 280 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 281 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 282 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 283 | mediametrics::Defer defer([&] { |
| 284 | mediametrics::LogItem(mMetricsId) |
| 285 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_PAUSE) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 286 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 287 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 288 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 289 | .record(); }); |
| 290 | |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 291 | // Send it now because the timestamp gets rounded up when stopStream() is called below. |
| 292 | // Also we don't need the timestamps while we are shutting down. |
| 293 | sendCurrentTimestamp(); |
| 294 | |
| 295 | result = stopTimestampThread(); |
| 296 | if (result != AAUDIO_OK) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 297 | disconnect_l(); |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 298 | return result; |
| 299 | } |
| 300 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 301 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 302 | if (endpoint == nullptr) { |
| 303 | ALOGE("%s() has no endpoint", __func__); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 304 | result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking |
| 305 | return result; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 306 | } |
| 307 | result = endpoint->stopStream(this, mClientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 308 | if (result != AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 309 | ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 310 | disconnect_l(); // TODO should we return or pause Base first? |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 313 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 314 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 315 | return result; |
| 316 | } |
| 317 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 318 | aaudio_result_t AAudioServiceStreamBase::stop() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 319 | std::lock_guard<std::mutex> lock(mLock); |
| 320 | return stop_l(); |
| 321 | } |
| 322 | |
| 323 | aaudio_result_t AAudioServiceStreamBase::stop_l() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 324 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 325 | if (!isRunning()) { |
| 326 | return result; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 327 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 328 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 329 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 330 | mediametrics::Defer defer([&] { |
| 331 | mediametrics::LogItem(mMetricsId) |
| 332 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 333 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 334 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 335 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 336 | .record(); }); |
| 337 | |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame] | 338 | setState(AAUDIO_STREAM_STATE_STOPPING); |
| 339 | |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 340 | // Send it now because the timestamp gets rounded up when stopStream() is called below. |
| 341 | // Also we don't need the timestamps while we are shutting down. |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 342 | sendCurrentTimestamp(); // warning - this calls a virtual function |
| 343 | result = stopTimestampThread(); |
| 344 | if (result != AAUDIO_OK) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 345 | disconnect_l(); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 346 | return result; |
| 347 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 348 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 349 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 350 | if (endpoint == nullptr) { |
| 351 | ALOGE("%s() has no endpoint", __func__); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 352 | result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking |
| 353 | return result; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 354 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 355 | // TODO wait for data to be played out |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 356 | result = endpoint->stopStream(this, mClientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 357 | if (result != AAUDIO_OK) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 358 | ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 359 | disconnect_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 360 | // TODO what to do with result here? |
| 361 | } |
| 362 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 363 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 364 | setState(AAUDIO_STREAM_STATE_STOPPED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 365 | return result; |
| 366 | } |
| 367 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 368 | aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() { |
| 369 | aaudio_result_t result = AAUDIO_OK; |
| 370 | // clear flag that tells thread to loop |
| 371 | if (mThreadEnabled.exchange(false)) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 372 | result = mTimestampThread.stop(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 373 | } |
| 374 | return result; |
| 375 | } |
| 376 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 377 | aaudio_result_t AAudioServiceStreamBase::flush() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 378 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 379 | aaudio_result_t result = AAudio_isFlushAllowed(getState()); |
| 380 | if (result != AAUDIO_OK) { |
| 381 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 382 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 383 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 384 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 385 | mediametrics::Defer defer([&] { |
| 386 | mediametrics::LogItem(mMetricsId) |
| 387 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_FLUSH) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 388 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 389 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 390 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 391 | .record(); }); |
| 392 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 393 | // Data will get flushed when the client receives the FLUSHED event. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 394 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 395 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 396 | return AAUDIO_OK; |
| 397 | } |
| 398 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 399 | // implement Runnable, periodically send timestamps to client |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 400 | __attribute__((no_sanitize("integer"))) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 401 | void AAudioServiceStreamBase::run() { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 402 | ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 403 | TimestampScheduler timestampScheduler; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 404 | timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 405 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 406 | int64_t nextTime = timestampScheduler.nextAbsoluteTime(); |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 407 | int32_t loopCount = 0; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 408 | while(mThreadEnabled.load()) { |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 409 | loopCount++; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 410 | if (AudioClock::getNanoseconds() >= nextTime) { |
| 411 | aaudio_result_t result = sendCurrentTimestamp(); |
| 412 | if (result != AAUDIO_OK) { |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 413 | ALOGE("%s() timestamp thread got result = %d", __func__, result); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | nextTime = timestampScheduler.nextAbsoluteTime(); |
| 417 | } else { |
| 418 | // Sleep until it is time to send the next timestamp. |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 419 | // 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] | 420 | AudioClock::sleepUntilNanoTime(nextTime); |
| 421 | } |
| 422 | } |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 423 | ALOGD("%s() %s exiting after %d loops <<<<<<<<<<<<<< TIMESTAMPS", |
| 424 | __func__, getTypeText(), loopCount); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 427 | void AAudioServiceStreamBase::disconnect() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 428 | std::lock_guard<std::mutex> lock(mLock); |
| 429 | disconnect_l(); |
| 430 | } |
| 431 | |
| 432 | void AAudioServiceStreamBase::disconnect_l() { |
| 433 | if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED |
| 434 | && getState() != AAUDIO_STREAM_STATE_CLOSED) { |
| 435 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 436 | mediametrics::LogItem(mMetricsId) |
| 437 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DISCONNECT) |
| 438 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 439 | .record(); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 440 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 441 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 442 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 443 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 446 | aaudio_result_t AAudioServiceStreamBase::registerAudioThread(pid_t clientThreadId, |
| 447 | int priority) { |
| 448 | std::lock_guard<std::mutex> lock(mLock); |
| 449 | aaudio_result_t result = AAUDIO_OK; |
| 450 | if (getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 451 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
| 452 | result = AAUDIO_ERROR_INVALID_STATE; |
| 453 | } else { |
| 454 | const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review |
| 455 | setRegisteredThread(clientThreadId); |
| 456 | int err = android::requestPriority(ownerPid, clientThreadId, |
| 457 | priority, true /* isForApp */); |
| 458 | if (err != 0) { |
| 459 | ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d", |
| 460 | clientThreadId, errno, priority); |
| 461 | result = AAUDIO_ERROR_INTERNAL; |
| 462 | } |
| 463 | } |
| 464 | return result; |
| 465 | } |
| 466 | |
| 467 | aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread(pid_t clientThreadId) { |
| 468 | std::lock_guard<std::mutex> lock(mLock); |
| 469 | aaudio_result_t result = AAUDIO_OK; |
| 470 | if (getRegisteredThread() != clientThreadId) { |
| 471 | ALOGE("%s(), wrong thread", __func__); |
| 472 | result = AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 473 | } else { |
| 474 | setRegisteredThread(0); |
| 475 | } |
| 476 | return result; |
| 477 | } |
| 478 | |
| 479 | void AAudioServiceStreamBase::setState(aaudio_stream_state_t state) { |
| 480 | // CLOSED is a final state. |
| 481 | if (mState != AAUDIO_STREAM_STATE_CLOSED) { |
| 482 | mState = state; |
| 483 | } else { |
| 484 | ALOGW_IF(mState != state, "%s(%d) when already CLOSED", __func__, state); |
| 485 | } |
| 486 | } |
| 487 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 488 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 489 | double dataDouble) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 490 | AAudioServiceMessage command; |
| 491 | command.what = AAudioServiceMessage::code::EVENT; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 492 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 493 | command.event.dataDouble = dataDouble; |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 494 | return writeUpMessageQueue(&command); |
| 495 | } |
| 496 | |
| 497 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
| 498 | int64_t dataLong) { |
| 499 | AAudioServiceMessage command; |
| 500 | command.what = AAudioServiceMessage::code::EVENT; |
| 501 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 502 | command.event.dataLong = dataLong; |
| 503 | return writeUpMessageQueue(&command); |
| 504 | } |
| 505 | |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 506 | bool AAudioServiceStreamBase::isUpMessageQueueBusy() { |
| 507 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 508 | if (mUpMessageQueue == nullptr) { |
| 509 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
| 510 | return true; |
| 511 | } |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 512 | // Is it half full or more |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 513 | return mUpMessageQueue->getFractionalFullness() >= 0.5; |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 516 | aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 517 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 518 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 519 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 520 | return AAUDIO_ERROR_NULL; |
| 521 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 522 | int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1); |
| 523 | if (count != 1) { |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 524 | ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s", |
| 525 | __func__, command->what, getTypeText()); |
| 526 | setSuspended(true); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 527 | return AAUDIO_ERROR_WOULD_BLOCK; |
| 528 | } else { |
| 529 | return AAUDIO_OK; |
| 530 | } |
| 531 | } |
| 532 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 533 | aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) { |
| 534 | return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount); |
| 535 | } |
| 536 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 537 | aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() { |
| 538 | AAudioServiceMessage command; |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 539 | // It is not worth filling up the queue with timestamps. |
| 540 | // That can cause the stream to get suspended. |
| 541 | // So just drop the timestamp if the queue is getting full. |
| 542 | if (isUpMessageQueueBusy()) { |
| 543 | return AAUDIO_OK; |
| 544 | } |
| 545 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 546 | // Send a timestamp for the clock model. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 547 | aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position, |
| 548 | &command.timestamp.timestamp); |
| 549 | if (result == AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 550 | ALOGV("%s() SERVICE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 551 | (long long) command.timestamp.position, |
| 552 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 553 | command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 554 | result = writeUpMessageQueue(&command); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 555 | |
| 556 | if (result == AAUDIO_OK) { |
| 557 | // Send a hardware timestamp for presentation time. |
| 558 | result = getHardwareTimestamp(&command.timestamp.position, |
| 559 | &command.timestamp.timestamp); |
| 560 | if (result == AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 561 | ALOGV("%s() HARDWARE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 562 | (long long) command.timestamp.position, |
| 563 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 564 | command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE; |
| 565 | result = writeUpMessageQueue(&command); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 570 | if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 571 | result = AAUDIO_OK; // just not available yet, try again later |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 572 | } |
| 573 | return result; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 576 | /** |
| 577 | * Get an immutable description of the in-memory queues |
| 578 | * used to communicate with the underlying HAL or Service. |
| 579 | */ |
| 580 | aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 581 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 582 | { |
| 583 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 584 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 585 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 586 | return AAUDIO_ERROR_NULL; |
| 587 | } |
| 588 | // Gather information on the message queue. |
| 589 | mUpMessageQueue->fillParcelable(parcelable, |
| 590 | parcelable.mUpMessageQueueParcelable); |
| 591 | } |
| 592 | return getAudioDataDescription(parcelable); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 593 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 594 | |
| 595 | void AAudioServiceStreamBase::onVolumeChanged(float volume) { |
| 596 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 597 | } |