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 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <vector> 24 25 #include <aidl/android/media/audio/IHalAdapterVendorExtension.h> 26 #include <aidl/android/hardware/audio/core/BpModule.h> 27 #include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h> 28 #include <android-base/thread_annotations.h> 29 #include <media/audiohal/DeviceHalInterface.h> 30 #include <media/audiohal/EffectHalInterface.h> 31 32 #include "Cleanups.h" 33 #include "ConversionHelperAidl.h" 34 #include "Hal2AidlMapper.h" 35 36 namespace android { 37 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 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 73 class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl, 74 public CallbackBroker, public MicrophoneInfoProvider { 75 public: 76 status_t getAudioPorts(std::vector<media::audio::common::AudioPort> *ports) override; 77 78 status_t getAudioRoutes(std::vector<media::AudioRoute> *routes) override; 79 80 status_t getSupportedModes(std::vector<media::audio::common::AudioMode> *modes) override; 81 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. 116 status_t getInputBufferSize(struct audio_config* config, size_t* size) override; 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, 123 const std::vector<playback_track_metadata_v7_t>& 124 sourceMetadata = {}) override; 125 126 // Creates and opens the audio hardware input stream. The stream is closed 127 // by releasing all references to the returned object. 128 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices, 129 struct audio_config* config, audio_input_flags_t flags, 130 const char* address, audio_source_t source, 131 audio_devices_t outputDevice, const char* outputDeviceAddress, 132 sp<StreamInHalInterface>* inStream) override; 133 134 // Returns whether createAudioPatch and releaseAudioPatch operations are supported. 135 status_t supportsAudioPatches(bool* supportsPatches) override; 136 137 // Creates an audio patch between several source and sink ports. 138 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources, 139 unsigned int num_sinks, const struct audio_port_config* sinks, 140 audio_patch_handle_t* patch) override; 141 142 // Releases an audio patch. 143 status_t releaseAudioPatch(audio_patch_handle_t patch) override; 144 145 // Fills the list of supported attributes for a given audio port. 146 status_t getAudioPort(struct audio_port* port) override; 147 148 // Fills the list of supported attributes for a given audio port. 149 status_t getAudioPort(struct audio_port_v7 *port) override; 150 151 // Set audio port configuration. 152 status_t setAudioPortConfig(const struct audio_port_config* config) override; 153 154 // List microphones 155 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override; 156 157 status_t addDeviceEffect( 158 const struct audio_port_config *device, sp<EffectHalInterface> effect) override; 159 160 status_t removeDeviceEffect( 161 const struct audio_port_config *device, sp<EffectHalInterface> effect) override; 162 163 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused, 164 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos 165 __unused) override; 166 167 int32_t getAAudioMixerBurstCount() override; 168 169 int32_t getAAudioHardwareBurstMinUsec() override; 170 171 error::Result<audio_hw_sync_t> getHwAvSync() override; 172 173 status_t supportsBluetoothVariableLatency(bool* supports __unused) override; 174 175 status_t getSoundDoseInterface(const std::string& module, 176 ::ndk::SpAIBinder* soundDoseBinder) override; 177 178 status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override; 179 180 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override; 181 182 status_t setSimulateDeviceConnections(bool enabled) override; 183 184 status_t getAudioMixPort(const struct audio_port_v7* devicePort, 185 struct audio_port_v7* mixPort) override; 186 187 status_t dump(int fd, const Vector<String16>& args) override; 188 189 private: 190 friend class sp<DeviceHalAidl>; 191 192 struct Callbacks { // No need to use `atomic_wp` because access is serialized. 193 wp<StreamOutHalInterfaceCallback> out; 194 wp<StreamOutHalInterfaceEventCallback> event; 195 wp<StreamOutHalInterfaceLatencyModeCallback> latency; 196 }; 197 struct Microphones { 198 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED }; 199 Status status = Status::UNKNOWN; 200 MicrophoneInfoProvider::Info info; 201 }; 202 203 // Must not be constructed directly by clients. 204 DeviceHalAidl( 205 const std::string& instance, 206 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module, 207 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext); 208 209 ~DeviceHalAidl() override = default; 210 211 status_t filterAndRetrieveBtA2dpParameters(AudioParameter &keys, AudioParameter *result); 212 status_t filterAndRetrieveBtLeParameters(AudioParameter &keys, AudioParameter *result); 213 status_t filterAndUpdateBtA2dpParameters(AudioParameter ¶meters); 214 status_t filterAndUpdateBtHfpParameters(AudioParameter ¶meters); 215 status_t filterAndUpdateBtLeParameters(AudioParameter ¶meters); 216 status_t filterAndUpdateBtScoParameters(AudioParameter ¶meters); 217 status_t filterAndUpdateScreenParameters(AudioParameter ¶meters); 218 status_t filterAndUpdateTelephonyParameters(AudioParameter ¶meters); 219 220 // CallbackBroker implementation 221 void clearCallbacks(void* cookie) override; 222 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override; 223 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override; 224 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override; 225 void setStreamOutEventCallback(void* cookie, 226 const sp<StreamOutHalInterfaceEventCallback>& cb) override; 227 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback( 228 void* cookie) override; 229 void setStreamOutLatencyModeCallback( 230 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override; 231 // Implementation helpers. 232 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field); 233 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb); 234 235 // MicrophoneInfoProvider implementation 236 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override; 237 238 // See below, the lock is only used to serialize calling into the interface. isModuleInitialized()239 bool isModuleInitialized() const NO_THREAD_SAFETY_ANALYSIS { return mModule != nullptr; } isTelephonyInitialized()240 bool isTelephonyInitialized() const NO_THREAD_SAFETY_ANALYSIS { return mTelephony != nullptr; } 241 242 mutable std::mutex mLock; 243 // GUARDED_BY is used to prevent concurrent calls into these interfaces from multiple threads. 244 // There is no requirement for IModule and its helper interfaces implementations 245 // to be thread-safe. 246 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule 247 GUARDED_BY(mLock); 248 const std::shared_ptr<::aidl::android::hardware::audio::core::ITelephony> mTelephony 249 GUARDED_BY(mLock); 250 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetooth> mBluetooth 251 GUARDED_BY(mLock); 252 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothA2dp> mBluetoothA2dp 253 GUARDED_BY(mLock); 254 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothLe> mBluetoothLe 255 GUARDED_BY(mLock); 256 257 const std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose> mSoundDose; 258 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt; 259 260 std::mutex mCallbacksLock; 261 // Use 'mCallbacksLock' only to implement exclusive access to 'mCallbacks'. Never hold it 262 // while making any calls. 263 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mCallbacksLock); 264 std::set<audio_port_handle_t> mDeviceDisconnectionNotified GUARDED_BY(mLock); 265 Hal2AidlMapper mMapper GUARDED_BY(mLock); 266 LockedAccessor<Hal2AidlMapper> mMapperAccessor; 267 Microphones mMicrophones GUARDED_BY(mLock); 268 }; 269 270 } // namespace android 271