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 | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <set> |
| 21 | #include <vector> |
| 22 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 23 | #include <aidl/android/hardware/audio/core/BpModule.h> |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 24 | #include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h> |
Mikhail Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 25 | #include <android-base/thread_annotations.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 26 | #include <media/audiohal/DeviceHalInterface.h> |
| 27 | #include <media/audiohal/EffectHalInterface.h> |
| 28 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 29 | #include "ConversionHelperAidl.h" |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
Mikhail Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 33 | class StreamOutHalInterfaceCallback; |
| 34 | class StreamOutHalInterfaceEventCallback; |
| 35 | class 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. |
| 41 | class 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 Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 60 | class 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 Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 68 | class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl, |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 69 | public CallbackBroker, public MicrophoneInfoProvider { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 70 | 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 Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 132 | // 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 Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 138 | // 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 Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 162 | status_t getSoundDoseInterface(const std::string& module, |
| 163 | ::ndk::SpAIBinder* soundDoseBinder) override; |
| 164 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 165 | private: |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 166 | friend class sp<DeviceHalAidl>; |
Mikhail Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 167 | |
| 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 Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 173 | struct Microphones { |
| 174 | enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED }; |
| 175 | Status status = Status::UNKNOWN; |
| 176 | MicrophoneInfoProvider::Info info; |
| 177 | }; |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 178 | 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 Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 184 | |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 185 | // 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 Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 193 | 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); |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 197 | status_t createPortConfig( |
| 198 | const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 199 | PortConfigs::iterator* result); |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 200 | 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 Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 213 | const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 214 | int32_t ioHandle, |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 215 | ::aidl::android::media::audio::common::AudioSource aidlSource, |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 216 | ::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 Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 230 | const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 231 | 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 Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 239 | ::aidl::android::media::audio::common::AudioSource aidlSource, |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 240 | struct audio_config* config, |
| 241 | Cleanups* cleanups, |
| 242 | ::aidl::android::media::audio::common::AudioConfig* aidlConfig, |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 243 | ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig, |
| 244 | int32_t* nominalLatency); |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 245 | void resetPatch(int32_t patchId); |
| 246 | void resetPortConfig(int32_t portConfigId); |
| 247 | |
Mikhail Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 248 | // 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 Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 263 | // MicrophoneInfoProvider implementation |
| 264 | MicrophoneInfoProvider::Info const* getMicrophoneInfo() override; |
| 265 | |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 266 | const std::string mInstance; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 267 | const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule; |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 268 | std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose> |
| 269 | mSoundDose = nullptr; |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 270 | Ports mPorts; |
Mikhail Naganov | 81bf4d0 | 2023-02-06 12:20:38 -0800 | [diff] [blame] | 271 | int32_t mDefaultInputPortId = -1; |
| 272 | int32_t mDefaultOutputPortId = -1; |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 273 | PortConfigs mPortConfigs; |
| 274 | Patches mPatches; |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 275 | Microphones mMicrophones; |
Mikhail Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 276 | std::mutex mLock; |
| 277 | std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | } // namespace android |