input/input: refactor input_source_t addition for slaves
authorThomas Guillem <thomas@gllm.fr>
Tue, 7 Jun 2016 15:19:19 +0000 (17:19 +0200)
committerThomas Guillem <thomas@gllm.fr>
Tue, 7 Jun 2016 16:25:53 +0000 (18:25 +0200)
input_SlaveSourceAdd is now used to create and add SPU and AUDIO sources. It
returns now an error.

src/input/input.c

index 32ef072..1fa356f 100644 (file)
@@ -96,13 +96,13 @@ static void InputGetExtraFiles( input_thread_t *p_input,
 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment, demux_t ***ppp_attachment_demux,
                               int i_new, input_attachment_t **pp_new, demux_t *p_demux );
 
-enum {
-    SUB_NOFLAG = 0x00,
-    SUB_FORCED = 0x01,
-    SUB_CANFAIL = 0x02,
-};
+#define SLAVE_ADD_NOFLAG    0
+#define SLAVE_ADD_FORCED    (1<<0)
+#define SLAVE_ADD_CANFAIL   (1<<1)
+#define SLAVE_ADD_SET_TIME  (1<<2)
 
-static void input_SubtitleAdd( input_thread_t *, const char *, unsigned );
+static int input_SlaveSourceAdd( input_thread_t *, enum slave_type,
+                                 const char *, unsigned );
 static char *input_SubtitleFile2Uri( input_thread_t *, const char * );
 static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */
 
@@ -1114,25 +1114,20 @@ static void LoadSlaves( input_thread_t *p_input )
     for( int i = 0; i < i_slaves && pp_slaves[i] != NULL; i++ )
     {
         input_item_slave_t *p_slave = pp_slaves[i];
-        if( p_slave->i_type == SLAVE_TYPE_SPU )
+        /* Slaves added via options should not fail */
+        unsigned i_flags = p_slave->i_priority != SLAVE_PRIORITY_USER
+                           ? SLAVE_ADD_CANFAIL : SLAVE_ADD_NOFLAG;
+
+        if( p_slave->b_forced || p_slave->i_priority == SLAVE_PRIORITY_USER )
+            i_flags |= SLAVE_ADD_FORCED;
+
+        if( input_SlaveSourceAdd( p_input, p_slave->i_type, p_slave->psz_uri,
+                                  i_flags ) == VLC_SUCCESS )
         {
-            msg_Err( p_input, "Loading spu slave: %s", p_slave->psz_uri );
-            unsigned i_flags = p_slave->i_priority == SLAVE_PRIORITY_USER
-                               ? SUB_FORCED : SUB_CANFAIL;
-            if( p_slave->b_forced )
-                i_flags |= SUB_FORCED;
-            input_SubtitleAdd( p_input, p_slave->psz_uri, i_flags );
+            input_item_AddSlave( p_input->p->p_item, p_slave );
         }
         else
-        {
-            msg_Err( p_input, "Loading slave: %s", p_slave->psz_uri );
-            input_source_t *p_source = InputSourceNew( p_input,
-                                                       p_slave->psz_uri,
-                                                       NULL, true );
-            if( p_source )
-                TAB_APPEND( p_input->p->i_slave, p_input->p->slave, p_source );
-        }
-        input_item_AddSlave( p_input->p->p_item, p_slave );
+            input_item_slave_Delete( p_slave );
     }
     TAB_CLEAN( i_slaves, pp_slaves );
 
@@ -1164,9 +1159,10 @@ static void LoadSlaves( input_thread_t *p_input )
         {
             var_SetString( p_input, "sub-description", a->psz_description ? a->psz_description : "");
 
-            input_SubtitleAdd( p_input, psz_mrl, SUB_NOFLAG );
-
+            input_SlaveSourceAdd( p_input, SLAVE_TYPE_SPU, psz_mrl,
+                                  SLAVE_ADD_NOFLAG );
             free( psz_mrl );
+            /* Don't update item slaves for attachements */
         }
         vlc_input_attachment_Delete( a );
     }
@@ -1998,7 +1994,8 @@ static bool Control( input_thread_t *p_input,
                 char *psz_uri = input_SubtitleFile2Uri( p_input, val.psz_string );
                 if( psz_uri != NULL )
                 {
-                    input_SubtitleAdd( p_input, psz_uri, SUB_FORCED );
+                    input_SlaveSourceAdd( p_input, SLAVE_TYPE_SPU, psz_uri,
+                                          SLAVE_ADD_FORCED );
                     free( psz_uri );
                 }
             }
@@ -2008,57 +2005,19 @@ static bool Control( input_thread_t *p_input,
             if( val.p_address )
             {
                 input_item_slave_t *p_item_slave  = val.p_address;
-                if( p_item_slave->i_type == SLAVE_TYPE_SPU )
-                {
-                    input_SubtitleAdd( p_input, p_item_slave->psz_uri,
-                                       SUB_CANFAIL | SUB_FORCED );
+                unsigned i_flags = SLAVE_ADD_CANFAIL | SLAVE_ADD_SET_TIME;
+                if( p_item_slave->b_forced )
+                    i_flags |= SLAVE_ADD_FORCED;
 
+                if( input_SlaveSourceAdd( p_input, p_item_slave->i_type,
+                                          p_item_slave->psz_uri, i_flags )
+                                          == VLC_SUCCESS )
+                {
                     /* Update item slaves */
                     input_item_AddSlave( p_input->p->p_item, p_item_slave );
-                    /* The slave is now owned by the ite */
+                    /* The slave is now owned by the item */
                     val.p_address = NULL;
-                    break;
-                }
-                const char *uri = p_item_slave->psz_uri;
-                input_source_t *slave = InputSourceNew( p_input, uri, NULL,
-                                                        false );
-                if( slave == NULL )
-                {
-                    msg_Warn( p_input, "failed to add %s as slave", uri );
-                    break;
                 }
-
-                int64_t i_time;
-
-                /* Add the slave */
-                msg_Dbg( p_input, "adding %s as slave on the fly", uri );
-
-                /* Set position */
-                if( demux_Control( p_input->p->master->p_demux,
-                                   DEMUX_GET_TIME, &i_time ) )
-                {
-                    msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
-                    InputSourceDestroy( slave );
-                    break;
-                }
-
-                if( demux_Control( slave->p_demux,
-                                   DEMUX_SET_TIME, i_time, true ) )
-                {
-                    msg_Err( p_input, "seek failed for new slave" );
-                    InputSourceDestroy( slave );
-                    break;
-                }
-
-                /* Get meta (access and demux) */
-                InputUpdateMeta( p_input, slave->p_demux );
-
-                TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
-
-                /* Update item slaves */
-                input_item_AddSlave( p_input->p->p_item, p_item_slave );
-                /* The slave is now owned by the ite */
-                val.p_address = NULL;
             }
             break;
 
@@ -2924,31 +2883,83 @@ static void MRLSections( const char *p,
     *pi_chapter_end = chapter_end;
 }
 
-/*****************************************************************************
- * input_AddSubtitles: add a subtitle file and enable it
- *****************************************************************************/
-static void input_SubtitleAdd( input_thread_t *p_input,
-                               const char *url, unsigned i_flags )
+static int input_SlaveSourceAdd( input_thread_t *p_input,
+                                 enum slave_type i_type, const char *psz_uri,
+                                 unsigned i_flags )
 {
     vlc_value_t count;
+    const char *psz_es;
+    const char *psz_forced_demux;
+    const bool b_can_fail = i_flags & SLAVE_ADD_CANFAIL;
+    const bool b_forced = i_flags & SLAVE_ADD_FORCED;
+    const bool b_set_time = i_flags & SLAVE_ADD_SET_TIME;
 
-    var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
+    switch( i_type )
+    {
+    case SLAVE_TYPE_SPU:
+        psz_es = "spu-es";
+        psz_forced_demux = "subtitle";
+        break;
+    case SLAVE_TYPE_AUDIO:
+        psz_es = "audio-es";
+        psz_forced_demux = NULL;
+        break;
+    default:
+        vlc_assert_unreachable();
+    }
 
-    input_source_t *sub = InputSourceNew( p_input, url, "subtitle",
-                                          (i_flags & SUB_CANFAIL) );
-    if( sub == NULL )
-        return;
+    if( b_forced )
+        var_Change( p_input, psz_es, VLC_VAR_CHOICESCOUNT, &count, NULL );
 
-    TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
+    msg_Err( p_input, "Loading %s slave: %s (forced: %d)", psz_es, psz_uri, b_forced );
 
-    if( !(i_flags & SUB_FORCED) )
-        return;
+    input_source_t *p_source = InputSourceNew( p_input, psz_uri,
+                                               psz_forced_demux, b_can_fail );
+    if( p_source == NULL )
+    {
+        msg_Warn( p_input, "failed to add %s as slave", psz_uri );
+        return VLC_EGENERIC;
+    }
+
+    if( i_type == SLAVE_TYPE_AUDIO )
+    {
+        if( b_set_time )
+        {
+            int64_t i_time;
+
+            /* Set position */
+            if( demux_Control( p_input->p->master->p_demux,
+                               DEMUX_GET_TIME, &i_time ) )
+            {
+                msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
+                InputSourceDestroy( p_source );
+                return VLC_EGENERIC;
+            }
+
+            if( demux_Control( p_source->p_demux,
+                               DEMUX_SET_TIME, i_time, true ) )
+            {
+                msg_Err( p_input, "seek failed for new slave" );
+                InputSourceDestroy( p_source );
+                return VLC_EGENERIC;
+            }
+        }
+
+        /* Get meta (access and demux) */
+        InputUpdateMeta( p_input, p_source->p_demux );
+    }
+
+    TAB_APPEND( p_input->p->i_slave, p_input->p->slave, p_source );
+
+    if( !b_forced )
+        return VLC_SUCCESS;
 
     /* Select the ES */
     vlc_value_t list;
 
-    if( var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES, &list, NULL ) )
-        return;
+    if( var_Change( p_input, psz_es, VLC_VAR_GETCHOICES, &list, NULL ) )
+        return VLC_SUCCESS;
+
     if( count.i_int == 0 )
         count.i_int++;
     /* if it was first one, there is disable too */
@@ -2961,6 +2972,8 @@ static void input_SubtitleAdd( input_thread_t *p_input,
         es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
     }
     var_FreeList( &list, NULL );
+
+    return VLC_SUCCESS;
 }
 
 static char *input_SubtitleFile2Uri( input_thread_t *p_input,