[vlc-devel] [PATCH 4/5] input: add url support for subtitles detection

Thomas Guillem thomas at gllm.fr
Sat Feb 13 18:43:48 CET 2016


---
 src/input/input.c     | 54 ++++++++++++++++++++++++++++++-------------
 src/input/subtitles.c | 64 +++++++++++++++++++++------------------------------
 2 files changed, 64 insertions(+), 54 deletions(-)

diff --git a/src/input/input.c b/src/input/input.c
index 2f8088e..bbc4fd7 100644
--- a/src/input/input.c
+++ b/src/input/input.c
@@ -2815,35 +2815,57 @@ static void input_SubtitleFileAdd( input_thread_t *p_input,
                                    const char *psz_subtitle, unsigned i_flags,
                                    bool b_check_idx )
 {
+    char *url = NULL;
+    if( strstr( psz_subtitle, "://" ) == NULL )
+    {
+        url = vlc_path2uri( psz_subtitle, NULL );
+        if( url == NULL )
+            return;
+        psz_subtitle = url;
+    }
+
     /* if we are provided a subtitle.sub file,
      * see if we don't have a subtitle.idx and use it instead */
-    char *psz_idxpath = NULL;
+    input_item_t *p_item = NULL;
     char *psz_extension = b_check_idx ? strrchr( psz_subtitle, '.') : NULL;
     if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
     {
-        psz_idxpath = strdup( psz_subtitle );
-        if( psz_idxpath )
+        char *psz_dir = strdup( psz_subtitle );
+        char *p_sep;
+        if( psz_dir && (p_sep = strrchr(psz_dir, '/') ) )
         {
-            struct stat st;
+            *p_sep = '\0';
 
-            psz_extension = psz_extension - psz_subtitle + psz_idxpath;
-            strcpy( psz_extension, ".idx" );
+            int i_flags = p_input->i_flags;
+            p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
+            /* XXX maybe add a third NOINTERACT argument to stream_UrlNew ? */
+            stream_t *p_stream = stream_UrlNew( p_input, psz_dir );
+            p_input->i_flags = i_flags;
 
-            if( !vlc_stat( psz_idxpath, &st ) && S_ISREG( st.st_mode ) )
+            if( p_stream != NULL )
             {
-                msg_Dbg( p_input, "using %s as subtitle file instead of %s",
-                         psz_idxpath, psz_subtitle );
-                psz_subtitle = psz_idxpath;
+                while( ( p_item = stream_ReadDir( p_stream ) ) )
+                {
+                    size_t i_len = strlen( p_item->psz_name );
+                    if( i_len > 4
+                     && strcmp( p_item->psz_name + i_len - 4, ".idx" ) == 0)
+                    {
+                        msg_Dbg( p_input, "using %s as subtitle file instead of %s",
+                                 p_item->psz_uri, psz_subtitle );
+                        psz_subtitle = p_item->psz_uri;
+                        break;
+                    }
+                    else
+                        input_item_Release( p_item );
+                }
+                stream_Delete( p_stream );
             }
         }
     }
 
-    char *url = vlc_path2uri( psz_subtitle, NULL );
-    free( psz_idxpath );
-    if( url == NULL )
-        return;
-
-    input_SubtitleAdd( p_input, url, i_flags );
+    input_SubtitleAdd( p_input, psz_subtitle, i_flags );
+    if( p_item != NULL )
+        input_item_Release( p_item );
     free( url );
 }
 
diff --git a/src/input/subtitles.c b/src/input/subtitles.c
index 23bf4f9..ca4b60e 100644
--- a/src/input/subtitles.c
+++ b/src/input/subtitles.c
@@ -259,27 +259,21 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
     if( !psz_name_org )
         return NULL;
 
-    char *psz_fname = vlc_uri2path( psz_name_org );
-    if( !psz_fname )
-        return NULL;
-
     /* extract filename & dirname from psz_fname */
-    char *f_dir = strdup( psz_fname );
+    char *f_dir = strdup( psz_name_org );
     if( f_dir == NULL )
     {
-        free( psz_fname );
         return NULL;
     }
 
-    const char *f_fname = strrchr( psz_fname, DIR_SEP_CHAR );
+    const char *f_fname = strrchr( psz_name_org, DIR_SEP_CHAR );
     if( !f_fname )
     {
         free( f_dir );
-        free( psz_fname );
         return NULL;
     }
     f_fname++; /* Skip the '/' */
-    f_dir[f_fname - psz_fname] = 0; /* keep dir separator in f_dir */
+    f_dir[f_fname - psz_name_org] = 0; /* keep dir separator in f_dir */
 
     i_fname_len = strlen( f_fname );
 
@@ -290,7 +284,6 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
         free( f_dir );
         free( f_fname_noext );
         free( f_fname_trim );
-        free( psz_fname );
         return NULL;
     }
 
@@ -305,18 +298,26 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
         if( psz_dir == NULL || ( j >= 0 && !strcmp( psz_dir, f_dir ) ) )
             continue;
 
-        /* parse psz_src dir */
-        DIR *dir = vlc_opendir( psz_dir );
-        if( dir == NULL )
+        int i_flags = p_this->i_flags;
+        p_this->i_flags |= OBJECT_FLAGS_NOINTERACT;
+        /* XXX maybe add a third NOINTERACT argument to stream_UrlNew ? */
+        stream_t *p_stream = stream_UrlNew( p_this, psz_dir );
+        p_this->i_flags = i_flags;
+        if( p_stream == NULL )
             continue;
 
         msg_Dbg( p_this, "looking for a subtitle file in %s", psz_dir );
 
-        const char *psz_name;
-        while( (psz_name = vlc_readdir( dir )) && i_sub_count < MAX_SUBTITLE_FILES )
+        input_item_t *p_item;
+        while( ( p_item = stream_ReadDir( p_stream ) )
+            && i_sub_count < MAX_SUBTITLE_FILES )
         {
+            const char *psz_name = p_item->psz_name;
             if( psz_name[0] == '.' || !subtitles_Filter( psz_name ) )
+            {
+                input_item_Release( p_item );
                 continue;
+            }
 
             char tmp_fname_noext[strlen( psz_name ) + 1];
             char tmp_fname_trim[strlen( psz_name ) + 1];
@@ -355,31 +356,19 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
                 /* doesn't contain the movie name, prefer files in f_dir over subdirs */
                 i_prio = SUB_PRIORITY_MATCH_NONE;
             }
-            if( i_prio >= i_fuzzy )
+            if( i_prio >= i_fuzzy && p_item->i_type == ITEM_TYPE_FILE )
             {
-                struct stat st;
-                char *path;
-
-                if( asprintf( &path, "%s"DIR_SEP"%s", psz_dir, psz_name ) < 0 )
-                    continue;
-
-                if( strcmp( path, psz_fname )
-                 && vlc_stat( path, &st ) == 0
-                 && S_ISREG( st.st_mode ) && result )
-                {
-                    msg_Dbg( p_this,
-                            "autodetected subtitle: %s with priority %d",
-                            path, i_prio );
-                    result[i_sub_count].priority = i_prio;
-                    result[i_sub_count].psz_fname = path;
-                    path = NULL;
-                    result[i_sub_count].psz_ext = strdup(tmp_fname_ext);
-                    i_sub_count++;
-                }
-                free( path );
+                msg_Dbg( p_this,
+                        "autodetected subtitle: %s with priority %d",
+                        p_item->psz_uri, i_prio );
+                result[i_sub_count].priority = i_prio;
+                result[i_sub_count].psz_fname = strdup(p_item->psz_uri);
+                result[i_sub_count].psz_ext = strdup(tmp_fname_ext);
+                i_sub_count++;
             }
+            input_item_Release( p_item );
         }
-        closedir( dir );
+        stream_Delete( p_stream );
     }
     if( subdirs )
     {
@@ -390,7 +379,6 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
     free( f_dir );
     free( f_fname_trim );
     free( f_fname_noext );
-    free( psz_fname );
 
     if( !result )
         return NULL;
-- 
2.7.0



More information about the vlc-devel mailing list