[xiph-commits] r2993 - in liboggplay/trunk: include/oggplay src/liboggplay

shans at svn.annodex.net shans at svn.annodex.net
Mon Jun 18 21:49:50 PDT 2007


Author: shans
Date: 2007-06-18 21:49:49 -0700 (Mon, 18 Jun 2007)
New Revision: 2993

Modified:
   liboggplay/trunk/include/oggplay/oggplay_reader.h
   liboggplay/trunk/src/liboggplay/oggplay.c
   liboggplay/trunk/src/liboggplay/oggplay_file_reader.c
   liboggplay/trunk/src/liboggplay/oggplay_file_reader.h
   liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c
Log:
Switched oggplay readers over to providing one io_read function rather than
two chunking functions.  Major simplifications to readers as a result.



Modified: liboggplay/trunk/include/oggplay/oggplay_reader.h
===================================================================
--- liboggplay/trunk/include/oggplay/oggplay_reader.h	2007-06-18 15:07:53 UTC (rev 2992)
+++ liboggplay/trunk/include/oggplay/oggplay_reader.h	2007-06-19 04:49:49 UTC (rev 2993)
@@ -40,15 +40,14 @@
 #ifndef __OGGPLAY_READER_H__
 #define __OGGPLAY_READER_H__
 
+#include <stdlib.h>
+
 struct _OggPlayReader;
 
 typedef struct _OggPlayReader {
   OggPlayErrorCode  (*initialise)(struct _OggPlayReader * me, int block);
   OggPlayErrorCode  (*destroy)(struct _OggPlayReader * me);
-  OggPlayErrorCode  (*get_next_chunk)(struct _OggPlayReader *me, 
-                                unsigned char **position, int *size, int *base);
-  OggPlayErrorCode  (*mark_chunk_consumed)(struct _OggPlayReader *me,
-                                           unsigned char *position);
+  size_t            (*io_read)(void *user_handle, void *buf, size_t n);
 } OggPlayReader;
 
 OggPlayReader *

Modified: liboggplay/trunk/src/liboggplay/oggplay.c
===================================================================
--- liboggplay/trunk/src/liboggplay/oggplay.c	2007-06-18 15:07:53 UTC (rev 2992)
+++ liboggplay/trunk/src/liboggplay/oggplay.c	2007-06-19 04:49:49 UTC (rev 2993)
@@ -49,6 +49,8 @@
 
 #include <time.h>
 
+#define OGGZ_READ_CHUNK_SIZE 8192
+
 OggPlay *
 oggplay_new_with_reader(OggPlayReader *reader) {
 
@@ -94,22 +96,15 @@
    * the main loop
    */
   me->oggz = oggz_new(OGGZ_READ | OGGZ_AUTO);
+  oggz_io_set_read(me->oggz, me->reader->io_read, me->reader);
   oggz_set_read_callback(me->oggz, -1, oggplay_callback_predetected, me);
   
   while (1) {
-    unsigned char * chunk_pos  = NULL;
-    int             chunk_size = 0;
-    int             chunk_base = 0;
- 
-    me->reader->get_next_chunk(me->reader, &chunk_pos, &chunk_size, 
-                    &chunk_base);
-
-    if (oggz_read_input(me->oggz, chunk_pos, chunk_size) < 0) {
+   
+    if (oggz_read(me->oggz, OGGZ_READ_CHUNK_SIZE) < 0) {
       return E_OGGPLAY_BAD_INPUT;
     }
-    
-    me->reader->mark_chunk_consumed(me->reader, chunk_pos);
-    
+
     if (me->all_tracks_initialised) {
       break;
     }
@@ -368,9 +363,6 @@
 
   OggPlayCallbackInfo  ** info;
   int                     num_records;
-  unsigned char         * chunk_pos  = NULL;
-  int                     chunk_size = 0;
-  int                     chunk_base = 0;
   int                     r;
   int                     i;
   int                     need_data  = 0;
@@ -395,6 +387,8 @@
      * left over at the end of a once-active track that has had all of its
      * data processed.  Look through the tracks to find these overhangs
      */
+    int r;
+
     if (me->active_tracks == 0) {
       int remaining = 0;
       for (i = 0; i < me->num_tracks; i++) {
@@ -445,8 +439,10 @@
     
     chunk_count += 1;
 
-    if (me->reader->get_next_chunk(me->reader, &chunk_pos, &chunk_size, 
-                   &chunk_base) == E_OGGPLAY_END_OF_FILE) {
+    r = oggz_read(me->oggz, OGGZ_READ_CHUNK_SIZE);
+
+    /* end-of-file */
+    if (r == 0) {
       num_records = oggplay_callback_info_prepare(me, &info);
       if (info != NULL) {
         me->callback (me, num_records, info, me->callback_user_ptr); 
@@ -455,16 +451,6 @@
       return E_OGGPLAY_OK;
     }
 
-    /*
-     * feed it into oggz
-     */
-    if (oggz_read_input(me->oggz, chunk_pos, chunk_size) < 0) {
-      return E_OGGPLAY_OGGZ_UNHAPPY;
-    }
-    /*
-     * mark the chunk as used
-     */
-    me->reader->mark_chunk_consumed(me->reader, chunk_pos);
   }
   /*
    * prepare a callback

Modified: liboggplay/trunk/src/liboggplay/oggplay_file_reader.c
===================================================================
--- liboggplay/trunk/src/liboggplay/oggplay_file_reader.c	2007-06-18 15:07:53 UTC (rev 2992)
+++ liboggplay/trunk/src/liboggplay/oggplay_file_reader.c	2007-06-19 04:49:49 UTC (rev 2993)
@@ -58,11 +58,9 @@
   if (me->file == NULL) {
     return E_OGGPLAY_BAD_INPUT;
   }
-
-  me->num_buffers = FILE_READER_INITIAL_NUM_BUFFERS;
-  me->buffers = malloc (sizeof (char *) * FILE_READER_INITIAL_NUM_BUFFERS);
-  memset(me->buffers, 0, sizeof (char *) * FILE_READER_INITIAL_NUM_BUFFERS);
   
+  me->current_position = 0;
+
   return E_OGGPLAY_OK;
 }
 
@@ -70,94 +68,28 @@
 oggplay_file_reader_destroy(OggPlayReader * opr) {
 
   OggPlayFileReader * me;
-  int                 i;
 
   me = (OggPlayFileReader *)opr;
-  fclose(me->file);
-  
-  for (i = 0; i < me->num_buffers; i++) {
-    if (me->buffers[i] != NULL) {
-      free(me->buffers[i]);
-    }
-  }
-  
-  free(me->buffers);
 
+  fclose(me->file);
   free(me);
   
   return E_OGGPLAY_OK;
 }
-  
 
-OggPlayErrorCode
-oggplay_file_reader_get_next_chunk(OggPlayReader * opr, 
-                unsigned char ** position, int * size, int * base) {
-
-  OggPlayFileReader * me;
-  int                 i;
-
-  me = (OggPlayFileReader *)opr;
-  *position = malloc (FILE_READER_CHUNK_SIZE);
+static size_t
+oggplay_file_reader_io_read(void * user_handle, void * buf, size_t n) {
   
-  /*
-   * find an empty buffer slot
-   */
-  for (i = 0; i < me->num_buffers; i++) {
-    if (me->buffers[i] == NULL) {
-      me->buffers[i] = *position;
-      break;
-    }
+  OggPlayFileReader *me = (OggPlayFileReader *)user_handle;
+  int r;
+  r = fread(buf, 1, n, me->file);
+  if (r > 0) {
+    me->current_position += r;
   }
- 
-  /*
-   * no empty buffer slots, grow size of buffer list
-   */
-  if (i == me->num_buffers) {
-    me->buffers = realloc (me->buffers, me->num_buffers * 2 * sizeof (char *));
-    memset (me->buffers + me->num_buffers, 0, 
-                      me->num_buffers * sizeof (char *));
-    me->buffers[me->num_buffers] = *position;
-    me->num_buffers *= 2;
-  }
 
-  /*
-   * read data
-   */
-  *size = fread(*position, 1, FILE_READER_CHUNK_SIZE, me->file);
-
-  if (*size == 0 && feof(me->file)) {
-    return E_OGGPLAY_END_OF_FILE;
-  }
-    
-  me->current_position += *size;
-  *base = me->current_position;
-
-  return E_OGGPLAY_OK;
+  return r;
 }
 
-OggPlayErrorCode 
-oggplay_file_reader_mark_chunk_consumed(OggPlayReader *opr, 
-                                                  unsigned char *position) {
-
-  OggPlayFileReader * me;
-  int                 i;
- 
-  me = (OggPlayFileReader *)opr;
-  
-  /*
-   * find chunk
-   */
-  for (i = 0; i < me->num_buffers; i++) {
-    if (me->buffers[i] == position) {
-      free(me->buffers[i]);
-      me->buffers[i] = NULL;
-      return E_OGGPLAY_OK;
-    }
-  }
-
-  return E_OGGPLAY_NO_SUCH_CHUNK;
-}
-
 OggPlayReader *
 oggplay_file_reader_new(char *file_name) {
 
@@ -169,10 +101,8 @@
 
   me->functions.initialise = &oggplay_file_reader_initialise;
   me->functions.destroy = &oggplay_file_reader_destroy;
+  me->functions.io_read = &oggplay_file_reader_io_read; 
   
-  me->functions.get_next_chunk = &oggplay_file_reader_get_next_chunk;
-  me->functions.mark_chunk_consumed = &oggplay_file_reader_mark_chunk_consumed;
-  
   return (OggPlayReader *)me;
 
 }

Modified: liboggplay/trunk/src/liboggplay/oggplay_file_reader.h
===================================================================
--- liboggplay/trunk/src/liboggplay/oggplay_file_reader.h	2007-06-18 15:07:53 UTC (rev 2992)
+++ liboggplay/trunk/src/liboggplay/oggplay_file_reader.h	2007-06-19 04:49:49 UTC (rev 2993)
@@ -49,8 +49,6 @@
   OggPlayReader     functions;
   char            * file_name;
   FILE            * file;
-  int               num_buffers;
-  unsigned char  ** buffers;
   int               current_position;
 } OggPlayFileReader;
 

Modified: liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c
===================================================================
--- liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c	2007-06-18 15:07:53 UTC (rev 2992)
+++ liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c	2007-06-19 04:49:49 UTC (rev 2993)
@@ -548,59 +548,27 @@
   return E_OGGPLAY_OK;
 }
 
+#define MIN(a,b) ((a)<(b)?(a):(b))
 
-OggPlayErrorCode
-oggplay_tcp_reader_get_next_chunk(OggPlayReader * opr, 
-                unsigned char ** position, int * size, int * base) {
+static size_t
+oggplay_tcp_reader_io_read(void * user_handle, void * buf, size_t n) {
 
-  OggPlayTCPReader * me               = (OggPlayTCPReader *)opr;
+  OggPlayTCPReader  * me = (OggPlayTCPReader *)user_handle;
+  int                 len;
 
-  /*
-   * check for end of file condition
-   */
-  if (me->buffer_size == 0 && me->socket == INVALID_SOCKET) {
-    return E_OGGPLAY_END_OF_FILE;
-  }
- 
   grab_some_data(me, 0);
-
-  *position = me->buffer + me->current_position;
-  *size = me->amount_in_memory - me->current_position;
-
-  /*
-   * last chunk
-   */
-  if (me->socket == INVALID_SOCKET) {
-    return E_OGGPLAY_OK;
-  }
-  
   if (me->amount_in_memory - me->current_position == 0) {
     grab_some_data(me, 1);
-    *position = me->buffer + me->current_position;
-    *size = me->amount_in_memory - me->current_position;
   }
-  
-  *base = me->stored_offset + me->current_position;
-  me->current_position += *size;
 
-  return E_OGGPLAY_OK;
-}
+  len = MIN(n, me->buffer_size - me->current_position);
+  memcpy(buf, me->buffer, len);
+    
+  me->current_position += len;
 
-
-OggPlayErrorCode 
-oggplay_tcp_reader_mark_chunk_consumed(OggPlayReader *opr, 
-                                                  unsigned char *position) {
-
-  OggPlayTCPReader * me;
-  me = (OggPlayTCPReader *)opr;
-
-  if (me->socket == INVALID_SOCKET) 
-    return E_OGGPLAY_OK;
-  grab_some_data(me, 0);
-  return E_OGGPLAY_OK;
+  return len;
 }
 
-
 OggPlayReader *
 oggplay_tcp_reader_new(char *location) {
 
@@ -615,29 +583,13 @@
 
   me->functions.initialise = &oggplay_tcp_reader_initialise;
   me->functions.destroy = &oggplay_tcp_reader_destroy;
-  me->functions.get_next_chunk = &oggplay_tcp_reader_get_next_chunk;
-  me->functions.mark_chunk_consumed = &oggplay_tcp_reader_mark_chunk_consumed;
+  me->functions.io_read = &oggplay_tcp_reader_io_read;
   
   return (OggPlayReader *)me;
 }
 
-/*
-#define MIN(a,b) ((a)<(b)?(a):(b))
 
-static size_t
-my_io_read(void * user_handle, void * buf, size_t n) {
-
-  OggPlayTCPReader  * me = (OggPlayTCPReader *)user_handle;
-  int                 len;
-
-  len = MIN(n, me->buffer_size - me->current_position);
-  memcpy(buf, me->buffer, len);
-    
-  me->current_position += len;
-
-  return len;
-}
-    
+/*
 static int
 my_io_seek(void * user_handle, long offset, int whence) {
 
@@ -668,4 +620,4 @@
   return me->current_position;
 
 }
-*/ 
+*/



More information about the commits mailing list