forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.c
More file actions
452 lines (380 loc) · 11.5 KB
/
plugin.c
File metadata and controls
452 lines (380 loc) · 11.5 KB
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
/*-*- linux-c -*-*/
/*
* ALSA <-> SOF PCM I/O plugin
*
* Copyright(c) 2022 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <sys/poll.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <assert.h>
#include <errno.h>
#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h>
#include "plugin.h"
#include "common.h"
int plug_mq_cmd_tx_rx(struct plug_mq_desc *ipc_tx, struct plug_mq_desc *ipc_rx,
void *msg, size_t len, void *reply, size_t rlen)
{
struct timespec ts;
ssize_t ipc_size;
char mailbox[IPC3_MAX_MSG_SIZE];
int err;
if (len > IPC3_MAX_MSG_SIZE) {
SNDERR("ipc: message too big %d\n", len);
return -EINVAL;
}
memset(mailbox, 0, IPC3_MAX_MSG_SIZE);
memcpy(mailbox, msg, len);
/* wait for sof-pipe reader to consume data or timeout */
err = clock_gettime(CLOCK_REALTIME, &ts);
if (err == -1) {
SNDERR("ipc: cant get time: %s", strerror(errno));
return -errno;
}
/* IPCs should be read under 10ms */
plug_timespec_add_ms(&ts, 10);
/* now return message completion status */
err = mq_timedsend(ipc_tx->mq, mailbox, IPC3_MAX_MSG_SIZE, 0, &ts);
if (err == -1) {
SNDERR("error: timeout can't send IPC message queue %s : %s\n",
ipc_tx->queue_name, strerror(errno));
return -errno;
}
/* wait for sof-pipe reader to consume data or timeout */
err = clock_gettime(CLOCK_REALTIME, &ts);
if (err == -1) {
SNDERR("ipc: cant get time: %s", strerror(errno));
return -errno;
}
/* IPCs should be processed under 20ms, but wait longer as
* some can take longer especially in valgrind
*/
plug_timespec_add_ms(&ts, 20);
ipc_size = mq_timedreceive(ipc_rx->mq, mailbox, IPC3_MAX_MSG_SIZE, NULL, &ts);
if (ipc_size == -1) {
//fprintf(stderr, "dbg: timeout can't read IPC message queue %s : %s retrying\n",
// ipc->queue_name, strerror(errno));
/* ok, its a long IPC or valgrind, wait longer */
plug_timespec_add_ms(&ts, 800);
ipc_size = mq_timedreceive(ipc_rx->mq, mailbox, IPC3_MAX_MSG_SIZE, NULL, &ts);
if (ipc_size == -1) {
SNDERR("error: timeout can't read IPC message queue %s : %s\n",
ipc_rx->queue_name, strerror(errno));
return -errno;
}
/* needed for valgrind to complete MQ op before next client IPC */
ts.tv_nsec = 20 * 1000 * 1000;
ts.tv_sec = 0;
nanosleep(&ts, NULL);
}
/* do the message work */
if (rlen && reply)
memcpy(reply, mailbox, rlen);
return 0;
}
int plug_mq_cmd(struct plug_mq_desc *ipc, void *msg, size_t len, void *reply, size_t rlen)
{
return plug_mq_cmd_tx_rx(ipc, ipc, msg, len, reply, rlen);
}
/*
* Open an existing message queue using IPC object.
*/
int plug_mq_open(struct plug_mq_desc *ipc)
{
/* now open new queue for Tx and Rx */
ipc->mq = mq_open(ipc->queue_name, O_RDWR);
if (ipc->mq < 0) {
// SNDERR("failed to open IPC queue %s: %s\n",
// ipc->queue_name, strerror(errno));
return -errno;
}
return 0;
}
/*
* Open an existing semaphore using lock object.
*/
int plug_lock_open(struct plug_sem_desc *lock)
{
lock->sem = sem_open(lock->name, O_RDWR);
if (lock->sem == SEM_FAILED) {
SNDERR("failed to open semaphore %s: %s\n", lock->name, strerror(errno));
return -errno;
}
return 0;
}
#define itemsize(type, member) sizeof(((type *)0)->member)
static int parse_conf_long(snd_config_t *cfg, void *obj, size_t size)
{
long val;
if (snd_config_get_integer(cfg, &val) < 0)
return -EINVAL;
*((long *)obj) = val;
return 0;
}
static int parse_conf_str(snd_config_t *cfg, void *obj, size_t size)
{
const char *id;
if (snd_config_get_id(cfg, &id) < 0)
return -EINVAL;
strncpy(obj, id, size);
return 0;
}
static int parse_conf_format(snd_config_t *cfg, void *obj, size_t size)
{
const char *id;
if (snd_config_get_string(cfg, &id) < 0)
return -EINVAL;
if (!strcmp("S16_LE", id)) {
*((long *)obj) = SND_PCM_FORMAT_S16_LE;
return 0;
}
if (!strcmp("S32_LE", id)) {
*((long *)obj) = SND_PCM_FORMAT_S32_LE;
return 0;
}
if (!strcmp("S24_4LE", id)) {
*((long *)obj) = SND_PCM_FORMAT_S24_LE;
return 0;
}
if (!strcmp("FLOAT", id)) {
*((long *)obj) = SND_PCM_FORMAT_FLOAT_LE;
return 0;
}
/* not found */
SNDERR("error: cant find format: %s", id);
return -EINVAL;
}
struct config_item {
char *name;
size_t size;
size_t offset;
int (*copy)(snd_config_t *cfg, void *obj, size_t size);
};
struct config_item config_items[] = {
{"name", itemsize(struct plug_config, name),
offsetof(struct plug_config, name), parse_conf_str},
{"rate", itemsize(struct plug_config, rate),
offsetof(struct plug_config, rate), parse_conf_long},
{"format", itemsize(struct plug_config, format),
offsetof(struct plug_config, format), parse_conf_format},
{"channels", itemsize(struct plug_config, channels),
offsetof(struct plug_config, channels), parse_conf_long},
{"period_time", itemsize(struct plug_config, period_time),
offsetof(struct plug_config, period_time), parse_conf_long},
{"period_frames", itemsize(struct plug_config, period_frames),
offsetof(struct plug_config, period_frames), parse_conf_long},
{"buffer_time", itemsize(struct plug_config, buffer_time),
offsetof(struct plug_config, buffer_time), parse_conf_long},
{"buffer_frames", itemsize(struct plug_config, buffer_frames),
offsetof(struct plug_config, buffer_frames), parse_conf_long},
};
static int parse_item(snd_config_t *cfg, const char *id, struct plug_config *dest_cfg)
{
void *dest = dest_cfg;
int i;
for (i = 0; i < ARRAY_SIZE(config_items); i++) {
/* does ID match */
if (strcmp(id, config_items[i].name))
continue;
/* now get the value */
return config_items[i].copy(cfg, dest + config_items[i].offset,
config_items[i].size);
}
/* not found - non fatal */
return 0;
}
static int parse_slave_configs(snd_sof_plug_t *plug, snd_config_t *n)
{
snd_config_iterator_t si1, si2, snext1, snext2;
struct plug_config *config;
const char *id;
fprintf(stdout, "Parsing ALSA conf for configs\n");
snd_config_for_each(si1, snext1, n) {
snd_config_t *sn1 = snd_config_iterator_entry(si1);
config = &plug->config[plug->num_configs];
/* get config name */
if (parse_item(sn1, "name", config) < 0) {
SNDERR("error: cant find config name");
return -EINVAL;
}
/* now get item values in each config */
snd_config_for_each(si2, snext2, sn1) {
snd_config_t *sn2 = snd_config_iterator_entry(si2);
if (snd_config_get_id(sn2, &id) < 0)
continue;
if (parse_item(sn2, id, config) < 0) {
SNDERR("error: malformed config: %s", id);
return -EINVAL;
}
}
fprintf(stdout, " config %d: %s\n", plug->num_configs,
config->name);
/* next config */
plug->num_configs++;
if (plug->num_configs >= PLUG_MAX_CONFIG) {
SNDERR("error: too many configs");
return -EINVAL;
}
}
return 0;
}
/*
* Parse the client cmdline. Format is
* tplg:pcm:card:dev:config[dai_pipe:card:dev:config]...]
*/
static int parse_client_cmdline(snd_sof_plug_t *plug, char *cmdline)
{
struct plug_cmdline_item *cmd_item;
char *tplg, *next, *card, *dev, *config, *pcm;
char *tplg_path = getenv("SOF_PLUGIN_TOPOLOGY_PATH");
char tplg_file[128];
int ret;
int i;
if (!tplg_path) {
SNDERR("Invalid topology path. Please set the SOF_PLUGIN_TOPOLOGY_PATH env variable\n");
return -EINVAL;
}
/* get topology file */
tplg = strtok_r(cmdline, ":", &next);
if (!tplg) {
SNDERR("invalid cmdline, cant find topology %s", cmdline);
return -EINVAL;
}
/* now convert to filename and add the topology path */
ret = snprintf(tplg_file, sizeof(tplg_file), "%ssof-%s.tplg", tplg_path, tplg);
if (ret < 0) {
SNDERR("invalid cmdline topology file %s", tplg);
return -EINVAL;
}
plug->tplg_file = strdup(tplg_file);
if (!plug->tplg_file)
return -ENOMEM;
/* get PCM ID */
pcm = strtok_r(next, ":", &next);
if (!pcm) {
SNDERR("invalid cmdline, cant find PCM %s", pcm);
return -EINVAL;
}
plug->pcm_id = atoi(pcm);
fprintf(stdout, "Parsing cmd line\n");
cmd_item = &plug->cmdline[plug->num_cmdline];
card = strtok_r(next, ":", &next);
if (!card) {
SNDERR("Invalid card name\n");
return -EINVAL;
}
dev = strtok_r(next, ":", &next);
if (!dev) {
SNDERR("Invalid dev name\n");
return -EINVAL;
}
config = strtok_r(next, ":", &next);
/* tuple needs all three, any missing ? */
if (!config) {
SNDERR("invalid cmdline, expected pcm(%s):card(%s):dev(%s):config(%s) from %s",
pcm, card, dev, config, tplg);
return -EINVAL;
}
cmd_item->pcm = atoi(pcm);
strncpy(cmd_item->card_name, card, sizeof(cmd_item->card_name));
strncpy(cmd_item->dev_name, dev, sizeof(cmd_item->dev_name));
strncpy(cmd_item->config_name, config, sizeof(cmd_item->config_name));
/*
* dev name is special, we cant use "," in the command line
* so need to replace it with a "." and then later change it
* back to ","
*/
for (i = 0; i < sizeof(cmd_item->dev_name); i++) {
if (cmd_item->dev_name[i] != '.')
continue;
cmd_item->dev_name[i] = ',';
break;
}
fprintf(stdout, " cmd %d: for pcm %d uses %s with PCM %s:%s\n",
plug->num_cmdline, cmd_item->pcm, cmd_item->config_name,
cmd_item->card_name, cmd_item->dev_name);
plug->num_cmdline++;
printf("plug: topology file %s with pipe %ld\n", plug->tplg_file, plug->tplg_pipeline);
return 0;
}
/*
* Parse the ALSA conf for the SOF plugin and construct the command line options
* to be passed into the SOF pipe executable.
* TODO: verify all args
* TODO: validate all args.
* TODO: contruct sof pipe cmd line.
*/
int plug_parse_conf(snd_sof_plug_t *plug, const char *name, snd_config_t *root,
snd_config_t *conf)
{
snd_config_iterator_t i, next;
const char *tplg = NULL;
/*
* The topology filename and topology PCM need to be passed in.
* i.e. aplay -Dsof:tplg:pcm:[card:dev:config]...]
*/
snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id;
if (snd_config_get_id(n, &id) < 0)
continue;
/* dont care */
if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 ||
strcmp(id, "hint") == 0)
continue;
/* client command line topology */
if (strcmp(id, "tplg") == 0) {
if (snd_config_get_string(n, &tplg) < 0) {
SNDERR("Invalid type for %s", id);
return -EINVAL;
} else if (!*tplg) {
tplg = NULL;
}
continue;
}
/* topology PCM configurations */
if (strcmp(id, "config") == 0) {
if (parse_slave_configs(plug, n))
return -EINVAL;
continue;
}
/* not fatal - carry on and verify later */
SNDERR("Unknown field %s", id);
}
/* verify mandatory inputs are specified */
if (!tplg) {
SNDERR("Missing topology topology");
return -EINVAL;
}
/* parse the client command line */
if (parse_client_cmdline(plug, (char *)tplg)) {
SNDERR("invalid sof cmd line");
return -EINVAL;
}
return 0;
}