summaryrefslogtreecommitdiffstats
path: root/plugins/alsa/alsa-api-loop.c
blob: c0ea549573df8c213f1b8e97a929f7d80ccb1d2b (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * 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 <avirt/avirt.h>
#include <string.h>

#define UID_AVIRT_LOOP "AVIRT-Loopback"

#define REGNUMID_0  51
#define REGNUMID_1  57
#define REGNUMID_2  63
#define REGNUMID_3  69
#define REGNUMID_4  75
#define REGNUMID_5  81
#define REGNUMID_6  87
#define REGNUMID_7  93
#define REGNUMID_8  99
#define REGNUMID_9  105
#define REGNUMID_10 111
#define REGNUMID_11 117
#define REGNUMID_12 123
#define REGNUMID_13 129
#define REGNUMID_14 135
#define REGNUMID_15 141

struct RegistryNumidMap {
    int index;
    int numid;
} numidmap[] = {
    { 0,  REGNUMID_0  },
    { 1,  REGNUMID_1  },
    { 2,  REGNUMID_2  },
    { 3,  REGNUMID_3  },
    { 4,  REGNUMID_4  },
    { 5,  REGNUMID_5  },
    { 6,  REGNUMID_6  },
    { 7,  REGNUMID_7  },
    { 8,  REGNUMID_8  },
    { 9,  REGNUMID_9  },
    { 10, REGNUMID_10 },
    { 11, REGNUMID_11 },
    { 12, REGNUMID_12 },
    { 13, REGNUMID_13 },
    { 14, REGNUMID_14 },
    { 15, REGNUMID_15 },
};

PUBLIC AlsaLoopSubdevT *ApiLoopFindSubdev(SoftMixerT *mixer, const char *streamUid, const char *targetUid, AlsaSndLoopT **loop) {

    // Either allocate a free loop subdev or search for a specific targetUid when specified
	AlsaSndLoopT * _loop;
	if (targetUid) {
		cds_list_for_each_entry(_loop, &mixer->loops.list, list) {
			AlsaLoopSubdevT * subdev;
			cds_list_for_each_entry(subdev, &_loop->subdevs.list, list) {
				if (subdev->uid && !strcmp(subdev->uid, targetUid)) {
					*loop = _loop;
					return subdev;
				}
			}
		}
	} else {
		cds_list_for_each_entry(_loop, &mixer->loops.list, list) {
			AlsaLoopSubdevT * subdev;
			cds_list_for_each_entry(subdev, &_loop->subdevs.list, list) {
				if (!subdev->uid) {
					subdev->uid = strdup(streamUid);
					if (!subdev->uid) {
						SOFTMIXER_NOMEM(mixer->api);
						goto fail;
					}
					*loop = _loop;
					return subdev;
				}
			}
		}
	}
fail:
	return NULL;
}

STATIC int CheckOneSubdev(SoftMixerT *mixer, AlsaSndLoopT *loop, AlsaLoopSubdevT *subdev) {
    // create loop subdev entry point with cardidx+device+subdev in order to open subdev and not sndcard
    AlsaDevInfoT loopSubdev;
    loopSubdev.devpath=NULL;
    loopSubdev.cardid=NULL;
    loopSubdev.pcmplug_params = NULL;
    loopSubdev.cardidx = loop->sndcard->cid.cardidx;
    loopSubdev.device = loop->capture;
    loopSubdev.subdev = subdev->index;

    // assert we may open this loopback subdev in capture mode
    AlsaPcmCtlT *pcmCtl = AlsaByPathOpenPcmCtl(mixer, &loopSubdev, SND_PCM_STREAM_CAPTURE);
    if (!pcmCtl) {
        AFB_API_ERROR(mixer->api, "%s: Failed", __func__);
        return -1;
    }

    pcmCtl->closeAtDeletion = true;

    // free PCM as we only open loop to assert it's a valid capture device
    AlsaMixerTransactionObjectForget(mixer->transaction, pcmCtl);
    AlsaPcmCtlDelete(mixer, pcmCtl);

    return 0;
}

STATIC AlsaLoopSubdevT *ProcessOneAvirtSubdev(SoftMixerT *mixer, AlsaSndLoopT *loop, json_object *streamJ, int index) {
    char *uid, *zone_uid;
    AlsaSndZoneT *zone;
    AFB_API_INFO(mixer->api, "%s: %d", __func__, index);
    int error;

    uid = alloca(32);

    error = wrap_json_unpack(streamJ, "{ss,s?s,s?s,ss,s?s,s?i,s?b,s?o,s?s !}"
            , "uid", &uid
            , "verb", NULL
            , "info", NULL
            , "zone", &zone_uid
            , "source", NULL
            , "volume", NULL
            , "mute", NULL
            , "params", NULL
            , "ramp", NULL
            );
    if (error) {
        AFB_API_NOTICE(mixer->api,
                       "%s: hal=%s missing 'uid|[info]|zone|source||[volume]|[mute]|[params]' error=%s stream=%s",
                       __func__, uid, wrap_json_get_error_string(error), json_object_get_string(streamJ));
        goto OnErrorExit;
    }

    if (mixer->nbZones != 0) {
        zone = ApiZoneGetByUid(mixer, zone_uid);
        if (!zone)
          goto OnErrorExit;
    } else {
        AFB_API_ERROR(mixer->api, "%s: No zones defined!", __func__);
        goto OnErrorExit;
    }

    error = snd_avirt_stream_new(uid, zone->ccount,
                                 SND_PCM_STREAM_PLAYBACK, "ap_loopback");
    if (error < 0) {
        AFB_API_ERROR(mixer->api,
        "%s: mixer=%s stream=%s could not create AVIRT stream [errno=%d]",
        __func__, mixer->uid, uid, error);
        return NULL;
    }

    AlsaLoopSubdevT *subdev = calloc(1, sizeof (AlsaPcmCtlT));
    subdev->uid = NULL;
    subdev->index = index;
    subdev->numid = numidmap[index].index;

    if (CheckOneSubdev(mixer, loop, subdev) < 0)
      return NULL;

    return subdev;

OnErrorExit:
    AFB_API_ERROR(mixer->api, "%s fail", __func__);
    return NULL;
}

STATIC AlsaLoopSubdevT *ProcessOneSubdev(SoftMixerT *mixer, AlsaSndLoopT *loop, json_object *subdevJ) {

	AFB_API_DEBUG(mixer->api, "%s : %s", __func__, json_object_get_string(subdevJ));

	AlsaLoopSubdevT *subdev = calloc(1, sizeof (AlsaLoopSubdevT));
	if (subdev == NULL) {
		SOFTMIXER_NOMEM(mixer->api);
		goto fail;
	}

    int error = wrap_json_unpack(subdevJ, "{s?s, si,si !}"
            , "uid", &subdev->uid
            , "subdev", &subdev->index
            , "numid", &subdev->numid
            );
    if (error) {
		AFB_API_ERROR(mixer->api,
					 "%s: loop=%s missing (uid|subdev|numid) error=%s json=%s",
					 __func__, loop->uid, wrap_json_get_error_string(error), json_object_get_string(subdevJ));
		goto fail_subdev;
    }

    // subdev with no UID are dynamically attached
	if (subdev->uid) {
		subdev->uid = strdup(subdev->uid);
		if (subdev->uid == NULL) {
			SOFTMIXER_NOMEM(mixer->api);
			goto fail_subdev;
		}
	}

    if (CheckOneSubdev(mixer, loop, subdev) < 0)
      goto fail_subdev_uid;

	AFB_API_DEBUG(mixer->api, "%s DONE", __func__);

    return subdev;

fail_subdev_uid:
	free((char*)subdev->uid);
fail_subdev:
	free(subdev);
fail:
    return NULL;
}

static void freeSubdev(SoftMixerT* mixer, AlsaLoopSubdevT * subdev) {
	free(subdev->uid);
	free(subdev);
}


STATIC AlsaSndLoopT *AttachOneLoop(SoftMixerT *mixer, const char *uid, json_object *argsJ, json_object *streamsJ) {

    json_object *subdevsJ = NULL, *devicesJ = NULL;
    int error;

	AFB_API_NOTICE(mixer->api, "%s, uid %s", __func__, uid);

	AlsaSndLoopT *loop = calloc(1, sizeof (AlsaSndLoopT));
	if (loop == NULL) {
		SOFTMIXER_NOMEM(mixer->api);
		goto fail;
	}

	CDS_INIT_LIST_HEAD(&loop->list);
	CDS_INIT_LIST_HEAD(&loop->subdevs.list);

	AlsaSndCtlT * sndctl = (AlsaSndCtlT*) calloc(1, sizeof (AlsaSndCtlT));
	if (sndctl == NULL) {
		SOFTMIXER_NOMEM(mixer->api);
		goto fail_loop;
	}

	CDS_INIT_LIST_HEAD(&sndctl->registryList);

	loop->sndcard = sndctl;

    error = wrap_json_unpack(argsJ, "{ss,s?s,s?s,s?o,s?o !}"
            , "uid", &loop->uid
            , "path", &loop->sndcard->cid.devpath
            , "cardid", &loop->sndcard->cid.cardid
            , "devices", &devicesJ
            , "subdevs", &subdevsJ
            );

    if (loop->uid) {
        if (!strcmp(loop->uid, UID_AVIRT_LOOP))
            loop->avirt = true;
        else
            loop->avirt = false;
    }

    if (error || !loop->uid || (!loop->avirt && !subdevsJ) || (!loop->sndcard->cid.devpath && !loop->sndcard->cid.cardid)) {
        AFB_API_NOTICE(mixer->api, "%s mixer=%s hal=%s missing 'uid|path|cardid|devices|subdevs' error=%s args=%s",
        		__func__, mixer->uid, uid, wrap_json_get_error_string(error),json_object_get_string(argsJ));
		goto fail_snd_card;
    }

    if (!loop->avirt) {
        // try to open sound card control interface
        loop->sndcard->ctl = AlsaByPathOpenCtl(mixer, loop->uid, loop->sndcard);
        if (!loop->sndcard->ctl) {
            AFB_API_ERROR(mixer->api, "%s mixer=%s hal=%s Fail open sndcard loop=%s devpath=%s cardid=%s (please check 'modprobe snd_aloop')",
                __func__, mixer->uid, uid, loop->uid, loop->sndcard->cid.devpath, loop->sndcard->cid.cardid);
            goto fail_snd_card;
        }

        // Default devices is playback=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_API_NOTICE(mixer->api, "%s mixer=%s hal=%s Loop=%s missing 'capture|playback' error=%s devices=%s",
                    __func__, mixer->uid, uid, loop->uid, wrap_json_get_error_string(error),json_object_get_string(devicesJ));
                goto fail_snd_card_ctl;
            }
        }

        AlsaLoopSubdevT * subdev;

        switch (json_object_get_type(subdevsJ)) {
            case json_type_object:
                subdev = ProcessOneSubdev(mixer, loop, subdevsJ);
                if (!subdev)
                    goto fail_loop_subdev;
                loop->nbSubdevs++;
                cds_list_add_tail(&subdev->list, &loop->subdevs.list);
                break;
            case json_type_array: {
                int nbSubDevs = (int) json_object_array_length(subdevsJ);
                for (int idx = 0; idx < nbSubDevs; idx++) {
                    json_object *subdevJ = json_object_array_get_idx(subdevsJ, idx);
                    subdev = ProcessOneSubdev(mixer, loop, subdevJ);
                    if (!subdev) {
                        goto fail_loop_subdev;
                    }
                    loop->nbSubdevs++;
                    cds_list_add_tail(&subdev->list, &loop->subdevs.list);
                }
                break;
            }
            default:
                AFB_API_ERROR(mixer->api, "%s mixer=%s hal=%s Loop=%s invalid subdevs= %s",
                    __func__, mixer->uid, uid, loop->uid, json_object_get_string(subdevsJ));
                goto fail_snd_card_ctl;
        }
    } else { // loop->avirt == true
        AFB_API_NOTICE(mixer->api, "nbStreams: %d max.streams: %d", mixer->nbStreams, mixer->max.streams);

        if (mixer->nbStreams >= mixer->max.streams)
            goto fail_snd_card_ctl;

        AlsaLoopSubdevT * subdev;

        // Create AVIRT streams
        switch (json_object_get_type(streamsJ)) {
            case json_type_object:
                subdev = ProcessOneAvirtSubdev(mixer, loop, streamsJ, 0);
                if (!subdev)
                    goto fail_loop_subdev;
                loop->nbSubdevs++;
                cds_list_add_tail(&subdev->list, &loop->subdevs.list);
                break;
            case json_type_array: {
                int nbSubDevs = (int) json_object_array_length(streamsJ);
                if (nbSubDevs >= mixer->max.streams)
                    goto fail_loop_subdev;
                for (int idx = 0; idx < nbSubDevs; idx++) {
                    json_object *streamJ = json_object_array_get_idx(streamsJ, idx);
                    subdev = ProcessOneAvirtSubdev(mixer, loop, streamJ, idx);
                    if (!subdev) {
                        goto fail_loop_subdev;
                    }
                    loop->nbSubdevs++;
                    cds_list_add_tail(&subdev->list, &loop->subdevs.list);
                }
                break;
            }
            default:
                goto fail_snd_card_ctl;
        }

        snd_avirt_card_seal();

        // try to open sound card control interface
        loop->sndcard->ctl = AlsaByPathOpenCtl(mixer, loop->uid, loop->sndcard);
        if (!loop->sndcard->ctl) {
            AFB_API_ERROR(mixer->api, "%s mixer=%s hal=%s Fail open sndcard loop=%s devpath=%s cardid=%s (please check 'modprobe snd_aloop')",
                __func__, mixer->uid, uid, loop->uid, loop->sndcard->cid.devpath, loop->sndcard->cid.cardid);
            goto fail_snd_card;
        }
    }

	AFB_API_NOTICE(mixer->api, "%s, uid %s DONE", __func__, uid);

    return loop;

fail_loop_subdev: {
	AFB_API_DEBUG(mixer->api, "%s cleanup", __func__);
	AlsaLoopSubdevT * subdev, *tmp;
	cds_list_for_each_entry_safe(subdev, tmp, &loop->subdevs.list, list) {
		cds_list_del(&subdev->list);
		freeSubdev(mixer, subdev);
	}
}

fail_snd_card_ctl:
	snd_ctl_close(loop->sndcard->ctl);
fail_snd_card:
	free(loop->sndcard);
fail_loop:
	free(loop);
fail:
    return NULL;
}

static void loopDestroy(SoftMixerT * mixer, void* arg) {
	AlsaSndLoopT * loop = (AlsaSndLoopT*) arg;
	AFB_API_DEBUG(mixer->api, "%s... %s", __func__, loop->uid);

	AlsaLoopSubdevT * subdev, *tmp;
	cds_list_for_each_entry_safe(subdev, tmp, &loop->subdevs.list, list) {
		cds_list_del(&subdev->list);
		freeSubdev(mixer, subdev);
	}

	if (loop->sndcard->ctl)
		snd_ctl_close(loop->sndcard->ctl);

	mixer->nbLoops--;
	cds_list_del(&loop->list);
	free(loop);
	AFB_API_DEBUG(mixer->api, "DONE !");

}

static AlsaSndLoopT * loopCreate(SoftMixerT *mixer, const char *uid, json_object *argsJ, json_object *streamsJ) {

	AlsaSndLoopT * newLoop = AttachOneLoop(mixer, uid, argsJ, streamsJ);
	if (!newLoop) {
		goto fail;
	}

	mixer->nbLoops++;
	cds_list_add(&newLoop->list, &mixer->loops.list);
	AlsaMixerTransactionObjectAdd(mixer->transaction, newLoop, loopDestroy);

	loopsDisplay(mixer);


fail:
	return newLoop;
}

PUBLIC int ApiLoopAttach(SoftMixerT *mixer, afb_req_t request, const char * uid, json_object * argsJ, json_object *streamsJ) {

	AlsaSndLoopT * newLoop = NULL;

	if (mixer->nbLoops >= mixer->max.loops) {
        AFB_IfReqFailF(mixer, request, "too-small", "mixer=%s hal=%s max loop=%d", mixer->uid, uid, mixer->max.loops);
		goto fail;
    }

    switch (json_object_get_type(argsJ)) {
            size_t count;

        case json_type_object:
			newLoop = loopCreate(mixer, uid, argsJ, streamsJ);
			if (!newLoop) {
				goto fail;
            }
            break;

        case json_type_array:
            count = json_object_array_length(argsJ);
			if (count > (mixer->max.loops - mixer->nbLoops)) {
                AFB_IfReqFailF(mixer, request, "too-small", "mixer=%s hal=%s max loop=%d", mixer->uid, uid, mixer->max.loops);
				goto fail_loop;
            }

            for (int idx = 0; idx < count; idx++) {
                json_object *loopJ = json_object_array_get_idx(argsJ, idx);
				newLoop = loopCreate(mixer, uid, loopJ, streamsJ);
				if (newLoop == NULL) {
					goto fail_loop;
                }
            }
            break;
        default:
            AFB_IfReqFailF(mixer, request, "bad-loop", "mixer=%s hal=%s loops invalid argsJ= %s", mixer->uid, uid, json_object_get_string(argsJ));
			goto fail;
    }

    return 0;

fail_loop:
fail:
    return -1;
}


static void subdevDisplay(SoftMixerT  *mixer, AlsaLoopSubdevT * subdev) {
	AFB_API_INFO(mixer->api, "\t\tsubdev id %d, uid %s", subdev->numid, subdev->uid);
}

PUBLIC void loopsDisplay(SoftMixerT *mixer) {
	AlsaSndLoopT * loop;
	AFB_API_INFO(mixer->api, "Mixer %s LOOPS:", mixer->uid);
	cds_list_for_each_entry(loop, &mixer->loops.list, list) {
		AFB_API_INFO(mixer->api, "\tLOOP %s", loop->uid);
		AlsaLoopSubdevT * subdev;
		cds_list_for_each_entry(subdev, &loop->subdevs.list, list) {
			subdevDisplay(mixer, subdev);
		}
	}
	AFB_API_INFO(mixer->api, "--------------");
}