Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 1 | /* |
| 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 Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 19 | #include <map> |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | #include <string> |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 25 | #include <aidl/android/media/audio/IHalAdapterVendorExtension.h> |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 26 | #include <aidl/android/hardware/audio/core/BpModule.h> |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 27 | #include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h> |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 28 | #include <android-base/thread_annotations.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 29 | #include <media/audiohal/DeviceHalInterface.h> |
| 30 | #include <media/audiohal/EffectHalInterface.h> |
| 31 | |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 32 | #include "Cleanups.h" |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 33 | #include "ConversionHelperAidl.h" |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 34 | #include "Hal2AidlMapper.h" |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 38 | class StreamOutHalInterfaceCallback; |
| 39 | class StreamOutHalInterfaceEventCallback; |
| 40 | class 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. |
| 46 | class 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 Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 65 | class 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 Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 73 | class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl, |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 74 | public CallbackBroker, public MicrophoneInfoProvider { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 75 | public: |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 76 | status_t getAudioPorts(std::vector<media::audio::common::AudioPort> *ports) override; |
| 77 | |
| 78 | status_t getAudioRoutes(std::vector<media::AudioRoute> *routes) override; |
| 79 | |
Mikhail Naganov | 1fba38c | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 80 | status_t getSupportedModes(std::vector<media::audio::common::AudioMode> *modes) override; |
| 81 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 82 | // 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 Naganov | d5b643f | 2024-02-15 11:51:26 -0800 | [diff] [blame^] | 116 | status_t getInputBufferSize(struct audio_config* config, size_t* size) override; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 117 | |
| 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 Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 143 | // 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 Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 149 | // Set audio port configuration. |
| 150 | status_t setAudioPortConfig(const struct audio_port_config* config) override; |
| 151 | |
| 152 | // List microphones |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 153 | status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 154 | |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 155 | status_t addDeviceEffect( |
| 156 | const struct audio_port_config *device, sp<EffectHalInterface> effect) override; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 157 | |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 158 | status_t removeDeviceEffect( |
| 159 | const struct audio_port_config *device, sp<EffectHalInterface> effect) override; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 160 | |
| 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 Laurent | 7af6ee7 | 2023-06-29 11:44:54 +0200 | [diff] [blame] | 171 | status_t supportsBluetoothVariableLatency(bool* supports __unused) override; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 172 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 173 | status_t getSoundDoseInterface(const std::string& module, |
| 174 | ::ndk::SpAIBinder* soundDoseBinder) override; |
| 175 | |
jiabin | 872de70 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 176 | status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override; |
| 177 | |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 178 | status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override; |
| 179 | |
| 180 | status_t setSimulateDeviceConnections(bool enabled) override; |
| 181 | |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 182 | status_t getAudioMixPort(const struct audio_port_v7* devicePort, |
| 183 | struct audio_port_v7* mixPort) override; |
| 184 | |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 185 | status_t dump(int __unused, const Vector<String16>& __unused) override; |
| 186 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 187 | private: |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 188 | friend class sp<DeviceHalAidl>; |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 189 | |
| 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 Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 195 | struct Microphones { |
| 196 | enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED }; |
| 197 | Status status = Status::UNKNOWN; |
| 198 | MicrophoneInfoProvider::Info info; |
| 199 | }; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 200 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 201 | // Must not be constructed directly by clients. |
| 202 | DeviceHalAidl( |
| 203 | const std::string& instance, |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 204 | const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module, |
| 205 | const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 206 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 207 | ~DeviceHalAidl() override = default; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 208 | |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 209 | status_t filterAndRetrieveBtA2dpParameters(AudioParameter &keys, AudioParameter *result); |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 210 | status_t filterAndUpdateBtA2dpParameters(AudioParameter ¶meters); |
| 211 | status_t filterAndUpdateBtHfpParameters(AudioParameter ¶meters); |
| 212 | status_t filterAndUpdateBtLeParameters(AudioParameter ¶meters); |
| 213 | status_t filterAndUpdateBtScoParameters(AudioParameter ¶meters); |
Mikhail Naganov | e92c34b | 2023-05-31 14:24:48 -0700 | [diff] [blame] | 214 | status_t filterAndUpdateScreenParameters(AudioParameter ¶meters); |
Mikhail Naganov | b9a8131 | 2023-07-18 13:55:34 -0700 | [diff] [blame] | 215 | status_t filterAndUpdateTelephonyParameters(AudioParameter ¶meters); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 216 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 217 | // 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 Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 232 | // MicrophoneInfoProvider implementation |
| 233 | MicrophoneInfoProvider::Info const* getMicrophoneInfo() override; |
| 234 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 235 | const std::string mInstance; |
| 236 | const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule; |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 237 | const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt; |
Mikhail Naganov | 1fba38c | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 238 | const std::shared_ptr<::aidl::android::hardware::audio::core::ITelephony> mTelephony; |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 239 | 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 Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 242 | const std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose> mSoundDose; |
| 243 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 244 | std::mutex mLock; |
| 245 | std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 246 | 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 Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | } // namespace android |