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) 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 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 149 // Set audio port configuration. 150 status_t setAudioPortConfig(const struct audio_port_config* config) override; 151 152 // List microphones 153 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override; 154 155 status_t addDeviceEffect( 156 const struct audio_port_config *device, sp<EffectHalInterface> effect) override; 157 158 status_t removeDeviceEffect( 159 const struct audio_port_config *device, sp<EffectHalInterface> effect) override; 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 171 status_t supportsBluetoothVariableLatency(bool* supports __unused) override; 172 173 status_t getSoundDoseInterface(const std::string& module, 174 ::ndk::SpAIBinder* soundDoseBinder) override; 175 176 status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override; 177 178 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override; 179 180 status_t setSimulateDeviceConnections(bool enabled) override; 181 182 status_t getAudioMixPort(const struct audio_port_v7* devicePort, 183 struct audio_port_v7* mixPort) override; 184 185 status_t dump(int fd, const Vector<String16>& args) override; 186 187 private: 188 friend class sp<DeviceHalAidl>; 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 }; 195 struct Microphones { 196 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED }; 197 Status status = Status::UNKNOWN; 198 MicrophoneInfoProvider::Info info; 199 }; 200 201 // Must not be constructed directly by clients. 202 DeviceHalAidl( 203 const std::string& instance, 204 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module, 205 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext); 206 207 ~DeviceHalAidl() override = default; 208 209 status_t filterAndRetrieveBtA2dpParameters(AudioParameter &keys, AudioParameter *result); 210 status_t filterAndRetrieveBtLeParameters(AudioParameter &keys, AudioParameter *result); 211 status_t filterAndUpdateBtA2dpParameters(AudioParameter ¶meters); 212 status_t filterAndUpdateBtHfpParameters(AudioParameter ¶meters); 213 status_t filterAndUpdateBtLeParameters(AudioParameter ¶meters); 214 status_t filterAndUpdateBtScoParameters(AudioParameter ¶meters); 215 status_t filterAndUpdateScreenParameters(AudioParameter ¶meters); 216 status_t filterAndUpdateTelephonyParameters(AudioParameter ¶meters); 217 218 // CallbackBroker implementation 219 void clearCallbacks(void* cookie) override; 220 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override; 221 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override; 222 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override; 223 void setStreamOutEventCallback(void* cookie, 224 const sp<StreamOutHalInterfaceEventCallback>& cb) override; 225 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback( 226 void* cookie) override; 227 void setStreamOutLatencyModeCallback( 228 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override; 229 // Implementation helpers. 230 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field); 231 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb); 232 233 // MicrophoneInfoProvider implementation 234 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override; 235 236 const std::string mInstance; 237 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule; 238 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt; 239 const std::shared_ptr<::aidl::android::hardware::audio::core::ITelephony> mTelephony; 240 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetooth> mBluetooth; 241 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothA2dp> mBluetoothA2dp; 242 const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothLe> mBluetoothLe; 243 const std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose> mSoundDose; 244 245 std::mutex mLock; 246 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock); 247 std::set<audio_port_handle_t> mDeviceDisconnectionNotified GUARDED_BY(mLock); 248 Hal2AidlMapper mMapper GUARDED_BY(mLock); 249 LockedAccessor<Hal2AidlMapper> mMapperAccessor; 250 Microphones mMicrophones GUARDED_BY(mLock); 251 }; 252 253 } // namespace android 254