blob: 0f34d7fea3b7ce22132397c50480b01163d8c415 [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>
Mikhail Naganovac9d4e72023-10-23 12:00:09 -070020#include <memory>
21#include <mutex>
22#include <string>
Mikhail Naganov5b1eed12023-01-25 11:29:11 -080023#include <vector>
24
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070025#include <aidl/android/media/audio/IHalAdapterVendorExtension.h>
Mikhail Naganov31d46652023-01-10 18:29:25 +000026#include <aidl/android/hardware/audio/core/BpModule.h>
Mikhail Naganovf56ce782023-01-25 11:29:11 -080027#include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h>
Mikhail Naganovdfd594e2023-02-08 16:59:41 -080028#include <android-base/thread_annotations.h>
Shunkai Yao51202502022-12-12 06:11:46 +000029#include <media/audiohal/DeviceHalInterface.h>
30#include <media/audiohal/EffectHalInterface.h>
31
Mikhail Naganovac9d4e72023-10-23 12:00:09 -070032#include "Cleanups.h"
Mikhail Naganov31d46652023-01-10 18:29:25 +000033#include "ConversionHelperAidl.h"
Mikhail Naganovac9d4e72023-10-23 12:00:09 -070034#include "Hal2AidlMapper.h"
Shunkai Yao51202502022-12-12 06:11:46 +000035
36namespace android {
37
Mikhail Naganovdfd594e2023-02-08 16:59:41 -080038class StreamOutHalInterfaceCallback;
39class StreamOutHalInterfaceEventCallback;
40class StreamOutHalInterfaceLatencyModeCallback;
41
42// The role of the broker is to connect AIDL callback interface implementations
43// with StreamOut callback implementations. Since AIDL requires all callbacks
44// to be provided upfront, while libaudiohal interfaces allow late registration,
45// there is a need to coordinate the matching process.
46class CallbackBroker : public virtual RefBase {
47 public:
48 virtual ~CallbackBroker() = default;
49 // The cookie is always the stream instance pointer. We don't use weak pointers to avoid extra
50 // costs on reference counting. The stream cleans up related entries on destruction. Since
51 // access to the callbacks map is synchronized, the possibility for pointer aliasing due to
52 // allocation of a new stream at the address of previously deleted stream is avoided.
53 virtual void clearCallbacks(void* cookie) = 0;
54 virtual sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) = 0;
55 virtual void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>&) = 0;
56 virtual sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) = 0;
57 virtual void setStreamOutEventCallback(void* cookie,
58 const sp<StreamOutHalInterfaceEventCallback>&) = 0;
59 virtual sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
60 void* cookie) = 0;
61 virtual void setStreamOutLatencyModeCallback(
62 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>&) = 0;
63};
64
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080065class MicrophoneInfoProvider : public virtual RefBase {
66 public:
67 using Info = std::vector<::aidl::android::media::audio::common::MicrophoneInfo>;
68 virtual ~MicrophoneInfoProvider() = default;
69 // Returns a nullptr if the HAL does not support microphone info retrieval.
70 virtual Info const* getMicrophoneInfo() = 0;
71};
72
Mikhail Naganovdfd594e2023-02-08 16:59:41 -080073class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl,
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080074 public CallbackBroker, public MicrophoneInfoProvider {
Shunkai Yao51202502022-12-12 06:11:46 +000075 public:
Mikhail Naganovf83b9742023-04-24 13:06:04 -070076 status_t getAudioPorts(std::vector<media::audio::common::AudioPort> *ports) override;
77
78 status_t getAudioRoutes(std::vector<media::AudioRoute> *routes) override;
79
Mikhail Naganov1fba38c2023-05-03 17:45:36 -070080 status_t getSupportedModes(std::vector<media::audio::common::AudioMode> *modes) override;
81
Shunkai Yao51202502022-12-12 06:11:46 +000082 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
83 status_t getSupportedDevices(uint32_t *devices) override;
84
85 // Check to see if the audio hardware interface has been initialized.
86 status_t initCheck() override;
87
88 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
89 status_t setVoiceVolume(float volume) override;
90
91 // Set the audio volume for all audio activities other than voice call.
92 status_t setMasterVolume(float volume) override;
93
94 // Get the current master volume value for the HAL.
95 status_t getMasterVolume(float *volume) override;
96
97 // Called when the audio mode changes.
98 status_t setMode(audio_mode_t mode) override;
99
100 // Muting control.
101 status_t setMicMute(bool state) override;
102
103 status_t getMicMute(bool* state) override;
104
105 status_t setMasterMute(bool state) override;
106
107 status_t getMasterMute(bool *state) override;
108
109 // Set global audio parameters.
110 status_t setParameters(const String8& kvPairs) override;
111
112 // Get global audio parameters.
113 status_t getParameters(const String8& keys, String8 *values) override;
114
115 // Returns audio input buffer size according to parameters passed.
Mikhail Naganovd5b643f2024-02-15 11:51:26 -0800116 status_t getInputBufferSize(struct audio_config* config, size_t* size) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000117
118 // Creates and opens the audio hardware output stream. The stream is closed
119 // by releasing all references to the returned object.
120 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
121 audio_output_flags_t flags, struct audio_config* config,
Haofan Wangbf44e092024-07-07 15:29:38 -0700122 const char* address, sp<StreamOutHalInterface>* outStream,
123 const std::vector<playback_track_metadata_v7_t>&
124 sourceMetadata = {}) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000125
126 // Creates and opens the audio hardware input stream. The stream is closed
127 // by releasing all references to the returned object.
128 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
129 struct audio_config* config, audio_input_flags_t flags,
130 const char* address, audio_source_t source,
131 audio_devices_t outputDevice, const char* outputDeviceAddress,
132 sp<StreamInHalInterface>* inStream) override;
133
134 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
135 status_t supportsAudioPatches(bool* supportsPatches) override;
136
137 // Creates an audio patch between several source and sink ports.
138 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
139 unsigned int num_sinks, const struct audio_port_config* sinks,
140 audio_patch_handle_t* patch) override;
141
142 // Releases an audio patch.
143 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
144
Mikhail Naganov31d46652023-01-10 18:29:25 +0000145 // Fills the list of supported attributes for a given audio port.
146 status_t getAudioPort(struct audio_port* port) override;
147
148 // Fills the list of supported attributes for a given audio port.
149 status_t getAudioPort(struct audio_port_v7 *port) override;
150
Shunkai Yao51202502022-12-12 06:11:46 +0000151 // Set audio port configuration.
152 status_t setAudioPortConfig(const struct audio_port_config* config) override;
153
154 // List microphones
Mikhail Naganove93a0862023-03-15 17:06:59 -0700155 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000156
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700157 status_t addDeviceEffect(
158 const struct audio_port_config *device, sp<EffectHalInterface> effect) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000159
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700160 status_t removeDeviceEffect(
161 const struct audio_port_config *device, sp<EffectHalInterface> effect) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000162
163 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
164 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
165 __unused) override;
166
167 int32_t getAAudioMixerBurstCount() override;
168
169 int32_t getAAudioHardwareBurstMinUsec() override;
170
171 error::Result<audio_hw_sync_t> getHwAvSync() override;
172
Eric Laurent7af6ee72023-06-29 11:44:54 +0200173 status_t supportsBluetoothVariableLatency(bool* supports __unused) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000174
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100175 status_t getSoundDoseInterface(const std::string& module,
176 ::ndk::SpAIBinder* soundDoseBinder) override;
177
jiabin872de702023-04-27 22:04:31 +0000178 status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override;
179
Mikhail Naganove93a0862023-03-15 17:06:59 -0700180 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override;
181
182 status_t setSimulateDeviceConnections(bool enabled) override;
183
jiabin12537fc2023-10-12 17:56:08 +0000184 status_t getAudioMixPort(const struct audio_port_v7* devicePort,
185 struct audio_port_v7* mixPort) override;
186
Mikhail Naganove93a0862023-03-15 17:06:59 -0700187 status_t dump(int __unused, const Vector<String16>& __unused) override;
188
Shunkai Yao51202502022-12-12 06:11:46 +0000189 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000190 friend class sp<DeviceHalAidl>;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800191
192 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
193 wp<StreamOutHalInterfaceCallback> out;
194 wp<StreamOutHalInterfaceEventCallback> event;
195 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
196 };
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800197 struct Microphones {
198 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
199 Status status = Status::UNKNOWN;
200 MicrophoneInfoProvider::Info info;
201 };
Mikhail Naganov31d46652023-01-10 18:29:25 +0000202
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800203 // Must not be constructed directly by clients.
204 DeviceHalAidl(
205 const std::string& instance,
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700206 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module,
207 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext);
Shunkai Yao51202502022-12-12 06:11:46 +0000208
Mikhail Naganov31d46652023-01-10 18:29:25 +0000209 ~DeviceHalAidl() override = default;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800210
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700211 status_t filterAndRetrieveBtA2dpParameters(AudioParameter &keys, AudioParameter *result);
Eric Laurent7e3c0832023-11-30 15:04:50 +0100212 status_t filterAndRetrieveBtLeParameters(AudioParameter &keys, AudioParameter *result);
Mikhail Naganovccc82112023-04-27 18:14:15 -0700213 status_t filterAndUpdateBtA2dpParameters(AudioParameter &parameters);
214 status_t filterAndUpdateBtHfpParameters(AudioParameter &parameters);
215 status_t filterAndUpdateBtLeParameters(AudioParameter &parameters);
216 status_t filterAndUpdateBtScoParameters(AudioParameter &parameters);
Mikhail Naganove92c34b2023-05-31 14:24:48 -0700217 status_t filterAndUpdateScreenParameters(AudioParameter &parameters);
Mikhail Naganovb9a81312023-07-18 13:55:34 -0700218 status_t filterAndUpdateTelephonyParameters(AudioParameter &parameters);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800219
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800220 // CallbackBroker implementation
221 void clearCallbacks(void* cookie) override;
222 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
223 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
224 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
225 void setStreamOutEventCallback(void* cookie,
226 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
227 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
228 void* cookie) override;
229 void setStreamOutLatencyModeCallback(
230 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
231 // Implementation helpers.
232 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
233 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
234
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800235 // MicrophoneInfoProvider implementation
236 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
237
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800238 const std::string mInstance;
239 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700240 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt;
Mikhail Naganov1fba38c2023-05-03 17:45:36 -0700241 const std::shared_ptr<::aidl::android::hardware::audio::core::ITelephony> mTelephony;
Mikhail Naganovccc82112023-04-27 18:14:15 -0700242 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetooth> mBluetooth;
243 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothA2dp> mBluetoothA2dp;
244 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothLe> mBluetoothLe;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700245 const std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose> mSoundDose;
246
Mikhail Naganov22578412024-08-16 16:50:34 -0700247 std::mutex mCallbacksLock;
248 // Use 'mCallbacksLock' only to implement exclusive access to 'mCallbacks'. Never hold it
249 // while making any calls.
250 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mCallbacksLock);
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800251 std::mutex mLock;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700252 std::set<audio_port_handle_t> mDeviceDisconnectionNotified GUARDED_BY(mLock);
253 Hal2AidlMapper mMapper GUARDED_BY(mLock);
254 LockedAccessor<Hal2AidlMapper> mMapperAccessor;
255 Microphones mMicrophones GUARDED_BY(mLock);
Shunkai Yao51202502022-12-12 06:11:46 +0000256};
257
258} // namespace android