blob: ca3f91aefc7e55d71b7cf7911882217440137fc8 [file] [log] [blame]
Shraddha Basantwani6bb69632023-04-25 15:26:38 +05301/*
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 Naganov06085452023-11-27 17:28:51 -080019#include <audio_utils/clock.h>
20#include <error/Result.h>
21#include <error/expected_utils.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053022
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053023#include "core-impl/StreamRemoteSubmix.h"
24
25using aidl::android::hardware::audio::common::SinkMetadata;
26using aidl::android::hardware::audio::common::SourceMetadata;
Shraddha Basantwani2e460342023-07-26 16:12:21 +000027using aidl::android::hardware::audio::core::r_submix::SubmixRoute;
28using aidl::android::media::audio::common::AudioDeviceAddress;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053029using aidl::android::media::audio::common::AudioOffloadInfo;
30using aidl::android::media::audio::common::MicrophoneDynamicInfo;
31using aidl::android::media::audio::common::MicrophoneInfo;
32
33namespace aidl::android::hardware::audio::core {
34
Shraddha Basantwani2e460342023-07-26 16:12:21 +000035StreamRemoteSubmix::StreamRemoteSubmix(StreamContext* context, const Metadata& metadata,
36 const AudioDeviceAddress& deviceAddress)
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070037 : StreamCommonImpl(context, metadata),
Shraddha Basantwani2e460342023-07-26 16:12:21 +000038 mDeviceAddress(deviceAddress),
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053039 mIsInput(isInput(metadata)) {
Mikhail Naganov1eedc132023-07-21 17:45:28 -070040 mStreamConfig.frameSize = context->getFrameSize();
41 mStreamConfig.format = context->getFormat();
42 mStreamConfig.channelLayout = context->getChannelLayout();
43 mStreamConfig.sampleRate = context->getSampleRate();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053044}
45
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053046::android::status_t StreamRemoteSubmix::init() {
Mikhail Naganov3b732892023-12-20 16:05:01 -080047 mCurrentRoute = SubmixRoute::findOrCreateRoute(mDeviceAddress, mStreamConfig);
48 if (mCurrentRoute == nullptr) {
49 return ::android::NO_INIT;
Mikhail Naganov06085452023-11-27 17:28:51 -080050 }
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 Naganov7b234d42023-12-05 11:28:56 -080060 if ((!mIsInput || mCurrentRoute->isStreamInOpen()) && sink->isShutdown()) {
61 LOG(DEBUG) << __func__ << ": Shut down sink when opening stream";
Mikhail Naganov06085452023-11-27 17:28:51 -080062 if (::android::OK != mCurrentRoute->resetPipe()) {
63 LOG(ERROR) << __func__ << ": reset pipe failed";
64 return ::android::NO_INIT;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053065 }
66 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053067 mCurrentRoute->openStream(mIsInput);
Mikhail Naganov49712b52023-06-27 16:39:33 -070068 return ::android::OK;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053069}
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 Naganov49712b52023-06-27 16:39:33 -070086::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 Naganov06085452023-11-27 17:28:51 -080093 mStartTimeNs = ::android::uptimeNanos();
94 mFramesSinceStart = 0;
Mikhail Naganov49712b52023-06-27 16:39:33 -070095 return ::android::OK;
96}
97
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053098ndk::ScopedAStatus StreamRemoteSubmix::prepareToClose() {
99 if (!mIsInput) {
Mikhail Naganov3b732892023-12-20 16:05:01 -0800100 std::shared_ptr<SubmixRoute> route = SubmixRoute::findRoute(mDeviceAddress);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530101 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 Naganov2ebe3902023-11-07 16:43:26 -0800109 // The client already considers this stream as closed, release the output end.
110 route->closeStream(mIsInput);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530111 } 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.
121void 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 Naganov3b732892023-12-20 16:05:01 -0800127 SubmixRoute::removeRoute(mDeviceAddress);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530128 }
Mikhail Naganov49712b52023-06-27 16:39:33 -0700129 mCurrentRoute.reset();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530130}
131
132::android::status_t StreamRemoteSubmix::transfer(void* buffer, size_t frameCount,
133 size_t* actualFrameCount, int32_t* latencyMs) {
Mikhail Naganov2aab7662023-10-24 13:56:07 -0700134 *latencyMs = getDelayInUsForFrameCount(getStreamPipeSizeInFrames()) / 1000;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530135 LOG(VERBOSE) << __func__ << ": Latency " << *latencyMs << "ms";
Shraddha Basantwani95f92322023-08-22 15:07:16 +0000136 mCurrentRoute->exitStandby(mIsInput);
Mikhail Naganova41ff512024-03-25 16:04:27 +0000137 ::android::status_t status = mIsInput ? inRead(buffer, frameCount, actualFrameCount)
138 : outWrite(buffer, frameCount, actualFrameCount);
139 if ((status != ::android::OK && mIsInput) ||
140 ((status != ::android::OK && status != ::android::DEAD_OBJECT) && !mIsInput)) {
141 return status;
142 }
Mikhail Naganov55e0afa2024-03-05 17:42:20 -0800143 mFramesSinceStart += *actualFrameCount;
Mikhail Naganova41ff512024-03-25 16:04:27 +0000144 if (!mIsInput && status != ::android::DEAD_OBJECT) return ::android::OK;
145 // Input streams always need to block, output streams need to block when there is no sink.
146 // When the sink exists, more sophisticated blocking algorithm is implemented by MonoPipe.
Mikhail Naganov06085452023-11-27 17:28:51 -0800147 const long bufferDurationUs =
148 (*actualFrameCount) * MICROS_PER_SECOND / mContext.getSampleRate();
Mikhail Naganov878afae2024-01-03 11:22:42 -0800149 const auto totalDurationUs = (::android::uptimeNanos() - mStartTimeNs) / NANOS_PER_MICROSECOND;
Mikhail Naganov06085452023-11-27 17:28:51 -0800150 const long totalOffsetUs =
151 mFramesSinceStart * MICROS_PER_SECOND / mContext.getSampleRate() - totalDurationUs;
152 LOG(VERBOSE) << __func__ << ": totalOffsetUs " << totalOffsetUs;
153 if (totalOffsetUs > 0) {
154 const long sleepTimeUs = std::min(totalOffsetUs, bufferDurationUs);
155 LOG(VERBOSE) << __func__ << ": sleeping for " << sleepTimeUs << " us";
156 usleep(sleepTimeUs);
157 }
158 return ::android::OK;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530159}
160
Mikhail Naganov459b7332023-08-03 10:26:21 -0700161::android::status_t StreamRemoteSubmix::refinePosition(StreamDescriptor::Position* position) {
Mikhail Naganov704aec42023-07-13 11:08:29 -0700162 sp<MonoPipeReader> source = mCurrentRoute->getSource();
163 if (source == nullptr) {
164 return ::android::NO_INIT;
165 }
166 const ssize_t framesInPipe = source->availableToRead();
Shraddha Basantwani95f92322023-08-22 15:07:16 +0000167 if (framesInPipe <= 0) {
168 // No need to update the position frames
169 return ::android::OK;
Mikhail Naganov704aec42023-07-13 11:08:29 -0700170 }
171 if (mIsInput) {
172 position->frames += framesInPipe;
Shraddha Basantwani95f92322023-08-22 15:07:16 +0000173 } else if (position->frames >= framesInPipe) {
174 position->frames -= framesInPipe;
Mikhail Naganov704aec42023-07-13 11:08:29 -0700175 }
176 return ::android::OK;
177}
178
Mikhail Naganov2aab7662023-10-24 13:56:07 -0700179long StreamRemoteSubmix::getDelayInUsForFrameCount(size_t frameCount) {
180 return frameCount * MICROS_PER_SECOND / mStreamConfig.sampleRate;
181}
182
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530183// Calculate the maximum size of the pipe buffer in frames for the specified stream.
184size_t StreamRemoteSubmix::getStreamPipeSizeInFrames() {
Mikhail Naganov3b732892023-12-20 16:05:01 -0800185 auto pipeConfig = mCurrentRoute->getPipeConfig();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530186 const size_t maxFrameSize = std::max(mStreamConfig.frameSize, pipeConfig.frameSize);
187 return (pipeConfig.frameCount * pipeConfig.frameSize) / maxFrameSize;
188}
189
190::android::status_t StreamRemoteSubmix::outWrite(void* buffer, size_t frameCount,
191 size_t* actualFrameCount) {
192 sp<MonoPipe> sink = mCurrentRoute->getSink();
193 if (sink != nullptr) {
194 if (sink->isShutdown()) {
195 sink.clear();
Mikhail Naganova41ff512024-03-25 16:04:27 +0000196 if (++mWriteShutdownCount < kMaxErrorLogs) {
197 LOG(DEBUG) << __func__ << ": pipe shutdown, ignoring the write. (limited logging)";
198 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530199 *actualFrameCount = frameCount;
Mikhail Naganova41ff512024-03-25 16:04:27 +0000200 return ::android::DEAD_OBJECT; // Induce wait in `transfer`.
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530201 }
202 } else {
203 LOG(FATAL) << __func__ << ": without a pipe!";
204 return ::android::UNKNOWN_ERROR;
205 }
Mikhail Naganova41ff512024-03-25 16:04:27 +0000206 mWriteShutdownCount = 0;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530207
Mikhail Naganov06085452023-11-27 17:28:51 -0800208 LOG(VERBOSE) << __func__ << ": " << mDeviceAddress.toString() << ", " << frameCount
209 << " frames";
210
Mikhail Naganov2aab7662023-10-24 13:56:07 -0700211 const bool shouldBlockWrite = mCurrentRoute->shouldBlockWrite();
212 size_t availableToWrite = sink->availableToWrite();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530213 // NOTE: sink has been checked above and sink and source life cycles are synchronized
214 sp<MonoPipeReader> source = mCurrentRoute->getSource();
215 // If the write to the sink should be blocked, flush enough frames from the pipe to make space
216 // to write the most recent data.
Mikhail Naganov2aab7662023-10-24 13:56:07 -0700217 if (!shouldBlockWrite && availableToWrite < frameCount) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530218 static uint8_t flushBuffer[64];
219 const size_t flushBufferSizeFrames = sizeof(flushBuffer) / mStreamConfig.frameSize;
220 size_t framesToFlushFromSource = frameCount - availableToWrite;
Mikhail Naganov2aab7662023-10-24 13:56:07 -0700221 LOG(DEBUG) << __func__ << ": flushing " << framesToFlushFromSource
222 << " frames from the pipe to avoid blocking";
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530223 while (framesToFlushFromSource) {
224 const size_t flushSize = std::min(framesToFlushFromSource, flushBufferSizeFrames);
225 framesToFlushFromSource -= flushSize;
226 // read does not block
227 source->read(flushBuffer, flushSize);
228 }
229 }
Mikhail Naganov2aab7662023-10-24 13:56:07 -0700230 availableToWrite = sink->availableToWrite();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530231
Mikhail Naganov2aab7662023-10-24 13:56:07 -0700232 if (!shouldBlockWrite && frameCount > availableToWrite) {
Mikhail Naganov06085452023-11-27 17:28:51 -0800233 LOG(WARNING) << __func__ << ": writing " << availableToWrite << " vs. requested "
234 << frameCount;
Mikhail Naganov2aab7662023-10-24 13:56:07 -0700235 // Truncate the request to avoid blocking.
236 frameCount = availableToWrite;
237 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530238 ssize_t writtenFrames = sink->write(buffer, frameCount);
239 if (writtenFrames < 0) {
240 if (writtenFrames == (ssize_t)::android::NEGOTIATE) {
241 LOG(ERROR) << __func__ << ": write to pipe returned NEGOTIATE";
242 sink.clear();
243 *actualFrameCount = 0;
244 return ::android::UNKNOWN_ERROR;
245 } else {
246 // write() returned UNDERRUN or WOULD_BLOCK, retry
247 LOG(ERROR) << __func__ << ": write to pipe returned unexpected " << writtenFrames;
248 writtenFrames = sink->write(buffer, frameCount);
249 }
250 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530251
252 if (writtenFrames < 0) {
253 LOG(ERROR) << __func__ << ": failed writing to pipe with " << writtenFrames;
254 *actualFrameCount = 0;
255 return ::android::UNKNOWN_ERROR;
256 }
Mikhail Naganov06085452023-11-27 17:28:51 -0800257 if (writtenFrames > 0 && frameCount > (size_t)writtenFrames) {
258 LOG(WARNING) << __func__ << ": wrote " << writtenFrames << " vs. requested " << frameCount;
259 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530260 *actualFrameCount = writtenFrames;
261 return ::android::OK;
262}
263
264::android::status_t StreamRemoteSubmix::inRead(void* buffer, size_t frameCount,
265 size_t* actualFrameCount) {
Mikhail Naganov06085452023-11-27 17:28:51 -0800266 // in any case, it is emulated that data for the entire buffer was available
267 memset(buffer, 0, mStreamConfig.frameSize * frameCount);
268 *actualFrameCount = frameCount;
269
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530270 // about to read from audio source
271 sp<MonoPipeReader> source = mCurrentRoute->getSource();
Shraddha Basantwani95f92322023-08-22 15:07:16 +0000272 if (source == nullptr) {
Mikhail Naganova41ff512024-03-25 16:04:27 +0000273 if (++mReadErrorCount < kMaxErrorLogs) {
Shraddha Basantwani95f92322023-08-22 15:07:16 +0000274 LOG(ERROR) << __func__
275 << ": no audio pipe yet we're trying to read! (not all errors will be "
276 "logged)";
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530277 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530278 return ::android::OK;
279 }
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800280 mReadErrorCount = 0;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530281
Mikhail Naganov06085452023-11-27 17:28:51 -0800282 LOG(VERBOSE) << __func__ << ": " << mDeviceAddress.toString() << ", " << frameCount
283 << " frames";
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530284 // read the data from the pipe
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530285 char* buff = (char*)buffer;
Mikhail Naganov06085452023-11-27 17:28:51 -0800286 size_t actuallyRead = 0;
287 long remainingFrames = frameCount;
Mikhail Naganov55e0afa2024-03-05 17:42:20 -0800288 const int64_t deadlineTimeNs =
289 ::android::uptimeNanos() +
290 getDelayInUsForFrameCount(frameCount) * NANOS_PER_MICROSECOND / 2;
Mikhail Naganov06085452023-11-27 17:28:51 -0800291 while (remainingFrames > 0) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530292 ssize_t framesRead = source->read(buff, remainingFrames);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530293 LOG(VERBOSE) << __func__ << ": frames read " << framesRead;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530294 if (framesRead > 0) {
295 remainingFrames -= framesRead;
296 buff += framesRead * mStreamConfig.frameSize;
Mikhail Naganov06085452023-11-27 17:28:51 -0800297 LOG(VERBOSE) << __func__ << ": got " << framesRead
Mikhail Naganov2aab7662023-10-24 13:56:07 -0700298 << " frames, remaining =" << remainingFrames;
Mikhail Naganov06085452023-11-27 17:28:51 -0800299 actuallyRead += framesRead;
300 }
301 if (::android::uptimeNanos() >= deadlineTimeNs) break;
302 if (framesRead <= 0) {
303 LOG(VERBOSE) << __func__ << ": read returned " << framesRead
304 << ", read failure, sleeping for " << kReadAttemptSleepUs << " us";
305 usleep(kReadAttemptSleepUs);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530306 }
307 }
Mikhail Naganov06085452023-11-27 17:28:51 -0800308 if (actuallyRead < frameCount) {
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800309 if (++mReadFailureCount < kMaxReadFailureAttempts) {
310 LOG(WARNING) << __func__ << ": read " << actuallyRead << " vs. requested " << frameCount
311 << " (not all errors will be logged)";
312 }
313 } else {
314 mReadFailureCount = 0;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530315 }
Mikhail Naganov06085452023-11-27 17:28:51 -0800316 mCurrentRoute->updateReadCounterFrames(*actualFrameCount);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530317 return ::android::OK;
318}
319
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700320StreamInRemoteSubmix::StreamInRemoteSubmix(StreamContext&& context,
321 const SinkMetadata& sinkMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530322 const std::vector<MicrophoneInfo>& microphones)
Mikhail Naganov459b7332023-08-03 10:26:21 -0700323 : StreamIn(std::move(context), microphones), StreamSwitcher(&mContextInstance, sinkMetadata) {}
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530324
325ndk::ScopedAStatus StreamInRemoteSubmix::getActiveMicrophones(
326 std::vector<MicrophoneDynamicInfo>* _aidl_return) {
327 LOG(DEBUG) << __func__ << ": not supported";
328 *_aidl_return = std::vector<MicrophoneDynamicInfo>();
329 return ndk::ScopedAStatus::ok();
330}
331
Shraddha Basantwani2e460342023-07-26 16:12:21 +0000332StreamSwitcher::DeviceSwitchBehavior StreamInRemoteSubmix::switchCurrentStream(
333 const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices) {
334 // This implementation effectively postpones stream creation until
335 // receiving the first call to 'setConnectedDevices' with a non-empty list.
336 if (isStubStream()) {
337 if (devices.size() == 1) {
338 auto deviceDesc = devices.front().type;
339 if (deviceDesc.type ==
340 ::aidl::android::media::audio::common::AudioDeviceType::IN_SUBMIX) {
341 return DeviceSwitchBehavior::CREATE_NEW_STREAM;
342 }
343 LOG(ERROR) << __func__ << ": Device type " << toString(deviceDesc.type)
344 << " not supported";
345 } else {
346 LOG(ERROR) << __func__ << ": Only single device supported.";
347 }
348 return DeviceSwitchBehavior::UNSUPPORTED_DEVICES;
349 }
350 return DeviceSwitchBehavior::USE_CURRENT_STREAM;
351}
352
353std::unique_ptr<StreamCommonInterfaceEx> StreamInRemoteSubmix::createNewStream(
354 const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices,
355 StreamContext* context, const Metadata& metadata) {
356 return std::unique_ptr<StreamCommonInterfaceEx>(
357 new InnerStreamWrapper<StreamRemoteSubmix>(context, metadata, devices.front().address));
358}
359
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700360StreamOutRemoteSubmix::StreamOutRemoteSubmix(StreamContext&& context,
361 const SourceMetadata& sourceMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530362 const std::optional<AudioOffloadInfo>& offloadInfo)
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700363 : StreamOut(std::move(context), offloadInfo),
Mikhail Naganov459b7332023-08-03 10:26:21 -0700364 StreamSwitcher(&mContextInstance, sourceMetadata) {}
Shraddha Basantwani2e460342023-07-26 16:12:21 +0000365
366StreamSwitcher::DeviceSwitchBehavior StreamOutRemoteSubmix::switchCurrentStream(
367 const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices) {
368 // This implementation effectively postpones stream creation until
369 // receiving the first call to 'setConnectedDevices' with a non-empty list.
370 if (isStubStream()) {
371 if (devices.size() == 1) {
372 auto deviceDesc = devices.front().type;
373 if (deviceDesc.type ==
374 ::aidl::android::media::audio::common::AudioDeviceType::OUT_SUBMIX) {
375 return DeviceSwitchBehavior::CREATE_NEW_STREAM;
376 }
377 LOG(ERROR) << __func__ << ": Device type " << toString(deviceDesc.type)
378 << " not supported";
379 } else {
380 LOG(ERROR) << __func__ << ": Only single device supported.";
381 }
382 return DeviceSwitchBehavior::UNSUPPORTED_DEVICES;
383 }
384 return DeviceSwitchBehavior::USE_CURRENT_STREAM;
385}
386
387std::unique_ptr<StreamCommonInterfaceEx> StreamOutRemoteSubmix::createNewStream(
388 const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices,
389 StreamContext* context, const Metadata& metadata) {
390 return std::unique_ptr<StreamCommonInterfaceEx>(
391 new InnerStreamWrapper<StreamRemoteSubmix>(context, metadata, devices.front().address));
392}
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530393
394} // namespace aidl::android::hardware::audio::core