-//if(old) diff = (9 * diff + (foo-old))/10;
- /* FIXME : take into account SDL latency instead of mdate() */
- p_buffer = aout_OutputNextBuffer( p_aout, mdate(), 0, VLC_TRUE );
- //p_buffer = aout_OutputNextBuffer( p_aout, foo - diff, 0, VLC_TRUE );
-//fprintf(stderr, "foo - old : %lli, diff : %lli\n", foo-old, diff);
-//old=foo;
+
+ /* We try to stay around call_time + buffer_time/2. This is kludgy but
+ * unavoidable because SDL is completely unable to 1. tell us about its
+ * latency, and 2. call SDLCallback at regular intervals. */
+ if( mdate() < p_sys->call_time + p_sys->buffer_time / 2 )
+ {
+ /* We can't wait too much, because SDL will be lost, and we can't
+ * wait too little, because we are not sure that there will be
+ * samples in the queue. */
+ mwait( p_sys->call_time + p_sys->buffer_time / 4 );
+ p_sys->call_time += p_sys->buffer_time;
+ }
+ else
+ {
+ p_sys->call_time = mdate() + p_sys->buffer_time / 4;
+ }
+
+ /* Tell the output we're playing samples at call_time + 2*buffer_time */
+ p_buffer = aout_OutputNextBuffer( p_aout, p_sys->call_time
+ + 2 * p_sys->buffer_time, VLC_TRUE );