]> git.sesse.net Git - vlc/blob - modules/services_discovery/mediadirs.c
upnp: probe support (untested)
[vlc] / modules / services_discovery / mediadirs.c
1 /*****************************************************************************
2  * mediadirs.c:  Picture/Music/Video user directories as service discoveries
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Erwan Tulou <erwan10 aT videolan DoT org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Includes
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_url.h>
35 #include <vlc_charset.h>
36 #include <vlc_services_discovery.h>
37
38 #include <sys/stat.h>
39
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44
45 enum type_e { Video = 0, Audio = 1, Picture = 2, Unknown = 3 };
46
47 static int  Open( vlc_object_t *, enum type_e );
48 static void Close( vlc_object_t * );
49
50 /* Main functions */
51 #define OPEN_MODULE( type )                        \
52 static int Open##type( vlc_object_t *p_this )      \
53 {                                                  \
54     msg_Dbg( p_this, "Starting " #type );          \
55     return Open( p_this, type );                   \
56 }
57
58 OPEN_MODULE( Video )
59 OPEN_MODULE( Audio )
60 OPEN_MODULE( Picture )
61
62 #undef OPEN_MODULE
63
64 vlc_module_begin ()
65     set_category( CAT_PLAYLIST )
66     set_subcategory( SUBCAT_PLAYLIST_SD )
67
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" )
73
74     add_submodule ()
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" )
80
81     add_submodule ()
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" )
87
88 vlc_module_end ()
89
90
91 /*****************************************************************************
92  * Local prototypes
93  *****************************************************************************/
94
95 static void* Run( void* );
96
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 *);
100
101 static enum type_e fileType( services_discovery_t *p_sd, const char* psz_file );
102 static void formatSnapshotItem( input_item_t* );
103
104 struct services_discovery_sys_t
105 {
106     vlc_thread_t thread;
107     enum type_e i_type;
108
109     char* psz_dir[2];
110     const char* psz_var;
111 };
112
113 /*****************************************************************************
114  * Open: initialize module
115  *****************************************************************************/
116 static int Open( vlc_object_t *p_this, enum type_e i_type )
117 {
118     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
119     services_discovery_sys_t *p_sys = p_sd->p_sys;
120
121     p_sd->p_sys = p_sys = calloc( 1, sizeof( *p_sys) );
122     if( !p_sys )
123         return VLC_ENOMEM;
124
125     p_sys->i_type = i_type;
126
127     if( p_sys->i_type == Video )
128     {
129         p_sys->psz_dir[0] = config_GetUserDir( VLC_VIDEOS_DIR );
130         p_sys->psz_dir[1] = var_CreateGetString( p_sd, "input-record-path" );
131
132         p_sys->psz_var = "record-file";
133     }
134     else if( p_sys->i_type == Audio )
135     {
136         p_sys->psz_dir[0] = config_GetUserDir( VLC_MUSIC_DIR );
137         p_sys->psz_dir[1] = var_CreateGetString( p_sd, "input-record-path" );
138
139         p_sys->psz_var = "record-file";
140     }
141     else if( p_sys->i_type == Picture )
142     {
143         p_sys->psz_dir[0] = config_GetUserDir( VLC_PICTURES_DIR );
144         p_sys->psz_dir[1] = var_CreateGetString( p_sd, "snapshot-path" );
145
146         p_sys->psz_var = "snapshot-file";
147     }
148     else
149     {
150         free( p_sys );
151         return VLC_EGENERIC;
152     }
153
154     var_AddCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
155
156     if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
157     {
158         var_DelCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
159         free( p_sys->psz_dir[1] );
160         free( p_sys->psz_dir[0] );
161         free( p_sys );
162         return VLC_EGENERIC;
163     }
164
165     return VLC_SUCCESS;
166 }
167
168 /*****************************************************************************
169  * Run:
170  *****************************************************************************/
171 static void *Run( void *data )
172 {
173     services_discovery_t *p_sd = data;
174     services_discovery_sys_t *p_sys = p_sd->p_sys;
175
176     int canc = vlc_savecancel();
177
178     int num_dir = sizeof( p_sys->psz_dir ) / sizeof( p_sys->psz_dir[0] );
179     for( int i = 0; i < num_dir; i++ )
180     {
181         char* psz_dir = p_sys->psz_dir[i];
182
183         /* make sure the directory exists */
184         struct stat st;
185         if( psz_dir == NULL            ||
186             utf8_stat( psz_dir, &st )  ||
187             !S_ISDIR( st.st_mode ) )
188             continue;
189
190         // TODO:  make_URI is only for file://, what about dir:// ?
191         // char* psz_uri = make_URI( psz_dir );
192         char* psz_uri;
193         if( asprintf( &psz_uri, "dir://%s",  psz_dir ) == -1 )
194             continue;
195
196         input_item_t* p_root = input_item_New( p_sd, psz_uri, NULL );
197         if( p_sys->i_type == Picture )
198             input_item_AddOption( p_root, "ignore-filetypes=ini,db,lnk,txt",
199                                   VLC_INPUT_OPTION_TRUSTED );
200
201         input_item_AddOption( p_root, "recursive=collapse",
202                               VLC_INPUT_OPTION_TRUSTED );
203
204
205         vlc_event_manager_t *p_em = &p_root->event_manager;
206         vlc_event_attach( p_em, vlc_InputItemSubItemAdded,
207                           input_item_subitem_added, p_sd );
208
209         input_Read( p_sd, p_root );
210
211         vlc_event_detach( p_em, vlc_InputItemSubItemAdded,
212                           input_item_subitem_added, p_sd );
213
214         vlc_gc_decref( p_root );
215         free( psz_uri );
216     }
217
218     vlc_restorecancel(canc);
219     return NULL;
220 }
221
222 /*****************************************************************************
223  * Close:
224  *****************************************************************************/
225 static void Close( vlc_object_t *p_this )
226 {
227     services_discovery_t *p_sd = (services_discovery_t *)p_this;
228     services_discovery_sys_t *p_sys = p_sd->p_sys;
229
230     vlc_cancel( p_sys->thread );
231     vlc_join( p_sys->thread, NULL );
232
233     var_DelCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
234
235     free( p_sys->psz_dir[1] );
236     free( p_sys->psz_dir[0] );
237     free( p_sys );
238 }
239
240
241 /*****************************************************************************
242  * Callbacks and helper functions
243  *****************************************************************************/
244 static void input_item_subitem_added( const vlc_event_t * p_event,
245                                       void * user_data )
246 {
247     services_discovery_t *p_sd = user_data;
248     services_discovery_sys_t *p_sys = p_sd->p_sys;
249
250     /* retrieve new item */
251     input_item_t *p_item = p_event->u.input_item_subitem_added.p_new_child;
252
253     if( p_sys->i_type == Picture )
254         formatSnapshotItem( p_item );
255
256     services_discovery_AddItem( p_sd, p_item, NULL );
257 }
258
259 static int onNewFileAdded( vlc_object_t *p_this, char const *psz_var,
260                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
261 {
262     services_discovery_t *p_sd = p_data;
263
264     (void)p_this; (void)psz_var; (void)oldval;
265     char* psz_file = newval.psz_string;
266     if( !psz_file || !*psz_file )
267         return VLC_EGENERIC;
268
269     char* psz_uri = make_URI( psz_file );
270     input_item_t* p_item = input_item_New( p_sd, psz_uri, NULL );
271
272     if( fileType( p_sd, psz_file ) == Picture )
273     {
274         formatSnapshotItem( p_item );
275         services_discovery_AddItem( p_sd, p_item, NULL );
276
277         msg_Dbg( p_sd, "New snapshot added : %s", psz_file );
278     }
279     else if( fileType( p_sd, psz_file ) == Audio )
280     {
281         services_discovery_AddItem( p_sd, p_item, NULL );
282
283         msg_Dbg( p_sd, "New recorded audio added : %s", psz_file );
284     }
285     else if( fileType( p_sd, psz_file ) == Video )
286     {
287         services_discovery_AddItem( p_sd, p_item, NULL );
288
289         msg_Dbg( p_sd, "New recorded video added : %s", psz_file );
290     }
291
292     vlc_gc_decref( p_item );
293     free( psz_uri );
294
295     return VLC_SUCCESS;
296 }
297
298 void formatSnapshotItem( input_item_t *p_item )
299 {
300     if( !p_item )
301         return;
302
303     if( !p_item->p_meta )
304         p_item->p_meta = vlc_meta_New();
305
306     /* copy the snapshot mrl as a ArtURL */
307     if( p_item->p_meta )
308     {
309         char* psz_uri = NULL;
310         psz_uri = input_item_GetURI( p_item );
311         if( psz_uri )
312             input_item_SetArtURL( p_item, psz_uri );
313         free( psz_uri );
314     }
315
316     /**
317      * TODO: select the best mrl for displaying snapshots
318      *   - vlc://pause:10  => snapshot are displayed as Art
319      *   - file:///path/image.ext  => snapshot are displayed as videos
320      **/
321     input_item_SetURI( p_item, "vlc://pause:10" );
322
323     // input_item_AddOption( p_item, "fake-duration=10000",
324     //                       VLC_INPUT_OPTION_TRUSTED );
325 }
326
327
328 enum type_e fileType( services_discovery_t *p_sd, const char* psz_file )
329 {
330     services_discovery_sys_t *p_sys = p_sd->p_sys;
331     enum type_e i_ret = Unknown;
332
333     char* psz_dir = strdup( psz_file );
334     char* psz_tmp = strrchr( psz_dir, DIR_SEP_CHAR );
335     if( psz_tmp )
336         *psz_tmp = '\0';
337
338     int num_dir = sizeof( p_sys->psz_dir ) / sizeof( p_sys->psz_dir[0] );
339     for( int i = 0; i < num_dir; i++ )
340     {
341         char* psz_known_dir = p_sys->psz_dir[i];
342
343         if( psz_known_dir && !strcmp( psz_dir, psz_known_dir ) )
344             i_ret = p_sys->i_type;
345     }
346
347     free( psz_dir );
348     return i_ret;
349 }