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> |
| 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 | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 24 | #include <android-base/thread_annotations.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 25 | #include <media/audiohal/DeviceHalInterface.h> |
| 26 | #include <media/audiohal/EffectHalInterface.h> |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame^] | 27 | #include <media/audiohal/StreamHalInterface.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 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 | dfd594e | 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 | dfd594e | 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 |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame^] | 142 | status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 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 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 158 | int32_t supportsBluetoothVariableLatency(bool* supports __unused) override; |
| 159 | |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame^] | 160 | status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override; |
| 161 | |
| 162 | status_t setSimulateDeviceConnections(bool enabled) override; |
| 163 | |
| 164 | status_t dump(int __unused, const Vector<String16>& __unused) override; |
| 165 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 166 | private: |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 167 | friend class sp<DeviceHalAidl>; |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 168 | |
| 169 | struct Callbacks { // No need to use `atomic_wp` because access is serialized. |
| 170 | wp<StreamOutHalInterfaceCallback> out; |
| 171 | wp<StreamOutHalInterfaceEventCallback> event; |
| 172 | wp<StreamOutHalInterfaceLatencyModeCallback> latency; |
| 173 | }; |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 174 | struct Microphones { |
| 175 | enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED }; |
| 176 | Status status = Status::UNKNOWN; |
| 177 | MicrophoneInfoProvider::Info info; |
| 178 | }; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 179 | using Patches = std::map<int32_t /*patch ID*/, |
| 180 | ::aidl::android::hardware::audio::core::AudioPatch>; |
| 181 | using PortConfigs = std::map<int32_t /*port config ID*/, |
| 182 | ::aidl::android::media::audio::common::AudioPortConfig>; |
| 183 | using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame^] | 184 | using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 185 | class Cleanups; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 186 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 187 | // Must not be constructed directly by clients. |
| 188 | DeviceHalAidl( |
| 189 | const std::string& instance, |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 190 | const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module) |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 191 | : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {} |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 192 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 193 | ~DeviceHalAidl() override = default; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 194 | |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 195 | bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device, |
| 196 | const ::aidl::android::media::audio::common::AudioPort& p); |
| 197 | bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device, |
| 198 | const ::aidl::android::media::audio::common::AudioPortConfig& p); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 199 | status_t createPortConfig( |
| 200 | const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 201 | PortConfigs::iterator* result); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 202 | status_t findOrCreatePatch( |
| 203 | const std::set<int32_t>& sourcePortConfigIds, |
| 204 | const std::set<int32_t>& sinkPortConfigIds, |
| 205 | ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created); |
| 206 | status_t findOrCreatePatch( |
| 207 | const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch, |
| 208 | ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created); |
| 209 | status_t findOrCreatePortConfig( |
| 210 | const ::aidl::android::media::audio::common::AudioDevice& device, |
| 211 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, |
| 212 | bool* created); |
| 213 | status_t findOrCreatePortConfig( |
| 214 | const ::aidl::android::media::audio::common::AudioConfig& config, |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 215 | const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 216 | int32_t ioHandle, |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 217 | ::aidl::android::media::audio::common::AudioSource aidlSource, |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 218 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created); |
| 219 | status_t findOrCreatePortConfig( |
| 220 | const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, |
| 221 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created); |
| 222 | Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds, |
| 223 | const std::set<int32_t>& sinkPortConfigIds); |
| 224 | Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device); |
| 225 | Ports::iterator findPort( |
| 226 | const ::aidl::android::media::audio::common::AudioConfig& config, |
| 227 | const ::aidl::android::media::audio::common::AudioIoFlags& flags); |
| 228 | PortConfigs::iterator findPortConfig( |
| 229 | const ::aidl::android::media::audio::common::AudioDevice& device); |
| 230 | PortConfigs::iterator findPortConfig( |
| 231 | const ::aidl::android::media::audio::common::AudioConfig& config, |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 232 | const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 233 | int32_t ioHandle); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 234 | status_t prepareToOpenStream( |
| 235 | int32_t aidlHandle, |
| 236 | const ::aidl::android::media::audio::common::AudioDevice& aidlDevice, |
| 237 | const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags, |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 238 | ::aidl::android::media::audio::common::AudioSource aidlSource, |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 239 | struct audio_config* config, |
| 240 | Cleanups* cleanups, |
| 241 | ::aidl::android::media::audio::common::AudioConfig* aidlConfig, |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 242 | ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig, |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame^] | 243 | ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 244 | void resetPatch(int32_t patchId); |
| 245 | void resetPortConfig(int32_t portConfigId); |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame^] | 246 | void resetUnusedPatches(); |
| 247 | void resetUnusedPatchesAndPortConfigs(); |
| 248 | void resetUnusedPortConfigs(); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 249 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 250 | // CallbackBroker implementation |
| 251 | void clearCallbacks(void* cookie) override; |
| 252 | sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override; |
| 253 | void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override; |
| 254 | sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override; |
| 255 | void setStreamOutEventCallback(void* cookie, |
| 256 | const sp<StreamOutHalInterfaceEventCallback>& cb) override; |
| 257 | sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback( |
| 258 | void* cookie) override; |
| 259 | void setStreamOutLatencyModeCallback( |
| 260 | void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override; |
| 261 | // Implementation helpers. |
| 262 | template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field); |
| 263 | template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb); |
| 264 | |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 265 | // MicrophoneInfoProvider implementation |
| 266 | MicrophoneInfoProvider::Info const* getMicrophoneInfo() override; |
| 267 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 268 | const std::string mInstance; |
| 269 | const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule; |
| 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 | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 273 | PortConfigs mPortConfigs; |
| 274 | Patches mPatches; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame^] | 275 | Streams mStreams; |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 276 | Microphones mMicrophones; |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 277 | std::mutex mLock; |
| 278 | std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | } // namespace android |