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 /*****************************************************************************
28 *****************************************************************************/
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>
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
56 /*****************************************************************************
58 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
64 set_description( N_("MTP input") )
65 set_shortname( N_("MTP") )
66 set_category( CAT_INPUT )
67 set_subcategory( SUBCAT_INPUT_ACCESS )
68 set_capability( "access", 0 )
70 set_callbacks( Open, Close )
73 /*****************************************************************************
75 *****************************************************************************/
77 static int Seek( access_t *, uint64_t );
78 static ssize_t Read( access_t *, uint8_t *, size_t );
79 static int Control( access_t *, int, va_list );
81 static int open_file( access_t *, const char * );
85 unsigned int i_nb_reads;
89 /*****************************************************************************
91 *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
94 access_t *p_access = ( access_t* )p_this;
98 uint16_t i_product_id;
100 LIBMTP_raw_device_t *p_rawdevices;
101 LIBMTP_mtpdevice_t *p_device;
105 if( sscanf( p_access->psz_location, "%"SCNu32":%"SCNu8":%"SCNu16":%d",
106 &i_bus, &i_dev, &i_product_id, &i_track_id ) != 4 )
108 i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
109 if( i_ret != 0 || i_numrawdevices <= 0 || !p_rawdevices )
112 for( int i = 0; i < i_numrawdevices; i++ )
114 if( i_bus == p_rawdevices[i].bus_location &&
115 i_dev == p_rawdevices[i].devnum &&
116 i_product_id == p_rawdevices[i].device_entry.product_id )
118 if( ( p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] )
121 free( p_access->psz_filepath );
122 #warning Oooh no! Not tempnam()!
123 p_access->psz_filepath = tempnam( NULL, "vlc" );
124 if( p_access->psz_filepath == NULL )
126 LIBMTP_Release_Device( p_device );
127 free( p_rawdevices );
132 msg_Dbg( p_access, "About to write %s",
133 p_access->psz_filepath );
134 LIBMTP_Get_File_To_File( p_device, i_track_id,
135 p_access->psz_filepath, NULL,
137 LIBMTP_Release_Device( p_device );
143 free( p_rawdevices );
148 free( p_rawdevices );
150 STANDARD_READ_ACCESS_INIT;
151 p_sys->i_nb_reads = 0;
152 int fd = p_sys->fd = -1;
155 msg_Dbg( p_access, "opening file `%s'", p_access->psz_filepath );
156 fd = open_file( p_access, p_access->psz_filepath );
165 #ifdef HAVE_SYS_STAT_H
167 if( fstat( fd, &st ) )
168 msg_Err( p_access, "fstat(%d): %m", fd );
169 p_access->info.i_size = st.st_size;
171 # warning File size not known!
177 /*****************************************************************************
178 * Close: close the target
179 *****************************************************************************/
180 static void Close( vlc_object_t * p_this )
182 access_t *p_access = ( access_t* )p_this;
183 access_sys_t *p_sys = p_access->p_sys;
186 if( vlc_unlink( p_access->psz_filepath ) != 0 )
187 msg_Err( p_access, "Error deleting file %s, %m",
188 p_access->psz_filepath );
192 /*****************************************************************************
193 * Read: standard read on a file descriptor.
194 *****************************************************************************/
195 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
197 access_sys_t *p_sys = p_access->p_sys;
201 i_ret = read( fd, p_buffer, i_len );
212 msg_Err( p_access, "read failed (%m)" );
213 dialog_Fatal( p_access, _( "File reading failed" ), "%s (%m)",
214 _( "VLC could not read the file." ) );
215 p_access->info.b_eof = true;
220 p_access->info.i_pos += i_ret;
222 p_access->info.b_eof = true;
230 /*****************************************************************************
231 * Seek: seek to a specific location in a file
232 *****************************************************************************/
233 static int Seek( access_t *p_access, uint64_t i_pos )
235 p_access->info.i_pos = i_pos;
236 p_access->info.b_eof = false;
238 lseek( p_access->p_sys->fd, i_pos, SEEK_SET );
242 /*****************************************************************************
244 *****************************************************************************/
245 static int Control( access_t *p_access, int i_query, va_list args )
253 case ACCESS_CAN_SEEK:
254 case ACCESS_CAN_FASTSEEK:
255 pb_bool = ( bool* )va_arg( args, bool* );
259 case ACCESS_CAN_PAUSE:
260 case ACCESS_CAN_CONTROL_PACE:
261 pb_bool = ( bool* )va_arg( args, bool* );
265 case ACCESS_GET_PTS_DELAY:
266 pi_64 = ( int64_t* )va_arg( args, int64_t * );
267 *pi_64 = INT64_C(1000)
268 * var_InheritInteger( p_access, "file-caching" );
272 case ACCESS_SET_PAUSE_STATE:
276 case ACCESS_GET_TITLE_INFO:
277 case ACCESS_SET_TITLE:
278 case ACCESS_SET_SEEKPOINT:
279 case ACCESS_SET_PRIVATE_ID_STATE:
280 case ACCESS_GET_META:
281 case ACCESS_GET_PRIVATE_ID_STATE:
282 case ACCESS_GET_CONTENT_TYPE:
286 msg_Warn( p_access, "unimplemented query %d in control", i_query );
293 /*****************************************************************************
294 * open_file: Opens a specific file
295 *****************************************************************************/
296 static int open_file( access_t *p_access, const char *path )
298 int fd = vlc_open( path, O_RDONLY | O_NONBLOCK );
301 msg_Err( p_access, "cannot open file %s (%m)", path );
302 dialog_Fatal( p_access, _( "File reading failed" ),
303 _( "VLC could not open the file \"%s\". (%m)" ), path );
307 #if defined( HAVE_FCNTL )
308 fcntl( fd, F_SETFD, fcntl( fd, F_GETFD ) | FD_CLOEXEC );
310 /* We'd rather use any available memory for reading ahead
311 * than for caching what we've already seen/heard */
312 # if defined( F_RDAHEAD )
313 fcntl( fd, F_RDAHEAD, 1 );
315 # if defined( F_NOCACHE )
316 fcntl( fd, F_NOCACHE, 1 );