blob: e29ae7904f2d2c36207cad9708b3b446e9583ede [file] [log] [blame]
Shunkai Yao51202502022-12-12 06:11:46 +00001/*
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
Mikhail Naganov5b1eed12023-01-25 11:29:11 -080019#include <map>
20#include <set>
21#include <vector>
22
Mikhail Naganov31d46652023-01-10 18:29:25 +000023#include <aidl/android/hardware/audio/core/BpModule.h>
Mikhail Naganovdfd594e2023-02-08 16:59:41 -080024#include <android-base/thread_annotations.h>
Shunkai Yao51202502022-12-12 06:11:46 +000025#include <media/audiohal/DeviceHalInterface.h>
26#include <media/audiohal/EffectHalInterface.h>
Mikhail Naganove93a0862023-03-15 17:06:59 -070027#include <media/audiohal/StreamHalInterface.h>
Shunkai Yao51202502022-12-12 06:11:46 +000028
Mikhail Naganov31d46652023-01-10 18:29:25 +000029#include "ConversionHelperAidl.h"
Shunkai Yao51202502022-12-12 06:11:46 +000030
31namespace android {
32
Mikhail Naganovdfd594e2023-02-08 16:59:41 -080033class StreamOutHalInterfaceCallback;
34class StreamOutHalInterfaceEventCallback;
35class StreamOutHalInterfaceLatencyModeCallback;
36
37// The role of the broker is to connect AIDL callback interface implementations
38// with StreamOut callback implementations. Since AIDL requires all callbacks
39// to be provided upfront, while libaudiohal interfaces allow late registration,
40// there is a need to coordinate the matching process.
41class CallbackBroker : public virtual RefBase {
42 public:
43 virtual ~CallbackBroker() = default;
44 // The cookie is always the stream instance pointer. We don't use weak pointers to avoid extra
45 // costs on reference counting. The stream cleans up related entries on destruction. Since
46 // access to the callbacks map is synchronized, the possibility for pointer aliasing due to
47 // allocation of a new stream at the address of previously deleted stream is avoided.
48 virtual void clearCallbacks(void* cookie) = 0;
49 virtual sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) = 0;
50 virtual void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>&) = 0;
51 virtual sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) = 0;
52 virtual void setStreamOutEventCallback(void* cookie,
53 const sp<StreamOutHalInterfaceEventCallback>&) = 0;
54 virtual sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
55 void* cookie) = 0;
56 virtual void setStreamOutLatencyModeCallback(
57 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>&) = 0;
58};
59
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080060class MicrophoneInfoProvider : public virtual RefBase {
61 public:
62 using Info = std::vector<::aidl::android::media::audio::common::MicrophoneInfo>;
63 virtual ~MicrophoneInfoProvider() = default;
64 // Returns a nullptr if the HAL does not support microphone info retrieval.
65 virtual Info const* getMicrophoneInfo() = 0;
66};
67
Mikhail Naganovdfd594e2023-02-08 16:59:41 -080068class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl,
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080069 public CallbackBroker, public MicrophoneInfoProvider {
Shunkai Yao51202502022-12-12 06:11:46 +000070 public:
Mikhail Naganovf83b9742023-04-24 13:06:04 -070071 status_t getAudioPorts(std::vector<media::audio::common::AudioPort> *ports) override;
72
73 status_t getAudioRoutes(std::vector<media::AudioRoute> *routes) override;
74
Mikhail Naganov1fba38c2023-05-03 17:45:36 -070075 status_t getSupportedModes(std::vector<media::audio::common::AudioMode> *modes) override;
76
Shunkai Yao51202502022-12-12 06:11:46 +000077 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
78 status_t getSupportedDevices(uint32_t *devices) override;
79
80 // Check to see if the audio hardware interface has been initialized.
81 status_t initCheck() override;
82
83 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
84 status_t setVoiceVolume(float volume) override;
85
86 // Set the audio volume for all audio activities other than voice call.
87 status_t setMasterVolume(float volume) override;
88
89 // Get the current master volume value for the HAL.
90 status_t getMasterVolume(float *volume) override;
91
92 // Called when the audio mode changes.
93 status_t setMode(audio_mode_t mode) override;
94
95 // Muting control.
96 status_t setMicMute(bool state) override;
97
98 status_t getMicMute(bool* state) override;
99
100 status_t setMasterMute(bool state) override;
101
102 status_t getMasterMute(bool *state) override;
103
104 // Set global audio parameters.
105 status_t setParameters(const String8& kvPairs) override;
106
107 // Get global audio parameters.
108 status_t getParameters(const String8& keys, String8 *values) override;
109
110 // Returns audio input buffer size according to parameters passed.
111 status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
112
113 // Creates and opens the audio hardware output stream. The stream is closed
114 // by releasing all references to the returned object.
115 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
116 audio_output_flags_t flags, struct audio_config* config,
117 const char* address, sp<StreamOutHalInterface>* outStream) override;
118
119 // Creates and opens the audio hardware input stream. The stream is closed
120 // by releasing all references to the returned object.
121 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
122 struct audio_config* config, audio_input_flags_t flags,
123 const char* address, audio_source_t source,
124 audio_devices_t outputDevice, const char* outputDeviceAddress,
125 sp<StreamInHalInterface>* inStream) override;
126
127 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
128 status_t supportsAudioPatches(bool* supportsPatches) override;
129
130 // Creates an audio patch between several source and sink ports.
131 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
132 unsigned int num_sinks, const struct audio_port_config* sinks,
133 audio_patch_handle_t* patch) override;
134
135 // Releases an audio patch.
136 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
137
Mikhail Naganov31d46652023-01-10 18:29:25 +0000138 // Fills the list of supported attributes for a given audio port.
139 status_t getAudioPort(struct audio_port* port) override;
140
141 // Fills the list of supported attributes for a given audio port.
142 status_t getAudioPort(struct audio_port_v7 *port) override;
143
Shunkai Yao51202502022-12-12 06:11:46 +0000144 // Set audio port configuration.
145 status_t setAudioPortConfig(const struct audio_port_config* config) override;
146
147 // List microphones
Mikhail Naganove93a0862023-03-15 17:06:59 -0700148 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000149
150 status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
151
152 status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
153
154 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
155 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
156 __unused) override;
157
158 int32_t getAAudioMixerBurstCount() override;
159
160 int32_t getAAudioHardwareBurstMinUsec() override;
161
162 error::Result<audio_hw_sync_t> getHwAvSync() override;
163
Shunkai Yao51202502022-12-12 06:11:46 +0000164 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
165
jiabin872de702023-04-27 22:04:31 +0000166 status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override;
167
Mikhail Naganove93a0862023-03-15 17:06:59 -0700168 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override;
169
170 status_t setSimulateDeviceConnections(bool enabled) override;
171
172 status_t dump(int __unused, const Vector<String16>& __unused) override;
173
Shunkai Yao51202502022-12-12 06:11:46 +0000174 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000175 friend class sp<DeviceHalAidl>;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800176
177 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
178 wp<StreamOutHalInterfaceCallback> out;
179 wp<StreamOutHalInterfaceEventCallback> event;
180 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
181 };
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800182 struct Microphones {
183 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
184 Status status = Status::UNKNOWN;
185 MicrophoneInfoProvider::Info info;
186 };
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800187 using Patches = std::map<int32_t /*patch ID*/,
188 ::aidl::android::hardware::audio::core::AudioPatch>;
189 using PortConfigs = std::map<int32_t /*port config ID*/,
190 ::aidl::android::media::audio::common::AudioPortConfig>;
191 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700192 using Routes = std::vector<::aidl::android::hardware::audio::core::AudioRoute>;
Mikhail Naganov289468a2023-03-29 10:06:15 -0700193 // Answers the question "whether portID 'first' is reachable from portID 'second'?"
194 // It's not a map because both portIDs are known. The matrix is symmetric.
195 using RoutingMatrix = std::set<std::pair<int32_t, int32_t>>;
Mikhail Naganove93a0862023-03-15 17:06:59 -0700196 using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800197 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000198
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800199 // Must not be constructed directly by clients.
200 DeviceHalAidl(
201 const std::string& instance,
Mikhail Naganov1fba38c2023-05-03 17:45:36 -0700202 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module);
Shunkai Yao51202502022-12-12 06:11:46 +0000203
Mikhail Naganov31d46652023-01-10 18:29:25 +0000204 ~DeviceHalAidl() override = default;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800205
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800206 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
207 const ::aidl::android::media::audio::common::AudioPort& p);
208 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
209 const ::aidl::android::media::audio::common::AudioPortConfig& p);
David Lia8675d42023-03-30 21:08:06 +0800210 status_t createOrUpdatePortConfig(
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800211 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
David Lia8675d42023-03-30 21:08:06 +0800212 PortConfigs::iterator* result, bool *created);
Mikhail Naganovccc82112023-04-27 18:14:15 -0700213 status_t filterAndUpdateBtA2dpParameters(AudioParameter &parameters);
214 status_t filterAndUpdateBtHfpParameters(AudioParameter &parameters);
215 status_t filterAndUpdateBtLeParameters(AudioParameter &parameters);
216 status_t filterAndUpdateBtScoParameters(AudioParameter &parameters);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800217 status_t findOrCreatePatch(
218 const std::set<int32_t>& sourcePortConfigIds,
219 const std::set<int32_t>& sinkPortConfigIds,
220 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
221 status_t findOrCreatePatch(
222 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
223 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
224 status_t findOrCreatePortConfig(
225 const ::aidl::android::media::audio::common::AudioDevice& device,
jiabin9c07faf2023-04-26 22:00:44 +0000226 const ::aidl::android::media::audio::common::AudioConfig* config,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800227 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
228 bool* created);
229 status_t findOrCreatePortConfig(
230 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800231 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800232 int32_t ioHandle,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800233 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganov289468a2023-03-29 10:06:15 -0700234 const std::set<int32_t>& destinationPortIds,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800235 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
236 status_t findOrCreatePortConfig(
237 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganov289468a2023-03-29 10:06:15 -0700238 const std::set<int32_t>& destinationPortIds,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800239 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
240 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
241 const std::set<int32_t>& sinkPortConfigIds);
242 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
243 Ports::iterator findPort(
244 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov289468a2023-03-29 10:06:15 -0700245 const ::aidl::android::media::audio::common::AudioIoFlags& flags,
246 const std::set<int32_t>& destinationPortIds);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800247 PortConfigs::iterator findPortConfig(
248 const ::aidl::android::media::audio::common::AudioDevice& device);
249 PortConfigs::iterator findPortConfig(
250 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800251 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800252 int32_t ioHandle);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800253 status_t prepareToOpenStream(
254 int32_t aidlHandle,
255 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
256 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800257 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800258 struct audio_config* config,
259 Cleanups* cleanups,
260 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800261 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
Mikhail Naganove93a0862023-03-15 17:06:59 -0700262 ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800263 void resetPatch(int32_t patchId);
264 void resetPortConfig(int32_t portConfigId);
Mikhail Naganove93a0862023-03-15 17:06:59 -0700265 void resetUnusedPatches();
266 void resetUnusedPatchesAndPortConfigs();
267 void resetUnusedPortConfigs();
Mikhail Naganov289468a2023-03-29 10:06:15 -0700268 status_t updateRoutes();
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800269
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800270 // CallbackBroker implementation
271 void clearCallbacks(void* cookie) override;
272 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
273 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
274 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
275 void setStreamOutEventCallback(void* cookie,
276 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
277 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
278 void* cookie) override;
279 void setStreamOutLatencyModeCallback(
280 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
281 // Implementation helpers.
282 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
283 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
284
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800285 // MicrophoneInfoProvider implementation
286 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
287
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800288 const std::string mInstance;
289 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Mikhail Naganov1fba38c2023-05-03 17:45:36 -0700290 const std::shared_ptr<::aidl::android::hardware::audio::core::ITelephony> mTelephony;
Mikhail Naganovccc82112023-04-27 18:14:15 -0700291 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetooth> mBluetooth;
292 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothA2dp> mBluetoothA2dp;
293 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothLe> mBluetoothLe;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800294 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800295 int32_t mDefaultInputPortId = -1;
296 int32_t mDefaultOutputPortId = -1;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800297 PortConfigs mPortConfigs;
jiabin9c07faf2023-04-26 22:00:44 +0000298 std::set<int32_t> mInitialPortConfigIds;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800299 Patches mPatches;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700300 Routes mRoutes;
Mikhail Naganov289468a2023-03-29 10:06:15 -0700301 RoutingMatrix mRoutingMatrix;
Mikhail Naganove93a0862023-03-15 17:06:59 -0700302 Streams mStreams;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800303 Microphones mMicrophones;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800304 std::mutex mLock;
305 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
jiabin872de702023-04-27 22:04:31 +0000306 std::set<audio_port_handle_t> mDeviceDisconnectionNotified;
Shunkai Yao51202502022-12-12 06:11:46 +0000307};
308
309} // namespace android