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