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>
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
)
64 static int vlc_sd_probe_Open( vlc_object_t
* );
67 set_category( CAT_PLAYLIST
)
68 set_subcategory( SUBCAT_PLAYLIST_SD
)
70 set_shortname( N_("Video") )
71 set_description( N_("My Videos") )
72 set_capability( "services_discovery", 10 )
73 set_callbacks( OpenVideo
, Close
)
74 add_shortcut( "video_dir" )
77 set_shortname( N_("Audio") )
78 set_description( N_("My Music") )
79 set_capability( "services_discovery", 10 )
80 set_callbacks( OpenAudio
, Close
)
81 add_shortcut( "audio_dir" )
84 set_shortname( N_("Picture") )
85 set_description( N_("My Pictures") )
86 set_capability( "services_discovery", 10 )
87 set_callbacks( OpenPicture
, Close
)
88 add_shortcut( "picture_dir" )
90 VLC_SD_PROBE_SUBMODULE
94 /*****************************************************************************
96 *****************************************************************************/
98 static void* Run( void* );
100 static void input_item_subitem_added( const vlc_event_t
*, void* );
101 static int onNewFileAdded( vlc_object_t
*, char const *,
102 vlc_value_t
, vlc_value_t
, void *);
104 static enum type_e
fileType( services_discovery_t
*p_sd
, const char* psz_file
);
105 static void formatSnapshotItem( input_item_t
* );
107 struct services_discovery_sys_t
116 /*****************************************************************************
117 * Open: initialize module
118 *****************************************************************************/
119 static int Open( vlc_object_t
*p_this
, enum type_e i_type
)
121 services_discovery_t
*p_sd
= ( services_discovery_t
* )p_this
;
122 services_discovery_sys_t
*p_sys
= p_sd
->p_sys
;
124 p_sd
->p_sys
= p_sys
= calloc( 1, sizeof( *p_sys
) );
128 p_sys
->i_type
= i_type
;
130 if( p_sys
->i_type
== Video
)
132 p_sys
->psz_dir
[0] = config_GetUserDir( VLC_VIDEOS_DIR
);
133 p_sys
->psz_dir
[1] = var_CreateGetString( p_sd
, "input-record-path" );
135 p_sys
->psz_var
= "record-file";
137 else if( p_sys
->i_type
== Audio
)
139 p_sys
->psz_dir
[0] = config_GetUserDir( VLC_MUSIC_DIR
);
140 p_sys
->psz_dir
[1] = var_CreateGetString( p_sd
, "input-record-path" );
142 p_sys
->psz_var
= "record-file";
144 else if( p_sys
->i_type
== Picture
)
146 p_sys
->psz_dir
[0] = config_GetUserDir( VLC_PICTURES_DIR
);
147 p_sys
->psz_dir
[1] = var_CreateGetString( p_sd
, "snapshot-path" );
149 p_sys
->psz_var
= "snapshot-file";
157 var_AddCallback( p_sd
->p_libvlc
, p_sys
->psz_var
, onNewFileAdded
, p_sd
);
159 if( vlc_clone( &p_sys
->thread
, Run
, p_sd
, VLC_THREAD_PRIORITY_LOW
) )
161 var_DelCallback( p_sd
->p_libvlc
, p_sys
->psz_var
, onNewFileAdded
, p_sd
);
162 free( p_sys
->psz_dir
[1] );
163 free( p_sys
->psz_dir
[0] );
171 /*****************************************************************************
173 *****************************************************************************/
174 static void *Run( void *data
)
176 services_discovery_t
*p_sd
= data
;
177 services_discovery_sys_t
*p_sys
= p_sd
->p_sys
;
179 int canc
= vlc_savecancel();
181 int num_dir
= sizeof( p_sys
->psz_dir
) / sizeof( p_sys
->psz_dir
[0] );
182 for( int i
= 0; i
< num_dir
; i
++ )
184 char* psz_dir
= p_sys
->psz_dir
[i
];
186 /* make sure the directory exists */
188 if( psz_dir
== NULL
||
189 vlc_stat( psz_dir
, &st
) ||
190 !S_ISDIR( st
.st_mode
) )
193 // TODO: make_URI is only for file://, what about dir:// ?
194 // char* psz_uri = make_URI( psz_dir );
196 if( asprintf( &psz_uri
, "dir://%s", psz_dir
) == -1 )
199 input_item_t
* p_root
= input_item_New( p_sd
, psz_uri
, NULL
);
200 if( p_sys
->i_type
== Picture
)
201 input_item_AddOption( p_root
, "ignore-filetypes=ini,db,lnk,txt",
202 VLC_INPUT_OPTION_TRUSTED
);
204 input_item_AddOption( p_root
, "recursive=collapse",
205 VLC_INPUT_OPTION_TRUSTED
);
208 vlc_event_manager_t
*p_em
= &p_root
->event_manager
;
209 vlc_event_attach( p_em
, vlc_InputItemSubItemAdded
,
210 input_item_subitem_added
, p_sd
);
212 input_Read( p_sd
, p_root
);
214 vlc_event_detach( p_em
, vlc_InputItemSubItemAdded
,
215 input_item_subitem_added
, p_sd
);
217 vlc_gc_decref( p_root
);
221 vlc_restorecancel(canc
);
225 /*****************************************************************************
227 *****************************************************************************/
228 static void Close( vlc_object_t
*p_this
)
230 services_discovery_t
*p_sd
= (services_discovery_t
*)p_this
;
231 services_discovery_sys_t
*p_sys
= p_sd
->p_sys
;
233 vlc_cancel( p_sys
->thread
);
234 vlc_join( p_sys
->thread
, NULL
);
236 var_DelCallback( p_sd
->p_libvlc
, p_sys
->psz_var
, onNewFileAdded
, p_sd
);
238 free( p_sys
->psz_dir
[1] );
239 free( p_sys
->psz_dir
[0] );
244 /*****************************************************************************
245 * Callbacks and helper functions
246 *****************************************************************************/
247 static void input_item_subitem_added( const vlc_event_t
* p_event
,
250 services_discovery_t
*p_sd
= user_data
;
251 services_discovery_sys_t
*p_sys
= p_sd
->p_sys
;
253 /* retrieve new item */
254 input_item_t
*p_item
= p_event
->u
.input_item_subitem_added
.p_new_child
;
256 if( p_sys
->i_type
== Picture
)
257 formatSnapshotItem( p_item
);
259 services_discovery_AddItem( p_sd
, p_item
, NULL
);
262 static int onNewFileAdded( vlc_object_t
*p_this
, char const *psz_var
,
263 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
265 services_discovery_t
*p_sd
= p_data
;
267 (void)p_this
; (void)psz_var
; (void)oldval
;
268 char* psz_file
= newval
.psz_string
;
269 if( !psz_file
|| !*psz_file
)
272 char* psz_uri
= make_URI( psz_file
);
273 input_item_t
* p_item
= input_item_New( p_sd
, psz_uri
, NULL
);
275 if( fileType( p_sd
, psz_file
) == Picture
)
277 formatSnapshotItem( p_item
);
278 services_discovery_AddItem( p_sd
, p_item
, NULL
);
280 msg_Dbg( p_sd
, "New snapshot added : %s", psz_file
);
282 else if( fileType( p_sd
, psz_file
) == Audio
)
284 services_discovery_AddItem( p_sd
, p_item
, NULL
);
286 msg_Dbg( p_sd
, "New recorded audio added : %s", psz_file
);
288 else if( fileType( p_sd
, psz_file
) == Video
)
290 services_discovery_AddItem( p_sd
, p_item
, NULL
);
292 msg_Dbg( p_sd
, "New recorded video added : %s", psz_file
);
295 vlc_gc_decref( p_item
);
301 void formatSnapshotItem( input_item_t
*p_item
)
306 if( !p_item
->p_meta
)
307 p_item
->p_meta
= vlc_meta_New();
309 /* copy the snapshot mrl as a ArtURL */
312 char* psz_uri
= NULL
;
313 psz_uri
= input_item_GetURI( p_item
);
315 input_item_SetArtURL( p_item
, psz_uri
);
320 * TODO: select the best mrl for displaying snapshots
321 * - vlc://pause:10 => snapshot are displayed as Art
322 * - file:///path/image.ext => snapshot are displayed as videos
324 input_item_SetURI( p_item
, "vlc://pause:10" );
326 // input_item_AddOption( p_item, "fake-duration=10000",
327 // VLC_INPUT_OPTION_TRUSTED );
331 enum type_e
fileType( services_discovery_t
*p_sd
, const char* psz_file
)
333 services_discovery_sys_t
*p_sys
= p_sd
->p_sys
;
334 enum type_e i_ret
= Unknown
;
336 char* psz_dir
= strdup( psz_file
);
337 char* psz_tmp
= strrchr( psz_dir
, DIR_SEP_CHAR
);
341 int num_dir
= sizeof( p_sys
->psz_dir
) / sizeof( p_sys
->psz_dir
[0] );
342 for( int i
= 0; i
< num_dir
; i
++ )
344 char* psz_known_dir
= p_sys
->psz_dir
[i
];
346 if( psz_known_dir
&& !strcmp( psz_dir
, psz_known_dir
) )
347 i_ret
= p_sys
->i_type
;
354 static int vlc_sd_probe_Open( vlc_object_t
*obj
)
356 vlc_probe_t
*probe
= (vlc_probe_t
*)obj
;
358 vlc_sd_probe_Add( probe
, "video_dir{longname=\"My Videos\"}",
359 N_("My Videos"), SD_CAT_MYCOMPUTER
);
360 vlc_sd_probe_Add( probe
, "audio_dir{longname=\"My Music\"}",
361 N_("My Music"), SD_CAT_MYCOMPUTER
);
362 vlc_sd_probe_Add( probe
, "picture_dir{longname=\"My Pictures\"}",
363 N_("My Pictures"), SD_CAT_MYCOMPUTER
);
364 return VLC_PROBE_CONTINUE
;