blob: b2bba1f89b299a9206a6e9b55d1beedfc9062f65 [file] [log] [blame]
Shunkai Yao51202502022-12-12 06:11:46 +00001/*
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 Naganovf56ce782023-01-25 11:29:11 -080019#include <map>
20#include <set>
21#include <vector>
22
Mikhail Naganov31d46652023-01-10 18:29:25 +000023#include <aidl/android/hardware/audio/core/BpModule.h>
Mikhail Naganovf56ce782023-01-25 11:29:11 -080024#include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h>
Mikhail Naganovb0c55252023-02-08 16:59:41 -080025#include <android-base/thread_annotations.h>
Shunkai Yao51202502022-12-12 06:11:46 +000026#include <media/audiohal/DeviceHalInterface.h>
27#include <media/audiohal/EffectHalInterface.h>
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -070028#include <media/audiohal/StreamHalInterface.h>
Shunkai Yao51202502022-12-12 06:11:46 +000029
Mikhail Naganov31d46652023-01-10 18:29:25 +000030#include "ConversionHelperAidl.h"
Shunkai Yao51202502022-12-12 06:11:46 +000031
32namespace android {
33
Mikhail Naganovb0c55252023-02-08 16:59:41 -080034class StreamOutHalInterfaceCallback;
35class StreamOutHalInterfaceEventCallback;
36class StreamOutHalInterfaceLatencyModeCallback;
37
38// The role of the broker is to connect AIDL callback interface implementations
39// with StreamOut callback implementations. Since AIDL requires all callbacks
40// to be provided upfront, while libaudiohal interfaces allow late registration,
41// there is a need to coordinate the matching process.
42class CallbackBroker : public virtual RefBase {
43 public:
44 virtual ~CallbackBroker() = default;
45 // The cookie is always the stream instance pointer. We don't use weak pointers to avoid extra
46 // costs on reference counting. The stream cleans up related entries on destruction. Since
47 // access to the callbacks map is synchronized, the possibility for pointer aliasing due to
48 // allocation of a new stream at the address of previously deleted stream is avoided.
49 virtual void clearCallbacks(void* cookie) = 0;
50 virtual sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) = 0;
51 virtual void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>&) = 0;
52 virtual sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) = 0;
53 virtual void setStreamOutEventCallback(void* cookie,
54 const sp<StreamOutHalInterfaceEventCallback>&) = 0;
55 virtual sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
56 void* cookie) = 0;
57 virtual void setStreamOutLatencyModeCallback(
58 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>&) = 0;
59};
60
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080061class MicrophoneInfoProvider : public virtual RefBase {
62 public:
63 using Info = std::vector<::aidl::android::media::audio::common::MicrophoneInfo>;
64 virtual ~MicrophoneInfoProvider() = default;
65 // Returns a nullptr if the HAL does not support microphone info retrieval.
66 virtual Info const* getMicrophoneInfo() = 0;
67};
68
Mikhail Naganovb0c55252023-02-08 16:59:41 -080069class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl,
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080070 public CallbackBroker, public MicrophoneInfoProvider {
Shunkai Yao51202502022-12-12 06:11:46 +000071 public:
Mikhail Naganov2d814892023-04-24 13:06:04 -070072 status_t getAudioPorts(std::vector<media::audio::common::AudioPort> *ports) override;
73
74 status_t getAudioRoutes(std::vector<media::AudioRoute> *routes) override;
75
Shunkai Yao51202502022-12-12 06:11:46 +000076 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
77 status_t getSupportedDevices(uint32_t *devices) override;
78
79 // Check to see if the audio hardware interface has been initialized.
80 status_t initCheck() override;
81
82 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
83 status_t setVoiceVolume(float volume) override;
84
85 // Set the audio volume for all audio activities other than voice call.
86 status_t setMasterVolume(float volume) override;
87
88 // Get the current master volume value for the HAL.
89 status_t getMasterVolume(float *volume) override;
90
91 // Called when the audio mode changes.
92 status_t setMode(audio_mode_t mode) override;
93
94 // Muting control.
95 status_t setMicMute(bool state) override;
96
97 status_t getMicMute(bool* state) override;
98
99 status_t setMasterMute(bool state) override;
100
101 status_t getMasterMute(bool *state) override;
102
103 // Set global audio parameters.
104 status_t setParameters(const String8& kvPairs) override;
105
106 // Get global audio parameters.
107 status_t getParameters(const String8& keys, String8 *values) override;
108
109 // Returns audio input buffer size according to parameters passed.
110 status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
111
112 // Creates and opens the audio hardware output stream. The stream is closed
113 // by releasing all references to the returned object.
114 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
115 audio_output_flags_t flags, struct audio_config* config,
116 const char* address, sp<StreamOutHalInterface>* outStream) override;
117
118 // Creates and opens the audio hardware input stream. The stream is closed
119 // by releasing all references to the returned object.
120 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
121 struct audio_config* config, audio_input_flags_t flags,
122 const char* address, audio_source_t source,
123 audio_devices_t outputDevice, const char* outputDeviceAddress,
124 sp<StreamInHalInterface>* inStream) override;
125
126 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
127 status_t supportsAudioPatches(bool* supportsPatches) override;
128
129 // Creates an audio patch between several source and sink ports.
130 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
131 unsigned int num_sinks, const struct audio_port_config* sinks,
132 audio_patch_handle_t* patch) override;
133
134 // Releases an audio patch.
135 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
136
Mikhail Naganov31d46652023-01-10 18:29:25 +0000137 // Fills the list of supported attributes for a given audio port.
138 status_t getAudioPort(struct audio_port* port) override;
139
140 // Fills the list of supported attributes for a given audio port.
141 status_t getAudioPort(struct audio_port_v7 *port) override;
142
Shunkai Yao51202502022-12-12 06:11:46 +0000143 // Set audio port configuration.
144 status_t setAudioPortConfig(const struct audio_port_config* config) override;
145
146 // List microphones
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700147 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000148
149 status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
150
151 status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
152
153 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
154 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
155 __unused) override;
156
157 int32_t getAAudioMixerBurstCount() override;
158
159 int32_t getAAudioHardwareBurstMinUsec() override;
160
161 error::Result<audio_hw_sync_t> getHwAvSync() override;
162
Shunkai Yao51202502022-12-12 06:11:46 +0000163 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
164
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100165 status_t getSoundDoseInterface(const std::string& module,
166 ::ndk::SpAIBinder* soundDoseBinder) override;
167
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700168 status_t setConnectedState(const struct audio_port_v7 *port, bool connected) override;
169
170 status_t setSimulateDeviceConnections(bool enabled) override;
171
172 status_t dump(int __unused, const Vector<String16>& __unused) override;
173
Shunkai Yao51202502022-12-12 06:11:46 +0000174 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000175 friend class sp<DeviceHalAidl>;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800176
177 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
178 wp<StreamOutHalInterfaceCallback> out;
179 wp<StreamOutHalInterfaceEventCallback> event;
180 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
181 };
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800182 struct Microphones {
183 enum Status { UNKNOWN, NOT_SUPPORTED, QUERIED };
184 Status status = Status::UNKNOWN;
185 MicrophoneInfoProvider::Info info;
186 };
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800187 using Patches = std::map<int32_t /*patch ID*/,
188 ::aidl::android::hardware::audio::core::AudioPatch>;
189 using PortConfigs = std::map<int32_t /*port config ID*/,
190 ::aidl::android::media::audio::common::AudioPortConfig>;
191 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
Mikhail Naganov2d814892023-04-24 13:06:04 -0700192 using Routes = std::vector<::aidl::android::hardware::audio::core::AudioRoute>;
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700193 // Answers the question "whether portID 'first' is reachable from portID 'second'?"
194 // It's not a map because both portIDs are known. The matrix is symmetric.
195 using RoutingMatrix = std::set<std::pair<int32_t, int32_t>>;
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700196 using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800197 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000198
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800199 // Must not be constructed directly by clients.
200 DeviceHalAidl(
201 const std::string& instance,
202 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module)
203 : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {}
204
205 ~DeviceHalAidl() override = default;
206
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800207 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
208 const ::aidl::android::media::audio::common::AudioPort& p);
209 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
210 const ::aidl::android::media::audio::common::AudioPortConfig& p);
David Lia8f1e582023-03-30 21:08:06 +0800211 status_t createOrUpdatePortConfig(
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800212 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
David Lia8f1e582023-03-30 21:08:06 +0800213 PortConfigs::iterator* result, bool *created);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800214 status_t findOrCreatePatch(
215 const std::set<int32_t>& sourcePortConfigIds,
216 const std::set<int32_t>& sinkPortConfigIds,
217 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
218 status_t findOrCreatePatch(
219 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
220 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
221 status_t findOrCreatePortConfig(
222 const ::aidl::android::media::audio::common::AudioDevice& device,
jiabin2248fa12023-04-27 22:04:16 +0000223 const ::aidl::android::media::audio::common::AudioConfig* config,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800224 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
225 bool* created);
226 status_t findOrCreatePortConfig(
227 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800228 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800229 int32_t ioHandle,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800230 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700231 const std::set<int32_t>& destinationPortIds,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800232 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
233 status_t findOrCreatePortConfig(
234 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700235 const std::set<int32_t>& destinationPortIds,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800236 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
237 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
238 const std::set<int32_t>& sinkPortConfigIds);
239 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
240 Ports::iterator findPort(
241 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700242 const ::aidl::android::media::audio::common::AudioIoFlags& flags,
243 const std::set<int32_t>& destinationPortIds);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800244 PortConfigs::iterator findPortConfig(
245 const ::aidl::android::media::audio::common::AudioDevice& device);
246 PortConfigs::iterator findPortConfig(
247 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800248 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800249 int32_t ioHandle);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800250 status_t prepareToOpenStream(
251 int32_t aidlHandle,
252 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
253 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800254 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800255 struct audio_config* config,
256 Cleanups* cleanups,
257 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800258 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700259 ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch);
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800260 void resetPatch(int32_t patchId);
261 void resetPortConfig(int32_t portConfigId);
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700262 void resetUnusedPatches();
263 void resetUnusedPatchesAndPortConfigs();
264 void resetUnusedPortConfigs();
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700265 status_t updateRoutes();
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800266
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800267 // CallbackBroker implementation
268 void clearCallbacks(void* cookie) override;
269 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
270 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
271 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
272 void setStreamOutEventCallback(void* cookie,
273 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
274 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
275 void* cookie) override;
276 void setStreamOutLatencyModeCallback(
277 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
278 // Implementation helpers.
279 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
280 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
281
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800282 // MicrophoneInfoProvider implementation
283 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
284
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800285 const std::string mInstance;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000286 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100287 std::shared_ptr<::aidl::android::hardware::audio::core::sounddose::ISoundDose>
288 mSoundDose = nullptr;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800289 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800290 int32_t mDefaultInputPortId = -1;
291 int32_t mDefaultOutputPortId = -1;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800292 PortConfigs mPortConfigs;
jiabin2248fa12023-04-27 22:04:16 +0000293 std::set<int32_t> mInitialPortConfigIds;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800294 Patches mPatches;
Mikhail Naganov2d814892023-04-24 13:06:04 -0700295 Routes mRoutes;
Mikhail Naganovecfafb72023-03-29 10:06:15 -0700296 RoutingMatrix mRoutingMatrix;
Mikhail Naganovb1ddbb02023-03-15 17:06:59 -0700297 Streams mStreams;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800298 Microphones mMicrophones;
Mikhail Naganovb0c55252023-02-08 16:59:41 -0800299 std::mutex mLock;
300 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
Shunkai Yao51202502022-12-12 06:11:46 +0000301};
302
303} // namespace android