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