• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <mutex>
18 
19 #define LOG_TAG "AHAL_SubmixRoute"
20 #include <android-base/logging.h>
21 #include <media/AidlConversionCppNdk.h>
22 
23 #include <Utils.h>
24 
25 #include "SubmixRoute.h"
26 
27 using aidl::android::hardware::audio::common::getChannelCount;
28 using aidl::android::media::audio::common::AudioDeviceAddress;
29 
30 namespace aidl::android::hardware::audio::core::r_submix {
31 
32 // static
getRoutes(bool tryLock)33 SubmixRoute::RoutesMonitor SubmixRoute::getRoutes(bool tryLock) {
34     static std::mutex submixRoutesLock;
35     static RoutesMap submixRoutes;
36     return !tryLock ? RoutesMonitor(submixRoutesLock, submixRoutes)
37                     : RoutesMonitor(submixRoutesLock, submixRoutes, tryLock);
38 }
39 
40 // static
findOrCreateRoute(const AudioDeviceAddress & deviceAddress,const AudioConfig & pipeConfig)41 std::shared_ptr<SubmixRoute> SubmixRoute::findOrCreateRoute(const AudioDeviceAddress& deviceAddress,
42                                                             const AudioConfig& pipeConfig) {
43     auto routes = getRoutes();
44     auto routeItr = routes->find(deviceAddress);
45     if (routeItr != routes->end()) {
46         return routeItr->second;
47     }
48     auto route = std::make_shared<SubmixRoute>();
49     if (::android::OK != route->createPipe(pipeConfig)) {
50         LOG(ERROR) << __func__ << ": create pipe failed";
51         return nullptr;
52     }
53     routes->emplace(deviceAddress, route);
54     return route;
55 }
56 
57 // static
findRoute(const AudioDeviceAddress & deviceAddress)58 std::shared_ptr<SubmixRoute> SubmixRoute::findRoute(const AudioDeviceAddress& deviceAddress) {
59     auto routes = getRoutes();
60     auto routeItr = routes->find(deviceAddress);
61     if (routeItr != routes->end()) {
62         return routeItr->second;
63     }
64     return nullptr;
65 }
66 
67 // static
removeRoute(const AudioDeviceAddress & deviceAddress)68 void SubmixRoute::removeRoute(const AudioDeviceAddress& deviceAddress) {
69     getRoutes()->erase(deviceAddress);
70 }
71 
72 // static
dumpRoutes()73 std::string SubmixRoute::dumpRoutes() {
74     auto routes = getRoutes(true /*tryLock*/);
75     std::string result;
76     if (routes->empty()) result.append(" <Empty>");
77     for (const auto& r : *(routes.operator->())) {
78         result.append(" - ")
79                 .append(r.first.toString())
80                 .append(": ")
81                 .append(r.second->dump())
82                 .append("\n");
83     }
84     return result;
85 }
86 
87 // Verify a submix input or output stream can be opened.
isStreamConfigValid(bool isInput,const AudioConfig & streamConfig)88 bool SubmixRoute::isStreamConfigValid(bool isInput, const AudioConfig& streamConfig) {
89     // If the stream is already open, don't open it again.
90     // ENABLE_LEGACY_INPUT_OPEN is default behaviour
91     if (!isInput && isStreamOutOpen()) {
92         LOG(ERROR) << __func__ << ": output stream already open.";
93         return false;
94     }
95     // If either stream is open, verify the existing pipe config matches the stream config.
96     if (hasAtleastOneStreamOpen() && !isStreamConfigCompatible(streamConfig)) {
97         return false;
98     }
99     return true;
100 }
101 
102 // Compare this stream config with existing pipe config, returning false if they do *not*
103 // match, true otherwise.
isStreamConfigCompatible(const AudioConfig & streamConfig)104 bool SubmixRoute::isStreamConfigCompatible(const AudioConfig& streamConfig) {
105     std::lock_guard guard(mLock);
106     if (streamConfig.channelLayout != mPipeConfig.channelLayout) {
107         LOG(ERROR) << __func__ << ": channel count mismatch, stream channels = "
108                    << streamConfig.channelLayout.toString()
109                    << " pipe config channels = " << mPipeConfig.channelLayout.toString();
110         return false;
111     }
112     if (streamConfig.sampleRate != mPipeConfig.sampleRate) {
113         LOG(ERROR) << __func__
114                    << ": sample rate mismatch, stream sample rate = " << streamConfig.sampleRate
115                    << " pipe config sample rate = " << mPipeConfig.sampleRate;
116         return false;
117     }
118     if (streamConfig.format != mPipeConfig.format) {
119         LOG(ERROR) << __func__
120                    << ": format mismatch, stream format = " << streamConfig.format.toString()
121                    << " pipe config format = " << mPipeConfig.format.toString();
122         return false;
123     }
124     return true;
125 }
126 
hasAtleastOneStreamOpen()127 bool SubmixRoute::hasAtleastOneStreamOpen() {
128     std::lock_guard guard(mLock);
129     return (mStreamInOpen || mStreamOutOpen);
130 }
131 
132 // We DO NOT block if:
133 // - no peer input stream is present
134 // - the peer input is in standby AFTER having been active.
135 // We DO block if:
136 // - the input was never activated to avoid discarding first frames in the pipe in case capture
137 // start was delayed
shouldBlockWrite()138 bool SubmixRoute::shouldBlockWrite() {
139     std::lock_guard guard(mLock);
140     return (mStreamInOpen || (mStreamInStandby && (mReadCounterFrames != 0)));
141 }
142 
updateReadCounterFrames(size_t frameCount)143 long SubmixRoute::updateReadCounterFrames(size_t frameCount) {
144     std::lock_guard guard(mLock);
145     mReadCounterFrames += frameCount;
146     return mReadCounterFrames;
147 }
148 
openStream(bool isInput)149 void SubmixRoute::openStream(bool isInput) {
150     std::lock_guard guard(mLock);
151     if (isInput) {
152         if (mStreamInOpen) {
153             mInputRefCount++;
154         } else {
155             mInputRefCount = 1;
156             mStreamInOpen = true;
157         }
158         mStreamInStandby = true;
159         mReadCounterFrames = 0;
160         if (mSink != nullptr) {
161             mSink->shutdown(false);
162         }
163     } else {
164         mStreamOutOpen = true;
165     }
166 }
167 
closeStream(bool isInput)168 void SubmixRoute::closeStream(bool isInput) {
169     std::lock_guard guard(mLock);
170     if (isInput) {
171         if (--mInputRefCount == 0) {
172             mStreamInOpen = false;
173             if (mSink != nullptr) {
174                 mSink->shutdown(true);
175             }
176         }
177     } else {
178         mStreamOutOpen = false;
179     }
180 }
181 
182 // If SubmixRoute doesn't exist for a port, create a pipe for the submix audio device of size
183 // buffer_size_frames and store config of the submix audio device.
createPipe(const AudioConfig & streamConfig)184 ::android::status_t SubmixRoute::createPipe(const AudioConfig& streamConfig) {
185     const int channelCount = getChannelCount(streamConfig.channelLayout);
186     const audio_format_t audioFormat = VALUE_OR_RETURN_STATUS(
187             aidl2legacy_AudioFormatDescription_audio_format_t(streamConfig.format));
188     const ::android::NBAIO_Format format =
189             ::android::Format_from_SR_C(streamConfig.sampleRate, channelCount, audioFormat);
190     const ::android::NBAIO_Format offers[1] = {format};
191     size_t numCounterOffers = 0;
192 
193     const size_t pipeSizeInFrames =
194             r_submix::kDefaultPipeSizeInFrames *
195             ((float)streamConfig.sampleRate / r_submix::kDefaultSampleRateHz);
196     LOG(VERBOSE) << __func__ << ": creating pipe, rate : " << streamConfig.sampleRate
197                  << ", pipe size : " << pipeSizeInFrames;
198 
199     // Create a MonoPipe with optional blocking set to true.
200     sp<MonoPipe> sink = sp<MonoPipe>::make(pipeSizeInFrames, format, true /*writeCanBlock*/);
201     if (sink == nullptr) {
202         LOG(FATAL) << __func__ << ": sink is null";
203         return ::android::UNEXPECTED_NULL;
204     }
205 
206     // Negotiation between the source and sink cannot fail as the device open operation
207     // creates both ends of the pipe using the same audio format.
208     ssize_t index = sink->negotiate(offers, 1, nullptr, numCounterOffers);
209     if (index != 0) {
210         LOG(FATAL) << __func__ << ": Negotiation for the sink failed, index = " << index;
211         return ::android::BAD_INDEX;
212     }
213     sp<MonoPipeReader> source = sp<MonoPipeReader>::make(sink.get());
214     if (source == nullptr) {
215         LOG(FATAL) << __func__ << ": source is null";
216         return ::android::UNEXPECTED_NULL;
217     }
218     numCounterOffers = 0;
219     index = source->negotiate(offers, 1, nullptr, numCounterOffers);
220     if (index != 0) {
221         LOG(FATAL) << __func__ << ": Negotiation for the source failed, index = " << index;
222         return ::android::BAD_INDEX;
223     }
224     LOG(VERBOSE) << __func__ << ": Pipe frame size : " << streamConfig.frameSize
225                  << ", pipe frames : " << sink->maxFrames();
226 
227     // Save references to the source and sink.
228     {
229         std::lock_guard guard(mLock);
230         mPipeConfig = streamConfig;
231         mPipeConfig.frameCount = sink->maxFrames();
232         mSink = std::move(sink);
233         mSource = std::move(source);
234     }
235 
236     return ::android::OK;
237 }
238 
239 // Release references to the sink and source.
releasePipe()240 AudioConfig SubmixRoute::releasePipe() {
241     std::lock_guard guard(mLock);
242     mSink.clear();
243     mSource.clear();
244     return mPipeConfig;
245 }
246 
resetPipe()247 ::android::status_t SubmixRoute::resetPipe() {
248     return createPipe(releasePipe());
249 }
250 
standby(bool isInput)251 void SubmixRoute::standby(bool isInput) {
252     std::lock_guard guard(mLock);
253 
254     if (isInput) {
255         mStreamInStandby = true;
256     } else if (!mStreamOutStandby) {
257         mStreamOutStandby = true;
258         mStreamOutStandbyTransition = true;
259     }
260 }
261 
exitStandby(bool isInput)262 void SubmixRoute::exitStandby(bool isInput) {
263     std::lock_guard guard(mLock);
264 
265     if (isInput) {
266         if (mStreamInStandby || mStreamOutStandbyTransition) {
267             mStreamInStandby = false;
268             mStreamOutStandbyTransition = false;
269             mReadCounterFrames = 0;
270         }
271     } else {
272         if (mStreamOutStandby) {
273             mStreamOutStandby = false;
274             mStreamOutStandbyTransition = true;
275         }
276     }
277 }
278 
dump()279 std::string SubmixRoute::dump() NO_THREAD_SAFETY_ANALYSIS {
280     const bool isLocked = mLock.try_lock();
281     std::string result = std::string(isLocked ? "" : "! ")
282                                  .append("Input ")
283                                  .append(mStreamInOpen ? "open" : "closed")
284                                  .append(mStreamInStandby ? ", standby" : ", active")
285                                  .append(", refcount: ")
286                                  .append(std::to_string(mInputRefCount))
287                                  .append(", framesRead: ")
288                                  .append(mSource ? std::to_string(mSource->framesRead()) : "<null>")
289                                  .append("; Output ")
290                                  .append(mStreamOutOpen ? "open" : "closed")
291                                  .append(mStreamOutStandby ? ", standby" : ", active")
292                                  .append(", framesWritten: ")
293                                  .append(mSink ? std::to_string(mSink->framesWritten()) : "<null>");
294     if (isLocked) mLock.unlock();
295     return result;
296 }
297 
298 }  // namespace aidl::android::hardware::audio::core::r_submix
299