Repository navigation
Expand file tree
/
Copy pathpipeline.c
More file actions
380 lines (309 loc) · 8.82 KB
/
Copy pathpipeline.c
File metadata and controls
380 lines (309 loc) · 8.82 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2022 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
/*
* SOF pipeline in userspace.
*/
#include <stdio.h>
#include <sys/poll.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
#include <mqueue.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <limits.h>
#include <dlfcn.h>
#include <rtos/sof.h>
#include <sof/audio/pipeline.h>
#include <sof/audio/component.h>
#include <sof/audio/component_ext.h>
#include <rtos/task.h>
#include <sof/lib/notifier.h>
#include <sof/schedule/edf_schedule.h>
#include <sof/schedule/ll_schedule.h>
#include <sof/schedule/ll_schedule_domain.h>
#include <sof/schedule/schedule.h>
#include <stdatomic.h>
#include "common.h"
#include "pipe.h"
#define MAX_PIPE_USERS 8
static struct ll_schedule_domain domain = {0};
// TODO: all these steps are probably not needed - i.e we only need IPC and pipeline.
int pipe_sof_setup(struct sof *sof)
{
/* register modules */
domain.next_tick = 0;
/* init components */
sys_comp_init(sof);
/* other necessary initializations, todo: follow better SOF init */
pipeline_posn_init(sof);
init_system_notify(sof);
/* init IPC */
if (ipc_init(sof) < 0) {
fprintf(stderr, "error: IPC init\n");
return -EINVAL;
}
/* init LL scheduler */
if (scheduler_init_ll(&domain) < 0) {
fprintf(stderr, "error: edf scheduler init\n");
return -EINVAL;
}
/* init EDF scheduler */
if (scheduler_init_edf() < 0) {
fprintf(stderr, "error: edf scheduler init\n");
return -EINVAL;
}
return 0;
}
static inline int pipe_copy_ready(struct pipethread_data *pd)
{
struct timespec delay;
int err;
/* get the current time for source delay */
err = clock_gettime(CLOCK_REALTIME, &delay);
if (err == -1) {
fprintf(_sp->log, "shm: cant get time: %s", strerror(errno));
return -errno;
}
// TODO get from rate
plug_timespec_add_ms(&delay, 2000);
/* wait for data from source */
err = sem_timedwait(pd->ready.sem, &delay);
if (err == -1) {
fprintf(_sp->log, "%s %d: fatal timeout: %s on %s\n", __FILE__, __LINE__,
strerror(errno), pd->ready.name);
return -errno;
}
return 0;
}
static inline void pipe_copy_done(struct pipethread_data *pd)
{
/* tell peer we are done */
sem_post(pd->done.sem);
}
static void *pipe_process_thread(void *arg)
{
struct pipethread_data *pd = arg;
int err;
fprintf(_sp->log, "pipe thread started for pipeline %d\n",
pd->pcm_pipeline->pipeline_id);
do {
if (pd->pcm_pipeline->status != COMP_STATE_ACTIVE) {
fprintf(_sp->log, "pipe state non active %d\n",
pd->pcm_pipeline->status);
break;
}
if (pd->pipe_users <= 0) {
fprintf(_sp->log, "pipe no users.\n");
break;
}
/* wait for pipe to be ready */
err = pipe_copy_ready(pd);
if (err < 0) {
fprintf(_sp->log, "pipe ready timeout on pipeline %d state %d users %d\n",
pd->pcm_pipeline->pipeline_id, pd->pcm_pipeline->status,
pd->pipe_users);
break;
}
/* sink has read data so now generate more it */
err = pipeline_copy(pd->pcm_pipeline);
pipe_copy_done(pd);
if (err < 0) {
fprintf(_sp->log, "pipe thread error %d\n", err);
break;
} else if (err > 0) {
fprintf(_sp->log, "pipe thread complete %d\n", err);
break;
}
} while (1);
fprintf(_sp->log, "pipe complete for pipeline %d\n",
pd->pcm_pipeline->pipeline_id);
return 0;
}
static void *pipe_ipc_process_thread(void *arg)
{
struct pipethread_data *pd = arg;
int err;
/* initialise semaphores to default starting value */
err = sem_init(pd->done.sem, 1, 0);
if (err < 0) {
fprintf(_sp->log, "failed to reset DONE: %s\n",
strerror(errno));
return NULL;
}
err = sem_init(pd->ready.sem, 1, 0);
if (err < 0) {
fprintf(_sp->log, "failed to reset READY: %s\n",
strerror(errno));
return NULL;
}
err = pipe_ipc_process(pd->sp, &pd->ipc_tx_mq, &pd->ipc_rx_mq);
if (err < 0) {
fprintf(_sp->log, "pipe IPC thread error for pipeline %d\n",
pd->pcm_pipeline->pipeline_id);
}
return NULL;
}
int pipe_thread_start(struct sof_pipe *sp, struct pipeline *p)
{
struct pipethread_data *pipeline_ctx = sp->pipeline_ctx;
struct pipethread_data *pd;
int pipeline_id;
int ret, pipe_users;
pipeline_id = p->pipeline_id;
if (pipeline_id >= MAX_PIPELINES) {
fprintf(_sp->log, "error: pipeline ID %d out of range\n", pipeline_id);
return -EINVAL;
}
if (!pipeline_ctx[pipeline_id].sp) {
fprintf(_sp->log, "error: pipeline ID %d not in use\n", pipeline_id);
return -EINVAL;
}
pd = &pipeline_ctx[pipeline_id];
/* only create thread if not active */
pipe_users = atomic_fetch_add(&pd->pipe_users, 1);
if (pipe_users > 0) {
fprintf(_sp->log, "pipeline ID %d thread already running %d users\n",
pipeline_id, pipe_users);
return 0;
}
fprintf(_sp->log, "pipeline ID %d thread not running so starting...\n", pipeline_id);
/* first user so start the PCM pipeline thread */
ret = pthread_create(&pd->pcm_thread, NULL, pipe_process_thread, pd);
if (ret < 0) {
fprintf(_sp->log, "failed to create PCM thread: %s\n", strerror(errno));
return -errno;
}
return ret;
}
int pipe_thread_stop(struct sof_pipe *sp, struct pipeline *p)
{
struct pipethread_data *pipeline_ctx = sp->pipeline_ctx;
struct pipethread_data *pd;
int pipeline_id;
int ret, pipe_users;
pipeline_id = p->pipeline_id;
/* this is called when the pipeline is PAUSED for the first time before RUNNING */
if (p->status == COMP_STATE_INIT)
return 0;
if (pipeline_id >= MAX_PIPELINES) {
fprintf(_sp->log, "error: pipeline ID %d out of range\n", pipeline_id);
return -EINVAL;
}
if (!pipeline_ctx[pipeline_id].sp) {
fprintf(_sp->log, "error: pipeline ID %d not in use\n", pipeline_id);
return -EINVAL;
}
pd = &pipeline_ctx[pipeline_id];
/* only join thread if not active */
pipe_users = atomic_fetch_sub(&pd->pipe_users, 1);
if (pipe_users != 1) {
fprintf(_sp->log, "pipeline ID %d thread has multiple %d users\n",
pipeline_id, pipe_users);
return 0;
}
fprintf(_sp->log, "pipeline ID %d thread can be stopped...\n", pipeline_id);
ret = pthread_cancel(pd->pcm_thread);
if (ret < 0) {
fprintf(_sp->log, "failed to cancel PCM thread: %s\n", strerror(errno));
return -errno;
}
return ret;
}
int pipe_thread_new(struct sof_pipe *sp, struct pipeline *p)
{
struct pipethread_data *pipeline_ctx = sp->pipeline_ctx;
struct pipethread_data *pd;
int ret;
if (!p) {
fprintf(_sp->log, "error: invalid pipeline\n");
return -EINVAL;
}
if (p->pipeline_id >= MAX_PIPELINES) {
fprintf(_sp->log, "error: pipeline ID %d out of range\n", p->pipeline_id);
return -EINVAL;
}
if (pipeline_ctx[p->pipeline_id].sp) {
fprintf(_sp->log, "error: pipeline ID %d in use\n", p->pipeline_id);
return -EINVAL;
}
pd = &pipeline_ctx[p->pipeline_id];
pd->sp = _sp;
pd->pcm_pipeline = p;
/* initialise global IPC data */
/* TODO: change the PCM name to tplg or make it per PID*/
ret = plug_mq_init(&pd->ipc_tx_mq, pd->sp->topology_name, "pcm-tx", p->pipeline_id);
if (ret < 0)
return -EINVAL;
mq_unlink(pd->ipc_tx_mq.queue_name);
ret = plug_mq_init(&pd->ipc_rx_mq, pd->sp->topology_name, "pcm-rx", p->pipeline_id);
if (ret < 0)
return -EINVAL;
mq_unlink(pd->ipc_rx_mq.queue_name);
/* init names of shared resources */
ret = plug_lock_init(&pd->ready, _sp->topology_name, "ready", p->pipeline_id);
if (ret < 0)
return ret;
ret = plug_lock_init(&pd->done, _sp->topology_name, "done", p->pipeline_id);
if (ret)
return ret;
/* open semaphores */
ret = plug_lock_create(&pd->ready);
if (ret < 0)
return ret;
ret = plug_lock_create(&pd->done);
if (ret < 0)
goto lock_err;
/* start IPC pipeline thread */
ret = pthread_create(&pd->ipc_thread, NULL, pipe_ipc_process_thread, pd);
if (ret < 0) {
fprintf(_sp->log, "failed to create IPC thread: %s\n", strerror(errno));
ret = -errno;
goto lock2_err;
}
return 0;
lock2_err:
plug_lock_free(&pd->done);
lock_err:
plug_lock_free(&pd->ready);
return ret;
}
int pipe_thread_free(struct sof_pipe *sp, int pipeline_id)
{
struct pipethread_data *pipeline_ctx = sp->pipeline_ctx;
struct pipethread_data *pd;
int err;
if (pipeline_id >= MAX_PIPELINES) {
fprintf(_sp->log, "error: pipeline ID %d out of range\n", pipeline_id);
return -EINVAL;
}
if (!pipeline_ctx[pipeline_id].sp) {
fprintf(_sp->log, "error: pipeline ID %d not in use\n", pipeline_id);
return -EINVAL;
}
pd = &pipeline_ctx[pipeline_id];
err = pthread_cancel(pd->ipc_thread);
if (err < 0) {
fprintf(_sp->log, "failed to create IPC thread: %s\n", strerror(errno));
return -errno;
}
plug_mq_free(&pd->ipc_tx_mq);
mq_unlink(pd->ipc_tx_mq.queue_name);
plug_mq_free(&pd->ipc_rx_mq);
mq_unlink(pd->ipc_rx_mq.queue_name);
plug_lock_free(&pd->ready);
plug_lock_free(&pd->done);
pd->sp = NULL;
return 0;
}