blob: 5e6f6629582be04878b83f66a45981f6adcf73cc [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>
27
Mikhail Naganov31d46652023-01-10 18:29:25 +000028#include "ConversionHelperAidl.h"
Shunkai Yao51202502022-12-12 06:11:46 +000029
30namespace android {
31
Mikhail Naganovdfd594e2023-02-08 16:59:41 -080032class StreamOutHalInterfaceCallback;
33class StreamOutHalInterfaceEventCallback;
34class StreamOutHalInterfaceLatencyModeCallback;
35
36// The role of the broker is to connect AIDL callback interface implementations
37// with StreamOut callback implementations. Since AIDL requires all callbacks
38// to be provided upfront, while libaudiohal interfaces allow late registration,
39// there is a need to coordinate the matching process.
40class CallbackBroker : public virtual RefBase {
41 public:
42 virtual ~CallbackBroker() = default;
43 // The cookie is always the stream instance pointer. We don't use weak pointers to avoid extra
44 // costs on reference counting. The stream cleans up related entries on destruction. Since
45 // access to the callbacks map is synchronized, the possibility for pointer aliasing due to
46 // allocation of a new stream at the address of previously deleted stream is avoided.
47 virtual void clearCallbacks(void* cookie) = 0;
48 virtual sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) = 0;
49 virtual void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>&) = 0;
50 virtual sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) = 0;
51 virtual void setStreamOutEventCallback(void* cookie,
52 const sp<StreamOutHalInterfaceEventCallback>&) = 0;
53 virtual sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
54 void* cookie) = 0;
55 virtual void setStreamOutLatencyModeCallback(
56 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>&) = 0;
57};
58
59class DeviceHalAidl : public DeviceHalInterface, public ConversionHelperAidl,
60 public CallbackBroker {
Shunkai Yao51202502022-12-12 06:11:46 +000061 public:
62 // Sets the value of 'devices' to a bitmask of 1 or more values of audio_devices_t.
63 status_t getSupportedDevices(uint32_t *devices) override;
64
65 // Check to see if the audio hardware interface has been initialized.
66 status_t initCheck() override;
67
68 // Set the audio volume of a voice call. Range is between 0.0 and 1.0.
69 status_t setVoiceVolume(float volume) override;
70
71 // Set the audio volume for all audio activities other than voice call.
72 status_t setMasterVolume(float volume) override;
73
74 // Get the current master volume value for the HAL.
75 status_t getMasterVolume(float *volume) override;
76
77 // Called when the audio mode changes.
78 status_t setMode(audio_mode_t mode) override;
79
80 // Muting control.
81 status_t setMicMute(bool state) override;
82
83 status_t getMicMute(bool* state) override;
84
85 status_t setMasterMute(bool state) override;
86
87 status_t getMasterMute(bool *state) override;
88
89 // Set global audio parameters.
90 status_t setParameters(const String8& kvPairs) override;
91
92 // Get global audio parameters.
93 status_t getParameters(const String8& keys, String8 *values) override;
94
95 // Returns audio input buffer size according to parameters passed.
96 status_t getInputBufferSize(const struct audio_config* config, size_t* size) override;
97
98 // Creates and opens the audio hardware output stream. The stream is closed
99 // by releasing all references to the returned object.
100 status_t openOutputStream(audio_io_handle_t handle, audio_devices_t devices,
101 audio_output_flags_t flags, struct audio_config* config,
102 const char* address, sp<StreamOutHalInterface>* outStream) override;
103
104 // Creates and opens the audio hardware input stream. The stream is closed
105 // by releasing all references to the returned object.
106 status_t openInputStream(audio_io_handle_t handle, audio_devices_t devices,
107 struct audio_config* config, audio_input_flags_t flags,
108 const char* address, audio_source_t source,
109 audio_devices_t outputDevice, const char* outputDeviceAddress,
110 sp<StreamInHalInterface>* inStream) override;
111
112 // Returns whether createAudioPatch and releaseAudioPatch operations are supported.
113 status_t supportsAudioPatches(bool* supportsPatches) override;
114
115 // Creates an audio patch between several source and sink ports.
116 status_t createAudioPatch(unsigned int num_sources, const struct audio_port_config* sources,
117 unsigned int num_sinks, const struct audio_port_config* sinks,
118 audio_patch_handle_t* patch) override;
119
120 // Releases an audio patch.
121 status_t releaseAudioPatch(audio_patch_handle_t patch) override;
122
Mikhail Naganov31d46652023-01-10 18:29:25 +0000123 // Fills the list of supported attributes for a given audio port.
124 status_t getAudioPort(struct audio_port* port) override;
125
126 // Fills the list of supported attributes for a given audio port.
127 status_t getAudioPort(struct audio_port_v7 *port) override;
128
Shunkai Yao51202502022-12-12 06:11:46 +0000129 // Set audio port configuration.
130 status_t setAudioPortConfig(const struct audio_port_config* config) override;
131
132 // List microphones
133 status_t getMicrophones(std::vector<audio_microphone_characteristic_t>* microphones);
134
135 status_t addDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
136
137 status_t removeDeviceEffect(audio_port_handle_t device, sp<EffectHalInterface> effect) override;
138
139 status_t getMmapPolicyInfos(media::audio::common::AudioMMapPolicyType policyType __unused,
140 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos
141 __unused) override;
142
143 int32_t getAAudioMixerBurstCount() override;
144
145 int32_t getAAudioHardwareBurstMinUsec() override;
146
147 error::Result<audio_hw_sync_t> getHwAvSync() override;
148
149 status_t dump(int __unused, const Vector<String16>& __unused) override;
150
151 int32_t supportsBluetoothVariableLatency(bool* supports __unused) override;
152
153 private:
Mikhail Naganov31d46652023-01-10 18:29:25 +0000154 friend class sp<DeviceHalAidl>;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800155
156 struct Callbacks { // No need to use `atomic_wp` because access is serialized.
157 wp<StreamOutHalInterfaceCallback> out;
158 wp<StreamOutHalInterfaceEventCallback> event;
159 wp<StreamOutHalInterfaceLatencyModeCallback> latency;
160 };
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800161 using Patches = std::map<int32_t /*patch ID*/,
162 ::aidl::android::hardware::audio::core::AudioPatch>;
163 using PortConfigs = std::map<int32_t /*port config ID*/,
164 ::aidl::android::media::audio::common::AudioPortConfig>;
165 using Ports = std::map<int32_t /*port ID*/, ::aidl::android::media::audio::common::AudioPort>;
166 class Cleanups;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000167
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800168 // Must not be constructed directly by clients.
169 DeviceHalAidl(
170 const std::string& instance,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000171 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule>& module)
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800172 : ConversionHelperAidl("DeviceHalAidl"), mInstance(instance), mModule(module) {}
Shunkai Yao51202502022-12-12 06:11:46 +0000173
Mikhail Naganov31d46652023-01-10 18:29:25 +0000174 ~DeviceHalAidl() override = default;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800175
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800176 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
177 const ::aidl::android::media::audio::common::AudioPort& p);
178 bool audioDeviceMatches(const ::aidl::android::media::audio::common::AudioDevice& device,
179 const ::aidl::android::media::audio::common::AudioPortConfig& p);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800180 status_t createPortConfig(
181 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
Mikhail Naganov1a44bc62023-02-16 17:35:06 -0800182 PortConfigs::iterator* result);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800183 status_t findOrCreatePatch(
184 const std::set<int32_t>& sourcePortConfigIds,
185 const std::set<int32_t>& sinkPortConfigIds,
186 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
187 status_t findOrCreatePatch(
188 const ::aidl::android::hardware::audio::core::AudioPatch& requestedPatch,
189 ::aidl::android::hardware::audio::core::AudioPatch* patch, bool* created);
190 status_t findOrCreatePortConfig(
191 const ::aidl::android::media::audio::common::AudioDevice& device,
192 ::aidl::android::media::audio::common::AudioPortConfig* portConfig,
193 bool* created);
194 status_t findOrCreatePortConfig(
195 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800196 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800197 int32_t ioHandle,
198 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
199 status_t findOrCreatePortConfig(
200 const ::aidl::android::media::audio::common::AudioPortConfig& requestedPortConfig,
201 ::aidl::android::media::audio::common::AudioPortConfig* portConfig, bool* created);
202 Patches::iterator findPatch(const std::set<int32_t>& sourcePortConfigIds,
203 const std::set<int32_t>& sinkPortConfigIds);
204 Ports::iterator findPort(const ::aidl::android::media::audio::common::AudioDevice& device);
205 Ports::iterator findPort(
206 const ::aidl::android::media::audio::common::AudioConfig& config,
207 const ::aidl::android::media::audio::common::AudioIoFlags& flags);
208 PortConfigs::iterator findPortConfig(
209 const ::aidl::android::media::audio::common::AudioDevice& device);
210 PortConfigs::iterator findPortConfig(
211 const ::aidl::android::media::audio::common::AudioConfig& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800212 const std::optional<::aidl::android::media::audio::common::AudioIoFlags>& flags,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800213 int32_t ioHandle);
214 // Currently unused but may be useful for implementing setAudioPortConfig
215 // PortConfigs::iterator findPortConfig(
216 // const ::aidl::android::media::audio::common::AudioPortConfig& portConfig);
217 status_t prepareToOpenStream(
218 int32_t aidlHandle,
219 const ::aidl::android::media::audio::common::AudioDevice& aidlDevice,
220 const ::aidl::android::media::audio::common::AudioIoFlags& aidlFlags,
221 struct audio_config* config,
222 Cleanups* cleanups,
223 ::aidl::android::media::audio::common::AudioConfig* aidlConfig,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800224 ::aidl::android::media::audio::common::AudioPortConfig* mixPortConfig,
225 int32_t* nominalLatency);
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800226 void resetPatch(int32_t patchId);
227 void resetPortConfig(int32_t portConfigId);
228
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800229 // CallbackBroker implementation
230 void clearCallbacks(void* cookie) override;
231 sp<StreamOutHalInterfaceCallback> getStreamOutCallback(void* cookie) override;
232 void setStreamOutCallback(void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) override;
233 sp<StreamOutHalInterfaceEventCallback> getStreamOutEventCallback(void* cookie) override;
234 void setStreamOutEventCallback(void* cookie,
235 const sp<StreamOutHalInterfaceEventCallback>& cb) override;
236 sp<StreamOutHalInterfaceLatencyModeCallback> getStreamOutLatencyModeCallback(
237 void* cookie) override;
238 void setStreamOutLatencyModeCallback(
239 void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) override;
240 // Implementation helpers.
241 template<class C> sp<C> getCallbackImpl(void* cookie, wp<C> Callbacks::* field);
242 template<class C> void setCallbackImpl(void* cookie, wp<C> Callbacks::* field, const sp<C>& cb);
243
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800244 const std::string mInstance;
245 const std::shared_ptr<::aidl::android::hardware::audio::core::IModule> mModule;
246 Ports mPorts;
Mikhail Naganov81bf4d02023-02-06 12:20:38 -0800247 int32_t mDefaultInputPortId = -1;
248 int32_t mDefaultOutputPortId = -1;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800249 PortConfigs mPortConfigs;
250 Patches mPatches;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800251 std::map<audio_patch_handle_t, int32_t /*patch ID*/> mFwkHandles;
252 std::mutex mLock;
253 std::map<void*, Callbacks> mCallbacks GUARDED_BY(mLock);
Shunkai Yao51202502022-12-12 06:11:46 +0000254};
255
256} // namespace android