Lines Matching refs:compress
4 * tinycompress library for compress audio offload in alsa
36 * tinycompress library for compress audio offload in alsa
84 struct compress {
96 static int oops(struct compress *compress, int e, const char *fmt, ...)
102 vsnprintf(compress->error, COMPR_ERR_MAX, fmt, ap);
104 sz = strlen(compress->error);
106 snprintf(compress->error + sz, COMPR_ERR_MAX - sz,
113 const char *compress_get_error(struct compress *compress)
115 return compress->error;
117 static struct compress bad_compress = {
121 int is_compress_running(struct compress *compress)
123 return ((compress->fd > 0) && compress->running) ? 1 : 0;
126 int is_compress_ready(struct compress *compress)
128 return (compress->fd > 0) ? 1 : 0;
131 static int get_compress_version(struct compress *compress)
135 if (ioctl(compress->fd, SNDRV_COMPRESS_IOCTL_VERSION, &version)) {
136 oops(compress, errno, "cant read version");
142 static bool _is_codec_supported(struct compress *compress, struct compr_config *config,
156 oops(compress, ENXIO, "this codec is not supported");
161 oops(compress, EINVAL, "requested fragment size %d is below min supported %d",
166 oops(compress, EINVAL, "requested fragment size %d is above max supported %d",
171 oops(compress, EINVAL, "requested fragments %d are below min supported %d",
176 oops(compress, EINVAL, "requested fragments %d are above max supported %d",
215 struct compress *compress_open(unsigned int card, unsigned int device,
218 struct compress *compress;
228 compress = calloc(1, sizeof(struct compress));
229 if (!compress) {
230 oops(&bad_compress, errno, "cannot allocate compress object");
234 compress->next_track = 0;
235 compress->gapless_metadata = 0;
236 compress->config = calloc(1, sizeof(*config));
237 if (!compress->config)
242 compress->max_poll_wait_ms = DEFAULT_MAX_POLL_WAIT_MS;
244 compress->flags = flags;
251 compress->fd = open(fn, O_RDONLY);
253 compress->fd = open(fn, O_WRONLY);
255 if (compress->fd < 0) {
260 if (ioctl(compress->fd, SNDRV_COMPRESS_GET_CAPS, &caps)) {
261 oops(compress, errno, "cannot get device caps");
275 if (_is_codec_supported(compress, config, &caps) == false) {
276 oops(compress, errno, "codec not supported\n");
281 memcpy(compress->config, config, sizeof(*compress->config));
284 if (ioctl(compress->fd, SNDRV_COMPRESS_SET_PARAMS, ¶ms)) {
289 return compress;
292 close(compress->fd);
293 compress->fd = -1;
295 free(compress->config);
297 free(compress);
301 void compress_close(struct compress *compress)
303 if (compress == &bad_compress)
306 if (compress->fd >= 0)
307 close(compress->fd);
308 compress->running = 0;
309 compress->fd = -1;
310 free(compress->config);
311 free(compress);
314 int compress_get_hpointer(struct compress *compress,
320 if (!is_compress_ready(compress))
321 return oops(compress, ENODEV, "device not ready");
323 if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &kavail))
324 return oops(compress, errno, "cannot get avail");
326 return oops(compress, ENODATA, "sample rate unknown");
335 int compress_get_tstamp(struct compress *compress,
340 if (!is_compress_ready(compress))
341 return oops(compress, ENODEV, "device not ready");
343 if (ioctl(compress->fd, SNDRV_COMPRESS_TSTAMP, &ktstamp))
344 return oops(compress, errno, "cannot get tstamp");
351 int compress_write(struct compress *compress, const void *buf, unsigned int size)
358 const unsigned int frag_size = compress->config->fragment_size;
360 if (!(compress->flags & COMPRESS_IN))
361 return oops(compress, EINVAL, "Invalid flag set");
362 if (!is_compress_ready(compress))
363 return oops(compress, ENODEV, "device not ready");
364 fds.fd = compress->fd;
369 if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail))
370 return oops(compress, errno, "cannot get avail");
377 if (compress->nonblocking)
380 ret = poll(&fds, 1, compress->max_poll_wait_ms);
382 return oops(compress, EIO, "poll returned error!");
389 return oops(compress, errno, "poll error");
399 written = write(compress->fd, cbuf, to_write);
404 return oops(compress, errno, "write failed!");
413 int compress_read(struct compress *compress, void *buf, unsigned int size)
420 const unsigned int frag_size = compress->config->fragment_size;
422 if (!(compress->flags & COMPRESS_OUT))
423 return oops(compress, EINVAL, "Invalid flag set");
424 if (!is_compress_ready(compress))
425 return oops(compress, ENODEV, "device not ready");
426 fds.fd = compress->fd;
430 if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail))
431 return oops(compress, errno, "cannot get avail");
437 if (compress->nonblocking)
440 ret = poll(&fds, 1, compress->max_poll_wait_ms);
442 return oops(compress, EIO, "poll returned error!");
449 return oops(compress, errno, "poll error");
459 num_read = read(compress->fd, cbuf, to_read);
464 return oops(compress, errno, "read failed!");
474 int compress_start(struct compress *compress)
476 if (!is_compress_ready(compress))
477 return oops(compress, ENODEV, "device not ready");
478 if (ioctl(compress->fd, SNDRV_COMPRESS_START))
479 return oops(compress, errno, "cannot start the stream");
480 compress->running = 1;
485 int compress_stop(struct compress *compress)
487 if (!is_compress_running(compress))
488 return oops(compress, ENODEV, "device not ready");
489 if (ioctl(compress->fd, SNDRV_COMPRESS_STOP))
490 return oops(compress, errno, "cannot stop the stream");
494 int compress_pause(struct compress *compress)
496 if (!is_compress_running(compress))
497 return oops(compress, ENODEV, "device not ready");
498 if (ioctl(compress->fd, SNDRV_COMPRESS_PAUSE))
499 return oops(compress, errno, "cannot pause the stream");
503 int compress_resume(struct compress *compress)
505 if (ioctl(compress->fd, SNDRV_COMPRESS_RESUME))
506 return oops(compress, errno, "cannot resume the stream");
510 int compress_drain(struct compress *compress)
512 if (!is_compress_running(compress))
513 return oops(compress, ENODEV, "device not ready");
514 if (ioctl(compress->fd, SNDRV_COMPRESS_DRAIN))
515 return oops(compress, errno, "cannot drain the stream");
519 int compress_partial_drain(struct compress *compress)
521 if (!is_compress_running(compress))
522 return oops(compress, ENODEV, "device not ready");
524 if (!compress->next_track)
525 return oops(compress, EPERM, "next track not signalled");
526 if (ioctl(compress->fd, SNDRV_COMPRESS_PARTIAL_DRAIN))
527 return oops(compress, errno, "cannot drain the stream\n");
528 compress->next_track = 0;
532 int compress_next_track(struct compress *compress)
534 if (!is_compress_running(compress))
535 return oops(compress, ENODEV, "device not ready");
537 if (!compress->gapless_metadata)
538 return oops(compress, EPERM, "metadata not set");
539 if (ioctl(compress->fd, SNDRV_COMPRESS_NEXT_TRACK))
540 return oops(compress, errno, "cannot set next track\n");
541 compress->next_track = 1;
542 compress->gapless_metadata = 0;
546 int compress_set_gapless_metadata(struct compress *compress,
552 if (!is_compress_ready(compress))
553 return oops(compress, ENODEV, "device not ready");
555 version = get_compress_version(compress);
560 return oops(compress, ENXIO, "gapless apis not supported in kernel");
564 if (ioctl(compress->fd, SNDRV_COMPRESS_SET_METADATA, &metadata))
565 return oops(compress, errno, "can't set metadata for stream\n");
569 if (ioctl(compress->fd, SNDRV_COMPRESS_SET_METADATA, &metadata))
570 return oops(compress, errno, "can't set metadata for stream\n");
571 compress->gapless_metadata = 1;
600 void compress_set_max_poll_wait(struct compress *compress, int milliseconds)
602 compress->max_poll_wait_ms = milliseconds;
605 void compress_nonblock(struct compress *compress, int nonblock)
607 compress->nonblocking = !!nonblock;
610 int compress_wait(struct compress *compress, int timeout_ms)
615 fds.fd = compress->fd;
621 return oops(compress, EIO, "poll returned error!");
626 return oops(compress, ETIME, "poll timed out");
628 return oops(compress, errno, "poll error");
630 return oops(compress, EIO, "poll signalled unhandled event");