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 | |
Mikhail Naganov | 9eb3314 | 2024-01-09 14:06:49 -0800 | [diff] [blame^] | 17 | #include <mutex> |
| 18 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 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; |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 28 | using aidl::android::media::audio::common::AudioDeviceAddress; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 29 | |
| 30 | namespace aidl::android::hardware::audio::core::r_submix { |
| 31 | |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 32 | // static |
Mikhail Naganov | 9eb3314 | 2024-01-09 14:06:49 -0800 | [diff] [blame^] | 33 | SubmixRoute::RoutesMonitor SubmixRoute::getRoutes(bool tryLock) { |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 34 | static std::mutex submixRoutesLock; |
| 35 | static RoutesMap submixRoutes; |
Mikhail Naganov | 9eb3314 | 2024-01-09 14:06:49 -0800 | [diff] [blame^] | 36 | return !tryLock ? RoutesMonitor(submixRoutesLock, submixRoutes) |
| 37 | : RoutesMonitor(submixRoutesLock, submixRoutes, tryLock); |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // static |
| 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 |
| 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 |
| 68 | void SubmixRoute::removeRoute(const AudioDeviceAddress& deviceAddress) { |
| 69 | getRoutes()->erase(deviceAddress); |
| 70 | } |
| 71 | |
Mikhail Naganov | 9eb3314 | 2024-01-09 14:06:49 -0800 | [diff] [blame^] | 72 | // static |
| 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 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 87 | // Verify a submix input or output stream can be opened. |
Shraddha Basantwani | 7770c15 | 2023-07-19 16:43:50 +0530 | [diff] [blame] | 88 | bool SubmixRoute::isStreamConfigValid(bool isInput, const AudioConfig& streamConfig) { |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 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. |
Shraddha Basantwani | 7770c15 | 2023-07-19 16:43:50 +0530 | [diff] [blame] | 104 | bool SubmixRoute::isStreamConfigCompatible(const AudioConfig& streamConfig) { |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 105 | std::lock_guard guard(mLock); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 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 | |
| 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 |
| 138 | bool SubmixRoute::shouldBlockWrite() { |
| 139 | std::lock_guard guard(mLock); |
| 140 | return (mStreamInOpen || (mStreamInStandby && (mReadCounterFrames != 0))); |
| 141 | } |
| 142 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 143 | long SubmixRoute::updateReadCounterFrames(size_t frameCount) { |
| 144 | std::lock_guard guard(mLock); |
| 145 | mReadCounterFrames += frameCount; |
| 146 | return mReadCounterFrames; |
| 147 | } |
| 148 | |
| 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; |
Mikhail Naganov | 7b234d4 | 2023-12-05 11:28:56 -0800 | [diff] [blame] | 160 | if (mSink != nullptr) { |
| 161 | mSink->shutdown(false); |
| 162 | } |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 163 | } else { |
| 164 | mStreamOutOpen = true; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void SubmixRoute::closeStream(bool isInput) { |
| 169 | std::lock_guard guard(mLock); |
| 170 | if (isInput) { |
Mikhail Naganov | 7b234d4 | 2023-12-05 11:28:56 -0800 | [diff] [blame] | 171 | if (--mInputRefCount == 0) { |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 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. |
Shraddha Basantwani | 7770c15 | 2023-07-19 16:43:50 +0530 | [diff] [blame] | 184 | ::android::status_t SubmixRoute::createPipe(const AudioConfig& streamConfig) { |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 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 | } |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 224 | LOG(VERBOSE) << __func__ << ": Pipe frame size : " << streamConfig.frameSize |
| 225 | << ", pipe frames : " << sink->maxFrames(); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 226 | |
| 227 | // Save references to the source and sink. |
| 228 | { |
| 229 | std::lock_guard guard(mLock); |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 230 | mPipeConfig = streamConfig; |
| 231 | mPipeConfig.frameCount = sink->maxFrames(); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 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. |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 240 | AudioConfig SubmixRoute::releasePipe() { |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 241 | std::lock_guard guard(mLock); |
| 242 | mSink.clear(); |
| 243 | mSource.clear(); |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 244 | return mPipeConfig; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | ::android::status_t SubmixRoute::resetPipe() { |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame] | 248 | return createPipe(releasePipe()); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void SubmixRoute::standby(bool isInput) { |
| 252 | std::lock_guard guard(mLock); |
| 253 | |
| 254 | if (isInput) { |
| 255 | mStreamInStandby = true; |
Shraddha Basantwani | 7770c15 | 2023-07-19 16:43:50 +0530 | [diff] [blame] | 256 | } else if (!mStreamOutStandby) { |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 257 | mStreamOutStandby = true; |
Shraddha Basantwani | 7770c15 | 2023-07-19 16:43:50 +0530 | [diff] [blame] | 258 | mStreamOutStandbyTransition = true; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 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; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 269 | mReadCounterFrames = 0; |
| 270 | } |
| 271 | } else { |
| 272 | if (mStreamOutStandby) { |
| 273 | mStreamOutStandby = false; |
| 274 | mStreamOutStandbyTransition = true; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Mikhail Naganov | 9eb3314 | 2024-01-09 14:06:49 -0800 | [diff] [blame^] | 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 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 298 | } // namespace aidl::android::hardware::audio::core::r_submix |