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>
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
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." )
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 );
79 /*****************************************************************************
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 );
88 unsigned int i_nb_reads;
91 GnomeVFSHandle *p_handle;
92 GnomeVFSFileInfo *p_file_info;
95 vlc_bool_t b_seekable;
96 vlc_bool_t b_pace_control;
99 /*****************************************************************************
100 * Open: open the file
101 *****************************************************************************/
102 static int Open( vlc_object_t *p_this )
104 access_t *p_access = (access_t*)p_this;
109 GnomeVFSResult i_ret;
111 if( !(gnome_vfs_init()) )
113 msg_Warn( p_access, "couldn't initilize GnomeVFS" );
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;
132 if( strcmp( "gnomevfs", p_access->psz_access ) &&
133 *(p_access->psz_access) != '\0')
135 psz_name = malloc( strlen( p_access->psz_access ) +
136 strlen( p_access->psz_path ) + 3 );
137 strcpy( psz_name, p_access->psz_access );
138 strcat( psz_name, "://" );
139 strcat( psz_name, p_access->psz_path );
143 psz_name = strdup( p_access->psz_path );
146 psz = ToLocale( psz_name );
147 psz_uri = gnome_vfs_make_uri_from_input_with_dirs( psz,
148 GNOME_VFS_MAKE_URI_DIR_CURRENT);
149 p_uri = gnome_vfs_uri_new( psz_uri );
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 );
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);
170 msg_Warn( p_access, "cannot parse MRL %s", psz_name );
179 msg_Dbg( p_access, "opening file `%s'", psz_name );
180 i_ret = gnome_vfs_open( &(p_sys->p_handle), psz_uri, 5 );
183 msg_Warn( p_access, "cannot open file %s: %s", psz_name,
184 gnome_vfs_result_to_string( i_ret ) );
188 gnome_vfs_uri_unref( p_uri);
194 if (GNOME_VFS_FILE_INFO_LOCAL( p_sys->p_file_info ))
196 p_sys->b_local = VLC_TRUE;
199 if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_REGULAR ||
200 p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE ||
201 p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_BLOCK_DEVICE )
203 p_sys->b_seekable = VLC_TRUE;
204 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
206 else if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_FIFO
207 || p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_SOCKET )
209 p_sys->b_seekable = VLC_FALSE;
213 msg_Err( p_access, "unknown file type for `%s'", psz_name );
217 if( p_sys->b_seekable && !p_access->info.i_size )
219 /* FIXME that's bad because all others access will be probed */
220 msg_Err( p_access, "file %s is empty, aborting", psz_name );
221 gnome_vfs_file_info_unref( p_sys->p_file_info );
227 /* Update default_pts to a suitable value for file access */
228 var_Create( p_access, "gnomevfs-caching",
229 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
232 p_sys->psz_name = psz_name;
233 gnome_vfs_uri_unref( p_uri);
238 /*****************************************************************************
239 * Close: close the target
240 *****************************************************************************/
241 static void Close( vlc_object_t * p_this )
243 access_t *p_access = (access_t*)p_this;
244 access_sys_t *p_sys = p_access->p_sys;
247 i_result = gnome_vfs_close( p_sys->p_handle );
250 msg_Err( p_access, "cannot close %s: %s", p_sys->psz_name,
251 gnome_vfs_result_to_string( i_result ) );
254 gnome_vfs_file_info_unref( p_sys->p_file_info );
256 free( p_sys->psz_name );
260 /*****************************************************************************
261 * Read: standard read on a file descriptor.
262 *****************************************************************************/
263 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
265 access_sys_t *p_sys = p_access->p_sys;
267 GnomeVFSFileSize i_read_len;
269 i_ret = gnome_vfs_read( p_sys->p_handle, p_buffer,
270 (GnomeVFSFileSize)i_len, &i_read_len );
274 p_access->info.b_eof = VLC_TRUE;
275 if( i_ret != GNOME_VFS_ERROR_EOF )
277 msg_Err( p_access, "read failed (%s)",
278 gnome_vfs_result_to_string( i_ret ) );
284 if( p_access->info.i_size != 0 &&
285 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 &&
288 gnome_vfs_file_info_clear( p_sys->p_file_info );
289 i_ret = gnome_vfs_get_file_info_from_handle( p_sys->p_handle,
290 p_sys->p_file_info, 8 );
293 msg_Warn( p_access, "couldn't get file properties again (%s)",
294 gnome_vfs_result_to_string( i_ret ) );
298 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
303 p_access->info.i_pos += (int64_t)i_read_len;
305 return (int)i_read_len;
308 /*****************************************************************************
309 * Seek: seek to a specific location in a file
310 *****************************************************************************/
311 static int Seek( access_t *p_access, int64_t i_pos )
313 access_sys_t *p_sys = p_access->p_sys;
316 i_ret = gnome_vfs_seek( p_sys->p_handle, GNOME_VFS_SEEK_START,
317 (GnomeVFSFileOffset)i_pos);
321 p_access->info.i_pos = i_pos;
325 GnomeVFSFileSize i_offset;
326 msg_Err( p_access, "cannot seek (%s)",
327 gnome_vfs_result_to_string( i_ret ) );
328 i_ret = gnome_vfs_tell( p_sys->p_handle, &i_offset );
331 msg_Err( p_access, "cannot tell the current position (%s)",
332 gnome_vfs_result_to_string( i_ret ) );
337 p_access->info.b_eof = VLC_FALSE;
343 /*****************************************************************************
345 *****************************************************************************/
346 static int Control( access_t *p_access, int i_query, va_list args )
348 access_sys_t *p_sys = p_access->p_sys;
356 case ACCESS_CAN_SEEK:
357 case ACCESS_CAN_FASTSEEK:
358 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
359 *pb_bool = p_sys->b_seekable;
362 case ACCESS_CAN_PAUSE:
363 case ACCESS_CAN_CONTROL_PACE:
364 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
365 *pb_bool = p_sys->b_pace_control;
370 pi_int = (int*)va_arg( args, int * );
374 case ACCESS_GET_PTS_DELAY:
375 pi_64 = (int64_t*)va_arg( args, int64_t * );
376 *pi_64 = var_GetInteger( p_access,
377 "gnomevfs-caching" ) * I64C(1000);
381 case ACCESS_SET_PAUSE_STATE:
385 case ACCESS_GET_TITLE_INFO:
386 case ACCESS_SET_TITLE:
387 case ACCESS_SET_SEEKPOINT:
388 case ACCESS_SET_PRIVATE_ID_STATE:
389 case ACCESS_GET_META:
393 msg_Warn( p_access, "unimplemented query in control" );