]> git.sesse.net Git - vlc/blob - modules/access/mtp.c
Update LGPL license blurb, choosing v2.1+.
[vlc] / modules / access / mtp.c
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
6  *
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>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
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>
38
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
48
49 #include <unistd.h>
50 #include <poll.h>
51
52 #include <vlc_fs.h>
53
54 #include "libmtp.h"
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59
60 static int  Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
62
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." )
67
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()
77
78 /*****************************************************************************
79  * Exported prototypes
80  *****************************************************************************/
81
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 );
85
86 static int  open_file( access_t *, const char * );
87
88 struct access_sys_t
89 {
90     unsigned int i_nb_reads;
91     int fd;
92 };
93
94 /*****************************************************************************
95  * Open: open the file
96  *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
98 {
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;
109
110     /* Update default_pts to a suitable value for file access */
111     var_Create( p_access, "file-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
112
113     if( sscanf( p_access->psz_location, "%"SCNu32":%"SCNu8":%"SCNu16":%d",
114                 &i_bus, &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;
119
120     for( int i = 0; i < i_numrawdevices; i++ )
121     {
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 )
125         {
126             if( ( p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] )
127                 ) != NULL )
128             {
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 )
133                 {
134                     LIBMTP_Release_Device( p_device );
135                     free( p_rawdevices );
136                     return VLC_ENOMEM;
137                 }
138                 else
139                 {
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,
144                                              NULL );
145                     LIBMTP_Release_Device( p_device );
146                     i = i_numrawdevices;
147                 }
148             }
149             else
150             {
151                 free( p_rawdevices );
152                 return VLC_EGENERIC;
153             }
154         }
155     }
156     free( p_rawdevices );
157
158     STANDARD_READ_ACCESS_INIT;
159     p_sys->i_nb_reads = 0;
160     int fd = p_sys->fd = -1;
161
162     /* Open file */
163     msg_Dbg( p_access, "opening file `%s'", p_access->psz_filepath );
164     fd = open_file( p_access, p_access->psz_filepath );
165
166     if( fd == -1 )
167     {
168         free( p_sys );
169         return VLC_EGENERIC;
170     }
171     p_sys->fd = fd;
172
173 #ifdef HAVE_SYS_STAT_H
174     struct stat st;
175     if( fstat( fd, &st ) )
176         msg_Err( p_access, "fstat(%d): %m", fd );
177     p_access->info.i_size = st.st_size;
178 #else
179 # warning File size not known!
180 #endif
181
182     return VLC_SUCCESS;
183 }
184
185 /*****************************************************************************
186  * Close: close the target
187  *****************************************************************************/
188 static void Close( vlc_object_t * p_this )
189 {
190     access_t     *p_access = ( access_t* )p_this;
191     access_sys_t *p_sys = p_access->p_sys;
192
193     close ( p_sys->fd );
194     if( vlc_unlink( p_access->psz_filepath ) != 0 )
195         msg_Err( p_access, "Error deleting file %s, %m",
196                  p_access->psz_filepath );
197     free( p_sys );
198 }
199
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 )
204 {
205     access_sys_t *p_sys = p_access->p_sys;
206     ssize_t i_ret;
207     int fd = p_sys->fd;
208
209     i_ret = read( fd, p_buffer, i_len );
210
211     if( i_ret < 0 )
212     {
213         switch( errno )
214         {
215             case EINTR:
216             case EAGAIN:
217                 break;
218
219             default:
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;
224                 return 0;
225         }
226     }
227     else if( i_ret > 0 )
228         p_access->info.i_pos += i_ret;
229     else
230         p_access->info.b_eof = true;
231
232     p_sys->i_nb_reads++;
233
234     return i_ret;
235 }
236
237
238 /*****************************************************************************
239  * Seek: seek to a specific location in a file
240  *****************************************************************************/
241 static int Seek( access_t *p_access, uint64_t i_pos )
242 {
243     p_access->info.i_pos = i_pos;
244     p_access->info.b_eof = false;
245
246     lseek( p_access->p_sys->fd, i_pos, SEEK_SET );
247     return VLC_SUCCESS;
248 }
249
250 /*****************************************************************************
251  * Control:
252  *****************************************************************************/
253 static int Control( access_t *p_access, int i_query, va_list args )
254 {
255     bool   *pb_bool;
256     int64_t      *pi_64;
257
258     switch( i_query )
259     {
260         /* */
261         case ACCESS_CAN_SEEK:
262         case ACCESS_CAN_FASTSEEK:
263             pb_bool = ( bool* )va_arg( args, bool* );
264             *pb_bool = true;
265             break;
266
267         case ACCESS_CAN_PAUSE:
268         case ACCESS_CAN_CONTROL_PACE:
269             pb_bool = ( bool* )va_arg( args, bool* );
270             *pb_bool = true;
271             break;
272
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 );
276             break;
277
278         /* */
279         case ACCESS_SET_PAUSE_STATE:
280             /* Nothing to do */
281             break;
282
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:
290             return VLC_EGENERIC;
291
292         default:
293             msg_Warn( p_access, "unimplemented query %d in control", i_query );
294             return VLC_EGENERIC;
295
296     }
297     return VLC_SUCCESS;
298 }
299
300 /*****************************************************************************
301  * open_file: Opens a specific file
302  *****************************************************************************/
303 static int open_file( access_t *p_access, const char *path )
304 {
305     int fd = vlc_open( path, O_RDONLY | O_NONBLOCK );
306     if( fd == -1 )
307     {
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 );
311         return -1;
312     }
313
314 #if defined( HAVE_FCNTL )
315     fcntl( fd, F_SETFD, fcntl( fd, F_GETFD ) | FD_CLOEXEC );
316
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 );
321 # endif
322 # if defined( F_NOCACHE )
323     fcntl( fd, F_NOCACHE, 1 );
324 # endif
325 #endif
326
327     return fd;
328 }