chiark / gitweb /
Hands-off reading for OGGs.
authorRichard Kettlewell <richard@kakajou.wlan.anjou.terraraq.org.uk>
Mon, 7 Jun 2010 20:19:04 +0000 (21:19 +0100)
committerRichard Kettlewell <richard@kakajou.wlan.anjou.terraraq.org.uk>
Mon, 7 Jun 2010 20:19:04 +0000 (21:19 +0100)
lib/hreader.c
lib/hreader.h
lib/wav.c
server/decode.c

index 9bcc808d9203041a68dab85db73ff180ced1dc3c..177aa931d7dfc2dd8fed71bfc39680ee74cb8cb1 100644 (file)
 #include "mem.h"
 #include <string.h>
 #include <fcntl.h>
+#include <sys/stat.h>
+#include <errno.h>
 
 static int hreader_fill(struct hreader *h, off_t offset);
 
-void hreader_init(const char *path, struct hreader *h) {
+int hreader_init(const char *path, struct hreader *h) {
+  struct stat sb;
+  if(stat(path, &sb) < 0)
+    return -1;
   memset(h, 0, sizeof *h);
   h->path = xstrdup(path);
+  h->size = sb.st_size;
   h->bufsize = 65536;
   h->buffer = xmalloc_noptr(h->bufsize);
+  return 0;
 }
 
 int hreader_read(struct hreader *h, void *buffer, size_t n) {
@@ -78,6 +85,18 @@ static int hreader_fill(struct hreader *h, off_t offset) {
   return n;
 }
 
+off_t hreader_seek(struct hreader *h, off_t offset, int whence) {
+  switch(whence) {
+  case SEEK_SET: break;
+  case SEEK_CUR: offset += h->read_offset; break;
+  case SEEK_END: offset += h->size; break;
+  default: einval: errno = EINVAL; return -1;
+  }
+  if(offset < 0) goto einval;
+  h->read_offset = offset;
+  return offset;
+}
+
 /*
 Local Variables:
 c-basic-offset:2
index 8ef3a4079e4229662f674267d6a5168c3e2e5da9..46f8bd918b9e9ba9e17d1e821af8ed2c7509762a 100644 (file)
@@ -29,6 +29,7 @@
  */
 struct hreader {
   const char *path;            /* file to read */
+  off_t size;                   /* file size */
   off_t read_offset;            /* for next hreader_read() */
   off_t buf_offset;             /* offset of start of buffer */
   char *buffer;                        /* input buffer */
@@ -39,8 +40,9 @@ struct hreader {
 /** @brief Initialize a hands-off reader
  * @param path File to read
  * @param h Reader to initialize
+ * @return 0 on success, -1 on error
  */
-void hreader_init(const char *path, struct hreader *h);
+int hreader_init(const char *path, struct hreader *h);
 
 /** @brief Read some bytes
  * @param h Reader to read from
@@ -59,6 +61,14 @@ int hreader_read(struct hreader *h, void *buffer, size_t n);
  */
 int hreader_pread(struct hreader *h, void *buffer, size_t n, off_t offset);
 
+/** @brief Seek within a file
+ * @param h Reader to seek
+ * @param offset Offset
+ * @param whence SEEK_*
+ * @return Result offset
+ */
+off_t hreader_seek(struct hreader *h, off_t offset, int whence);
+
 #endif /* HREADER_H */
 
 /*
index 8b2d640a044bee70fabcea46085b542727872b8a..6558c693a27c735725a8af3c7591cef57ad33030 100644 (file)
--- a/lib/wav.c
+++ b/lib/wav.c
@@ -129,7 +129,7 @@ int wav_init(struct wavfile *f, const char *path) {
   
   memset(f, 0, sizeof *f);
   f->data = -1;
-  hreader_init(path, f->input);
+  if(hreader_init(path, f->input)) goto error_errno;
   /* Read the file header
    *
    *  offset  size  meaning
index 17248768ed9e144de4d4f97d07851df6e7ff63b1..81aa35109425e03c275ad6bed0a90aebc541edaa 100644 (file)
@@ -269,7 +269,8 @@ static enum mad_flow mp3_error(void attribute((unused)) *data,
 static void decode_mp3(void) {
   struct mad_decoder mad[1];
 
-  hreader_init(path, input);
+  if(hreader_init(path, input))
+    disorder_fatal(errno, "opening %s", path);
   mad_decoder_init(mad, 0/*data*/, mp3_input, 0/*header*/, 0/*filter*/,
                   mp3_output, mp3_error, 0/*message*/);
   if(mad_decoder_run(mad, MAD_DECODER_MODE_SYNC))
@@ -277,21 +278,52 @@ static void decode_mp3(void) {
   mad_decoder_finish(mad);
 }
 
+static size_t ogg_read_func(void *ptr, size_t size, size_t nmemb, void *datasource) {
+  struct hreader *h = datasource;
+  
+  int n = hreader_read(h, ptr, size * nmemb);
+  if(n < 0) n = 0;
+  return n / size;
+}
+
+static int ogg_seek_func(void *datasource, ogg_int64_t offset, int whence) {
+  struct hreader *h = datasource;
+  
+  return hreader_seek(h, offset, whence) < 0 ? -1 : 0;
+}
+
+static int ogg_close_func(void attribute((unused)) *datasource) {
+  return 0;
+}
+
+static long ogg_tell_func(void *datasource) {
+  struct hreader *h = datasource;
+  
+  return hreader_seek(h, 0, SEEK_CUR);
+}
+
+static const ov_callbacks ogg_callbacks = {
+  ogg_read_func,
+  ogg_seek_func,
+  ogg_close_func,
+  ogg_tell_func,
+};
+
 /** @brief OGG decoder */
 static void decode_ogg(void) {
-  FILE *fp;
+  struct hreader ogginput[1];
   OggVorbis_File vf[1];
   int err;
   long n;
   int bitstream;
   vorbis_info *vi;
 
-  if(!(fp = fopen(path, "rb")))
-    disorder_fatal(errno, "cannot open %s", path);
+  hreader_init(path, ogginput);
   /* There doesn't seem to be any standard function for mapping the error codes
    * to strings l-( */
-  if((err = ov_open(fp, vf, 0/*initial*/, 0/*ibytes*/)))
-    disorder_fatal(0, "ov_fopen %s: %d", path, err);
+  if((err = ov_open_callbacks(ogginput, vf, 0/*initial*/, 0/*ibytes*/,
+                              ogg_callbacks)))
+    disorder_fatal(0, "ov_open_callbacks %s: %d", path, err);
   if(!(vi = ov_info(vf, 0/*link*/)))
     disorder_fatal(0, "ov_info %s: failed", path);
   while((n = ov_read(vf, input_buffer, sizeof input_buffer, 1/*bigendianp*/,