]> git.sesse.net Git - vlc/blob - modules/access/gnomevfs.c
* New gnomevfs access module
[vlc] / modules / access / gnomevfs.c
1 /*****************************************************************************
2  * file.c: file input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id: file.c 12318 2005-08-21 17:46:48Z damienf $
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", 0 );
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 = strdup( p_access->psz_path );
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     psz = ToLocale( psz_name );
132     psz_uri = gnome_vfs_make_uri_from_input_with_dirs( psz,
133                                     GNOME_VFS_MAKE_URI_DIR_CURRENT);
134     p_uri = gnome_vfs_uri_new( psz_uri );
135     msg_Dbg( p_access, "opening file `%s'", psz_name );
136
137     if( p_uri )
138     {
139         i_ret = gnome_vfs_open( &(p_sys->p_handle), psz_uri, 5 );
140         if( i_ret )
141         {
142             msg_Warn( p_access, "cannot open file %s: %s", psz_name,
143                                     gnome_vfs_result_to_string( i_ret ) );
144
145             LocaleFree( psz );
146             g_free( psz_uri );
147             gnome_vfs_uri_unref( p_uri);
148             free( p_sys );
149             free( psz_name );
150             return VLC_EGENERIC;
151         }
152         gnome_vfs_uri_unref( p_uri);
153     }
154     else
155     {
156         msg_Warn( p_access, "cannot parse MRL %s", psz_name );
157         LocaleFree( psz );
158         g_free( psz_uri );
159         free( p_sys );
160         free( psz_name );
161         return VLC_EGENERIC;
162     }
163     LocaleFree( psz );
164     g_free( psz_uri );
165
166     p_sys->p_file_info = gnome_vfs_file_info_new();
167     i_ret = gnome_vfs_get_file_info_from_handle( p_sys->p_handle,
168                                                 p_sys->p_file_info, 8 ); 
169
170     if( i_ret )
171     {
172         msg_Err( p_access, "cannot get file info %s", 
173                                     gnome_vfs_result_to_string( i_ret ) );
174         gnome_vfs_file_info_unref( p_sys->p_file_info );
175         free( p_sys );
176         free( psz_name );
177         return VLC_EGENERIC;
178     }
179
180     if (GNOME_VFS_FILE_INFO_LOCAL( p_sys->p_file_info ))
181     {
182         p_sys->b_local = VLC_TRUE;
183     }
184
185     if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_REGULAR || 
186         p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE ||
187         p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_BLOCK_DEVICE )
188     {
189         p_sys->b_seekable = VLC_TRUE;
190         p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
191     }
192     else if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_FIFO
193               || p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_SOCKET )
194     {
195         p_sys->b_seekable = VLC_FALSE;
196     }
197     else
198     {
199         msg_Err( p_access, "unknown file type for `%s'", psz_name );
200         return VLC_EGENERIC;
201     }
202
203     if( p_sys->b_seekable && !p_access->info.i_size )
204     {
205         /* FIXME that's bad because all others access will be probed */
206         msg_Err( p_access, "file %s is empty, aborting", psz_name );
207         gnome_vfs_file_info_unref( p_sys->p_file_info );
208         free( p_sys );
209         free( psz_name );
210         return VLC_EGENERIC;
211     }
212
213     /* Update default_pts to a suitable value for file access */
214     var_Create( p_access, "gnomevfs-caching",
215                                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
216
217     p_sys->psz_name = psz_name;
218     return VLC_SUCCESS;
219
220 }
221
222 /*****************************************************************************
223  * Close: close the target
224  *****************************************************************************/
225 static void Close( vlc_object_t * p_this )
226 {
227     access_t     *p_access = (access_t*)p_this;
228     access_sys_t *p_sys = p_access->p_sys;
229     int i_result;
230
231     i_result = gnome_vfs_close( p_sys->p_handle );
232     if( i_result )
233     {
234          msg_Err( p_access, "cannot close %s: %s", p_sys->psz_name,
235                                 gnome_vfs_result_to_string( i_result ) );
236     }
237
238     gnome_vfs_file_info_unref( p_sys->p_file_info );
239
240     free( p_sys->psz_name );
241     free( p_sys );
242 }
243
244 /*****************************************************************************
245  * Read: standard read on a file descriptor.
246  *****************************************************************************/
247 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
248 {
249     access_sys_t *p_sys = p_access->p_sys;
250     int i_ret;
251     GnomeVFSFileSize i_read_len;
252
253     i_ret = gnome_vfs_read( p_sys->p_handle, p_buffer,
254                                   (GnomeVFSFileSize)i_len, &i_read_len );
255
256     if( i_ret )
257     {
258         p_access->info.b_eof = VLC_TRUE;
259         if( i_ret != GNOME_VFS_ERROR_EOF )
260         {
261             msg_Err( p_access, "read failed (%s)",
262                                     gnome_vfs_result_to_string( i_ret ) );
263         }
264     }
265     else
266     {
267         p_sys->i_nb_reads++;
268         if( p_access->info.i_size != 0 &&
269             (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 &&
270             p_sys->b_local )
271         {
272             gnome_vfs_file_info_clear( p_sys->p_file_info );
273             i_ret = gnome_vfs_get_file_info_from_handle( p_sys->p_handle,
274                                                 p_sys->p_file_info, 8 );
275             if( i_ret )
276             {
277                 msg_Warn( p_access, "couldn't get file properties again (%s)",
278                                         gnome_vfs_result_to_string( i_ret ) );
279             }
280             else
281             {
282                 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
283             }
284         }
285     }
286
287     p_access->info.i_pos += (int64_t)i_read_len;
288
289     return (int)i_read_len;
290 }
291
292 /*****************************************************************************
293  * Seek: seek to a specific location in a file
294  *****************************************************************************/
295 static int Seek( access_t *p_access, int64_t i_pos )
296 {
297     access_sys_t *p_sys = p_access->p_sys;
298     int i_ret;
299
300     i_ret = gnome_vfs_seek( p_sys->p_handle, GNOME_VFS_SEEK_START,
301                                             (GnomeVFSFileOffset)i_pos);
302
303     if ( !i_ret )
304     {
305         p_access->info.i_pos = i_pos;
306     }
307     else
308     {
309         GnomeVFSFileSize i_offset;
310         msg_Err( p_access, "cannot seek (%s)",
311                                         gnome_vfs_result_to_string( i_ret ) );
312         i_ret = gnome_vfs_tell( p_sys->p_handle, &i_offset );
313         if( !i_ret )
314         {
315             msg_Err( p_access, "cannot tell the current position (%s)",
316                                         gnome_vfs_result_to_string( i_ret ) );
317             return VLC_EGENERIC;
318         }
319     }
320     /* Reset eof */
321     p_access->info.b_eof = VLC_FALSE;
322
323     /* FIXME */
324     return VLC_SUCCESS;
325 }
326
327 /*****************************************************************************
328  * Control:
329  *****************************************************************************/
330 static int Control( access_t *p_access, int i_query, va_list args )
331 {
332     access_sys_t *p_sys = p_access->p_sys;
333     vlc_bool_t   *pb_bool;
334     int          *pi_int;
335     int64_t      *pi_64;
336
337     switch( i_query )
338     {
339         /* */
340         case ACCESS_CAN_SEEK:
341         case ACCESS_CAN_FASTSEEK:
342             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
343             *pb_bool = p_sys->b_seekable;
344             break;
345
346         case ACCESS_CAN_PAUSE:
347         case ACCESS_CAN_CONTROL_PACE:
348             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
349             *pb_bool = p_sys->b_pace_control;
350             break;
351
352         /* */
353         case ACCESS_GET_MTU:
354             pi_int = (int*)va_arg( args, int * );
355             *pi_int = 0;
356             break;
357
358         case ACCESS_GET_PTS_DELAY:
359             pi_64 = (int64_t*)va_arg( args, int64_t * );
360             *pi_64 = var_GetInteger( p_access,
361                                         "gnomevfs-caching" ) * I64C(1000);
362             break;
363
364         /* */
365         case ACCESS_SET_PAUSE_STATE:
366             /* Nothing to do */
367             break;
368
369         case ACCESS_GET_TITLE_INFO:
370         case ACCESS_SET_TITLE:
371         case ACCESS_SET_SEEKPOINT:
372         case ACCESS_SET_PRIVATE_ID_STATE:
373         case ACCESS_GET_META:
374             return VLC_EGENERIC;
375
376         default:
377             msg_Warn( p_access, "unimplemented query in control" );
378             return VLC_EGENERIC;
379
380     }
381     return VLC_SUCCESS;
382 }
383