blob: 7e1ee27eda901e9e6dabf6113e1695b55a4cf583 [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 Naganov5b1eed12023-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 Naganovdfd594e2023-02-08 16:59:41 -080024#include <android-base/thread_annotations.h>
Shunkai Yao51202502022-12-12 06:11:46 +000025#include <media/audiohal/DeviceHalInterface.h>
26#include <media/audiohal/EffectHalInterface.h>
Mikhail Naganove93a0862023-03-15 17:06:59 -070027#include <media/audiohal/StreamHalInterface.h>
Shunkai Yao51202502022-12-12 06:11:46 +000028
Mikhail Naganov31d46652023-01-10 18:29:25 +000029#include "ConversionHelperAidl.h"
Shunkai Yao51202502022-12-12 06:11:46 +000030
31namespace android {
32
Mikhail Naganovdfd594e2023-02-08 16:59:41 -080033class StreamOutHalInterfaceCallback;
34class StreamOutHalInterfaceEventCallback;
35class StreamOutHalInterfaceLatencyModeCallback;
36
37// The role of the broker is to connect AIDL callback interface implementations
38// with StreamOut callback implementations. Since AIDL requires all callbacks
39// to be provided upfront, while libaudiohal interfaces allow late registration,
40// there is a need to coordinate the matching process.
41class CallbackBroker : public virtual RefBase {
42 public:
43 virtual ~CallbackBroker() = default;
44 // The cookie is always the stream instance pointer. We don't use weak pointers to avoid extra
45 // costs on reference counting. The stream cleans up related entries on destruction. Since
46 // access to the callbacks map is synchronized, the possibility for pointer aliasing due to
47 // allocation of a new stream at the address of previously deleted stream is avoided.
48 virtual void clearCallbacks(void* cookie) = 0;
49 virtual sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) = 0;
50 virtual void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>&) = 0;
51 virtual sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) = 0;
52 virtual void setStreamOutEventCallback(void* cookie,
53 const sp<StreamOutHalInterfaceEventCallback>&) = 0;
54 virtual sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
55 void* cookie) = 0;
56 virtual void setStreamOutLatencyModeCallback(
57 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>&) = 0;
58};
59
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080060class MicrophoneInfoProvider : public virtual RefBase {
61 public:
62 using Info = std::vector<::aidl::android::media::audio::common::MicrophoneInfo>;
63 virtual ~MicrophoneInfoProvider() = default;
64 // Returns a nullptr if the HAL does not support microphone info retrieval.
65 virtual Info const* getMicrophoneInfo() = 0;
66};
67
Mikhail Naganovdfd594e2023-02-08 16:59:41 -080068class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl,
Mikhail Naganovcad0afe2023-03-10 14:25:57 -080069 public CallbackBroker, public MicrophoneInfoProvider {
Shunkai Yao51202502022-12-12 06:11:46 +000070 public:
Mikhail Naganovf83b9742023-04-24 13:06:04 -070071 status_t getAudioPorts(std::vector<media::audio::common::AudioPort> *ports) override;
72
73 status_t getAudioRoutes(std::vector<media::AudioRoute> *routes) override;
74
Mikhail Naganov1fba38c2023-05-03 17:45:36 -070075 status_t getSupportedModes(std::vector<media::audio::common::AudioMode> *modes) override;
76
Shunkai Yao51202502022-12-12 06:11:46 +000077 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
78 status_t getSupportedDevices(uint32_t *devices) override;
79
80 // Check to see if the audio hardware interface has been initialized.
81 status_t initCheck() override;
82
83 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
84 status_t setVoiceVolume(float volume) override;
85
86 // Set the audio volume for all audio activities other than voice call.
87 status_t setMasterVolume(float volume) override;
88
89 // Get the current master volume value for the HAL.
90 status_t getMasterVolume(float *volume) override;
91
92 // Called when the audio mode changes.
93 status_t setMode(audio_mode_t mode) override;
94
95 // Muting control.
96 status_t setMicMute(bool state) override;
97
98 status_t getMicMute(bool* state) override;
99
100 status_t setMasterMute(bool state) override;
101
102 status_t getMasterMute(bool *state) override;
103
104 // Set global audio parameters.
105 status_t setParameters(const String8& kvPairs) override;
106
107 // Get global audio parameters.
108 status_t getParameters(const String8& keys, String8 *values) override;
109
110 // Returns audio input buffer size according to parameters passed.
111 status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
112
113 // Creates and opens the audio hardware output stream. The stream is closed
114 // by releasing all references to the returned object.
115 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
116 audio_output_flags_t flags, struct audio_config* config,
117 const char* address, sp<StreamOutHalInterface>* outStream) override;
118
119 // Creates and opens the audio hardware input stream. The stream is closed
120 // by releasing all references to the returned object.
121 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
122 struct audio_config* config, audio_input_flags_t flags,
123 const char* address, audio_source_t source,
124 audio_devices_t outputDevice, const char* outputDeviceAddress,
125 sp<StreamInHalInterface>* inStream) override;
126
127 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
128 status_t supportsAudioPatches(bool* supportsPatches) override;
129
130 // Creates an audio patch between several source and sink ports.
131 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
132 unsigned int num_sinks, const struct audio_port_config* sinks,
133 audio_patch_handle_t* patch) override;
134
135 // Releases an audio patch.
136 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
137
Mikhail Naganov31d46652023-01-10 18:29:25 +0000138 // Fills the list of supported attributes for a given audio port.
139 status_t getAudioPort(struct audio_port* port) override;
140
141 // Fills the list of supported attributes for a given audio port.
142 status_t getAudioPort(struct audio_port_v7 *port) override;
143
Shunkai Yao51202502022-12-12 06:11:46 +0000144 // Set audio port configuration.
145 status_t setAudioPortConfig(const struct audio_port_config* config) override;
146
147 // List microphones
Mikhail Naganove93a0862023-03-15 17:06:59 -0700148 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones) override;
Shunkai Yao51202502022-12-12 06:11:46 +0000149
150 status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
151
152 status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
153
154 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
155 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
156 __unused) override;
157
158 int32_t getAAudioMixerBurstCount() override;
159
160 int32_t getAAudioHardwareBurstMinUsec() override;
161
162 error::Result<audio_hw_sync_t> getHwAvSync() override;
163
Shunkai Yao51202502022-12-12 06:11:46 +0000164 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
165
jiabin872de702023-04-27 22:04:31 +0000166 status_t prepareToDisconnectExternalDevice(const struct audio_port_v7 *port) override;
167
Mikhail Naganove93a0862023-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 Naganovdfd594e2023-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 Naganov5b1eed12023-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 Naganovf83b9742023-04-24 13:06:04 -0700192 using Routes = std::vector<::aidl::android::hardware::audio::core::AudioRoute>;
Mikhail Naganov289468a2023-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 Naganove93a0862023-03-15 17:06:59 -0700196 using Streams = std::map<wp<StreamHalInterface>, int32_t /*patch ID*/>;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800197 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000198
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800199 // Must not be constructed directly by clients.
200 DeviceHalAidl(
201 const std::string& instance,
Mikhail Naganov1fba38c2023-05-03 17:45:36 -0700202 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module);
Shunkai Yao51202502022-12-12 06:11:46 +0000203
Mikhail Naganov31d46652023-01-10 18:29:25 +0000204 ~DeviceHalAidl() override = default;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800205
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800206 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
207 const ::aidl::android::media::audio::common::AudioPort& p);
208 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
209 const ::aidl::android::media::audio::common::AudioPortConfig& p);
David Lia8675d42023-03-30 21:08:06 +0800210 status_t createOrUpdatePortConfig(
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800211 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
David Lia8675d42023-03-30 21:08:06 +0800212 PortConfigs::iterator* result, bool *created);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800213 status_t findOrCreatePatch(
214 const std::set<int32_t>& sourcePortConfigIds,
215 const std::set<int32_t>& sinkPortConfigIds,
216 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
217 status_t findOrCreatePatch(
218 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
219 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
220 status_t findOrCreatePortConfig(
221 const ::aidl::android::media::audio::common::AudioDevice& device,
jiabin9c07faf2023-04-26 22:00:44 +0000222 const ::aidl::android::media::audio::common::AudioConfig* config,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800223 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
224 bool* created);
225 status_t findOrCreatePortConfig(
226 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800227 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800228 int32_t ioHandle,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800229 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganov289468a2023-03-29 10:06:15 -0700230 const std::set<int32_t>& destinationPortIds,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800231 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
232 status_t findOrCreatePortConfig(
233 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganov289468a2023-03-29 10:06:15 -0700234 const std::set<int32_t>& destinationPortIds,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800235 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
236 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
237 const std::set<int32_t>& sinkPortConfigIds);
238 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
239 Ports::iterator findPort(
240 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov289468a2023-03-29 10:06:15 -0700241 const ::aidl::android::media::audio::common::AudioIoFlags& flags,
242 const std::set<int32_t>& destinationPortIds);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800243 PortConfigs::iterator findPortConfig(
244 const ::aidl::android::media::audio::common::AudioDevice& device);
245 PortConfigs::iterator findPortConfig(
246 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800247 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800248 int32_t ioHandle);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800249 status_t prepareToOpenStream(
250 int32_t aidlHandle,
251 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
252 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
Mikhail Naganovd8d01f72023-03-09 16:24:40 -0800253 ::aidl::android::media::audio::common::AudioSource aidlSource,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800254 struct audio_config* config,
255 Cleanups* cleanups,
256 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800257 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
Mikhail Naganove93a0862023-03-15 17:06:59 -0700258 ::aidl::android::hardware::audio::core::AudioPatch* aidlPatch);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800259 void resetPatch(int32_t patchId);
260 void resetPortConfig(int32_t portConfigId);
Mikhail Naganove93a0862023-03-15 17:06:59 -0700261 void resetUnusedPatches();
262 void resetUnusedPatchesAndPortConfigs();
263 void resetUnusedPortConfigs();
Mikhail Naganov289468a2023-03-29 10:06:15 -0700264 status_t updateRoutes();
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800265
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800266 // CallbackBroker implementation
267 void clearCallbacks(void* cookie) override;
268 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
269 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
270 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
271 void setStreamOutEventCallback(void* cookie,
272 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
273 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
274 void* cookie) override;
275 void setStreamOutLatencyModeCallback(
276 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
277 // Implementation helpers.
278 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
279 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
280
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800281 // MicrophoneInfoProvider implementation
282 MicrophoneInfoProvider::Info const* getMicrophoneInfo() override;
283
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800284 const std::string mInstance;
285 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
Mikhail Naganov1fba38c2023-05-03 17:45:36 -0700286 const std::shared_ptr<::aidl::android::hardware::audio::core::ITelephony> mTelephony;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800287 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800288 int32_t mDefaultInputPortId = -1;
289 int32_t mDefaultOutputPortId = -1;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800290 PortConfigs mPortConfigs;
jiabin9c07faf2023-04-26 22:00:44 +0000291 std::set<int32_t> mInitialPortConfigIds;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800292 Patches mPatches;
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700293 Routes mRoutes;
Mikhail Naganov289468a2023-03-29 10:06:15 -0700294 RoutingMatrix mRoutingMatrix;
Mikhail Naganove93a0862023-03-15 17:06:59 -0700295 Streams mStreams;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800296 Microphones mMicrophones;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800297 std::mutex mLock;
298 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
jiabin872de702023-04-27 22:04:31 +0000299 std::set<audio_port_handle_t> mDeviceDisconnectionNotified;
Shunkai Yao51202502022-12-12 06:11:46 +0000300};
301
302} // namespace android