summaryrefslogtreecommitdiffstats
path: root/plugins/alsa/alsa-api-loop.c
blob: 16491a0cd458352885dbf6e7dea59a774fef224d (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
/*
 * 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>

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 = streamUid;
					*loop = _loop;
					return subdev;
				}
			}
		}
	}
	return NULL;
}


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

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

	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_ApiError(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;
		}
	}

    // 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) {
		goto fail_subdev_uid;
	}

	pcmCtl->closeAtDeletion = true;

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

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

    return subdev;

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

static void freeSubdev(AlsaLoopSubdevT * subdev) {
	if (subdev->uid)
		free((char*)subdev->uid);
	free(subdev);
}


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

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

	AFB_ApiNotice(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,so,so !}"
            , "uid", &loop->uid
            , "path", &loop->sndcard->cid.devpath
            , "cardid", &loop->sndcard->cid.cardid
            , "devices", &devicesJ
            , "subdevs", &subdevsJ
            );
    if (error || !loop->uid || !subdevsJ || (!loop->sndcard->cid.devpath && !loop->sndcard->cid.cardid)) {
        AFB_ApiNotice(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;
    }

    // try to open sound card control interface
    loop->sndcard->ctl = AlsaByPathOpenCtl(mixer, loop->uid, loop->sndcard);
    if (!loop->sndcard->ctl) {
        AFB_ApiError(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_ApiNotice(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_ApiError(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;
    }

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

    return loop;

fail_loop_subdev: {
	AFB_ApiDebug(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(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_ApiDebug(mixer->api, "%s... %s not implemented", __func__, loop->uid);
	return;

	if (loop->sndcard) {
		if (loop->sndcard->ctl) {
			snd_ctl_close(loop->sndcard->ctl);
			free(loop->sndcard->ctl);
		}
		free(loop->sndcard);
	}

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

}

static AlsaSndLoopT * loopCreate(SoftMixerT *mixer, const char *uid, json_object *argsJ) {
	AlsaSndLoopT * newLoop = AttachOneLoop(mixer, uid, argsJ);
	if (!newLoop) {
		goto fail;
	}

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

fail:
	return newLoop;
}

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

	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);
			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);
				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;
}