]> git.alsa-project.org Git - tinycompress.git/commitdiff
compress: Add support for capture streams
authorRichard Fitzgerald <rf@opensource.wolfsonmicro.com>
Fri, 26 Apr 2013 15:14:22 +0000 (16:14 +0100)
committerVinod Koul <vinod.koul@intel.com>
Fri, 26 Apr 2013 16:47:36 +0000 (22:17 +0530)
Signed-off-by: Charles Keepax <ckeepax@opensource.wolfsonmicro.com>
Signed-off-by: Richard Fitzgerald <rf@opensource.wolfsonmicro.com>
Signed-off-by: Vinod Koul <vinod.koul@intel.com>
compress.c
include/tinycompress/tinycompress.h

index 86cca88bd746d295f810717ef5ef9d960973c4f0..e80e5982c9b37f1470ed422164a74d4ca25eb817 100644 (file)
@@ -240,13 +240,12 @@ struct compress *compress_open(unsigned int card, unsigned int device,
                oops(&bad_compress, -EINVAL, "can't deduce device direction from given flags");
                goto config_fail;
        }
+
        if (flags & COMPRESS_OUT) {
-               /* this should be removed once we have capture tested */
-               oops(&bad_compress, -EINVAL, "this version doesnt support capture");
-               goto config_fail;
+               compress->fd = open(fn, O_RDONLY);
+       } else {
+               compress->fd = open(fn, O_WRONLY);
        }
-
-       compress->fd = open(fn, O_WRONLY);
        if (compress->fd < 0) {
                oops(&bad_compress, errno, "cannot open device '%s'", fn);
                goto config_fail;
@@ -403,7 +402,60 @@ int compress_write(struct compress *compress, const void *buf, unsigned int size
 
 int compress_read(struct compress *compress, void *buf, unsigned int size)
 {
-       return oops(compress, -ENOTTY, "Not supported yet in lib");
+       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.fd = compress->fd;
+       fds.events = POLLIN;
+
+       while (size) {
+               if (ioctl(compress->fd, 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
+                        */
+                       ret = poll(&fds, 1, compress->max_poll_wait_ms);
+                       /* A pause will cause -EBADFD or zero.
+                        * This is not an error, just stop reading */
+                       if ((ret == 0) || (ret == -EBADFD))
+                               break;
+                       if (ret < 0)
+                               return oops(compress, errno, "poll error");
+                       if (fds.revents & POLLIN) {
+                               continue;
+                       }
+                       if (fds.revents & POLLERR) {
+                               return oops(compress, -EIO, "poll returned error!");
+                       }
+               }
+               /* read avail bytes */
+               if (size > avail.avail)
+                       to_read = avail.avail;
+               else
+                       to_read = size;
+               num_read = read(compress->fd, cbuf, to_read);
+               /* If play was paused the read returns -EBADFD */
+               if (num_read == -EBADFD)
+                       break;
+               if (num_read < 0)
+                       return oops(compress, errno, "read failed!");
+
+               size -= num_read;
+               cbuf += num_read;
+               total += num_read;
+       }
+
+       return total;
 }
 
 int compress_start(struct compress *compress)
index ba9f3b64ab719c37a21d78307f3fbeafc907bad4..9867afca2976f27d150ecbbe77254389faffce7d 100644 (file)
@@ -76,7 +76,7 @@ struct compr_gapless_mdata {
        __u32 encoder_padding;
 };
 
-#define COMPRESS_OUT        0x00000000
+#define COMPRESS_OUT        0x20000000
 #define COMPRESS_IN         0x10000000
 
 struct compress;