1 /*
2 * Copyright (C) 2011 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 #define LOG_TAG "legacy_audio_policy_hal"
18 //#define LOG_NDEBUG 0
19
20 #include <stdint.h>
21
22 #include <hardware/hardware.h>
23 #include <system/audio.h>
24 #include <system/audio_policy.h>
25 #include <hardware/audio_policy.h>
26
27 #include <hardware_legacy/AudioPolicyInterface.h>
28 #include <hardware_legacy/AudioSystemLegacy.h>
29
30 #include "AudioPolicyCompatClient.h"
31
32 namespace android_audio_legacy {
33
34 extern "C" {
35
36 struct legacy_ap_module {
37 struct audio_policy_module module;
38 };
39
40 struct legacy_ap_device {
41 struct audio_policy_device device;
42 };
43
44 struct legacy_audio_policy {
45 struct audio_policy policy;
46
47 void *service;
48 struct audio_policy_service_ops *aps_ops;
49 AudioPolicyCompatClient *service_client;
50 AudioPolicyInterface *apm;
51 };
52
to_lap(struct audio_policy * pol)53 static inline struct legacy_audio_policy * to_lap(struct audio_policy *pol)
54 {
55 return reinterpret_cast<struct legacy_audio_policy *>(pol);
56 }
57
to_clap(const struct audio_policy * pol)58 static inline const struct legacy_audio_policy * to_clap(const struct audio_policy *pol)
59 {
60 return reinterpret_cast<const struct legacy_audio_policy *>(pol);
61 }
62
63
ap_set_device_connection_state(struct audio_policy * pol,audio_devices_t device,audio_policy_dev_state_t state,const char * device_address)64 static int ap_set_device_connection_state(struct audio_policy *pol,
65 audio_devices_t device,
66 audio_policy_dev_state_t state,
67 const char *device_address)
68 {
69 struct legacy_audio_policy *lap = to_lap(pol);
70 return lap->apm->setDeviceConnectionState(
71 (AudioSystem::audio_devices)device,
72 (AudioSystem::device_connection_state)state,
73 device_address);
74 }
75
ap_get_device_connection_state(const struct audio_policy * pol,audio_devices_t device,const char * device_address)76 static audio_policy_dev_state_t ap_get_device_connection_state(
77 const struct audio_policy *pol,
78 audio_devices_t device,
79 const char *device_address)
80 {
81 const struct legacy_audio_policy *lap = to_clap(pol);
82 return (audio_policy_dev_state_t)lap->apm->getDeviceConnectionState(
83 (AudioSystem::audio_devices)device,
84 device_address);
85 }
86
ap_set_phone_state(struct audio_policy * pol,int state)87 static void ap_set_phone_state(struct audio_policy *pol, int state)
88 {
89 struct legacy_audio_policy *lap = to_lap(pol);
90 lap->apm->setPhoneState(state);
91 }
92
93 /* indicate a change in ringer mode */
ap_set_ringer_mode(struct audio_policy * pol,uint32_t mode,uint32_t mask)94 static void ap_set_ringer_mode(struct audio_policy *pol, uint32_t mode,
95 uint32_t mask)
96 {
97 struct legacy_audio_policy *lap = to_lap(pol);
98 lap->apm->setRingerMode(mode, mask);
99 }
100
101 /* force using a specific device category for the specified usage */
ap_set_force_use(struct audio_policy * pol,audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)102 static void ap_set_force_use(struct audio_policy *pol,
103 audio_policy_force_use_t usage,
104 audio_policy_forced_cfg_t config)
105 {
106 struct legacy_audio_policy *lap = to_lap(pol);
107 lap->apm->setForceUse((AudioSystem::force_use)usage,
108 (AudioSystem::forced_config)config);
109 }
110
111 /* retreive current device category forced for a given usage */
ap_get_force_use(const struct audio_policy * pol,audio_policy_force_use_t usage)112 static audio_policy_forced_cfg_t ap_get_force_use(
113 const struct audio_policy *pol,
114 audio_policy_force_use_t usage)
115 {
116 const struct legacy_audio_policy *lap = to_clap(pol);
117 return (audio_policy_forced_cfg_t)lap->apm->getForceUse(
118 (AudioSystem::force_use)usage);
119 }
120
121 /* if can_mute is true, then audio streams that are marked ENFORCED_AUDIBLE
122 * can still be muted. */
ap_set_can_mute_enforced_audible(struct audio_policy * pol,bool can_mute)123 static void ap_set_can_mute_enforced_audible(struct audio_policy *pol,
124 bool can_mute)
125 {
126 struct legacy_audio_policy *lap = to_lap(pol);
127 lap->apm->setSystemProperty("ro.camera.sound.forced", can_mute ? "0" : "1");
128 }
129
ap_init_check(const struct audio_policy * pol)130 static int ap_init_check(const struct audio_policy *pol)
131 {
132 const struct legacy_audio_policy *lap = to_clap(pol);
133 return lap->apm->initCheck();
134 }
135
ap_get_output(struct audio_policy * pol,audio_stream_type_t stream,uint32_t sampling_rate,uint32_t format,uint32_t channels,audio_policy_output_flags_t flags)136 static audio_io_handle_t ap_get_output(struct audio_policy *pol,
137 audio_stream_type_t stream,
138 uint32_t sampling_rate,
139 uint32_t format,
140 uint32_t channels,
141 audio_policy_output_flags_t flags)
142 {
143 struct legacy_audio_policy *lap = to_lap(pol);
144
145 LOGV("%s: tid %d", __func__, gettid());
146 return lap->apm->getOutput((AudioSystem::stream_type)stream,
147 sampling_rate, format, channels,
148 (AudioSystem::output_flags)flags);
149 }
150
ap_start_output(struct audio_policy * pol,audio_io_handle_t output,audio_stream_type_t stream,int session)151 static int ap_start_output(struct audio_policy *pol, audio_io_handle_t output,
152 audio_stream_type_t stream, int session)
153 {
154 struct legacy_audio_policy *lap = to_lap(pol);
155 return lap->apm->startOutput(output, (AudioSystem::stream_type)stream,
156 session);
157 }
158
ap_stop_output(struct audio_policy * pol,audio_io_handle_t output,audio_stream_type_t stream,int session)159 static int ap_stop_output(struct audio_policy *pol, audio_io_handle_t output,
160 audio_stream_type_t stream, int session)
161 {
162 struct legacy_audio_policy *lap = to_lap(pol);
163 return lap->apm->stopOutput(output, (AudioSystem::stream_type)stream,
164 session);
165 }
166
ap_release_output(struct audio_policy * pol,audio_io_handle_t output)167 static void ap_release_output(struct audio_policy *pol,
168 audio_io_handle_t output)
169 {
170 struct legacy_audio_policy *lap = to_lap(pol);
171 lap->apm->releaseOutput(output);
172 }
173
ap_get_input(struct audio_policy * pol,int inputSource,uint32_t sampling_rate,uint32_t format,uint32_t channels,audio_in_acoustics_t acoustics)174 static audio_io_handle_t ap_get_input(struct audio_policy *pol, int inputSource,
175 uint32_t sampling_rate,
176 uint32_t format,
177 uint32_t channels,
178 audio_in_acoustics_t acoustics)
179 {
180 struct legacy_audio_policy *lap = to_lap(pol);
181 return lap->apm->getInput(inputSource, sampling_rate, format, channels,
182 (AudioSystem::audio_in_acoustics)acoustics);
183 }
184
ap_start_input(struct audio_policy * pol,audio_io_handle_t input)185 static int ap_start_input(struct audio_policy *pol, audio_io_handle_t input)
186 {
187 struct legacy_audio_policy *lap = to_lap(pol);
188 return lap->apm->startInput(input);
189 }
190
ap_stop_input(struct audio_policy * pol,audio_io_handle_t input)191 static int ap_stop_input(struct audio_policy *pol, audio_io_handle_t input)
192 {
193 struct legacy_audio_policy *lap = to_lap(pol);
194 return lap->apm->stopInput(input);
195 }
196
ap_release_input(struct audio_policy * pol,audio_io_handle_t input)197 static void ap_release_input(struct audio_policy *pol, audio_io_handle_t input)
198 {
199 struct legacy_audio_policy *lap = to_lap(pol);
200 lap->apm->releaseInput(input);
201 }
202
ap_init_stream_volume(struct audio_policy * pol,audio_stream_type_t stream,int index_min,int index_max)203 static void ap_init_stream_volume(struct audio_policy *pol,
204 audio_stream_type_t stream, int index_min,
205 int index_max)
206 {
207 struct legacy_audio_policy *lap = to_lap(pol);
208 lap->apm->initStreamVolume((AudioSystem::stream_type)stream, index_min,
209 index_max);
210 }
211
ap_set_stream_volume_index(struct audio_policy * pol,audio_stream_type_t stream,int index)212 static int ap_set_stream_volume_index(struct audio_policy *pol,
213 audio_stream_type_t stream,
214 int index)
215 {
216 struct legacy_audio_policy *lap = to_lap(pol);
217 return lap->apm->setStreamVolumeIndex((AudioSystem::stream_type)stream,
218 index);
219 }
220
ap_get_stream_volume_index(const struct audio_policy * pol,audio_stream_type_t stream,int * index)221 static int ap_get_stream_volume_index(const struct audio_policy *pol,
222 audio_stream_type_t stream,
223 int *index)
224 {
225 const struct legacy_audio_policy *lap = to_clap(pol);
226 return lap->apm->getStreamVolumeIndex((AudioSystem::stream_type)stream,
227 index);
228 }
229
ap_get_strategy_for_stream(const struct audio_policy * pol,audio_stream_type_t stream)230 static uint32_t ap_get_strategy_for_stream(const struct audio_policy *pol,
231 audio_stream_type_t stream)
232 {
233 const struct legacy_audio_policy *lap = to_clap(pol);
234 return lap->apm->getStrategyForStream((AudioSystem::stream_type)stream);
235 }
236
ap_get_devices_for_stream(const struct audio_policy * pol,audio_stream_type_t stream)237 static uint32_t ap_get_devices_for_stream(const struct audio_policy *pol,
238 audio_stream_type_t stream)
239 {
240 const struct legacy_audio_policy *lap = to_clap(pol);
241 return lap->apm->getDevicesForStream((AudioSystem::stream_type)stream);
242 }
243
ap_get_output_for_effect(struct audio_policy * pol,struct effect_descriptor_s * desc)244 static audio_io_handle_t ap_get_output_for_effect(struct audio_policy *pol,
245 struct effect_descriptor_s *desc)
246 {
247 struct legacy_audio_policy *lap = to_lap(pol);
248 return lap->apm->getOutputForEffect(desc);
249 }
250
ap_register_effect(struct audio_policy * pol,struct effect_descriptor_s * desc,audio_io_handle_t io,uint32_t strategy,int session,int id)251 static int ap_register_effect(struct audio_policy *pol,
252 struct effect_descriptor_s *desc,
253 audio_io_handle_t io,
254 uint32_t strategy,
255 int session,
256 int id)
257 {
258 struct legacy_audio_policy *lap = to_lap(pol);
259 return lap->apm->registerEffect(desc, io, strategy, session, id);
260 }
261
ap_unregister_effect(struct audio_policy * pol,int id)262 static int ap_unregister_effect(struct audio_policy *pol, int id)
263 {
264 struct legacy_audio_policy *lap = to_lap(pol);
265 return lap->apm->unregisterEffect(id);
266 }
267
ap_set_effect_enabled(struct audio_policy * pol,int id,bool enabled)268 static int ap_set_effect_enabled(struct audio_policy *pol, int id, bool enabled)
269 {
270 struct legacy_audio_policy *lap = to_lap(pol);
271 return lap->apm->setEffectEnabled(id, enabled);
272 }
273
ap_is_stream_active(const struct audio_policy * pol,int stream,uint32_t in_past_ms)274 static bool ap_is_stream_active(const struct audio_policy *pol, int stream,
275 uint32_t in_past_ms)
276 {
277 const struct legacy_audio_policy *lap = to_clap(pol);
278 return lap->apm->isStreamActive(stream, in_past_ms);
279 }
280
ap_dump(const struct audio_policy * pol,int fd)281 static int ap_dump(const struct audio_policy *pol, int fd)
282 {
283 const struct legacy_audio_policy *lap = to_clap(pol);
284 return lap->apm->dump(fd);
285 }
286
create_legacy_ap(const struct audio_policy_device * device,struct audio_policy_service_ops * aps_ops,void * service,struct audio_policy ** ap)287 static int create_legacy_ap(const struct audio_policy_device *device,
288 struct audio_policy_service_ops *aps_ops,
289 void *service,
290 struct audio_policy **ap)
291 {
292 struct legacy_audio_policy *lap;
293 int ret;
294
295 if (!service || !aps_ops)
296 return -EINVAL;
297
298 lap = (struct legacy_audio_policy *)calloc(1, sizeof(*lap));
299 if (!lap)
300 return -ENOMEM;
301
302 lap->policy.set_device_connection_state = ap_set_device_connection_state;
303 lap->policy.get_device_connection_state = ap_get_device_connection_state;
304 lap->policy.set_phone_state = ap_set_phone_state;
305 lap->policy.set_ringer_mode = ap_set_ringer_mode;
306 lap->policy.set_force_use = ap_set_force_use;
307 lap->policy.get_force_use = ap_get_force_use;
308 lap->policy.set_can_mute_enforced_audible =
309 ap_set_can_mute_enforced_audible;
310 lap->policy.init_check = ap_init_check;
311 lap->policy.get_output = ap_get_output;
312 lap->policy.start_output = ap_start_output;
313 lap->policy.stop_output = ap_stop_output;
314 lap->policy.release_output = ap_release_output;
315 lap->policy.get_input = ap_get_input;
316 lap->policy.start_input = ap_start_input;
317 lap->policy.stop_input = ap_stop_input;
318 lap->policy.release_input = ap_release_input;
319 lap->policy.init_stream_volume = ap_init_stream_volume;
320 lap->policy.set_stream_volume_index = ap_set_stream_volume_index;
321 lap->policy.get_stream_volume_index = ap_get_stream_volume_index;
322 lap->policy.get_strategy_for_stream = ap_get_strategy_for_stream;
323 lap->policy.get_devices_for_stream = ap_get_devices_for_stream;
324 lap->policy.get_output_for_effect = ap_get_output_for_effect;
325 lap->policy.register_effect = ap_register_effect;
326 lap->policy.unregister_effect = ap_unregister_effect;
327 lap->policy.set_effect_enabled = ap_set_effect_enabled;
328 lap->policy.is_stream_active = ap_is_stream_active;
329 lap->policy.dump = ap_dump;
330
331 lap->service = service;
332 lap->aps_ops = aps_ops;
333 lap->service_client =
334 new AudioPolicyCompatClient(aps_ops, service);
335 if (!lap->service_client) {
336 ret = -ENOMEM;
337 goto err_new_compat_client;
338 }
339
340 lap->apm = createAudioPolicyManager(lap->service_client);
341 if (!lap->apm) {
342 ret = -ENOMEM;
343 goto err_create_apm;
344 }
345
346 *ap = &lap->policy;
347 return 0;
348
349 err_create_apm:
350 delete lap->service_client;
351 err_new_compat_client:
352 free(lap);
353 *ap = NULL;
354 return ret;
355 }
356
destroy_legacy_ap(const struct audio_policy_device * ap_dev,struct audio_policy * ap)357 static int destroy_legacy_ap(const struct audio_policy_device *ap_dev,
358 struct audio_policy *ap)
359 {
360 struct legacy_audio_policy *lap = to_lap(ap);
361
362 if (!lap)
363 return 0;
364
365 if (lap->apm)
366 destroyAudioPolicyManager(lap->apm);
367 if (lap->service_client)
368 delete lap->service_client;
369 free(lap);
370 return 0;
371 }
372
legacy_ap_dev_close(hw_device_t * device)373 static int legacy_ap_dev_close(hw_device_t* device)
374 {
375 if (device)
376 free(device);
377 return 0;
378 }
379
legacy_ap_dev_open(const hw_module_t * module,const char * name,hw_device_t ** device)380 static int legacy_ap_dev_open(const hw_module_t* module, const char* name,
381 hw_device_t** device)
382 {
383 struct legacy_ap_device *dev;
384
385 if (strcmp(name, AUDIO_POLICY_INTERFACE) != 0)
386 return -EINVAL;
387
388 dev = (struct legacy_ap_device *)calloc(1, sizeof(*dev));
389 if (!dev)
390 return -ENOMEM;
391
392 dev->device.common.tag = HARDWARE_DEVICE_TAG;
393 dev->device.common.version = 0;
394 dev->device.common.module = const_cast<hw_module_t*>(module);
395 dev->device.common.close = legacy_ap_dev_close;
396 dev->device.create_audio_policy = create_legacy_ap;
397 dev->device.destroy_audio_policy = destroy_legacy_ap;
398
399 *device = &dev->device.common;
400
401 return 0;
402 }
403
404 static struct hw_module_methods_t legacy_ap_module_methods = {
405 open: legacy_ap_dev_open
406 };
407
408 struct legacy_ap_module HAL_MODULE_INFO_SYM = {
409 module: {
410 common: {
411 tag: HARDWARE_MODULE_TAG,
412 version_major: 1,
413 version_minor: 0,
414 id: AUDIO_POLICY_HARDWARE_MODULE_ID,
415 name: "LEGACY Audio Policy HAL",
416 author: "The Android Open Source Project",
417 methods: &legacy_ap_module_methods,
418 dso : NULL,
419 reserved : {0},
420 },
421 },
422 };
423
424 }; // extern "C"
425
426 }; // namespace android_audio_legacy
427