]> git.sesse.net Git - vlc/blob - modules/access/mtp.c
fe094ecac72d67780811a6ef014d3d1abf6c8e92
[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 <assert.h>
34 #include <errno.h>
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_STAT_H
37 #   include <sys/stat.h>
38 #endif
39 #ifdef HAVE_FCNTL_H
40 #   include <fcntl.h>
41 #endif
42 #include <unistd.h>
43 #include <poll.h>
44
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_input.h>
48 #include <vlc_access.h>
49 #include <vlc_dialog.h>
50 #include <vlc_fs.h>
51
52 #include "libmtp.h"
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57
58 static int  Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
60
61 vlc_module_begin()
62     set_description( N_("MTP input") )
63     set_shortname( N_("MTP") )
64     set_category( CAT_INPUT )
65     set_subcategory( SUBCAT_INPUT_ACCESS )
66     set_capability( "access", 0 )
67     add_shortcut( "mtp" )
68     set_callbacks( Open, Close )
69 vlc_module_end()
70
71 /*****************************************************************************
72  * Exported prototypes
73  *****************************************************************************/
74
75 static int  Seek( access_t *, uint64_t );
76 static ssize_t Read( access_t *, uint8_t *, size_t );
77 static int  Control( access_t *, int, va_list );
78
79 static int  open_file( access_t *, const char * );
80
81 struct access_sys_t
82 {
83     unsigned int i_nb_reads;
84     int fd;
85 };
86
87 /*****************************************************************************
88  * Open: open the file
89  *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
91 {
92     access_t     *p_access = ( access_t* )p_this;
93     access_sys_t *p_sys;
94     uint32_t i_bus;
95     uint8_t i_dev;
96     uint16_t i_product_id;
97     int i_track_id;
98     LIBMTP_raw_device_t *p_rawdevices;
99     LIBMTP_mtpdevice_t *p_device;
100     int i_numrawdevices;
101     int i_ret;
102
103     if( sscanf( p_access->psz_location, "%"SCNu32":%"SCNu8":%"SCNu16":%d",
104                 &i_bus, &i_dev, &i_product_id, &i_track_id ) != 4 )
105         return VLC_EGENERIC;
106     i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
107     if( i_ret != 0 || i_numrawdevices <= 0 || !p_rawdevices )
108         return VLC_EGENERIC;
109
110     for( int i = 0; i < i_numrawdevices; i++ )
111     {
112         if( i_bus == p_rawdevices[i].bus_location &&
113             i_dev == p_rawdevices[i].devnum &&
114             i_product_id == p_rawdevices[i].device_entry.product_id )
115         {
116             if( ( p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] )
117                 ) != NULL )
118             {
119                 free( p_access->psz_filepath );
120 #warning Oooh no! Not tempnam()!
121                 p_access->psz_filepath = tempnam( NULL, "vlc" );
122                 if( p_access->psz_filepath == NULL )
123                 {
124                     LIBMTP_Release_Device( p_device );
125                     free( p_rawdevices );
126                     return VLC_ENOMEM;
127                 }
128                 else
129                 {
130                     msg_Dbg( p_access, "About to write %s",
131                              p_access->psz_filepath );
132                     LIBMTP_Get_File_To_File( p_device, i_track_id,
133                                              p_access->psz_filepath, NULL,
134                                              NULL );
135                     LIBMTP_Release_Device( p_device );
136                     i = i_numrawdevices;
137                 }
138             }
139             else
140             {
141                 free( p_rawdevices );
142                 return VLC_EGENERIC;
143             }
144         }
145     }
146     free( p_rawdevices );
147
148     STANDARD_READ_ACCESS_INIT;
149     p_sys->i_nb_reads = 0;
150     int fd = p_sys->fd = -1;
151
152     /* Open file */
153     msg_Dbg( p_access, "opening file `%s'", p_access->psz_filepath );
154     fd = open_file( p_access, p_access->psz_filepath );
155
156     if( fd == -1 )
157     {
158         free( p_sys );
159         return VLC_EGENERIC;
160     }
161     p_sys->fd = fd;
162
163 #ifdef HAVE_SYS_STAT_H
164     struct stat st;
165     if( fstat( fd, &st ) )
166         msg_Err( p_access, "fstat(%d): %m", fd );
167     p_access->info.i_size = st.st_size;
168 #else
169 # warning File size not known!
170 #endif
171
172     return VLC_SUCCESS;
173 }
174
175 /*****************************************************************************
176  * Close: close the target
177  *****************************************************************************/
178 static void Close( vlc_object_t * p_this )
179 {
180     access_t     *p_access = ( access_t* )p_this;
181     access_sys_t *p_sys = p_access->p_sys;
182
183     close ( p_sys->fd );
184     if( vlc_unlink( p_access->psz_filepath ) != 0 )
185         msg_Err( p_access, "Error deleting file %s, %m",
186                  p_access->psz_filepath );
187     free( p_sys );
188 }
189
190 /*****************************************************************************
191  * Read: standard read on a file descriptor.
192  *****************************************************************************/
193 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
194 {
195     access_sys_t *p_sys = p_access->p_sys;
196     ssize_t i_ret;
197     int fd = p_sys->fd;
198
199     i_ret = read( fd, p_buffer, i_len );
200
201     if( i_ret < 0 )
202     {
203         switch( errno )
204         {
205             case EINTR:
206             case EAGAIN:
207                 break;
208
209             default:
210                 msg_Err( p_access, "read failed (%m)" );
211                 dialog_Fatal( p_access, _( "File reading failed" ), "%s (%m)",
212                                 _( "VLC could not read the file." ) );
213                 p_access->info.b_eof = true;
214                 return 0;
215         }
216     }
217     else if( i_ret > 0 )
218         p_access->info.i_pos += i_ret;
219     else
220         p_access->info.b_eof = true;
221
222     p_sys->i_nb_reads++;
223
224     return i_ret;
225 }
226
227
228 /*****************************************************************************
229  * Seek: seek to a specific location in a file
230  *****************************************************************************/
231 static int Seek( access_t *p_access, uint64_t i_pos )
232 {
233     p_access->info.i_pos = i_pos;
234     p_access->info.b_eof = false;
235
236     lseek( p_access->p_sys->fd, i_pos, SEEK_SET );
237     return VLC_SUCCESS;
238 }
239
240 /*****************************************************************************
241  * Control:
242  *****************************************************************************/
243 static int Control( access_t *p_access, int i_query, va_list args )
244 {
245     bool   *pb_bool;
246     int64_t      *pi_64;
247
248     switch( i_query )
249     {
250         /* */
251         case ACCESS_CAN_SEEK:
252         case ACCESS_CAN_FASTSEEK:
253             pb_bool = ( bool* )va_arg( args, bool* );
254             *pb_bool = true;
255             break;
256
257         case ACCESS_CAN_PAUSE:
258         case ACCESS_CAN_CONTROL_PACE:
259             pb_bool = ( bool* )va_arg( args, bool* );
260             *pb_bool = true;
261             break;
262
263         case ACCESS_GET_PTS_DELAY:
264             pi_64 = ( int64_t* )va_arg( args, int64_t * );
265             *pi_64 = INT64_C(1000)
266                    * var_InheritInteger( p_access, "file-caching" );
267             break;
268
269         /* */
270         case ACCESS_SET_PAUSE_STATE:
271             /* Nothing to do */
272             break;
273
274         case ACCESS_GET_TITLE_INFO:
275         case ACCESS_SET_TITLE:
276         case ACCESS_SET_SEEKPOINT:
277         case ACCESS_SET_PRIVATE_ID_STATE:
278         case ACCESS_GET_META:
279         case ACCESS_GET_PRIVATE_ID_STATE:
280         case ACCESS_GET_CONTENT_TYPE:
281             return VLC_EGENERIC;
282
283         default:
284             msg_Warn( p_access, "unimplemented query %d in control", i_query );
285             return VLC_EGENERIC;
286
287     }
288     return VLC_SUCCESS;
289 }
290
291 /*****************************************************************************
292  * open_file: Opens a specific file
293  *****************************************************************************/
294 static int open_file( access_t *p_access, const char *path )
295 {
296     int fd = vlc_open( path, O_RDONLY | O_NONBLOCK );
297     if( fd == -1 )
298     {
299         msg_Err( p_access, "cannot open file %s (%m)", path );
300         dialog_Fatal( p_access, _( "File reading failed" ),
301                         _( "VLC could not open the file \"%s\". (%m)" ), path );
302         return -1;
303     }
304
305 #if defined( HAVE_FCNTL )
306     fcntl( fd, F_SETFD, fcntl( fd, F_GETFD ) | FD_CLOEXEC );
307
308     /* We'd rather use any available memory for reading ahead
309      * than for caching what we've already seen/heard */
310 # if defined( F_RDAHEAD )
311     fcntl( fd, F_RDAHEAD, 1 );
312 # endif
313 # if defined( F_NOCACHE )
314     fcntl( fd, F_NOCACHE, 1 );
315 # endif
316 #endif
317
318     return fd;
319 }