blob: f04e607283b7641e6a2021eba3107cf574de9d36 [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_SubmixRoute"
18#include <android-base/logging.h>
19#include <media/AidlConversionCppNdk.h>
20
21#include <Utils.h>
22
23#include "SubmixRoute.h"
24
25using aidl::android::hardware::audio::common::getChannelCount;
26
27namespace aidl::android::hardware::audio::core::r_submix {
28
29// Verify a submix input or output stream can be opened.
Shraddha Basantwani7770c152023-07-19 16:43:50 +053030bool SubmixRoute::isStreamConfigValid(bool isInput, const AudioConfig& streamConfig) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053031 // If the stream is already open, don't open it again.
32 // ENABLE_LEGACY_INPUT_OPEN is default behaviour
33 if (!isInput && isStreamOutOpen()) {
34 LOG(ERROR) << __func__ << ": output stream already open.";
35 return false;
36 }
37 // If either stream is open, verify the existing pipe config matches the stream config.
38 if (hasAtleastOneStreamOpen() && !isStreamConfigCompatible(streamConfig)) {
39 return false;
40 }
41 return true;
42}
43
44// Compare this stream config with existing pipe config, returning false if they do *not*
45// match, true otherwise.
Shraddha Basantwani7770c152023-07-19 16:43:50 +053046bool SubmixRoute::isStreamConfigCompatible(const AudioConfig& streamConfig) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053047 if (streamConfig.channelLayout != mPipeConfig.channelLayout) {
48 LOG(ERROR) << __func__ << ": channel count mismatch, stream channels = "
49 << streamConfig.channelLayout.toString()
50 << " pipe config channels = " << mPipeConfig.channelLayout.toString();
51 return false;
52 }
53 if (streamConfig.sampleRate != mPipeConfig.sampleRate) {
54 LOG(ERROR) << __func__
55 << ": sample rate mismatch, stream sample rate = " << streamConfig.sampleRate
56 << " pipe config sample rate = " << mPipeConfig.sampleRate;
57 return false;
58 }
59 if (streamConfig.format != mPipeConfig.format) {
60 LOG(ERROR) << __func__
61 << ": format mismatch, stream format = " << streamConfig.format.toString()
62 << " pipe config format = " << mPipeConfig.format.toString();
63 return false;
64 }
65 return true;
66}
67
68bool SubmixRoute::hasAtleastOneStreamOpen() {
69 std::lock_guard guard(mLock);
70 return (mStreamInOpen || mStreamOutOpen);
71}
72
73// We DO NOT block if:
74// - no peer input stream is present
75// - the peer input is in standby AFTER having been active.
76// We DO block if:
77// - the input was never activated to avoid discarding first frames in the pipe in case capture
78// start was delayed
79bool SubmixRoute::shouldBlockWrite() {
80 std::lock_guard guard(mLock);
81 return (mStreamInOpen || (mStreamInStandby && (mReadCounterFrames != 0)));
82}
83
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053084long SubmixRoute::updateReadCounterFrames(size_t frameCount) {
85 std::lock_guard guard(mLock);
86 mReadCounterFrames += frameCount;
87 return mReadCounterFrames;
88}
89
90void SubmixRoute::openStream(bool isInput) {
91 std::lock_guard guard(mLock);
92 if (isInput) {
93 if (mStreamInOpen) {
94 mInputRefCount++;
95 } else {
96 mInputRefCount = 1;
97 mStreamInOpen = true;
98 }
99 mStreamInStandby = true;
100 mReadCounterFrames = 0;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530101 } else {
102 mStreamOutOpen = true;
103 }
104}
105
106void SubmixRoute::closeStream(bool isInput) {
107 std::lock_guard guard(mLock);
108 if (isInput) {
Shunkai Yao0347f0e2023-12-12 22:55:55 +0000109 mInputRefCount--;
110 if (mInputRefCount == 0) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530111 mStreamInOpen = false;
112 if (mSink != nullptr) {
113 mSink->shutdown(true);
114 }
115 }
116 } else {
117 mStreamOutOpen = false;
118 }
119}
120
121// If SubmixRoute doesn't exist for a port, create a pipe for the submix audio device of size
122// buffer_size_frames and store config of the submix audio device.
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530123::android::status_t SubmixRoute::createPipe(const AudioConfig& streamConfig) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530124 const int channelCount = getChannelCount(streamConfig.channelLayout);
125 const audio_format_t audioFormat = VALUE_OR_RETURN_STATUS(
126 aidl2legacy_AudioFormatDescription_audio_format_t(streamConfig.format));
127 const ::android::NBAIO_Format format =
128 ::android::Format_from_SR_C(streamConfig.sampleRate, channelCount, audioFormat);
129 const ::android::NBAIO_Format offers[1] = {format};
130 size_t numCounterOffers = 0;
131
132 const size_t pipeSizeInFrames =
133 r_submix::kDefaultPipeSizeInFrames *
134 ((float)streamConfig.sampleRate / r_submix::kDefaultSampleRateHz);
135 LOG(VERBOSE) << __func__ << ": creating pipe, rate : " << streamConfig.sampleRate
136 << ", pipe size : " << pipeSizeInFrames;
137
138 // Create a MonoPipe with optional blocking set to true.
139 sp<MonoPipe> sink = sp<MonoPipe>::make(pipeSizeInFrames, format, true /*writeCanBlock*/);
140 if (sink == nullptr) {
141 LOG(FATAL) << __func__ << ": sink is null";
142 return ::android::UNEXPECTED_NULL;
143 }
144
145 // Negotiation between the source and sink cannot fail as the device open operation
146 // creates both ends of the pipe using the same audio format.
147 ssize_t index = sink->negotiate(offers, 1, nullptr, numCounterOffers);
148 if (index != 0) {
149 LOG(FATAL) << __func__ << ": Negotiation for the sink failed, index = " << index;
150 return ::android::BAD_INDEX;
151 }
152 sp<MonoPipeReader> source = sp<MonoPipeReader>::make(sink.get());
153 if (source == nullptr) {
154 LOG(FATAL) << __func__ << ": source is null";
155 return ::android::UNEXPECTED_NULL;
156 }
157 numCounterOffers = 0;
158 index = source->negotiate(offers, 1, nullptr, numCounterOffers);
159 if (index != 0) {
160 LOG(FATAL) << __func__ << ": Negotiation for the source failed, index = " << index;
161 return ::android::BAD_INDEX;
162 }
163 LOG(VERBOSE) << __func__ << ": created pipe";
164
165 mPipeConfig = streamConfig;
166 mPipeConfig.frameCount = sink->maxFrames();
167
168 LOG(VERBOSE) << __func__ << ": Pipe frame size : " << mPipeConfig.frameSize
169 << ", pipe frames : " << mPipeConfig.frameCount;
170
171 // Save references to the source and sink.
172 {
173 std::lock_guard guard(mLock);
174 mSink = std::move(sink);
175 mSource = std::move(source);
176 }
177
178 return ::android::OK;
179}
180
181// Release references to the sink and source.
182void SubmixRoute::releasePipe() {
183 std::lock_guard guard(mLock);
184 mSink.clear();
185 mSource.clear();
186}
187
188::android::status_t SubmixRoute::resetPipe() {
189 releasePipe();
190 return createPipe(mPipeConfig);
191}
192
193void SubmixRoute::standby(bool isInput) {
194 std::lock_guard guard(mLock);
195
196 if (isInput) {
197 mStreamInStandby = true;
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530198 } else if (!mStreamOutStandby) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530199 mStreamOutStandby = true;
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530200 mStreamOutStandbyTransition = true;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530201 }
202}
203
204void SubmixRoute::exitStandby(bool isInput) {
205 std::lock_guard guard(mLock);
206
207 if (isInput) {
208 if (mStreamInStandby || mStreamOutStandbyTransition) {
209 mStreamInStandby = false;
210 mStreamOutStandbyTransition = false;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530211 mReadCounterFrames = 0;
212 }
213 } else {
214 if (mStreamOutStandby) {
215 mStreamOutStandby = false;
216 mStreamOutStandbyTransition = true;
217 }
218 }
219}
220
221} // namespace aidl::android::hardware::audio::core::r_submix