summaryrefslogtreecommitdiff
path: root/tizen-audio-impl-ctrl.c
blob: cf4b4ce055e7d35f83689b7a9e359f5f2ba7e8bc (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * audio-hal
 *
 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#include "tizen-audio-internal.h"

#ifdef __MIXER_PARAM_DUMP
static void __dump_mixer_param(char *dump, long *param, int size)
{
    int i, len;

    for (i = 0; i < size; i++) {
        len = sprintf(dump, "%ld", *param);
        if (len > 0)
            dump += len;
        if (i != size -1)
            *dump++ = ',';

        param++;
    }
    *dump = '\0';
}
#endif

audio_return_e _mixer_control_init(audio_hal_s *ah)
{
    AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);

    pthread_mutex_init(&(ah->mixer.mutex), NULL);
    return AUDIO_RET_OK;
}

audio_return_e _mixer_control_deinit(audio_hal_s *ah)
{
    AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);

    pthread_mutex_destroy(&(ah->mixer.mutex));
    return AUDIO_RET_OK;
}

audio_return_e _mixer_control_set_param(audio_hal_s *ah, const char* ctl_name, snd_ctl_elem_value_t* param, int size)
{
    AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);

    /* TODO. */
    return AUDIO_RET_OK;
}

audio_return_e _mixer_control_get_value(audio_hal_s *ah, const char *card, const char *ctl_name, int *val)
{
    snd_ctl_t *handle;
    snd_ctl_elem_value_t *control;
    snd_ctl_elem_id_t *id;
    snd_ctl_elem_info_t *info;
    snd_ctl_elem_type_t type;

    int ret = 0, count = 0, i = 0;

    AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(card, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(ctl_name, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(val, AUDIO_ERR_PARAMETER);

    pthread_mutex_lock(&(ah->mixer.mutex));

    ret = snd_ctl_open(&handle, card, 0);
    if (ret < 0) {
        AUDIO_LOG_ERROR("snd_ctl_open error, %s\n", snd_strerror(ret));
        pthread_mutex_unlock(&(ah->mixer.mutex));
        return AUDIO_ERR_IOCTL;
    }

    // Get Element Info

    snd_ctl_elem_id_alloca(&id);
    snd_ctl_elem_info_alloca(&info);
    snd_ctl_elem_value_alloca(&control);

    snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
    snd_ctl_elem_id_set_name(id, ctl_name);

    snd_ctl_elem_info_set_id(info, id);
    if (snd_ctl_elem_info(handle, info) < 0) {
        AUDIO_LOG_ERROR("Cannot find control element: %s\n", ctl_name);
        goto close;
    }
    snd_ctl_elem_info_get_id(info, id);

    type = snd_ctl_elem_info_get_type(info);
    count = snd_ctl_elem_info_get_count(info);

    snd_ctl_elem_value_set_id(control, id);

    if (snd_ctl_elem_read(handle, control) < 0) {
        AUDIO_LOG_ERROR("snd_ctl_elem_read failed \n");
        goto close;
}

    switch (type) {
    case SND_CTL_ELEM_TYPE_BOOLEAN:
        *val = snd_ctl_elem_value_get_boolean(control, i);
        break;
    case SND_CTL_ELEM_TYPE_INTEGER:
        for (i = 0; i < count; i++)
        *val = snd_ctl_elem_value_get_integer(control, i);
        break;
    case SND_CTL_ELEM_TYPE_ENUMERATED:
        for (i = 0; i < count; i++)
        *val = snd_ctl_elem_value_get_enumerated(control, i);
        break;
    default:
        AUDIO_LOG_WARN("unsupported control element type\n");
        goto close;
    }

    snd_ctl_close(handle);

#ifdef AUDIO_DEBUG
    AUDIO_LOG_INFO("get mixer(%s) = %d success", ctl_name, *val);
#endif

    pthread_mutex_unlock(&(ah->mixer.mutex));
    return AUDIO_RET_OK;

close:
    AUDIO_LOG_ERROR("Error\n");
    snd_ctl_close(handle);
    pthread_mutex_unlock(&(ah->mixer.mutex));
    return AUDIO_ERR_UNDEFINED;
}

audio_return_e _mixer_control_set_value(audio_hal_s *ah, const char *card, const char *ctl_name, int val)
{
    snd_ctl_t *handle;
    snd_ctl_elem_value_t *control;
    snd_ctl_elem_id_t *id;
    snd_ctl_elem_info_t *info;
    snd_ctl_elem_type_t type;
    int ret = 0, count = 0, i = 0;

    AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(card, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(ctl_name, AUDIO_ERR_PARAMETER);

    pthread_mutex_lock(&(ah->mixer.mutex));

    ret = snd_ctl_open(&handle, card, 0);
    if (ret < 0) {
        AUDIO_LOG_ERROR("snd_ctl_open error, card: %s: %s", card, snd_strerror(ret));
        pthread_mutex_unlock(&(ah->mixer.mutex));
        return AUDIO_ERR_IOCTL;
    }

    // Get Element Info

    snd_ctl_elem_id_alloca(&id);
    snd_ctl_elem_info_alloca(&info);
    snd_ctl_elem_value_alloca(&control);

    snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
    snd_ctl_elem_id_set_name(id, ctl_name);

    snd_ctl_elem_info_set_id(info, id);
    if (snd_ctl_elem_info(handle, info) < 0) {
        AUDIO_LOG_ERROR("Cannot find control element: %s", ctl_name);
        goto close;
    }
    snd_ctl_elem_info_get_id(info, id);

    type = snd_ctl_elem_info_get_type(info);
    count = snd_ctl_elem_info_get_count(info);

    snd_ctl_elem_value_set_id(control, id);

    snd_ctl_elem_read(handle, control);

    switch (type) {
    case SND_CTL_ELEM_TYPE_BOOLEAN:
        for (i = 0; i < count; i++)
            snd_ctl_elem_value_set_boolean(control, i, val);
        break;
    case SND_CTL_ELEM_TYPE_INTEGER:
        for (i = 0; i < count; i++)
            snd_ctl_elem_value_set_integer(control, i, val);
        break;
    case SND_CTL_ELEM_TYPE_ENUMERATED:
        for (i = 0; i < count; i++)
            snd_ctl_elem_value_set_enumerated(control, i, val);
        break;

    default:
        AUDIO_LOG_WARN("unsupported control element type");
        goto close;
    }

    snd_ctl_elem_write(handle, control);

    snd_ctl_close(handle);

    AUDIO_LOG_INFO("set mixer(%s) = %d success", ctl_name, val);

    pthread_mutex_unlock(&(ah->mixer.mutex));
    return AUDIO_RET_OK;

close:
    AUDIO_LOG_ERROR("Error");
    snd_ctl_close(handle);
    pthread_mutex_unlock(&(ah->mixer.mutex));
    return AUDIO_ERR_UNDEFINED;
}

audio_return_e _mixer_control_set_value_string(audio_hal_s *ah, const char* ctl_name, const char* value)
{
    AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(ctl_name, AUDIO_ERR_PARAMETER);

    /* TODO. */
    return AUDIO_RET_OK;
}


audio_return_e _mixer_control_get_element(audio_hal_s *ah, const char *ctl_name, snd_hctl_elem_t **elem)
{
    AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(ctl_name, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(elem, AUDIO_ERR_PARAMETER);

    /* TODO. */
    return AUDIO_RET_OK;
}