| 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. | 
 | 39 | class Hal2AidlMapper { | 
 | 40 |   public: | 
 | 41 |     using Cleanups = Cleanups<Hal2AidlMapper>; | 
 | 42 |  | 
 | 43 |     Hal2AidlMapper( | 
 | 44 |             const std::string& instance, | 
 | 45 |             const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module); | 
 | 46 |  | 
| Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 47 |     void addStream(const sp<StreamHalInterface>& stream, int32_t portConfigId, int32_t patchId); | 
| Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 48 |     status_t createOrUpdatePatch( | 
 | 49 |             const std::vector<::aidl::android::media::audio::common::AudioPortConfig>& sources, | 
 | 50 |             const std::vector<::aidl::android::media::audio::common::AudioPortConfig>& sinks, | 
 | 51 |             int32_t* patchId, Cleanups* cleanups); | 
 | 52 |     status_t findOrCreatePortConfig( | 
 | 53 |             const ::aidl::android::media::audio::common::AudioDevice& device, | 
 | 54 |             const ::aidl::android::media::audio::common::AudioConfig* config, | 
 | 55 |             ::aidl::android::media::audio::common::AudioPortConfig* portConfig, | 
 | 56 |             bool* created); | 
 | 57 |     status_t findOrCreatePortConfig( | 
 | 58 |             const ::aidl::android::media::audio::common::AudioConfig& config, | 
 | 59 |             const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, | 
 | 60 |             int32_t ioHandle, | 
 | 61 |             ::aidl::android::media::audio::common::AudioSource source, | 
 | 62 |             const std::set<int32_t>& destinationPortIds, | 
 | 63 |             ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created); | 
 | 64 |     status_t findOrCreatePortConfig( | 
 | 65 |         const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, | 
 | 66 |         const std::set<int32_t>& destinationPortIds, | 
 | 67 |         ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created); | 
 | 68 |     status_t findOrCreatePortConfig( | 
 | 69 |         const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, | 
 | 70 |         const std::set<int32_t>& destinationPortIds, | 
 | 71 |         ::aidl::android::media::audio::common::AudioPortConfig* portConfig, | 
 | 72 |         Cleanups* cleanups = nullptr); | 
 | 73 |     status_t findPortConfig( | 
 | 74 |             const ::aidl::android::media::audio::common::AudioDevice& device, | 
 | 75 |             ::aidl::android::media::audio::common::AudioPortConfig* portConfig); | 
 | 76 |     status_t getAudioMixPort( | 
 | 77 |             int32_t ioHandle, ::aidl::android::media::audio::common::AudioPort* port); | 
 | 78 |     status_t getAudioPortCached( | 
 | 79 |             const ::aidl::android::media::audio::common::AudioDevice& device, | 
 | 80 |             ::aidl::android::media::audio::common::AudioPort* port); | 
 | 81 |     template<typename OutputContainer, typename Func> | 
 | 82 |     status_t getAudioPorts(OutputContainer* ports, Func converter) { | 
 | 83 |         return ::aidl::android::convertContainer(mPorts, ports, | 
 | 84 |                 [&converter](const auto& pair) { return converter(pair.second); }); | 
 | 85 |     } | 
 | 86 |     template<typename OutputContainer, typename Func> | 
 | 87 |     status_t getAudioRoutes(OutputContainer* routes, Func converter) { | 
 | 88 |         return ::aidl::android::convertContainer(mRoutes, routes, converter); | 
 | 89 |     } | 
 | 90 |     status_t initialize(); | 
 | 91 |     status_t prepareToOpenStream( | 
 | 92 |         int32_t ioHandle, | 
 | 93 |         const ::aidl::android::media::audio::common::AudioDevice& device, | 
 | 94 |         const ::aidl::android::media::audio::common::AudioIoFlags& flags, | 
 | 95 |         ::aidl::android::media::audio::common::AudioSource source, | 
 | 96 |         Cleanups* cleanups, | 
 | 97 |         ::aidl::android::media::audio::common::AudioConfig* config, | 
 | 98 |         ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig, | 
 | 99 |         ::aidl::android::hardware::audio::core::AudioPatch* patch); | 
 | 100 |     status_t releaseAudioPatch(int32_t patchId); | 
 | 101 |     void resetUnusedPatchesAndPortConfigs(); | 
 | 102 |     status_t setDevicePortConnectedState( | 
 | 103 |             const ::aidl::android::media::audio::common::AudioPort& devicePort, bool connected); | 
 | 104 |  | 
 | 105 |   private: | 
 | 106 |     // IDs of ports for connected external devices, and whether they are held by streams. | 
 | 107 |     using ConnectedPorts = std::map<int32_t /*port ID*/, bool>; | 
 | 108 |     using Patches = std::map<int32_t /*patch ID*/, | 
 | 109 |             ::aidl::android::hardware::audio::core::AudioPatch>; | 
 | 110 |     using PortConfigs = std::map<int32_t /*port config ID*/, | 
 | 111 |             ::aidl::android::media::audio::common::AudioPortConfig>; | 
 | 112 |     using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>; | 
 | 113 |     using Routes = std::vector<::aidl::android::hardware::audio::core::AudioRoute>; | 
 | 114 |     // Answers the question "whether portID 'first' is reachable from portID 'second'?" | 
 | 115 |     // It's not a map because both portIDs are known. The matrix is symmetric. | 
 | 116 |     using RoutingMatrix = std::set<std::pair<int32_t, int32_t>>; | 
| Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 117 |     // There is always a port config ID set. The patch ID is set after stream | 
 | 118 |     // creation, and can be set to '-1' later if the framework happens to create | 
 | 119 |     // a patch between the same endpoints. In that case, the ownership of the patch | 
 | 120 |     // is on the framework. | 
 | 121 |     using Streams = std::map<wp<StreamHalInterface>, | 
 | 122 |             std::pair<int32_t /*port config ID*/, int32_t /*patch ID*/>>; | 
| Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 123 |  | 
 | 124 |     const std::string mInstance; | 
 | 125 |     const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule; | 
 | 126 |  | 
 | 127 |     bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device, | 
 | 128 |             const ::aidl::android::media::audio::common::AudioPort& p); | 
 | 129 |     bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device, | 
 | 130 |             const ::aidl::android::media::audio::common::AudioPortConfig& p); | 
 | 131 |     status_t createOrUpdatePortConfig( | 
 | 132 |             const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, | 
 | 133 |             PortConfigs::iterator* result, bool *created); | 
 | 134 |     void eraseConnectedPort(int32_t portId); | 
 | 135 |     status_t findOrCreatePatch( | 
 | 136 |         const std::set<int32_t>& sourcePortConfigIds, | 
 | 137 |         const std::set<int32_t>& sinkPortConfigIds, | 
 | 138 |         ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created); | 
 | 139 |     status_t findOrCreatePatch( | 
 | 140 |         const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch, | 
 | 141 |         ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created); | 
 | 142 |     Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds, | 
 | 143 |             const std::set<int32_t>& sinkPortConfigIds); | 
 | 144 |     Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device); | 
 | 145 |     Ports::iterator findPort( | 
 | 146 |             const ::aidl::android::media::audio::common::AudioConfig& config, | 
 | 147 |             const ::aidl::android::media::audio::common::AudioIoFlags& flags, | 
 | 148 |             const std::set<int32_t>& destinationPortIds); | 
 | 149 |     PortConfigs::iterator findPortConfig( | 
 | 150 |             const ::aidl::android::media::audio::common::AudioDevice& device); | 
 | 151 |     PortConfigs::iterator findPortConfig( | 
 | 152 |             const std::optional<::aidl::android::media::audio::common::AudioConfig>& config, | 
 | 153 |             const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, | 
 | 154 |             int32_t ioHandle); | 
| Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 155 |     bool isPortBeingHeld(int32_t portId); | 
 | 156 |     bool portConfigBelongsToPort(int32_t portConfigId, int32_t portId) { | 
 | 157 |         auto it = mPortConfigs.find(portConfigId); | 
 | 158 |         return it != mPortConfigs.end() && it->second.portId == portId; | 
 | 159 |     } | 
 | 160 |     status_t releaseAudioPatches(const std::set<int32_t>& patchIds); | 
 | 161 |     void resetPatch(int32_t patchId) { (void)releaseAudioPatch(patchId); } | 
| Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 162 |     void resetPortConfig(int32_t portConfigId); | 
| Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 163 |     void resetUnusedPortConfigs(); | 
 | 164 |     status_t updateAudioPort( | 
 | 165 |             int32_t portId, ::aidl::android::media::audio::common::AudioPort* port); | 
 | 166 |     status_t updateRoutes(); | 
 | 167 |  | 
 | 168 |     Ports mPorts; | 
 | 169 |     // Remote submix "template" ports (no address specified, no profiles). | 
 | 170 |     // They are excluded from `mPorts` as their presence confuses the framework code. | 
 | 171 |     std::optional<::aidl::android::media::audio::common::AudioPort> mRemoteSubmixIn; | 
 | 172 |     std::optional<::aidl::android::media::audio::common::AudioPort> mRemoteSubmixOut; | 
 | 173 |     int32_t mDefaultInputPortId = -1; | 
 | 174 |     int32_t mDefaultOutputPortId = -1; | 
 | 175 |     PortConfigs mPortConfigs; | 
 | 176 |     std::set<int32_t> mInitialPortConfigIds; | 
 | 177 |     Patches mPatches; | 
 | 178 |     Routes mRoutes; | 
 | 179 |     RoutingMatrix mRoutingMatrix; | 
 | 180 |     Streams mStreams; | 
 | 181 |     ConnectedPorts mConnectedPorts; | 
 | 182 |     std::pair<int32_t, ::aidl::android::media::audio::common::AudioPort> | 
 | 183 |             mDisconnectedPortReplacement; | 
 | 184 | }; | 
 | 185 |  | 
 | 186 | }  // namespace android |