summaryrefslogtreecommitdiff
path: root/audio/utils/src/ALSAMixer.cpp
blob: 53b3d9a7b5e33d3b2387c03cd6dacb6dc39a9606 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright (C) 2013 Texas Instruments
 *
 * 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.
 */

// #define LOG_NDEBUG 0
// #define VERY_VERBOSE_LOGGING

#include <errno.h>
#include <system/audio.h>
#include <audio_route/audio_route.h>
#include <tinyalsa/asoundlib.h>

#include <tiaudioutils/Log.h>
#include <tiaudioutils/ALSAMixer.h>

namespace tiaudioutils {

ALSAControl::ALSAControl(string &name, int val)
    : mName(name), mStrVal(""), mIntVal(val), mType(TYPE_INT)
{
}

ALSAControl::ALSAControl(string &name, string &val)
    : mName(name), mStrVal(val), mIntVal(-1), mType(TYPE_STR)
{
}

ALSAControl::ALSAControl(const char *name, int val)
    : mName(name), mStrVal(""), mIntVal(val), mType(TYPE_INT)
{
}

ALSAControl::ALSAControl(const char *name, const char *val)
    : mName(name), mStrVal(val), mIntVal(-1), mType(TYPE_STR)
{
}

bool ALSAControl::isIntegerType() const
{
    return (mType == TYPE_INT);
}

bool ALSAControl::isStringType() const
{
    return (mType == TYPE_STR);
}

/* ---------------------------------------------------------------------------------------- */

const string ALSAMixer::kAudioRoutePrefix = "/system/etc/";
const string ALSAMixer::kAudioRouteSuffix = "_paths.xml";

ALSAMixer::ALSAMixer(uint32_t card)
    : mCard(card), mRouter(NULL)
{
    ALOGI("ALSAMixer: open mixer for hw:%u", card);
    mMixer = mixer_open(card);

    initDeviceMap();

    char name[256] = "default";
    /* FIXME read card name from procfs since tinyalsa doesn't support it natively */
#ifdef OMAP_ENHANCEMENT
    int ret = mixer_get_card_name(card, name, sizeof(name));
#else
    int ret = 0;
#endif
    if (!ret) {
        string xmlRouteFile = kAudioRoutePrefix + name + kAudioRouteSuffix;

        mRouter = audio_route_init(card, xmlRouteFile.c_str());
        if (mRouter)
            ALOGI("ALSAMixer: using audio paths file: %s", xmlRouteFile.c_str());
        else
            ALOGI("ALSAMixer: no audio paths file found for card hw:%u", mCard);
    }
}

ALSAMixer::~ALSAMixer()
{
    if (mMixer) {
        ALOGI("ALSAMixer: close mixer for hw:%u", mCard);
        mixer_close(mMixer);
    }

    if (mRouter)
        audio_route_free(mRouter);
}

bool ALSAMixer::initCheck() const
{
    return (mMixer != NULL);
}

int ALSAMixer::set(const ALSAControl &control, bool on)
{
    struct mixer_ctl *ctl;

    AutoMutex lock(mLock);

    ctl = mixer_get_ctl_by_name(mMixer, control.name().c_str());
    if (!ctl) {
        ALOGE("ALSAMixer: failed to get control '%s'", control.name().c_str());
        return -EINVAL;
    }

    if (control.isIntegerType()) {
        int val = on ? control.intVal() : 0;

        ALOGV("ALSAMixer: %s name='%s' val=%d",
              on ? "set" : "clear", control.name().c_str(), val);

        for (uint32_t i = 0; i < mixer_ctl_get_num_values(ctl); i++)
            mixer_ctl_set_value(ctl, i, val);
    } else {
        const char *val = on ? control.strVal().c_str() : "Off";

        ALOGV("ALSAMixer: set name='%s' val='%s'",
              control.name().c_str(), control.strVal().c_str());

        mixer_ctl_set_enum_by_string(ctl, val);
    }

    return 0;
}

int ALSAMixer::set(const ALSAControlList &controls, bool on)
{
    for (ALSAControlList::const_iterator i = controls.begin(); i != controls.end(); ++i) {
        ALOGV("ALSAMixer: %s control '%s'",
              on ? "set" : "clear", i->name().c_str());

        int ret = set(*i, on);
        if (ret) {
            ALOGE("ALSAMixer: failed to %s control '%s'",
                  on ? "set" : "clear", i->name().c_str());
            return ret;
        }
    }

    return 0;
}

int ALSAMixer::initRoutes()
{
    if (!mRouter) {
        ALOGW("ALSAMixer: no audio router available for card hw:%u", mCard);
        return 0;
    }

    ALOGV("ALSAMixer: set card default routes");

    audio_route_reset(mRouter);
    int ret = audio_route_update_mixer(mRouter);
    if (ret)
        ALOGE("ALSAMixer: failed to set card default routes %d", ret);

    return ret;
}

int ALSAMixer::setPath(const char *path, bool on)
{
    if (!mRouter) {
        ALOGW("ALSAMixer: no audio router available for card hw:%u", mCard);
        return 0;
    }

    if (path == NULL)
        return -EINVAL;

    ALOGI("ALSAMixer: %s path for '%s'", on ? "apply" : "reset", path);

    int ret;
    if (on)
        ret = audio_route_apply_path(mRouter, path);
    else
        ret = audio_route_reset_path(mRouter, path);

    if (ret) {
        ALOGE("ALSAMixer: path '%s' not found", path);
        return -EINVAL;
    }

    ret = audio_route_update_mixer(mRouter);
    if (ret)
        ALOGE("ALSAMixer: failed to update mixer for '%s'", path);

    return ret;
}

int ALSAMixer::setPath(audio_devices_t devices, bool on)
{
    if (!mRouter) {
        ALOGW("ALSAMixer: no audio router available for card hw:%u", mCard);
        return 0;
    }

    ALOGV("ALSAMixer: %s path for devices 0x%08x",
          on ? "apply" : "reset", devices);

    int ret;
    for (audio_devices_t mask = 1; mask != AUDIO_DEVICE_BIT_DEFAULT; mask <<= 1) {
        audio_devices_t dev = devices;

        bool found;
        if (audio_is_input_device(dev)) {
            dev &= (mask | AUDIO_DEVICE_BIT_IN);
            found = (dev != AUDIO_DEVICE_BIT_IN);
        } else {
            dev &= mask;
            found = (dev != 0);
        }

        if (!found)
            continue;

        DeviceMap::iterator i = mDevMap.find(dev);
        if (i == mDevMap.end()) {
            ALOGE("ALSAMixer: no map entry for device 0x%08x", dev);
            return -EINVAL;
        }

        const char *path = mDevMap[dev].c_str();
        ALOGV("ALSAMixer: %s path '%s' for device 0x%08x",
              on ? "apply" : "reset", path, dev);
        if (on)
            ret = audio_route_apply_path(mRouter, path);
        else
            ret = audio_route_reset_path(mRouter, path);

        if (ret) {
            ALOGW("ALSAMixer: path '%s' not found", path);
            continue;
        }
    }

    ret = audio_route_update_mixer(mRouter);
    if (ret)
        ALOGE("ALSAMixer: failed to update mixer with new paths %d", ret);

    return ret;
}

int ALSAMixer::updatePath(audio_devices_t oldDevices, audio_devices_t newDevices)
{
    if (!mRouter) {
        ALOGW("ALSAMixer: no audio router available for card hw:%u", mCard);
        return 0;
    }

    audio_devices_t inactive = oldDevices & ~newDevices;
    audio_devices_t active = newDevices & ~oldDevices;

    ALOGV("ALSAMixer: update paths for devices from 0x%08x to 0x%08x",
          oldDevices, newDevices);

    /* First reset the paths for inactive devices */
    int ret = setPath(inactive, false);
    if (ret) {
        ALOGE("ALSAMixer: failed to reset path for inactive devices 0x%08x",
              inactive);
        return ret;
    }

    /* Now apply the path for new active devices */
    ret = setPath(active, true);
    if (ret)
        ALOGE("ALSAMixer: failed to apply path for new active devices 0x%08x",
              active);

    return ret;
}

#define addToDevMap(device)    mDevMap[device] = #device

void ALSAMixer::initDeviceMap()
{
    /* input devices */
    addToDevMap(AUDIO_DEVICE_IN_COMMUNICATION);
    addToDevMap(AUDIO_DEVICE_IN_AMBIENT);
    addToDevMap(AUDIO_DEVICE_IN_BUILTIN_MIC);
    addToDevMap(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET);
    addToDevMap(AUDIO_DEVICE_IN_WIRED_HEADSET);
    addToDevMap(AUDIO_DEVICE_IN_AUX_DIGITAL);
    addToDevMap(AUDIO_DEVICE_IN_VOICE_CALL);
    addToDevMap(AUDIO_DEVICE_IN_BACK_MIC);
    addToDevMap(AUDIO_DEVICE_IN_REMOTE_SUBMIX);
    addToDevMap(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET);
    addToDevMap(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET);
    addToDevMap(AUDIO_DEVICE_IN_USB_ACCESSORY);
    addToDevMap(AUDIO_DEVICE_IN_USB_DEVICE);
    addToDevMap(AUDIO_DEVICE_IN_DEFAULT);

    /* output devices */
    addToDevMap(AUDIO_DEVICE_OUT_EARPIECE);
    addToDevMap(AUDIO_DEVICE_OUT_SPEAKER);
    addToDevMap(AUDIO_DEVICE_OUT_WIRED_HEADSET);
    addToDevMap(AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
    addToDevMap(AUDIO_DEVICE_OUT_BLUETOOTH_SCO);
    addToDevMap(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET);
    addToDevMap(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
    addToDevMap(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP);
    addToDevMap(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES);
    addToDevMap(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
    addToDevMap(AUDIO_DEVICE_OUT_AUX_DIGITAL);
    addToDevMap(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
    addToDevMap(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET);
    addToDevMap(AUDIO_DEVICE_OUT_USB_ACCESSORY);
    addToDevMap(AUDIO_DEVICE_OUT_USB_DEVICE);
    addToDevMap(AUDIO_DEVICE_OUT_REMOTE_SUBMIX);
    addToDevMap(AUDIO_DEVICE_OUT_DEFAULT);

#ifdef OMAP_ENHANCEMENT
    addToDevMap(AUDIO_DEVICE_IN_USB_HEADSET);
    addToDevMap(AUDIO_DEVICE_OUT_WIRED_HEADPHONE2);
#endif
}

} /* namespace tiaudioutils */