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;
92 if( !(gnome_vfs_init()) )
94 msg_Warn( p_access, "couldn't initilize GnomeVFS" );
98 Since GnomeVFS segfaults on exit if we initialize it without trying to
99 open a file with a valid protocol, try top open at least file:// */
100 gnome_vfs_open( &(p_sys->p_handle), "file://", 5 );
102 p_access->pf_read = Read;
103 p_access->pf_block = NULL;
104 p_access->pf_seek = Seek;
105 p_access->pf_control = Control;
106 p_access->info.i_update = 0;
107 p_access->info.i_size = 0;
108 p_access->info.i_pos = 0;
109 p_access->info.b_eof = VLC_FALSE;
110 p_access->info.i_title = 0;
111 p_access->info.i_seekpoint = 0;
112 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
113 p_sys->i_nb_reads = 0;
114 p_sys->b_pace_control = VLC_TRUE;
117 if( strcmp( "gnomevfs", p_access->psz_access ) &&
118 *(p_access->psz_access) != '\0')
120 psz_name = malloc( strlen( p_access->psz_access ) +
121 strlen( p_access->psz_path ) + 3 );
122 strcpy( psz_name, p_access->psz_access );
123 strcat( psz_name, "://" );
124 strcat( psz_name, p_access->psz_path );
128 psz_name = strdup( p_access->psz_path );
131 psz = ToLocale( psz_name );
133 /* Use gnome_vfs_make_uri_from_input_with_dirs for local paths, as it deals
134 relative directories, and gnome_vfs_make_uri_from_input_with_dirs
136 psz_uri = gnome_vfs_make_uri_from_input_with_dirs( psz,
137 GNOME_VFS_MAKE_URI_DIR_CURRENT);
138 if( *psz_uri != '/' )
141 psz_uri = gnome_vfs_escape_host_and_path_string( psz );
144 p_uri = gnome_vfs_uri_new( psz_uri );
148 p_sys->p_file_info = gnome_vfs_file_info_new();
149 i_ret = gnome_vfs_get_file_info_uri( p_uri,
150 p_sys->p_file_info, 8 );
154 msg_Err( p_access, "cannot get file info (%s)",
155 gnome_vfs_result_to_string( i_ret ) );
156 gnome_vfs_file_info_unref( p_sys->p_file_info );
157 gnome_vfs_uri_unref( p_uri);
166 msg_Warn( p_access, "cannot parse MRL %s or unsupported protocol", psz_name );
175 msg_Dbg( p_access, "opening file `%s'", psz_uri );
176 i_ret = gnome_vfs_open( &(p_sys->p_handle), psz_uri, 5 );
179 msg_Warn( p_access, "cannot open file %s: %s", psz_uri,
180 gnome_vfs_result_to_string( i_ret ) );
184 gnome_vfs_uri_unref( p_uri);
191 if (GNOME_VFS_FILE_INFO_LOCAL( p_sys->p_file_info ))
193 p_sys->b_local = VLC_TRUE;
196 if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_REGULAR ||
197 p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE ||
198 p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_BLOCK_DEVICE )
200 p_sys->b_seekable = VLC_TRUE;
201 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
203 else if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_FIFO
204 || p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_SOCKET )
206 p_sys->b_seekable = VLC_FALSE;
210 msg_Err( p_access, "unknown file type for `%s'", psz_name );
214 if( p_sys->b_seekable && !p_access->info.i_size )
216 /* FIXME that's bad because all others access will be probed */
217 msg_Err( p_access, "file %s is empty, aborting", psz_name );
218 gnome_vfs_file_info_unref( p_sys->p_file_info );
225 /* Update default_pts to a suitable value for file access */
226 var_Create( p_access, "gnomevfs-caching",
227 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
230 p_sys->psz_name = psz_name;
231 gnome_vfs_uri_unref( p_uri);
236 /*****************************************************************************
237 * Close: close the target
238 *****************************************************************************/
239 static void Close( vlc_object_t * p_this )
241 access_t *p_access = (access_t*)p_this;
242 access_sys_t *p_sys = p_access->p_sys;
245 i_result = gnome_vfs_close( p_sys->p_handle );
248 msg_Err( p_access, "cannot close %s: %s", p_sys->psz_name,
249 gnome_vfs_result_to_string( i_result ) );
252 gnome_vfs_file_info_unref( p_sys->p_file_info );
254 free( p_sys->psz_name );
258 /*****************************************************************************
259 * Read: standard read on a file descriptor.
260 *****************************************************************************/
261 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
263 access_sys_t *p_sys = p_access->p_sys;
265 GnomeVFSFileSize i_read_len;
267 i_ret = gnome_vfs_read( p_sys->p_handle, p_buffer,
268 (GnomeVFSFileSize)i_len, &i_read_len );
272 p_access->info.b_eof = VLC_TRUE;
273 if( i_ret != GNOME_VFS_ERROR_EOF )
275 msg_Err( p_access, "read failed (%s)",
276 gnome_vfs_result_to_string( i_ret ) );
282 if( p_access->info.i_size != 0 &&
283 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 &&
286 gnome_vfs_file_info_clear( p_sys->p_file_info );
287 i_ret = gnome_vfs_get_file_info_from_handle( p_sys->p_handle,
288 p_sys->p_file_info, 8 );
291 msg_Warn( p_access, "couldn't get file properties again (%s)",
292 gnome_vfs_result_to_string( i_ret ) );
296 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
301 p_access->info.i_pos += (int64_t)i_read_len;
303 /* Some Acces (http) never return EOF and loop on the file */
304 if( p_access->info.i_pos > p_access->info.i_size )
306 p_access->info.b_eof = VLC_TRUE;
309 return (int)i_read_len;
312 /*****************************************************************************
313 * Seek: seek to a specific location in a file
314 *****************************************************************************/
315 static int Seek( access_t *p_access, int64_t i_pos )
317 access_sys_t *p_sys = p_access->p_sys;
320 i_ret = gnome_vfs_seek( p_sys->p_handle, GNOME_VFS_SEEK_START,
321 (GnomeVFSFileOffset)i_pos);
325 p_access->info.i_pos = i_pos;
329 GnomeVFSFileSize i_offset;
330 msg_Err( p_access, "cannot seek (%s)",
331 gnome_vfs_result_to_string( i_ret ) );
332 i_ret = gnome_vfs_tell( p_sys->p_handle, &i_offset );
335 msg_Err( p_access, "cannot tell the current position (%s)",
336 gnome_vfs_result_to_string( i_ret ) );
341 p_access->info.b_eof = VLC_FALSE;
347 /*****************************************************************************
349 *****************************************************************************/
350 static int Control( access_t *p_access, int i_query, va_list args )
352 access_sys_t *p_sys = p_access->p_sys;
360 case ACCESS_CAN_SEEK:
361 case ACCESS_CAN_FASTSEEK:
362 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
363 *pb_bool = p_sys->b_seekable;
366 case ACCESS_CAN_PAUSE:
367 case ACCESS_CAN_CONTROL_PACE:
368 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
369 *pb_bool = p_sys->b_pace_control;
374 pi_int = (int*)va_arg( args, int * );
378 case ACCESS_GET_PTS_DELAY:
379 pi_64 = (int64_t*)va_arg( args, int64_t * );
380 *pi_64 = var_GetInteger( p_access,
381 "gnomevfs-caching" ) * I64C(1000);
385 case ACCESS_SET_PAUSE_STATE:
389 case ACCESS_GET_TITLE_INFO:
390 case ACCESS_SET_TITLE:
391 case ACCESS_SET_SEEKPOINT:
392 case ACCESS_SET_PRIVATE_ID_STATE:
393 case ACCESS_GET_META:
397 msg_Warn( p_access, "unimplemented query in control" );