-
Notifications
You must be signed in to change notification settings - Fork 2
/
compress.c
653 lines (559 loc) · 18.1 KB
/
compress.c
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
* BSD LICENSE
*
* tinycompress library for compress audio offload in alsa
* Copyright (c) 2011-2012, Intel Corporation
* All rights reserved.
*
* Author: Vinod Koul <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* LGPL LICENSE
*
* tinycompress library for compress audio offload in alsa
* Copyright (c) 2011-2012, Intel Corporation.
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 program; if not, write to
* the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <poll.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <limits.h>
#include <linux/types.h>
#include <linux/ioctl.h>
#define __force
#define __bitwise
#define __user
#include <sound/asound.h>
#include "sound/compress_params.h"
#include "sound/compress_offload.h"
#include "tinycompress/tinycompress.h"
#include "compress_ops.h"
#include "snd_utils.h"
#define COMPR_ERR_MAX 128
/* Default maximum time we will wait in a poll() - 20 seconds */
#define DEFAULT_MAX_POLL_WAIT_MS 20000
struct compress {
int fd;
unsigned int flags;
char error[COMPR_ERR_MAX];
struct compr_config *config;
int running;
int max_poll_wait_ms;
int nonblocking;
unsigned int gapless_metadata;
unsigned int next_track;
struct compress_ops *ops;
void *data;
void *snd_node;
};
extern struct compress_ops compr_hw_ops;
extern struct compress_ops compr_plug_ops;
static int oops(struct compress *compress, int e, const char *fmt, ...)
{
va_list ap;
int sz;
va_start(ap, fmt);
vsnprintf(compress->error, COMPR_ERR_MAX, fmt, ap);
va_end(ap);
sz = strlen(compress->error);
snprintf(compress->error + sz, COMPR_ERR_MAX - sz,
": %s", strerror(e));
errno = e;
return -1;
}
const char *compress_get_error(struct compress *compress)
{
return compress->error;
}
static struct compress bad_compress = {
.fd = -1,
};
int is_compress_running(struct compress *compress)
{
return ((compress->fd >= 0) && compress->running) ? 1 : 0;
}
int is_compress_ready(struct compress *compress)
{
return (compress->fd >= 0) ? 1 : 0;
}
static int get_compress_version(struct compress *compress)
{
int version = 0;
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_IOCTL_VERSION, &version)) {
oops(compress, errno, "cant read version");
return -1;
}
return version;
}
static bool _is_codec_type_supported(struct compress_ops *ops, void *data,
struct snd_codec *codec)
{
struct snd_compr_caps caps;
bool found = false;
unsigned int i;
if (ops->ioctl(data, SNDRV_COMPRESS_GET_CAPS, &caps)) {
oops(&bad_compress, errno, "cannot get device caps");
return false;
}
for (i = 0; i < caps.num_codecs; i++) {
if (caps.codecs[i] == codec->id) {
/* found the codec */
found = true;
break;
}
}
/* TODO: match the codec properties */
return found;
}
static inline void
fill_compress_params(struct compr_config *config, struct snd_compr_params *params)
{
params->buffer.fragment_size = config->fragment_size;
params->buffer.fragments = config->fragments;
memcpy(¶ms->codec, config->codec, sizeof(params->codec));
}
struct compress *compress_open(unsigned int card, unsigned int device,
unsigned int flags, struct compr_config *config)
{
struct compress *compress;
struct snd_compr_params params;
struct snd_compr_caps caps;
int compress_type;
if (!config) {
oops(&bad_compress, EINVAL, "passed bad config");
return &bad_compress;
}
compress = calloc(1, sizeof(struct compress));
if (!compress) {
oops(&bad_compress, errno, "cannot allocate compress object");
return &bad_compress;
}
compress->next_track = 0;
compress->gapless_metadata = 0;
compress->config = calloc(1, sizeof(*config));
if (!compress->config)
goto input_fail;
compress->max_poll_wait_ms = DEFAULT_MAX_POLL_WAIT_MS;
compress->flags = flags;
if (!((flags & COMPRESS_OUT) || (flags & COMPRESS_IN))) {
oops(&bad_compress, EINVAL, "can't deduce device direction from given flags");
goto config_fail;
}
compress->snd_node = snd_utils_get_dev_node(card, device, NODE_COMPRESS);
compress_type = snd_utils_get_node_type(compress->snd_node);
if (compress_type == SND_NODE_TYPE_PLUGIN)
compress->ops = &compr_plug_ops;
else
compress->ops = &compr_hw_ops;
compress->fd = compress->ops->open(card, device, flags,
&compress->data, compress->snd_node);
if (compress->fd < 0) {
oops(&bad_compress, errno, "cannot open card(%u) device(%u)",
card, device);
goto config_fail;
}
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_GET_CAPS, &caps)) {
oops(compress, errno, "cannot get device caps");
goto codec_fail;
}
/* If caller passed "don't care" fill in default values */
if ((config->fragment_size == 0) || (config->fragments == 0)) {
config->fragment_size = caps.min_fragment_size;
config->fragments = caps.max_fragments;
}
#if 0
/* FIXME need to turn this On when DSP supports
* and treat in no support case
*/
if (_is_codec_supported(compress, config, &caps) == false) {
oops(compress, errno, "codec not supported\n");
goto codec_fail;
}
#endif
memcpy(compress->config, config, sizeof(*compress->config));
fill_compress_params(config, ¶ms);
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_SET_PARAMS, ¶ms)) {
oops(&bad_compress, errno, "cannot set device");
goto codec_fail;
}
return compress;
codec_fail:
snd_utils_put_dev_node(compress->snd_node);
compress->ops->close(compress->data);
compress->fd = -1;
config_fail:
free(compress->config);
input_fail:
free(compress);
return &bad_compress;
}
void compress_close(struct compress *compress)
{
if (compress == &bad_compress)
return;
snd_utils_put_dev_node(compress->snd_node);
compress->ops->close(compress->data);
compress->running = 0;
compress->fd = -1;
free(compress->config);
free(compress);
}
int compress_get_hpointer(struct compress *compress,
unsigned int *avail, struct timespec *tstamp)
{
struct snd_compr_avail kavail;
__u64 time;
if (!is_compress_ready(compress))
return oops(compress, ENODEV, "device not ready");
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_AVAIL, &kavail))
return oops(compress, errno, "cannot get avail");
if (0 == kavail.tstamp.sampling_rate)
return oops(compress, ENODATA, "sample rate unknown");
*avail = (unsigned int)kavail.avail;
time = kavail.tstamp.pcm_io_frames / kavail.tstamp.sampling_rate;
tstamp->tv_sec = time;
time = kavail.tstamp.pcm_io_frames % kavail.tstamp.sampling_rate;
tstamp->tv_nsec = time * 1000000000 / kavail.tstamp.sampling_rate;
return 0;
}
int compress_get_tstamp(struct compress *compress,
unsigned long *samples, unsigned int *sampling_rate)
{
struct snd_compr_tstamp ktstamp;
if (!is_compress_ready(compress))
return oops(compress, ENODEV, "device not ready");
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_TSTAMP, &ktstamp))
return oops(compress, errno, "cannot get tstamp");
*samples = ktstamp.pcm_io_frames;
*sampling_rate = ktstamp.sampling_rate;
return 0;
}
int compress_write(struct compress *compress, const void *buf, unsigned int size)
{
struct snd_compr_avail avail;
struct pollfd fds;
int to_write = 0; /* zero indicates we haven't written yet */
int written, total = 0, ret;
const char* cbuf = buf;
const unsigned int frag_size = compress->config->fragment_size;
if (!(compress->flags & COMPRESS_IN))
return oops(compress, EINVAL, "Invalid flag set");
if (!is_compress_ready(compress))
return oops(compress, ENODEV, "device not ready");
fds.events = POLLOUT;
/*TODO: treat auto start here first */
while (size) {
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_AVAIL, &avail))
return oops(compress, errno, "cannot get avail");
/* We can write if we have at least one fragment available
* or there is enough space for all remaining data
*/
if ((avail.avail < frag_size) && (avail.avail < size)) {
if (compress->nonblocking)
return total;
ret = compress->ops->poll(compress->data, &fds, 1,
compress->max_poll_wait_ms);
if (fds.revents & POLLERR) {
return oops(compress, EIO, "poll returned error!");
}
/* A pause will cause -EBADFD or zero.
* This is not an error, just stop writing */
if ((ret == 0) || (ret < 0 && errno == EBADFD))
break;
if (ret < 0)
return oops(compress, errno, "poll error");
if (fds.revents & POLLOUT) {
continue;
}
}
/* write avail bytes */
if (size > avail.avail)
to_write = avail.avail;
else
to_write = size;
written = compress->ops->write(compress->data, cbuf, to_write);
if (written < 0) {
/* If play was paused the write returns -EBADFD */
if (errno == EBADFD)
break;
return oops(compress, errno, "write failed!");
}
size -= written;
cbuf += written;
total += written;
}
return total;
}
int compress_read(struct compress *compress, void *buf, unsigned int size)
{
struct snd_compr_avail avail;
struct pollfd fds;
int to_read = 0;
int num_read, total = 0, ret;
char* cbuf = buf;
const unsigned int frag_size = compress->config->fragment_size;
if (!(compress->flags & COMPRESS_OUT))
return oops(compress, EINVAL, "Invalid flag set");
if (!is_compress_ready(compress))
return oops(compress, ENODEV, "device not ready");
fds.events = POLLIN;
while (size) {
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_AVAIL, &avail))
return oops(compress, errno, "cannot get avail");
if ( (avail.avail < frag_size) && (avail.avail < size) ) {
/* Less than one fragment available and not at the
* end of the read, so poll
*/
if (compress->nonblocking)
return total;
ret = compress->ops->poll(compress->data, &fds, 1,
compress->max_poll_wait_ms);
if (fds.revents & POLLERR) {
return oops(compress, EIO, "poll returned error!");
}
/* A pause will cause -EBADFD or zero.
* This is not an error, just stop reading */
if ((ret == 0) || (ret < 0 && errno == EBADFD))
break;
if (ret < 0)
return oops(compress, errno, "poll error");
if (fds.revents & POLLIN) {
continue;
}
}
/* read avail bytes */
if (size > avail.avail)
to_read = avail.avail;
else
to_read = size;
num_read = compress->ops->read(compress->data, cbuf, to_read);
if (num_read < 0) {
/* If play was paused the read returns -EBADFD */
if (errno == EBADFD)
break;
return oops(compress, errno, "read failed!");
}
size -= num_read;
cbuf += num_read;
total += num_read;
}
return total;
}
int compress_start(struct compress *compress)
{
if (!is_compress_ready(compress))
return oops(compress, ENODEV, "device not ready");
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_START))
return oops(compress, errno, "cannot start the stream");
compress->running = 1;
return 0;
}
int compress_stop(struct compress *compress)
{
if (!is_compress_running(compress))
return oops(compress, ENODEV, "device not ready");
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_STOP))
return oops(compress, errno, "cannot stop the stream");
return 0;
}
int compress_pause(struct compress *compress)
{
if (!is_compress_running(compress))
return oops(compress, ENODEV, "device not ready");
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_PAUSE))
return oops(compress, errno, "cannot pause the stream");
return 0;
}
int compress_resume(struct compress *compress)
{
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_RESUME))
return oops(compress, errno, "cannot resume the stream");
return 0;
}
int compress_drain(struct compress *compress)
{
if (!is_compress_running(compress))
return oops(compress, ENODEV, "device not ready");
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_DRAIN))
return oops(compress, errno, "cannot drain the stream");
return 0;
}
int compress_partial_drain(struct compress *compress)
{
if (!is_compress_running(compress))
return oops(compress, ENODEV, "device not ready");
if (!compress->next_track)
return oops(compress, EPERM, "next track not signalled");
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_PARTIAL_DRAIN))
return oops(compress, errno, "cannot drain the stream\n");
compress->next_track = 0;
return 0;
}
int compress_next_track(struct compress *compress)
{
if (!is_compress_running(compress))
return oops(compress, ENODEV, "device not ready");
if (!compress->gapless_metadata)
return oops(compress, EPERM, "metadata not set");
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_NEXT_TRACK))
return oops(compress, errno, "cannot set next track\n");
compress->next_track = 1;
compress->gapless_metadata = 0;
return 0;
}
int compress_set_gapless_metadata(struct compress *compress,
struct compr_gapless_mdata *mdata)
{
struct snd_compr_metadata metadata;
int version;
if (!is_compress_ready(compress))
return oops(compress, ENODEV, "device not ready");
version = get_compress_version(compress);
if (version <= 0)
return -1;
if (version < SNDRV_PROTOCOL_VERSION(0, 1, 1))
return oops(compress, ENXIO, "gapless apis not supported in kernel");
metadata.key = SNDRV_COMPRESS_ENCODER_PADDING;
metadata.value[0] = mdata->encoder_padding;
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_SET_METADATA, &metadata))
return oops(compress, errno, "can't set metadata for stream\n");
metadata.key = SNDRV_COMPRESS_ENCODER_DELAY;
metadata.value[0] = mdata->encoder_delay;
if (compress->ops->ioctl(compress->data, SNDRV_COMPRESS_SET_METADATA, &metadata))
return oops(compress, errno, "can't set metadata for stream\n");
compress->gapless_metadata = 1;
return 0;
}
#ifdef ENABLE_EXTENDED_COMPRESS_FORMAT
int compress_set_next_track_param(struct compress *compress,
union snd_codec_options *codec_options)
{
if (!is_compress_running(compress))
return oops(compress, ENODEV, "device not ready");
if (ioctl(compress->fd, SNDRV_COMPRESS_SET_NEXT_TRACK_PARAM, codec_options))
return oops(compress, errno, "cannot set next track params\n");
return 0;
}
#endif
bool is_codec_supported(unsigned int card, unsigned int device,
unsigned int flags, struct snd_codec *codec)
{
struct compress_ops *ops;
void *snd_node, *data;
bool ret;
int compress_type, fd;
snd_node = snd_utils_get_dev_node(card, device, NODE_COMPRESS);
compress_type = snd_utils_get_node_type(snd_node);
if (compress_type == SND_NODE_TYPE_PLUGIN)
ops = &compr_plug_ops;
else
ops = &compr_hw_ops;
fd = ops->open(card, device, flags, &data, NULL);
if (fd < 0)
return oops(&bad_compress, errno, "cannot open card %u, device %u",
card, device);
ret = _is_codec_type_supported(ops, data, codec);
snd_utils_put_dev_node(snd_node);
ops->close(data);
return ret;
}
void compress_set_max_poll_wait(struct compress *compress, int milliseconds)
{
compress->max_poll_wait_ms = milliseconds;
}
void compress_nonblock(struct compress *compress, int nonblock)
{
compress->nonblocking = !!nonblock;
}
int compress_wait(struct compress *compress, int timeout_ms)
{
struct pollfd fds;
int ret;
fds.events = POLLOUT | POLLIN;
ret = compress->ops->poll(compress->data, &fds, 1, timeout_ms);
if (ret > 0) {
if (fds.revents & POLLERR)
return oops(compress, EIO, "poll returned error!");
if (fds.revents & (POLLOUT | POLLIN))
return 0;
}
if (ret == 0)
return oops(compress, ETIME, "poll timed out");
if (ret < 0)
return oops(compress, errno, "poll error");
return oops(compress, EIO, "poll signalled unhandled event");
}
#ifdef ENABLE_EXTENDED_COMPRESS_FORMAT
int compress_get_metadata(struct compress *compress,
struct snd_compr_metadata *mdata) {
int version;
if (!is_compress_ready(compress))
return oops(compress, ENODEV, "device not ready");
version = get_compress_version(compress);
if (version <= 0)
return -1;
if (ioctl(compress->fd, SNDRV_COMPRESS_GET_METADATA, mdata)) {
return oops(compress, errno, "can't get metadata for stream\n");
}
return 0;
}
int compress_set_metadata(struct compress *compress,
struct snd_compr_metadata *mdata) {
int version;
if (!is_compress_ready(compress))
return oops(compress, ENODEV, "device not ready");
version = get_compress_version(compress);
if (version <= 0)
return -1;
if (ioctl(compress->fd, SNDRV_COMPRESS_SET_METADATA, mdata)) {
return oops(compress, errno, "can't set metadata for stream\n");
}
return 0;
}
#endif