blob: f302c23cd3cecedfd83d53d3e9cb88962534a845 [file] [log] [blame]
Mikhail Naganovac9d4e72023-10-23 12:00:09 -07001/*
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
30namespace android {
31
32class Hal2AidlMapper;
33class 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 Naganovca92a5c2023-12-07 14:00:48 -080039//
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 Naganovac9d4e72023-10-23 12:00:09 -070044class 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 Naganova317a802024-03-15 18:03:10 +000052 void addStream(const sp<StreamHalInterface>& stream, int32_t mixPortConfigId, int32_t patchId);
Mikhail Naganovac9d4e72023-10-23 12:00:09 -070053 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 Naganovac9d4e72023-10-23 12:00:09 -070057 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();
jiabin62750c22023-12-21 22:06:07 +000075 status_t prepareToDisconnectExternalDevice(
76 const ::aidl::android::media::audio::common::AudioPort& devicePort);
Mikhail Naganovca92a5c2023-12-07 14:00:48 -080077 // If the resulting 'mixPortConfig->id' is 0, that means the stream was not created,
78 // and 'config' is a suggested config.
Mikhail Naganovac9d4e72023-10-23 12:00:09 -070079 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 Naganovca92a5c2023-12-07 14:00:48 -080088 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 Naganovac9d4e72023-10-23 12:00:09 -070093 status_t releaseAudioPatch(int32_t patchId);
Mikhail Naganova317a802024-03-15 18:03:10 +000094 void resetUnusedPatchesAndPortConfigs();
Mikhail Naganovac9d4e72023-10-23 12:00:09 -070095 status_t setDevicePortConnectedState(
96 const ::aidl::android::media::audio::common::AudioPort& devicePort, bool connected);
97
Mikhail Naganova317a802024-03-15 18:03:10 +000098 // Methods to work with FwkPatches.
99 void eraseFwkPatch(int32_t fwkPatchId) { mFwkPatches.erase(fwkPatchId); }
100 int32_t findFwkPatch(int32_t fwkPatchId) {
101 const auto it = mFwkPatches.find(fwkPatchId);
102 return it != mFwkPatches.end() ? it->second : 0;
103 }
104 void updateFwkPatch(int32_t fwkPatchId, int32_t halPatchId) {
105 mFwkPatches[fwkPatchId] = halPatchId;
106 }
107
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700108 private:
Mikhail Naganova317a802024-03-15 18:03:10 +0000109 // 'FwkPatches' is used to store patches that diverge from the framework's state.
110 // Uses framework patch ID (aka audio_patch_handle_t) values for indexing.
111 // When the 'key == value', that means Hal2AidlMapper has removed this patch, and it is absent
112 // from 'mPatches', but it still "exists" for the framework. It will either remove it or
113 // re-patch. If the framework re-patches, it will continue to use the same patch handle,
114 // but the HAL will use the new one (since the old patch was reset), thus 'key != value'
115 // for such patches. Since they "exist" both for the framework and the HAL, 'mPatches'
116 // contains their data under HAL patch ID ('value' of 'FwkPatches').
117 // To avoid confusion, all patchIDs used by Hal2AidlMapper are HAL IDs. Mapping between
118 // framework patch IDs and HAL patch IDs is done by DeviceHalAidl.
119 using FwkPatches = std::map<int32_t /*audio_patch_handle_t*/, int32_t /*patch ID*/>;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700120 using Patches = std::map<int32_t /*patch ID*/,
121 ::aidl::android::hardware::audio::core::AudioPatch>;
122 using PortConfigs = std::map<int32_t /*port config ID*/,
123 ::aidl::android::media::audio::common::AudioPortConfig>;
124 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
125 using Routes = std::vector<::aidl::android::hardware::audio::core::AudioRoute>;
126 // Answers the question "whether portID 'first' is reachable from portID 'second'?"
127 // It's not a map because both portIDs are known. The matrix is symmetric.
128 using RoutingMatrix = std::set<std::pair<int32_t, int32_t>>;
Mikhail Naganova317a802024-03-15 18:03:10 +0000129 // There is always a mix port config ID set. The patch ID is set after stream
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800130 // creation, and can be set to '-1' later if the framework happens to create
131 // a patch between the same endpoints. In that case, the ownership of the patch
132 // is on the framework.
133 using Streams = std::map<wp<StreamHalInterface>,
Mikhail Naganova317a802024-03-15 18:03:10 +0000134 std::pair<int32_t /*mix port config ID*/, int32_t /*patch ID*/>>;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700135
136 const std::string mInstance;
137 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
138
139 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
140 const ::aidl::android::media::audio::common::AudioPort& p);
141 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
142 const ::aidl::android::media::audio::common::AudioPortConfig& p);
Mikhail Naganovca92a5c2023-12-07 14:00:48 -0800143 // If the 'result->id' is 0, that means, the config was not created/updated,
144 // and the 'result' is a suggestion from the HAL.
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700145 status_t createOrUpdatePortConfig(
146 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganovca92a5c2023-12-07 14:00:48 -0800147 ::aidl::android::media::audio::common::AudioPortConfig* result, bool *created);
David Lifef5d0c2024-01-11 16:57:17 +0000148 status_t createOrUpdatePortConfigRetry(
149 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
150 ::aidl::android::media::audio::common::AudioPortConfig* result, bool *created);
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700151 void eraseConnectedPort(int32_t portId);
152 status_t findOrCreatePatch(
153 const std::set<int32_t>& sourcePortConfigIds,
154 const std::set<int32_t>& sinkPortConfigIds,
155 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
156 status_t findOrCreatePatch(
157 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
158 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
Mikhail Naganovca92a5c2023-12-07 14:00:48 -0800159 status_t findOrCreateDevicePortConfig(
160 const ::aidl::android::media::audio::common::AudioDevice& device,
161 const ::aidl::android::media::audio::common::AudioConfig* config,
162 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
163 bool* created);
164 // If the resulting 'portConfig->id' is 0, that means the config was not created,
165 // and 'portConfig' is a suggested config.
166 status_t findOrCreateMixPortConfig(
167 const ::aidl::android::media::audio::common::AudioConfig& config,
168 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
169 int32_t ioHandle,
170 ::aidl::android::media::audio::common::AudioSource source,
171 const std::set<int32_t>& destinationPortIds,
172 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
173 status_t findOrCreatePortConfig(
174 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
175 const std::set<int32_t>& destinationPortIds,
176 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700177 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
178 const std::set<int32_t>& sinkPortConfigIds);
179 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
180 Ports::iterator findPort(
181 const ::aidl::android::media::audio::common::AudioConfig& config,
182 const ::aidl::android::media::audio::common::AudioIoFlags& flags,
183 const std::set<int32_t>& destinationPortIds);
184 PortConfigs::iterator findPortConfig(
185 const ::aidl::android::media::audio::common::AudioDevice& device);
186 PortConfigs::iterator findPortConfig(
187 const std::optional<::aidl::android::media::audio::common::AudioConfig>& config,
188 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
189 int32_t ioHandle);
Mikhail Naganova317a802024-03-15 18:03:10 +0000190 std::set<int32_t> getPatchIdsByPortId(int32_t portId);
Mikhail Naganov38220af2023-12-07 14:00:48 -0800191 status_t prepareToOpenStreamHelper(
192 int32_t ioHandle, int32_t devicePortId, int32_t devicePortConfigId,
193 const ::aidl::android::media::audio::common::AudioIoFlags& flags,
194 ::aidl::android::media::audio::common::AudioSource source,
195 const ::aidl::android::media::audio::common::AudioConfig& initialConfig,
196 Cleanups* cleanups, ::aidl::android::media::audio::common::AudioConfig* config,
197 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
198 ::aidl::android::hardware::audio::core::AudioPatch* patch);
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800199 bool portConfigBelongsToPort(int32_t portConfigId, int32_t portId) {
200 auto it = mPortConfigs.find(portConfigId);
201 return it != mPortConfigs.end() && it->second.portId == portId;
202 }
Mikhail Naganova317a802024-03-15 18:03:10 +0000203 status_t releaseAudioPatch(Patches::iterator it);
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800204 status_t releaseAudioPatches(const std::set<int32_t>& patchIds);
205 void resetPatch(int32_t patchId) { (void)releaseAudioPatch(patchId); }
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700206 void resetPortConfig(int32_t portConfigId);
Mikhail Naganova317a802024-03-15 18:03:10 +0000207 void resetUnusedPortConfigs();
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700208 status_t updateAudioPort(
209 int32_t portId, ::aidl::android::media::audio::common::AudioPort* port);
210 status_t updateRoutes();
jiabin255ff7f2024-01-11 00:24:47 +0000211 void updateDynamicMixPorts();
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700212
213 Ports mPorts;
214 // Remote submix "template" ports (no address specified, no profiles).
215 // They are excluded from `mPorts` as their presence confuses the framework code.
216 std::optional<::aidl::android::media::audio::common::AudioPort> mRemoteSubmixIn;
217 std::optional<::aidl::android::media::audio::common::AudioPort> mRemoteSubmixOut;
218 int32_t mDefaultInputPortId = -1;
219 int32_t mDefaultOutputPortId = -1;
Mikhail Naganova317a802024-03-15 18:03:10 +0000220 FwkPatches mFwkPatches;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700221 PortConfigs mPortConfigs;
222 std::set<int32_t> mInitialPortConfigIds;
223 Patches mPatches;
224 Routes mRoutes;
225 RoutingMatrix mRoutingMatrix;
226 Streams mStreams;
Mikhail Naganova317a802024-03-15 18:03:10 +0000227 std::set<int32_t> mConnectedPorts;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700228 std::pair<int32_t, ::aidl::android::media::audio::common::AudioPort>
229 mDisconnectedPortReplacement;
jiabin255ff7f2024-01-11 00:24:47 +0000230 std::set<int32_t> mDynamicMixPortIds;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700231};
232
233} // namespace android