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 | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 49 | : mUpMessageQueue(nullptr) |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 50 | , mTimestampThread("AATime") |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 51 | , mAtomicStreamTimestamp() |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 52 | , mAudioService(audioService) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 53 | mMmapClient.clientUid = -1; |
| 54 | mMmapClient.clientPid = -1; |
| 55 | mMmapClient.packageName = String16(""); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 58 | AAudioServiceStreamBase::~AAudioServiceStreamBase() { |
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 | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 143 | mUpMessageQueue = new 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 | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 182 | stop_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 183 | |
Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 184 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 185 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 186 | if (endpoint == nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 187 | result = AAUDIO_ERROR_INVALID_STATE; |
| 188 | } else { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 189 | endpoint->unregisterStream(this); |
| 190 | AAudioEndpointManager &endpointManager = AAudioEndpointManager::getInstance(); |
| 191 | endpointManager.closeEndpoint(endpoint); |
| 192 | |
| 193 | // AAudioService::closeStream() prevents two threads from closing at the same time. |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 194 | mServiceEndpoint.clear(); // endpoint will hold the pointer after this method returns. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | { |
| 198 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 199 | stopTimestampThread(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 200 | delete mUpMessageQueue; |
| 201 | mUpMessageQueue = nullptr; |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 202 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 203 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 204 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 205 | |
| 206 | mediametrics::LogItem(mMetricsId) |
| 207 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CLOSE) |
| 208 | .record(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 209 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 212 | aaudio_result_t AAudioServiceStreamBase::startDevice() { |
| 213 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 214 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 215 | if (endpoint == nullptr) { |
| 216 | ALOGE("%s() has no endpoint", __func__); |
| 217 | return AAUDIO_ERROR_INVALID_STATE; |
| 218 | } |
| 219 | return endpoint->startStream(this, &mClientHandle); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 222 | /** |
| 223 | * Start the flow of audio data. |
| 224 | * |
| 225 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
| 226 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 227 | aaudio_result_t AAudioServiceStreamBase::start() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 228 | std::lock_guard<std::mutex> lock(mLock); |
| 229 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 230 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 231 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 232 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 233 | if (auto state = getState(); |
| 234 | state == AAUDIO_STREAM_STATE_CLOSED || state == AAUDIO_STREAM_STATE_DISCONNECTED) { |
| 235 | ALOGW("%s() already CLOSED, returns INVALID_STATE, handle = %d", |
| 236 | __func__, getHandle()); |
| 237 | return AAUDIO_ERROR_INVALID_STATE; |
| 238 | } |
| 239 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 240 | mediametrics::Defer defer([&] { |
| 241 | mediametrics::LogItem(mMetricsId) |
| 242 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 243 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 244 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 245 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 246 | .record(); }); |
| 247 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 248 | if (isRunning()) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 249 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 250 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 251 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 252 | setFlowing(false); |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 253 | setSuspended(false); |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 254 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 255 | // Start with fresh presentation timestamps. |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 256 | mAtomicStreamTimestamp.clear(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 257 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 258 | mClientHandle = AUDIO_PORT_HANDLE_NONE; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 259 | result = startDevice(); |
| 260 | if (result != AAUDIO_OK) goto error; |
| 261 | |
| 262 | // This should happen at the end of the start. |
| 263 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED); |
| 264 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 265 | mThreadEnabled.store(true); |
| 266 | result = mTimestampThread.start(this); |
| 267 | if (result != AAUDIO_OK) goto error; |
| 268 | |
| 269 | return result; |
| 270 | |
| 271 | error: |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 272 | disconnect_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 273 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | aaudio_result_t AAudioServiceStreamBase::pause() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 277 | std::lock_guard<std::mutex> lock(mLock); |
| 278 | return pause_l(); |
| 279 | } |
| 280 | |
| 281 | aaudio_result_t AAudioServiceStreamBase::pause_l() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 282 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 283 | if (!isRunning()) { |
| 284 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 285 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 286 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 287 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 288 | mediametrics::Defer defer([&] { |
| 289 | mediametrics::LogItem(mMetricsId) |
| 290 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_PAUSE) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 291 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 292 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 293 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 294 | .record(); }); |
| 295 | |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 296 | // Send it now because the timestamp gets rounded up when stopStream() is called below. |
| 297 | // Also we don't need the timestamps while we are shutting down. |
| 298 | sendCurrentTimestamp(); |
| 299 | |
| 300 | result = stopTimestampThread(); |
| 301 | if (result != AAUDIO_OK) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 302 | disconnect_l(); |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 303 | return result; |
| 304 | } |
| 305 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 306 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 307 | if (endpoint == nullptr) { |
| 308 | ALOGE("%s() has no endpoint", __func__); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 309 | result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking |
| 310 | return result; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 311 | } |
| 312 | result = endpoint->stopStream(this, mClientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 313 | if (result != AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 314 | ALOGE("%s() mServiceEndpoint returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 315 | disconnect_l(); // TODO should we return or pause Base first? |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 318 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 319 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 320 | return result; |
| 321 | } |
| 322 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 323 | aaudio_result_t AAudioServiceStreamBase::stop() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 324 | std::lock_guard<std::mutex> lock(mLock); |
| 325 | return stop_l(); |
| 326 | } |
| 327 | |
| 328 | aaudio_result_t AAudioServiceStreamBase::stop_l() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 329 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 330 | if (!isRunning()) { |
| 331 | return result; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 332 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 333 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 334 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 335 | mediametrics::Defer defer([&] { |
| 336 | mediametrics::LogItem(mMetricsId) |
| 337 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 338 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 339 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 340 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 341 | .record(); }); |
| 342 | |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame] | 343 | setState(AAUDIO_STREAM_STATE_STOPPING); |
| 344 | |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 345 | // Send it now because the timestamp gets rounded up when stopStream() is called below. |
| 346 | // Also we don't need the timestamps while we are shutting down. |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 347 | sendCurrentTimestamp(); // warning - this calls a virtual function |
| 348 | result = stopTimestampThread(); |
| 349 | if (result != AAUDIO_OK) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 350 | disconnect_l(); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 351 | return result; |
| 352 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 353 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 354 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 355 | if (endpoint == nullptr) { |
| 356 | ALOGE("%s() has no endpoint", __func__); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 357 | result = AAUDIO_ERROR_INVALID_STATE; // for MediaMetric tracking |
| 358 | return result; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 359 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 360 | // TODO wait for data to be played out |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 361 | result = endpoint->stopStream(this, mClientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 362 | if (result != AAUDIO_OK) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 363 | ALOGE("%s() stopStream returned %d, %s", __func__, result, getTypeText()); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 364 | disconnect_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 365 | // TODO what to do with result here? |
| 366 | } |
| 367 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 368 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 369 | setState(AAUDIO_STREAM_STATE_STOPPED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 370 | return result; |
| 371 | } |
| 372 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 373 | aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() { |
| 374 | aaudio_result_t result = AAUDIO_OK; |
| 375 | // clear flag that tells thread to loop |
| 376 | if (mThreadEnabled.exchange(false)) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 377 | result = mTimestampThread.stop(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 378 | } |
| 379 | return result; |
| 380 | } |
| 381 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 382 | aaudio_result_t AAudioServiceStreamBase::flush() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 383 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 384 | aaudio_result_t result = AAudio_isFlushAllowed(getState()); |
| 385 | if (result != AAUDIO_OK) { |
| 386 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 387 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 388 | const int64_t beginNs = AudioClock::getNanoseconds(); |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 389 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 390 | mediametrics::Defer defer([&] { |
| 391 | mediametrics::LogItem(mMetricsId) |
| 392 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_FLUSH) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 393 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(AudioClock::getNanoseconds() - beginNs)) |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 394 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 395 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result) |
| 396 | .record(); }); |
| 397 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 398 | // Data will get flushed when the client receives the FLUSHED event. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 399 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 400 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 401 | return AAUDIO_OK; |
| 402 | } |
| 403 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 404 | // implement Runnable, periodically send timestamps to client |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 405 | __attribute__((no_sanitize("integer"))) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 406 | void AAudioServiceStreamBase::run() { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 407 | ALOGD("%s() %s entering >>>>>>>>>>>>>> TIMESTAMPS", __func__, getTypeText()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 408 | TimestampScheduler timestampScheduler; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 409 | timestampScheduler.setBurstPeriod(mFramesPerBurst, getSampleRate()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 410 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 411 | int64_t nextTime = timestampScheduler.nextAbsoluteTime(); |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 412 | int32_t loopCount = 0; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 413 | while(mThreadEnabled.load()) { |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 414 | loopCount++; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 415 | if (AudioClock::getNanoseconds() >= nextTime) { |
| 416 | aaudio_result_t result = sendCurrentTimestamp(); |
| 417 | if (result != AAUDIO_OK) { |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 418 | ALOGE("%s() timestamp thread got result = %d", __func__, result); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | nextTime = timestampScheduler.nextAbsoluteTime(); |
| 422 | } else { |
| 423 | // Sleep until it is time to send the next timestamp. |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 424 | // 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] | 425 | AudioClock::sleepUntilNanoTime(nextTime); |
| 426 | } |
| 427 | } |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 428 | ALOGD("%s() %s exiting after %d loops <<<<<<<<<<<<<< TIMESTAMPS", |
| 429 | __func__, getTypeText(), loopCount); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 432 | void AAudioServiceStreamBase::disconnect() { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 433 | std::lock_guard<std::mutex> lock(mLock); |
| 434 | disconnect_l(); |
| 435 | } |
| 436 | |
| 437 | void AAudioServiceStreamBase::disconnect_l() { |
| 438 | if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED |
| 439 | && getState() != AAUDIO_STREAM_STATE_CLOSED) { |
| 440 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 441 | mediametrics::LogItem(mMetricsId) |
| 442 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DISCONNECT) |
| 443 | .set(AMEDIAMETRICS_PROP_STATE, AudioGlobal_convertStreamStateToText(getState())) |
| 444 | .record(); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 445 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 446 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 447 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 448 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 451 | aaudio_result_t AAudioServiceStreamBase::registerAudioThread(pid_t clientThreadId, |
| 452 | int priority) { |
| 453 | std::lock_guard<std::mutex> lock(mLock); |
| 454 | aaudio_result_t result = AAUDIO_OK; |
| 455 | if (getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 456 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
| 457 | result = AAUDIO_ERROR_INVALID_STATE; |
| 458 | } else { |
| 459 | const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review |
| 460 | setRegisteredThread(clientThreadId); |
| 461 | int err = android::requestPriority(ownerPid, clientThreadId, |
| 462 | priority, true /* isForApp */); |
| 463 | if (err != 0) { |
| 464 | ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d", |
| 465 | clientThreadId, errno, priority); |
| 466 | result = AAUDIO_ERROR_INTERNAL; |
| 467 | } |
| 468 | } |
| 469 | return result; |
| 470 | } |
| 471 | |
| 472 | aaudio_result_t AAudioServiceStreamBase::unregisterAudioThread(pid_t clientThreadId) { |
| 473 | std::lock_guard<std::mutex> lock(mLock); |
| 474 | aaudio_result_t result = AAUDIO_OK; |
| 475 | if (getRegisteredThread() != clientThreadId) { |
| 476 | ALOGE("%s(), wrong thread", __func__); |
| 477 | result = AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 478 | } else { |
| 479 | setRegisteredThread(0); |
| 480 | } |
| 481 | return result; |
| 482 | } |
| 483 | |
| 484 | void AAudioServiceStreamBase::setState(aaudio_stream_state_t state) { |
| 485 | // CLOSED is a final state. |
| 486 | if (mState != AAUDIO_STREAM_STATE_CLOSED) { |
| 487 | mState = state; |
| 488 | } else { |
| 489 | ALOGW_IF(mState != state, "%s(%d) when already CLOSED", __func__, state); |
| 490 | } |
| 491 | } |
| 492 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 493 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 494 | double dataDouble) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 495 | AAudioServiceMessage command; |
| 496 | command.what = AAudioServiceMessage::code::EVENT; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 497 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 498 | command.event.dataDouble = dataDouble; |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 499 | return writeUpMessageQueue(&command); |
| 500 | } |
| 501 | |
| 502 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
| 503 | int64_t dataLong) { |
| 504 | AAudioServiceMessage command; |
| 505 | command.what = AAudioServiceMessage::code::EVENT; |
| 506 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 507 | command.event.dataLong = dataLong; |
| 508 | return writeUpMessageQueue(&command); |
| 509 | } |
| 510 | |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 511 | bool AAudioServiceStreamBase::isUpMessageQueueBusy() { |
| 512 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 513 | if (mUpMessageQueue == nullptr) { |
| 514 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
| 515 | return true; |
| 516 | } |
| 517 | int32_t framesAvailable = mUpMessageQueue->getFifoBuffer() |
| 518 | ->getFullFramesAvailable(); |
| 519 | int32_t capacity = mUpMessageQueue->getFifoBuffer() |
| 520 | ->getBufferCapacityInFrames(); |
| 521 | // Is it half full or more |
| 522 | return framesAvailable >= (capacity / 2); |
| 523 | } |
| 524 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 525 | aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 526 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 527 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 528 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 529 | return AAUDIO_ERROR_NULL; |
| 530 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 531 | int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1); |
| 532 | if (count != 1) { |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 533 | ALOGW("%s(): Queue full. Did client stop? Suspending stream. what = %u, %s", |
| 534 | __func__, command->what, getTypeText()); |
| 535 | setSuspended(true); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 536 | return AAUDIO_ERROR_WOULD_BLOCK; |
| 537 | } else { |
| 538 | return AAUDIO_OK; |
| 539 | } |
| 540 | } |
| 541 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 542 | aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) { |
| 543 | return sendServiceEvent(AAUDIO_SERVICE_EVENT_XRUN, (int64_t) xRunCount); |
| 544 | } |
| 545 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 546 | aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() { |
| 547 | AAudioServiceMessage command; |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 548 | // It is not worth filling up the queue with timestamps. |
| 549 | // That can cause the stream to get suspended. |
| 550 | // So just drop the timestamp if the queue is getting full. |
| 551 | if (isUpMessageQueueBusy()) { |
| 552 | return AAUDIO_OK; |
| 553 | } |
| 554 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 555 | // Send a timestamp for the clock model. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 556 | aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position, |
| 557 | &command.timestamp.timestamp); |
| 558 | if (result == AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 559 | ALOGV("%s() SERVICE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 560 | (long long) command.timestamp.position, |
| 561 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 562 | command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 563 | result = writeUpMessageQueue(&command); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 564 | |
| 565 | if (result == AAUDIO_OK) { |
| 566 | // Send a hardware timestamp for presentation time. |
| 567 | result = getHardwareTimestamp(&command.timestamp.position, |
| 568 | &command.timestamp.timestamp); |
| 569 | if (result == AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 570 | ALOGV("%s() HARDWARE %8lld at %lld", __func__, |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 571 | (long long) command.timestamp.position, |
| 572 | (long long) command.timestamp.timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 573 | command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE; |
| 574 | result = writeUpMessageQueue(&command); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 579 | if (result == AAUDIO_ERROR_UNAVAILABLE) { // TODO review best error code |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 580 | result = AAUDIO_OK; // just not available yet, try again later |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 581 | } |
| 582 | return result; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 585 | /** |
| 586 | * Get an immutable description of the in-memory queues |
| 587 | * used to communicate with the underlying HAL or Service. |
| 588 | */ |
| 589 | aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 590 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 591 | { |
| 592 | std::lock_guard<std::mutex> lock(mUpMessageQueueLock); |
| 593 | if (mUpMessageQueue == nullptr) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 594 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 595 | return AAUDIO_ERROR_NULL; |
| 596 | } |
| 597 | // Gather information on the message queue. |
| 598 | mUpMessageQueue->fillParcelable(parcelable, |
| 599 | parcelable.mUpMessageQueueParcelable); |
| 600 | } |
| 601 | return getAudioDataDescription(parcelable); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 602 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 603 | |
| 604 | void AAudioServiceStreamBase::onVolumeChanged(float volume) { |
| 605 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 606 | } |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 607 | |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 608 | int32_t AAudioServiceStreamBase::incrementServiceReferenceCount_l() { |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 609 | return ++mCallingCount; |
| 610 | } |
| 611 | |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 612 | int32_t AAudioServiceStreamBase::decrementServiceReferenceCount_l() { |
| 613 | int32_t count = --mCallingCount; |
| 614 | // Each call to increment should be balanced with one call to decrement. |
| 615 | assert(count >= 0); |
| 616 | return count; |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 617 | } |