]> git.sesse.net Git - vlc/blobdiff - src/input/es_out.c
Privatized input_clock_t to clock.c
[vlc] / src / input / es_out.c
index 509c42c8b7ff27cf98dda4d2a5338970092db787..cca6a6e1599fefc757678d603c5b3e6604004168 100644 (file)
@@ -29,9 +29,9 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
-
 #include <stdio.h>
+#include <assert.h>
+#include <vlc_common.h>
 
 #include <vlc_input.h>
 #include <vlc_es_out.h>
@@ -40,8 +40,9 @@
 
 #include "input_internal.h"
 
-#include "vlc_playlist.h"
-#include "iso_lang.h"
+#include "../stream_output/stream_output.h"
+
+#include <vlc_iso_lang.h>
 /* FIXME we should find a better way than including that */
 #include "../text/iso-639_def.h"
 
@@ -59,7 +60,7 @@ typedef struct
     bool b_selected;
 
     /* Clock for this program */
-    input_clock_t clock;
+    input_clock_t *p_clock;
 
     char    *psz_name;
     char    *psz_now_playing;
@@ -84,6 +85,7 @@ struct es_out_id_t
     char        *psz_language_code;
 
     decoder_t   *p_dec;
+    decoder_t   *p_dec_record;
 
     /* Fields for Video with CC */
     bool  pb_cc_present[4];
@@ -135,6 +137,9 @@ struct es_out_sys_t
 
     /* Rate used to rescale ES ts */
     int         i_rate;
+
+    /* Record */
+    sout_instance_t *p_sout_record;
 };
 
 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
@@ -178,17 +183,25 @@ static inline int EsOutGetClosedCaptionsChannel( vlc_fourcc_t fcc )
  *****************************************************************************/
 es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
 {
-    es_out_t     *out = malloc( sizeof( es_out_t ) );
-    es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
     vlc_value_t  val;
     int i;
 
+    es_out_t     *out = malloc( sizeof( es_out_t ) );
+    if( !out ) return NULL;
+
+    es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
+    if( !p_sys )
+    {
+        free( out );
+        return NULL;
+    }
+
     out->pf_add     = EsOutAdd;
     out->pf_send    = EsOutSend;
     out->pf_del     = EsOutDel;
     out->pf_control = EsOutControl;
     out->p_sys      = p_sys;
-    out->b_sout     = (p_input->p->p_sout != NULL ? true : false);
+    out->b_sout     = p_input->p->p_sout != NULL;
 
     p_sys->p_input = p_input;
 
@@ -259,6 +272,8 @@ es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
 
     p_sys->i_rate = i_rate;
 
+    p_sys->p_sout_record = NULL;
+
     return out;
 }
 
@@ -270,12 +285,14 @@ void input_EsOutDelete( es_out_t *out )
     es_out_sys_t *p_sys = out->p_sys;
     int i;
 
+    if( p_sys->p_sout_record )
+        input_EsOutSetRecord( out, false );
+
     for( i = 0; i < p_sys->i_es; i++ )
     {
         if( p_sys->es[i]->p_dec )
-        {
             input_DecoderDelete( p_sys->es[i]->p_dec );
-        }
+
         free( p_sys->es[i]->psz_language );
         free( p_sys->es[i]->psz_language_code );
         es_format_Clean( &p_sys->es[i]->fmt );
@@ -294,13 +311,13 @@ void input_EsOutDelete( es_out_t *out )
             free( p_sys->ppsz_sub_language[i] );
         free( p_sys->ppsz_sub_language );
     }
-
     free( p_sys->es );
 
     /* FIXME duplicate work EsOutProgramDel (but we cannot use it) add a EsOutProgramClean ? */
     for( i = 0; i < p_sys->i_pgrm; i++ )
     {
         es_out_pgrm_t *p_pgrm = p_sys->pgrm[i];
+        input_ClockDelete( p_pgrm->p_clock );
         free( p_pgrm->psz_now_playing );
         free( p_pgrm->psz_publisher );
         free( p_pgrm->psz_name );
@@ -332,6 +349,15 @@ es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
     return NULL;
 }
 
+mtime_t input_EsOutGetWakeup( es_out_t *out )
+{
+    es_out_sys_t      *p_sys = out->p_sys;
+
+    if( !p_sys->p_pgrm )
+        return 0;
+    return input_ClockGetWakeup( p_sys->p_input, p_sys->p_pgrm->p_clock );
+}
+
 static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
 {
     es_out_sys_t      *p_sys = out->p_sys;
@@ -344,7 +370,11 @@ static void EsOutDiscontinuity( es_out_t *out, bool b_flush, bool b_audio )
         /* Send a dummy block to let decoder know that
          * there is a discontinuity */
         if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
+        {
             input_DecoderDiscontinuity( es->p_dec, b_flush );
+            if( es->p_dec_record )
+                input_DecoderDiscontinuity( es->p_dec_record, b_flush );
+        }
     }
 }
 void input_EsOutChangeRate( es_out_t *out, int i_rate )
@@ -356,9 +386,80 @@ void input_EsOutChangeRate( es_out_t *out, int i_rate )
     EsOutDiscontinuity( out, false, false );
 
     for( i = 0; i < p_sys->i_pgrm; i++ )
-        input_ClockSetRate( &p_sys->pgrm[i]->clock, i_rate );
+        input_ClockSetRate( p_sys->pgrm[i]->p_clock, i_rate );
 }
 
+int input_EsOutSetRecord(  es_out_t *out, bool b_record )
+{
+    es_out_sys_t   *p_sys = out->p_sys;
+    input_thread_t *p_input = p_sys->p_input;
+
+    assert( ( b_record && !p_sys->p_sout_record ) || ( !b_record && p_sys->p_sout_record ) );
+
+    if( b_record )
+    {
+        char *psz_path = var_CreateGetString( p_input, "input-record-path" );
+        if( !psz_path || *psz_path == '\0' )
+        {
+            free( psz_path );
+            psz_path = strdup( config_GetHomeDir() );
+        }
+
+        char *psz_sout = NULL;  // TODO conf
+
+        if( !psz_sout && psz_path )
+        {
+            char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
+            if( psz_file )
+            {
+                if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
+                    psz_sout = NULL;
+                free( psz_file );
+            }
+        }
+        free( psz_path );
+
+        if( !psz_sout )
+            return VLC_EGENERIC;
+
+#ifdef ENABLE_SOUT
+        p_sys->p_sout_record = sout_NewInstance( p_input, psz_sout );
+#endif
+        free( psz_sout );
+
+        if( !p_sys->p_sout_record )
+            return VLC_EGENERIC;
+
+        for( int i = 0; i < p_sys->i_es; i++ )
+        {
+            es_out_id_t *p_es = p_sys->es[i];
+
+            if( !p_es->p_dec || p_es->p_master )
+                continue;
+
+            p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
+        }
+    }
+    else
+    {
+        for( int i = 0; i < p_sys->i_es; i++ )
+        {
+            es_out_id_t *p_es = p_sys->es[i];
+
+            if( !p_es->p_dec_record )
+                continue;
+
+            input_DecoderDelete( p_es->p_dec_record );
+            p_es->p_dec_record = NULL;
+        }
+#ifdef ENABLE_SOUT
+        sout_DeleteInstance( p_sys->p_sout_record );
+#endif
+        p_sys->p_sout_record = NULL;
+    }
+
+    return VLC_SUCCESS;
+}
 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
 {
     es_out_sys_t *p_sys = out->p_sys;
@@ -404,6 +505,8 @@ bool input_EsOutDecodersEmpty( es_out_t *out )
 
         if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
             return false;
+        if( es->p_dec_record && !input_DecoderEmpty( es->p_dec_record ) )
+            return false;
     }
     return true;
 }
@@ -416,6 +519,7 @@ static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt,
 {
     es_out_sys_t      *p_sys = out->p_sys;
     input_thread_t    *p_input = p_sys->p_input;
+    const  bool b_teletext = fmt->i_cat == SPU_ES && fmt->i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' );
     vlc_value_t       val, text;
 
     const char *psz_var;
@@ -431,8 +535,12 @@ static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt,
 
     if( b_delete )
     {
+        if( b_teletext )
+            var_SetInteger( p_sys->p_input, "teletext-es", -1 );
+
         val.i_int = i_id;
         var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
+
         var_SetBool( p_sys->p_input, "intf-change", true );
         return;
     }
@@ -465,17 +573,13 @@ static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt,
     {
         if( psz_language && *psz_language )
         {
-            char *temp;
-            text.psz_string = malloc( strlen( _("Track %i") )+
-                                      strlen( psz_language ) + 30 );
-            asprintf( &temp,  _("Track %i"), val.i_int );
-            sprintf( text.psz_string, "%s - [%s]", temp, psz_language );
-            free( temp );
+            if( asprintf( &text.psz_string, "%s %i - [%s]", _( "Track" ), val.i_int, psz_language ) == -1 )
+                text.psz_string = NULL;
         }
         else
         {
-            text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
-            sprintf( text.psz_string, _("Track %i"), val.i_int );
+            if( asprintf( &text.psz_string, "%s %i", _( "Track" ), val.i_int ) == -1 )
+                text.psz_string = NULL;
         }
     }
 
@@ -484,6 +588,12 @@ static void EsOutESVarUpdateGeneric( es_out_t *out, int i_id, es_format_t *fmt,
 
     free( text.psz_string );
 
+    if( b_teletext )
+    {
+        if( var_GetInteger( p_sys->p_input, "teletext-es" ) < 0 )
+            var_SetInteger( p_sys->p_input, "teletext-es", i_id );
+    }
+
     var_SetBool( p_sys->p_input, "intf-change", true );
 }
 
@@ -529,11 +639,9 @@ static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
     p_pgrm->b_selected = true;
 
     /* Switch master stream */
-    if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
-    {
-        p_sys->p_pgrm->clock.b_master = false;
-    }
-    p_pgrm->clock.b_master = true;
+    if( p_sys->p_pgrm )
+        input_ClockSetMaster( p_sys->p_pgrm->p_clock, false );
+    input_ClockSetMaster( p_pgrm->p_clock, true );
     p_sys->p_pgrm = p_pgrm;
 
     /* Update "program" */
@@ -544,6 +652,7 @@ static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
     var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
     var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
     var_Change( p_input, "spu-es",   VLC_VAR_CLEARCHOICES, NULL, NULL );
+    var_SetInteger( p_input, "teletext-es", -1 );
     for( i = 0; i < p_sys->i_es; i++ )
     {
         if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
@@ -570,6 +679,8 @@ static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
     vlc_value_t       val;
 
     es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );
+    if( !p_pgrm )
+        return NULL;
 
     /* Init */
     p_pgrm->i_id = i_group;
@@ -579,7 +690,12 @@ static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
     p_pgrm->psz_now_playing = NULL;
     p_pgrm->psz_publisher = NULL;
     p_pgrm->p_epg = NULL;
-    input_ClockInit( &p_pgrm->clock, false, p_input->p->input.i_cr_average, p_sys->i_rate );
+    p_pgrm->p_clock = input_ClockNew( false, p_input->p->input.i_cr_average, p_sys->i_rate );
+    if( !p_pgrm->p_clock )
+    {
+        free( p_pgrm );
+        return NULL;
+    }
 
     /* Append it */
     TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
@@ -632,7 +748,10 @@ static int EsOutProgramDel( es_out_t *out, int i_group )
     TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
 
     /* If program is selected we need to unselect it */
-    if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = NULL;
+    if( p_sys->p_pgrm == p_pgrm )
+        p_sys->p_pgrm = NULL;
+
+    input_ClockDelete( p_pgrm->p_clock );
 
     free( p_pgrm->psz_name );
     free( p_pgrm->psz_now_playing );
@@ -656,9 +775,15 @@ static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm )
 {
     char *psz = NULL;
     if( p_pgrm->psz_name )
-        asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id );
+    {
+        if( asprintf( &psz, _("%s [%s %d]"), p_pgrm->psz_name, _("Program"), p_pgrm->i_id ) == -1 )
+            return NULL;
+    }
     else
-        asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id );
+    {
+        if( asprintf( &psz, "%s %d", _("Program"), p_pgrm->i_id ) == -1 )
+            return NULL;
+    }
     return psz;
 }
 
@@ -722,9 +847,11 @@ static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
 
         if( psz_provider && *psz_provider )
         {
-            asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
-            var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
-            free( text.psz_string );
+            if( asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider ) != -1 )
+            {
+                var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
+                free( text.psz_string );
+            }
         }
         else
         {
@@ -889,9 +1016,12 @@ static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
     es_out_pgrm_t     *p_pgrm = NULL;
     int i;
 
+    if( !es ) return NULL;
+
     if( fmt->i_group < 0 )
     {
         msg_Err( p_input, "invalid group number" );
+        free( es );
         return NULL;
     }
 
@@ -929,8 +1059,8 @@ static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
 
         es->i_channel = p_sys->i_audio;
 
-        vlc_mutex_lock( &p_input->p->input.p_item->lock );
         memset( &rg, 0, sizeof(rg) );
+        vlc_mutex_lock( &p_input->p->input.p_item->lock );
         vlc_audio_replay_gain_MergeFromMeta( &rg, p_input->p->input.p_item->p_meta );
         vlc_mutex_unlock( &p_input->p->input.p_item->lock );
 
@@ -970,9 +1100,10 @@ static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
     es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
     es->psz_language_code = LanguageGetCode( fmt->psz_language );
     es->p_dec = NULL;
+    es->p_dec_record = NULL;
     for( i = 0; i < 4; i++ )
         es->pb_cc_present[i] = false;
-    es->p_master = false;
+    es->p_master = NULL;
 
     if( es->p_pgrm == p_sys->p_pgrm )
         EsOutESVarUpdate( out, es, false );
@@ -1019,6 +1150,32 @@ static bool EsIsSelected( es_out_id_t *es )
         return es->p_dec != NULL;
     }
 }
+static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
+{
+    es_out_sys_t   *p_sys = out->p_sys;
+    input_thread_t *p_input = p_sys->p_input;
+
+    p_es->p_dec = input_DecoderNew( p_input, &p_es->fmt, p_input->p->p_sout );
+    if( p_es->p_dec && !p_es->p_master && p_sys->p_sout_record )
+        p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_sys->p_sout_record );
+}
+static void EsDestroyDecoder( es_out_t *out, es_out_id_t *p_es )
+{
+    VLC_UNUSED(out);
+
+    if( !p_es->p_dec )
+        return;
+
+    input_DecoderDelete( p_es->p_dec );
+    p_es->p_dec = NULL;
+
+    if( p_es->p_dec_record )
+    {
+        input_DecoderDelete( p_es->p_dec_record );
+        p_es->p_dec_record = NULL;
+    }
+}
+
 static void EsSelect( es_out_t *out, es_out_id_t *es )
 {
     es_out_sys_t   *p_sys = out->p_sys;
@@ -1046,8 +1203,7 @@ static void EsSelect( es_out_t *out, es_out_id_t *es )
     {
         if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
         {
-            if( !var_GetBool( p_input, "video" ) ||
-                ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
+            if( !var_GetBool( p_input, out->b_sout ? "sout-video" : "video" ) )
             {
                 msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x",
                          es->i_id );
@@ -1057,8 +1213,7 @@ static void EsSelect( es_out_t *out, es_out_id_t *es )
         else if( es->fmt.i_cat == AUDIO_ES )
         {
             var_Get( p_input, "audio", &val );
-            if( !var_GetBool( p_input, "audio" ) ||
-                ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
+            if( !var_GetBool( p_input, out->b_sout ? "sout-audio" : "audio" ) )
             {
                 msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
                          es->i_id );
@@ -1068,8 +1223,7 @@ static void EsSelect( es_out_t *out, es_out_id_t *es )
         if( es->fmt.i_cat == SPU_ES )
         {
             var_Get( p_input, "spu", &val );
-            if( !var_GetBool( p_input, "spu" ) ||
-                ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
+            if( !var_GetBool( p_input, out->b_sout ? "sout-spu" : "spu" ) )
             {
                 msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
                          es->i_id );
@@ -1078,7 +1232,8 @@ static void EsSelect( es_out_t *out, es_out_id_t *es )
         }
 
         es->i_preroll_end = -1;
-        es->p_dec = input_DecoderNew( p_input, &es->fmt, false );
+        EsCreateDecoder( out, es );
+
         if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
             return;
     }
@@ -1142,8 +1297,7 @@ static void EsUnselect( es_out_t *out, es_out_id_t *es, bool b_update )
 
             es->pb_cc_present[i] = false;
         }
-        input_DecoderDelete( es->p_dec );
-        es->p_dec = NULL;
+        EsDestroyDecoder( out, es );
     }
 
     if( !b_update )
@@ -1392,7 +1546,7 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
     else if( p_block->i_dts > 0 )
     {
         p_block->i_dts =
-            input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
+            input_ClockGetTS( p_input, p_pgrm->p_clock, p_block->i_dts ) + i_delay;
     }
     if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
     {
@@ -1401,7 +1555,7 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
     else if( p_block->i_pts > 0 )
     {
         p_block->i_pts =
-            input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
+            input_ClockGetTS( p_input, p_pgrm->p_clock, p_block->i_pts ) + i_delay;
     }
     if ( p_block->i_rate == INPUT_RATE_DEFAULT &&
          es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
@@ -1429,6 +1583,12 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
         bool pb_cc[4];
         bool b_cc_new = false;
         int i;
+        if( es->p_dec_record )
+        {
+            block_t *p_dup = block_Duplicate( p_block );
+            if( p_dup )
+                input_DecoderDecode( es->p_dec_record, p_dup );
+        }
         input_DecoderDecode( es->p_dec, p_block );
 
         /* Check CC status */
@@ -1441,12 +1601,6 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
                 VLC_FOURCC('c', 'c', '3', ' '),
                 VLC_FOURCC('c', 'c', '4', ' '),
             };
-            static const char *ppsz_description[4] = {
-                N_("Closed captions 1"),
-                N_("Closed captions 2"),
-                N_("Closed captions 3"),
-                N_("Closed captions 4"),
-            };
             es_format_t fmt;
 
             if(  es->pb_cc_present[i] || !pb_cc[i] )
@@ -1455,7 +1609,9 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
 
             es_format_Init( &fmt, SPU_ES, fcc[i] );
             fmt.i_group = es->fmt.i_group;
-            fmt.psz_description = strdup( _(ppsz_description[i] ) );
+            if( asprintf( &fmt.psz_description,
+                          _("Closed captions %u"), 1 + i ) == -1 )
+                fmt.psz_description = NULL;
             es->pp_cc_es[i] = EsOutAdd( out, &fmt );
             es->pp_cc_es[i]->p_master = es;
             es_format_Clean( &fmt );
@@ -1489,7 +1645,8 @@ static void EsOutDel( es_out_t *out, es_out_id_t *es )
     {
         while( !out->p_sys->p_input->b_die && es->p_dec )
         {
-            if( input_DecoderEmpty( es->p_dec ) )
+            if( input_DecoderEmpty( es->p_dec ) &&
+                ( !es->p_dec_record || input_DecoderEmpty( es->p_dec_record ) ))
                 break;
             msleep( 20*1000 );
         }
@@ -1628,50 +1785,26 @@ static int EsOutControl( es_out_t *out, int i_query, va_list args )
             return VLC_SUCCESS;
 
         case ES_OUT_SET_ES:
+        case ES_OUT_RESTART_ES:
+        {
+            int i_cat;
+
             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
-            /* Special case NULL, NULL+i_cat */
+
             if( es == NULL )
-            {
-                for( i = 0; i < p_sys->i_es; i++ )
-                {
-                    if( EsIsSelected( p_sys->es[i] ) )
-                        EsUnselect( out, p_sys->es[i],
-                                    p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
-                }
-            }
+                i_cat = UNKNOWN_ES;
             else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
-            {
-                for( i = 0; i < p_sys->i_es; i++ )
-                {
-                    if( p_sys->es[i]->fmt.i_cat == AUDIO_ES &&
-                        EsIsSelected( p_sys->es[i] ) )
-                        EsUnselect( out, p_sys->es[i],
-                                    p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
-                }
-            }
+                i_cat = AUDIO_ES;
             else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
-            {
-                for( i = 0; i < p_sys->i_es; i++ )
-                {
-                    if( p_sys->es[i]->fmt.i_cat == VIDEO_ES &&
-                        EsIsSelected( p_sys->es[i] ) )
-                        EsUnselect( out, p_sys->es[i],
-                                    p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
-                }
-            }
+                i_cat = VIDEO_ES;
             else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
-            {
-                for( i = 0; i < p_sys->i_es; i++ )
-                {
-                    if( p_sys->es[i]->fmt.i_cat == SPU_ES &&
-                        EsIsSelected( p_sys->es[i] ) )
-                        EsUnselect( out, p_sys->es[i],
-                                    p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
-                }
-            }
+                i_cat = SPU_ES;
             else
+                i_cat = -1;
+
+            for( i = 0; i < p_sys->i_es; i++ )
             {
-                for( i = 0; i < p_sys->i_es; i++ )
+                if( i_cat == -1 )
                 {
                     if( es == p_sys->es[i] )
                     {
@@ -1679,21 +1812,37 @@ static int EsOutControl( es_out_t *out, int i_query, va_list args )
                         break;
                     }
                 }
-            }
-            {
-                /* FIXME: we don't want to depend on the playlist */
-                playlist_t * p_playlist = vlc_object_find( p_sys->p_input,
-                    VLC_OBJECT_PLAYLIST, FIND_PARENT );
-                if( p_playlist )
+                else
                 {
-                    PL_LOCK;
-                    p_playlist->gc_date = mdate();
-                    vlc_object_signal_unlocked( p_playlist );
-                    PL_UNLOCK;
-                    vlc_object_release( p_playlist );
+                    if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
+                    {
+                        if( EsIsSelected( p_sys->es[i] ) )
+                        {
+                            if( i_query == ES_OUT_RESTART_ES )
+                            {
+                                if( p_sys->es[i]->p_dec )
+                                {
+                                    EsDestroyDecoder( out, p_sys->es[i] );
+                                    EsCreateDecoder( out, p_sys->es[i] );
+                                }
+                            }
+                            else
+                            {
+                                EsUnselect( out, p_sys->es[i],
+                                            p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
+                            }
+                        }
+                    }
                 }
             }
+            if( i_query == ES_OUT_SET_ES )
+            {
+                vlc_event_t event;
+                event.type = vlc_InputSelectedStreamChanged;
+                vlc_event_send( &p_sys->p_input->p->event_manager, &event );
+            }
             return VLC_SUCCESS;
+        }
  
         case ES_OUT_SET_DEFAULT:
         {
@@ -1759,14 +1908,15 @@ static int EsOutControl( es_out_t *out, int i_query, va_list args )
                 p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
 
             i_pcr = (int64_t)va_arg( args, int64_t );
-            /* search program */
-            input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
+            /* search program
+             * TODO do not use mdate() but proper stream acquisition date */
+            input_ClockSetPCR( p_sys->p_input, p_pgrm->p_clock, i_pcr, mdate() );
             return VLC_SUCCESS;
         }
 
         case ES_OUT_RESET_PCR:
             for( i = 0; i < p_sys->i_pgrm; i++ )
-                input_ClockResetPCR( &p_sys->pgrm[i]->clock );
+                input_ClockResetPCR( p_sys->pgrm[i]->p_clock );
             return VLC_SUCCESS;
 
         case ES_OUT_GET_TS:
@@ -1775,7 +1925,7 @@ static int EsOutControl( es_out_t *out, int i_query, va_list args )
                 int64_t i_ts = (int64_t)va_arg( args, int64_t );
                 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
                 *pi_ts = input_ClockGetTS( p_sys->p_input,
-                                           &p_sys->p_pgrm->clock, i_ts );
+                                           p_sys->p_pgrm->p_clock, i_ts );
                 return VLC_SUCCESS;
             }
             return VLC_EGENERIC;
@@ -1811,7 +1961,8 @@ static int EsOutControl( es_out_t *out, int i_query, va_list args )
             es_format_t *p_fmt;
             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
             p_fmt = (es_format_t*) va_arg( args, es_format_t * );
-            if( es == NULL ) return VLC_EGENERIC;
+            if( es == NULL )
+                return VLC_EGENERIC;
 
             if( p_fmt->i_extra )
             {
@@ -1819,13 +1970,12 @@ static int EsOutControl( es_out_t *out, int i_query, va_list args )
                 es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
                 memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
 
-                if( !es->p_dec ) return VLC_SUCCESS;
-
+                if( !es->p_dec )
+                    return VLC_SUCCESS;
 #if 1
-                input_DecoderDelete( es->p_dec );
-                es->p_dec = input_DecoderNew( p_sys->p_input,
-                                              &es->fmt, false );
+                EsDestroyDecoder( out, es );
 
+                EsCreateDecoder( out, es );
 #else
                 es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
                 es->p_dec->fmt_in.p_extra =
@@ -2030,7 +2180,8 @@ static void EsOutAddInfo( es_out_t *out, es_out_id_t *es )
     lldiv_t         div;
 
     /* Add stream info */
-    asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
+    if( asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 ) == -1 )
+        return;
 
     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
                    "%.4s", (char*)&fmt->i_codec );