blob: caa82f1cd196c65ee17ba872a98551f69e1c9e56 [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 Naganovb1ddbb02023-03-15 17:06:59 -0700188 using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800189 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000190
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800191 // Must not be constructed directly by clients.
192 DeviceHalAidl(
193 const std::string& instance,
194 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module)
195 : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {}
196
197 ~DeviceHalAidl() override = default;
198
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800199 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
200 const ::aidl::android::media::audio::common::AudioPort& p);
201 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
202 const ::aidl::android::media::audio::common::AudioPortConfig& p);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800203 status_t createPortConfig(
204 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganov1a44bc62023-02-16 17:35:06 -0800205 PortConfigs::iterator* result);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800206 status_t findOrCreatePatch(
207 const std::set<int32_t>& sourcePortConfigIds,
208 const std::set<int32_t>& sinkPortConfigIds,
209 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
210 status_t findOrCreatePatch(
211 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
212 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
213 status_t findOrCreatePortConfig(
214 const ::aidl::android::media::audio::common::AudioDevice& device,
215 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
216 bool* created);
217 status_t findOrCreatePortConfig(
218 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800219 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800220 int32_t ioHandle,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800221 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800222 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
223 status_t findOrCreatePortConfig(
224 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
225 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
226 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
227 const std::set<int32_t>& sinkPortConfigIds);
228 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
229 Ports::iterator findPort(
230 const ::aidl::android::media::audio::common::AudioConfig& config,
231 const ::aidl::android::media::audio::common::AudioIoFlags& flags);
232 PortConfigs::iterator findPortConfig(
233 const ::aidl::android::media::audio::common::AudioDevice& device);
234 PortConfigs::iterator findPortConfig(
235 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800236 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800237 int32_t ioHandle);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800238 status_t prepareToOpenStream(
239 int32_t aidlHandle,
240 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
241 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800242 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800243 struct audio_config* config,
244 Cleanups* cleanups,
245 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800246 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700247 ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800248 void resetPatch(int32_t patchId);
249 void resetPortConfig(int32_t portConfigId);
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700250 void resetUnusedPatches();
251 void resetUnusedPatchesAndPortConfigs();
252 void resetUnusedPortConfigs();
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800253
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800254 // CallbackBroker implementation
255 void clearCallbacks(void* cookie) override;
256 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
257 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
258 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
259 void setStreamOutEventCallback(void* cookie,
260 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
261 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
262 void* cookie) override;
263 void setStreamOutLatencyModeCallback(
264 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
265 // Implementation helpers.
266 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
267 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
268
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800269 // MicrophoneInfoProvider implementation
270 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
271
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800272 const std::string mInstance;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000273 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100274 std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose>
275 mSoundDose = nullptr;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800276 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800277 int32_t mDefaultInputPortId = -1;
278 int32_t mDefaultOutputPortId = -1;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800279 PortConfigs mPortConfigs;
280 Patches mPatches;
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700281 Streams mStreams;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800282 Microphones mMicrophones;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800283 std::mutex mLock;
284 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
Shunkai Yao51202502022-12-12 06:11:46 +0000285};
286
287} // namespace android