blob: e4d5ec6143de2fe93d1314dfb04e9ca9bf0c4564 [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 Naganovf56ce782023-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 Naganovf56ce782023-01-25 11:29:11 -080024#include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h>
Mikhail Naganovb0c55252023-02-08 16:59:41 -080025#include <android-base/thread_annotations.h>
Shunkai Yao51202502022-12-12 06:11:46 +000026#include <media/audiohal/DeviceHalInterface.h>
27#include <media/audiohal/EffectHalInterface.h>
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -070028#include <media/audiohal/StreamHalInterface.h>
Shunkai Yao51202502022-12-12 06:11:46 +000029
Mikhail Naganov31d46652023-01-10 18:29:25 +000030#include "ConversionHelperAidl.h"
Shunkai Yao51202502022-12-12 06:11:46 +000031
32namespace android {
33
Mikhail Naganovb0c55252023-02-08 16:59:41 -080034class StreamOutHalInterfaceCallback;
35class StreamOutHalInterfaceEventCallback;
36class StreamOutHalInterfaceLatencyModeCallback;
37
38// The role of the broker is to connect AIDL callback interface implementations
39// with StreamOut callback implementations. Since AIDL requires all callbacks
40// to be provided upfront, while libaudiohal interfaces allow late registration,
41// there is a need to coordinate the matching process.
42class CallbackBroker : public virtual RefBase {
43 public:
44 virtual ~CallbackBroker() = default;
45 // The cookie is always the stream instance pointer. We don't use weak pointers to avoid extra
46 // costs on reference counting. The stream cleans up related entries on destruction. Since
47 // access to the callbacks map is synchronized, the possibility for pointer aliasing due to
48 // allocation of a new stream at the address of previously deleted stream is avoided.
49 virtual void clearCallbacks(void* cookie) = 0;
50 virtual sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) = 0;
51 virtual void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>&) = 0;
52 virtual sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) = 0;
53 virtual void setStreamOutEventCallback(void* cookie,
54 const sp<StreamOutHalInterfaceEventCallback>&) = 0;
55 virtual sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
56 void* cookie) = 0;
57 virtual void setStreamOutLatencyModeCallback(
58 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>&) = 0;
59};
60
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080061class MicrophoneInfoProvider : public virtual RefBase {
62 public:
63 using Info = std::vector<::aidl::android::media::audio::common::MicrophoneInfo>;
64 virtual ~MicrophoneInfoProvider() = default;
65 // Returns a nullptr if the HAL does not support microphone info retrieval.
66 virtual Info const* getMicrophoneInfo() = 0;
67};
68
Mikhail Naganovb0c55252023-02-08 16:59:41 -080069class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl,
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080070 public CallbackBroker, public MicrophoneInfoProvider {
Shunkai Yao51202502022-12-12 06:11:46 +000071 public:
72 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
73 status_t getSupportedDevices(uint32_t *devices) override;
74
75 // Check to see if the audio hardware interface has been initialized.
76 status_t initCheck() override;
77
78 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
79 status_t setVoiceVolume(float volume) override;
80
81 // Set the audio volume for all audio activities other than voice call.
82 status_t setMasterVolume(float volume) override;
83
84 // Get the current master volume value for the HAL.
85 status_t getMasterVolume(float *volume) override;
86
87 // Called when the audio mode changes.
88 status_t setMode(audio_mode_t mode) override;
89
90 // Muting control.
91 status_t setMicMute(bool state) override;
92
93 status_t getMicMute(bool* state) override;
94
95 status_t setMasterMute(bool state) override;
96
97 status_t getMasterMute(bool *state) override;
98
99 // Set global audio parameters.
100 status_t setParameters(const String8& kvPairs) override;
101
102 // Get global audio parameters.
103 status_t getParameters(const String8& keys, String8 *values) override;
104
105 // Returns audio input buffer size according to parameters passed.
106 status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
107
108 // Creates and opens the audio hardware output stream. The stream is closed
109 // by releasing all references to the returned object.
110 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
111 audio_output_flags_t flags, struct audio_config* config,
112 const char* address, sp<StreamOutHalInterface>* outStream) override;
113
114 // Creates and opens the audio hardware input stream. The stream is closed
115 // by releasing all references to the returned object.
116 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
117 struct audio_config* config, audio_input_flags_t flags,
118 const char* address, audio_source_t source,
119 audio_devices_t outputDevice, const char* outputDeviceAddress,
120 sp<StreamInHalInterface>* inStream) override;
121
122 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
123 status_t supportsAudioPatches(bool* supportsPatches) override;
124
125 // Creates an audio patch between several source and sink ports.
126 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
127 unsigned int num_sinks, const struct audio_port_config* sinks,
128 audio_patch_handle_t* patch) override;
129
130 // Releases an audio patch.
131 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
132
Mikhail Naganov31d46652023-01-10 18:29:25 +0000133 // Fills the list of supported attributes for a given audio port.
134 status_t getAudioPort(struct audio_port* port) override;
135
136 // Fills the list of supported attributes for a given audio port.
137 status_t getAudioPort(struct audio_port_v7 *port) override;
138
Shunkai Yao51202502022-12-12 06:11:46 +0000139 // Set audio port configuration.
140 status_t setAudioPortConfig(const struct audio_port_config* config) override;
141
142 // List microphones
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700143 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000144
145 status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
146
147 status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
148
149 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
150 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
151 __unused) override;
152
153 int32_t getAAudioMixerBurstCount() override;
154
155 int32_t getAAudioHardwareBurstMinUsec() override;
156
157 error::Result<audio_hw_sync_t> getHwAvSync() override;
158
Shunkai Yao51202502022-12-12 06:11:46 +0000159 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
160
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100161 status_t getSoundDoseInterface(const std::string& module,
162 ::ndk::SpAIBinder* soundDoseBinder) override;
163
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700164 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override;
165
166 status_t setSimulateDeviceConnections(bool enabled) override;
167
168 status_t dump(int __unused, const Vector<String16>& __unused) override;
169
Shunkai Yao51202502022-12-12 06:11:46 +0000170 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000171 friend class sp<DeviceHalAidl>;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800172
173 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
174 wp<StreamOutHalInterfaceCallback> out;
175 wp<StreamOutHalInterfaceEventCallback> event;
176 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
177 };
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800178 struct Microphones {
179 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
180 Status status = Status::UNKNOWN;
181 MicrophoneInfoProvider::Info info;
182 };
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800183 using Patches = std::map<int32_t /*patch ID*/,
184 ::aidl::android::hardware::audio::core::AudioPatch>;
185 using PortConfigs = std::map<int32_t /*port config ID*/,
186 ::aidl::android::media::audio::common::AudioPortConfig>;
187 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700188 // Answers the question "whether portID 'first' is reachable from portID 'second'?"
189 // It's not a map because both portIDs are known. The matrix is symmetric.
190 using RoutingMatrix = std::set<std::pair<int32_t, int32_t>>;
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700191 using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800192 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000193
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800194 // Must not be constructed directly by clients.
195 DeviceHalAidl(
196 const std::string& instance,
197 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module)
198 : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {}
199
200 ~DeviceHalAidl() override = default;
201
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800202 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
203 const ::aidl::android::media::audio::common::AudioPort& p);
204 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
205 const ::aidl::android::media::audio::common::AudioPortConfig& p);
David Lia8f1e582023-03-30 21:08:06 +0800206 status_t createOrUpdatePortConfig(
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800207 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
David Lia8f1e582023-03-30 21:08:06 +0800208 PortConfigs::iterator* result, bool *created);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800209 status_t findOrCreatePatch(
210 const std::set<int32_t>& sourcePortConfigIds,
211 const std::set<int32_t>& sinkPortConfigIds,
212 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
213 status_t findOrCreatePatch(
214 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
215 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
216 status_t findOrCreatePortConfig(
217 const ::aidl::android::media::audio::common::AudioDevice& device,
218 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
219 bool* created);
220 status_t findOrCreatePortConfig(
221 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800222 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800223 int32_t ioHandle,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800224 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700225 const std::set<int32_t>& destinationPortIds,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800226 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
227 status_t findOrCreatePortConfig(
228 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700229 const std::set<int32_t>& destinationPortIds,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800230 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
231 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
232 const std::set<int32_t>& sinkPortConfigIds);
233 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
234 Ports::iterator findPort(
235 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700236 const ::aidl::android::media::audio::common::AudioIoFlags& flags,
237 const std::set<int32_t>& destinationPortIds);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800238 PortConfigs::iterator findPortConfig(
239 const ::aidl::android::media::audio::common::AudioDevice& device);
240 PortConfigs::iterator findPortConfig(
241 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800242 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800243 int32_t ioHandle);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800244 status_t prepareToOpenStream(
245 int32_t aidlHandle,
246 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
247 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800248 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800249 struct audio_config* config,
250 Cleanups* cleanups,
251 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800252 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700253 ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800254 void resetPatch(int32_t patchId);
255 void resetPortConfig(int32_t portConfigId);
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700256 void resetUnusedPatches();
257 void resetUnusedPatchesAndPortConfigs();
258 void resetUnusedPortConfigs();
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700259 status_t updateRoutes();
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800260
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800261 // CallbackBroker implementation
262 void clearCallbacks(void* cookie) override;
263 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
264 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
265 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
266 void setStreamOutEventCallback(void* cookie,
267 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
268 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
269 void* cookie) override;
270 void setStreamOutLatencyModeCallback(
271 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
272 // Implementation helpers.
273 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
274 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
275
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800276 // MicrophoneInfoProvider implementation
277 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
278
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800279 const std::string mInstance;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000280 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100281 std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose>
282 mSoundDose = nullptr;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800283 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800284 int32_t mDefaultInputPortId = -1;
285 int32_t mDefaultOutputPortId = -1;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800286 PortConfigs mPortConfigs;
287 Patches mPatches;
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700288 RoutingMatrix mRoutingMatrix;
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700289 Streams mStreams;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800290 Microphones mMicrophones;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800291 std::mutex mLock;
292 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
Shunkai Yao51202502022-12-12 06:11:46 +0000293};
294
295} // namespace android