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

Stan Seibert volsung at xiph.org
Wed Nov 21 14:57:25 PST 2001



volsung     01/11/21 14:57:24

  Modified:    ogg123   Tag: volsung_kc_20011011 buffer.c buffer.h ogg123.c
  Log:
  Squashes some annoying threading bugs (deadlocking when EOS and prebuff),
  and partially fixes the times displayed on the status line.  Complete fix
  of the status line requires a slight redesign, which will come later.

Revision  Changes    Path
No                   revision

No                   revision

1.7.2.23.2.4 +21 -4     vorbis-tools/ogg123/buffer.c

Index: buffer.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/ogg123/buffer.c,v
retrieving revision 1.7.2.23.2.3
retrieving revision 1.7.2.23.2.4
diff -u -r1.7.2.23.2.3 -r1.7.2.23.2.4
--- buffer.c	2001/10/17 16:58:14	1.7.2.23.2.3
+++ buffer.c	2001/11/21 22:57:23	1.7.2.23.2.4
@@ -11,7 +11,7 @@
  *                                                                  *
  ********************************************************************
 
- last mod: $Id: buffer.c,v 1.7.2.23.2.3 2001/10/17 16:58:14 volsung Exp $
+ last mod: $Id: buffer.c,v 1.7.2.23.2.4 2001/11/21 22:57:23 volsung Exp $
 
  ********************************************************************/
 
@@ -98,8 +98,6 @@
       if (!ret)
         pthread_exit ((void*)ret);
     }
-	
-  _buffer_init_vars(buf);
 }
 
 
@@ -147,7 +145,9 @@
          NEVER reduce the number of bytes stored in the buffer */
       DEBUG("Sending %d bytes to the audio device", write_amount);
       buf->write_func(buf->buffer + buf->start, sizeof(chunk), write_amount,
-		      buf->data, buf->eos);
+		      buf->data, 
+		      /* Only set EOS if this is the last chunk */
+		      write_amount == buf->curfill ? buf->eos : 0);
       
       LOCK_MUTEX(buf->mutex);
       buf->curfill -= write_amount;
@@ -283,6 +283,22 @@
 }
 
 
+void buffer_sync_reset (buf_t *buf)
+{
+  /* Cleanup pthread variables */
+  pthread_mutex_destroy (&buf->mutex);
+  pthread_cond_destroy (&buf->write_cond);
+  pthread_cond_destroy (&buf->playback_cond);
+  
+  /* Reinit pthread variables */
+  pthread_mutex_init (&buf->mutex, NULL);
+  pthread_cond_init (&buf->write_cond, NULL);
+  pthread_cond_init (&buf->playback_cond, NULL);
+
+  _buffer_init_vars(buf);
+}
+
+
 void buffer_destroy (buf_t *buf)
 {
   DEBUG("buffer_destroy");
@@ -414,6 +430,7 @@
 
   LOCK_MUTEX(buf->mutex);
   buf->eos = 1;
+  buf->prebuffering = 0;
   COND_SIGNAL(buf->playback_cond);
   UNLOCK_MUTEX(buf->mutex);
 }

1.2.2.16.2.3 +3 -1      vorbis-tools/ogg123/buffer.h

Index: buffer.h
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/ogg123/buffer.h,v
retrieving revision 1.2.2.16.2.2
retrieving revision 1.2.2.16.2.3
diff -u -r1.2.2.16.2.2 -r1.2.2.16.2.3
--- buffer.h	2001/10/17 16:58:14	1.2.2.16.2.2
+++ buffer.h	2001/11/21 22:57:23	1.2.2.16.2.3
@@ -11,7 +11,7 @@
  *                                                                  *
  ********************************************************************
  
- last mod: $Id: buffer.h,v 1.2.2.16.2.2 2001/10/17 16:58:14 volsung Exp $
+ last mod: $Id: buffer.h,v 1.2.2.16.2.3 2001/11/21 22:57:23 volsung Exp $
  
 ********************************************************************/
 
@@ -68,6 +68,8 @@
 buf_t *buffer_create (long size, long prebuffer, void *data, 
                       pWriteFunc write_func, void *initData, 
                       pInitFunc init_func, int audio_chunk_size);
+
+void buffer_sync_reset (buf_t *buf);
 void buffer_destroy (buf_t *buf);
 
 /* --- Buffer thread control --- */

1.39.2.30.2.8 +32 -39    vorbis-tools/ogg123/ogg123.c

Index: ogg123.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/ogg123/ogg123.c,v
retrieving revision 1.39.2.30.2.7
retrieving revision 1.39.2.30.2.8
diff -u -r1.39.2.30.2.7 -r1.39.2.30.2.8
--- ogg123.c	2001/11/02 02:51:47	1.39.2.30.2.7
+++ ogg123.c	2001/11/21 22:57:23	1.39.2.30.2.8
@@ -14,7 +14,7 @@
  *                                                                  *
  ********************************************************************
 
- last mod: $Id: ogg123.c,v 1.39.2.30.2.7 2001/11/02 02:51:47 volsung Exp $
+ last mod: $Id: ogg123.c,v 1.39.2.30.2.8 2001/11/21 22:57:23 volsung Exp $
 
  ********************************************************************/
 
@@ -171,7 +171,7 @@
   cur->type = stat_doublearg;
 
   cur = &stats[7]; /* input buffer status */
-  cur->prio = 2; cur->enabled = 0; cur->formatstr = "(%s)";
+  cur->prio = 2; cur->enabled = 0; cur->formatstr = "%s";
   cur->type = stat_stringarg; cur->arg.stringarg = NULL;
 
   cur = &stats[8]; /* output buffer fill % */
@@ -223,7 +223,7 @@
   char *sep = "(";
 
   if (buf->prebuffering) {
-    cur += sprintf (cur, "%sPrebuffering", sep);
+    cur += sprintf (cur, "%sPrebuf", sep);
     sep = comma;
   }
   if (buf->paused) {
@@ -236,6 +236,8 @@
   }
   if (cur != strbuf)
     cur += sprintf (cur, ")");
+  else
+    *cur = '\0';
 }
 
 
@@ -256,8 +258,6 @@
           / (double) Options.inputOpts.data->buf->size * 100.0f;
       }
 
-      if (stats[7].arg.stringarg)
-	free (stats[7].arg.stringarg);
       stats[7].arg.stringarg = strdup (strbuf);
       
       memset (strbuf, 0, 80);
@@ -274,9 +274,10 @@
       stats[9].arg.stringarg = strdup (strbuf);
       
       PrintStatsLine (stats);
+
+      pthread_mutex_unlock(&stats_lock);
     }
 
-  pthread_mutex_unlock(&stats_lock);
 }
 
 
@@ -287,45 +288,22 @@
                       char iseos)
 {
   static ogg_int64_t cursample = 0;
-  static unsigned char RechunkBuffer[BUFFER_CHUNK_SIZE];
-  static size_t curBuffered = 0;
   size_t origSize;
-  unsigned char *data = ptr;
 
   origSize = size;
   size *= nmemb;
   
-  SetTime (Options.statOpts.stats, cursample);
-  UpdateStats();
   cursample += Options.playOpts.nth * size / Options.outputOpts.channels / 2 
     / Options.playOpts.ntimes; /* locked to 16-bit */
-  //  fprintf(stderr, "nmemb = %d, size = %d, cursample = %lld\n", nmemb, size,
-  //  cursample);
 
-  /* optimized fast path */
-  if (curBuffered == BUFFER_CHUNK_SIZE && curBuffered == 0)
-    devices_write (ptr, size, 1, Options.outputOpts.devices);
-  else
-    /* don't actually write until we have a full chunk, or of course EOS */
-    while (size) {
-      size_t toChunkNow = BUFFER_CHUNK_SIZE - curBuffered <= size ? 
-	BUFFER_CHUNK_SIZE - curBuffered : size;
-      memmove (RechunkBuffer + curBuffered, data, toChunkNow);
-      size -= toChunkNow;
-      data += toChunkNow;
-      curBuffered += toChunkNow;
-      if (curBuffered == BUFFER_CHUNK_SIZE) {
-	devices_write (RechunkBuffer, curBuffered, 1, 
-		       Options.outputOpts.devices);
-	curBuffered = 0;
-      }
-    }
+  devices_write (ptr, size, 1, Options.outputOpts.devices);
+
+  SetTime (Options.statOpts.stats, cursample);
+  UpdateStats();
 
-  if (iseos) {
+  if (iseos)
     cursample = 0;
-    devices_write (RechunkBuffer, curBuffered, 1, Options.outputOpts.devices);
-    curBuffered = 0;
-  }
+
   return origSize;
 }
 
@@ -737,7 +715,8 @@
   int is_big_endian = ao_is_big_endian();
   double realseekpos = Options.playOpts.seekpos;
   int nthc = 0, ntimesc = 0;
-    ov_callbacks VorbisfileCallbacks;
+  long bitrate;
+  ov_callbacks VorbisfileCallbacks;
   
   /* Setup callbacks and data structures for HTTP stream or file */
   if (IsURL(Options.playOpts.read_file))
@@ -872,8 +851,15 @@
     
 
     /* Start the audio playback thread before we begin sending data */    
-    if (Options.outputOpts.buffer)
+    if (Options.outputOpts.buffer) {
+
+      /* First reset mutexes and other synchronization variables */
+      buffer_sync_reset (Options.outputOpts.buffer);
+      pthread_mutex_destroy (&stats_lock);
+      pthread_mutex_init (&stats_lock, NULL);
+
       buffer_thread_start (Options.outputOpts.buffer);
+    }
     
 
     /* Loop through data within a logical bitstream */
@@ -932,8 +918,15 @@
         
 
         /* Update bitrate display */
-	Options.statOpts.stats[4].arg.doublearg = 
-	  (double) ov_bitrate_instant (&vf) / 1000.0f;
+	bitrate = ov_bitrate_instant (&vf);
+	if (bitrate == OV_FALSE) {
+	  /* Not playing */
+	  Options.statOpts.stats[4].arg.doublearg = 0.0f;
+	} else if (bitrate > 0) {
+	  /* New bitrate information to report */
+	  Options.statOpts.stats[4].arg.doublearg = 
+	    (double) bitrate  / 1000.0f;
+	}
 
 
         /* Write audio data block to output, skipping or repeating chunks

--- >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