• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <set>
21 #include <vector>
22 
23 #include <aidl/android/media/audio/IHalAdapterVendorExtension.h>
24 #include <aidl/android/hardware/audio/core/BpModule.h>
25 #include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h>
26 #include <android-base/thread_annotations.h>
27 #include <media/audiohal/DeviceHalInterface.h>
28 #include <media/audiohal/EffectHalInterface.h>
29 #include <media/audiohal/StreamHalInterface.h>
30 
31 #include "ConversionHelperAidl.h"
32 
33 namespace android {
34 
35 class StreamOutHalInterfaceCallback;
36 class StreamOutHalInterfaceEventCallback;
37 class StreamOutHalInterfaceLatencyModeCallback;
38 
39 // The role of the broker is to connect AIDL callback interface implementations
40 // with StreamOut callback implementations. Since AIDL requires all callbacks
41 // to be provided upfront, while libaudiohal interfaces allow late registration,
42 // there is a need to coordinate the matching process.
43 class CallbackBroker : public virtual RefBase {
44   public:
45     virtual ~CallbackBroker() = default;
46     // The cookie is always the stream instance pointer. We don't use weak pointers to avoid extra
47     // costs on reference counting. The stream cleans up related entries on destruction. Since
48     // access to the callbacks map is synchronized, the possibility for pointer aliasing due to
49     // allocation of a new stream at the address of previously deleted stream is avoided.
50     virtual void clearCallbacks(void* cookie) = 0;
51     virtual sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) = 0;
52     virtual void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>&) = 0;
53     virtual sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) = 0;
54     virtual void setStreamOutEventCallback(void* cookie,
55             const sp<StreamOutHalInterfaceEventCallback>&) = 0;
56     virtual sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
57             void* cookie) = 0;
58     virtual void setStreamOutLatencyModeCallback(
59             void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>&) = 0;
60 };
61 
62 class MicrophoneInfoProvider : public virtual RefBase {
63   public:
64     using Info = std::vector<::aidl::android::media::audio::common::MicrophoneInfo>;
65     virtual ~MicrophoneInfoProvider() = default;
66     // Returns a nullptr if the HAL does not support microphone info retrieval.
67     virtual Info const* getMicrophoneInfo() = 0;
68 };
69 
70 class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl,
71                       public CallbackBroker, public MicrophoneInfoProvider {
72   public:
73     status_t getAudioPorts(std::vector<media::audio::common::AudioPort> *ports) override;
74 
75     status_t getAudioRoutes(std::vector<media::AudioRoute> *routes) override;
76 
77     status_t getSupportedModes(std::vector<media::audio::common::AudioMode> *modes) override;
78 
79     // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
80     status_t getSupportedDevices(uint32_t *devices) override;
81 
82     // Check to see if the audio hardware interface has been initialized.
83     status_t initCheck() override;
84 
85     // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
86     status_t setVoiceVolume(float volume) override;
87 
88     // Set the audio volume for all audio activities other than voice call.
89     status_t setMasterVolume(float volume) override;
90 
91     // Get the current master volume value for the HAL.
92     status_t getMasterVolume(float *volume) override;
93 
94     // Called when the audio mode changes.
95     status_t setMode(audio_mode_t mode) override;
96 
97     // Muting control.
98     status_t setMicMute(bool state) override;
99 
100     status_t getMicMute(bool* state) override;
101 
102     status_t setMasterMute(bool state) override;
103 
104     status_t getMasterMute(bool *state) override;
105 
106     // Set global audio parameters.
107     status_t setParameters(const String8& kvPairs) override;
108 
109     // Get global audio parameters.
110     status_t getParameters(const String8& keys, String8 *values) override;
111 
112     // Returns audio input buffer size according to parameters passed.
113     status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
114 
115     // Creates and opens the audio hardware output stream. The stream is closed
116     // by releasing all references to the returned object.
117     status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
118                               audio_output_flags_t flags, struct audio_config* config,
119                               const char* address, sp<StreamOutHalInterface>* outStream) override;
120 
121     // Creates and opens the audio hardware input stream. The stream is closed
122     // by releasing all references to the returned object.
123     status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
124                              struct audio_config* config, audio_input_flags_t flags,
125                              const char* address, audio_source_t source,
126                              audio_devices_t outputDevice, const char* outputDeviceAddress,
127                              sp<StreamInHalInterface>* inStream) override;
128 
129     // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
130     status_t supportsAudioPatches(bool* supportsPatches) override;
131 
132     // Creates an audio patch between several source and sink ports.
133     status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
134                               unsigned int num_sinks, const struct audio_port_config* sinks,
135                               audio_patch_handle_t* patch) override;
136 
137     // Releases an audio patch.
138     status_t releaseAudioPatch(audio_patch_handle_t patch) override;
139 
140     // Fills the list of supported attributes for a given audio port.
141     status_t getAudioPort(struct audio_port* port) override;
142 
143     // Fills the list of supported attributes for a given audio port.
144     status_t getAudioPort(struct audio_port_v7 *port) override;
145 
146     // Set audio port configuration.
147     status_t setAudioPortConfig(const struct audio_port_config* config) override;
148 
149     // List microphones
150     status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
151 
152     status_t addDeviceEffect(
153             const struct audio_port_config *device, sp<EffectHalInterface> effect) override;
154 
155     status_t removeDeviceEffect(
156             const struct audio_port_config *device, sp<EffectHalInterface> effect) override;
157 
158     status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
159                                 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
160                                         __unused) override;
161 
162     int32_t getAAudioMixerBurstCount() override;
163 
164     int32_t getAAudioHardwareBurstMinUsec() override;
165 
166     error::Result<audio_hw_sync_t> getHwAvSync() override;
167 
168     status_t supportsBluetoothVariableLatency(bool* supports __unused) override;
169 
170     status_t getSoundDoseInterface(const std::string& module,
171                                    ::ndk::SpAIBinder* soundDoseBinder) override;
172 
173     status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override;
174 
175     status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override;
176 
177     status_t setSimulateDeviceConnections(bool enabled) override;
178 
179     status_t dump(int __unused, const Vector<String16>& __unused) override;
180 
181   private:
182     friend class sp<DeviceHalAidl>;
183 
184     struct Callbacks {  // No need to use `atomic_wp` because access is serialized.
185         wp<StreamOutHalInterfaceCallback> out;
186         wp<StreamOutHalInterfaceEventCallback> event;
187         wp<StreamOutHalInterfaceLatencyModeCallback> latency;
188     };
189     struct Microphones {
190         enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
191         Status status = Status::UNKNOWN;
192         MicrophoneInfoProvider::Info info;
193     };
194     // IDs of ports for connected external devices, and whether they are held by streams.
195     using ConnectedPorts = std::map<int32_t /*port ID*/, bool>;
196     using Patches = std::map<int32_t /*patch ID*/,
197             ::aidl::android::hardware::audio::core::AudioPatch>;
198     using PortConfigs = std::map<int32_t /*port config ID*/,
199             ::aidl::android::media::audio::common::AudioPortConfig>;
200     using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
201     using Routes = std::vector<::aidl::android::hardware::audio::core::AudioRoute>;
202     // Answers the question "whether portID 'first' is reachable from portID 'second'?"
203     // It's not a map because both portIDs are known. The matrix is symmetric.
204     using RoutingMatrix = std::set<std::pair<int32_t, int32_t>>;
205     using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>;
206     class Cleanups;
207 
208     // Must not be constructed directly by clients.
209     DeviceHalAidl(
210             const std::string& instance,
211             const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module,
212             const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext);
213 
214     ~DeviceHalAidl() override = default;
215 
216     bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
217             const ::aidl::android::media::audio::common::AudioPort& p);
218     bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
219             const ::aidl::android::media::audio::common::AudioPortConfig& p);
220     status_t createOrUpdatePortConfig(
221             const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
222             PortConfigs::iterator* result, bool *created);
223     status_t filterAndRetrieveBtA2dpParameters(AudioParameter &keys, AudioParameter *result);
224     status_t filterAndUpdateBtA2dpParameters(AudioParameter &parameters);
225     status_t filterAndUpdateBtHfpParameters(AudioParameter &parameters);
226     status_t filterAndUpdateBtLeParameters(AudioParameter &parameters);
227     status_t filterAndUpdateBtScoParameters(AudioParameter &parameters);
228     status_t filterAndUpdateScreenParameters(AudioParameter &parameters);
229     status_t filterAndUpdateTelephonyParameters(AudioParameter &parameters);
230     status_t findOrCreatePatch(
231         const std::set<int32_t>& sourcePortConfigIds,
232         const std::set<int32_t>& sinkPortConfigIds,
233         ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
234     status_t findOrCreatePatch(
235         const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
236         ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
237     status_t findOrCreatePortConfig(
238             const ::aidl::android::media::audio::common::AudioDevice& device,
239             const ::aidl::android::media::audio::common::AudioConfig* config,
240             ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
241             bool* created);
242     status_t findOrCreatePortConfig(
243             const ::aidl::android::media::audio::common::AudioConfig& config,
244             const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
245             int32_t ioHandle,
246             ::aidl::android::media::audio::common::AudioSource aidlSource,
247             const std::set<int32_t>& destinationPortIds,
248             ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
249     status_t findOrCreatePortConfig(
250         const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
251         const std::set<int32_t>& destinationPortIds,
252         ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
253     Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
254             const std::set<int32_t>& sinkPortConfigIds);
255     Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
256     Ports::iterator findPort(
257             const ::aidl::android::media::audio::common::AudioConfig& config,
258             const ::aidl::android::media::audio::common::AudioIoFlags& flags,
259             const std::set<int32_t>& destinationPortIds);
260     PortConfigs::iterator findPortConfig(
261             const ::aidl::android::media::audio::common::AudioDevice& device);
262     PortConfigs::iterator findPortConfig(
263             const ::aidl::android::media::audio::common::AudioConfig& config,
264             const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
265             int32_t ioHandle);
266     bool isPortHeldByAStream(int32_t portId);
267     status_t prepareToOpenStream(
268         int32_t aidlHandle,
269         const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
270         const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
271         ::aidl::android::media::audio::common::AudioSource aidlSource,
272         struct audio_config* config,
273         Cleanups* cleanups,
274         ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
275         ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
276         ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch);
277     void resetPatch(int32_t patchId);
278     void resetPortConfig(int32_t portConfigId);
279     void resetUnusedPatches();
280     void resetUnusedPatchesAndPortConfigs();
281     void resetUnusedPortConfigs();
282     status_t updateRoutes();
283 
284     // CallbackBroker implementation
285     void clearCallbacks(void* cookie) override;
286     sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
287     void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
288     sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
289     void setStreamOutEventCallback(void* cookie,
290             const sp<StreamOutHalInterfaceEventCallback>& cb) override;
291     sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
292             void* cookie) override;
293     void setStreamOutLatencyModeCallback(
294             void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
295     // Implementation helpers.
296     template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
297     template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
298 
299     // MicrophoneInfoProvider implementation
300     MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
301 
302     const std::string mInstance;
303     const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
304     const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt;
305     const std::shared_ptr<::aidl::android::hardware::audio::core::ITelephony> mTelephony;
306     const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetooth> mBluetooth;
307     const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothA2dp> mBluetoothA2dp;
308     const std::shared_ptr<::aidl::android::hardware::audio::core::IBluetoothLe> mBluetoothLe;
309     std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose>
310         mSoundDose = nullptr;
311     Ports mPorts;
312     int32_t mDefaultInputPortId = -1;
313     int32_t mDefaultOutputPortId = -1;
314     PortConfigs mPortConfigs;
315     std::set<int32_t> mInitialPortConfigIds;
316     Patches mPatches;
317     Routes mRoutes;
318     RoutingMatrix mRoutingMatrix;
319     Streams mStreams;
320     Microphones mMicrophones;
321     std::mutex mLock;
322     std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
323     std::set<audio_port_handle_t> mDeviceDisconnectionNotified;
324     ConnectedPorts mConnectedPorts;
325 };
326 
327 } // namespace android
328