summaryrefslogtreecommitdiff
path: root/hal/audio_extn/dev_arbi.c
blob: 4eb5deefadc407c9e6bc346c43a54d1764a3924c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (c) 2014, 2016-2017 The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of The Linux Foundation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define LOG_TAG "audio_hw_dev_arbi"
/*#define LOG_NDEBUG 0*/
#define LOG_NDDEBUG 0

#include <errno.h>
#include <log/log.h>
#include <fcntl.h>
#include "audio_hw.h"
#include "platform.h"
#include "platform_api.h"
#include <sys/stat.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <cutils/properties.h>
#include "audio_extn.h"

#ifdef DYNAMIC_LOG_ENABLED
#include <log_xml_parser.h>
#define LOG_MASK HAL_MOD_FILE_DEV_ARBI
#include <log_utils.h>
#endif

#ifdef DEV_ARBI_ENABLED

typedef int (init_fn_t)();
typedef int (deinit_fn_t)();
typedef int (acquire_fn_t)(audio_devices_t aud_dev);
typedef int (release_fn_t)(audio_devices_t aud_dev);

typedef struct {
    snd_device_t snd_device;
    audio_devices_t aud_device;
} snd_aud_dev_mapping_t;

static void* lib_handle = NULL;

static init_fn_t *init_fp = NULL;
static deinit_fn_t *deinit_fp = NULL;
static acquire_fn_t *acquire_fp = NULL;
static release_fn_t *release_fp = NULL;

static int load_dev_arbi_lib()
{
    int rc = -EINVAL;

    if (lib_handle != NULL) {
        ALOGE("%s: library already loaded", __func__);
        return rc;
    }

    lib_handle = dlopen("libaudiodevarb.so", RTLD_NOW);
    if (lib_handle != NULL) {
        init_fp = (init_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_init");
        deinit_fp = (deinit_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_deinit");
        acquire_fp = (acquire_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_acquire");
        release_fp = (release_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_release");

        if ((init_fp == NULL) ||
            (deinit_fp == NULL) ||
            (acquire_fp == NULL) ||
            (release_fp == NULL)) {

            ALOGE("%s: error loading symbols from library", __func__);

            init_fp = NULL;
            deinit_fp = NULL;
            acquire_fp = NULL;
            release_fp = NULL;
        } else
            return 0;
    }

    return rc;
}

int audio_extn_dev_arbi_init()
{
    int rc = load_dev_arbi_lib();
    if (!rc)
        rc = init_fp();

    return rc;
}

int audio_extn_dev_arbi_deinit()
{
    int rc = -EINVAL;

    if(deinit_fp != NULL) {
        rc = deinit_fp();

        init_fp = NULL;
        deinit_fp = NULL;
        acquire_fp = NULL;
        release_fp = NULL;

        dlclose(lib_handle);
        lib_handle = NULL;
    }

    return rc;
}

static audio_devices_t get_audio_device(snd_device_t snd_device)
{
    static snd_aud_dev_mapping_t snd_aud_dev_map[] = {
        {SND_DEVICE_OUT_HANDSET, AUDIO_DEVICE_OUT_EARPIECE},
        {SND_DEVICE_OUT_VOICE_HANDSET, AUDIO_DEVICE_OUT_EARPIECE},
        {SND_DEVICE_OUT_SPEAKER, AUDIO_DEVICE_OUT_SPEAKER},
        {SND_DEVICE_OUT_VOICE_SPEAKER, AUDIO_DEVICE_OUT_SPEAKER},
        {SND_DEVICE_OUT_VOICE_SPEAKER_2, AUDIO_DEVICE_OUT_SPEAKER},
        {SND_DEVICE_OUT_HEADPHONES, AUDIO_DEVICE_OUT_WIRED_HEADPHONE},
        {SND_DEVICE_OUT_VOICE_HEADPHONES, AUDIO_DEVICE_OUT_WIRED_HEADPHONE},
        {SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
            AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_WIRED_HEADPHONE}
        {SND_DEVICE_OUT_VOICE_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADSET},
    };

    audio_devices_t aud_device = AUDIO_DEVICE_NONE;
    uint32_t ind = 0;

    for (ind = 0; ind < ARRAY_SIZE(snd_aud_dev_map); ++ind) {
        if (snd_device == snd_aud_dev_map[ind].snd_device) {
            aud_device = snd_aud_dev_map[ind].aud_device;
            break;
        }
    }

    return aud_device;
}

int audio_extn_dev_arbi_acquire(snd_device_t snd_device)
{
    int rc = -EINVAL;
    audio_devices_t audio_device = get_audio_device(snd_device);

    if ((acquire_fp != NULL) && (audio_device != AUDIO_DEVICE_NONE))
        rc = acquire_fp(audio_device);

    return rc;
}

int audio_extn_dev_arbi_release(snd_device_t snd_device)
{
    int rc = -EINVAL;
    audio_devices_t audio_device = get_audio_device(snd_device);

    if ((release_fp != NULL) && (audio_device != AUDIO_DEVICE_NONE))
        rc = release_fp(audio_device);

    return rc;
}

#endif /*DEV_ARBI_ENABLED*/