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 *****************************************************************************/
35 #include <sys/types.h>
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_input.h>
44 #include <vlc_access.h>
45 #include <vlc_dialog.h>
50 /*****************************************************************************
52 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
58 set_description( N_("MTP input") )
59 set_shortname( N_("MTP") )
60 set_category( CAT_INPUT )
61 set_subcategory( SUBCAT_INPUT_ACCESS )
62 set_capability( "access", 0 )
64 set_callbacks( Open, Close )
67 /*****************************************************************************
69 *****************************************************************************/
71 static int Seek( access_t *, uint64_t );
72 static ssize_t Read( access_t *, uint8_t *, size_t );
73 static int Control( access_t *, int, va_list );
75 static int open_file( access_t *, const char * );
79 unsigned int i_nb_reads;
83 /*****************************************************************************
85 *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
88 access_t *p_access = ( access_t* )p_this;
92 uint16_t i_product_id;
94 LIBMTP_raw_device_t *p_rawdevices;
95 LIBMTP_mtpdevice_t *p_device;
99 if( sscanf( p_access->psz_location, "%"SCNu32":%"SCNu8":%"SCNu16":%d",
100 &i_bus, &i_dev, &i_product_id, &i_track_id ) != 4 )
102 i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
103 if( i_ret != 0 || i_numrawdevices <= 0 || !p_rawdevices )
106 for( int i = 0; i < i_numrawdevices; i++ )
108 if( i_bus == p_rawdevices[i].bus_location &&
109 i_dev == p_rawdevices[i].devnum &&
110 i_product_id == p_rawdevices[i].device_entry.product_id )
112 if( ( p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] )
115 free( p_access->psz_filepath );
116 #warning Oooh no! Not tempnam()!
117 p_access->psz_filepath = tempnam( NULL, "vlc" );
118 if( p_access->psz_filepath == NULL )
120 LIBMTP_Release_Device( p_device );
121 free( p_rawdevices );
126 msg_Dbg( p_access, "About to write %s",
127 p_access->psz_filepath );
128 LIBMTP_Get_File_To_File( p_device, i_track_id,
129 p_access->psz_filepath, NULL,
131 LIBMTP_Release_Device( p_device );
137 free( p_rawdevices );
142 free( p_rawdevices );
144 STANDARD_READ_ACCESS_INIT;
145 p_sys->i_nb_reads = 0;
146 int fd = p_sys->fd = -1;
149 msg_Dbg( p_access, "opening file `%s'", p_access->psz_filepath );
150 fd = open_file( p_access, p_access->psz_filepath );
160 if( fstat( fd, &st ) )
161 msg_Err( p_access, "fstat(%d): %m", fd );
162 p_access->info.i_size = st.st_size;
167 /*****************************************************************************
168 * Close: close the target
169 *****************************************************************************/
170 static void Close( vlc_object_t * p_this )
172 access_t *p_access = ( access_t* )p_this;
173 access_sys_t *p_sys = p_access->p_sys;
176 if( vlc_unlink( p_access->psz_filepath ) != 0 )
177 msg_Err( p_access, "Error deleting file %s, %m",
178 p_access->psz_filepath );
182 /*****************************************************************************
183 * Read: standard read on a file descriptor.
184 *****************************************************************************/
185 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
187 access_sys_t *p_sys = p_access->p_sys;
191 i_ret = read( fd, p_buffer, i_len );
202 msg_Err( p_access, "read failed (%m)" );
203 dialog_Fatal( p_access, _( "File reading failed" ), "%s (%m)",
204 _( "VLC could not read the file." ) );
205 p_access->info.b_eof = true;
210 p_access->info.i_pos += i_ret;
212 p_access->info.b_eof = true;
220 /*****************************************************************************
221 * Seek: seek to a specific location in a file
222 *****************************************************************************/
223 static int Seek( access_t *p_access, uint64_t i_pos )
225 p_access->info.i_pos = i_pos;
226 p_access->info.b_eof = false;
228 lseek( p_access->p_sys->fd, i_pos, SEEK_SET );
232 /*****************************************************************************
234 *****************************************************************************/
235 static int Control( access_t *p_access, int i_query, va_list args )
243 case ACCESS_CAN_SEEK:
244 case ACCESS_CAN_FASTSEEK:
245 pb_bool = ( bool* )va_arg( args, bool* );
249 case ACCESS_CAN_PAUSE:
250 case ACCESS_CAN_CONTROL_PACE:
251 pb_bool = ( bool* )va_arg( args, bool* );
255 case ACCESS_GET_PTS_DELAY:
256 pi_64 = ( int64_t* )va_arg( args, int64_t * );
257 *pi_64 = INT64_C(1000)
258 * var_InheritInteger( p_access, "file-caching" );
262 case ACCESS_SET_PAUSE_STATE:
266 case ACCESS_GET_TITLE_INFO:
267 case ACCESS_SET_TITLE:
268 case ACCESS_SET_SEEKPOINT:
269 case ACCESS_SET_PRIVATE_ID_STATE:
270 case ACCESS_GET_META:
271 case ACCESS_GET_PRIVATE_ID_STATE:
272 case ACCESS_GET_CONTENT_TYPE:
276 msg_Warn( p_access, "unimplemented query %d in control", i_query );
283 /*****************************************************************************
284 * open_file: Opens a specific file
285 *****************************************************************************/
286 static int open_file( access_t *p_access, const char *path )
288 int fd = vlc_open( path, O_RDONLY | O_NONBLOCK );
291 msg_Err( p_access, "cannot open file %s (%m)", path );
292 dialog_Fatal( p_access, _( "File reading failed" ),
293 _( "VLC could not open the file \"%s\". (%m)" ), path );
297 #if defined( HAVE_FCNTL )
298 fcntl( fd, F_SETFD, fcntl( fd, F_GETFD ) | FD_CLOEXEC );
300 /* We'd rather use any available memory for reading ahead
301 * than for caching what we've already seen/heard */
302 # if defined( F_RDAHEAD )
303 fcntl( fd, F_RDAHEAD, 1 );
305 # if defined( F_NOCACHE )
306 fcntl( fd, F_NOCACHE, 1 );