Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #pragma once |
| 18 | |
| 19 | #include <optional> |
| 20 | #include <set> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 24 | #include <aidl/android/hardware/audio/core/AudioRoute.h> |
| 25 | #include <aidl/android/hardware/audio/core/IModule.h> |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 26 | #include <aidl/android/media/audio/common/AudioOffloadInfo.h> |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 27 | #include <aidl/android/media/audio/common/AudioPort.h> |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 28 | |
| 29 | class ModuleConfig { |
| 30 | public: |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 31 | using SrcSinkPair = std::pair<aidl::android::media::audio::common::AudioPortConfig, |
| 32 | aidl::android::media::audio::common::AudioPortConfig>; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 33 | using SrcSinkGroup = |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 34 | std::pair<aidl::android::hardware::audio::core::AudioRoute, std::vector<SrcSinkPair>>; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 35 | |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 36 | static std::optional<aidl::android::media::audio::common::AudioOffloadInfo> |
| 37 | generateOffloadInfoIfNeeded( |
| 38 | const aidl::android::media::audio::common::AudioPortConfig& portConfig); |
| 39 | |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 40 | explicit ModuleConfig(aidl::android::hardware::audio::core::IModule* module); |
| 41 | const ndk::ScopedAStatus& getStatus() const { return mStatus; } |
| 42 | std::string getError() const { return mStatus.getMessage(); } |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 43 | |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 44 | std::vector<aidl::android::media::audio::common::AudioPort> getAttachedDevicePorts() const; |
| 45 | std::vector<aidl::android::media::audio::common::AudioPort> getExternalDevicePorts() const; |
| 46 | std::vector<aidl::android::media::audio::common::AudioPort> getInputMixPorts() const; |
| 47 | std::vector<aidl::android::media::audio::common::AudioPort> getOutputMixPorts() const; |
| 48 | std::vector<aidl::android::media::audio::common::AudioPort> getMixPorts(bool isInput) const { |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 49 | return isInput ? getInputMixPorts() : getOutputMixPorts(); |
| 50 | } |
| 51 | |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 52 | std::vector<aidl::android::media::audio::common::AudioPort> getAttachedDevicesPortsForMixPort( |
| 53 | bool isInput, const aidl::android::media::audio::common::AudioPort& mixPort) const { |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 54 | return isInput ? getAttachedSourceDevicesPortsForMixPort(mixPort) |
| 55 | : getAttachedSinkDevicesPortsForMixPort(mixPort); |
| 56 | } |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 57 | std::vector<aidl::android::media::audio::common::AudioPort> |
| 58 | getAttachedSinkDevicesPortsForMixPort( |
| 59 | const aidl::android::media::audio::common::AudioPort& mixPort) const; |
| 60 | std::vector<aidl::android::media::audio::common::AudioPort> |
| 61 | getAttachedSourceDevicesPortsForMixPort( |
| 62 | const aidl::android::media::audio::common::AudioPort& mixPort) const; |
| 63 | std::optional<aidl::android::media::audio::common::AudioPort> |
| 64 | getSourceMixPortForAttachedDevice() const; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 65 | |
| 66 | std::optional<SrcSinkPair> getNonRoutableSrcSinkPair(bool isInput) const; |
| 67 | std::optional<SrcSinkPair> getRoutableSrcSinkPair(bool isInput) const; |
| 68 | std::vector<SrcSinkGroup> getRoutableSrcSinkGroups(bool isInput) const; |
| 69 | |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 70 | std::vector<aidl::android::media::audio::common::AudioPortConfig> |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 71 | getPortConfigsForAttachedDevicePorts() const { |
| 72 | return generateAudioDevicePortConfigs(getAttachedDevicePorts(), false); |
| 73 | } |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 74 | std::vector<aidl::android::media::audio::common::AudioPortConfig> getPortConfigsForMixPorts() |
| 75 | const { |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 76 | auto inputs = generateAudioMixPortConfigs(getInputMixPorts(), true, false); |
| 77 | auto outputs = generateAudioMixPortConfigs(getOutputMixPorts(), false, false); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 78 | inputs.insert(inputs.end(), outputs.begin(), outputs.end()); |
| 79 | return inputs; |
| 80 | } |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 81 | std::vector<aidl::android::media::audio::common::AudioPortConfig> getPortConfigsForMixPorts( |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 82 | bool isInput) const { |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 83 | return generateAudioMixPortConfigs(getMixPorts(isInput), isInput, false); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 84 | } |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 85 | std::vector<aidl::android::media::audio::common::AudioPortConfig> getPortConfigsForMixPorts( |
| 86 | bool isInput, const aidl::android::media::audio::common::AudioPort& port) const { |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 87 | return generateAudioMixPortConfigs({port}, isInput, false); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 88 | } |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 89 | std::optional<aidl::android::media::audio::common::AudioPortConfig> getSingleConfigForMixPort( |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 90 | bool isInput) const { |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 91 | const auto config = generateAudioMixPortConfigs(getMixPorts(isInput), isInput, true); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 92 | if (!config.empty()) { |
| 93 | return *config.begin(); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 94 | } |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 95 | return {}; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 96 | } |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 97 | std::optional<aidl::android::media::audio::common::AudioPortConfig> getSingleConfigForMixPort( |
| 98 | bool isInput, const aidl::android::media::audio::common::AudioPort& port) const { |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 99 | const auto config = generateAudioMixPortConfigs({port}, isInput, true); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 100 | if (!config.empty()) { |
| 101 | return *config.begin(); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 102 | } |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 103 | return {}; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 106 | std::vector<aidl::android::media::audio::common::AudioPortConfig> getPortConfigsForDevicePort( |
| 107 | const aidl::android::media::audio::common::AudioPort& port) const { |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 108 | return generateAudioDevicePortConfigs({port}, false); |
| 109 | } |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 110 | aidl::android::media::audio::common::AudioPortConfig getSingleConfigForDevicePort( |
| 111 | const aidl::android::media::audio::common::AudioPort& port) const { |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 112 | const auto config = generateAudioDevicePortConfigs({port}, true); |
| 113 | return *config.begin(); |
| 114 | } |
| 115 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 116 | std::string toString() const; |
| 117 | |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 118 | private: |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 119 | std::vector<aidl::android::media::audio::common::AudioPortConfig> generateAudioMixPortConfigs( |
| 120 | const std::vector<aidl::android::media::audio::common::AudioPort>& ports, bool isInput, |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 121 | bool singleProfile) const; |
| 122 | |
| 123 | // Unlike MixPorts, the generator for DevicePorts always returns a non-empty |
| 124 | // vector for a non-empty input port list. If there are no profiles in the |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 125 | // port, its initial configs are looked up, if there are none, |
| 126 | // then an empty config is used, assuming further negotiation via setAudioPortConfig. |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 127 | std::vector<aidl::android::media::audio::common::AudioPortConfig> |
| 128 | generateAudioDevicePortConfigs( |
| 129 | const std::vector<aidl::android::media::audio::common::AudioPort>& ports, |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 130 | bool singleProfile) const; |
| 131 | |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 132 | ndk::ScopedAStatus mStatus = ndk::ScopedAStatus::ok(); |
| 133 | std::vector<aidl::android::media::audio::common::AudioPort> mPorts; |
| 134 | std::vector<aidl::android::media::audio::common::AudioPortConfig> mInitialConfigs; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 135 | std::set<int32_t> mAttachedSinkDevicePorts; |
| 136 | std::set<int32_t> mAttachedSourceDevicePorts; |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 137 | std::set<int32_t> mExternalDevicePorts; |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 138 | std::vector<aidl::android::hardware::audio::core::AudioRoute> mRoutes; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 139 | }; |