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> |
| 27 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 28 | #include "ConversionHelperAidl.h" |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 32 | class StreamOutHalInterfaceCallback; |
| 33 | class StreamOutHalInterfaceEventCallback; |
| 34 | class StreamOutHalInterfaceLatencyModeCallback; |
| 35 | |
| 36 | // The role of the broker is to connect AIDL callback interface implementations |
| 37 | // with StreamOut callback implementations. Since AIDL requires all callbacks |
| 38 | // to be provided upfront, while libaudiohal interfaces allow late registration, |
| 39 | // there is a need to coordinate the matching process. |
| 40 | class CallbackBroker : public virtual RefBase { |
| 41 | public: |
| 42 | virtual ~CallbackBroker() = default; |
| 43 | // The cookie is always the stream instance pointer. We don't use weak pointers to avoid extra |
| 44 | // costs on reference counting. The stream cleans up related entries on destruction. Since |
| 45 | // access to the callbacks map is synchronized, the possibility for pointer aliasing due to |
| 46 | // allocation of a new stream at the address of previously deleted stream is avoided. |
| 47 | virtual void clearCallbacks(void* cookie) = 0; |
| 48 | virtual sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) = 0; |
| 49 | virtual void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>&) = 0; |
| 50 | virtual sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) = 0; |
| 51 | virtual void setStreamOutEventCallback(void* cookie, |
| 52 | const sp<StreamOutHalInterfaceEventCallback>&) = 0; |
| 53 | virtual sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback( |
| 54 | void* cookie) = 0; |
| 55 | virtual void setStreamOutLatencyModeCallback( |
| 56 | void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>&) = 0; |
| 57 | }; |
| 58 | |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame^] | 59 | class MicrophoneInfoProvider : public virtual RefBase { |
| 60 | public: |
| 61 | using Info = std::vector<::aidl::android::media::audio::common::MicrophoneInfo>; |
| 62 | virtual ~MicrophoneInfoProvider() = default; |
| 63 | // Returns a nullptr if the HAL does not support microphone info retrieval. |
| 64 | virtual Info const* getMicrophoneInfo() = 0; |
| 65 | }; |
| 66 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 67 | class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl, |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame^] | 68 | public CallbackBroker, public MicrophoneInfoProvider { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 69 | public: |
| 70 | // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t. |
| 71 | status_t getSupportedDevices(uint32_t *devices) override; |
| 72 | |
| 73 | // Check to see if the audio hardware interface has been initialized. |
| 74 | status_t initCheck() override; |
| 75 | |
| 76 | // Set the audio volume of a voice call. Range is between 0.0 and 1.0. |
| 77 | status_t setVoiceVolume(float volume) override; |
| 78 | |
| 79 | // Set the audio volume for all audio activities other than voice call. |
| 80 | status_t setMasterVolume(float volume) override; |
| 81 | |
| 82 | // Get the current master volume value for the HAL. |
| 83 | status_t getMasterVolume(float *volume) override; |
| 84 | |
| 85 | // Called when the audio mode changes. |
| 86 | status_t setMode(audio_mode_t mode) override; |
| 87 | |
| 88 | // Muting control. |
| 89 | status_t setMicMute(bool state) override; |
| 90 | |
| 91 | status_t getMicMute(bool* state) override; |
| 92 | |
| 93 | status_t setMasterMute(bool state) override; |
| 94 | |
| 95 | status_t getMasterMute(bool *state) override; |
| 96 | |
| 97 | // Set global audio parameters. |
| 98 | status_t setParameters(const String8& kvPairs) override; |
| 99 | |
| 100 | // Get global audio parameters. |
| 101 | status_t getParameters(const String8& keys, String8 *values) override; |
| 102 | |
| 103 | // Returns audio input buffer size according to parameters passed. |
| 104 | status_t getInputBufferSize(const struct audio_config* config, size_t* size) override; |
| 105 | |
| 106 | // Creates and opens the audio hardware output stream. The stream is closed |
| 107 | // by releasing all references to the returned object. |
| 108 | status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices, |
| 109 | audio_output_flags_t flags, struct audio_config* config, |
| 110 | const char* address, sp<StreamOutHalInterface>* outStream) override; |
| 111 | |
| 112 | // Creates and opens the audio hardware input stream. The stream is closed |
| 113 | // by releasing all references to the returned object. |
| 114 | status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices, |
| 115 | struct audio_config* config, audio_input_flags_t flags, |
| 116 | const char* address, audio_source_t source, |
| 117 | audio_devices_t outputDevice, const char* outputDeviceAddress, |
| 118 | sp<StreamInHalInterface>* inStream) override; |
| 119 | |
| 120 | // Returns whether createAudioPatch and releaseAudioPatch operations are supported. |
| 121 | status_t supportsAudioPatches(bool* supportsPatches) override; |
| 122 | |
| 123 | // Creates an audio patch between several source and sink ports. |
| 124 | status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources, |
| 125 | unsigned int num_sinks, const struct audio_port_config* sinks, |
| 126 | audio_patch_handle_t* patch) override; |
| 127 | |
| 128 | // Releases an audio patch. |
| 129 | status_t releaseAudioPatch(audio_patch_handle_t patch) override; |
| 130 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 131 | // Fills the list of supported attributes for a given audio port. |
| 132 | status_t getAudioPort(struct audio_port* port) override; |
| 133 | |
| 134 | // Fills the list of supported attributes for a given audio port. |
| 135 | status_t getAudioPort(struct audio_port_v7 *port) override; |
| 136 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 137 | // Set audio port configuration. |
| 138 | status_t setAudioPortConfig(const struct audio_port_config* config) override; |
| 139 | |
| 140 | // List microphones |
| 141 | status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones); |
| 142 | |
| 143 | status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override; |
| 144 | |
| 145 | status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override; |
| 146 | |
| 147 | status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused, |
| 148 | std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos |
| 149 | __unused) override; |
| 150 | |
| 151 | int32_t getAAudioMixerBurstCount() override; |
| 152 | |
| 153 | int32_t getAAudioHardwareBurstMinUsec() override; |
| 154 | |
| 155 | error::Result<audio_hw_sync_t> getHwAvSync() override; |
| 156 | |
| 157 | status_t dump(int __unused, const Vector<String16>& __unused) override; |
| 158 | |
| 159 | int32_t supportsBluetoothVariableLatency(bool* supports __unused) override; |
| 160 | |
| 161 | private: |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 162 | friend class sp<DeviceHalAidl>; |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 163 | |
| 164 | struct Callbacks { // No need to use `atomic_wp` because access is serialized. |
| 165 | wp<StreamOutHalInterfaceCallback> out; |
| 166 | wp<StreamOutHalInterfaceEventCallback> event; |
| 167 | wp<StreamOutHalInterfaceLatencyModeCallback> latency; |
| 168 | }; |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame^] | 169 | struct Microphones { |
| 170 | enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED }; |
| 171 | Status status = Status::UNKNOWN; |
| 172 | MicrophoneInfoProvider::Info info; |
| 173 | }; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 174 | using Patches = std::map<int32_t /*patch ID*/, |
| 175 | ::aidl::android::hardware::audio::core::AudioPatch>; |
| 176 | using PortConfigs = std::map<int32_t /*port config ID*/, |
| 177 | ::aidl::android::media::audio::common::AudioPortConfig>; |
| 178 | using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>; |
| 179 | class Cleanups; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 180 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 181 | // Must not be constructed directly by clients. |
| 182 | DeviceHalAidl( |
| 183 | const std::string& instance, |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 184 | const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module) |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 185 | : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {} |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 186 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 187 | ~DeviceHalAidl() override = default; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 188 | |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 189 | bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device, |
| 190 | const ::aidl::android::media::audio::common::AudioPort& p); |
| 191 | bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device, |
| 192 | const ::aidl::android::media::audio::common::AudioPortConfig& p); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 193 | status_t createPortConfig( |
| 194 | const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 195 | PortConfigs::iterator* result); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 196 | status_t findOrCreatePatch( |
| 197 | const std::set<int32_t>& sourcePortConfigIds, |
| 198 | const std::set<int32_t>& sinkPortConfigIds, |
| 199 | ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created); |
| 200 | status_t findOrCreatePatch( |
| 201 | const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch, |
| 202 | ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created); |
| 203 | status_t findOrCreatePortConfig( |
| 204 | const ::aidl::android::media::audio::common::AudioDevice& device, |
| 205 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, |
| 206 | bool* created); |
| 207 | status_t findOrCreatePortConfig( |
| 208 | const ::aidl::android::media::audio::common::AudioConfig& config, |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 209 | const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 210 | int32_t ioHandle, |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 211 | ::aidl::android::media::audio::common::AudioSource aidlSource, |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 212 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created); |
| 213 | status_t findOrCreatePortConfig( |
| 214 | const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig, |
| 215 | ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created); |
| 216 | Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds, |
| 217 | const std::set<int32_t>& sinkPortConfigIds); |
| 218 | Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device); |
| 219 | Ports::iterator findPort( |
| 220 | const ::aidl::android::media::audio::common::AudioConfig& config, |
| 221 | const ::aidl::android::media::audio::common::AudioIoFlags& flags); |
| 222 | PortConfigs::iterator findPortConfig( |
| 223 | const ::aidl::android::media::audio::common::AudioDevice& device); |
| 224 | PortConfigs::iterator findPortConfig( |
| 225 | const ::aidl::android::media::audio::common::AudioConfig& config, |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 226 | const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags, |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 227 | int32_t ioHandle); |
| 228 | // Currently unused but may be useful for implementing setAudioPortConfig |
| 229 | // PortConfigs::iterator findPortConfig( |
| 230 | // const ::aidl::android::media::audio::common::AudioPortConfig& portConfig); |
| 231 | status_t prepareToOpenStream( |
| 232 | int32_t aidlHandle, |
| 233 | const ::aidl::android::media::audio::common::AudioDevice& aidlDevice, |
| 234 | const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags, |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 235 | ::aidl::android::media::audio::common::AudioSource aidlSource, |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 236 | struct audio_config* config, |
| 237 | Cleanups* cleanups, |
| 238 | ::aidl::android::media::audio::common::AudioConfig* aidlConfig, |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 239 | ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig, |
| 240 | int32_t* nominalLatency); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 241 | void resetPatch(int32_t patchId); |
| 242 | void resetPortConfig(int32_t portConfigId); |
| 243 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 244 | // CallbackBroker implementation |
| 245 | void clearCallbacks(void* cookie) override; |
| 246 | sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override; |
| 247 | void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override; |
| 248 | sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override; |
| 249 | void setStreamOutEventCallback(void* cookie, |
| 250 | const sp<StreamOutHalInterfaceEventCallback>& cb) override; |
| 251 | sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback( |
| 252 | void* cookie) override; |
| 253 | void setStreamOutLatencyModeCallback( |
| 254 | void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override; |
| 255 | // Implementation helpers. |
| 256 | template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field); |
| 257 | template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb); |
| 258 | |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame^] | 259 | // MicrophoneInfoProvider implementation |
| 260 | MicrophoneInfoProvider::Info const* getMicrophoneInfo() override; |
| 261 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 262 | const std::string mInstance; |
| 263 | const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule; |
| 264 | Ports mPorts; |
Mikhail Naganov | 81bf4d0 | 2023-02-06 12:20:38 -0800 | [diff] [blame] | 265 | int32_t mDefaultInputPortId = -1; |
| 266 | int32_t mDefaultOutputPortId = -1; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 267 | PortConfigs mPortConfigs; |
| 268 | Patches mPatches; |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame^] | 269 | Microphones mMicrophones; |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 270 | std::mutex mLock; |
| 271 | std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | } // namespace android |