aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/alsa/alsa-api-frontend.c
blob: 11b8a7eedcce8c59fb7c565ff6e9a620ceae6c57 (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
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author Fulup Ar Foll <fulup@iot.bzh>
 *
 * 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 _GNU_SOURCE  // needed for vasprintf

#include "alsa-softmixer.h"
#include <string.h>

// Fulup need to be cleanup with new controller version
extern Lua2cWrapperT Lua2cWrap;

STATIC int ProcessOneRamp(CtlSourceT *source, const char* uid, json_object *rampJ, AlsaVolRampT *ramp) {
    const char*rampUid;

    int error = wrap_json_unpack(rampJ, "{ss,si,si,si !}"
            , "uid", &rampUid
            , "delay", &ramp->delay
            , "up", &ramp->stepUp
            , "down", &ramp->stepDown
            );
    if (error) goto OnErrorExit;

    ramp->delay=ramp->delay*100; // move from ms to us
    ramp->uid = strdup(rampUid);
    return 0;

OnErrorExit:
    AFB_ApiError(source->api, "ProcessOneRamp: sndcard=%s ramps: missing (uid||delay|up|down) json=%s", uid, json_object_get_string(rampJ));
    return -1;
}

STATIC int ProcessOneSubdev(CtlSourceT *source, AlsaSndLoopT *loop, json_object *subdevJ, AlsaPcmHwInfoT *loopDefParams, AlsaPcmInfoT *subdev) {
    json_object *paramsJ = NULL;

    int error = wrap_json_unpack(subdevJ, "{si,si,s?o !}", "subdev", &subdev->subdev, "numid", &subdev->numid, "params", &paramsJ);
    if (error) {
        AFB_ApiError(source->api, "ProcessOneSubdev: loop=%s missing (uid|subdev|numid) json=%s", loop->uid, json_object_get_string(subdevJ));
        goto OnErrorExit;
    }

    if (paramsJ) {
        error = ProcessSndParams(source, loop->uid, paramsJ, loopDefParams);
        if (error) {
            AFB_ApiError(source->api, "ProcessOneLoop: sndcard=%s invalid params=%s", loop->uid, json_object_get_string(paramsJ));
            goto OnErrorExit;
        }
    } else {
        // use global loop params definition as default
        memcpy(&subdev->params, loopDefParams, sizeof (AlsaPcmHwInfoT));
    }
    // create a fake uid and complete subdev info from loop handle
    char subuid[30];
    snprintf(subuid, sizeof (subuid), "loop:/%i/%i", subdev->subdev, subdev->numid);
    subdev->uid = strdup(subuid);
    subdev->device = loop->capture; // Fulup: with alsaloop softmixer only use capture device (playback is used by applications)
    subdev->devpath = loop->devpath;
    subdev->cardid = NULL; // force AlsaByPathDevId to rebuild a new one for each subdev
    subdev->cardidx = loop->cardidx;

    // check if card exist
    error = AlsaByPathDevid(source, subdev);
    if (error) {
        AFB_ApiError(source->api, "ProcessOneSubdev: loop=%s fail to open subdev=%s", loop->uid, json_object_get_string(subdevJ));
        goto OnErrorExit;
    }

    return 0;

OnErrorExit:
    return -1;
}

STATIC int ProcessOneLoop(CtlSourceT *source, json_object *loopJ, AlsaSndLoopT *loop) {
    json_object *subdevsJ = NULL, *devicesJ = NULL, *paramsJ = NULL, *rampsJ = NULL;
    int error;

    error = wrap_json_unpack(loopJ, "{ss,s?s,s?s,s?i,s?o,so,s?o,s?o !}"
            , "uid", &loop->uid
            , "devpath", &loop->devpath
            , "cardid", &loop->cardid
            , "cardidx", &loop->cardidx
            , "devices", &devicesJ
            , "subdevs", &subdevsJ
            , "params", &paramsJ
            , "ramps", &rampsJ
            );
    if (error || !loop->uid || !subdevsJ || (!loop->devpath && !loop->cardid && loop->cardidx)) {
        AFB_ApiNotice(source->api, "ProcessOneLoop missing 'uid|devpath|cardid|cardidx|devices|subdevs' loop=%s", json_object_get_string(loopJ));
        goto OnErrorExit;
    }

    AlsaPcmHwInfoT *loopDefParams = alloca(sizeof (AlsaPcmHwInfoT));
    if (paramsJ) {
        error = ProcessSndParams(source, loop->uid, paramsJ, loopDefParams);
        if (error) {
            AFB_ApiError(source->api, "ProcessOneLoop: sndcard=%s invalid params=%s", loop->uid, json_object_get_string(paramsJ));
            goto OnErrorExit;
        }
    } else {
        loopDefParams->rate = ALSA_DEFAULT_PCM_RATE;
        loopDefParams->rate = ALSA_DEFAULT_PCM_RATE;
        loopDefParams->access = SND_PCM_ACCESS_RW_INTERLEAVED;
        loopDefParams->format = SND_PCM_FORMAT_S16_LE;
        loopDefParams->channels = 2;
        loopDefParams->sampleSize = 0;
    }

    // Fake a sound card to check if loop is a valid Alsa snd driver
    AlsaPcmInfoT sndLoop;
    sndLoop.uid = loop->uid;
    sndLoop.devpath = loop->devpath;
    sndLoop.cardid = loop->cardid;
    sndLoop.device = 0;
    sndLoop.subdev = 0;
    error = AlsaByPathDevid(source, &sndLoop);
    if (error) {
        AFB_ApiError(source->api, "ProcessOneLoop: loop=%s not found config=%s", loop->uid, json_object_get_string(loopJ));
        goto OnErrorExit;
    }
    loop->uid = sndLoop.uid;
    loop->devpath = sndLoop.devpath;
    loop->cardid = sndLoop.cardid;
    loop->cardidx = sndLoop.cardidx;
    loop->registry= calloc (1,sizeof(RegistryHandleT));

    // process volume ramps
    if (rampsJ) {
        int rcount;
        switch (json_object_get_type(rampsJ)) {
            case json_type_object:
                rcount = 1;
                loop->ramps = calloc(rcount+1, sizeof (AlsaVolRampT));
                error = ProcessOneRamp(source, loop->uid, rampsJ, &loop->ramps[0]);
                if (error) goto OnErrorExit;
                break;
            case json_type_array:
                rcount = (int) json_object_array_length(rampsJ);
                loop->ramps = calloc(rcount+1, sizeof (AlsaVolRampT));
                for (int idx = 0; idx < rcount; idx++) {
                    json_object *rampJ = json_object_array_get_idx(rampsJ, idx);
                    error = ProcessOneRamp(source, loop->uid, rampJ, &loop->ramps[idx]);
                    if (error) goto OnErrorExit;
                }
                break;
            default:
                AFB_ApiError(source->api, "ProcessOneLoop:%s invalid ramps=%s", loop->uid, json_object_get_string(rampsJ));
                goto OnErrorExit;
        }
    }

    // Default devices is payback=0 capture=1
    if (!devicesJ) {
        loop->playback = 0;
        loop->capture = 1;
    } else {
        error = wrap_json_unpack(devicesJ, "{si,si !}", "capture", &loop->capture, "playback", &loop->playback);
        if (error) {
            AFB_ApiNotice(source->api, "ProcessOneLoop=%s missing 'capture|playback' devices=%s", loop->uid, json_object_get_string(devicesJ));
            goto OnErrorExit;
        }
    }

    switch (json_object_get_type(subdevsJ)) {
        case json_type_object:
            loop->scount = 1;
            loop->subdevs = calloc(loop->scount + 1, sizeof (AlsaPcmInfoT));
            error = ProcessOneSubdev(source, loop, subdevsJ, loopDefParams, &loop->subdevs[0]);
            if (error) goto OnErrorExit;
            break;
        case json_type_array:
            loop->scount = (int) json_object_array_length(subdevsJ);
            loop->subdevs = calloc(loop->scount + 1, sizeof (AlsaPcmInfoT));
            for (int idx = 0; idx < loop->scount; idx++) {
                json_object *subdevJ = json_object_array_get_idx(subdevsJ, idx);
                error = ProcessOneSubdev(source, loop, subdevJ, loopDefParams, &loop->subdevs[idx]);
                if (error) goto OnErrorExit;
            }
            break;
        default:
            AFB_ApiError(source->api, "ProcessOneLoop=%s invalid subdevs= %s", loop->uid, json_object_get_string(subdevsJ));
            goto OnErrorExit;
    }

    return 0;

OnErrorExit:
    return -1;
}

PUBLIC int SndFrontend(CtlSourceT *source, json_object *argsJ) {
    SoftMixerHandleT *mixer = (SoftMixerHandleT*) source->context;
    int error;

    assert(mixer);

    if (mixer->frontend) {
        AFB_ApiError(source->api, "SndFrontend: mixer=%s SndFrontend already declared %s", mixer->uid, json_object_get_string(argsJ));
        goto OnErrorExit;
    }

    mixer->frontend = calloc(1, sizeof (AlsaSndLoopT));

    // or syntax purpose array is accepted but frontend should have a single driver entry
    json_type type = json_object_get_type(argsJ);
    if (type == json_type_array) {
        size_t count = json_object_array_length(argsJ);
        if (count != 1) {
            AFB_ApiError(source->api, "SndFrontend: mixer=%s frontend only support on input driver args=%s", mixer->uid, json_object_get_string(argsJ));
            goto OnErrorExit;
        }
        argsJ = json_object_array_get_idx(argsJ, 0);
    }

    type = json_object_get_type(argsJ);
    if (type != json_type_object) {
        AFB_ApiError(source->api, "SndFrontend: mixer=%s invalid object type= %s", mixer->uid, json_object_get_string(argsJ));
        goto OnErrorExit;
    }

    error = ProcessOneLoop(source, argsJ, mixer->frontend);
    if (error) {
        AFB_ApiError(source->api, "SndFrontend: mixer=%s invalid object= %s", mixer->uid, json_object_get_string(argsJ));
        goto OnErrorExit;
    }

    return 0;

OnErrorExit:
    return -1;
}