summaryrefslogtreecommitdiff
path: root/src/xine-engine/metronom.c
blob: 6b3effcc6eed062593f6ff830bdb77d2ce887902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/* 
 * Copyright (C) 2000-2001 the xine project
 * 
 * This file is part of xine, a unix video player.
 * 
 * xine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * xine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 * $Id: metronom.c,v 1.32 2001/10/24 22:23:10 guenter Exp $
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/time.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include "monitor.h"
#include "xine_internal.h"
#include "metronom.h"
#include "utils.h"

#define MAX_PTS_TOLERANCE  5000
#define MAX_VIDEO_DELTA    1600
#define MAX_AUDIO_DELTA    1600
#define AUDIO_SAMPLE_NUM   32768
#define WRAP_START_TIME    100000
#define WRAP_TRESHOLD      30000 
#define MAX_NUM_WRAP_DIFF  100
#define MAX_SCR_PROVIDERS  10
#define REALTIME_PTS       90000.0
#define PREBUFFER_PTS_OFFSET 30000

/*
#define METRONOM_LOG
*/

/*
 * ****************************************
 *   primary SCR plugin: 
 *    unix System Clock Reference
 * ****************************************
 */

typedef struct unixscr_s {
  scr_plugin_t     scr;

  struct timeval   cur_time;
  uint32_t         cur_pts;
  double           speed_factor;

  pthread_mutex_t  lock;

} unixscr_t;

static int unixscr_get_priority (scr_plugin_t *scr) {
  return 5; /* low priority */
}

/* Only call this when already mutex locked */
static void unixscr_set_pivot (unixscr_t *this) {

  struct   timeval tv;
  uint32_t pts;
  double   pts_calc; 

  gettimeofday(&tv, NULL);
  pts_calc = (tv.tv_sec  - this->cur_time.tv_sec) * this->speed_factor;
  pts_calc += (tv.tv_usec - this->cur_time.tv_usec) * this->speed_factor / 1e6;
  pts = this->cur_pts + pts_calc;

/* This next part introduces a one off inaccuracy 
 * to the scr due to rounding tv to pts. 
 */
  this->cur_time.tv_sec=tv.tv_sec;
  this->cur_time.tv_usec=tv.tv_usec;
  this->cur_pts=pts; 

  return ;
}

static int unixscr_set_speed (scr_plugin_t *scr, int speed) {
  unixscr_t *this = (unixscr_t*) scr;

  pthread_mutex_lock (&this->lock);

  unixscr_set_pivot( this );
  this->speed_factor = (double) speed * 90000.0 / 4.0;

  pthread_mutex_unlock (&this->lock);

  return speed;
}

static void unixscr_adjust (scr_plugin_t *scr, uint32_t vpts) {
  unixscr_t *this = (unixscr_t*) scr;
  struct   timeval tv;

  pthread_mutex_lock (&this->lock);

  gettimeofday(&tv, NULL);
  this->cur_time.tv_sec=tv.tv_sec;
  this->cur_time.tv_usec=tv.tv_usec;
  this->cur_pts = vpts;

  pthread_mutex_unlock (&this->lock);
}

static void unixscr_start (scr_plugin_t *scr, uint32_t start_vpts) {
  unixscr_t *this = (unixscr_t*) scr;

  pthread_mutex_lock (&this->lock);

  gettimeofday(&this->cur_time, NULL);
  this->cur_pts = start_vpts;

  pthread_mutex_unlock (&this->lock);
}

static uint32_t unixscr_get_current (scr_plugin_t *scr) {
  unixscr_t *this = (unixscr_t*) scr;

  struct   timeval tv;
  uint32_t pts;
  double   pts_calc; 
  pthread_mutex_lock (&this->lock);

  gettimeofday(&tv, NULL);
  
  pts_calc = (tv.tv_sec  - this->cur_time.tv_sec) * this->speed_factor;
  pts_calc += (tv.tv_usec - this->cur_time.tv_usec) * this->speed_factor / 1e6;

  pts = this->cur_pts + pts_calc;
  
  pthread_mutex_unlock (&this->lock);

  return pts;
}

static scr_plugin_t* unixscr_init () {
  unixscr_t *this;

  this = malloc(sizeof(*this));
  memset(this, 0, sizeof(*this));
  
  this->scr.interface_version = 2;
  this->scr.get_priority      = unixscr_get_priority;
  this->scr.set_speed         = unixscr_set_speed;
  this->scr.adjust            = unixscr_adjust;
  this->scr.start             = unixscr_start;
  this->scr.get_current       = unixscr_get_current;
  unixscr_set_speed (&this->scr, SPEED_NORMAL);

  pthread_mutex_init (&this->lock, NULL);

  return &this->scr;
}
 

/*
 * ****************************************
 *       master clock feature
 * ****************************************
 */


static void metronom_start_clock (metronom_t *this, uint32_t pts) {
  scr_plugin_t** scr;
  for (scr = this->scr_list; scr < this->scr_list+MAX_SCR_PROVIDERS; scr++)
    if (*scr) (*scr)->start(*scr, pts);
}


static uint32_t metronom_get_current_time (metronom_t *this) {
  return this->scr_master->get_current(this->scr_master);
}


static void metronom_stop_clock(metronom_t *this) {
  scr_plugin_t** scr;
  for (scr = this->scr_list; scr < this->scr_list+MAX_SCR_PROVIDERS; scr++)
    if (*scr) (*scr)->set_speed(*scr, SPEED_PAUSE);
}

static void metronom_resume_clock(metronom_t *this) {
  scr_plugin_t** scr;
  for (scr = this->scr_list; scr < this->scr_list+MAX_SCR_PROVIDERS; scr++)
    if (*scr) (*scr)->set_speed(*scr, SPEED_NORMAL);
}



static void metronom_adjust_clock(metronom_t *this, uint32_t desired_pts) {
  this->scr_master->adjust(this->scr_master, desired_pts);
}

static int metronom_set_speed (metronom_t *this, int speed) {

  scr_plugin_t **scr;
  int            true_speed;

  true_speed = this->scr_master->set_speed (this->scr_master, speed);

  for (scr = this->scr_list; scr < this->scr_list+MAX_SCR_PROVIDERS; scr++)
    if (*scr) (*scr)->set_speed(*scr, true_speed);

  return true_speed;
}

/*
 * virtual pts calculation
*/

static void metronom_video_stream_start (metronom_t *this) {

  pthread_mutex_lock (&this->lock);

  printf ("metronom: video stream start...\n");

  if (this->video_stream_running) {
    printf ("metronom: video stream start ignored\n");
    pthread_mutex_unlock (&this->lock);
    return;
  }

  this->pts_per_frame             = 3000;

  this->video_vpts                = PREBUFFER_PTS_OFFSET;

  this->video_pts_delta           = 0;

  this->last_video_pts            = 0;
  this->num_video_vpts_guessed    = 1;

  this->video_wrap_offset         = 0;
  this->wrap_diff_counter         = 0;

  this->video_stream_running      = 1;
  this->video_stream_starting     = 1;

  if (this->have_audio) {
    /*while (!this->audio_stream_running) {*/
    if (!this->audio_stream_running) {
      printf ("metronom: waiting for audio to start...\n");
      pthread_cond_wait (&this->audio_started, &this->lock);
    }
  }
  pthread_cond_signal (&this->video_started);

  pthread_mutex_unlock (&this->lock);

  metronom_start_clock (this, 0);
}


static void metronom_video_stream_end (metronom_t *this) {
  
  pthread_mutex_lock (&this->lock);

  printf ("metronom: video stream end\n");

  if (!this->video_stream_running) {
    printf ("metronom: video stream end ignored\n");
    pthread_mutex_unlock (&this->lock);
    return;
  }

  this->video_stream_running = 0;

  if (this->have_audio) {
    /* while (this->audio_stream_running) { */
    if (this->audio_stream_running) {
      printf ("metronom: waiting for audio to end...\n");
      pthread_cond_wait (&this->audio_ended, &this->lock);
    }
  }
  pthread_cond_signal (&this->video_ended);


  pthread_mutex_unlock (&this->lock);
}

static void metronom_audio_stream_start (metronom_t *this) {

  pthread_mutex_lock (&this->lock);

  printf ("metronom: audio stream start...\n");

  if (this->audio_stream_running) {
    printf ("metronom: audio stream start ignored\n");
    pthread_mutex_unlock (&this->lock);
    return;
  }

  this->audio_vpts                = PREBUFFER_PTS_OFFSET;

  this->audio_pts_delta           = 0;

  this->num_audio_samples_guessed = 1;
  this->last_audio_pts            = 0;

  this->audio_wrap_offset         = 0;
  this->wrap_diff_counter         = 0;

  this->audio_stream_running      = 1;
  this->audio_stream_starting     = 1;

  /*while (!this->video_stream_running) { */
  if (!this->video_stream_running) {
    printf ("metronom: waiting for video to start...\n");
    pthread_cond_wait (&this->video_started, &this->lock);
  }

  pthread_cond_signal (&this->audio_started);

  pthread_mutex_unlock (&this->lock);

  printf ("metronom: audio stream start...done\n");

  metronom_start_clock (this, 0);
}

static void metronom_audio_stream_end (metronom_t *this) {
  
  pthread_mutex_lock (&this->lock);

  printf ("metronom: audio stream end\n");
  if (!this->audio_stream_running) {
    printf ("metronom: audio stream end ignored\n");
    pthread_mutex_unlock (&this->lock);
    return;
  }

  this->audio_stream_running = 0;

  /* while (this->video_stream_running) { */
  if (this->video_stream_running) {
    printf ("waiting for video to end...\n");
    pthread_cond_wait (&this->video_ended, &this->lock);
  }

  pthread_cond_signal (&this->audio_ended);
  pthread_mutex_unlock (&this->lock);
}

static void metronom_set_video_rate (metronom_t *this, uint32_t pts_per_frame) {
  pthread_mutex_lock (&this->lock);

  this->pts_per_frame = pts_per_frame;

  pthread_mutex_unlock (&this->lock);
}

static uint32_t metronom_get_video_rate (metronom_t *this) {
  return this->pts_per_frame + this->video_pts_delta;
}

static void metronom_set_audio_rate (metronom_t *this, uint32_t pts_per_smpls) {
  pthread_mutex_lock (&this->lock);

  this->pts_per_smpls = pts_per_smpls;

  pthread_mutex_unlock (&this->lock);

  xprintf (METRONOM | VERBOSE, "metronom: %d pts per %d samples\n", pts_per_smpls, AUDIO_SAMPLE_NUM);

}

static uint32_t metronom_got_spu_packet (metronom_t *this, uint32_t pts,uint32_t duration) {
  if (pts) {
    this->spu_vpts=pts;
  } else {
    pts=this->spu_vpts;
    this->spu_vpts=this->spu_vpts;
  }

  /* it happens with the dxr3 that got_spu_packet is called before  *
   * got_video_frame. Since video_wrap_offset is zero until then,   *
   * the return value would be wrong. In this case zero is returned */
  if (this->video_stream_starting)
    return 0;
    
  return pts + this->video_wrap_offset;
}

static void metronom_expect_audio_discontinuity (metronom_t *this) {

  printf ("metronom: expecting audio discontinuity\n");

  this->audio_discontinuity  = 10;
}

static void metronom_expect_video_discontinuity (metronom_t *this) {

  printf ("metronom: expecting video discontinuity\n");

  this->video_discontinuity  = 10;
}

static uint32_t metronom_got_video_frame (metronom_t *this, uint32_t pts) {

  uint32_t vpts;

  pthread_mutex_lock (&this->lock);

  if (pts) {

    /*
     * first video pts ?
     */
    if (this->video_stream_starting) {
      this->video_stream_starting = 0;
      
      this->video_wrap_offset = this->video_vpts + this->pts_per_frame - pts;
      assert (this->video_pts_delta == 0);
  
      if (this->audio_wrap_offset) {
	if (this->audio_wrap_offset>this->video_wrap_offset) 
	  this->video_wrap_offset = this->audio_wrap_offset;
	else
	  this->audio_wrap_offset = this->video_wrap_offset;
      }

      printf ("metronom: first video pts => offset = %d\n", this->video_wrap_offset); 

    }

    /*
     * did a wrap-around occur?
     */

    if ( ( (pts + WRAP_TRESHOLD) <this->last_video_pts) 
	 && (this->video_discontinuity || (pts<WRAP_START_TIME)) ) {
      
      this->video_discontinuity = 0;

      this->video_wrap_offset += this->last_video_pts - pts 
	+ this->num_video_vpts_guessed *(this->pts_per_frame + this->video_pts_delta);
      
      printf ("metronom: video pts wraparound detected, wrap_offset = %d\n",
	      this->video_wrap_offset);
    }

    /* don't expect discontinuities forever */
    if (this->video_discontinuity)
      this->video_discontinuity--;

    /*
     * audio and video wrap are not allowed to differ
     * for too long
     */

    if ( !this->audio_stream_starting && this->have_audio
	 && (this->video_wrap_offset != this->audio_wrap_offset)) {
      this->wrap_diff_counter++;

      if (this->wrap_diff_counter > MAX_NUM_WRAP_DIFF) {

	printf ("metronom: forcing video_wrap (%d) and audio wrap (%d)",
		this->video_wrap_offset, this->audio_wrap_offset);

	if (this->video_wrap_offset > this->audio_wrap_offset)
	  this->audio_wrap_offset = this->video_wrap_offset;
	else
	  this->video_wrap_offset = this->audio_wrap_offset;

	printf ("to %d\n", this->video_wrap_offset);

	this->wrap_diff_counter = 0;
      }
    }

    vpts = pts + this->video_wrap_offset;

    /*
     * jump into the future? 
     */
    if (this->video_discontinuity 
	&& (vpts > (this->video_vpts + 10000))) {

      printf ("metronom: video jump into the future\n");

      this->video_discontinuity = 0;

      this->video_wrap_offset += this->last_video_pts - pts 
	+ this->num_video_vpts_guessed *(this->pts_per_frame + this->video_pts_delta);

      vpts = pts + this->video_wrap_offset;
    }

    /*
     * calc delta to compensate wrong framerates 
     */
      
    if (this->last_video_pts && (pts>this->last_video_pts)) {
      int32_t  vpts_diff;

      vpts_diff   = vpts - this->video_vpts;

      this->video_pts_delta += vpts_diff / (this->num_video_vpts_guessed);
      
      if (abs(this->video_pts_delta) >= MAX_VIDEO_DELTA) 
	this->video_pts_delta = 0;
    }

    this->num_video_vpts_guessed = 0;
    this->last_video_pts  = pts;
    this->video_vpts      = vpts;
  } else
    vpts = this->video_vpts;

  this->video_vpts += this->pts_per_frame + this->video_pts_delta;
  this->num_video_vpts_guessed++ ;

#ifdef METRONOM_LOG
  printf ("metronom: video vpts for %10d : %10d\n", pts, vpts);
#endif

  pthread_mutex_unlock (&this->lock);

  return vpts + this->av_offset;
}

static void metronom_got_audio_still (metronom_t *this) {

  pthread_mutex_lock (&this->lock);

  this->audio_vpts            += this->pts_per_frame + this->video_pts_delta;
  this->audio_wrap_offset      = this->video_wrap_offset;
  this->last_audio_pts         = this->audio_vpts - this->audio_wrap_offset;
  this->audio_stream_starting  = 0;

#ifdef METRONOM_LOG
  printf ("metronom: got audio still, vpts = %d, wrap = %d\n", 
	  this->audio_vpts, this->audio_wrap_offset);
#endif

  pthread_mutex_unlock (&this->lock);
}

static uint32_t metronom_got_audio_samples (metronom_t *this, uint32_t pts, uint32_t nsamples) {

  uint32_t vpts;
  
  xprintf (METRONOM | VERBOSE, "metronom: got %d audio samples (pts=%d)\n",
	   nsamples,pts);

  pthread_mutex_lock (&this->lock);

  if (pts) {

    /*
     * first audio pts ?
     */
    if (this->audio_stream_starting) {
      this->audio_stream_starting = 0;
      
      this->audio_wrap_offset = this->audio_vpts + (nsamples * this->pts_per_smpls) / AUDIO_SAMPLE_NUM - pts;
      assert (this->audio_pts_delta == 0);

      if (this->video_wrap_offset) {
	if (this->audio_wrap_offset>this->video_wrap_offset) 
	  this->video_wrap_offset = this->audio_wrap_offset;
	else
	  this->audio_wrap_offset = this->video_wrap_offset;
      }

      printf ("metronom: first audio pts => offset = %d\n", this->audio_wrap_offset); 
    }

    /*
     * did a wrap-around occur?
     */
    if ( ( (pts + WRAP_TRESHOLD) < this->last_audio_pts )
	 && (this->audio_discontinuity || (pts<WRAP_START_TIME)) ) {

      this->audio_discontinuity = 0;
      
      this->audio_wrap_offset += this->last_audio_pts - pts
	+ this->num_audio_samples_guessed *(this->audio_pts_delta + this->pts_per_smpls) / AUDIO_SAMPLE_NUM ;
      
      printf ("metronom: audio pts wraparound detected, wrap_offset = %d\n",
	      this->audio_wrap_offset);
    }

    /* don't expect discontinuities forever */
    if (this->audio_discontinuity)
      this->audio_discontinuity--;

    /*
     * audio and video wrap are not allowed to differ
     * for too long
     */

    if ( !this->video_stream_starting
	 && this->video_wrap_offset != this->audio_wrap_offset) {
      this->wrap_diff_counter++;

      if (this->wrap_diff_counter > MAX_NUM_WRAP_DIFF) {

	printf ("metronom: forcing video_wrap (%d) and audio wrap (%d)",
		this->video_wrap_offset, this->audio_wrap_offset);

	if (this->video_wrap_offset > this->audio_wrap_offset)
	  this->audio_wrap_offset = this->video_wrap_offset;
	else
	  this->video_wrap_offset = this->audio_wrap_offset;

	printf ("to %d\n", this->video_wrap_offset);

	this->wrap_diff_counter = 0;
      }
    }

    vpts = pts + this->audio_wrap_offset;

    /*
     * jump into the future? 
     */
    if (this->audio_discontinuity 
	&& (vpts > (this->audio_vpts + 10000))) {

      printf ("metronom: video jump into the future\n");

      this->audio_discontinuity = 0;

      this->audio_wrap_offset += this->last_audio_pts - pts 
	+ this->num_audio_samples_guessed *(this->audio_pts_delta + this->pts_per_smpls) / AUDIO_SAMPLE_NUM ;

      vpts = pts + this->audio_wrap_offset;
    }

    /*
     * calc delta to compensate wrong samplerates 
     */

    if (this->last_audio_pts && (pts>this->last_audio_pts)) {
      int32_t  vpts_diff;

      vpts_diff   = vpts - this->audio_vpts;

      this->audio_pts_delta += vpts_diff*AUDIO_SAMPLE_NUM / (this->num_audio_samples_guessed);
	
      if (abs(this->audio_pts_delta) >= MAX_AUDIO_DELTA) 
	this->audio_pts_delta = 0;
    }      

    this->num_audio_samples_guessed = 0;
    this->last_audio_pts = pts;
    this->audio_vpts     = vpts;
  } else
    vpts = this->audio_vpts;

  this->audio_vpts += nsamples * (this->audio_pts_delta + this->pts_per_smpls) / AUDIO_SAMPLE_NUM;
  this->num_audio_samples_guessed += nsamples;

#ifdef METRONOM_LOG
  printf ("metronom: audio vpts for %10d : %10d\n", pts, vpts);
#endif

  pthread_mutex_unlock (&this->lock);

  return vpts;
}

static void metronom_set_av_offset (metronom_t *this, int32_t pts) {

  pthread_mutex_lock (&this->lock);

  this->av_offset = pts;

  pthread_mutex_unlock (&this->lock);

  printf ("metronom: av_offset=%d pts\n", pts);
}

static int32_t metronom_get_av_offset (metronom_t *this) {
  return this->av_offset;
}

static scr_plugin_t* get_master_scr(metronom_t *this) {
  int select = -1, maxprio = 0, i;

  /* find the SCR provider with the highest priority */
  for (i=0; i<MAX_SCR_PROVIDERS; i++) if (this->scr_list[i]) {
    scr_plugin_t *scr = this->scr_list[i];
    
    if (maxprio < scr->get_priority(scr)) {
      select = i;
      maxprio = scr->get_priority(scr);
    }
  }
  if (select < 0) {
    printf("panic: No scr provider found!\n");
    return NULL;
  }
  return this->scr_list[select];
}

static int metronom_register_scr (metronom_t *this, scr_plugin_t *scr) {
  int i;

  if (scr->interface_version != 2) return -1;

  for (i=0; i<MAX_SCR_PROVIDERS; i++)
    if (this->scr_list[i] == NULL) break;
  if (i >= MAX_SCR_PROVIDERS)
    return -1; /* No free slot available */

  scr->metronom = this;
  this->scr_list[i] = scr;
  this->scr_master = get_master_scr(this);
  return 0;
}

static void metronom_unregister_scr (metronom_t *this, scr_plugin_t *scr) {
  int i;

  /* Never unregister scr_list[0]! */
  for (i=1; i<MAX_SCR_PROVIDERS; i++)
    if (this->scr_list[i] == scr) break;

  if (i >= MAX_SCR_PROVIDERS)
    return; /* Not found */
  
  this->scr_list[i] = NULL;
  this->scr_master = get_master_scr(this);
}

static int metronom_sync_loop (metronom_t *this) {
  scr_plugin_t** scr;
  uint32_t pts;
  
  while (1) {
    pts = this->scr_master->get_current(this->scr_master);
    
    for (scr = this->scr_list; scr < this->scr_list+MAX_SCR_PROVIDERS; scr++)
      if (*scr && *scr != this->scr_master) (*scr)->adjust(*scr, pts);

    sleep(5); /* synchronise every 5 seconds */
  }
}


metronom_t * metronom_init (int have_audio) {

  metronom_t *this = xmalloc (sizeof (metronom_t));
  int         err;

  this->audio_stream_start   = metronom_audio_stream_start;
  this->audio_stream_end     = metronom_audio_stream_end  ;
  this->video_stream_start   = metronom_video_stream_start;
  this->video_stream_end     = metronom_video_stream_end  ;
  this->set_video_rate       = metronom_set_video_rate;
  this->get_video_rate       = metronom_get_video_rate;
  this->set_audio_rate       = metronom_set_audio_rate;
  this->got_video_frame      = metronom_got_video_frame;
  this->got_audio_samples    = metronom_got_audio_samples;
  this->got_spu_packet       = metronom_got_spu_packet;
  this->set_av_offset        = metronom_set_av_offset;
  this->get_av_offset        = metronom_get_av_offset;
  this->expect_video_discontinuity = metronom_expect_video_discontinuity;
  this->expect_audio_discontinuity = metronom_expect_audio_discontinuity;
  this->start_clock          = metronom_start_clock;
  this->stop_clock           = metronom_stop_clock;
  this->resume_clock         = metronom_resume_clock;
  this->get_current_time     = metronom_get_current_time;
  this->adjust_clock         = metronom_adjust_clock;
  this->register_scr         = metronom_register_scr;
  this->unregister_scr       = metronom_unregister_scr;
  this->set_speed            = metronom_set_speed;
  this->got_audio_still      = metronom_got_audio_still;

  this->scr_list = calloc(MAX_SCR_PROVIDERS, sizeof(void*));
  this->register_scr(this, unixscr_init());

  if ((err = pthread_create(&this->sync_thread, NULL,
      			    (void*(*)(void*)) metronom_sync_loop, this)) != 0)
    fprintf(stderr, "metronom: cannot create sync thread (%s)\n",
     strerror(err));

  pthread_mutex_init (&this->lock, NULL);
  pthread_cond_init (&this->video_started, NULL);
  pthread_cond_init (&this->audio_started, NULL);
  pthread_cond_init (&this->video_ended, NULL);
  pthread_cond_init (&this->audio_ended, NULL);
    
  this->av_offset   = 0;
  this->have_audio  = have_audio;

  return this;
}