]> git.sesse.net Git - vlc/blobdiff - src/input/decoder.c
Allows audio output users to control vout creation.
[vlc] / src / input / decoder.c
index 03cfa9682f87fba14981206fa3c4340741c2c520..d9a12618d102a453e4ff0254b2304f461d663530 100644 (file)
@@ -46,8 +46,9 @@
 #include "input_internal.h"
 #include "clock.h"
 #include "decoder.h"
+#include "event.h"
 
-#include "../video_output/vout_internal.h"
+#include "../video_output/vout_control.h"
 
 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
 static void       DeleteDecoder( decoder_t * );
@@ -118,6 +119,7 @@ struct decoder_owner_sys_t
     struct
     {
         mtime_t i_date;
+        int     i_ignore;
     } pause;
 
     /* Buffering */
@@ -136,6 +138,9 @@ struct decoder_owner_sys_t
 
         aout_buffer_t *p_audio;
         aout_buffer_t **pp_audio_next;
+
+        block_t       *p_block;
+        block_t       **pp_block_next;
     } buffer;
 
     /* Flushing */
@@ -157,54 +162,88 @@ struct decoder_owner_sys_t
 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
 
+/* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
+ * a bogus PTS and won't be displayed */
+#define DECODER_BOGUS_VIDEO_DELAY                ((mtime_t)(DEFAULT_PTS_DELAY * 30))
+
+/* */
+#define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
+
+
 /*****************************************************************************
  * Public functions
  *****************************************************************************/
+picture_t *decoder_NewPicture( decoder_t *p_decoder )
+{
+    picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
+    if( !p_picture )
+        msg_Warn( p_decoder, "can't get output picture" );
+    return p_picture;
+}
+void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
+{
+    p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
+}
+void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
+{
+    p_decoder->pf_picture_link( p_decoder, p_picture );
+}
+void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
+{
+    p_decoder->pf_picture_unlink( p_decoder, p_picture );
+}
 
-/* decoder_GetInputAttachment:
- */
-input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
-                                                const char *psz_name )
+aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
 {
-    input_attachment_t *p_attachment;
-    if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
+    if( !p_decoder->pf_aout_buffer_new )
         return NULL;
-    return p_attachment;
+    return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
+}
+void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
+{
+    p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
 }
+
+subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
+{
+    subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
+    if( !p_subpicture )
+        msg_Warn( p_decoder, "can't get output subpicture" );
+    return p_subpicture;
+}
+void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
+{
+    p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
+}
+
 /* decoder_GetInputAttachments:
  */
 int decoder_GetInputAttachments( decoder_t *p_dec,
                                  input_attachment_t ***ppp_attachment,
                                  int *pi_attachment )
 {
-    return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
-                          ppp_attachment, pi_attachment );
+    if( !p_dec->pf_get_attachments )
+        return VLC_EGENERIC;
+
+    return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
 }
 /* decoder_GetDisplayDate:
  */
 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
 {
-    decoder_owner_sys_t *p_owner = p_dec->p_owner;
-
-    vlc_mutex_lock( &p_owner->lock );
-    if( p_owner->b_buffering )
-        i_ts = 0;
-    vlc_mutex_unlock( &p_owner->lock );
-
-    if( !p_owner->p_clock || !i_ts )
-        return i_ts;
+    if( !p_dec->pf_get_display_date )
+        return 0;
 
-    return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
+    return p_dec->pf_get_display_date( p_dec, i_ts );
 }
 /* decoder_GetDisplayRate:
  */
 int decoder_GetDisplayRate( decoder_t *p_dec )
 {
-    decoder_owner_sys_t *p_owner = p_dec->p_owner;
-
-    if( !p_owner->p_clock )
+    if( !p_dec->pf_get_display_rate )
         return INPUT_RATE_DEFAULT;
-    return input_clock_GetRate( p_owner->p_clock );
+
+    return p_dec->pf_get_display_rate( p_dec );
 }
 
 /**
@@ -220,9 +259,7 @@ decoder_t *input_DecoderNew( input_thread_t *p_input,
     decoder_t *p_dec = NULL;
     int i_priority;
 
-#ifndef ENABLE_SOUT
-    (void)b_force_decoder;
-#else
+#ifdef ENABLE_SOUT
     /* If we are in sout mode, search for packetizer module */
     if( p_sout )
     {
@@ -303,13 +340,8 @@ void input_DecoderDelete( decoder_t *p_dec )
     }
     vlc_mutex_unlock( &p_owner->lock );
 
-    /* Make sure the thread leaves the function */
-    block_FifoWake( p_owner->p_fifo );
-
     vlc_thread_join( p_dec );
-
-    /* Don't module_unneed() here because of the dll loader that wants
-     * close() in the same thread than open()/decode() */
+    module_unneed( p_dec, p_dec->p_module );
 
     /* */
     if( p_dec->p_owner->cc.b_supported )
@@ -328,6 +360,7 @@ void input_DecoderDelete( decoder_t *p_dec )
 
 /**
  * Put a block_t in the decoder's fifo.
+ * Thread-safe w.r.t. the decoder. May be a cancellation point.
  *
  * \param p_dec the decoder object
  * \param p_block the data block
@@ -338,17 +371,17 @@ void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
 
     if( p_owner->p_input->p->b_out_pace_control )
     {
-        /* FIXME !!!!! */
-        while( vlc_object_alive( p_dec ) && !p_dec->b_error &&
-               block_FifoCount( p_owner->p_fifo ) > 10 )
-        {
-            msleep( 1000 );
-        }
+        /* The fifo is not consummed when buffering and so will
+         * deadlock vlc.
+         * There is no need to lock as b_buffering is never modify
+         * inside decoder thread. */
+        if( !p_owner->b_buffering )
+            block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
     }
     else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
     {
         /* FIXME: ideally we would check the time amount of data
-         * in the fifo instead of its size. */
+         * in the FIFO instead of its size. */
         msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
                   "consumed quickly enough), resetting fifo!" );
         block_FifoEmpty( p_owner->p_fifo );
@@ -460,6 +493,7 @@ void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
 
     p_owner->b_paused = b_paused;
     p_owner->pause.i_date = i_date;
+    p_owner->pause.i_ignore = 0;
     vlc_cond_signal( &p_owner->wait );
 
     DecoderOutputChangePause( p_dec, b_paused, i_date );
@@ -488,7 +522,8 @@ void input_DecoderStartBuffering( decoder_t *p_dec )
     p_owner->buffer.b_full = false;
     p_owner->buffer.i_count = 0;
 
-    assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic && !p_owner->buffer.p_audio );
+    assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
+            !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
 
     p_owner->buffer.p_picture = NULL;
     p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
@@ -499,6 +534,10 @@ void input_DecoderStartBuffering( decoder_t *p_dec )
     p_owner->buffer.p_audio = NULL;
     p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
 
+    p_owner->buffer.p_block = NULL;
+    p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
+
+
     p_owner->b_buffering = true;
 
     vlc_cond_signal( &p_owner->wait );
@@ -533,10 +572,62 @@ void input_DecoderWaitBuffering( decoder_t *p_dec )
 
     vlc_mutex_unlock( &p_owner->lock );
 }
+void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    *pi_duration = 0;
+
+    vlc_mutex_lock( &p_owner->lock );
+    if( p_dec->fmt_in.i_cat == VIDEO_ES )
+    {
+        if( p_owner->b_paused && p_owner->p_vout )
+        {
+            vout_NextPicture( p_owner->p_vout, pi_duration );
+            p_owner->pause.i_ignore++;
+            vlc_cond_signal( &p_owner->wait );
+        }
+    }
+    else
+    {
+        /* TODO subtitle should not be flushed */
+        DecoderFlush( p_dec );
+    }
+    vlc_mutex_unlock( &p_owner->lock );
+}
 
 /*****************************************************************************
  * Internal functions
  *****************************************************************************/
+static int DecoderGetInputAttachments( decoder_t *p_dec,
+                                       input_attachment_t ***ppp_attachment,
+                                       int *pi_attachment )
+{
+    return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
+                          ppp_attachment, pi_attachment );
+}
+static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_mutex_lock( &p_owner->lock );
+    if( p_owner->b_buffering || p_owner->b_paused )
+        i_ts = 0;
+    vlc_mutex_unlock( &p_owner->lock );
+
+    if( !p_owner->p_clock || !i_ts )
+        return i_ts;
+
+    return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
+}
+static int DecoderGetDisplayRate( decoder_t *p_dec )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    if( !p_owner->p_clock )
+        return INPUT_RATE_DEFAULT;
+    return input_clock_GetRate( p_owner->p_clock );
+}
 
 /* */
 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
@@ -619,6 +710,10 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
     p_dec->pf_picture_unlink  = vout_unlink_picture;
     p_dec->pf_spu_buffer_new  = spu_new_buffer;
     p_dec->pf_spu_buffer_del  = spu_del_buffer;
+    /* */
+    p_dec->pf_get_attachments  = DecoderGetInputAttachments;
+    p_dec->pf_get_display_date = DecoderGetDisplayDate;
+    p_dec->pf_get_display_rate = DecoderGetDisplayRate;
 
     vlc_object_attach( p_dec, p_input );
 
@@ -681,6 +776,7 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
 
     p_owner->b_paused = false;
     p_owner->pause.i_date = 0;
+    p_owner->pause.i_ignore = 0;
 
     p_owner->b_buffering = false;
     p_owner->buffer.b_first = true;
@@ -689,6 +785,7 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
     p_owner->buffer.p_picture = NULL;
     p_owner->buffer.p_subpic = NULL;
     p_owner->buffer.p_audio = NULL;
+    p_owner->buffer.p_block = NULL;
 
     p_owner->b_flushing = false;
 
@@ -721,35 +818,24 @@ static void *DecoderThread( vlc_object_t *p_this )
     decoder_t *p_dec = (decoder_t *)p_this;
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
 
-    int canc = vlc_savecancel();
-
     /* The decoder's main loop */
-    while( vlc_object_alive( p_dec ) && !p_dec->b_error )
-    {
-        block_t *p_block = block_FifoGet( p_owner->p_fifo );
-
-        DecoderSignalBuffering( p_dec, p_block == NULL );
-
-        if( p_block && DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
-            break;
-    }
-
-    while( vlc_object_alive( p_dec ) )
+    for( ;; )
     {
         block_t *p_block = block_FifoGet( p_owner->p_fifo );
-
+        /* Make sure there is no cancellation point other than this one^^.
+         * If you need one, be sure to push cleanup of p_block. */
         DecoderSignalBuffering( p_dec, p_block == NULL );
 
-        /* Trash all received PES packets */
         if( p_block )
-            block_Release( p_block );
+        {
+            if( p_dec->b_error )
+                block_Release( p_block );
+            else
+                DecoderProcess( p_dec, p_block );
+        }
     }
-    DecoderSignalBuffering( p_dec, true );
 
-    /* We do it here because of the dll loader that wants close() in the
-     * same thread than open()/decode() */
-    module_unneed( p_dec, p_dec->p_module );
-    vlc_restorecancel( canc );
+    DecoderSignalBuffering( p_dec, true );
     return NULL;
 }
 
@@ -837,12 +923,21 @@ static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
 
     while( !p_owner->b_flushing )
     {
-        if( p_owner->b_paused && p_owner->b_buffering && !p_owner->buffer.b_full )
-            break;
-
-        if( !p_owner->b_paused && ( !p_owner->b_buffering || !p_owner->buffer.b_full ) )
-            break;
-
+        if( p_owner->b_paused )
+        {
+            if( p_owner->b_buffering && !p_owner->buffer.b_full )
+                break;
+            if( p_owner->pause.i_ignore > 0 )
+            {
+                p_owner->pause.i_ignore--;
+                break;
+            }
+        }
+        else
+        {
+            if( !p_owner->b_buffering || !p_owner->buffer.b_full )
+                break;
+        }
         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
     }
 
@@ -1166,6 +1261,7 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
     vout_thread_t  *p_vout = p_owner->p_vout;
+    bool b_first_buffered;
 
     if( p_picture->date <= 0 )
     {
@@ -1193,12 +1289,14 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
             vlc_cond_signal( &p_owner->wait );
         }
     }
+    b_first_buffered = p_owner->buffer.p_picture != NULL;
 
-    for( ;; )
+    for( ;; b_first_buffered = false )
     {
         bool b_has_more = false;
 
         bool b_reject;
+
         DecoderWaitUnblock( p_dec, &b_reject );
 
         if( p_owner->b_buffering && !p_owner->buffer.b_first )
@@ -1231,37 +1329,30 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
             assert( !p_owner->buffer.i_count );
             msg_Dbg( p_dec, "Received first picture" );
             p_owner->buffer.b_first = false;
-            p_picture->date = mdate();
             p_picture->b_force = true;
             i_delay = 0;
             if( p_owner->p_clock )
                 i_rate = input_clock_GetRate( p_owner->p_clock );
         }
-        else
-        {
-            DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
-                          &i_rate, &i_delay, false );
-        }
+        DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
+                      &i_rate, &i_delay, false );
 
         vlc_mutex_unlock( &p_owner->lock );
 
         /* */
-        const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
+        const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
 
-        if( p_picture->date <= 0 || p_picture->date >= i_max_date )
+        if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
             b_reject = true;
 
         if( !b_reject )
         {
-            if( i_rate != p_owner->i_last_rate  )
+            if( i_rate != p_owner->i_last_rate || b_first_buffered )
             {
                 /* Be sure to not display old picture after our own */
                 vout_Flush( p_vout, p_picture->date );
                 p_owner->i_last_rate = i_rate;
             }
-
-            vout_DatePicture( p_vout, p_picture, p_picture->date );
-
             vout_DisplayPicture( p_vout, p_picture );
         }
         else
@@ -1443,18 +1534,69 @@ static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
 
     assert( p_owner->p_clock );
+    assert( !p_sout_block->p_next );
 
     vlc_mutex_lock( &p_owner->lock );
 
-    bool b_reject;
-    DecoderWaitUnblock( p_dec, &b_reject );
+    if( p_owner->b_buffering || p_owner->buffer.p_block )
+    {
+        block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
+
+        p_owner->buffer.i_count++;
+        /* XXX it is important to be full after the first one */
+        if( p_owner->buffer.i_count > 0 )
+        {
+            p_owner->buffer.b_full = true;
+            vlc_cond_signal( &p_owner->wait );
+        }
+    }
+
+    for( ;; )
+    {
+        bool b_has_more = false;
+        bool b_reject;
+        DecoderWaitUnblock( p_dec, &b_reject );
 
-    DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts, &p_sout_block->i_length,
-                  &p_sout_block->i_rate, NULL, b_telx );
+        if( p_owner->b_buffering )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            return;
+        }
 
-    vlc_mutex_unlock( &p_owner->lock );
+        /* */
+        if( p_owner->buffer.p_block )
+        {
+            p_sout_block = p_owner->buffer.p_block;
+
+            p_owner->buffer.p_block = p_sout_block->p_next;
+            p_owner->buffer.i_count--;
+
+            b_has_more = p_owner->buffer.p_block != NULL;
+            if( !b_has_more )
+                p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
+        }
+        p_sout_block->p_next = NULL;
+
+        DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
+                      &p_sout_block->i_length,
+                      &p_sout_block->i_rate, NULL, b_telx );
+
+        vlc_mutex_unlock( &p_owner->lock );
+
+        if( !b_reject )
+            sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
+        else
+            block_Release( p_sout_block );
 
-    sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
+        if( !b_has_more )
+            break;
+        vlc_mutex_lock( &p_owner->lock );
+        if( !p_owner->buffer.p_block )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            break;
+        }
+    }
 }
 
 /* */
@@ -1501,6 +1643,14 @@ static void DecoderFlushBuffering( decoder_t *p_dec )
         if( !p_owner->buffer.p_subpic )
             p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
     }
+    if( p_owner->buffer.p_block )
+    {
+        block_ChainRelease( p_owner->buffer.p_block );
+
+        p_owner->buffer.i_count = 0;
+        p_owner->buffer.p_block = NULL;
+        p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
+    }
 }
 
 /* This function process a block for sout
@@ -1729,6 +1879,7 @@ static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
         return VLC_SUCCESS;
     }
 
+    int canc = vlc_savecancel();
 #ifdef ENABLE_SOUT
     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
     {
@@ -1780,6 +1931,7 @@ static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
 
         DecoderSignalFlushed( p_dec );
     }
+    vlc_restorecancel( canc );
 
     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
 }
@@ -1822,7 +1974,7 @@ static void DeleteDecoder( decoder_t * p_dec )
 
         /* We are about to die. Reattach video output to p_vlc. */
         vout_Request( p_dec, p_owner->p_vout, NULL );
-        var_SetBool( p_owner->p_input, "intf-change-vout", true );
+        input_SendEventVout( p_owner->p_input );
     }
 
 #ifdef ENABLE_SOUT
@@ -1870,6 +2022,17 @@ static void DeleteDecoder( decoder_t * p_dec )
 /*****************************************************************************
  * Buffers allocation callbacks for the decoders
  *****************************************************************************/
+static vout_thread_t *aout_request_vout( void *p_private,
+                                         vout_thread_t *p_vout, video_format_t *p_fmt )
+{
+    decoder_t *p_dec = p_private;
+
+    p_vout =  vout_Request( p_dec, p_vout, p_fmt );
+    input_SendEventVout( p_dec->p_owner->p_input );
+
+    return p_vout;
+}
+
 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
@@ -1901,15 +2064,17 @@ static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
         audio_sample_format_t format;
         aout_input_t *p_aout_input;
         aout_instance_t *p_aout;
+        aout_request_vout_t request_vout;
 
         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
         p_owner->audio = p_dec->fmt_out.audio;
 
         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
-        if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
-                                    == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
+        if( i_force_dolby &&
+            (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
+                (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
         {
-            if ( i_force_dolby == 1 )
+            if( i_force_dolby == 1 )
             {
                 format.i_original_channels = format.i_original_channels |
                                              AOUT_CHAN_DOLBYSTEREO;
@@ -1921,9 +2086,12 @@ static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
             }
         }
 
+        request_vout.pf_request_vout = aout_request_vout;
+        request_vout.p_private = p_dec;
+
         p_aout = p_owner->p_aout;
         p_aout_input = aout_DecNew( p_dec, &p_aout,
-                                    &format, &p_dec->fmt_out.audio_replay_gain );
+                                    &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
 
         vlc_mutex_lock( &p_owner->lock );
         p_owner->p_aout = p_aout;
@@ -2036,7 +2204,7 @@ static picture_t *vout_new_buffer( decoder_t *p_dec )
         p_owner->p_vout = p_vout;
         vlc_mutex_unlock( &p_owner->lock );
 
-        var_SetBool( p_owner->p_input, "intf-change-vout", true );
+        input_SendEventVout( p_owner->p_input );
         if( p_vout == NULL )
         {
             msg_Err( p_dec, "failed to create video output" );
@@ -2082,6 +2250,7 @@ static picture_t *vout_new_buffer( decoder_t *p_dec )
         /* Check the decoder doesn't leak pictures */
         vout_FixLeaks( p_owner->p_vout, false );
 
+        /* FIXME add a vout_WaitPictureAvailable (timedwait) */
         msleep( VOUT_OUTMEM_SLEEP );
     }
 }
@@ -2117,7 +2286,7 @@ static subpicture_t *spu_new_buffer( decoder_t *p_dec )
         if( p_vout )
             break;
 
-        msleep( VOUT_DISPLAY_DELAY );
+        msleep( DECODER_SPU_VOUT_WAIT_DURATION );
     }
 
     if( !p_vout )