Fixed aout visual vout recycling.
[vlc.git] / src / input / decoder.c
index 555e646..bcae5fe 100644 (file)
@@ -48,6 +48,7 @@
 #include "clock.h"
 #include "decoder.h"
 #include "event.h"
+#include "ressource.h"
 
 #include "../video_output/vout_control.h"
 
@@ -311,8 +312,7 @@ decoder_t *input_DecoderNew( input_thread_t *p_input,
         i_priority = VLC_THREAD_PRIORITY_VIDEO;
 
     /* Spawn the decoder thread */
-    if( vlc_thread_create( p_dec, "decoder", DecoderThread,
-                           i_priority, false ) )
+    if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
     {
         msg_Err( p_dec, "cannot spawn decoder thread" );
         module_unneed( p_dec, p_dec->p_module );
@@ -339,6 +339,7 @@ void input_DecoderDelete( decoder_t *p_dec )
 
     /* Make sure we aren't paused/buffering/waiting anymore */
     vlc_mutex_lock( &p_owner->lock );
+    const bool b_was_paused = p_owner->b_paused;
     p_owner->b_paused = false;
     p_owner->b_buffering = false;
     p_owner->b_flushing = true;
@@ -346,6 +347,8 @@ void input_DecoderDelete( decoder_t *p_dec )
     vlc_mutex_unlock( &p_owner->lock );
 
     vlc_thread_join( p_dec );
+    p_owner->b_paused = b_was_paused;
+
     module_unneed( p_dec, p_dec->p_module );
 
     /* */
@@ -652,7 +655,10 @@ static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
     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 );
+    if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
+        return 0;
+
+    return i_ts;
 }
 static int DecoderGetDisplayRate( decoder_t *p_dec )
 {
@@ -755,9 +761,9 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
 
     /* Find a suitable decoder/packetizer module */
     if( i_object_type == VLC_OBJECT_DECODER )
-        p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
+        p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
     else
-        p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
+        p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
 
     /* Check if decoder requires already packetized data */
     if( i_object_type == VLC_OBJECT_DECODER &&
@@ -777,7 +783,7 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
 
             p_dec->p_owner->p_packetizer->p_module =
                 module_need( p_dec->p_owner->p_packetizer,
-                             "packetizer", "$packetizer", 0 );
+                             "packetizer", "$packetizer", false );
 
             if( !p_dec->p_owner->p_packetizer->p_module )
             {
@@ -862,6 +868,7 @@ static void *DecoderThread( vlc_object_t *p_this )
     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 );
@@ -878,8 +885,6 @@ static void *DecoderThread( vlc_object_t *p_this )
             vlc_restorecancel( canc );
         }
     }
-
-    DecoderSignalBuffering( p_dec, true );
     return NULL;
 }
 
@@ -1021,7 +1026,7 @@ static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
 }
 
-static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
+static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
 {
     mtime_t current_date = mdate();
 
@@ -1030,41 +1035,42 @@ static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
     {
         /* ETSI EN 300 472 Annex A : do not take into account the PTS
          * for teletext streams. */
-        return current_date + 400000 + i_ts_delay;
+        return current_date + 400000;
     }
     return i_ts;
 }
 
 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
-                          mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
+                          mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
     input_clock_t   *p_clock = p_owner->p_clock;
-    int i_rate = 0;
 
     vlc_assert_locked( &p_owner->lock );
 
-    const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
     const mtime_t i_es_delay = p_owner->i_ts_delay;
 
     if( p_clock )
     {
         const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
+        int i_rate;
 
         if( *pi_ts0 > 0 )
-            *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
-                                         *pi_ts0 + i_es_delay );
-        if( pi_ts1 && *pi_ts1 > 0 )
-            *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
-                                         *pi_ts1 + i_es_delay );
+        {
+            *pi_ts0 += i_es_delay;
+            if( pi_ts1 && *pi_ts1 > 0 )
+                *pi_ts1 += i_es_delay;
+            input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound );
+        }
+        else
+        {
+            i_rate = input_clock_GetRate( p_clock );
+        }
 
         /* Do not create ephemere data because of rounding errors */
         if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
             *pi_ts1 += 1;
 
-        if( i_rate <= 0 )
-            i_rate = input_clock_GetRate( p_clock ); 
-
         if( pi_duration )
             *pi_duration = ( *pi_duration * i_rate +
                                     INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
@@ -1074,16 +1080,11 @@ static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
 
         if( b_telx )
         {
-            *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
+            *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
             if( pi_ts1 && *pi_ts1 <= 0 )
                 *pi_ts1 = *pi_ts0;
         }
     }
-    if( pi_delay )
-    {
-        const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
-        *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
-    }
 }
 
 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
@@ -1147,19 +1148,16 @@ static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
         }
 
         /* */
+        const bool b_dated = p_audio->start_date > 0;
         int i_rate = INPUT_RATE_DEFAULT;
-        mtime_t i_delay;
 
         DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
-                      &i_rate, &i_delay, false );
+                      &i_rate, AOUT_MAX_ADVANCE_TIME, false );
 
         vlc_mutex_unlock( &p_owner->lock );
 
-        /* */
-        const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
-
         if( !p_aout || !p_aout_input ||
-            p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
+            p_audio->start_date <= 0 ||
             i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
             i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
             b_reject = true;
@@ -1187,15 +1185,11 @@ static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
         }
         else
         {
-            if( p_audio->start_date <= 0 )
-            {
+            if( b_dated )
+                msg_Warn( p_aout, "received buffer in the future" );
+            else
                 msg_Warn( p_dec, "non-dated audio buffer received" );
-            }
-            else if( p_audio->start_date > i_max_date )
-            {
-                msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
-                          p_audio->start_date - mdate() );
-            }
+
             *pi_lost_sum += 1;
             aout_BufferFree( p_audio );
         }
@@ -1225,8 +1219,6 @@ static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
     {
         aout_instance_t *p_aout = p_owner->p_aout;
         aout_input_t    *p_aout_input = p_owner->p_aout_input;
-        int i_lost = 0;
-        int i_played;
 
         if( p_dec->b_die )
         {
@@ -1375,9 +1367,6 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
         }
 
         /* */
-        int i_rate = INPUT_RATE_DEFAULT;
-        mtime_t i_delay;
-
         if( b_buffering_first )
         {
             assert( p_owner->buffer.b_first );
@@ -1385,19 +1374,17 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
             msg_Dbg( p_dec, "Received first picture" );
             p_owner->buffer.b_first = false;
             p_picture->b_force = true;
-            i_delay = 0;
-            if( p_owner->p_clock )
-                i_rate = input_clock_GetRate( p_owner->p_clock );
         }
+
+        const bool b_dated = p_picture->date > 0;
+        int i_rate = INPUT_RATE_DEFAULT;
         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
-                      &i_rate, &i_delay, false );
+                      &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
 
         vlc_mutex_unlock( &p_owner->lock );
 
         /* */
-        const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
-
-        if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
+        if( !p_picture->b_force && p_picture->date <= 0 )
             b_reject = true;
 
         if( !b_reject )
@@ -1412,15 +1399,11 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
         }
         else
         {
-            if( p_picture->date <= 0 )
-            {
-                msg_Warn( p_vout, "non-dated video buffer received" );
-            }
+            if( b_dated )
+                msg_Warn( p_vout, "early picture skipped" );
             else
-            {
-                msg_Warn( p_vout, "early picture skipped (%"PRId64")",
-                          p_picture->date - mdate() );
-            }
+                msg_Warn( p_vout, "non-dated video buffer received" );
+
             *pi_lost_sum += 1;
             vout_DropPicture( p_vout, p_picture );
         }
@@ -1563,7 +1546,7 @@ static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
 
         /* */
         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
-                      NULL, NULL, b_telx );
+                      NULL, INT64_MAX, b_telx );
 
         vlc_mutex_unlock( &p_owner->lock );
 
@@ -1634,7 +1617,7 @@ static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
 
         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 );
+                      &p_sout_block->i_rate, INT64_MAX, b_telx );
 
         vlc_mutex_unlock( &p_owner->lock );
 
@@ -1879,7 +1862,7 @@ static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush
         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
 
-        p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+        p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
         if( p_vout && p_owner->p_spu_vout == p_vout )
         {
             /* Preroll does not work very well with subtitle */
@@ -1904,7 +1887,7 @@ static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush
 
     if( b_flush && p_owner->p_spu_vout )
     {
-        p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+        p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
 
         if( p_vout && p_owner->p_spu_vout == p_vout )
             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
@@ -1946,7 +1929,7 @@ static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
     }
 
 #ifdef ENABLE_SOUT
-    if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
+    if( vlc_internals( p_dec )->i_object_type == VLC_OBJECT_PACKETIZER )
     {
         if( p_block )
             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
@@ -2033,16 +2016,21 @@ static void DeleteDecoder( decoder_t * p_dec )
         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
     if( p_owner->p_aout )
     {
-        vlc_object_release( p_owner->p_aout );
+        input_ressource_RequestAout( p_owner->p_input->p->p_ressource,
+                                     p_owner->p_aout );
+        input_SendEventAout( p_owner->p_input );
         p_owner->p_aout = NULL;
     }
     if( p_owner->p_vout )
     {
-        /* Hack to make sure all the the pictures are freed by the decoder */
+        /* Hack to make sure all the the pictures are freed by the decoder
+         * and that the vout is not paused anymore */
         vout_FixLeaks( p_owner->p_vout, true );
+        if( p_owner->b_paused )
+            vout_ChangePause( p_owner->p_vout, false, mdate() );
 
-        /* We are about to die. Reattach video output to p_vlc. */
-        vout_Request( p_dec, p_owner->p_vout, NULL );
+        /* */
+        input_ressource_RequestVout( p_owner->p_input->p->p_ressource, p_owner->p_vout, NULL );
         input_SendEventVout( p_owner->p_input );
     }
 
@@ -2058,7 +2046,7 @@ static void DeleteDecoder( decoder_t * p_dec )
     {
         vout_thread_t *p_vout;
 
-        p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+        p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
         if( p_vout )
         {
             if( p_owner->p_spu_vout == p_vout )
@@ -2117,12 +2105,17 @@ static void DecoderUpdateFormatLocked( decoder_t *p_dec )
     p_dec->p_description = NULL;
 }
 static vout_thread_t *aout_request_vout( void *p_private,
-                                         vout_thread_t *p_vout, video_format_t *p_fmt )
+                                         vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
 {
     decoder_t *p_dec = p_private;
+    input_thread_t *p_input = p_dec->p_owner->p_input;
 
-    p_vout =  vout_Request( p_dec, p_vout, p_fmt );
-    input_SendEventVout( p_dec->p_owner->p_input );
+    p_vout = input_ressource_RequestVout( p_input->p->p_ressource, p_vout, p_fmt );
+    /* TODO it would be better to give b_recyle to input_ressource_RequestVout
+     * as here we are not sure of which vout we destroy */
+    if( !b_recyle )
+        input_ressource_TerminateVout( p_input->p->p_ressource );
+    input_SendEventVout( p_input );
 
     return p_vout;
 }
@@ -2184,6 +2177,8 @@ static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
         request_vout.p_private = p_dec;
 
         p_aout = p_owner->p_aout;
+        if( !p_aout )
+            p_aout = input_ressource_RequestAout( p_owner->p_input->p->p_ressource, NULL );
         p_aout_input = aout_DecNew( p_dec, &p_aout,
                                     &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
 
@@ -2195,6 +2190,8 @@ static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
 
         vlc_mutex_unlock( &p_owner->lock );
 
+        input_SendEventAout( p_owner->p_input );
+
         if( p_owner->p_aout_input == NULL )
         {
             msg_Err( p_dec, "failed to create audio output" );
@@ -2295,7 +2292,8 @@ static picture_t *vout_new_buffer( decoder_t *p_dec )
         p_owner->p_vout = NULL;
         vlc_mutex_unlock( &p_owner->lock );
 
-        p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
+        p_vout = input_ressource_RequestVout( p_owner->p_input->p->p_ressource,
+                                              p_vout, &p_dec->fmt_out.video );
 
         vlc_mutex_lock( &p_owner->lock );
         p_owner->p_vout = p_vout;
@@ -2382,7 +2380,7 @@ static subpicture_t *spu_new_buffer( decoder_t *p_dec )
         if( p_dec->b_die || p_dec->b_error )
             break;
 
-        p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+        p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
         if( p_vout )
             break;
 
@@ -2427,7 +2425,7 @@ static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
     vout_thread_t *p_vout = NULL;
 
-    p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+    p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
     if( !p_vout || p_owner->p_spu_vout != p_vout )
     {
         if( p_vout )