Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | |
| 17 | #define LOG_TAG "AHAL_StreamRemoteSubmix" |
| 18 | #include <android-base/logging.h> |
| 19 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 20 | #include "core-impl/StreamRemoteSubmix.h" |
| 21 | |
| 22 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 23 | using aidl::android::hardware::audio::common::SourceMetadata; |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 24 | using aidl::android::hardware::audio::core::r_submix::SubmixRoute; |
| 25 | using aidl::android::media::audio::common::AudioDeviceAddress; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 26 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 27 | using aidl::android::media::audio::common::MicrophoneDynamicInfo; |
| 28 | using aidl::android::media::audio::common::MicrophoneInfo; |
| 29 | |
| 30 | namespace aidl::android::hardware::audio::core { |
| 31 | |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 32 | StreamRemoteSubmix::StreamRemoteSubmix(StreamContext* context, const Metadata& metadata, |
| 33 | const AudioDeviceAddress& deviceAddress) |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 34 | : StreamCommonImpl(context, metadata), |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 35 | mDeviceAddress(deviceAddress), |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 36 | mIsInput(isInput(metadata)) { |
Mikhail Naganov | 1eedc13 | 2023-07-21 17:45:28 -0700 | [diff] [blame] | 37 | mStreamConfig.frameSize = context->getFrameSize(); |
| 38 | mStreamConfig.format = context->getFormat(); |
| 39 | mStreamConfig.channelLayout = context->getChannelLayout(); |
| 40 | mStreamConfig.sampleRate = context->getSampleRate(); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | std::mutex StreamRemoteSubmix::sSubmixRoutesLock; |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 44 | std::map<AudioDeviceAddress, std::shared_ptr<SubmixRoute>> StreamRemoteSubmix::sSubmixRoutes; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 45 | |
| 46 | ::android::status_t StreamRemoteSubmix::init() { |
| 47 | { |
| 48 | std::lock_guard guard(sSubmixRoutesLock); |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 49 | auto routeItr = sSubmixRoutes.find(mDeviceAddress); |
| 50 | if (routeItr != sSubmixRoutes.end()) { |
| 51 | mCurrentRoute = routeItr->second; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | // If route is not available for this port, add it. |
| 55 | if (mCurrentRoute == nullptr) { |
| 56 | // Initialize the pipe. |
| 57 | mCurrentRoute = std::make_shared<SubmixRoute>(); |
| 58 | if (::android::OK != mCurrentRoute->createPipe(mStreamConfig)) { |
| 59 | LOG(ERROR) << __func__ << ": create pipe failed"; |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 60 | return ::android::NO_INIT; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 61 | } |
| 62 | { |
| 63 | std::lock_guard guard(sSubmixRoutesLock); |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 64 | sSubmixRoutes.emplace(mDeviceAddress, mCurrentRoute); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 65 | } |
| 66 | } else { |
| 67 | if (!mCurrentRoute->isStreamConfigValid(mIsInput, mStreamConfig)) { |
| 68 | LOG(ERROR) << __func__ << ": invalid stream config"; |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 69 | return ::android::NO_INIT; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 70 | } |
| 71 | sp<MonoPipe> sink = mCurrentRoute->getSink(); |
| 72 | if (sink == nullptr) { |
| 73 | LOG(ERROR) << __func__ << ": nullptr sink when opening stream"; |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 74 | return ::android::NO_INIT; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 75 | } |
| 76 | // If the sink has been shutdown or pipe recreation is forced, delete the pipe and |
| 77 | // recreate it. |
| 78 | if (sink->isShutdown()) { |
| 79 | LOG(DEBUG) << __func__ << ": Non-nullptr shut down sink when opening stream"; |
| 80 | if (::android::OK != mCurrentRoute->resetPipe()) { |
| 81 | LOG(ERROR) << __func__ << ": reset pipe failed"; |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 82 | return ::android::NO_INIT; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | mCurrentRoute->openStream(mIsInput); |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 88 | return ::android::OK; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | ::android::status_t StreamRemoteSubmix::drain(StreamDescriptor::DrainMode) { |
| 92 | usleep(1000); |
| 93 | return ::android::OK; |
| 94 | } |
| 95 | |
| 96 | ::android::status_t StreamRemoteSubmix::flush() { |
| 97 | usleep(1000); |
| 98 | return ::android::OK; |
| 99 | } |
| 100 | |
| 101 | ::android::status_t StreamRemoteSubmix::pause() { |
| 102 | usleep(1000); |
| 103 | return ::android::OK; |
| 104 | } |
| 105 | |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 106 | ::android::status_t StreamRemoteSubmix::standby() { |
| 107 | mCurrentRoute->standby(mIsInput); |
| 108 | return ::android::OK; |
| 109 | } |
| 110 | |
| 111 | ::android::status_t StreamRemoteSubmix::start() { |
| 112 | mCurrentRoute->exitStandby(mIsInput); |
| 113 | return ::android::OK; |
| 114 | } |
| 115 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 116 | ndk::ScopedAStatus StreamRemoteSubmix::prepareToClose() { |
| 117 | if (!mIsInput) { |
| 118 | std::shared_ptr<SubmixRoute> route = nullptr; |
| 119 | { |
| 120 | std::lock_guard guard(sSubmixRoutesLock); |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 121 | auto routeItr = sSubmixRoutes.find(mDeviceAddress); |
| 122 | if (routeItr != sSubmixRoutes.end()) { |
| 123 | route = routeItr->second; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | if (route != nullptr) { |
| 127 | sp<MonoPipe> sink = route->getSink(); |
| 128 | if (sink == nullptr) { |
| 129 | ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 130 | } |
| 131 | LOG(DEBUG) << __func__ << ": shutting down MonoPipe sink"; |
| 132 | |
| 133 | sink->shutdown(true); |
| 134 | } else { |
| 135 | LOG(DEBUG) << __func__ << ": stream already closed."; |
| 136 | ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 137 | } |
| 138 | } |
| 139 | return ndk::ScopedAStatus::ok(); |
| 140 | } |
| 141 | |
| 142 | // Remove references to the specified input and output streams. When the device no longer |
| 143 | // references input and output streams destroy the associated pipe. |
| 144 | void StreamRemoteSubmix::shutdown() { |
| 145 | mCurrentRoute->closeStream(mIsInput); |
| 146 | // If all stream instances are closed, we can remove route information for this port. |
| 147 | if (!mCurrentRoute->hasAtleastOneStreamOpen()) { |
| 148 | mCurrentRoute->releasePipe(); |
| 149 | LOG(DEBUG) << __func__ << ": pipe destroyed"; |
| 150 | |
| 151 | std::lock_guard guard(sSubmixRoutesLock); |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 152 | sSubmixRoutes.erase(mDeviceAddress); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 153 | } |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 154 | mCurrentRoute.reset(); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | ::android::status_t StreamRemoteSubmix::transfer(void* buffer, size_t frameCount, |
| 158 | size_t* actualFrameCount, int32_t* latencyMs) { |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 159 | *latencyMs = getDelayInUsForFrameCount(getStreamPipeSizeInFrames()) / 1000; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 160 | LOG(VERBOSE) << __func__ << ": Latency " << *latencyMs << "ms"; |
Shraddha Basantwani | 95f9232 | 2023-08-22 15:07:16 +0000 | [diff] [blame] | 161 | mCurrentRoute->exitStandby(mIsInput); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 162 | return (mIsInput ? inRead(buffer, frameCount, actualFrameCount) |
| 163 | : outWrite(buffer, frameCount, actualFrameCount)); |
| 164 | } |
| 165 | |
Mikhail Naganov | 459b733 | 2023-08-03 10:26:21 -0700 | [diff] [blame] | 166 | ::android::status_t StreamRemoteSubmix::refinePosition(StreamDescriptor::Position* position) { |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 167 | sp<MonoPipeReader> source = mCurrentRoute->getSource(); |
| 168 | if (source == nullptr) { |
| 169 | return ::android::NO_INIT; |
| 170 | } |
| 171 | const ssize_t framesInPipe = source->availableToRead(); |
Shraddha Basantwani | 95f9232 | 2023-08-22 15:07:16 +0000 | [diff] [blame] | 172 | if (framesInPipe <= 0) { |
| 173 | // No need to update the position frames |
| 174 | return ::android::OK; |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 175 | } |
| 176 | if (mIsInput) { |
| 177 | position->frames += framesInPipe; |
Shraddha Basantwani | 95f9232 | 2023-08-22 15:07:16 +0000 | [diff] [blame] | 178 | } else if (position->frames >= framesInPipe) { |
| 179 | position->frames -= framesInPipe; |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 180 | } |
| 181 | return ::android::OK; |
| 182 | } |
| 183 | |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 184 | long StreamRemoteSubmix::getDelayInUsForFrameCount(size_t frameCount) { |
| 185 | return frameCount * MICROS_PER_SECOND / mStreamConfig.sampleRate; |
| 186 | } |
| 187 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 188 | // Calculate the maximum size of the pipe buffer in frames for the specified stream. |
| 189 | size_t StreamRemoteSubmix::getStreamPipeSizeInFrames() { |
| 190 | auto pipeConfig = mCurrentRoute->mPipeConfig; |
| 191 | const size_t maxFrameSize = std::max(mStreamConfig.frameSize, pipeConfig.frameSize); |
| 192 | return (pipeConfig.frameCount * pipeConfig.frameSize) / maxFrameSize; |
| 193 | } |
| 194 | |
| 195 | ::android::status_t StreamRemoteSubmix::outWrite(void* buffer, size_t frameCount, |
| 196 | size_t* actualFrameCount) { |
| 197 | sp<MonoPipe> sink = mCurrentRoute->getSink(); |
| 198 | if (sink != nullptr) { |
| 199 | if (sink->isShutdown()) { |
| 200 | sink.clear(); |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 201 | const auto delayUs = getDelayInUsForFrameCount(frameCount); |
| 202 | LOG(DEBUG) << __func__ << ": pipe shutdown, ignoring the write, sleeping for " |
| 203 | << delayUs << " us"; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 204 | // the pipe has already been shutdown, this buffer will be lost but we must |
| 205 | // simulate timing so we don't drain the output faster than realtime |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 206 | usleep(delayUs); |
| 207 | *actualFrameCount = frameCount; |
| 208 | return ::android::OK; |
| 209 | } |
| 210 | } else { |
| 211 | LOG(FATAL) << __func__ << ": without a pipe!"; |
| 212 | return ::android::UNKNOWN_ERROR; |
| 213 | } |
| 214 | |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 215 | const bool shouldBlockWrite = mCurrentRoute->shouldBlockWrite(); |
| 216 | size_t availableToWrite = sink->availableToWrite(); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 217 | // NOTE: sink has been checked above and sink and source life cycles are synchronized |
| 218 | sp<MonoPipeReader> source = mCurrentRoute->getSource(); |
| 219 | // If the write to the sink should be blocked, flush enough frames from the pipe to make space |
| 220 | // to write the most recent data. |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 221 | if (!shouldBlockWrite && availableToWrite < frameCount) { |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 222 | static uint8_t flushBuffer[64]; |
| 223 | const size_t flushBufferSizeFrames = sizeof(flushBuffer) / mStreamConfig.frameSize; |
| 224 | size_t framesToFlushFromSource = frameCount - availableToWrite; |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 225 | LOG(DEBUG) << __func__ << ": flushing " << framesToFlushFromSource |
| 226 | << " frames from the pipe to avoid blocking"; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 227 | while (framesToFlushFromSource) { |
| 228 | const size_t flushSize = std::min(framesToFlushFromSource, flushBufferSizeFrames); |
| 229 | framesToFlushFromSource -= flushSize; |
| 230 | // read does not block |
| 231 | source->read(flushBuffer, flushSize); |
| 232 | } |
| 233 | } |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 234 | availableToWrite = sink->availableToWrite(); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 235 | |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 236 | if (!shouldBlockWrite && frameCount > availableToWrite) { |
| 237 | // Truncate the request to avoid blocking. |
| 238 | frameCount = availableToWrite; |
| 239 | } |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 240 | ssize_t writtenFrames = sink->write(buffer, frameCount); |
| 241 | if (writtenFrames < 0) { |
| 242 | if (writtenFrames == (ssize_t)::android::NEGOTIATE) { |
| 243 | LOG(ERROR) << __func__ << ": write to pipe returned NEGOTIATE"; |
| 244 | sink.clear(); |
| 245 | *actualFrameCount = 0; |
| 246 | return ::android::UNKNOWN_ERROR; |
| 247 | } else { |
| 248 | // write() returned UNDERRUN or WOULD_BLOCK, retry |
| 249 | LOG(ERROR) << __func__ << ": write to pipe returned unexpected " << writtenFrames; |
| 250 | writtenFrames = sink->write(buffer, frameCount); |
| 251 | } |
| 252 | } |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 253 | |
| 254 | if (writtenFrames < 0) { |
| 255 | LOG(ERROR) << __func__ << ": failed writing to pipe with " << writtenFrames; |
| 256 | *actualFrameCount = 0; |
| 257 | return ::android::UNKNOWN_ERROR; |
| 258 | } |
| 259 | LOG(VERBOSE) << __func__ << ": wrote " << writtenFrames << "frames"; |
| 260 | *actualFrameCount = writtenFrames; |
| 261 | return ::android::OK; |
| 262 | } |
| 263 | |
| 264 | ::android::status_t StreamRemoteSubmix::inRead(void* buffer, size_t frameCount, |
| 265 | size_t* actualFrameCount) { |
| 266 | // about to read from audio source |
| 267 | sp<MonoPipeReader> source = mCurrentRoute->getSource(); |
Shraddha Basantwani | 95f9232 | 2023-08-22 15:07:16 +0000 | [diff] [blame] | 268 | if (source == nullptr) { |
| 269 | int readErrorCount = mCurrentRoute->notifyReadError(); |
| 270 | if (readErrorCount < kMaxReadErrorLogs) { |
| 271 | LOG(ERROR) << __func__ |
| 272 | << ": no audio pipe yet we're trying to read! (not all errors will be " |
| 273 | "logged)"; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 274 | } else { |
Shraddha Basantwani | 95f9232 | 2023-08-22 15:07:16 +0000 | [diff] [blame] | 275 | LOG(ERROR) << __func__ << ": Read errors " << readErrorCount; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 276 | } |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 277 | const auto delayUs = getDelayInUsForFrameCount(frameCount); |
| 278 | LOG(DEBUG) << __func__ << ": no source, ignoring the read, sleeping for " << delayUs |
| 279 | << " us"; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 280 | usleep(delayUs); |
| 281 | memset(buffer, 0, mStreamConfig.frameSize * frameCount); |
| 282 | *actualFrameCount = frameCount; |
| 283 | return ::android::OK; |
| 284 | } |
| 285 | |
| 286 | // read the data from the pipe |
| 287 | int attempts = 0; |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 288 | const long delayUs = kReadAttemptSleepUs; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 289 | char* buff = (char*)buffer; |
| 290 | size_t remainingFrames = frameCount; |
Shraddha Basantwani | 95f9232 | 2023-08-22 15:07:16 +0000 | [diff] [blame] | 291 | int availableToRead = source->availableToRead(); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 292 | |
Shraddha Basantwani | 95f9232 | 2023-08-22 15:07:16 +0000 | [diff] [blame] | 293 | while ((remainingFrames > 0) && (availableToRead > 0) && (attempts < kMaxReadFailureAttempts)) { |
| 294 | LOG(VERBOSE) << __func__ << ": frames available to read " << availableToRead; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 295 | |
| 296 | ssize_t framesRead = source->read(buff, remainingFrames); |
| 297 | |
| 298 | LOG(VERBOSE) << __func__ << ": frames read " << framesRead; |
| 299 | |
| 300 | if (framesRead > 0) { |
| 301 | remainingFrames -= framesRead; |
| 302 | buff += framesRead * mStreamConfig.frameSize; |
Shraddha Basantwani | 95f9232 | 2023-08-22 15:07:16 +0000 | [diff] [blame] | 303 | availableToRead -= framesRead; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 304 | LOG(VERBOSE) << __func__ << ": (attempts = " << attempts << ") got " << framesRead |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 305 | << " frames, remaining =" << remainingFrames; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 306 | } else { |
| 307 | attempts++; |
| 308 | LOG(WARNING) << __func__ << ": read returned " << framesRead |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 309 | << " , read failure attempts = " << attempts << ", sleeping for " |
| 310 | << delayUs << " us"; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 311 | usleep(delayUs); |
| 312 | } |
| 313 | } |
| 314 | // done using the source |
| 315 | source.clear(); |
| 316 | |
| 317 | if (remainingFrames > 0) { |
| 318 | const size_t remainingBytes = remainingFrames * mStreamConfig.frameSize; |
| 319 | LOG(VERBOSE) << __func__ << ": clearing remaining_frames = " << remainingFrames; |
| 320 | memset(((char*)buffer) + (mStreamConfig.frameSize * frameCount) - remainingBytes, 0, |
| 321 | remainingBytes); |
| 322 | } |
| 323 | |
| 324 | long readCounterFrames = mCurrentRoute->updateReadCounterFrames(frameCount); |
| 325 | *actualFrameCount = frameCount; |
| 326 | |
| 327 | // compute how much we need to sleep after reading the data by comparing the wall clock with |
| 328 | // the projected time at which we should return. |
| 329 | // wall clock after reading from the pipe |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 330 | auto recordDurationUs = std::chrono::duration_cast<std::chrono::microseconds>( |
| 331 | std::chrono::steady_clock::now() - mCurrentRoute->getRecordStartTime()); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 332 | |
| 333 | // readCounterFrames contains the number of frames that have been read since the beginning of |
| 334 | // recording (including this call): it's converted to usec and compared to how long we've been |
| 335 | // recording for, which gives us how long we must wait to sync the projected recording time, and |
| 336 | // the observed recording time. |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 337 | const long projectedVsObservedOffsetUs = |
| 338 | getDelayInUsForFrameCount(readCounterFrames) - recordDurationUs.count(); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 339 | |
| 340 | LOG(VERBOSE) << __func__ << ": record duration " << recordDurationUs.count() |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame^] | 341 | << " us, will wait: " << projectedVsObservedOffsetUs << " us"; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 342 | if (projectedVsObservedOffsetUs > 0) { |
| 343 | usleep(projectedVsObservedOffsetUs); |
| 344 | } |
| 345 | return ::android::OK; |
| 346 | } |
| 347 | |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 348 | StreamInRemoteSubmix::StreamInRemoteSubmix(StreamContext&& context, |
| 349 | const SinkMetadata& sinkMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 350 | const std::vector<MicrophoneInfo>& microphones) |
Mikhail Naganov | 459b733 | 2023-08-03 10:26:21 -0700 | [diff] [blame] | 351 | : StreamIn(std::move(context), microphones), StreamSwitcher(&mContextInstance, sinkMetadata) {} |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 352 | |
| 353 | ndk::ScopedAStatus StreamInRemoteSubmix::getActiveMicrophones( |
| 354 | std::vector<MicrophoneDynamicInfo>* _aidl_return) { |
| 355 | LOG(DEBUG) << __func__ << ": not supported"; |
| 356 | *_aidl_return = std::vector<MicrophoneDynamicInfo>(); |
| 357 | return ndk::ScopedAStatus::ok(); |
| 358 | } |
| 359 | |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 360 | StreamSwitcher::DeviceSwitchBehavior StreamInRemoteSubmix::switchCurrentStream( |
| 361 | const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices) { |
| 362 | // This implementation effectively postpones stream creation until |
| 363 | // receiving the first call to 'setConnectedDevices' with a non-empty list. |
| 364 | if (isStubStream()) { |
| 365 | if (devices.size() == 1) { |
| 366 | auto deviceDesc = devices.front().type; |
| 367 | if (deviceDesc.type == |
| 368 | ::aidl::android::media::audio::common::AudioDeviceType::IN_SUBMIX) { |
| 369 | return DeviceSwitchBehavior::CREATE_NEW_STREAM; |
| 370 | } |
| 371 | LOG(ERROR) << __func__ << ": Device type " << toString(deviceDesc.type) |
| 372 | << " not supported"; |
| 373 | } else { |
| 374 | LOG(ERROR) << __func__ << ": Only single device supported."; |
| 375 | } |
| 376 | return DeviceSwitchBehavior::UNSUPPORTED_DEVICES; |
| 377 | } |
| 378 | return DeviceSwitchBehavior::USE_CURRENT_STREAM; |
| 379 | } |
| 380 | |
| 381 | std::unique_ptr<StreamCommonInterfaceEx> StreamInRemoteSubmix::createNewStream( |
| 382 | const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices, |
| 383 | StreamContext* context, const Metadata& metadata) { |
| 384 | return std::unique_ptr<StreamCommonInterfaceEx>( |
| 385 | new InnerStreamWrapper<StreamRemoteSubmix>(context, metadata, devices.front().address)); |
| 386 | } |
| 387 | |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 388 | StreamOutRemoteSubmix::StreamOutRemoteSubmix(StreamContext&& context, |
| 389 | const SourceMetadata& sourceMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 390 | const std::optional<AudioOffloadInfo>& offloadInfo) |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 391 | : StreamOut(std::move(context), offloadInfo), |
Mikhail Naganov | 459b733 | 2023-08-03 10:26:21 -0700 | [diff] [blame] | 392 | StreamSwitcher(&mContextInstance, sourceMetadata) {} |
Shraddha Basantwani | 2e46034 | 2023-07-26 16:12:21 +0000 | [diff] [blame] | 393 | |
| 394 | StreamSwitcher::DeviceSwitchBehavior StreamOutRemoteSubmix::switchCurrentStream( |
| 395 | const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices) { |
| 396 | // This implementation effectively postpones stream creation until |
| 397 | // receiving the first call to 'setConnectedDevices' with a non-empty list. |
| 398 | if (isStubStream()) { |
| 399 | if (devices.size() == 1) { |
| 400 | auto deviceDesc = devices.front().type; |
| 401 | if (deviceDesc.type == |
| 402 | ::aidl::android::media::audio::common::AudioDeviceType::OUT_SUBMIX) { |
| 403 | return DeviceSwitchBehavior::CREATE_NEW_STREAM; |
| 404 | } |
| 405 | LOG(ERROR) << __func__ << ": Device type " << toString(deviceDesc.type) |
| 406 | << " not supported"; |
| 407 | } else { |
| 408 | LOG(ERROR) << __func__ << ": Only single device supported."; |
| 409 | } |
| 410 | return DeviceSwitchBehavior::UNSUPPORTED_DEVICES; |
| 411 | } |
| 412 | return DeviceSwitchBehavior::USE_CURRENT_STREAM; |
| 413 | } |
| 414 | |
| 415 | std::unique_ptr<StreamCommonInterfaceEx> StreamOutRemoteSubmix::createNewStream( |
| 416 | const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices, |
| 417 | StreamContext* context, const Metadata& metadata) { |
| 418 | return std::unique_ptr<StreamCommonInterfaceEx>( |
| 419 | new InnerStreamWrapper<StreamRemoteSubmix>(context, metadata, devices.front().address)); |
| 420 | } |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 421 | |
| 422 | } // namespace aidl::android::hardware::audio::core |