[xiph-cvs] cvs commit: vorbis-tools/ogg123 buffer.c buffer.h curl_interface.c curl_interface.h ogg123.c

Kenneth C. Arnold kcarnold at xiph.org
Sat Aug 11 09:04:23 PDT 2001



kcarnold    01/08/11 09:04:23

  Modified:    ogg123   Tag: kcarnold_work buffer.c buffer.h
                        curl_interface.c curl_interface.h ogg123.c
  Log:
  Pointers are evil. void* pointers are worse. Typecasting void* pointers
  is just plain ugly. Fixed a real stupid segfault.
  
  Input buffer gets the 8500 byte chunk that vorbisfile wants nearly perfectly now.

Revision  Changes    Path
No                   revision

No                   revision

1.7.2.10  +19 -15    vorbis-tools/ogg123/buffer.c

Index: buffer.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/ogg123/buffer.c,v
retrieving revision 1.7.2.9
retrieving revision 1.7.2.10
diff -u -r1.7.2.9 -r1.7.2.10
--- buffer.c	2001/08/11 02:55:37	1.7.2.9
+++ buffer.c	2001/08/11 16:04:21	1.7.2.10
@@ -11,7 +11,7 @@
  *                                                                  *
  ********************************************************************
 
- last mod: $Id: buffer.c,v 1.7.2.9 2001/08/11 02:55:37 kcarnold Exp $
+ last mod: $Id: buffer.c,v 1.7.2.10 2001/08/11 16:04:21 kcarnold Exp $
 
  ********************************************************************/
 
@@ -136,14 +136,17 @@
            * |-------------------------------|
            * |-^       ^---------------------|
            *  reader   writer, our range
+	   * EOS applicable only if reader is at beginning of buffer
            */
           DEBUG1("up to buf->end, buf->end - buf->writer + 1 = %d", buf->end - buf->writer + 1);
-	  if (buf->end - buf->writer + 1 > TARGET_WRITE_SIZE) {
-	    WriteThisTime = TARGET_WRITE_SIZE;
+	  if (buf->end - buf->writer + 1 > buf->OptimalWriteSize) {
+	    WriteThisTime = buf->OptimalWriteSize;
             NewWriterPtr = buf->writer + WriteThisTime;
           } else {
             NewWriterPtr = buf->buffer;
             WriteThisTime = buf->end - buf->writer + 1;
+	    if (buf->reader == buf->buffer)
+	      iseos = buf->eos;
           }
         }
       else
@@ -153,17 +156,18 @@
            *    ^--------------^
            *   writer         reader
            * but we can't use buf->reader itself, becuase that's not in the data.
+	   * EOS applicable if we're reading right up to reader.
            */
           DEBUG1("up to buf->reader, buf->reader - buf->writer = %d", buf->reader - buf->writer);
-	  if (buf->reader - buf->writer > TARGET_WRITE_SIZE)
-	    WriteThisTime = TARGET_WRITE_SIZE;
+	  if (buf->reader - buf->writer > buf->OptimalWriteSize)
+	    WriteThisTime = buf->OptimalWriteSize;
           else {
             WriteThisTime = buf->reader - buf->writer;
             iseos = buf->eos;
           }
           NewWriterPtr = buf->writer + WriteThisTime;
         }
-
+      
       DEBUG0("writing chunk to output");
       /* unlock while playing sample */
       UNLOCK_MUTEX (buf->SizeMutex);
@@ -178,7 +182,7 @@
 
       /* slight abuse of the DataReady condition, but makes sense. */
       DEBUG0 ("signalling buffer no longer full");
-      if (buf->curfill + WriteThisTime + TARGET_WRITE_SIZE >= buf->size)
+      if (buf->curfill + WriteThisTime + buf->OptimalWriteSize >= buf->size)
         pthread_cond_signal (&buf->DataReadyCondition);
    }
   /* should never get here */
@@ -188,7 +192,7 @@
 
 buf_t *StartBuffer (long size, long prebuffer, void *data, 
                     size_t (*write_func) (void *, size_t, size_t, void *, char),
-		    void *initData, int (*init_func) (void*))
+		    void *initData, int (*init_func) (void*), int OptimalWriteSize)
 {
   buf_t *buf = malloc (sizeof(buf_t) + sizeof (chunk) * (size - 1));
 
@@ -217,6 +221,7 @@
 
   buf->reader = buf->writer = buf->buffer;
   buf->end = buf->buffer + (size - 1);
+  buf->OptimalWriteSize = OptimalWriteSize;
   buf->size = size;
   buf->prebuffer = prebuffer;
   Prebuffer (buf);
@@ -287,13 +292,11 @@
 void SubmitData (buf_t *buf, chunk *data, size_t size, size_t nmemb)
 {
   int i, s;
-  while (nmemb > 0) {
-    for (i = 0; i < size; i += TARGET_WRITE_SIZE) {
-      s = i + TARGET_WRITE_SIZE <= size ? TARGET_WRITE_SIZE : size - i;
-      _SubmitDataChunk (buf, data, s);
-      data += s;
-    }
-    nmemb--;
+  size *= nmemb;
+  for (i = 0; i < size; i += buf->OptimalWriteSize) {
+    s = i + buf->OptimalWriteSize <= size ? buf->OptimalWriteSize : size - i;
+    _SubmitDataChunk (buf, data, s);
+    data += s;
   }
 }
 
@@ -363,6 +366,7 @@
   if (buf) {
     buffer_shutdown (buf);
     PthreadCleanup (buf);
+    memset (buf, 0, sizeof(buf));
     free (buf);
   }
 }

1.2.2.10  +3 -4      vorbis-tools/ogg123/buffer.h

Index: buffer.h
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/ogg123/buffer.h,v
retrieving revision 1.2.2.9
retrieving revision 1.2.2.10
diff -u -r1.2.2.9 -r1.2.2.10
--- buffer.h	2001/08/11 02:55:37	1.2.2.9
+++ buffer.h	2001/08/11 16:04:22	1.2.2.10
@@ -11,7 +11,7 @@
  *                                                                  *
  ********************************************************************
  
- last mod: $Id: buffer.h,v 1.2.2.9 2001/08/11 02:55:37 kcarnold Exp $
+ last mod: $Id: buffer.h,v 1.2.2.10 2001/08/11 16:04:22 kcarnold Exp $
  
 ********************************************************************/
 
@@ -43,6 +43,7 @@
   
   /* the buffer itself */
   char StatMask;
+  int OptimalWriteSize; /* optimal size to write out in chunks of, if possible. */
   long size;         /* buffer size, for reference */
   long curfill;      /* how much the buffer is currently filled */
   long prebuffer;    /* number of chunks to prebuffer */
@@ -57,11 +58,9 @@
 #define STAT_PLAYING 2
 #define STAT_EMPTYING 4
 
-#define TARGET_WRITE_SIZE 4096 /* to agree with other mechanisms used in ogg123 */
-
 buf_t *StartBuffer (long size, long prebuffer, void *data, 
                     size_t (*write_func) (void *, size_t, size_t, void *, char),
-		    void *initData, int (*init_func) (void*));
+		    void *initData, int (*init_func) (void*), int OptimalWriteSize);
 void SubmitData (buf_t *buf, chunk *data, size_t size, size_t nmemb);
 void buffer_MarkEOS (buf_t *buf);
 void buffer_shutdown (buf_t *buf);

1.1.2.3   +90 -47    vorbis-tools/ogg123/Attic/curl_interface.c

Index: curl_interface.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/ogg123/Attic/curl_interface.c,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- curl_interface.c	2001/08/11 02:55:37	1.1.2.2
+++ curl_interface.c	2001/08/11 16:04:22	1.1.2.3
@@ -11,7 +11,7 @@
  *                                                                  *
  ********************************************************************
  
- last mod: $Id: curl_interface.c,v 1.1.2.2 2001/08/11 02:55:37 kcarnold Exp $
+ last mod: $Id: curl_interface.c,v 1.1.2.3 2001/08/11 16:04:22 kcarnold Exp $
  
 ********************************************************************/
 
@@ -19,31 +19,44 @@
 
 #include <stdlib.h>
 #include <stdio.h>
-#include <signal.h> /* for SIGTERM */
+#include <signal.h>		/* for SIGTERM */
 
+#define DEBUG_CURLINTERFACE
+
+#ifdef DEBUG_CURLINTERFACE
 #define debug(x, y...) do { fprintf (stderr, x , ## y); } while (0)
+#else
+#define debug(x, y...) do { } while (0)
+#endif
 
-size_t CurlWriteFunction (void *ptr, size_t size, size_t nmemb, void *arg)
+size_t
+CurlWriteFunction (void *ptr, size_t size, size_t nmemb, void *arg)
 {
   buf_t *buf = arg;
-  debug("CurlWriteFunction, submitting %d bytes.\n", size*nmemb);
+  debug ("CurlWriteFunction, submitting %d bytes.\n", size * nmemb);
   SubmitData (buf, ptr, size, nmemb);
-  return size*nmemb;
+  return size * nmemb;
 }
 
-size_t BufferWriteChunk (void *ptr, size_t size, void *arg, char iseos)
+size_t
+BufferWriteChunk (void *ptr, size_t size, void *arg, char iseos)
 {
   StreamInputBufferData_t *data = arg;
 
-  debug("buffer writing chunk of %d, %d bytes to go\n", size, data->BytesRequested);
+  debug ("buffer writing chunk of %d, %d bytes to go\n", size,
+	 data->BytesRequested);
 
   pthread_mutex_lock (&data->ReadDataMutex);
   while (data->BytesRequested == 0)
     pthread_cond_wait (&data->ReadRequestedCondition, &data->ReadDataMutex);
 
+  data->EOS = iseos;
+  if (iseos)
+    debug ("End of stream.\n");
+
   if (size <= data->BytesRequested)
     {
-      fprintf (stderr, "simply moving %d bytes in.\n", size);
+      debug ("simply moving %d bytes in.\n", size);
       memmove (data->CurWritePtr, ptr, size);
       data->CurWritePtr += size;
       data->BytesRequested -= size;
@@ -54,20 +67,17 @@
   else
     {
       /* There will be some excess data here. Write it, then block on needing more data. */
-      fprintf (stderr, "writing %d bytes, ", data->BytesRequested);
+      debug ("writing %d bytes, ", data->BytesRequested);
       memmove (data->CurWritePtr, ptr, data->BytesRequested);
       data->CurWritePtr += data->BytesRequested;
       size -= data->BytesRequested;
       ptr += data->BytesRequested;
       data->BytesRequested = 0;
-      fprintf (stderr, "saving %d bytes of excess data\n", size);
+      debug ("saving %d bytes of excess data\n", size);
       memmove (data->ExcessData, ptr, size);
       data->ExcessDataSize = size;
-      
+
       debug ("signalling successful read\n");
-      data->EOS = iseos;
-      if (iseos)
-	debug ("End of stream.\n");
       pthread_mutex_unlock (&data->ReadDataMutex);
       pthread_cond_signal (&data->ReadDoneCondition);
     }
@@ -76,7 +86,9 @@
   return size;
 }
 
-size_t BufferWriteFunction (void *ptr, size_t size, size_t nmemb, void *arg, char iseos)
+size_t
+BufferWriteFunction (void *ptr, size_t size, size_t nmemb, void *arg,
+		     char iseos)
 {
   size_t written = 0;
   while (nmemb > 0)
@@ -90,13 +102,16 @@
   return written;
 }
 
-int CurlProgressFunction (void *arg, size_t dltotal, size_t dlnow, size_t ultotal, size_t ulnow)
+int
+CurlProgressFunction (void *arg, size_t dltotal, size_t dlnow, size_t ultotal,
+		      size_t ulnow)
 {
   debug ("curlprogressfunction\n");
   return 0;
 }
 
-void CurlSetopts (CURL* handle, buf_t *buf, InputOpts_t inputOpts)
+void
+CurlSetopts (CURL * handle, buf_t * buf, InputOpts_t inputOpts)
 {
   curl_easy_setopt (handle, CURLOPT_FILE, buf);
   curl_easy_setopt (handle, CURLOPT_WRITEFUNCTION, CurlWriteFunction);
@@ -111,7 +126,8 @@
   if (inputOpts.Netrc)
     curl_easy_setopt (handle, CURLOPT_NETRC, inputOpts.Netrc);
   if (inputOpts.FollowLocation)
-    curl_easy_setopt (handle, CURLOPT_FOLLOWLOCATION, inputOpts.FollowLocation);
+    curl_easy_setopt (handle, CURLOPT_FOLLOWLOCATION,
+		      inputOpts.FollowLocation);
   if (inputOpts.Referer)
     curl_easy_setopt (handle, CURLOPT_REFERER, inputOpts.Referer);
   if (inputOpts.UserAgent)
@@ -124,19 +140,23 @@
   curl_easy_setopt (handle, CURLOPT_PROGRESSDATA, buf);
 }
 
-void* CurlGo (void *arg)
+void *
+CurlGo (void *arg)
 {
   buf_t *buf = arg;
   StreamInputBufferData_t *data = buf->data;
   CURLcode ret;
-  fprintf (stderr, "CurlGo\n");
-  ret = curl_easy_perform ((CURL*) data->CurlHandle);
+  debug ("CurlGo\n");
+  ret = curl_easy_perform ((CURL *) data->CurlHandle);
   debug ("curl done.\n");
   buffer_MarkEOS (buf);
-  return (void*) ret;
+  curl_easy_cleanup (data->CurlHandle);
+  data->CurlHandle = 0;
+  return (void *) ret;
 }
 
-buf_t *InitStream (InputOpts_t inputOpts)
+buf_t *
+InitStream (InputOpts_t inputOpts)
 {
   StreamInputBufferData_t *data = malloc (sizeof (StreamInputBufferData_t));
   buf_t *buf;
@@ -147,7 +167,7 @@
   if (!data)
     {
       perror ("malloc");
-      exit(1);
+      exit (1);
     }
 
   debug (" init pthreads\n");
@@ -160,16 +180,18 @@
   if (!data->CurlHandle)
     {
       perror ("curl_easy_init");
-      exit(1);
+      exit (1);
     }
 
   debug (" start buffer\n");
-  buf = StartBuffer (inputOpts.BufferSize, inputOpts.Prebuffer, data, BufferWriteFunction, NULL, NULL);
+  buf =
+    StartBuffer (inputOpts.BufferSize, inputOpts.Prebuffer, data,
+		 BufferWriteFunction, NULL, NULL, VORBIS_CHUNKIN_SIZE);
 
   if (!buf)
     {
       perror ("StartBuffer");
-      exit(1);
+      exit (1);
     }
 
   debug (" set curl opts\n");
@@ -181,12 +203,13 @@
   return buf;
 }
 
-size_t StreamBufferRead (void *ptr, size_t size, size_t nmemb, void *arg)
+size_t
+StreamBufferRead (void *ptr, size_t size, size_t nmemb, void *arg)
 {
   StreamInputBufferData_t *data = arg;
   size_t ret;
 
-  ret = size *= nmemb; /* makes things simpler and run smoother */
+  ret = size *= nmemb;		/* makes things simpler and run smoother */
 
   debug ("StreamBufferRead %d bytes\n", ret);
 
@@ -198,7 +221,8 @@
       memmove (ptr, data->ExcessData, size);
       data->ExcessDataSize -= size;
       if (size < data->ExcessDataSize)
-	memmove (data->ExcessData, data->ExcessData + size, data->ExcessDataSize);
+	memmove (data->ExcessData, data->ExcessData + size,
+		 data->ExcessDataSize);
       pthread_mutex_unlock (&data->ReadDataMutex);
     }
   else
@@ -217,18 +241,22 @@
         }
       else
         data->ExcessDataSize = 0;
-      
+
       data->BytesRequested = size;
       data->WriteTarget = data->CurWritePtr = ptr;
-      
+
       pthread_mutex_unlock (&data->ReadDataMutex);
       pthread_cond_signal (&data->ReadRequestedCondition);
       pthread_mutex_lock (&data->ReadDataMutex);
-      
-      while (data->BytesRequested > 0) {
-	debug ("Waiting for %d bytes of data to be read.\n", data->BytesRequested);
-	pthread_cond_wait (&data->ReadDoneCondition, &data->ReadDataMutex);
-      }
+
+      while (data->BytesRequested > 0 && !data->EOS)
+	{
+	  debug ("Waiting for %d bytes of data to be read.\n",
+		 data->BytesRequested);
+	  pthread_cond_wait (&data->ReadDoneCondition, &data->ReadDataMutex);
+	}
+      if (data->EOS)
+	ret -= data->BytesRequested;
       pthread_mutex_unlock (&data->ReadDataMutex);
     }
   debug ("buffer read done.\n");
@@ -236,27 +264,42 @@
 }
 
 /* These are no-ops for now. */
-int StreamBufferSeek (void *arg, ogg_int64_t offset, int whence)
+int
+StreamBufferSeek (void *arg, ogg_int64_t offset, int whence)
 {
   debug ("StreamBufferSeek\n");
   return -1;
 }
 
-int StreamBufferClose (void *arg)
+void StreamInputDataCleanup (StreamInputBufferData_t *data)
 {
-  buf_t *buf = arg;
-  StreamInputBufferData_t *data = buf->data;
+}
 
-  pthread_kill (data->CurlThread, SIGTERM);
-  pthread_join (data->CurlThread, NULL);
-  data->CurlThread = 0;
-  buffer_flush (buf);
-  buffer_shutdown (buf);
+int
+StreamBufferClose (void *arg)
+{
+  StreamInputBufferData_t *data = arg;
+
+  debug ("StreamBufferClose");
+  if (data)
+    {
+      pthread_kill (data->CurlThread, SIGTERM);
+      pthread_join (data->CurlThread, NULL);
+      memset (data, 0, sizeof(data));
+      free (data);
+    }
   return 0;
 }
 
-long StreamBufferTell (void *arg)
+long
+StreamBufferTell (void *arg)
 {
   return 0;
 }
 
+void StreamInputCleanup (buf_t *buf)
+{ 
+  StreamInputDataCleanup (buf->data);
+  buffer_flush (buf);
+  buffer_cleanup (buf);
+}

1.1.2.3   +5 -2      vorbis-tools/ogg123/Attic/curl_interface.h

Index: curl_interface.h
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/ogg123/Attic/curl_interface.h,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- curl_interface.h	2001/08/11 02:55:37	1.1.2.2
+++ curl_interface.h	2001/08/11 16:04:22	1.1.2.3
@@ -11,7 +11,7 @@
  *                                                                  *
  ********************************************************************
  
- last mod: $Id: curl_interface.h,v 1.1.2.2 2001/08/11 02:55:37 kcarnold Exp $
+ last mod: $Id: curl_interface.h,v 1.1.2.3 2001/08/11 16:04:22 kcarnold Exp $
  
 ********************************************************************/
 
@@ -43,6 +43,8 @@
   char *CookieFile;
 } InputOpts_t;
 
+#define VORBIS_CHUNKIN_SIZE (8500)
+
 typedef struct StreamInputBufferData_s {
   pthread_t CurlThread;
   pthread_mutex_t ReadDataMutex;
@@ -56,7 +58,7 @@
   size_t BytesRequested;
   unsigned char *WriteTarget;
   unsigned char *CurWritePtr;
-  unsigned char ExcessData[TARGET_WRITE_SIZE];
+  unsigned char ExcessData[VORBIS_CHUNKIN_SIZE];
   int ExcessDataSize;
 } StreamInputBufferData_t;
 
@@ -65,5 +67,6 @@
 int StreamBufferSeek (void *arg, ogg_int64_t offset, int whence);
 int StreamBufferClose (void *arg);
 long StreamBufferTell (void *arg);
+void StreamBufferCleanup (buf_t *buf);
 
 #endif /* __CURL_INTERFACE_H */

1.39.2.12 +8 -5      vorbis-tools/ogg123/ogg123.c

Index: ogg123.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/ogg123/ogg123.c,v
retrieving revision 1.39.2.11
retrieving revision 1.39.2.12
diff -u -r1.39.2.11 -r1.39.2.12
--- ogg123.c	2001/08/11 02:55:37	1.39.2.11
+++ ogg123.c	2001/08/11 16:04:22	1.39.2.12
@@ -14,7 +14,7 @@
  *                                                                  *
  ********************************************************************
 
- last mod: $Id: ogg123.c,v 1.39.2.11 2001/08/11 02:55:37 kcarnold Exp $
+ last mod: $Id: ogg123.c,v 1.39.2.12 2001/08/11 16:04:22 kcarnold Exp $
 
  ********************************************************************/
 
@@ -247,7 +247,7 @@
         opt.prebuffer = (int) ((double) opt.prebuffer * (double) opt.buffer_size / 100.0F);
         OutBuffer = StartBuffer (opt.buffer_size, opt.prebuffer,
                                  opt.outdevices, devices_write,
-				 NULL, NULL);
+				 NULL, NULL, 4096);
       }
     
     if (opt.shuffle) {
@@ -272,8 +272,6 @@
 
     if (OutBuffer != NULL) {
       buffer_WaitForEmpty (OutBuffer);
-      buffer_cleanup (OutBuffer);
-      OutBuffer = NULL;
     }
     
     exit (0);
@@ -344,7 +342,7 @@
       VorbisfileCallbacks.tell_func = StreamBufferTell;
       
       inputOpts.BufferSize = 1024*1024;
-      inputOpts.Prebuffer = 0;
+      inputOpts.Prebuffer = 10000;
       inputOpts.URL = opt.read_file;
       InBuffer = InitStream (inputOpts);
       if ((ov_open_callbacks (InBuffer->data, &vf, NULL, 0, VorbisfileCallbacks)) < 0) {
@@ -721,6 +719,11 @@
 {
   ogg123_options_t *opt = (ogg123_options_t*) arg;
 
+  if (InBuffer) {
+    StreamInputCleanup (InBuffer);
+    InBuffer = NULL;
+  }
+      
   if (OutBuffer) {
     buffer_flush (OutBuffer);
     buffer_cleanup (OutBuffer);

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list