Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [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 | #pragma once |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <map> |
| 21 | #include <set> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <aidl/android/hardware/audio/core/IModule.h> |
| 26 | #include <media/AidlConversionUtil.h> |
| 27 | |
| 28 | #include "Cleanups.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | class Hal2AidlMapper; |
| 33 | class StreamHalInterface; |
| 34 | |
| 35 | // The mapper class is needed because the framework was not yet updated to operate on AIDL-based |
| 36 | // structures directly. Mapper does the job of translating the "legacy" way of identifying ports |
| 37 | // and port configs (by device addresses and I/O handles) into AIDL IDs. Once the framework will |
| 38 | // be updated to provide these IDs directly to libaudiohal, the need for the mapper will cease. |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 39 | // |
| 40 | // Note that unlike DeviceHalInterface, which sometimes allows a method to return an error, |
| 41 | // but still consider some of the outputs to be valid (for example, in 'open{Input|Output}Stream'), |
| 42 | // 'Hal2AidlMapper' follows the Binder convention. It means that if a method returns an error, |
| 43 | // the outputs may not be initialized at all and should not be considered by the caller. |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 44 | class Hal2AidlMapper { |
| 45 | public: |
| 46 | using Cleanups = Cleanups<Hal2AidlMapper>; |
| 47 | |
| 48 | Hal2AidlMapper( |
| 49 | const std::string& instance, |
| 50 | const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module); |
| 51 | |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 52 | void addStream(const sp<StreamHalInterface>& stream, int32_t portConfigId, int32_t patchId); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 53 | status_t createOrUpdatePatch( |
| 54 | const std::vector<::aidl::android::media::audio::common::AudioPortConfig>& sources, |
| 55 | const std::vector<::aidl::android::media::audio::common::AudioPortConfig>& sinks, |
| 56 | int32_t* patchId, Cleanups* cleanups); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 57 | status_t findPortConfig( |
| 58 | const ::aidl::android::media::audio::common::AudioDevice& device, |
| 59 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig); |
| 60 | status_t getAudioMixPort( |
| 61 | int32_t ioHandle, ::aidl::android::media::audio::common::AudioPort* port); |
| 62 | status_t getAudioPortCached( |
| 63 | const ::aidl::android::media::audio::common::AudioDevice& device, |
| 64 | ::aidl::android::media::audio::common::AudioPort* port); |
| 65 | template<typename OutputContainer, typename Func> |
| 66 | status_t getAudioPorts(OutputContainer* ports, Func converter) { |
| 67 | return ::aidl::android::convertContainer(mPorts, ports, |
| 68 | [&converter](const auto& pair) { return converter(pair.second); }); |
| 69 | } |
| 70 | template<typename OutputContainer, typename Func> |
| 71 | status_t getAudioRoutes(OutputContainer* routes, Func converter) { |
| 72 | return ::aidl::android::convertContainer(mRoutes, routes, converter); |
| 73 | } |
| 74 | status_t initialize(); |
jiabin | 62750c2 | 2023-12-21 22:06:07 +0000 | [diff] [blame] | 75 | status_t prepareToDisconnectExternalDevice( |
| 76 | const ::aidl::android::media::audio::common::AudioPort& devicePort); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 77 | // If the resulting 'mixPortConfig->id' is 0, that means the stream was not created, |
| 78 | // and 'config' is a suggested config. |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 79 | status_t prepareToOpenStream( |
| 80 | int32_t ioHandle, |
| 81 | const ::aidl::android::media::audio::common::AudioDevice& device, |
| 82 | const ::aidl::android::media::audio::common::AudioIoFlags& flags, |
| 83 | ::aidl::android::media::audio::common::AudioSource source, |
| 84 | Cleanups* cleanups, |
| 85 | ::aidl::android::media::audio::common::AudioConfig* config, |
| 86 | ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig, |
| 87 | ::aidl::android::hardware::audio::core::AudioPatch* patch); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 88 | status_t setPortConfig( |
| 89 | const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, |
| 90 | const std::set<int32_t>& destinationPortIds, |
| 91 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, |
| 92 | Cleanups* cleanups = nullptr); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 93 | status_t releaseAudioPatch(int32_t patchId); |
| 94 | void resetUnusedPatchesAndPortConfigs(); |
| 95 | status_t setDevicePortConnectedState( |
| 96 | const ::aidl::android::media::audio::common::AudioPort& devicePort, bool connected); |
| 97 | |
| 98 | private: |
| 99 | // IDs of ports for connected external devices, and whether they are held by streams. |
| 100 | using ConnectedPorts = std::map<int32_t /*port ID*/, bool>; |
| 101 | using Patches = std::map<int32_t /*patch ID*/, |
| 102 | ::aidl::android::hardware::audio::core::AudioPatch>; |
| 103 | using PortConfigs = std::map<int32_t /*port config ID*/, |
| 104 | ::aidl::android::media::audio::common::AudioPortConfig>; |
| 105 | using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>; |
| 106 | using Routes = std::vector<::aidl::android::hardware::audio::core::AudioRoute>; |
| 107 | // Answers the question "whether portID 'first' is reachable from portID 'second'?" |
| 108 | // It's not a map because both portIDs are known. The matrix is symmetric. |
| 109 | using RoutingMatrix = std::set<std::pair<int32_t, int32_t>>; |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 110 | // There is always a port config ID set. The patch ID is set after stream |
| 111 | // creation, and can be set to '-1' later if the framework happens to create |
| 112 | // a patch between the same endpoints. In that case, the ownership of the patch |
| 113 | // is on the framework. |
| 114 | using Streams = std::map<wp<StreamHalInterface>, |
| 115 | std::pair<int32_t /*port config ID*/, int32_t /*patch ID*/>>; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 116 | |
| 117 | const std::string mInstance; |
| 118 | const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule; |
| 119 | |
| 120 | bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device, |
| 121 | const ::aidl::android::media::audio::common::AudioPort& p); |
| 122 | bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device, |
| 123 | const ::aidl::android::media::audio::common::AudioPortConfig& p); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 124 | // If the 'result->id' is 0, that means, the config was not created/updated, |
| 125 | // and the 'result' is a suggestion from the HAL. |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 126 | status_t createOrUpdatePortConfig( |
| 127 | const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 128 | ::aidl::android::media::audio::common::AudioPortConfig* result, bool *created); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 129 | void eraseConnectedPort(int32_t portId); |
| 130 | status_t findOrCreatePatch( |
| 131 | const std::set<int32_t>& sourcePortConfigIds, |
| 132 | const std::set<int32_t>& sinkPortConfigIds, |
| 133 | ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created); |
| 134 | status_t findOrCreatePatch( |
| 135 | const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch, |
| 136 | ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 137 | status_t findOrCreateDevicePortConfig( |
| 138 | const ::aidl::android::media::audio::common::AudioDevice& device, |
| 139 | const ::aidl::android::media::audio::common::AudioConfig* config, |
| 140 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, |
| 141 | bool* created); |
| 142 | // If the resulting 'portConfig->id' is 0, that means the config was not created, |
| 143 | // and 'portConfig' is a suggested config. |
| 144 | status_t findOrCreateMixPortConfig( |
| 145 | const ::aidl::android::media::audio::common::AudioConfig& config, |
| 146 | const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, |
| 147 | int32_t ioHandle, |
| 148 | ::aidl::android::media::audio::common::AudioSource source, |
| 149 | const std::set<int32_t>& destinationPortIds, |
| 150 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created); |
| 151 | status_t findOrCreatePortConfig( |
| 152 | const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, |
| 153 | const std::set<int32_t>& destinationPortIds, |
| 154 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 155 | Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds, |
| 156 | const std::set<int32_t>& sinkPortConfigIds); |
| 157 | Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device); |
| 158 | Ports::iterator findPort( |
| 159 | const ::aidl::android::media::audio::common::AudioConfig& config, |
| 160 | const ::aidl::android::media::audio::common::AudioIoFlags& flags, |
| 161 | const std::set<int32_t>& destinationPortIds); |
| 162 | PortConfigs::iterator findPortConfig( |
| 163 | const ::aidl::android::media::audio::common::AudioDevice& device); |
| 164 | PortConfigs::iterator findPortConfig( |
| 165 | const std::optional<::aidl::android::media::audio::common::AudioConfig>& config, |
| 166 | const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, |
| 167 | int32_t ioHandle); |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 168 | bool isPortBeingHeld(int32_t portId); |
Mikhail Naganov | 38220af | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 169 | status_t prepareToOpenStreamHelper( |
| 170 | int32_t ioHandle, int32_t devicePortId, int32_t devicePortConfigId, |
| 171 | const ::aidl::android::media::audio::common::AudioIoFlags& flags, |
| 172 | ::aidl::android::media::audio::common::AudioSource source, |
| 173 | const ::aidl::android::media::audio::common::AudioConfig& initialConfig, |
| 174 | Cleanups* cleanups, ::aidl::android::media::audio::common::AudioConfig* config, |
| 175 | ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig, |
| 176 | ::aidl::android::hardware::audio::core::AudioPatch* patch); |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 177 | bool portConfigBelongsToPort(int32_t portConfigId, int32_t portId) { |
| 178 | auto it = mPortConfigs.find(portConfigId); |
| 179 | return it != mPortConfigs.end() && it->second.portId == portId; |
| 180 | } |
| 181 | status_t releaseAudioPatches(const std::set<int32_t>& patchIds); |
| 182 | void resetPatch(int32_t patchId) { (void)releaseAudioPatch(patchId); } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 183 | void resetPortConfig(int32_t portConfigId); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 184 | void resetUnusedPortConfigs(); |
| 185 | status_t updateAudioPort( |
| 186 | int32_t portId, ::aidl::android::media::audio::common::AudioPort* port); |
| 187 | status_t updateRoutes(); |
| 188 | |
| 189 | Ports mPorts; |
| 190 | // Remote submix "template" ports (no address specified, no profiles). |
| 191 | // They are excluded from `mPorts` as their presence confuses the framework code. |
| 192 | std::optional<::aidl::android::media::audio::common::AudioPort> mRemoteSubmixIn; |
| 193 | std::optional<::aidl::android::media::audio::common::AudioPort> mRemoteSubmixOut; |
| 194 | int32_t mDefaultInputPortId = -1; |
| 195 | int32_t mDefaultOutputPortId = -1; |
| 196 | PortConfigs mPortConfigs; |
| 197 | std::set<int32_t> mInitialPortConfigIds; |
| 198 | Patches mPatches; |
| 199 | Routes mRoutes; |
| 200 | RoutingMatrix mRoutingMatrix; |
| 201 | Streams mStreams; |
| 202 | ConnectedPorts mConnectedPorts; |
| 203 | std::pair<int32_t, ::aidl::android::media::audio::common::AudioPort> |
| 204 | mDisconnectedPortReplacement; |
| 205 | }; |
| 206 | |
| 207 | } // namespace android |