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