blob: ea3e12596c8821892f9b7c092417c9ecd8716ca3 [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
Shunkai Yao51202502022-12-12 06:11:46 +000075 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
76 status_t getSupportedDevices(uint32_t *devices) override;
77
78 // Check to see if the audio hardware interface has been initialized.
79 status_t initCheck() override;
80
81 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
82 status_t setVoiceVolume(float volume) override;
83
84 // Set the audio volume for all audio activities other than voice call.
85 status_t setMasterVolume(float volume) override;
86
87 // Get the current master volume value for the HAL.
88 status_t getMasterVolume(float *volume) override;
89
90 // Called when the audio mode changes.
91 status_t setMode(audio_mode_t mode) override;
92
93 // Muting control.
94 status_t setMicMute(bool state) override;
95
96 status_t getMicMute(bool* state) override;
97
98 status_t setMasterMute(bool state) override;
99
100 status_t getMasterMute(bool *state) override;
101
102 // Set global audio parameters.
103 status_t setParameters(const String8& kvPairs) override;
104
105 // Get global audio parameters.
106 status_t getParameters(const String8& keys, String8 *values) override;
107
108 // Returns audio input buffer size according to parameters passed.
109 status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
110
111 // Creates and opens the audio hardware output stream. The stream is closed
112 // by releasing all references to the returned object.
113 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
114 audio_output_flags_t flags, struct audio_config* config,
115 const char* address, sp<StreamOutHalInterface>* outStream) override;
116
117 // Creates and opens the audio hardware input stream. The stream is closed
118 // by releasing all references to the returned object.
119 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
120 struct audio_config* config, audio_input_flags_t flags,
121 const char* address, audio_source_t source,
122 audio_devices_t outputDevice, const char* outputDeviceAddress,
123 sp<StreamInHalInterface>* inStream) override;
124
125 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
126 status_t supportsAudioPatches(bool* supportsPatches) override;
127
128 // Creates an audio patch between several source and sink ports.
129 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
130 unsigned int num_sinks, const struct audio_port_config* sinks,
131 audio_patch_handle_t* patch) override;
132
133 // Releases an audio patch.
134 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
135
Mikhail Naganov31d46652023-01-10 18:29:25 +0000136 // Fills the list of supported attributes for a given audio port.
137 status_t getAudioPort(struct audio_port* port) override;
138
139 // Fills the list of supported attributes for a given audio port.
140 status_t getAudioPort(struct audio_port_v7 *port) override;
141
Shunkai Yao51202502022-12-12 06:11:46 +0000142 // Set audio port configuration.
143 status_t setAudioPortConfig(const struct audio_port_config* config) override;
144
145 // List microphones
Mikhail Naganove93a0862023-03-15 17:06:59 -0700146 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000147
148 status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
149
150 status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
151
152 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
153 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
154 __unused) override;
155
156 int32_t getAAudioMixerBurstCount() override;
157
158 int32_t getAAudioHardwareBurstMinUsec() override;
159
160 error::Result<audio_hw_sync_t> getHwAvSync() override;
161
Shunkai Yao51202502022-12-12 06:11:46 +0000162 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
163
jiabin872de702023-04-27 22:04:31 +0000164 status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override;
165
Mikhail Naganove93a0862023-03-15 17:06:59 -0700166 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override;
167
168 status_t setSimulateDeviceConnections(bool enabled) override;
169
170 status_t dump(int __unused, const Vector<String16>& __unused) override;
171
Shunkai Yao51202502022-12-12 06:11:46 +0000172 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000173 friend class sp<DeviceHalAidl>;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800174
175 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
176 wp<StreamOutHalInterfaceCallback> out;
177 wp<StreamOutHalInterfaceEventCallback> event;
178 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
179 };
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800180 struct Microphones {
181 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
182 Status status = Status::UNKNOWN;
183 MicrophoneInfoProvider::Info info;
184 };
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800185 using Patches = std::map<int32_t /*patch ID*/,
186 ::aidl::android::hardware::audio::core::AudioPatch>;
187 using PortConfigs = std::map<int32_t /*port config ID*/,
188 ::aidl::android::media::audio::common::AudioPortConfig>;
189 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700190 using Routes = std::vector<::aidl::android::hardware::audio::core::AudioRoute>;
Mikhail Naganov289468a2023-03-29 10:06:15 -0700191 // Answers the question "whether portID 'first' is reachable from portID 'second'?"
192 // It's not a map because both portIDs are known. The matrix is symmetric.
193 using RoutingMatrix = std::set<std::pair<int32_t, int32_t>>;
Mikhail Naganove93a0862023-03-15 17:06:59 -0700194 using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800195 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000196
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800197 // Must not be constructed directly by clients.
198 DeviceHalAidl(
199 const std::string& instance,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000200 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module)
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800201 : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {}
Shunkai Yao51202502022-12-12 06:11:46 +0000202
Mikhail Naganov31d46652023-01-10 18:29:25 +0000203 ~DeviceHalAidl() override = default;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800204
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800205 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
206 const ::aidl::android::media::audio::common::AudioPort& p);
207 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
208 const ::aidl::android::media::audio::common::AudioPortConfig& p);
David Lia8675d42023-03-30 21:08:06 +0800209 status_t createOrUpdatePortConfig(
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800210 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
David Lia8675d42023-03-30 21:08:06 +0800211 PortConfigs::iterator* result, bool *created);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800212 status_t findOrCreatePatch(
213 const std::set<int32_t>& sourcePortConfigIds,
214 const std::set<int32_t>& sinkPortConfigIds,
215 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
216 status_t findOrCreatePatch(
217 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
218 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
219 status_t findOrCreatePortConfig(
220 const ::aidl::android::media::audio::common::AudioDevice& device,
jiabin9c07faf2023-04-26 22:00:44 +0000221 const ::aidl::android::media::audio::common::AudioConfig* config,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800222 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
223 bool* created);
224 status_t findOrCreatePortConfig(
225 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800226 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800227 int32_t ioHandle,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800228 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganov289468a2023-03-29 10:06:15 -0700229 const std::set<int32_t>& destinationPortIds,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800230 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
231 status_t findOrCreatePortConfig(
232 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganov289468a2023-03-29 10:06:15 -0700233 const std::set<int32_t>& destinationPortIds,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800234 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
235 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
236 const std::set<int32_t>& sinkPortConfigIds);
237 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
238 Ports::iterator findPort(
239 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov289468a2023-03-29 10:06:15 -0700240 const ::aidl::android::media::audio::common::AudioIoFlags& flags,
241 const std::set<int32_t>& destinationPortIds);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800242 PortConfigs::iterator findPortConfig(
243 const ::aidl::android::media::audio::common::AudioDevice& device);
244 PortConfigs::iterator findPortConfig(
245 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800246 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800247 int32_t ioHandle);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800248 status_t prepareToOpenStream(
249 int32_t aidlHandle,
250 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
251 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800252 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800253 struct audio_config* config,
254 Cleanups* cleanups,
255 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800256 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
Mikhail Naganove93a0862023-03-15 17:06:59 -0700257 ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800258 void resetPatch(int32_t patchId);
259 void resetPortConfig(int32_t portConfigId);
Mikhail Naganove93a0862023-03-15 17:06:59 -0700260 void resetUnusedPatches();
261 void resetUnusedPatchesAndPortConfigs();
262 void resetUnusedPortConfigs();
Mikhail Naganov289468a2023-03-29 10:06:15 -0700263 status_t updateRoutes();
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800264
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800265 // CallbackBroker implementation
266 void clearCallbacks(void* cookie) override;
267 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
268 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
269 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
270 void setStreamOutEventCallback(void* cookie,
271 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
272 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
273 void* cookie) override;
274 void setStreamOutLatencyModeCallback(
275 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
276 // Implementation helpers.
277 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
278 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
279
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800280 // MicrophoneInfoProvider implementation
281 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
282
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800283 const std::string mInstance;
284 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
285 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800286 int32_t mDefaultInputPortId = -1;
287 int32_t mDefaultOutputPortId = -1;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800288 PortConfigs mPortConfigs;
jiabin9c07faf2023-04-26 22:00:44 +0000289 std::set<int32_t> mInitialPortConfigIds;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800290 Patches mPatches;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700291 Routes mRoutes;
Mikhail Naganov289468a2023-03-29 10:06:15 -0700292 RoutingMatrix mRoutingMatrix;
Mikhail Naganove93a0862023-03-15 17:06:59 -0700293 Streams mStreams;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800294 Microphones mMicrophones;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800295 std::mutex mLock;
296 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
jiabin872de702023-04-27 22:04:31 +0000297 std::set<audio_port_handle_t> mDeviceDisconnectionNotified;
Shunkai Yao51202502022-12-12 06:11:46 +0000298};
299
300} // namespace android