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