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; |
| 30 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 31 | using aidl::android::media::audio::common::AudioPort; |
| 32 | using aidl::android::media::audio::common::AudioPortConfig; |
| 33 | using aidl::android::media::audio::common::MicrophoneInfo; |
| 34 | |
| 35 | namespace aidl::android::hardware::audio::core { |
| 36 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 37 | ndk::ScopedAStatus ModuleRemoteSubmix::getMicMute(bool* _aidl_return __unused) { |
| 38 | LOG(DEBUG) << __func__ << ": is not supported"; |
| 39 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 40 | } |
| 41 | |
| 42 | ndk::ScopedAStatus ModuleRemoteSubmix::setMicMute(bool in_mute __unused) { |
| 43 | LOG(DEBUG) << __func__ << ": is not supported"; |
| 44 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 45 | } |
| 46 | |
| 47 | ndk::ScopedAStatus ModuleRemoteSubmix::createInputStream( |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 48 | StreamContext&& context, const SinkMetadata& sinkMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 49 | const std::vector<MicrophoneInfo>& microphones, std::shared_ptr<StreamIn>* result) { |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 50 | return createStreamInstance<StreamInRemoteSubmix>(result, std::move(context), sinkMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 51 | microphones); |
| 52 | } |
| 53 | |
| 54 | ndk::ScopedAStatus ModuleRemoteSubmix::createOutputStream( |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 55 | StreamContext&& context, const SourceMetadata& sourceMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 56 | const std::optional<AudioOffloadInfo>& offloadInfo, std::shared_ptr<StreamOut>* result) { |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 57 | return createStreamInstance<StreamOutRemoteSubmix>(result, std::move(context), sourceMetadata, |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 58 | offloadInfo); |
| 59 | } |
| 60 | |
| 61 | ndk::ScopedAStatus ModuleRemoteSubmix::populateConnectedDevicePort(AudioPort* audioPort) { |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 62 | // Find the corresponding mix port and copy its profiles. |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 63 | // 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] | 64 | std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(audioPort->id); |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 65 | if (routes.empty()) { |
| 66 | LOG(ERROR) << __func__ << ": no routes found for the port " << audioPort->toString(); |
| 67 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 68 | } |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 69 | const auto& route = *routes.begin(); |
| 70 | AudioPort mixPort; |
Mikhail Naganov | 84bcc04 | 2023-10-05 17:36:57 -0700 | [diff] [blame] | 71 | if (route->sinkPortId == audioPort->id) { |
| 72 | if (route->sourcePortIds.empty()) { |
| 73 | LOG(ERROR) << __func__ << ": invalid route " << route->toString(); |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 74 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 75 | } |
Mikhail Naganov | 84bcc04 | 2023-10-05 17:36:57 -0700 | [diff] [blame] | 76 | RETURN_STATUS_IF_ERROR(getAudioPort(*route->sourcePortIds.begin(), &mixPort)); |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 77 | } else { |
Mikhail Naganov | 84bcc04 | 2023-10-05 17:36:57 -0700 | [diff] [blame] | 78 | RETURN_STATUS_IF_ERROR(getAudioPort(route->sinkPortId, &mixPort)); |
David Li | b089c0c | 2023-08-10 12:47:44 +0800 | [diff] [blame] | 79 | } |
| 80 | audioPort->profiles = mixPort.profiles; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 81 | return ndk::ScopedAStatus::ok(); |
| 82 | } |
| 83 | |
| 84 | ndk::ScopedAStatus ModuleRemoteSubmix::checkAudioPatchEndpointsMatch( |
| 85 | const std::vector<AudioPortConfig*>& sources, const std::vector<AudioPortConfig*>& sinks) { |
| 86 | for (const auto& source : sources) { |
| 87 | for (const auto& sink : sinks) { |
| 88 | if (source->sampleRate != sink->sampleRate || |
| 89 | source->channelMask != sink->channelMask || source->format != sink->format) { |
| 90 | LOG(ERROR) << __func__ |
| 91 | << ": mismatch port configuration, source=" << source->toString() |
| 92 | << ", sink=" << sink->toString(); |
| 93 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | return ndk::ScopedAStatus::ok(); |
| 98 | } |
| 99 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 100 | ndk::ScopedAStatus ModuleRemoteSubmix::onMasterMuteChanged(bool __unused) { |
| 101 | LOG(DEBUG) << __func__ << ": is not supported"; |
| 102 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 103 | } |
| 104 | |
| 105 | ndk::ScopedAStatus ModuleRemoteSubmix::onMasterVolumeChanged(float __unused) { |
| 106 | LOG(DEBUG) << __func__ << ": is not supported"; |
| 107 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 108 | } |
| 109 | |
Mikhail Naganov | 1350187 | 2023-10-18 16:15:46 -0700 | [diff] [blame^] | 110 | int32_t ModuleRemoteSubmix::getNominalLatencyMs(const AudioPortConfig&) { |
| 111 | // See the note on kDefaultPipePeriodCount. |
| 112 | static constexpr int32_t kMaxLatencyMs = |
| 113 | (r_submix::kDefaultPipeSizeInFrames * 1000) / r_submix::kDefaultSampleRateHz; |
| 114 | static constexpr int32_t kMinLatencyMs = kMaxLatencyMs / r_submix::kDefaultPipePeriodCount; |
| 115 | return (kMaxLatencyMs + kMinLatencyMs) / 2; |
| 116 | } |
| 117 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 118 | } // namespace aidl::android::hardware::audio::core |