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