blob: 6f8afe50f79fd8cc73c8d82351a8a8a66ce31b67 [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,
122 const char* address, sp<StreamOutHalInterface>* outStream) override;
123
124 // Creates and opens the audio hardware input stream. The stream is closed
125 // by releasing all references to the returned object.
126 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
127 struct audio_config* config, audio_input_flags_t flags,
128 const char* address, audio_source_t source,
129 audio_devices_t outputDevice, const char* outputDeviceAddress,
130 sp<StreamInHalInterface>* inStream) override;
131
132 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
133 status_t supportsAudioPatches(bool* supportsPatches) override;
134
135 // Creates an audio patch between several source and sink ports.
136 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
137 unsigned int num_sinks, const struct audio_port_config* sinks,
138 audio_patch_handle_t* patch) override;
139
140 // Releases an audio patch.
141 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
142
Mikhail Naganov31d46652023-01-10 18:29:25 +0000143 // Fills the list of supported attributes for a given audio port.
144 status_t getAudioPort(struct audio_port* port) override;
145
146 // Fills the list of supported attributes for a given audio port.
147 status_t getAudioPort(struct audio_port_v7 *port) override;
148
Shunkai Yao51202502022-12-12 06:11:46 +0000149 // Set audio port configuration.
150 status_t setAudioPortConfig(const struct audio_port_config* config) override;
151
152 // List microphones
Mikhail Naganove93a0862023-03-15 17:06:59 -0700153 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000154
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700155 status_t addDeviceEffect(
156 const struct audio_port_config *device, sp<EffectHalInterface> effect) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000157
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700158 status_t removeDeviceEffect(
159 const struct audio_port_config *device, sp<EffectHalInterface> effect) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000160
161 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
162 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
163 __unused) override;
164
165 int32_t getAAudioMixerBurstCount() override;
166
167 int32_t getAAudioHardwareBurstMinUsec() override;
168
169 error::Result<audio_hw_sync_t> getHwAvSync() override;
170
Eric Laurent7af6ee72023-06-29 11:44:54 +0200171 status_t supportsBluetoothVariableLatency(bool* supports __unused) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000172
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100173 status_t getSoundDoseInterface(const std::string& module,
174 ::ndk::SpAIBinder* soundDoseBinder) override;
175
jiabin872de702023-04-27 22:04:31 +0000176 status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override;
177
Mikhail Naganove93a0862023-03-15 17:06:59 -0700178 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override;
179
180 status_t setSimulateDeviceConnections(bool enabled) override;
181
jiabin12537fc2023-10-12 17:56:08 +0000182 status_t getAudioMixPort(const struct audio_port_v7* devicePort,
183 struct audio_port_v7* mixPort) override;
184
Mikhail Naganove93a0862023-03-15 17:06:59 -0700185 status_t dump(int __unused, const Vector<String16>& __unused) override;
186
Shunkai Yao51202502022-12-12 06:11:46 +0000187 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000188 friend class sp<DeviceHalAidl>;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800189
190 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
191 wp<StreamOutHalInterfaceCallback> out;
192 wp<StreamOutHalInterfaceEventCallback> event;
193 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
194 };
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800195 struct Microphones {
196 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
197 Status status = Status::UNKNOWN;
198 MicrophoneInfoProvider::Info info;
199 };
Mikhail Naganov31d46652023-01-10 18:29:25 +0000200
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800201 // Must not be constructed directly by clients.
202 DeviceHalAidl(
203 const std::string& instance,
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700204 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module,
205 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext);
Shunkai Yao51202502022-12-12 06:11:46 +0000206
Mikhail Naganov31d46652023-01-10 18:29:25 +0000207 ~DeviceHalAidl() override = default;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800208
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700209 status_t filterAndRetrieveBtA2dpParameters(AudioParameter &keys, AudioParameter *result);
Mikhail Naganovccc82112023-04-27 18:14:15 -0700210 status_t filterAndUpdateBtA2dpParameters(AudioParameter &parameters);
211 status_t filterAndUpdateBtHfpParameters(AudioParameter &parameters);
212 status_t filterAndUpdateBtLeParameters(AudioParameter &parameters);
213 status_t filterAndUpdateBtScoParameters(AudioParameter &parameters);
Mikhail Naganove92c34b2023-05-31 14:24:48 -0700214 status_t filterAndUpdateScreenParameters(AudioParameter &parameters);
Mikhail Naganovb9a81312023-07-18 13:55:34 -0700215 status_t filterAndUpdateTelephonyParameters(AudioParameter &parameters);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800216
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800217 // CallbackBroker implementation
218 void clearCallbacks(void* cookie) override;
219 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
220 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
221 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
222 void setStreamOutEventCallback(void* cookie,
223 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
224 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
225 void* cookie) override;
226 void setStreamOutLatencyModeCallback(
227 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
228 // Implementation helpers.
229 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
230 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
231
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800232 // MicrophoneInfoProvider implementation
233 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
234
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800235 const std::string mInstance;
236 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700237 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt;
Mikhail Naganov1fba38c2023-05-03 17:45:36 -0700238 const std::shared_ptr<::aidl::android::hardware::audio::core::ITelephony> mTelephony;
Mikhail Naganovccc82112023-04-27 18:14:15 -0700239 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetooth> mBluetooth;
240 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothA2dp> mBluetoothA2dp;
241 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothLe> mBluetoothLe;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700242 const std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose> mSoundDose;
243
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800244 std::mutex mLock;
245 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700246 std::set<audio_port_handle_t> mDeviceDisconnectionNotified GUARDED_BY(mLock);
247 Hal2AidlMapper mMapper GUARDED_BY(mLock);
248 LockedAccessor<Hal2AidlMapper> mMapperAccessor;
249 Microphones mMicrophones GUARDED_BY(mLock);
Shunkai Yao51202502022-12-12 06:11:46 +0000250};
251
252} // namespace android