blob: f2c64de0fa6e42350e2ad7940084017e1a86bdd8 [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:
Mikhail Naganov2d814892023-04-24 13:06:04 -070072 status_t getAudioPorts(std::vector<media::audio::common::AudioPort> *ports) override;
73
74 status_t getAudioRoutes(std::vector<media::AudioRoute> *routes) override;
75
Shunkai Yao51202502022-12-12 06:11:46 +000076 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
77 status_t getSupportedDevices(uint32_t *devices) override;
78
79 // Check to see if the audio hardware interface has been initialized.
80 status_t initCheck() override;
81
82 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
83 status_t setVoiceVolume(float volume) override;
84
85 // Set the audio volume for all audio activities other than voice call.
86 status_t setMasterVolume(float volume) override;
87
88 // Get the current master volume value for the HAL.
89 status_t getMasterVolume(float *volume) override;
90
91 // Called when the audio mode changes.
92 status_t setMode(audio_mode_t mode) override;
93
94 // Muting control.
95 status_t setMicMute(bool state) override;
96
97 status_t getMicMute(bool* state) override;
98
99 status_t setMasterMute(bool state) override;
100
101 status_t getMasterMute(bool *state) override;
102
103 // Set global audio parameters.
104 status_t setParameters(const String8& kvPairs) override;
105
106 // Get global audio parameters.
107 status_t getParameters(const String8& keys, String8 *values) override;
108
109 // Returns audio input buffer size according to parameters passed.
110 status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
111
112 // Creates and opens the audio hardware output stream. The stream is closed
113 // by releasing all references to the returned object.
114 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
115 audio_output_flags_t flags, struct audio_config* config,
116 const char* address, sp<StreamOutHalInterface>* outStream) override;
117
118 // Creates and opens the audio hardware input stream. The stream is closed
119 // by releasing all references to the returned object.
120 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
121 struct audio_config* config, audio_input_flags_t flags,
122 const char* address, audio_source_t source,
123 audio_devices_t outputDevice, const char* outputDeviceAddress,
124 sp<StreamInHalInterface>* inStream) override;
125
126 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
127 status_t supportsAudioPatches(bool* supportsPatches) override;
128
129 // Creates an audio patch between several source and sink ports.
130 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
131 unsigned int num_sinks, const struct audio_port_config* sinks,
132 audio_patch_handle_t* patch) override;
133
134 // Releases an audio patch.
135 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
136
Mikhail Naganov31d46652023-01-10 18:29:25 +0000137 // Fills the list of supported attributes for a given audio port.
138 status_t getAudioPort(struct audio_port* port) override;
139
140 // Fills the list of supported attributes for a given audio port.
141 status_t getAudioPort(struct audio_port_v7 *port) override;
142
Shunkai Yao51202502022-12-12 06:11:46 +0000143 // Set audio port configuration.
144 status_t setAudioPortConfig(const struct audio_port_config* config) override;
145
146 // List microphones
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700147 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000148
149 status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
150
151 status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
152
153 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
154 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
155 __unused) override;
156
157 int32_t getAAudioMixerBurstCount() override;
158
159 int32_t getAAudioHardwareBurstMinUsec() override;
160
161 error::Result<audio_hw_sync_t> getHwAvSync() override;
162
Shunkai Yao51202502022-12-12 06:11:46 +0000163 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
164
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100165 status_t getSoundDoseInterface(const std::string& module,
166 ::ndk::SpAIBinder* soundDoseBinder) override;
167
jiabinc0048632023-04-27 22:04:31 +0000168 status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override;
169
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700170 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override;
171
172 status_t setSimulateDeviceConnections(bool enabled) override;
173
174 status_t dump(int __unused, const Vector<String16>& __unused) override;
175
Shunkai Yao51202502022-12-12 06:11:46 +0000176 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000177 friend class sp<DeviceHalAidl>;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800178
179 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
180 wp<StreamOutHalInterfaceCallback> out;
181 wp<StreamOutHalInterfaceEventCallback> event;
182 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
183 };
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800184 struct Microphones {
185 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
186 Status status = Status::UNKNOWN;
187 MicrophoneInfoProvider::Info info;
188 };
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800189 using Patches = std::map<int32_t /*patch ID*/,
190 ::aidl::android::hardware::audio::core::AudioPatch>;
191 using PortConfigs = std::map<int32_t /*port config ID*/,
192 ::aidl::android::media::audio::common::AudioPortConfig>;
193 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
Mikhail Naganov2d814892023-04-24 13:06:04 -0700194 using Routes = std::vector<::aidl::android::hardware::audio::core::AudioRoute>;
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700195 // Answers the question "whether portID 'first' is reachable from portID 'second'?"
196 // It's not a map because both portIDs are known. The matrix is symmetric.
197 using RoutingMatrix = std::set<std::pair<int32_t, int32_t>>;
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700198 using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800199 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000200
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800201 // Must not be constructed directly by clients.
202 DeviceHalAidl(
203 const std::string& instance,
204 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module)
205 : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {}
206
207 ~DeviceHalAidl() override = default;
208
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800209 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
210 const ::aidl::android::media::audio::common::AudioPort& p);
211 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
212 const ::aidl::android::media::audio::common::AudioPortConfig& p);
David Lia8f1e582023-03-30 21:08:06 +0800213 status_t createOrUpdatePortConfig(
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800214 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
David Lia8f1e582023-03-30 21:08:06 +0800215 PortConfigs::iterator* result, bool *created);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800216 status_t findOrCreatePatch(
217 const std::set<int32_t>& sourcePortConfigIds,
218 const std::set<int32_t>& sinkPortConfigIds,
219 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
220 status_t findOrCreatePatch(
221 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
222 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
223 status_t findOrCreatePortConfig(
224 const ::aidl::android::media::audio::common::AudioDevice& device,
jiabin2248fa12023-04-27 22:04:16 +0000225 const ::aidl::android::media::audio::common::AudioConfig* config,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800226 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
227 bool* created);
228 status_t findOrCreatePortConfig(
229 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800230 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800231 int32_t ioHandle,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800232 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700233 const std::set<int32_t>& destinationPortIds,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800234 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
235 status_t findOrCreatePortConfig(
236 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700237 const std::set<int32_t>& destinationPortIds,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800238 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
239 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
240 const std::set<int32_t>& sinkPortConfigIds);
241 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
242 Ports::iterator findPort(
243 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700244 const ::aidl::android::media::audio::common::AudioIoFlags& flags,
245 const std::set<int32_t>& destinationPortIds);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800246 PortConfigs::iterator findPortConfig(
247 const ::aidl::android::media::audio::common::AudioDevice& device);
248 PortConfigs::iterator findPortConfig(
249 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800250 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800251 int32_t ioHandle);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800252 status_t prepareToOpenStream(
253 int32_t aidlHandle,
254 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
255 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800256 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800257 struct audio_config* config,
258 Cleanups* cleanups,
259 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800260 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700261 ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800262 void resetPatch(int32_t patchId);
263 void resetPortConfig(int32_t portConfigId);
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700264 void resetUnusedPatches();
265 void resetUnusedPatchesAndPortConfigs();
266 void resetUnusedPortConfigs();
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700267 status_t updateRoutes();
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800268
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800269 // CallbackBroker implementation
270 void clearCallbacks(void* cookie) override;
271 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
272 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
273 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
274 void setStreamOutEventCallback(void* cookie,
275 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
276 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
277 void* cookie) override;
278 void setStreamOutLatencyModeCallback(
279 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
280 // Implementation helpers.
281 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
282 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
283
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800284 // MicrophoneInfoProvider implementation
285 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
286
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800287 const std::string mInstance;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000288 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100289 std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose>
290 mSoundDose = nullptr;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800291 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800292 int32_t mDefaultInputPortId = -1;
293 int32_t mDefaultOutputPortId = -1;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800294 PortConfigs mPortConfigs;
jiabin2248fa12023-04-27 22:04:16 +0000295 std::set<int32_t> mInitialPortConfigIds;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800296 Patches mPatches;
Mikhail Naganov2d814892023-04-24 13:06:04 -0700297 Routes mRoutes;
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700298 RoutingMatrix mRoutingMatrix;
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700299 Streams mStreams;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800300 Microphones mMicrophones;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800301 std::mutex mLock;
302 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
jiabinc0048632023-04-27 22:04:31 +0000303 std::set<audio_port_handle_t> mDeviceDisconnectionNotified;
Shunkai Yao51202502022-12-12 06:11:46 +0000304};
305
306} // namespace android