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
|
/*
* 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_MIXER // needed for vasprintf
#include "alsa-softmixer.h"
#include <string.h>
// Fulup need to be cleanup with new controller version
extern Lua2cWrapperT Lua2cWrap;
PUBLIC AlsaSndZoneT *ApiZoneGetByUid(SoftMixerT *mixer, const char *target) {
AlsaSndZoneT * zone = NULL;
if (mixer->nbZones == 0) {
AFB_ApiError(mixer->api, "%s mixer=%s does not have any zone", __func__, mixer->uid);
goto fail;
}
// search for subdev into every registered loop
cds_list_for_each_entry(zone, &mixer->zones.list, list) {
if (zone->uid && !strcasecmp(zone->uid, target)) {
return zone;
}
}
fail:
AFB_ApiError(mixer->api, "%s mixer=%s fail to find zone=%s", __func__, mixer->uid, target);
return NULL;
}
STATIC AlsaPcmChannelT* ProcessOneChannel(SoftMixerT *mixer, const char* uid, json_object *channelJ) {
AlsaPcmChannelT *channel = calloc(1, sizeof (AlsaPcmChannelT));
if (!channel) {
SOFTMIXER_NOMEM(mixer->api);
goto fail;
}
CDS_INIT_LIST_HEAD(&channel->list);
channel->volume = -1;
int error = wrap_json_unpack(channelJ, "{ss,si,s?f !}"
, "target", &channel->uid
, "channel", &channel->port
, "volume", &channel->volume
);
if (error)
goto fail;
channel->uid = strdup(channel->uid);
if (!channel->uid) {
SOFTMIXER_NOMEM(mixer->api);
goto fail_channel;
}
AFB_ApiDebug(mixer->api, "%s: uid %s, channel uid %s, port %d, volume %f",
__func__, uid, channel->uid, channel->port, channel->volume);
return channel;
fail_channel:
free(channel);
fail:
AFB_ApiError(mixer->api, "%s: zone=%s channel: missing (target|channel) json=%s", __func__, uid, json_object_get_string(channelJ));
return NULL;
}
STATIC AlsaSndZoneT *AttachOneZone(SoftMixerT *mixer, const char *uid, json_object *zoneJ) {
AFB_ApiDebug(mixer->api, "%s uid %s", __func__, uid);
json_object *sinkJ = NULL, *sourceJ = NULL;
size_t count;
int error;
AlsaSndZoneT *zone = calloc(1, sizeof (AlsaSndZoneT));
if (!zone) {
SOFTMIXER_NOMEM(mixer->api);
goto fail;
}
CDS_INIT_LIST_HEAD(&zone->list);
CDS_INIT_LIST_HEAD(&zone->sinks.list);
CDS_INIT_LIST_HEAD(&zone->sources.list);
error = wrap_json_unpack(zoneJ, "{ss,s?o,s?o !}"
, "uid", &zone->uid
, "sink", &sinkJ
, "source", &sourceJ
);
if (error || (!sinkJ && sourceJ)) {
AFB_ApiNotice(mixer->api, "%s missing 'uid|sink|source' error=%s zone=%s", __func__, wrap_json_get_error_string(error), json_object_get_string(zoneJ));
goto fail_zone;
}
// make sure remain valid even when json object is removed
zone->uid = strdup(zone->uid);
if (!zone->uid) {
SOFTMIXER_NOMEM(mixer->api);
goto fail_zone;
}
AlsaPcmChannelT * channel = NULL;
if (sinkJ) {
switch (json_object_get_type(sinkJ)) {
case json_type_object:
channel = ProcessOneChannel(mixer, zone->uid, sinkJ);
if (!channel)
goto fail_uid;
zone->nbSinks++;
cds_list_add_tail(&channel->list, &zone->sinks.list);
break;
case json_type_array:
count = json_object_array_length(sinkJ);
for (int idx = 0; idx < count; idx++) {
json_object *subdevJ = json_object_array_get_idx(sinkJ, idx);
channel = ProcessOneChannel(mixer, zone->uid, subdevJ);
if (!channel)
goto fail_uid;
zone->nbSinks++;
cds_list_add_tail(&channel->list, &zone->sinks.list);
}
break;
default:
AFB_ApiError(mixer->api, "%s: Mixer=%s Hal=%s zone=%s invalid mapping=%s", __func__, mixer->uid, uid, zone->uid, json_object_get_string(sinkJ));
goto fail;
}
}
if (sourceJ) {
switch (json_object_get_type(sourceJ)) {
case json_type_object:
channel = ProcessOneChannel(mixer, zone->uid, sourceJ);
if (!channel)
goto fail_uid;
zone->nbSources++;
cds_list_add(&channel->list, &zone->sources.list);
break;
case json_type_array:
count = json_object_array_length(sourceJ);
for (int idx = 0; idx < count; idx++) {
json_object *subdevJ = json_object_array_get_idx(sourceJ, idx);
channel = ProcessOneChannel(mixer, zone->uid, subdevJ);
if (!zone)
goto fail_uid;
zone->nbSources++;
cds_list_add(&channel->list, &zone->sources.list);
}
break;
default:
AFB_ApiError(mixer->api, "%s: Mixer=%s Hal=%s zone=%s mapping=%s",
__func__, mixer->uid, uid, zone->uid, json_object_get_string(sourceJ));
goto fail;
}
}
AFB_ApiDebug(mixer->api, "%s uid %s DONE", __func__, uid);
return zone;
fail_uid:
free((char*)zone->uid);
fail_zone:
free(zone);
fail:
return NULL;
}
static void zoneDestroy(SoftMixerT* mixer, void * arg) {
AlsaSndZoneT * zone = (AlsaSndZoneT*) arg;
if (zone->routeConfig) {
AFB_ApiDebug(mixer->api, "%s... %s delete route config", __func__, zone->uid);
snd_config_delete(zone->routeConfig);
snd_config_update();
zone->routeConfig = NULL;
}
}
PUBLIC AlsaSndZoneT * zoneCreate(SoftMixerT* mixer, const char * uid, json_object * argsJ) {
AlsaSndZoneT * zone = AttachOneZone(mixer, uid, argsJ);
if (!zone)
goto fail;
// must be set now; AlsaCreateRoute needs it !
mixer->nbZones++;
cds_list_add_tail(&zone->list, &mixer->zones.list);
AlsaMixerTransactionObjectAdd(mixer->transaction, zone, zoneDestroy);
AlsaPcmCtlT *routeConfig = AlsaCreateRoute(mixer, zone, 0);
if (!routeConfig) {
AFB_ApiError(mixer->api,
"%s: Mixer=%s Hal=%s zone=%s Fail to attach PCM Route",
__func__, mixer->uid, uid, zone->uid);
goto fail;
}
fail:
return zone;
}
PUBLIC int ApiZoneAttach(SoftMixerT *mixer, AFB_ReqT request, const char * uid, json_object * argsJ) {
AFB_ApiDebug(mixer->api, "%s uid %s", __func__, uid);
if (mixer->nbZones >= mixer->max.zones) {
AFB_ReqFailF(request, "too-small", "mixer=%s max zone=%d", mixer->uid, mixer->max.zones);
goto OnErrorExit;
}
switch (json_object_get_type(argsJ)) {
long count;
case json_type_object: {
AlsaSndZoneT * newZone = zoneCreate(mixer, uid, argsJ);
if (!newZone) {
AFB_ReqFailF(request, "bad-zone", "mixer=%s invalid zone= %s", mixer->uid, json_object_get_string(argsJ));
goto OnErrorExit;
}
break;
}
case json_type_array:
count = json_object_array_length(argsJ);
if (count > (mixer->max.zones - mixer->nbZones)) {
AFB_ReqFailF(request, "too-small", "mixer=%s max zone=%d", mixer->uid, mixer->max.zones);
goto OnErrorExit;
}
for (int idx = 0; idx < count; idx++) {
json_object *zoneJ = json_object_array_get_idx(argsJ, idx);
AlsaSndZoneT * newZone = zoneCreate(mixer, uid, zoneJ);
if (!newZone) {
AFB_ReqFailF(request, "bad-zone", "mixer=%s invalid zone= %s", mixer->uid, json_object_get_string(zoneJ));
goto OnErrorExit;
}
}
break;
default:
AFB_ReqFailF(request, "invalid-syntax", "mixer=%s zones invalid argsJ= %s", mixer->uid, json_object_get_string(argsJ));
goto OnErrorExit;
}
AFB_ApiDebug(mixer->api, "%s uid %s DONE", __func__, uid);
return 0;
OnErrorExit:
return -1;
}
|