1 /*****************************************************************************
2 * gnomevfs.c: GnomeVFS input
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Benjamin Pracht <bigben -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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/input.h>
29 #include <libgnomevfs/gnome-vfs.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
43 #define CACHING_TEXT N_("Caching value in ms")
44 #define CACHING_LONGTEXT N_( \
45 "Allows you to modify the default caching value for GnomeVFS streams."\
46 "This value should be set in millisecond units." )
49 set_description( _("GnomeVFS filesystem file input") );
50 set_shortname( _("GnomeVFS") );
51 set_category( CAT_INPUT );
52 set_subcategory( SUBCAT_INPUT_ACCESS );
53 add_integer( "gnomevfs-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
54 set_capability( "access2", 10 );
55 add_shortcut( "gnomevfs" );
56 set_callbacks( Open, Close );
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Seek( access_t *, int64_t );
64 static int Read( access_t *, uint8_t *, int );
65 static int Control( access_t *, int, va_list );
69 unsigned int i_nb_reads;
72 GnomeVFSHandle *p_handle;
73 GnomeVFSFileInfo *p_file_info;
76 vlc_bool_t b_seekable;
77 vlc_bool_t b_pace_control;
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
85 access_t *p_access = (access_t*)p_this;
86 access_sys_t *p_sys = NULL;
87 char *psz_name = NULL;
90 GnomeVFSURI *p_uri = NULL;
92 GnomeVFSHandle *p_handle = NULL;
94 if( !(gnome_vfs_init()) )
96 msg_Warn( p_access, "couldn't initilize GnomeVFS" );
101 Since GnomeVFS segfaults on exit if we initialize it without trying to
102 open a file with a valid protocol, try to open at least file:// */
103 gnome_vfs_open( &p_handle, "file://", 5 );
105 p_access->pf_read = Read;
106 p_access->pf_block = NULL;
107 p_access->pf_seek = Seek;
108 p_access->pf_control = Control;
109 p_access->info.i_update = 0;
110 p_access->info.i_size = 0;
111 p_access->info.i_pos = 0;
112 p_access->info.b_eof = VLC_FALSE;
113 p_access->info.i_title = 0;
114 p_access->info.i_seekpoint = 0;
116 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
120 p_sys->p_handle = p_handle;
121 p_sys->i_nb_reads = 0;
122 p_sys->b_pace_control = VLC_TRUE;
124 if( strcmp( "gnomevfs", p_access->psz_access ) &&
125 *(p_access->psz_access) != '\0')
127 psz_name = malloc( strlen( p_access->psz_access ) +
128 strlen( p_access->psz_path ) + 3 );
129 strcpy( psz_name, p_access->psz_access );
130 strcat( psz_name, "://" );
131 strcat( psz_name, p_access->psz_path );
135 psz_name = strdup( p_access->psz_path );
138 psz = ToLocale( psz_name );
140 /* Use gnome_vfs_make_uri_from_input_with_dirs for local paths, as it deals
141 relative directories, and gnome_vfs_make_uri_from_input_with_dirs
143 psz_uri = gnome_vfs_make_uri_from_input_with_dirs( psz,
144 GNOME_VFS_MAKE_URI_DIR_CURRENT);
145 if( *psz_uri != '/' )
148 psz_uri = gnome_vfs_escape_host_and_path_string( psz );
151 p_uri = gnome_vfs_uri_new( psz_uri );
154 p_sys->p_file_info = gnome_vfs_file_info_new();
155 i_ret = gnome_vfs_get_file_info_uri( p_uri,
156 p_sys->p_file_info, 8 );
160 msg_Err( p_access, "cannot get file info (%s)",
161 gnome_vfs_result_to_string( i_ret ) );
162 gnome_vfs_file_info_unref( p_sys->p_file_info );
163 gnome_vfs_uri_unref( p_uri);
172 msg_Warn( p_access, "cannot parse MRL %s or unsupported protocol", psz_name );
181 msg_Dbg( p_access, "opening file `%s'", psz_uri );
182 i_ret = gnome_vfs_open( &(p_sys->p_handle), psz_uri, 5 );
185 msg_Warn( p_access, "cannot open file %s: %s", psz_uri,
186 gnome_vfs_result_to_string( i_ret ) );
190 gnome_vfs_uri_unref( p_uri);
197 if (GNOME_VFS_FILE_INFO_LOCAL( p_sys->p_file_info ))
199 p_sys->b_local = VLC_TRUE;
202 if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_REGULAR ||
203 p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE ||
204 p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_BLOCK_DEVICE )
206 p_sys->b_seekable = VLC_TRUE;
207 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
209 else if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_FIFO
210 || p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_SOCKET )
212 p_sys->b_seekable = VLC_FALSE;
216 msg_Err( p_access, "unknown file type for `%s'", psz_name );
220 if( p_sys->b_seekable && !p_access->info.i_size )
222 /* FIXME that's bad because all others access will be probed */
223 msg_Err( p_access, "file %s is empty, aborting", psz_name );
224 gnome_vfs_file_info_unref( p_sys->p_file_info );
231 /* Update default_pts to a suitable value for file access */
232 var_Create( p_access, "gnomevfs-caching",
233 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
236 p_sys->psz_name = psz_name;
237 gnome_vfs_uri_unref( p_uri);
241 /*****************************************************************************
242 * Close: close the target
243 *****************************************************************************/
244 static void Close( vlc_object_t * p_this )
246 access_t *p_access = (access_t*)p_this;
247 access_sys_t *p_sys = p_access->p_sys;
250 i_result = gnome_vfs_close( p_sys->p_handle );
253 msg_Err( p_access, "cannot close %s: %s", p_sys->psz_name,
254 gnome_vfs_result_to_string( i_result ) );
257 gnome_vfs_file_info_unref( p_sys->p_file_info );
259 free( p_sys->psz_name );
263 /*****************************************************************************
264 * Read: standard read on a file descriptor.
265 *****************************************************************************/
266 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
268 access_sys_t *p_sys = p_access->p_sys;
269 GnomeVFSFileSize i_read_len;
272 i_ret = gnome_vfs_read( p_sys->p_handle, p_buffer,
273 (GnomeVFSFileSize)i_len, &i_read_len );
276 p_access->info.b_eof = VLC_TRUE;
277 if( i_ret != GNOME_VFS_ERROR_EOF )
279 msg_Err( p_access, "read failed (%s)",
280 gnome_vfs_result_to_string( i_ret ) );
286 if( p_access->info.i_size != 0 &&
287 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 &&
290 gnome_vfs_file_info_clear( p_sys->p_file_info );
291 i_ret = gnome_vfs_get_file_info_from_handle( p_sys->p_handle,
292 p_sys->p_file_info, 8 );
295 msg_Warn( p_access, "couldn't get file properties again (%s)",
296 gnome_vfs_result_to_string( i_ret ) );
300 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
305 p_access->info.i_pos += (int64_t)i_read_len;
307 /* Some Acces (http) never return EOF and loop on the file */
308 if( p_access->info.i_pos > p_access->info.i_size )
310 p_access->info.b_eof = VLC_TRUE;
313 return (int)i_read_len;
316 /*****************************************************************************
317 * Seek: seek to a specific location in a file
318 *****************************************************************************/
319 static int Seek( access_t *p_access, int64_t i_pos )
321 access_sys_t *p_sys = p_access->p_sys;
324 i_ret = gnome_vfs_seek( p_sys->p_handle, GNOME_VFS_SEEK_START,
325 (GnomeVFSFileOffset)i_pos);
328 p_access->info.i_pos = i_pos;
332 GnomeVFSFileSize i_offset;
333 msg_Err( p_access, "cannot seek (%s)",
334 gnome_vfs_result_to_string( i_ret ) );
335 i_ret = gnome_vfs_tell( p_sys->p_handle, &i_offset );
338 msg_Err( p_access, "cannot tell the current position (%s)",
339 gnome_vfs_result_to_string( i_ret ) );
344 p_access->info.b_eof = VLC_FALSE;
350 /*****************************************************************************
352 *****************************************************************************/
353 static int Control( access_t *p_access, int i_query, va_list args )
355 access_sys_t *p_sys = p_access->p_sys;
363 case ACCESS_CAN_SEEK:
364 case ACCESS_CAN_FASTSEEK:
365 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
366 *pb_bool = p_sys->b_seekable;
369 case ACCESS_CAN_PAUSE:
370 case ACCESS_CAN_CONTROL_PACE:
371 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
372 *pb_bool = p_sys->b_pace_control;
377 pi_int = (int*)va_arg( args, int * );
381 case ACCESS_GET_PTS_DELAY:
382 pi_64 = (int64_t*)va_arg( args, int64_t * );
383 *pi_64 = var_GetInteger( p_access,
384 "gnomevfs-caching" ) * I64C(1000);
388 case ACCESS_SET_PAUSE_STATE:
392 case ACCESS_GET_TITLE_INFO:
393 case ACCESS_SET_TITLE:
394 case ACCESS_SET_SEEKPOINT:
395 case ACCESS_SET_PRIVATE_ID_STATE:
396 case ACCESS_GET_META:
400 msg_Warn( p_access, "unimplemented query in control" );