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