]> git.sesse.net Git - vlc/blob - modules/access/gnomevfs.c
* Try the gnomevfs plugin last if we didn't find any other matching access
[vlc] / modules / access / gnomevfs.c
1 /*****************************************************************************
2  * gnomevfs.c: GnomeVFS input
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben -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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29 #include <libgnomevfs/gnome-vfs.h>
30
31 #if 0
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #ifdef HAVE_SYS_TYPES_H
36 #   include <sys/types.h>
37 #endif
38 #ifdef HAVE_SYS_TIME_H
39 #   include <sys/time.h>
40 #endif
41 #ifdef HAVE_SYS_STAT_H
42 #   include <sys/stat.h>
43 #endif
44 #ifdef HAVE_FCNTL_H
45 #   include <fcntl.h>
46 #endif
47
48 #ifdef HAVE_UNISTD_H
49 #   include <unistd.h>
50 #endif
51
52 #endif
53
54 #include "charset.h"
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 static int  Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
61
62 #define CACHING_TEXT N_("Caching value in ms")
63 #define CACHING_LONGTEXT N_( \
64     "Allows you to modify the default caching value for GnomeVFS streams."\
65     "This value should be set in millisecond units." )
66
67 vlc_module_begin();
68     set_description( _("GnomeVFS filesystem file input") );
69     set_shortname( _("GnomeVFS") );
70     set_category( CAT_INPUT );
71     set_subcategory( SUBCAT_INPUT_ACCESS );
72     add_integer( "gnomevfs-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
73     set_capability( "access2", 10 );
74     add_shortcut( "gnomevfs" );
75     set_callbacks( Open, Close );
76 vlc_module_end();
77
78
79 /*****************************************************************************
80  * Exported prototypes
81  *****************************************************************************/
82 static int  Seek( access_t *, int64_t );
83 static int  Read( access_t *, uint8_t *, int );
84 static int  Control( access_t *, int, va_list );
85
86 struct access_sys_t
87 {
88     unsigned int i_nb_reads;
89     char *psz_name;
90
91     GnomeVFSHandle *p_handle;
92     GnomeVFSFileInfo *p_file_info;
93
94     vlc_bool_t b_local;
95     vlc_bool_t b_seekable;
96     vlc_bool_t b_pace_control;
97 };
98
99 /*****************************************************************************
100  * Open: open the file
101  *****************************************************************************/
102 static int Open( vlc_object_t *p_this )
103 {
104     access_t     *p_access = (access_t*)p_this;
105     access_sys_t *p_sys;
106     char *psz_name;
107     char *psz, *psz_uri;
108     GnomeVFSURI         *p_uri;
109     GnomeVFSResult      i_ret;
110
111     if( !(gnome_vfs_init()) )
112     {
113         msg_Warn( p_access, "couldn't initilize GnomeVFS" );
114         return VLC_EGENERIC;
115     }
116
117     p_access->pf_read = Read;
118     p_access->pf_block = NULL;
119     p_access->pf_seek = Seek;
120     p_access->pf_control = Control;
121     p_access->info.i_update = 0;
122     p_access->info.i_size = 0;
123     p_access->info.i_pos = 0;
124     p_access->info.b_eof = VLC_FALSE;
125     p_access->info.i_title = 0;
126     p_access->info.i_seekpoint = 0;
127     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
128     p_sys->i_nb_reads = 0;
129     p_sys->b_pace_control = VLC_TRUE;
130
131
132     if( strcmp( "gnomevfs", p_access->psz_access ) )
133     {
134         psz_name = malloc( strlen( p_access->psz_access ) +
135                                             strlen( p_access->psz_path ) + 3 );
136         strcpy( psz_name, p_access->psz_access );
137         strcat( psz_name, "://" );
138         strcat( psz_name, p_access->psz_path );
139     }
140     else
141     {
142         psz_name = strdup( p_access->psz_path );
143     }
144
145     psz = ToLocale( psz_name );
146     psz_uri = gnome_vfs_make_uri_from_input_with_dirs( psz,
147                                     GNOME_VFS_MAKE_URI_DIR_CURRENT);
148     p_uri = gnome_vfs_uri_new( psz_uri );
149     msg_Dbg( p_access, "opening file `%s'", psz_name );
150
151     if( p_uri )
152     {
153         p_sys->p_file_info = gnome_vfs_file_info_new();
154         i_ret = gnome_vfs_get_file_info_uri( p_uri,
155                                                 p_sys->p_file_info, 8 ); 
156
157         if( i_ret )
158         {
159             msg_Err( p_access, "cannot get file info %s", 
160                                     gnome_vfs_result_to_string( i_ret ) );
161             gnome_vfs_file_info_unref( p_sys->p_file_info );
162             gnome_vfs_uri_unref( p_uri);
163             free( p_sys );
164             free( psz_name );
165             return VLC_EGENERIC;
166         }
167     }
168     else
169     {
170         msg_Warn( p_access, "cannot parse MRL %s", psz_name );
171         LocaleFree( psz );
172         g_free( psz_uri );
173         free( p_sys );
174         free( psz_name );
175         return VLC_EGENERIC;
176     }
177     LocaleFree( psz );
178
179     i_ret = gnome_vfs_open( &(p_sys->p_handle), psz_uri, 5 );
180     if( i_ret )
181     {
182         msg_Warn( p_access, "cannot open file %s: %s", psz_name,
183                                 gnome_vfs_result_to_string( i_ret ) );
184
185         LocaleFree( psz );
186         g_free( psz_uri );
187         gnome_vfs_uri_unref( p_uri);
188         free( p_sys );
189         free( psz_name );
190         return VLC_EGENERIC;
191     }
192
193     if (GNOME_VFS_FILE_INFO_LOCAL( p_sys->p_file_info ))
194     {
195         p_sys->b_local = VLC_TRUE;
196     }
197
198     if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_REGULAR || 
199         p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE ||
200         p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_BLOCK_DEVICE )
201     {
202         p_sys->b_seekable = VLC_TRUE;
203         p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
204     }
205     else if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_FIFO
206               || p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_SOCKET )
207     {
208         p_sys->b_seekable = VLC_FALSE;
209     }
210     else
211     {
212         msg_Err( p_access, "unknown file type for `%s'", psz_name );
213         return VLC_EGENERIC;
214     }
215
216     if( p_sys->b_seekable && !p_access->info.i_size )
217     {
218         /* FIXME that's bad because all others access will be probed */
219         msg_Err( p_access, "file %s is empty, aborting", psz_name );
220         gnome_vfs_file_info_unref( p_sys->p_file_info );
221         free( p_sys );
222         free( psz_name );
223         return VLC_EGENERIC;
224     }
225
226     /* Update default_pts to a suitable value for file access */
227     var_Create( p_access, "gnomevfs-caching",
228                                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
229
230     g_free( psz_uri );
231     p_sys->psz_name = psz_name;
232     gnome_vfs_uri_unref( p_uri);
233     return VLC_SUCCESS;
234
235 }
236
237 /*****************************************************************************
238  * Close: close the target
239  *****************************************************************************/
240 static void Close( vlc_object_t * p_this )
241 {
242     access_t     *p_access = (access_t*)p_this;
243     access_sys_t *p_sys = p_access->p_sys;
244     int i_result;
245
246     i_result = gnome_vfs_close( p_sys->p_handle );
247     if( i_result )
248     {
249          msg_Err( p_access, "cannot close %s: %s", p_sys->psz_name,
250                                 gnome_vfs_result_to_string( i_result ) );
251     }
252
253     gnome_vfs_file_info_unref( p_sys->p_file_info );
254
255     free( p_sys->psz_name );
256     free( p_sys );
257 }
258
259 /*****************************************************************************
260  * Read: standard read on a file descriptor.
261  *****************************************************************************/
262 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
263 {
264     access_sys_t *p_sys = p_access->p_sys;
265     int i_ret;
266     GnomeVFSFileSize i_read_len;
267
268     i_ret = gnome_vfs_read( p_sys->p_handle, p_buffer,
269                                   (GnomeVFSFileSize)i_len, &i_read_len );
270
271     if( i_ret )
272     {
273         p_access->info.b_eof = VLC_TRUE;
274         if( i_ret != GNOME_VFS_ERROR_EOF )
275         {
276             msg_Err( p_access, "read failed (%s)",
277                                     gnome_vfs_result_to_string( i_ret ) );
278         }
279     }
280     else
281     {
282         p_sys->i_nb_reads++;
283         if( p_access->info.i_size != 0 &&
284             (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 &&
285             p_sys->b_local )
286         {
287             gnome_vfs_file_info_clear( p_sys->p_file_info );
288             i_ret = gnome_vfs_get_file_info_from_handle( p_sys->p_handle,
289                                                 p_sys->p_file_info, 8 );
290             if( i_ret )
291             {
292                 msg_Warn( p_access, "couldn't get file properties again (%s)",
293                                         gnome_vfs_result_to_string( i_ret ) );
294             }
295             else
296             {
297                 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
298             }
299         }
300     }
301
302     p_access->info.i_pos += (int64_t)i_read_len;
303
304     return (int)i_read_len;
305 }
306
307 /*****************************************************************************
308  * Seek: seek to a specific location in a file
309  *****************************************************************************/
310 static int Seek( access_t *p_access, int64_t i_pos )
311 {
312     access_sys_t *p_sys = p_access->p_sys;
313     int i_ret;
314
315     i_ret = gnome_vfs_seek( p_sys->p_handle, GNOME_VFS_SEEK_START,
316                                             (GnomeVFSFileOffset)i_pos);
317
318     if ( !i_ret )
319     {
320         p_access->info.i_pos = i_pos;
321     }
322     else
323     {
324         GnomeVFSFileSize i_offset;
325         msg_Err( p_access, "cannot seek (%s)",
326                                         gnome_vfs_result_to_string( i_ret ) );
327         i_ret = gnome_vfs_tell( p_sys->p_handle, &i_offset );
328         if( !i_ret )
329         {
330             msg_Err( p_access, "cannot tell the current position (%s)",
331                                         gnome_vfs_result_to_string( i_ret ) );
332             return VLC_EGENERIC;
333         }
334     }
335     /* Reset eof */
336     p_access->info.b_eof = VLC_FALSE;
337
338     /* FIXME */
339     return VLC_SUCCESS;
340 }
341
342 /*****************************************************************************
343  * Control:
344  *****************************************************************************/
345 static int Control( access_t *p_access, int i_query, va_list args )
346 {
347     access_sys_t *p_sys = p_access->p_sys;
348     vlc_bool_t   *pb_bool;
349     int          *pi_int;
350     int64_t      *pi_64;
351
352     switch( i_query )
353     {
354         /* */
355         case ACCESS_CAN_SEEK:
356         case ACCESS_CAN_FASTSEEK:
357             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
358             *pb_bool = p_sys->b_seekable;
359             break;
360
361         case ACCESS_CAN_PAUSE:
362         case ACCESS_CAN_CONTROL_PACE:
363             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
364             *pb_bool = p_sys->b_pace_control;
365             break;
366
367         /* */
368         case ACCESS_GET_MTU:
369             pi_int = (int*)va_arg( args, int * );
370             *pi_int = 0;
371             break;
372
373         case ACCESS_GET_PTS_DELAY:
374             pi_64 = (int64_t*)va_arg( args, int64_t * );
375             *pi_64 = var_GetInteger( p_access,
376                                         "gnomevfs-caching" ) * I64C(1000);
377             break;
378
379         /* */
380         case ACCESS_SET_PAUSE_STATE:
381             /* Nothing to do */
382             break;
383
384         case ACCESS_GET_TITLE_INFO:
385         case ACCESS_SET_TITLE:
386         case ACCESS_SET_SEEKPOINT:
387         case ACCESS_SET_PRIVATE_ID_STATE:
388         case ACCESS_GET_META:
389             return VLC_EGENERIC;
390
391         default:
392             msg_Warn( p_access, "unimplemented query in control" );
393             return VLC_EGENERIC;
394
395     }
396     return VLC_SUCCESS;
397 }
398