summaryrefslogtreecommitdiffstats
path: root/plugins/alsa/alsa-plug-route.c
blob: ce17b867246a0aba248494a3e69fd1f97003d179 (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
/*
 * 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 <stdbool.h>

ALSA_PLUG_PROTO(route);

static void routeConfigClean(SoftMixerT *mixer, void * arg) {
	snd_config_t * routeConfig = arg;
	AFB_API_DEBUG(mixer->api, "%s... route config", __func__);
	snd_config_delete(routeConfig);
	snd_config_update();
}


typedef struct {
    const char *uid;
    const char *cardid;
	const char * pcmplug_params;
    int cardidx;
    int ccount;
    int port;
	bool isPcmPlug;
	unsigned int quirks;
} ChannelCardPortT;

STATIC int CardChannelByUid(SoftMixerT *mixer, const char *uid, ChannelCardPortT *response) {

	AlsaSndPcmT * pcm = NULL;
	bool found = false;

    // search for channel within all sound card sink (channel port target is computed by order)
	cds_list_for_each_entry(pcm, &mixer->sinks.list, list) {

	   if (pcm->nbChannels == 0) {
            AFB_API_ERROR(mixer->api,
                         "%s: No Sink card=%s [should declare channels]",
                         __func__, pcm->uid);
            goto OnErrorExit;
        }

	   AlsaPcmChannelT * channel = NULL;
	   cds_list_for_each_entry(channel, &pcm->channels.list, list) {

		   if (channel->uid && !strcasecmp(channel->uid, uid)) {
			   response->port 	  = channel->port;
			   response->uid	 = pcm->uid;
			   response->ccount  = pcm->nbChannels;
			   response->cardid  = pcm->sndcard->cid.cardid;
			   response->cardidx = pcm->sndcard->cid.cardidx;
			   response->pcmplug_params = pcm->sndcard->cid.pcmplug_params;
			   response->isPcmPlug= pcm->isPcmPlug;
			   response->quirks   = pcm->quirks;
			   found = true;
			   break;
		   }
        }
    }

    if (!found) {
        AFB_API_ERROR(mixer->api,
                     "%s: No Channel with uid=%s [should declare channels]",
                     __func__, uid);
        goto OnErrorExit;
    }

    return 0;

OnErrorExit:
    return -1;
}

PUBLIC AlsaPcmCtlT* AlsaCreateRoute(SoftMixerT *mixer, AlsaSndZoneT *zone, int open) {
    snd_config_t *routeConfig = NULL, *elemConfig, *slaveConfig, *tableConfig, *pcmConfig;
	int error = 0;
	ChannelCardPortT slave, channelCardPort;
	AlsaPcmCtlT *pcmRoute = NULL;

    char *cardid = NULL;
	char *slaveUid = NULL;

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

	if (mixer->nbSinks == 0) {
		AFB_API_ERROR(mixer->api, "%s: mixer=%s zone(%s)(zone) No sink found [should Registry sound card first]", __func__, mixer->uid, zone->uid);
		goto fail;
    }

	if (asprintf(&cardid, "route-%s", zone->uid) == -1) {
		SOFTMIXER_NOMEM(mixer->api);
		goto fail;
	}

	pcmRoute = AlsaPcmCtlNew(mixer, cardid);
	if (!pcmRoute) {
		SOFTMIXER_NOMEM(mixer->api);
		goto fail;
	}

    pcmRoute->cid.cardid = (const char *) cardid;
    
    pcmRoute->params = ApiSinkGetParamsByZone(mixer, zone->uid);
	if (pcmRoute->params == NULL) {
		AFB_API_ERROR(mixer->api, "%s: Failed to retrieve zone parameters", __func__);
		goto fail_nodump;
	}

	pcmRoute->params = ApiPcmParamsDup(mixer, pcmRoute->params);
	if (!pcmRoute->params) {
		SOFTMIXER_NOMEM(mixer->api);
		goto fail_nodump;
	}

	zone->params = ApiPcmParamsDup(mixer, pcmRoute->params);
	if (!zone->params) {
		SOFTMIXER_NOMEM(mixer->api);
		goto fail_nodump;
	}
    
    // use 1st zone channel to retrieve sound card name + channel count. 

	AlsaPcmChannelT * channel = cds_list_first_entry(&zone->sinks.list, AlsaPcmChannelT, list);
	error = CardChannelByUid(mixer, channel->uid, &slave);
    if (error) {
		AFB_API_ERROR(mixer->api, "%s:zone(%s) fail to find channel=%s", __func__, zone->uid, channel->uid);
		goto fail;
    }
    
	// DMIX only supports real hardware as a slave

	if (!slave.isPcmPlug)  {
		// move from hardware to DMIX attach to sndcard
		if (asprintf(&slaveUid, "dmix-%s", slave.uid) == -1) {
			SOFTMIXER_NOMEM(mixer->api);
			goto fail;
		}
	} else {
		slaveUid = strdup(slave.pcmplug_params);
		if (!slaveUid) {
			SOFTMIXER_NOMEM(mixer->api);
			goto fail;
		}
		pcmRoute->isPcmPlug = true;
	}

	pcmRoute->quirks = slave.quirks;

    // temporary store to enable multiple channel to route to the same port

	snd_config_t **cports = alloca(zone->nbSinks * sizeof (void*));
	memset(cports, 0, zone->nbSinks * sizeof (void*));
	int portCount = 0;

    // We create 1st ttable to retrieve sndcard slave and channel count
    (void)snd_config_make_compound(&tableConfig, "ttable", 0);

	// used physical ports
	bool targets[16];
	memset(targets, 0, sizeof(targets));

	cds_list_for_each_entry(channel, &zone->sinks.list, list) {

		AFB_API_DEBUG(mixer->api, "%s: zone->sink channel %s ", __func__, channel->uid);

		error = CardChannelByUid(mixer, channel->uid, &channelCardPort);
        if (error) {
			AFB_API_ERROR(mixer->api, "%s: zone(%s) fail to find channel=%s", __func__, zone->uid, channel->uid);
			goto fail;
        }

		if (slave.cardidx != channelCardPort.cardidx) {
			AFB_API_ERROR(mixer->api,
						 "%s: zone(%s) cannot span over multiple sound card %s != %s ",
						 __func__, zone->uid, slave.cardid, channelCardPort.cardid);
			goto fail;
        }

		int target = channelCardPort.port;
		int port = channel->port;

		if (!targets[target]) {
			zone->physicalChannelCount++;
			targets[target] = true;
		}

        double volume = 1.0; // currently only support 100%

		// if channel entry does not exist into ttable create it now
		if (!cports[port]) {

            char channelS[4]; // 999 channels should be more than enough
            snprintf(channelS, sizeof (channelS), "%d", port);

			error = snd_config_make_compound(&cports[port], channelS, 0);
			if (error) {
				AFB_API_ERROR(mixer->api, "%s: make_compound failed , err %s", __func__, snd_strerror(error));
				goto fail;
			}

			error = snd_config_add(tableConfig, cports[port]);
			if (error) {
				AFB_API_ERROR(mixer->api, "%s: add compound to table failed, err %s", __func__, snd_strerror(error));
				goto fail;
			}
        }

		portCount++;

        // ttable require target port as a table and volume as a value
        char targetS[4];
        snprintf(targetS, sizeof (targetS), "%d", target);

		if (channel->volume != -1)
			volume = channel->volume;
		else
			volume = 1.0;

		error = snd_config_imake_real(&elemConfig, targetS, volume);
		if (error) {
			AFB_API_ERROR(mixer->api, "%s: Failed to interpret volume real value, err %s", __func__, snd_strerror(error));
			goto fail;
		}

		error = snd_config_add(cports[port], elemConfig);
		if (error) {
			AFB_API_ERROR(mixer->api, "%s Failed to add ttable entry:err=%s", __func__, snd_strerror(error));
			goto fail;
		}

    }

    // update zone with route channel count and sndcard params
    pcmRoute->ccount = portCount;
    zone->ccount=portCount;

    AFB_API_DEBUG(mixer->api, "%s: ZONE has %d ports and %d hardware channels", __func__, zone->ccount, zone->physicalChannelCount);

    // refresh global alsalib config and create PCM top config
    snd_config_update();
    error += snd_config_top(&routeConfig);
	if (error) goto fail;
    error += snd_config_set_id(routeConfig, cardid);
	if (error) goto fail;
    error += snd_config_imake_string(&elemConfig, "type", "route");
	if (error) goto fail;
    error += snd_config_add(routeConfig, elemConfig);
	if (error) goto fail;
    error += snd_config_make_compound(&slaveConfig, "slave", 0);
	if (error) goto fail;
	error += snd_config_imake_string(&elemConfig, "pcm", slaveUid);
	if (error) goto fail;
    error += snd_config_add(slaveConfig, elemConfig);
	if (error) goto fail;

    error += snd_config_imake_integer(&elemConfig, "channels", slave.ccount);
	if (error) goto fail;
    error += snd_config_add(slaveConfig, elemConfig);

	if (error) goto fail;
    error += snd_config_add(routeConfig, slaveConfig);
	if (error) goto fail;
    error += snd_config_add(routeConfig, tableConfig);
	if (error) goto fail;

    if (open) error = _snd_pcm_route_open(&pcmRoute->handle, pcmRoute->cid.cardid, snd_config, routeConfig, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
    if (error) {
        AFB_API_ERROR(mixer->api,
                     "%s: zone(%s) fail to create Plug=%s error=%s",
                     __func__, zone->uid, pcmRoute->cid.cardid, snd_strerror(error));
		goto fail;
    }

    snd_config_update();
    error += snd_config_search(snd_config, "pcm", &pcmConfig);
    error += snd_config_add(pcmConfig, routeConfig);
    if (error) {
        AFB_API_ERROR(mixer->api,
                     "%s: %s fail to add config route=%s error=%s",
                     __func__, zone->uid, pcmRoute->cid.cardid, snd_strerror(error));
		goto fail;
    }

	pcmRoute->private_data = routeConfig;
	pcmRoute->private_data_clean = routeConfigClean;

    // Debug config & pcm
    AFB_API_NOTICE(mixer->api, "%s: zone(%s) DONE", __func__, zone->uid);
    AlsaDumpCtlConfig(mixer, "plug-route", routeConfig, 1);

    return pcmRoute;

fail:
//	AFB_API_ERROR(mixer->api, "%s ERROR, DUMPING PCM...", __func__);
//	AlsaDumpCtlConfig(mixer, "plug-pcm", snd_config, 1);
	AFB_API_ERROR(mixer->api, "%s ERROR, DUMPING ROUTE...", __func__);
	AlsaDumpCtlConfig(mixer, "plug-route", routeConfig, 1);

fail_nodump:
    free(pcmRoute);
    free(cardid);
	free(slaveUid);

    AFB_API_NOTICE(mixer->api, "%s: zone(%s) FAIL", __func__, zone->uid);
    return NULL;
}