]> git.sesse.net Git - vlc/blob - src/audio_output/output.c
aout_OutputNextBuffer: do not dequeue when paused
[vlc] / src / audio_output / output.c
1 /*****************************************************************************
2  * output.c : internal management of output streams for the audio output
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <math.h>
32
33 #include <assert.h>
34 #include <vlc_common.h>
35 #include <vlc_aout.h>
36 #include <vlc_aout_intf.h>
37 #include <vlc_cpu.h>
38 #include <vlc_modules.h>
39
40 #include "libvlc.h"
41 #include "aout_internal.h"
42
43 /*****************************************************************************
44  * aout_OutputNew : allocate a new output and rework the filter pipeline
45  *****************************************************************************
46  * This function is entered with the mixer lock.
47  *****************************************************************************/
48 int aout_OutputNew( audio_output_t *p_aout,
49                     const audio_sample_format_t * p_format )
50 {
51     aout_owner_t *owner = aout_owner (p_aout);
52
53     aout_assert_locked( p_aout );
54     p_aout->format = *p_format;
55
56     /* Retrieve user defaults. */
57     int i_rate = var_InheritInteger( p_aout, "aout-rate" );
58     if ( i_rate != 0 )
59         p_aout->format.i_rate = i_rate;
60     aout_FormatPrepare( &p_aout->format );
61
62     /* Find the best output plug-in. */
63     owner->module = module_need (p_aout, "audio output", "$aout", false);
64     if (owner->module == NULL)
65     {
66         msg_Err( p_aout, "no suitable audio output module" );
67         return -1;
68     }
69
70     if ( var_Type( p_aout, "audio-channels" ) ==
71              (VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
72     {
73         /* The user may have selected a different channels configuration. */
74         switch( var_InheritInteger( p_aout, "audio-channels" ) )
75         {
76             case AOUT_VAR_CHAN_RSTEREO:
77                 p_aout->format.i_original_channels |= AOUT_CHAN_REVERSESTEREO;
78                 break;
79             case AOUT_VAR_CHAN_STEREO:
80                 p_aout->format.i_original_channels =
81                                               AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
82                 break;
83             case AOUT_VAR_CHAN_LEFT:
84                 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
85                 break;
86             case AOUT_VAR_CHAN_RIGHT:
87                 p_aout->format.i_original_channels = AOUT_CHAN_RIGHT;
88                 break;
89             case AOUT_VAR_CHAN_DOLBYS:
90                 p_aout->format.i_original_channels =
91                       AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
92                 break;
93         }
94     }
95     else if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER
96               && (p_aout->format.i_original_channels
97                    & AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
98     {
99         vlc_value_t val, text;
100
101         /* Mono - create the audio-channels variable. */
102         var_Create( p_aout, "audio-channels",
103                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
104         text.psz_string = _("Audio Channels");
105         var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
106
107         val.i_int = AOUT_VAR_CHAN_STEREO; text.psz_string = _("Stereo");
108         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
109         val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
110         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
111         val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
112         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
113         if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
114         {
115             /* Go directly to the left channel. */
116             p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
117             var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
118         }
119         var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
120                          NULL );
121     }
122     else if ( p_aout->format.i_physical_channels ==
123                (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
124                 && (p_aout->format.i_original_channels &
125                      (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
126     {
127         vlc_value_t val, text;
128
129         /* Stereo - create the audio-channels variable. */
130         var_Create( p_aout, "audio-channels",
131                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
132         text.psz_string = _("Audio Channels");
133         var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
134
135         if ( p_aout->format.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
136         {
137             val.i_int = AOUT_VAR_CHAN_DOLBYS;
138             text.psz_string = _("Dolby Surround");
139         }
140         else
141         {
142             val.i_int = AOUT_VAR_CHAN_STEREO;
143             text.psz_string = _("Stereo");
144         }
145         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
146         val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
147         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
148         val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
149         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
150         val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
151         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
152         if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
153         {
154             /* Go directly to the left channel. */
155             p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
156             var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
157         }
158         var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
159                          NULL );
160     }
161     var_TriggerCallback( p_aout, "intf-change" );
162
163     aout_FormatPrepare( &p_aout->format );
164     aout_FormatPrint( p_aout, "output", &p_aout->format );
165
166     /* Choose the mixer format. */
167     owner->mixer_format = p_aout->format;
168     if (AOUT_FMT_NON_LINEAR(&p_aout->format))
169         owner->mixer_format.i_format = p_format->i_format;
170     else
171     /* Most audio filters can only deal with single-precision,
172      * so lets always use that when hardware supports floating point. */
173     if( HAVE_FPU )
174         owner->mixer_format.i_format = VLC_CODEC_FL32;
175     else
176     /* Otherwise, audio filters will not work. Use fixed-point if the input has
177      * more than 16-bits depth. */
178     if( p_format->i_bitspersample > 16 )
179         owner->mixer_format.i_format = VLC_CODEC_FI32;
180     else
181     /* Fallback to 16-bits. This avoids pointless conversion to and from
182      * 32-bits samples for the sole purpose of software mixing. */
183         owner->mixer_format.i_format = VLC_CODEC_S16N;
184
185     aout_FormatPrepare (&owner->mixer_format);
186     aout_FormatPrint (p_aout, "mixer", &owner->mixer_format);
187
188     /* Create filters. */
189     owner->nb_filters = 0;
190     if (aout_FiltersCreatePipeline (p_aout, owner->filters,
191                                     &owner->nb_filters, &owner->mixer_format,
192                                     &p_aout->format) < 0)
193     {
194         msg_Err( p_aout, "couldn't create audio output pipeline" );
195         module_unneed (p_aout, owner->module);
196         owner->module = NULL;
197         return -1;
198     }
199     return 0;
200 }
201
202 /*****************************************************************************
203  * aout_OutputDelete : delete the output
204  *****************************************************************************
205  * This function is entered with the mixer lock.
206  *****************************************************************************/
207 void aout_OutputDelete( audio_output_t * p_aout )
208 {
209     aout_owner_t *owner = aout_owner (p_aout);
210
211     aout_assert_locked( p_aout );
212
213     if (owner->module == NULL)
214         return;
215
216     module_unneed (p_aout, owner->module);
217     aout_VolumeNoneInit( p_aout ); /* clear volume callback */
218     owner->module = NULL;
219     aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
220 }
221
222 /*****************************************************************************
223  * aout_OutputPlay : play a buffer
224  *****************************************************************************
225  * This function is entered with the mixer lock.
226  *****************************************************************************/
227 void aout_OutputPlay (audio_output_t *aout, block_t *block)
228 {
229     aout_owner_t *owner = aout_owner (aout);
230
231     aout_assert_locked (aout);
232
233     aout_FiltersPlay (owner->filters, owner->nb_filters, &block);
234     if (block == NULL)
235         return;
236     if (block->i_buffer == 0)
237     {
238         block_Release (block);
239         return;
240     }
241
242     aout->pf_play (aout, block);
243 }
244
245 /**
246  * Notifies the audio output (if any) of pause/resume events.
247  * This enables the output to expedite pause, instead of waiting for its
248  * buffers to drain.
249  */
250 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
251 {
252     aout_assert_locked( aout );
253     if( aout->pf_pause != NULL )
254         aout->pf_pause( aout, pause, date );
255 }
256
257 /**
258  * Flushes or drains the audio output buffers.
259  * This enables the output to expedite seek and stop.
260  * @param wait if true, wait for buffer playback (i.e. drain),
261  *             if false, discard the buffers immediately (i.e. flush)
262  */
263 void aout_OutputFlush( audio_output_t *aout, bool wait )
264 {
265     aout_assert_locked( aout );
266
267     if( aout->pf_flush != NULL )
268         aout->pf_flush( aout, wait );
269 }
270
271
272 /*** Volume handling ***/
273
274 /**
275  * Dummy volume setter. This is the default volume setter.
276  */
277 static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
278 {
279     (void)aout; (void)volume; (void)mute;
280     return -1;
281 }
282
283 /**
284  * Configures the dummy volume setter.
285  * @note Audio output plugins for which volume is irrelevant
286  * should call this function during activation.
287  */
288 void aout_VolumeNoneInit (audio_output_t *aout)
289 {
290     /* aout_New() -safely- calls this function without the lock, before any
291      * other thread knows of this audio output instance.
292     aout_assert_locked (aout); */
293     aout->pf_volume_set = aout_VolumeNoneSet;
294     var_Destroy (aout, "volume");
295     var_Destroy (aout, "mute");
296 }
297
298 /**
299  * Volume setter for software volume.
300  */
301 static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
302 {
303     aout_owner_t *owner = aout_owner (aout);
304
305     aout_assert_locked (aout);
306
307     /* Cubic mapping from software volume to amplification factor.
308      * This provides a good tradeoff between low and high volume ranges.
309      *
310      * This code is only used for the VLC software mixer. If you change this
311      * formula, be sure to update the aout_VolumeHardInit()-based plugins also.
312      */
313     if (!mute)
314         volume = volume * volume * volume;
315     else
316         volume = 0.;
317
318     owner->volume.multiplier = volume;
319     return 0;
320 }
321
322 /**
323  * Configures the volume setter for software mixing
324  * and apply the default volume.
325  * @note Audio output plugins that cannot apply the volume
326  * should call this function during activation.
327  */
328 void aout_VolumeSoftInit (audio_output_t *aout)
329 {
330     audio_volume_t volume = var_InheritInteger (aout, "volume");
331     bool mute = var_InheritBool (aout, "mute");
332
333     aout_assert_locked (aout);
334     aout->pf_volume_set = aout_VolumeSoftSet;
335     aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
336 }
337
338 /**
339  * Configures a custom volume setter. This is used by audio outputs that can
340  * control the hardware volume directly and/or emulate it internally.
341  * @param setter volume setter callback
342  */
343 void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
344 {
345     aout_assert_locked (aout);
346     aout->pf_volume_set = setter;
347     var_Create (aout, "volume", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
348     var_Create (aout, "mute", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
349 }
350
351 /**
352  * Supply or update the current custom ("hardware") volume.
353  * @note This only makes sense after calling aout_VolumeHardInit().
354  * @param setter volume setter callback
355  * @param volume current custom volume
356  * @param mute current mute flag
357  *
358  * @warning The caller (i.e. the audio output plug-in) is responsible for
359  * interlocking and synchronizing call to this function and to the
360  * audio_output_t.pf_volume_set callback. This ensures that VLC gets correct
361  * volume information (possibly with a latency).
362  */
363 void aout_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
364 {
365     audio_volume_t vol = lroundf (volume * (float)AOUT_VOLUME_DEFAULT);
366
367     /* We cannot acquire the volume lock as this gets called from the audio
368      * output plug-in (it would cause a lock inversion). */
369     var_SetInteger (aout, "volume", vol);
370     var_SetBool (aout, "mute", mute);
371     var_TriggerCallback (aout, "intf-change");
372 }
373
374
375 /*** Packet-oriented audio output support ***/
376
377 static inline aout_packet_t *aout_packet (audio_output_t *aout)
378 {
379     return (aout_packet_t *)(aout->sys);
380 }
381
382 void aout_PacketInit (audio_output_t *aout, aout_packet_t *p, unsigned samples)
383 {
384     assert (p == aout_packet (aout));
385
386     vlc_mutex_init (&p->lock);
387     aout_FifoInit (aout, &p->partial, aout->format.i_rate);
388     aout_FifoInit (aout, &p->fifo, aout->format.i_rate);
389     p->pause_date = VLC_TS_INVALID;
390     p->samples = samples;
391     p->starving = true;
392 }
393
394 void aout_PacketDestroy (audio_output_t *aout)
395 {
396     aout_packet_t *p = aout_packet (aout);
397
398     aout_FifoDestroy (&p->partial);
399     aout_FifoDestroy (&p->fifo);
400     vlc_mutex_destroy (&p->lock);
401 }
402
403 static block_t *aout_OutputSlice (audio_output_t *);
404
405 void aout_PacketPlay (audio_output_t *aout, block_t *block)
406 {
407     aout_packet_t *p = aout_packet (aout);
408
409     vlc_mutex_lock (&p->lock);
410     aout_FifoPush (&p->partial, block);
411     while ((block = aout_OutputSlice (aout)) != NULL)
412         aout_FifoPush (&p->fifo, block);
413     vlc_mutex_unlock (&p->lock);
414 }
415
416 void aout_PacketPause (audio_output_t *aout, bool pause, mtime_t date)
417 {
418     aout_packet_t *p = aout_packet (aout);
419
420     if (pause)
421     {
422         assert (p->pause_date == VLC_TS_INVALID);
423         p->pause_date = date;
424     }
425     else
426     {
427         assert (p->pause_date != VLC_TS_INVALID);
428
429         mtime_t duration = date - p->pause_date;
430
431         p->pause_date = VLC_TS_INVALID;
432         vlc_mutex_lock (&p->lock);
433         aout_FifoMoveDates (&p->partial, duration);
434         aout_FifoMoveDates (&p->fifo, duration);
435         vlc_mutex_unlock (&p->lock);
436     }
437 }
438
439 void aout_PacketFlush (audio_output_t *aout, bool drain)
440 {
441     aout_packet_t *p = aout_packet (aout);
442
443     vlc_mutex_lock (&p->lock);
444     aout_FifoReset (&p->partial);
445     aout_FifoReset (&p->fifo);
446     vlc_mutex_unlock (&p->lock);
447
448     (void) drain; /* TODO */
449 }
450
451
452 /**
453  * Rearranges audio blocks in correct number of samples.
454  * @note (FIXME) This is left here for historical reasons. It belongs in the
455  * output code. Besides, this operation should be avoided if possible.
456  */
457 static block_t *aout_OutputSlice (audio_output_t *p_aout)
458 {
459     aout_packet_t *p = aout_packet (p_aout);
460     aout_fifo_t *p_fifo = &p->partial;
461     const unsigned samples = p->samples;
462     assert( samples > 0 );
463
464     vlc_assert_locked( &p->lock );
465
466     /* Retrieve the date of the next buffer. */
467     date_t exact_start_date = p->fifo.end_date;
468     mtime_t start_date = date_Get( &exact_start_date );
469
470     /* See if we have enough data to prepare a new buffer for the audio output. */
471     aout_buffer_t *p_buffer = p_fifo->p_first;
472     if( p_buffer == NULL )
473         return NULL;
474
475     /* Find the earliest start date available. */
476     if ( start_date == VLC_TS_INVALID )
477     {
478         start_date = p_buffer->i_pts;
479         date_Set( &exact_start_date, start_date );
480     }
481     /* Compute the end date for the new buffer. */
482     mtime_t end_date = date_Increment( &exact_start_date, samples );
483
484     /* Check that start_date is available. */
485     mtime_t prev_date;
486     for( ;; )
487     {
488         /* Check for the continuity of start_date */
489         prev_date = p_buffer->i_pts + p_buffer->i_length;
490         if( prev_date >= start_date - 1 )
491             break;
492         /* We authorize a +-1 because rounding errors get compensated
493          * regularly. */
494         msg_Warn( p_aout, "got a packet in the past (%"PRId64")",
495                   start_date - prev_date );
496         aout_BufferFree( aout_FifoPop( p_fifo ) );
497
498         p_buffer = p_fifo->p_first;
499         if( p_buffer == NULL )
500             return NULL;
501     }
502
503     /* Check that we have enough samples. */
504     while( prev_date < end_date )
505     {
506         p_buffer = p_buffer->p_next;
507         if( p_buffer == NULL )
508             return NULL;
509
510         /* Check that all buffers are contiguous. */
511         if( prev_date != p_buffer->i_pts )
512         {
513             msg_Warn( p_aout,
514                       "buffer hole, dropping packets (%"PRId64")",
515                       p_buffer->i_pts - prev_date );
516
517             aout_buffer_t *p_deleted;
518             while( (p_deleted = p_fifo->p_first) != p_buffer )
519                 aout_BufferFree( aout_FifoPop( p_fifo ) );
520         }
521
522         prev_date = p_buffer->i_pts + p_buffer->i_length;
523     }
524
525     if( !AOUT_FMT_NON_LINEAR( &p_aout->format ) )
526     {
527         p_buffer = p_fifo->p_first;
528
529         /* Additionally check that p_first_byte_to_mix is well located. */
530         const unsigned framesize = p_aout->format.i_bytes_per_frame;
531         ssize_t delta = (start_date - p_buffer->i_pts)
532                       * p_aout->format.i_rate / CLOCK_FREQ;
533         if( delta != 0 )
534             msg_Warn( p_aout, "input start is not output end (%zd)", delta );
535         if( delta < 0 )
536         {
537             /* Is it really the best way to do it ? */
538             aout_FifoReset (&p->fifo);
539             return NULL;
540         }
541         if( delta > 0 )
542         {
543             mtime_t t = delta * CLOCK_FREQ / p_aout->format.i_rate;
544             p_buffer->i_nb_samples -= delta;
545             p_buffer->i_pts += t;
546             p_buffer->i_length -= t;
547             delta *= framesize;
548             p_buffer->p_buffer += delta;
549             p_buffer->i_buffer -= delta;
550         }
551
552         /* Build packet with adequate number of samples */
553         unsigned needed = samples * framesize;
554         p_buffer = block_Alloc( needed );
555         if( unlikely(p_buffer == NULL) )
556             /* XXX: should free input buffers */
557             return NULL;
558         p_buffer->i_nb_samples = samples;
559
560         for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
561         {
562             aout_buffer_t *p_inbuf = p_fifo->p_first;
563             if( unlikely(p_inbuf == NULL) )
564             {
565                 msg_Err( p_aout, "packetization error" );
566                 vlc_memset( p_out, 0, needed );
567                 break;
568             }
569
570             const uint8_t *p_in = p_inbuf->p_buffer;
571             size_t avail = p_inbuf->i_nb_samples * framesize;
572             if( avail > needed )
573             {
574                 vlc_memcpy( p_out, p_in, needed );
575                 p_fifo->p_first->p_buffer += needed;
576                 p_fifo->p_first->i_buffer -= needed;
577                 needed /= framesize;
578                 p_fifo->p_first->i_nb_samples -= needed;
579
580                 mtime_t t = needed * CLOCK_FREQ / p_aout->format.i_rate;
581                 p_fifo->p_first->i_pts += t;
582                 p_fifo->p_first->i_length -= t;
583                 break;
584             }
585
586             vlc_memcpy( p_out, p_in, avail );
587             needed -= avail;
588             p_out += avail;
589             /* Next buffer */
590             aout_BufferFree( aout_FifoPop( p_fifo ) );
591         }
592     }
593     else
594         p_buffer = aout_FifoPop( p_fifo );
595
596     p_buffer->i_pts = start_date;
597     p_buffer->i_length = end_date - start_date;
598
599     return p_buffer;
600 }
601
602 /*****************************************************************************
603  * aout_OutputNextBuffer : give the audio output plug-in the right buffer
604  *****************************************************************************
605  * If b_can_sleek is 1, the aout core functions won't try to resample
606  * new buffers to catch up - that is we suppose that the output plug-in can
607  * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
608  * This function is entered with no lock at all :-).
609  *****************************************************************************/
610 aout_buffer_t * aout_OutputNextBuffer( audio_output_t * p_aout,
611                                        mtime_t start_date,
612                                        bool b_can_sleek )
613 {
614     aout_packet_t *p = aout_packet (p_aout);
615     aout_fifo_t *p_fifo = &p->fifo;
616     aout_buffer_t *p_buffer = NULL;
617     mtime_t now = mdate();
618
619     vlc_mutex_lock( &p->lock );
620     if( p->pause_date != VLC_TS_INVALID )
621         goto out;
622
623     /* Drop the audio sample if the audio output is really late.
624      * In the case of b_can_sleek, we don't use a resampler so we need to be
625      * a lot more severe. */
626     while( ((p_buffer = p_fifo->p_first) != NULL)
627      && p_buffer->i_pts < (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY )
628     {
629         msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
630                  "trashing %"PRId64"us", now - p_buffer->i_pts,
631                  p_buffer->i_length );
632         aout_BufferFree( aout_FifoPop( p_fifo ) );
633     }
634
635     if( p_buffer == NULL )
636     {
637 #if 0 /* This is bad because the audio output might just be trying to fill
638        * in its internal buffers. And anyway, it's up to the audio output
639        * to deal with this kind of starvation. */
640
641         /* Set date to 0, to allow the mixer to send a new buffer ASAP */
642         aout_FifoReset( &p->fifo );
643         if ( !p->starving )
644             msg_Dbg( p_aout,
645                  "audio output is starving (no input), playing silence" );
646         p_aout->starving = true;
647 #endif
648         goto out;
649     }
650
651     mtime_t delta = start_date - p_buffer->i_pts;
652     /* Here we suppose that all buffers have the same duration - this is
653      * generally true, and anyway if it's wrong it won't be a disaster.
654      */
655     if ( 0 > delta + p_buffer->i_length )
656     {
657         if (!p->starving)
658             msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
659                      "playing silence", -delta );
660         p->starving = true;
661         p_buffer = NULL;
662         goto out;
663     }
664
665     p->starving = false;
666     p_buffer = aout_FifoPop( p_fifo );
667
668     if( !b_can_sleek
669      && ( delta > AOUT_MAX_PTS_DELAY || delta < -AOUT_MAX_PTS_ADVANCE ) )
670     {
671         /* Try to compensate the drift by doing some resampling. */
672         msg_Warn( p_aout, "output date isn't PTS date, requesting "
673                   "resampling (%"PRId64")", delta );
674
675         aout_FifoMoveDates (&p->partial, delta);
676         aout_FifoMoveDates (p_fifo, delta);
677 #warning FIXME: feed back to input for resampling!!!
678     }
679 out:
680     vlc_mutex_unlock( &p->lock );
681     return p_buffer;
682 }