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
* );
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." )
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 )
75 set_callbacks( Open
, Close
)
78 /*****************************************************************************
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 * );
90 unsigned int i_nb_reads
;
94 /*****************************************************************************
96 *****************************************************************************/
97 static int Open( vlc_object_t
*p_this
)
99 access_t
*p_access
= ( access_t
* )p_this
;
103 uint16_t i_product_id
;
105 LIBMTP_raw_device_t
*p_rawdevices
;
106 LIBMTP_mtpdevice_t
*p_device
;
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_location
, "%"SCNu32
":%"SCNu8
":%"SCNu16
":%d",
114 &i_bus
, &i_dev
, &i_product_id
, &i_track_id
) != 4 )
116 i_ret
= LIBMTP_Detect_Raw_Devices( &p_rawdevices
, &i_numrawdevices
);
117 if( i_ret
!= 0 || i_numrawdevices
<= 0 || !p_rawdevices
)
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
] )
129 free( p_access
->psz_filepath
);
130 #warning Oooh no! Not tempnam()!
131 p_access
->psz_filepath
= tempnam( NULL
, "vlc" );
132 if( p_access
->psz_filepath
== NULL
)
134 LIBMTP_Release_Device( p_device
);
135 free( p_rawdevices
);
140 msg_Dbg( p_access
, "About to write %s",
141 p_access
->psz_filepath
);
142 LIBMTP_Get_File_To_File( p_device
, i_track_id
,
143 p_access
->psz_filepath
, NULL
,
145 LIBMTP_Release_Device( p_device
);
151 free( p_rawdevices
);
156 free( p_rawdevices
);
158 STANDARD_READ_ACCESS_INIT
;
159 p_sys
->i_nb_reads
= 0;
160 int fd
= p_sys
->fd
= -1;
163 msg_Dbg( p_access
, "opening file `%s'", p_access
->psz_filepath
);
164 fd
= open_file( p_access
, p_access
->psz_filepath
);
173 #ifdef HAVE_SYS_STAT_H
175 if( fstat( fd
, &st
) )
176 msg_Err( p_access
, "fstat(%d): %m", fd
);
177 p_access
->info
.i_size
= st
.st_size
;
179 # warning File size not known!
185 /*****************************************************************************
186 * Close: close the target
187 *****************************************************************************/
188 static void Close( vlc_object_t
* p_this
)
190 access_t
*p_access
= ( access_t
* )p_this
;
191 access_sys_t
*p_sys
= p_access
->p_sys
;
194 if( vlc_unlink( p_access
->psz_filepath
) != 0 )
195 msg_Err( p_access
, "Error deleting file %s, %m",
196 p_access
->psz_filepath
);
200 /*****************************************************************************
201 * Read: standard read on a file descriptor.
202 *****************************************************************************/
203 static ssize_t
Read( access_t
*p_access
, uint8_t *p_buffer
, size_t i_len
)
205 access_sys_t
*p_sys
= p_access
->p_sys
;
209 i_ret
= read( fd
, p_buffer
, i_len
);
220 msg_Err( p_access
, "read failed (%m)" );
221 dialog_Fatal( p_access
, _( "File reading failed" ), "%s",
222 _( "VLC could not read the file." ) );
223 p_access
->info
.b_eof
= true;
228 p_access
->info
.i_pos
+= i_ret
;
230 p_access
->info
.b_eof
= true;
238 /*****************************************************************************
239 * Seek: seek to a specific location in a file
240 *****************************************************************************/
241 static int Seek( access_t
*p_access
, uint64_t i_pos
)
243 p_access
->info
.i_pos
= i_pos
;
244 p_access
->info
.b_eof
= false;
246 lseek( p_access
->p_sys
->fd
, i_pos
, SEEK_SET
);
250 /*****************************************************************************
252 *****************************************************************************/
253 static int Control( access_t
*p_access
, int i_query
, va_list args
)
261 case ACCESS_CAN_SEEK
:
262 case ACCESS_CAN_FASTSEEK
:
263 pb_bool
= ( bool* )va_arg( args
, bool* );
267 case ACCESS_CAN_PAUSE
:
268 case ACCESS_CAN_CONTROL_PACE
:
269 pb_bool
= ( bool* )va_arg( args
, bool* );
273 case ACCESS_GET_PTS_DELAY
:
274 pi_64
= ( int64_t* )va_arg( args
, int64_t * );
275 *pi_64
= var_GetInteger( p_access
, "file-caching" ) * INT64_C( 1000 );
279 case ACCESS_SET_PAUSE_STATE
:
283 case ACCESS_GET_TITLE_INFO
:
284 case ACCESS_SET_TITLE
:
285 case ACCESS_SET_SEEKPOINT
:
286 case ACCESS_SET_PRIVATE_ID_STATE
:
287 case ACCESS_GET_META
:
288 case ACCESS_GET_PRIVATE_ID_STATE
:
289 case ACCESS_GET_CONTENT_TYPE
:
293 msg_Warn( p_access
, "unimplemented query %d in control", i_query
);
300 /*****************************************************************************
301 * open_file: Opens a specific file
302 *****************************************************************************/
303 static int open_file( access_t
*p_access
, const char *path
)
305 int fd
= vlc_open( path
, O_RDONLY
| O_NONBLOCK
);
308 msg_Err( p_access
, "cannot open file %s (%m)", path
);
309 dialog_Fatal( p_access
, _( "File reading failed" ),
310 _( "VLC could not open the file \"%s\"." ), path
);
314 #if defined( HAVE_FCNTL )
315 fcntl( fd
, F_SETFD
, fcntl( fd
, F_GETFD
) | FD_CLOEXEC
);
317 /* We'd rather use any available memory for reading ahead
318 * than for caching what we've already seen/heard */
319 # if defined( F_RDAHEAD )
320 fcntl( fd
, F_RDAHEAD
, 1 );
322 # if defined( F_NOCACHE )
323 fcntl( fd
, F_NOCACHE
, 1 );