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 | |
| 17 | #define LOG_TAG "AHAL_ModuleRemoteSubmix" |
| 18 | |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <android-base/logging.h> |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 22 | #include <error/expected_utils.h> |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 23 | |
Mikhail Naganov | 1350187 | 2023-10-18 16:15:46 -0700 | [diff] [blame] | 24 | #include "SubmixRoute.h" |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 25 | #include "core-impl/ModuleRemoteSubmix.h" |
| 26 | #include "core-impl/StreamRemoteSubmix.h" |
| 27 | |
| 28 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 29 | using aidl::android::hardware::audio::common::SourceMetadata; |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame^] | 30 | using aidl::android::media::audio::common::AudioDeviceAddress; |
Dean Wheatley | ce9767a | 2023-10-24 11:23:49 +1100 | [diff] [blame] | 31 | using aidl::android::media::audio::common::AudioFormatType; |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame^] | 32 | using aidl::android::media::audio::common::AudioIoFlags; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 33 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 34 | using aidl::android::media::audio::common::AudioPort; |
| 35 | using aidl::android::media::audio::common::AudioPortConfig; |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame^] | 36 | using aidl::android::media::audio::common::AudioPortExt; |
| 37 | using aidl::android::media::audio::common::AudioProfile; |
| 38 | using aidl::android::media::audio::common::Int; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 39 | using aidl::android::media::audio::common::MicrophoneInfo; |
| 40 | |
| 41 | namespace aidl::android::hardware::audio::core { |
| 42 | |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame^] | 43 | namespace { |
| 44 | |
| 45 | std::optional<r_submix::AudioConfig> getRemoteEndConfig(const AudioPort& audioPort) { |
| 46 | const auto& deviceAddress = audioPort.ext.get<AudioPortExt::device>().device.address; |
| 47 | const bool isInput = audioPort.flags.getTag() == AudioIoFlags::input; |
| 48 | if (auto submixRoute = r_submix::SubmixRoute::findRoute(deviceAddress); |
| 49 | submixRoute != nullptr) { |
| 50 | if ((isInput && submixRoute->isStreamOutOpen()) || |
| 51 | (!isInput && submixRoute->isStreamInOpen())) { |
| 52 | return submixRoute->getPipeConfig(); |
| 53 | } |
| 54 | } |
| 55 | return {}; |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 60 | ndk::ScopedAStatus ModuleRemoteSubmix::getMicMute(bool* _aidl_return __unused) { |
| 61 | LOG(DEBUG) << __func__ << ": is not supported"; |
| 62 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 63 | } |
| 64 | |
| 65 | ndk::ScopedAStatus ModuleRemoteSubmix::setMicMute(bool in_mute __unused) { |
| 66 | LOG(DEBUG) << __func__ << ": is not supported"; |
| 67 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 68 | } |
| 69 | |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame^] | 70 | ndk::ScopedAStatus ModuleRemoteSubmix::setAudioPortConfig(const AudioPortConfig& in_requested, |
| 71 | AudioPortConfig* out_suggested, |
| 72 | bool* _aidl_return) { |
| 73 | auto fillConfig = [this](const AudioPort& port, AudioPortConfig* config) { |
| 74 | if (port.ext.getTag() == AudioPortExt::device) { |
| 75 | if (auto pipeConfig = getRemoteEndConfig(port); pipeConfig.has_value()) { |
| 76 | LOG(DEBUG) << "setAudioPortConfig: suggesting port config from the remote end."; |
| 77 | config->format = pipeConfig->format; |
| 78 | config->channelMask = pipeConfig->channelLayout; |
| 79 | config->sampleRate = Int{.value = pipeConfig->sampleRate}; |
| 80 | config->flags = port.flags; |
| 81 | config->ext = port.ext; |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | return generateDefaultPortConfig(port, config); |
| 86 | }; |
| 87 | return Module::setAudioPortConfigImpl(in_requested, fillConfig, out_suggested, _aidl_return); |
| 88 | } |
| 89 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 90 | ndk::ScopedAStatus ModuleRemoteSubmix::createInputStream( |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 91 | StreamContext&& context, const SinkMetadata& sinkMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 92 | const std::vector<MicrophoneInfo>& microphones, std::shared_ptr<StreamIn>* result) { |
Dean Wheatley | ce9767a | 2023-10-24 11:23:49 +1100 | [diff] [blame] | 93 | if (context.getFormat().type != AudioFormatType::PCM) { |
| 94 | LOG(DEBUG) << __func__ << ": not supported for format " << context.getFormat().toString(); |
| 95 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 96 | } |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 97 | return createStreamInstance<StreamInRemoteSubmix>(result, std::move(context), sinkMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 98 | microphones); |
| 99 | } |
| 100 | |
| 101 | ndk::ScopedAStatus ModuleRemoteSubmix::createOutputStream( |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 102 | StreamContext&& context, const SourceMetadata& sourceMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 103 | const std::optional<AudioOffloadInfo>& offloadInfo, std::shared_ptr<StreamOut>* result) { |
Dean Wheatley | ce9767a | 2023-10-24 11:23:49 +1100 | [diff] [blame] | 104 | if (context.getFormat().type != AudioFormatType::PCM) { |
| 105 | LOG(DEBUG) << __func__ << ": not supported for format " << context.getFormat().toString(); |
| 106 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 107 | } |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 108 | return createStreamInstance<StreamOutRemoteSubmix>(result, std::move(context), sourceMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 109 | offloadInfo); |
| 110 | } |
| 111 | |
Mikhail Naganov | a92039a | 2023-12-20 14:27:22 -0800 | [diff] [blame] | 112 | ndk::ScopedAStatus ModuleRemoteSubmix::populateConnectedDevicePort(AudioPort* audioPort, int32_t) { |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame^] | 113 | if (audioPort->ext.getTag() != AudioPortExt::device) { |
| 114 | LOG(ERROR) << __func__ << ": not a device port: " << audioPort->toString(); |
| 115 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 116 | } |
| 117 | // If there is already a pipe with a stream for the port address, provide its configuration as |
| 118 | // the only option. Otherwise, find the corresponding mix port and copy its profiles. |
| 119 | if (auto pipeConfig = getRemoteEndConfig(*audioPort); pipeConfig.has_value()) { |
| 120 | audioPort->profiles.clear(); |
| 121 | audioPort->profiles.push_back(AudioProfile{ |
| 122 | .format = pipeConfig->format, |
| 123 | .channelMasks = std::vector<AudioChannelLayout>({pipeConfig->channelLayout}), |
| 124 | .sampleRates = std::vector<int>({pipeConfig->sampleRate})}); |
| 125 | LOG(DEBUG) << __func__ << ": populated from remote end as: " << audioPort->toString(); |
| 126 | return ndk::ScopedAStatus::ok(); |
| 127 | } |
| 128 | |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 129 | // At this moment, the port has the same ID as the template port, see connectExternalDevice. |
Mikhail Naganov | 84bcc04 | 2023-10-05 17:36:57 -0700 | [diff] [blame] | 130 | std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(audioPort->id); |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 131 | if (routes.empty()) { |
| 132 | LOG(ERROR) << __func__ << ": no routes found for the port " << audioPort->toString(); |
| 133 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 134 | } |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 135 | const auto& route = *routes.begin(); |
| 136 | AudioPort mixPort; |
Mikhail Naganov | 84bcc04 | 2023-10-05 17:36:57 -0700 | [diff] [blame] | 137 | if (route->sinkPortId == audioPort->id) { |
| 138 | if (route->sourcePortIds.empty()) { |
| 139 | LOG(ERROR) << __func__ << ": invalid route " << route->toString(); |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 140 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 141 | } |
Mikhail Naganov | 84bcc04 | 2023-10-05 17:36:57 -0700 | [diff] [blame] | 142 | RETURN_STATUS_IF_ERROR(getAudioPort(*route->sourcePortIds.begin(), &mixPort)); |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 143 | } else { |
Mikhail Naganov | 84bcc04 | 2023-10-05 17:36:57 -0700 | [diff] [blame] | 144 | RETURN_STATUS_IF_ERROR(getAudioPort(route->sinkPortId, &mixPort)); |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 145 | } |
| 146 | audioPort->profiles = mixPort.profiles; |
Mikhail Naganov | 3b73289 | 2023-12-20 16:05:01 -0800 | [diff] [blame^] | 147 | LOG(DEBUG) << __func__ << ": populated from the mix port as: " << audioPort->toString(); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 148 | return ndk::ScopedAStatus::ok(); |
| 149 | } |
| 150 | |
| 151 | ndk::ScopedAStatus ModuleRemoteSubmix::checkAudioPatchEndpointsMatch( |
| 152 | const std::vector<AudioPortConfig*>& sources, const std::vector<AudioPortConfig*>& sinks) { |
| 153 | for (const auto& source : sources) { |
| 154 | for (const auto& sink : sinks) { |
| 155 | if (source->sampleRate != sink->sampleRate || |
| 156 | source->channelMask != sink->channelMask || source->format != sink->format) { |
| 157 | LOG(ERROR) << __func__ |
| 158 | << ": mismatch port configuration, source=" << source->toString() |
| 159 | << ", sink=" << sink->toString(); |
| 160 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | return ndk::ScopedAStatus::ok(); |
| 165 | } |
| 166 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 167 | ndk::ScopedAStatus ModuleRemoteSubmix::onMasterMuteChanged(bool __unused) { |
| 168 | LOG(DEBUG) << __func__ << ": is not supported"; |
| 169 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 170 | } |
| 171 | |
| 172 | ndk::ScopedAStatus ModuleRemoteSubmix::onMasterVolumeChanged(float __unused) { |
| 173 | LOG(DEBUG) << __func__ << ": is not supported"; |
| 174 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 175 | } |
| 176 | |
Mikhail Naganov | 1350187 | 2023-10-18 16:15:46 -0700 | [diff] [blame] | 177 | int32_t ModuleRemoteSubmix::getNominalLatencyMs(const AudioPortConfig&) { |
| 178 | // See the note on kDefaultPipePeriodCount. |
| 179 | static constexpr int32_t kMaxLatencyMs = |
| 180 | (r_submix::kDefaultPipeSizeInFrames * 1000) / r_submix::kDefaultSampleRateHz; |
| 181 | static constexpr int32_t kMinLatencyMs = kMaxLatencyMs / r_submix::kDefaultPipePeriodCount; |
Mikhail Naganov | 7b234d4 | 2023-12-05 11:28:56 -0800 | [diff] [blame] | 182 | return kMinLatencyMs; |
Mikhail Naganov | 1350187 | 2023-10-18 16:15:46 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 185 | } // namespace aidl::android::hardware::audio::core |