1 /*****************************************************************************
2 * mediadirs.c: Picture/Music/Video user directories as service discoveries
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
7 * Authors: Erwan Tulou <erwan10 aT videolan DoT org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_charset.h>
36 #include <vlc_services_discovery.h>
41 /*****************************************************************************
43 *****************************************************************************/
45 enum type_e { Video = 0, Audio = 1, Picture = 2, Unknown = 3 };
47 static int Open( vlc_object_t *, enum type_e );
48 static void Close( vlc_object_t * );
51 #define OPEN_MODULE( type ) \
52 static int Open##type( vlc_object_t *p_this ) \
54 msg_Dbg( p_this, "Starting " #type ); \
55 return Open( p_this, type ); \
60 OPEN_MODULE( Picture )
65 set_category( CAT_PLAYLIST )
66 set_subcategory( SUBCAT_PLAYLIST_SD )
68 set_shortname( "Video" )
69 set_description( N_("My Videos") )
70 set_capability( "services_discovery", 10 )
71 set_callbacks( OpenVideo, Close )
72 add_shortcut( "video_dir" )
75 set_shortname( "Audio" )
76 set_description( N_("My Music") )
77 set_capability( "services_discovery", 10 )
78 set_callbacks( OpenAudio, Close )
79 add_shortcut( "audio_dir" )
82 set_shortname( "Picture")
83 set_description( N_("My Pictures") )
84 set_capability( "services_discovery", 10 )
85 set_callbacks( OpenPicture, Close )
86 add_shortcut( "picture_dir" )
91 /*****************************************************************************
93 *****************************************************************************/
95 static void* Run( void* );
97 static void input_item_subitem_added( const vlc_event_t*, void* );
98 static int onNewFileAdded( vlc_object_t*, char const *,
99 vlc_value_t, vlc_value_t, void *);
101 static enum type_e fileType( services_discovery_t *p_sd, const char* psz_file );
102 static void formatSnapshotItem( input_item_t* );
104 struct services_discovery_sys_t
113 /*****************************************************************************
114 * Open: initialize module
115 *****************************************************************************/
116 static int Open( vlc_object_t *p_this, enum type_e i_type )
118 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
119 services_discovery_sys_t *p_sys = p_sd->p_sys;
121 p_sd->p_sys = p_sys = calloc( 1, sizeof( *p_sys) );
125 p_sys->i_type = i_type;
127 if( p_sys->i_type == Video )
129 p_sys->psz_dir[0] = config_GetUserDir( VLC_VIDEOS_DIR );
130 p_sys->psz_dir[1] = var_CreateGetString( p_sd, "input-record-path" );
132 p_sys->psz_var = strdup( "record-file" );
134 else if( p_sys->i_type == Audio )
136 p_sys->psz_dir[0] = config_GetUserDir( VLC_MUSIC_DIR );
137 p_sys->psz_dir[1] = var_CreateGetString( p_sd, "input-record-path" );
139 p_sys->psz_var = strdup( "record-file" );
141 else if( p_sys->i_type == Picture )
143 p_sys->psz_dir[0] = config_GetUserDir( VLC_PICTURES_DIR );
144 p_sys->psz_dir[1] = var_CreateGetString( p_sd, "snapshot-path" );
146 p_sys->psz_var = strdup( "snapshot-file" );
154 var_AddCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
156 if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
158 var_DelCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
159 free( p_sys->psz_var );
160 free( p_sys->psz_dir[1] );
161 free( p_sys->psz_dir[0] );
169 /*****************************************************************************
171 *****************************************************************************/
172 static void *Run( void *data )
174 services_discovery_t *p_sd = data;
175 services_discovery_sys_t *p_sys = p_sd->p_sys;
177 int canc = vlc_savecancel();
179 int num_dir = sizeof( p_sys->psz_dir ) / sizeof( p_sys->psz_dir[0] );
180 for( int i = 0; i < num_dir; i++ )
182 char* psz_dir = p_sys->psz_dir[i];
184 /* make sure the directory exists */
186 if( psz_dir == NULL ||
187 utf8_stat( psz_dir, &st ) ||
188 !S_ISDIR( st.st_mode ) )
191 // TODO: make_URI is only for file://, what about dir:// ?
192 // char* psz_uri = make_URI( psz_dir );
194 if( asprintf( &psz_uri, "dir://%s", psz_dir ) == -1 )
197 input_item_t* p_root = input_item_New( p_sd, psz_uri, NULL );
198 if( p_sys->i_type == Picture )
199 input_item_AddOption( p_root, "ignore-filetypes=ini,db,lnk,txt",
200 VLC_INPUT_OPTION_TRUSTED );
202 input_item_AddOption( p_root, "recursive=expand",
203 VLC_INPUT_OPTION_TRUSTED );
206 vlc_event_manager_t *p_em = &p_root->event_manager;
207 vlc_event_attach( p_em, vlc_InputItemSubItemAdded,
208 input_item_subitem_added, p_sd );
210 input_Read( p_sd, p_root, true );
212 vlc_event_detach( p_em, vlc_InputItemSubItemAdded,
213 input_item_subitem_added, p_sd );
215 vlc_gc_decref( p_root );
219 vlc_restorecancel(canc);
223 /*****************************************************************************
225 *****************************************************************************/
226 static void Close( vlc_object_t *p_this )
228 services_discovery_t *p_sd = (services_discovery_t *)p_this;
229 services_discovery_sys_t *p_sys = p_sd->p_sys;
231 vlc_cancel( p_sys->thread );
232 vlc_join( p_sys->thread, NULL );
234 var_DelCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
236 free( p_sys->psz_var );
237 free( p_sys->psz_dir[1] );
238 free( p_sys->psz_dir[0] );
243 /*****************************************************************************
244 * Callbacks and helper functions
245 *****************************************************************************/
246 static void input_item_subitem_added( const vlc_event_t * p_event,
249 services_discovery_t *p_sd = user_data;
250 services_discovery_sys_t *p_sys = p_sd->p_sys;
252 /* retrieve new item */
253 input_item_t *p_item = p_event->u.input_item_subitem_added.p_new_child;
255 if( p_sys->i_type == Picture )
256 formatSnapshotItem( p_item );
258 services_discovery_AddItem( p_sd, p_item, NULL );
261 static int onNewFileAdded( vlc_object_t *p_this, char const *psz_var,
262 vlc_value_t oldval, vlc_value_t newval, void *p_data )
264 services_discovery_t *p_sd = p_data;
266 (void)p_this; (void)psz_var; (void)oldval;
267 char* psz_file = newval.psz_string;
268 if( !psz_file || !*psz_file )
271 char* psz_uri = make_URI( psz_file );
272 input_item_t* p_item = input_item_New( p_sd, psz_uri, NULL );
274 if( fileType( p_sd, psz_file ) == Picture )
276 formatSnapshotItem( p_item );
277 services_discovery_AddItem( p_sd, p_item, NULL );
279 msg_Dbg( p_sd, "New snapshot added : %s", psz_file );
281 else if( fileType( p_sd, psz_file ) == Audio )
283 services_discovery_AddItem( p_sd, p_item, NULL );
285 msg_Dbg( p_sd, "New recorded audio added : %s", psz_file );
287 else if( fileType( p_sd, psz_file ) == Video )
289 services_discovery_AddItem( p_sd, p_item, NULL );
291 msg_Dbg( p_sd, "New recorded video added : %s", psz_file );
294 vlc_gc_decref( p_item );
300 void formatSnapshotItem( input_item_t *p_item )
305 if( !p_item->p_meta )
306 p_item->p_meta = vlc_meta_New();
308 /* copy the snapshot mrl as a ArtURL */
311 char* psz_uri = NULL;
312 psz_uri = input_item_GetURI( p_item );
314 input_item_SetArtURL( p_item, psz_uri );
319 * TODO: select the best mrl for displaying snapshots
320 * - vlc://pause:10 => snapshot are displayed as Art
321 * - file:///path/image.ext => snapshot are displayed as videos
323 input_item_SetURI( p_item, "vlc://pause:10" );
325 // input_item_AddOption( p_item, "fake-duration=10000",
326 // VLC_INPUT_OPTION_TRUSTED );
330 enum type_e fileType( services_discovery_t *p_sd, const char* psz_file )
332 services_discovery_sys_t *p_sys = p_sd->p_sys;
333 enum type_e i_ret = Unknown;
335 char* psz_dir = strdup( psz_file );
336 char* psz_tmp = strrchr( psz_dir, DIR_SEP_CHAR );
340 int num_dir = sizeof( p_sys->psz_dir ) / sizeof( p_sys->psz_dir[0] );
341 for( int i = 0; i < num_dir; i++ )
343 char* psz_known_dir = p_sys->psz_dir[i];
345 if( psz_known_dir && !strcmp( psz_dir, psz_known_dir ) )
346 i_ret = p_sys->i_type;