blob: ea0cd4d1391221a7fcc9bc202bfe19a80f71b1f7 [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
60class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl,
61 public CallbackBroker {
Shunkai Yao51202502022-12-12 06:11:46 +000062 public:
63 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
64 status_t getSupportedDevices(uint32_t *devices) override;
65
66 // Check to see if the audio hardware interface has been initialized.
67 status_t initCheck() override;
68
69 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
70 status_t setVoiceVolume(float volume) override;
71
72 // Set the audio volume for all audio activities other than voice call.
73 status_t setMasterVolume(float volume) override;
74
75 // Get the current master volume value for the HAL.
76 status_t getMasterVolume(float *volume) override;
77
78 // Called when the audio mode changes.
79 status_t setMode(audio_mode_t mode) override;
80
81 // Muting control.
82 status_t setMicMute(bool state) override;
83
84 status_t getMicMute(bool* state) override;
85
86 status_t setMasterMute(bool state) override;
87
88 status_t getMasterMute(bool *state) override;
89
90 // Set global audio parameters.
91 status_t setParameters(const String8& kvPairs) override;
92
93 // Get global audio parameters.
94 status_t getParameters(const String8& keys, String8 *values) override;
95
96 // Returns audio input buffer size according to parameters passed.
97 status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
98
99 // Creates and opens the audio hardware output stream. The stream is closed
100 // by releasing all references to the returned object.
101 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
102 audio_output_flags_t flags, struct audio_config* config,
103 const char* address, sp<StreamOutHalInterface>* outStream) override;
104
105 // Creates and opens the audio hardware input stream. The stream is closed
106 // by releasing all references to the returned object.
107 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
108 struct audio_config* config, audio_input_flags_t flags,
109 const char* address, audio_source_t source,
110 audio_devices_t outputDevice, const char* outputDeviceAddress,
111 sp<StreamInHalInterface>* inStream) override;
112
113 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
114 status_t supportsAudioPatches(bool* supportsPatches) override;
115
116 // Creates an audio patch between several source and sink ports.
117 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
118 unsigned int num_sinks, const struct audio_port_config* sinks,
119 audio_patch_handle_t* patch) override;
120
121 // Releases an audio patch.
122 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
123
Mikhail Naganov31d46652023-01-10 18:29:25 +0000124 // Fills the list of supported attributes for a given audio port.
125 status_t getAudioPort(struct audio_port* port) override;
126
127 // Fills the list of supported attributes for a given audio port.
128 status_t getAudioPort(struct audio_port_v7 *port) override;
129
Shunkai Yao51202502022-12-12 06:11:46 +0000130 // Set audio port configuration.
131 status_t setAudioPortConfig(const struct audio_port_config* config) override;
132
133 // List microphones
134 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones);
135
136 status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
137
138 status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
139
140 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
141 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
142 __unused) override;
143
144 int32_t getAAudioMixerBurstCount() override;
145
146 int32_t getAAudioHardwareBurstMinUsec() override;
147
148 error::Result<audio_hw_sync_t> getHwAvSync() override;
149
150 status_t dump(int __unused, const Vector<String16>& __unused) override;
151
152 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
153
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100154 status_t getSoundDoseInterface(const std::string& module,
155 ::ndk::SpAIBinder* soundDoseBinder) override;
156
Shunkai Yao51202502022-12-12 06:11:46 +0000157 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000158 friend class sp<DeviceHalAidl>;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800159
160 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
161 wp<StreamOutHalInterfaceCallback> out;
162 wp<StreamOutHalInterfaceEventCallback> event;
163 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
164 };
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800165 using Patches = std::map<int32_t /*patch ID*/,
166 ::aidl::android::hardware::audio::core::AudioPatch>;
167 using PortConfigs = std::map<int32_t /*port config ID*/,
168 ::aidl::android::media::audio::common::AudioPortConfig>;
169 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
170 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000171
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800172 // Must not be constructed directly by clients.
173 DeviceHalAidl(
174 const std::string& instance,
175 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module)
176 : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {}
177
178 ~DeviceHalAidl() override = default;
179
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800180 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
181 const ::aidl::android::media::audio::common::AudioPort& p);
182 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
183 const ::aidl::android::media::audio::common::AudioPortConfig& p);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800184 status_t createPortConfig(
185 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
186 ::aidl::android::media::audio::common::AudioPortConfig* appliedPortConfig);
187 status_t findOrCreatePatch(
188 const std::set<int32_t>& sourcePortConfigIds,
189 const std::set<int32_t>& sinkPortConfigIds,
190 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
191 status_t findOrCreatePatch(
192 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
193 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
194 status_t findOrCreatePortConfig(
195 const ::aidl::android::media::audio::common::AudioDevice& device,
196 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
197 bool* created);
198 status_t findOrCreatePortConfig(
199 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800200 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800201 int32_t ioHandle,
202 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
203 status_t findOrCreatePortConfig(
204 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
205 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
206 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
207 const std::set<int32_t>& sinkPortConfigIds);
208 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
209 Ports::iterator findPort(
210 const ::aidl::android::media::audio::common::AudioConfig& config,
211 const ::aidl::android::media::audio::common::AudioIoFlags& flags);
212 PortConfigs::iterator findPortConfig(
213 const ::aidl::android::media::audio::common::AudioDevice& device);
214 PortConfigs::iterator findPortConfig(
215 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800216 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800217 int32_t ioHandle);
218 // Currently unused but may be useful for implementing setAudioPortConfig
219 // PortConfigs::iterator findPortConfig(
220 // const ::aidl::android::media::audio::common::AudioPortConfig& portConfig);
221 status_t prepareToOpenStream(
222 int32_t aidlHandle,
223 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
224 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
225 struct audio_config* config,
226 Cleanups* cleanups,
227 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800228 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
229 int32_t* nominalLatency);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800230 void resetPatch(int32_t patchId);
231 void resetPortConfig(int32_t portConfigId);
232
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800233 // CallbackBroker implementation
234 void clearCallbacks(void* cookie) override;
235 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
236 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
237 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
238 void setStreamOutEventCallback(void* cookie,
239 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
240 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
241 void* cookie) override;
242 void setStreamOutLatencyModeCallback(
243 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
244 // Implementation helpers.
245 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
246 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
247
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800248 const std::string mInstance;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000249 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100250 std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose>
251 mSoundDose = nullptr;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800252 Ports mPorts;
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800253 int32_t mDefaultInputPortId;
254 int32_t mDefaultOutputPortId;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800255 PortConfigs mPortConfigs;
256 Patches mPatches;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800257 std::map<audio_patch_handle_t, int32_t /*patch ID*/> mFwkHandles;
258 std::mutex mLock;
259 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
Shunkai Yao51202502022-12-12 06:11:46 +0000260};
261
262} // namespace android