blob: 7a3d8870e0c96ea95cd098e9bddf5c7c22a19ca0 [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>
28
Mikhail Naganov31d46652023-01-10 18:29:25 +000029#include "ConversionHelperAidl.h"
Shunkai Yao51202502022-12-12 06:11:46 +000030
31namespace android {
32
Mikhail Naganovb0c55252023-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 Naganovb0c55252023-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
142 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones);
143
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
158 status_t dump(int __unused, const Vector<String16>& __unused) override;
159
160 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
161
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100162 status_t getSoundDoseInterface(const std::string& module,
163 ::ndk::SpAIBinder* soundDoseBinder) override;
164
Shunkai Yao51202502022-12-12 06:11:46 +0000165 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000166 friend class sp<DeviceHalAidl>;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800167
168 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
169 wp<StreamOutHalInterfaceCallback> out;
170 wp<StreamOutHalInterfaceEventCallback> event;
171 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
172 };
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800173 struct Microphones {
174 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
175 Status status = Status::UNKNOWN;
176 MicrophoneInfoProvider::Info info;
177 };
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800178 using Patches = std::map<int32_t /*patch ID*/,
179 ::aidl::android::hardware::audio::core::AudioPatch>;
180 using PortConfigs = std::map<int32_t /*port config ID*/,
181 ::aidl::android::media::audio::common::AudioPortConfig>;
182 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
183 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000184
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800185 // Must not be constructed directly by clients.
186 DeviceHalAidl(
187 const std::string& instance,
188 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module)
189 : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {}
190
191 ~DeviceHalAidl() override = default;
192
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800193 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
194 const ::aidl::android::media::audio::common::AudioPort& p);
195 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
196 const ::aidl::android::media::audio::common::AudioPortConfig& p);
David Lia8f1e582023-03-30 21:08:06 +0800197 status_t createOrUpdatePortConfig(
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800198 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
David Lia8f1e582023-03-30 21:08:06 +0800199 PortConfigs::iterator* result, bool *created);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800200 status_t findOrCreatePatch(
201 const std::set<int32_t>& sourcePortConfigIds,
202 const std::set<int32_t>& sinkPortConfigIds,
203 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
204 status_t findOrCreatePatch(
205 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
206 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
207 status_t findOrCreatePortConfig(
208 const ::aidl::android::media::audio::common::AudioDevice& device,
209 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
210 bool* created);
211 status_t findOrCreatePortConfig(
212 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800213 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800214 int32_t ioHandle,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800215 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800216 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
217 status_t findOrCreatePortConfig(
218 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
219 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
220 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
221 const std::set<int32_t>& sinkPortConfigIds);
222 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
223 Ports::iterator findPort(
224 const ::aidl::android::media::audio::common::AudioConfig& config,
225 const ::aidl::android::media::audio::common::AudioIoFlags& flags);
226 PortConfigs::iterator findPortConfig(
227 const ::aidl::android::media::audio::common::AudioDevice& device);
228 PortConfigs::iterator findPortConfig(
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);
232 // Currently unused but may be useful for implementing setAudioPortConfig
233 // PortConfigs::iterator findPortConfig(
234 // const ::aidl::android::media::audio::common::AudioPortConfig& portConfig);
235 status_t prepareToOpenStream(
236 int32_t aidlHandle,
237 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
238 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800239 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800240 struct audio_config* config,
241 Cleanups* cleanups,
242 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800243 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
244 int32_t* nominalLatency);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800245 void resetPatch(int32_t patchId);
246 void resetPortConfig(int32_t portConfigId);
247
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800248 // CallbackBroker implementation
249 void clearCallbacks(void* cookie) override;
250 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
251 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
252 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
253 void setStreamOutEventCallback(void* cookie,
254 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
255 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
256 void* cookie) override;
257 void setStreamOutLatencyModeCallback(
258 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
259 // Implementation helpers.
260 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
261 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
262
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800263 // MicrophoneInfoProvider implementation
264 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
265
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800266 const std::string mInstance;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000267 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100268 std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose>
269 mSoundDose = nullptr;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800270 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800271 int32_t mDefaultInputPortId = -1;
272 int32_t mDefaultOutputPortId = -1;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800273 PortConfigs mPortConfigs;
274 Patches mPatches;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800275 Microphones mMicrophones;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800276 std::mutex mLock;
277 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
Shunkai Yao51202502022-12-12 06:11:46 +0000278};
279
280} // namespace android