blob: 35429cad8db2c907926b0519fca4b555fea37c35 [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:
71 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
72 status_t getSupportedDevices(uint32_t *devices) override;
73
74 // Check to see if the audio hardware interface has been initialized.
75 status_t initCheck() override;
76
77 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
78 status_t setVoiceVolume(float volume) override;
79
80 // Set the audio volume for all audio activities other than voice call.
81 status_t setMasterVolume(float volume) override;
82
83 // Get the current master volume value for the HAL.
84 status_t getMasterVolume(float *volume) override;
85
86 // Called when the audio mode changes.
87 status_t setMode(audio_mode_t mode) override;
88
89 // Muting control.
90 status_t setMicMute(bool state) override;
91
92 status_t getMicMute(bool* state) override;
93
94 status_t setMasterMute(bool state) override;
95
96 status_t getMasterMute(bool *state) override;
97
98 // Set global audio parameters.
99 status_t setParameters(const String8& kvPairs) override;
100
101 // Get global audio parameters.
102 status_t getParameters(const String8& keys, String8 *values) override;
103
104 // Returns audio input buffer size according to parameters passed.
105 status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
106
107 // Creates and opens the audio hardware output stream. The stream is closed
108 // by releasing all references to the returned object.
109 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
110 audio_output_flags_t flags, struct audio_config* config,
111 const char* address, sp<StreamOutHalInterface>* outStream) override;
112
113 // Creates and opens the audio hardware input stream. The stream is closed
114 // by releasing all references to the returned object.
115 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
116 struct audio_config* config, audio_input_flags_t flags,
117 const char* address, audio_source_t source,
118 audio_devices_t outputDevice, const char* outputDeviceAddress,
119 sp<StreamInHalInterface>* inStream) override;
120
121 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
122 status_t supportsAudioPatches(bool* supportsPatches) override;
123
124 // Creates an audio patch between several source and sink ports.
125 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
126 unsigned int num_sinks, const struct audio_port_config* sinks,
127 audio_patch_handle_t* patch) override;
128
129 // Releases an audio patch.
130 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
131
Mikhail Naganov31d46652023-01-10 18:29:25 +0000132 // Fills the list of supported attributes for a given audio port.
133 status_t getAudioPort(struct audio_port* port) override;
134
135 // Fills the list of supported attributes for a given audio port.
136 status_t getAudioPort(struct audio_port_v7 *port) override;
137
Shunkai Yao51202502022-12-12 06:11:46 +0000138 // Set audio port configuration.
139 status_t setAudioPortConfig(const struct audio_port_config* config) override;
140
141 // List microphones
Mikhail Naganove93a0862023-03-15 17:06:59 -0700142 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000143
144 status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
145
146 status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
147
148 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
149 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
150 __unused) override;
151
152 int32_t getAAudioMixerBurstCount() override;
153
154 int32_t getAAudioHardwareBurstMinUsec() override;
155
156 error::Result<audio_hw_sync_t> getHwAvSync() override;
157
Shunkai Yao51202502022-12-12 06:11:46 +0000158 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
159
Mikhail Naganove93a0862023-03-15 17:06:59 -0700160 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override;
161
162 status_t setSimulateDeviceConnections(bool enabled) override;
163
164 status_t dump(int __unused, const Vector<String16>& __unused) override;
165
Shunkai Yao51202502022-12-12 06:11:46 +0000166 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000167 friend class sp<DeviceHalAidl>;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800168
169 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
170 wp<StreamOutHalInterfaceCallback> out;
171 wp<StreamOutHalInterfaceEventCallback> event;
172 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
173 };
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800174 struct Microphones {
175 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
176 Status status = Status::UNKNOWN;
177 MicrophoneInfoProvider::Info info;
178 };
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800179 using Patches = std::map<int32_t /*patch ID*/,
180 ::aidl::android::hardware::audio::core::AudioPatch>;
181 using PortConfigs = std::map<int32_t /*port config ID*/,
182 ::aidl::android::media::audio::common::AudioPortConfig>;
183 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
Mikhail Naganove93a0862023-03-15 17:06:59 -0700184 using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800185 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000186
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800187 // Must not be constructed directly by clients.
188 DeviceHalAidl(
189 const std::string& instance,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000190 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module)
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800191 : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {}
Shunkai Yao51202502022-12-12 06:11:46 +0000192
Mikhail Naganov31d46652023-01-10 18:29:25 +0000193 ~DeviceHalAidl() override = default;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800194
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800195 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
196 const ::aidl::android::media::audio::common::AudioPort& p);
197 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
198 const ::aidl::android::media::audio::common::AudioPortConfig& p);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800199 status_t createPortConfig(
200 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganov1a44bc62023-02-16 17:35:06 -0800201 PortConfigs::iterator* result);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800202 status_t findOrCreatePatch(
203 const std::set<int32_t>& sourcePortConfigIds,
204 const std::set<int32_t>& sinkPortConfigIds,
205 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
206 status_t findOrCreatePatch(
207 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
208 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
209 status_t findOrCreatePortConfig(
210 const ::aidl::android::media::audio::common::AudioDevice& device,
211 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
212 bool* created);
213 status_t findOrCreatePortConfig(
214 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800215 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800216 int32_t ioHandle,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800217 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800218 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
219 status_t findOrCreatePortConfig(
220 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
221 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
222 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
223 const std::set<int32_t>& sinkPortConfigIds);
224 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
225 Ports::iterator findPort(
226 const ::aidl::android::media::audio::common::AudioConfig& config,
227 const ::aidl::android::media::audio::common::AudioIoFlags& flags);
228 PortConfigs::iterator findPortConfig(
229 const ::aidl::android::media::audio::common::AudioDevice& device);
230 PortConfigs::iterator findPortConfig(
231 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800232 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800233 int32_t ioHandle);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800234 status_t prepareToOpenStream(
235 int32_t aidlHandle,
236 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
237 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800238 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800239 struct audio_config* config,
240 Cleanups* cleanups,
241 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800242 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
Mikhail Naganove93a0862023-03-15 17:06:59 -0700243 ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800244 void resetPatch(int32_t patchId);
245 void resetPortConfig(int32_t portConfigId);
Mikhail Naganove93a0862023-03-15 17:06:59 -0700246 void resetUnusedPatches();
247 void resetUnusedPatchesAndPortConfigs();
248 void resetUnusedPortConfigs();
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800249
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800250 // CallbackBroker implementation
251 void clearCallbacks(void* cookie) override;
252 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
253 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
254 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
255 void setStreamOutEventCallback(void* cookie,
256 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
257 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
258 void* cookie) override;
259 void setStreamOutLatencyModeCallback(
260 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
261 // Implementation helpers.
262 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
263 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
264
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800265 // MicrophoneInfoProvider implementation
266 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
267
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800268 const std::string mInstance;
269 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
270 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800271 int32_t mDefaultInputPortId = -1;
272 int32_t mDefaultOutputPortId = -1;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800273 PortConfigs mPortConfigs;
274 Patches mPatches;
Mikhail Naganove93a0862023-03-15 17:06:59 -0700275 Streams mStreams;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800276 Microphones mMicrophones;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800277 std::mutex mLock;
278 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
Shunkai Yao51202502022-12-12 06:11:46 +0000279};
280
281} // namespace android