blob: 235c9a3f32bf4da27272c94c5ff9ab837922ad69 [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;
Mikhail Naganov7b234d42023-12-05 11:28:56 -0800101 if (mSink != nullptr) {
102 mSink->shutdown(false);
103 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530104 } else {
105 mStreamOutOpen = true;
106 }
107}
108
109void SubmixRoute::closeStream(bool isInput) {
110 std::lock_guard guard(mLock);
111 if (isInput) {
Mikhail Naganov7b234d42023-12-05 11:28:56 -0800112 if (--mInputRefCount == 0) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530113 mStreamInOpen = false;
114 if (mSink != nullptr) {
115 mSink->shutdown(true);
116 }
117 }
118 } else {
119 mStreamOutOpen = false;
120 }
121}
122
123// If SubmixRoute doesn't exist for a port, create a pipe for the submix audio device of size
124// buffer_size_frames and store config of the submix audio device.
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530125::android::status_t SubmixRoute::createPipe(const AudioConfig& streamConfig) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530126 const int channelCount = getChannelCount(streamConfig.channelLayout);
127 const audio_format_t audioFormat = VALUE_OR_RETURN_STATUS(
128 aidl2legacy_AudioFormatDescription_audio_format_t(streamConfig.format));
129 const ::android::NBAIO_Format format =
130 ::android::Format_from_SR_C(streamConfig.sampleRate, channelCount, audioFormat);
131 const ::android::NBAIO_Format offers[1] = {format};
132 size_t numCounterOffers = 0;
133
134 const size_t pipeSizeInFrames =
135 r_submix::kDefaultPipeSizeInFrames *
136 ((float)streamConfig.sampleRate / r_submix::kDefaultSampleRateHz);
137 LOG(VERBOSE) << __func__ << ": creating pipe, rate : " << streamConfig.sampleRate
138 << ", pipe size : " << pipeSizeInFrames;
139
140 // Create a MonoPipe with optional blocking set to true.
141 sp<MonoPipe> sink = sp<MonoPipe>::make(pipeSizeInFrames, format, true /*writeCanBlock*/);
142 if (sink == nullptr) {
143 LOG(FATAL) << __func__ << ": sink is null";
144 return ::android::UNEXPECTED_NULL;
145 }
146
147 // Negotiation between the source and sink cannot fail as the device open operation
148 // creates both ends of the pipe using the same audio format.
149 ssize_t index = sink->negotiate(offers, 1, nullptr, numCounterOffers);
150 if (index != 0) {
151 LOG(FATAL) << __func__ << ": Negotiation for the sink failed, index = " << index;
152 return ::android::BAD_INDEX;
153 }
154 sp<MonoPipeReader> source = sp<MonoPipeReader>::make(sink.get());
155 if (source == nullptr) {
156 LOG(FATAL) << __func__ << ": source is null";
157 return ::android::UNEXPECTED_NULL;
158 }
159 numCounterOffers = 0;
160 index = source->negotiate(offers, 1, nullptr, numCounterOffers);
161 if (index != 0) {
162 LOG(FATAL) << __func__ << ": Negotiation for the source failed, index = " << index;
163 return ::android::BAD_INDEX;
164 }
165 LOG(VERBOSE) << __func__ << ": created pipe";
166
167 mPipeConfig = streamConfig;
168 mPipeConfig.frameCount = sink->maxFrames();
169
170 LOG(VERBOSE) << __func__ << ": Pipe frame size : " << mPipeConfig.frameSize
171 << ", pipe frames : " << mPipeConfig.frameCount;
172
173 // Save references to the source and sink.
174 {
175 std::lock_guard guard(mLock);
176 mSink = std::move(sink);
177 mSource = std::move(source);
178 }
179
180 return ::android::OK;
181}
182
183// Release references to the sink and source.
184void SubmixRoute::releasePipe() {
185 std::lock_guard guard(mLock);
186 mSink.clear();
187 mSource.clear();
188}
189
190::android::status_t SubmixRoute::resetPipe() {
191 releasePipe();
192 return createPipe(mPipeConfig);
193}
194
195void SubmixRoute::standby(bool isInput) {
196 std::lock_guard guard(mLock);
197
198 if (isInput) {
199 mStreamInStandby = true;
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530200 } else if (!mStreamOutStandby) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530201 mStreamOutStandby = true;
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530202 mStreamOutStandbyTransition = true;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530203 }
204}
205
206void SubmixRoute::exitStandby(bool isInput) {
207 std::lock_guard guard(mLock);
208
209 if (isInput) {
210 if (mStreamInStandby || mStreamOutStandbyTransition) {
211 mStreamInStandby = false;
212 mStreamOutStandbyTransition = false;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530213 mReadCounterFrames = 0;
214 }
215 } else {
216 if (mStreamOutStandby) {
217 mStreamOutStandby = false;
218 mStreamOutStandbyTransition = true;
219 }
220 }
221}
222
223} // namespace aidl::android::hardware::audio::core::r_submix