~ubuntu-branches/debian/sid/vorbis-tools/sid

« back to all changes in this revision

Viewing changes to ogg123/buffer.c

  • Committer: Bazaar Package Importer
  • Author(s): Jesus Climent
  • Date: 2005-04-10 09:22:24 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050410092224-xtukpa3qghghhjje
Tags: 1.0.1-1.3
* Authorized NMU.
* Modified alsa to mention alsa09 (although the device might be nowadays
  alsa, back, since alsa1.0 has been already released). (Closes: #258286)
* Modified the manpage/help message for vorbiscomment to make it a bit more
  userfiendly: Closes: #252531.
* Added oggdec to the long description field, so that it triggers apt-cache
  searches: Closes: #274894.
* Typos in manpages: Closes: #302150.
* Escaped dashes in manpage: Closes: #264365.
* Quiet option is actually with -Q, not -q (Closes: #211289) Reported
  upstream but patched for Debian.
* Change input.wav with inputfile, since we accept flac-formated files:
  Closes: #262509.
* Translation bits:
  * Updated translation hu.po: Closes: #272037.
  * French translation correction: Encodage -> Codage (Closes: #248431).
  * debian/rules: remove .gmo's to avoid clash with uploaded tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 *                                                                  *
12
12
 ********************************************************************
13
13
 
14
 
 last mod: $Id: buffer.c,v 1.13 2001/12/20 00:24:53 volsung Exp $
 
14
 last mod: $Id: buffer.c,v 1.22 2003/09/01 20:15:19 volsung Exp $
15
15
 
16
16
 ********************************************************************/
17
17
 
27
27
 
28
28
#include "compat.h"
29
29
#include "buffer.h"
 
30
#include "i18n.h"
 
31
#include "ogg123.h"
30
32
 
31
33
#define MIN(x,y)       ( (x) < (y) ? (x) : (y) )
32
34
#define MIN3(x,y,z)    MIN(x,MIN(y,z))
33
35
#define MIN4(w,x,y,z)  MIN( MIN(w,x), MIN(y,z) )
34
36
 
35
 
/* Special debugging code.  THIS IS NOT PORTABLE! */
36
37
#ifdef DEBUG_BUFFER
37
38
FILE *debugfile;
38
 
#define DEBUG(x, y...) { fprintf (debugfile, "%d: " x "\n", getpid(),  ## y); }
 
39
#define DEBUG(x) { fprintf (debugfile, "%d: " x "\n", getpid()); }
 
40
#define DEBUG1(x, y) { fprintf (debugfile, "%d: " x "\n", getpid(), y); }
 
41
#define DEBUG2(x, y, z) { fprintf (debugfile, "%d: " x "\n", getpid(), y, z); }
39
42
#else
40
 
#define DEBUG(x, y...)
 
43
#define DEBUG(x)
 
44
#define DEBUG1(x, y)
 
45
#define DEBUG2(x, y, z)
41
46
#endif
42
47
 
43
48
/* Macros that support debugging of threading structures */
44
49
 
45
 
#define LOCK_MUTEX(mutex) { DEBUG("Locking mutex %s.", #mutex); pthread_mutex_lock (&(mutex)); }
46
 
#define UNLOCK_MUTEX(mutex) { DEBUG("Unlocking mutex %s", #mutex); pthread_mutex_unlock(&(mutex)); }
47
 
#define COND_WAIT(cond, mutex) { DEBUG("Unlocking %s and waiting on %s", #mutex, #cond); pthread_cond_wait(&(cond), &(mutex)); }
48
 
#define COND_SIGNAL(cond) { DEBUG("Signalling %s", #cond); pthread_cond_signal(&(cond)); }
 
50
#define LOCK_MUTEX(mutex) { DEBUG1("Locking mutex %s.", #mutex); pthread_mutex_lock (&(mutex)); }
 
51
#define UNLOCK_MUTEX(mutex) { DEBUG1("Unlocking mutex %s", #mutex); pthread_mutex_unlock(&(mutex)); }
 
52
#define COND_WAIT(cond, mutex) { DEBUG2("Unlocking %s and waiting on %s", #mutex, #cond); pthread_cond_wait(&(cond), &(mutex)); }
 
53
#define COND_SIGNAL(cond) { DEBUG1("Signalling %s", #cond); pthread_cond_signal(&(cond)); }
 
54
 
 
55
extern signal_request_t sig_request;  /* Need access to global cancel flag */
49
56
 
50
57
 
51
58
/* -------------------- Private Functions ------------------ */
57
64
  buf->paused = 0;
58
65
  buf->eos = 0;
59
66
  buf->abort_write = 0;
60
 
  
 
67
  buf->cancel_flag = 0;
 
68
 
61
69
  buf->curfill = 0;
62
70
  buf->start = 0;
63
71
  buf->position = 0;
85
93
  buf_t *buf = (buf_t *)arg;
86
94
 
87
95
  DEBUG("Enter buffer_thread_cleanup");
88
 
 
89
 
  /* Cleanup thread data structures */
90
 
  pthread_mutex_unlock(&buf->mutex);
91
 
  pthread_mutex_destroy(&buf->mutex);
92
 
  pthread_cond_destroy(&buf->playback_cond);
93
 
  pthread_cond_destroy(&buf->write_cond);
94
96
}
95
97
 
96
98
 
109
111
  action = malloc(sizeof(action_t));
110
112
  
111
113
  if (action == NULL) {
112
 
    fprintf(stderr, "Error: Out of memory in malloc_action().\n");
 
114
    fprintf(stderr, _("Error: Out of memory in malloc_action().\n"));
113
115
    exit(1);
114
116
  }
115
117
 
201
203
  
202
204
  /* This test is safe since curfill will never decrease and eos will
203
205
     never be unset. */
204
 
  while ( !(buf->eos && buf->curfill == 0) ) {
 
206
  while ( !(buf->eos && buf->curfill == 0)) {
 
207
 
 
208
    if (buf->cancel_flag || sig_request.cancel)
 
209
      break;
205
210
    
206
211
    DEBUG("Check for something to play");
207
212
    /* Block until we can play something */
213
218
      DEBUG("Waiting for more data to play.");
214
219
      COND_WAIT(buf->playback_cond, buf->mutex);
215
220
    }
216
 
 
 
221
    
217
222
    DEBUG("Ready to play");
218
223
    
219
224
    UNLOCK_MUTEX(buf->mutex);
220
225
 
221
 
    pthread_testcancel();
 
226
    if (buf->cancel_flag || sig_request.cancel)
 
227
      break;
222
228
 
223
229
    /* Don't need to lock buffer while running actions since position
224
230
       won't change.  We clear out any actions before we compute the
231
237
    /* Need to be locked while we check things. */
232
238
    write_amount = compute_dequeue_size(buf, buf->audio_chunk_size);
233
239
 
234
 
    pthread_testcancel();
235
 
 
236
240
    UNLOCK_MUTEX(buf->mutex);
237
241
 
238
242
    /* No need to lock mutex here because the other thread will
239
243
       NEVER reduce the number of bytes stored in the buffer */
240
 
    DEBUG("Sending %d bytes to the audio device", write_amount);
241
 
    buf->write_func(buf->buffer + buf->start, write_amount,
 
244
    DEBUG1("Sending %d bytes to the audio device", write_amount);
 
245
    write_amount = buf->write_func(buf->buffer + buf->start, write_amount,
242
246
                    /* Only set EOS if this is the last chunk */
243
247
                    write_amount == buf->curfill ? buf->eos : 0,
244
248
                    buf->write_arg);
248
252
    buf->curfill -= write_amount;
249
253
    buf->position += write_amount;
250
254
    buf->start = (buf->start + write_amount) % buf->size;
251
 
    DEBUG("Updated buffer fill, curfill = %ld", buf->curfill);
 
255
    DEBUG1("Updated buffer fill, curfill = %ld", buf->curfill);
252
256
 
253
257
    /* If we've essentially emptied the buffer and prebuffering is enabled,
254
258
       we need to do another prebuffering session */
275
279
  long   buf_write_pos; /* offset of first available write location */
276
280
  size_t write_size;
277
281
 
278
 
  DEBUG("Enter submit_data_chunk, size %d", size);
 
282
  DEBUG1("Enter submit_data_chunk, size %d", size);
279
283
 
280
284
  pthread_cleanup_push(buffer_mutex_unlock, buf);
281
285
 
282
286
  /* Put the data into the buffer as space is made available */
283
 
  while (size > 0) {
 
287
  while (size > 0 && !buf->abort_write) {
284
288
    
285
289
    /* Section 1: Write a chunk of data */
286
290
    DEBUG("Obtaining lock on buffer");
300
304
      data += write_size;
301
305
      size -= write_size;
302
306
      buf->position_end += write_size;
303
 
      DEBUG("writing chunk into buffer, curfill = %ld", buf->curfill);
 
307
      DEBUG1("writing chunk into buffer, curfill = %ld", buf->curfill);
304
308
    }
305
309
    else {
306
310
 
340
344
  new_stats = malloc(sizeof(buffer_stats_t));
341
345
 
342
346
  if (new_stats == NULL) {
343
 
    fprintf(stderr, "Error: Could not allocate memory in malloc_buffer_stats()\n");
 
347
    fprintf(stderr, _("Error: Could not allocate memory in malloc_buffer_stats()\n"));
344
348
    exit(1);
345
349
  }
346
350
 
369
373
#endif
370
374
 
371
375
  /* Initialize the buffer structure. */
372
 
  DEBUG("buffer_create, size = %ld", size);
 
376
  DEBUG1("buffer_create, size = %ld", size);
373
377
 
374
378
  memset (buf, 0, sizeof(*buf));
375
379
 
430
434
void buffer_destroy (buf_t *buf)
431
435
{
432
436
  DEBUG("buffer_destroy");
 
437
 
 
438
  /* Cleanup pthread variables */
 
439
  pthread_mutex_destroy(&buf->mutex);
 
440
  COND_SIGNAL(buf->write_cond);
 
441
  pthread_cond_destroy(&buf->write_cond);
 
442
  COND_SIGNAL(buf->playback_cond);
 
443
  pthread_cond_destroy(&buf->playback_cond);
 
444
  
433
445
  free(buf);
434
446
}
435
447
 
480
492
{
481
493
  DEBUG("Attempting to kill playback thread.");
482
494
 
483
 
  /* End thread */
484
 
  pthread_cancel (buf->thread);
 
495
  /* Flag the cancellation */
 
496
  buf->cancel_flag = 1;
485
497
  
486
 
  /* Signal all the playback condition to wake stuff up */
 
498
  /* Signal the playback condition to wake stuff up */
487
499
  COND_SIGNAL(buf->playback_cond);
488
500
 
489
501
  pthread_join(buf->thread, NULL);
517
529
  /* Put the data into the buffer as space is made available */
518
530
  while (nbytes > 0) {
519
531
 
 
532
    if (buf->abort_write)
 
533
      break;
 
534
 
520
535
    DEBUG("Obtaining lock on buffer");
521
536
    /* Block until we can read something */
522
537
    if (buf->curfill == 0 && buf->eos)
523
538
      break; /* No more data to read */
524
539
      
525
 
    if (buf->curfill == 0 && (buf->prebuffering && !buf->eos)) {
 
540
    if (buf->curfill == 0 || (buf->prebuffering && !buf->eos)) {
526
541
      DEBUG("Waiting for more data to copy.");
527
542
      COND_WAIT(buf->playback_cond, buf->mutex);
528
543
    }
545
560
 
546
561
    /* No need to lock mutex here because the other thread will
547
562
       NEVER reduce the number of bytes stored in the buffer */
548
 
    DEBUG("Copying %d bytes from the buffer", write_amount);
 
563
    DEBUG1("Copying %d bytes from the buffer", write_amount);
549
564
    memcpy(data, buf->buffer + buf->start, write_amount);
550
565
    LOCK_MUTEX(buf->mutex);
551
566
 
553
568
    data += write_amount;
554
569
    nbytes -= write_amount;
555
570
    buf->start = (buf->start + write_amount) % buf->size;
556
 
    DEBUG("Updated buffer fill, curfill = %ld", buf->curfill);
 
571
    DEBUG1("Updated buffer fill, curfill = %ld", buf->curfill);
557
572
    
558
573
    /* Signal a waiting decoder thread that they can put more
559
574
       audio into the buffer */
589
604
 
590
605
void buffer_abort_write (buf_t *buf)
591
606
{
592
 
  DEBUG("buffer_mark_eos");
 
607
  DEBUG("buffer_abort_write");
593
608
 
594
609
  pthread_cleanup_push(buffer_mutex_unlock, buf);
595
610
 
596
611
  LOCK_MUTEX(buf->mutex);
597
612
  buf->abort_write = 1;
598
613
  COND_SIGNAL(buf->write_cond);
 
614
  COND_SIGNAL(buf->playback_cond);
599
615
  UNLOCK_MUTEX(buf->mutex);  
600
616
 
601
617
  pthread_cleanup_pop(0);
727
743
  while (!empty) {
728
744
 
729
745
    if (buf->curfill > 0) {
730
 
      DEBUG("Buffer curfill = %ld, going back to sleep.", buf->curfill);
 
746
      DEBUG1("Buffer curfill = %ld, going back to sleep.", buf->curfill);
731
747
      COND_WAIT(buf->write_cond, buf->mutex);
732
748
    } else 
733
749
      empty = 1;