On the road to 1.1.0-RC
[vlc/solaris.git] / modules / access / mtp.c
blob261c943dee1f9efdd6dece214dfafcfcbc4d1511
1 /*****************************************************************************
2 * mtp.c: mtp input (mtp: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
5 * Copyright © 2006-2008 Rémi Denis-Courmont
7 * Authors: Fabio Ritrovato <exsephiroth87@gmail.com>
8 * Original file.c: Christophe Massiot <massiot@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan # org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_dialog.h>
39 #include <assert.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
44 #endif
45 #ifdef HAVE_FCNTL_H
46 # include <fcntl.h>
47 #endif
49 #include <unistd.h>
50 #include <poll.h>
52 #include <vlc_fs.h>
54 #include "libmtp.h"
56 /*****************************************************************************
57 * Module descriptor
58 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 #define CACHING_TEXT N_("Caching value in ms")
64 #define CACHING_LONGTEXT N_( \
65 "Caching value for files. This " \
66 "value should be set in milliseconds." )
68 vlc_module_begin()
69 set_description( N_("MTP input") )
70 set_shortname( N_("MTP") )
71 set_category( CAT_INPUT )
72 set_subcategory( SUBCAT_INPUT_ACCESS )
73 set_capability( "access", 0 )
74 add_shortcut( "mtp" )
75 set_callbacks( Open, Close )
76 vlc_module_end()
78 /*****************************************************************************
79 * Exported prototypes
80 *****************************************************************************/
82 static int Seek( access_t *, uint64_t );
83 static ssize_t Read( access_t *, uint8_t *, size_t );
84 static int Control( access_t *, int, va_list );
86 static int open_file( access_t *, const char * );
88 struct access_sys_t
90 unsigned int i_nb_reads;
91 int fd;
94 /*****************************************************************************
95 * Open: open the file
96 *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
99 access_t *p_access = ( access_t* )p_this;
100 access_sys_t *p_sys;
101 uint32_t i_bus;
102 uint8_t i_dev;
103 uint16_t i_product_id;
104 int i_track_id;
105 LIBMTP_raw_device_t *p_rawdevices;
106 LIBMTP_mtpdevice_t *p_device;
107 int i_numrawdevices;
108 int i_ret;
110 /* Update default_pts to a suitable value for file access */
111 var_Create( p_access, "file-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
113 if( sscanf( p_access->psz_path, "%"SCNu32":%"SCNu8":%"SCNu16":%d", &i_bus,
114 &i_dev, &i_product_id, &i_track_id ) != 4 )
115 return VLC_EGENERIC;
116 i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
117 if( i_ret != 0 || i_numrawdevices <= 0 || !p_rawdevices )
118 return VLC_EGENERIC;
120 for( int i = 0; i < i_numrawdevices; i++ )
122 if( i_bus == p_rawdevices[i].bus_location &&
123 i_dev == p_rawdevices[i].devnum &&
124 i_product_id == p_rawdevices[i].device_entry.product_id )
126 if( ( p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] )
127 ) != NULL )
129 free( p_access->psz_path );
130 if( ( p_access->psz_path = tempnam( NULL, "vlc" ) ) == NULL )
132 LIBMTP_Release_Device( p_device );
133 free( p_rawdevices );
134 return VLC_ENOMEM;
136 else
138 msg_Dbg( p_access, "About to write %s", p_access->psz_path );
139 LIBMTP_Get_File_To_File( p_device, i_track_id,
140 p_access->psz_path, NULL, NULL );
141 LIBMTP_Release_Device( p_device );
142 i = i_numrawdevices;
145 else
147 free( p_rawdevices );
148 return VLC_EGENERIC;
152 free( p_rawdevices );
154 STANDARD_READ_ACCESS_INIT;
155 p_sys->i_nb_reads = 0;
156 int fd = p_sys->fd = -1;
158 /* Open file */
159 msg_Dbg( p_access, "opening file `%s'", p_access->psz_path );
160 fd = open_file( p_access, p_access->psz_path );
162 if( fd == -1 )
164 free( p_sys );
165 return VLC_EGENERIC;
167 p_sys->fd = fd;
169 #ifdef HAVE_SYS_STAT_H
170 struct stat st;
171 if( fstat( fd, &st ) )
172 msg_Err( p_access, "fstat(%d): %m", fd );
173 p_access->info.i_size = st.st_size;
174 #else
175 # warning File size not known!
176 #endif
178 return VLC_SUCCESS;
181 /*****************************************************************************
182 * Close: close the target
183 *****************************************************************************/
184 static void Close( vlc_object_t * p_this )
186 access_t *p_access = ( access_t* )p_this;
187 access_sys_t *p_sys = p_access->p_sys;
189 close ( p_sys->fd );
190 if( vlc_unlink( p_access->psz_path ) != 0 )
191 msg_Err( p_access, "Error deleting file %s, %m", p_access->psz_path );
192 free( p_sys );
195 /*****************************************************************************
196 * Read: standard read on a file descriptor.
197 *****************************************************************************/
198 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
200 access_sys_t *p_sys = p_access->p_sys;
201 ssize_t i_ret;
202 int fd = p_sys->fd;
204 i_ret = read( fd, p_buffer, i_len );
206 if( i_ret < 0 )
208 switch( errno )
210 case EINTR:
211 case EAGAIN:
212 break;
214 default:
215 msg_Err( p_access, "read failed (%m)" );
216 dialog_Fatal( p_access, _( "File reading failed" ), "%s",
217 _( "VLC could not read the file." ) );
218 p_access->info.b_eof = true;
219 return 0;
222 else if( i_ret > 0 )
223 p_access->info.i_pos += i_ret;
224 else
225 p_access->info.b_eof = true;
227 p_sys->i_nb_reads++;
229 return i_ret;
233 /*****************************************************************************
234 * Seek: seek to a specific location in a file
235 *****************************************************************************/
236 static int Seek( access_t *p_access, uint64_t i_pos )
238 p_access->info.i_pos = i_pos;
239 p_access->info.b_eof = false;
241 lseek( p_access->p_sys->fd, i_pos, SEEK_SET );
242 return VLC_SUCCESS;
245 /*****************************************************************************
246 * Control:
247 *****************************************************************************/
248 static int Control( access_t *p_access, int i_query, va_list args )
250 bool *pb_bool;
251 int64_t *pi_64;
253 switch( i_query )
255 /* */
256 case ACCESS_CAN_SEEK:
257 case ACCESS_CAN_FASTSEEK:
258 pb_bool = ( bool* )va_arg( args, bool* );
259 *pb_bool = true;
260 break;
262 case ACCESS_CAN_PAUSE:
263 case ACCESS_CAN_CONTROL_PACE:
264 pb_bool = ( bool* )va_arg( args, bool* );
265 *pb_bool = true;
266 break;
268 case ACCESS_GET_PTS_DELAY:
269 pi_64 = ( int64_t* )va_arg( args, int64_t * );
270 *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C( 1000 );
271 break;
273 /* */
274 case ACCESS_SET_PAUSE_STATE:
275 /* Nothing to do */
276 break;
278 case ACCESS_GET_TITLE_INFO:
279 case ACCESS_SET_TITLE:
280 case ACCESS_SET_SEEKPOINT:
281 case ACCESS_SET_PRIVATE_ID_STATE:
282 case ACCESS_GET_META:
283 case ACCESS_GET_PRIVATE_ID_STATE:
284 case ACCESS_GET_CONTENT_TYPE:
285 return VLC_EGENERIC;
287 default:
288 msg_Warn( p_access, "unimplemented query %d in control", i_query );
289 return VLC_EGENERIC;
292 return VLC_SUCCESS;
295 /*****************************************************************************
296 * open_file: Opens a specific file
297 *****************************************************************************/
298 static int open_file( access_t *p_access, const char *path )
300 int fd = vlc_open( path, O_RDONLY | O_NONBLOCK );
301 if( fd == -1 )
303 msg_Err( p_access, "cannot open file %s (%m)", path );
304 dialog_Fatal( p_access, _( "File reading failed" ),
305 _( "VLC could not open the file \"%s\"." ), path );
306 return -1;
309 #if defined( HAVE_FCNTL )
310 fcntl( fd, F_SETFD, fcntl( fd, F_GETFD ) | FD_CLOEXEC );
312 /* We'd rather use any available memory for reading ahead
313 * than for caching what we've already seen/heard */
314 # if defined( F_RDAHEAD )
315 fcntl( fd, F_RDAHEAD, 1 );
316 # endif
317 # if defined( F_NOCACHE )
318 fcntl( fd, F_NOCACHE, 1 );
319 # endif
320 #endif
322 return fd;