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