]> git.sesse.net Git - vlc/blob - modules/access/mtp.c
avio: specify libav private options
[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 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <poll.h>
40
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>
46 #include <vlc_fs.h>
47
48 #include "libmtp.h"
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53
54 static int  Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
56
57 vlc_module_begin()
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 )
63     add_shortcut( "mtp" )
64     set_callbacks( Open, Close )
65 vlc_module_end()
66
67 /*****************************************************************************
68  * Exported prototypes
69  *****************************************************************************/
70
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 );
74
75 static int  open_file( access_t *, const char * );
76
77 struct access_sys_t
78 {
79     unsigned int i_nb_reads;
80     int fd;
81 };
82
83 /*****************************************************************************
84  * Open: open the file
85  *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
87 {
88     access_t     *p_access = ( access_t* )p_this;
89     access_sys_t *p_sys;
90     uint32_t i_bus;
91     uint8_t i_dev;
92     uint16_t i_product_id;
93     int i_track_id;
94     LIBMTP_raw_device_t *p_rawdevices;
95     LIBMTP_mtpdevice_t *p_device;
96     int i_numrawdevices;
97     int i_ret;
98
99     if( sscanf( p_access->psz_location, "%"SCNu32":%"SCNu8":%"SCNu16":%d",
100                 &i_bus, &i_dev, &i_product_id, &i_track_id ) != 4 )
101         return VLC_EGENERIC;
102     i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
103     if( i_ret != 0 || i_numrawdevices <= 0 || !p_rawdevices )
104         return VLC_EGENERIC;
105
106     for( int i = 0; i < i_numrawdevices; i++ )
107     {
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 )
111         {
112             if( ( p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] )
113                 ) != NULL )
114             {
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 )
119                 {
120                     LIBMTP_Release_Device( p_device );
121                     free( p_rawdevices );
122                     return VLC_ENOMEM;
123                 }
124                 else
125                 {
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,
130                                              NULL );
131                     LIBMTP_Release_Device( p_device );
132                     i = i_numrawdevices;
133                 }
134             }
135             else
136             {
137                 free( p_rawdevices );
138                 return VLC_EGENERIC;
139             }
140         }
141     }
142     free( p_rawdevices );
143
144     STANDARD_READ_ACCESS_INIT;
145     p_sys->i_nb_reads = 0;
146     int fd = p_sys->fd = -1;
147
148     /* Open file */
149     msg_Dbg( p_access, "opening file `%s'", p_access->psz_filepath );
150     fd = open_file( p_access, p_access->psz_filepath );
151
152     if( fd == -1 )
153     {
154         free( p_sys );
155         return VLC_EGENERIC;
156     }
157     p_sys->fd = fd;
158
159     struct stat st;
160     if( fstat( fd, &st ) )
161         msg_Err( p_access, "fstat(%d): %m", fd );
162     p_access->info.i_size = st.st_size;
163
164     return VLC_SUCCESS;
165 }
166
167 /*****************************************************************************
168  * Close: close the target
169  *****************************************************************************/
170 static void Close( vlc_object_t * p_this )
171 {
172     access_t     *p_access = ( access_t* )p_this;
173     access_sys_t *p_sys = p_access->p_sys;
174
175     close ( p_sys->fd );
176     if( vlc_unlink( p_access->psz_filepath ) != 0 )
177         msg_Err( p_access, "Error deleting file %s, %m",
178                  p_access->psz_filepath );
179     free( p_sys );
180 }
181
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 )
186 {
187     access_sys_t *p_sys = p_access->p_sys;
188     ssize_t i_ret;
189     int fd = p_sys->fd;
190
191     i_ret = read( fd, p_buffer, i_len );
192
193     if( i_ret < 0 )
194     {
195         switch( errno )
196         {
197             case EINTR:
198             case EAGAIN:
199                 break;
200
201             default:
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;
206                 return 0;
207         }
208     }
209     else if( i_ret > 0 )
210         p_access->info.i_pos += i_ret;
211     else
212         p_access->info.b_eof = true;
213
214     p_sys->i_nb_reads++;
215
216     return i_ret;
217 }
218
219
220 /*****************************************************************************
221  * Seek: seek to a specific location in a file
222  *****************************************************************************/
223 static int Seek( access_t *p_access, uint64_t i_pos )
224 {
225     p_access->info.i_pos = i_pos;
226     p_access->info.b_eof = false;
227
228     lseek( p_access->p_sys->fd, i_pos, SEEK_SET );
229     return VLC_SUCCESS;
230 }
231
232 /*****************************************************************************
233  * Control:
234  *****************************************************************************/
235 static int Control( access_t *p_access, int i_query, va_list args )
236 {
237     bool   *pb_bool;
238     int64_t      *pi_64;
239
240     switch( i_query )
241     {
242         /* */
243         case ACCESS_CAN_SEEK:
244         case ACCESS_CAN_FASTSEEK:
245             pb_bool = ( bool* )va_arg( args, bool* );
246             *pb_bool = true;
247             break;
248
249         case ACCESS_CAN_PAUSE:
250         case ACCESS_CAN_CONTROL_PACE:
251             pb_bool = ( bool* )va_arg( args, bool* );
252             *pb_bool = true;
253             break;
254
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" );
259             break;
260
261         /* */
262         case ACCESS_SET_PAUSE_STATE:
263             /* Nothing to do */
264             break;
265
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:
273             return VLC_EGENERIC;
274
275         default:
276             msg_Warn( p_access, "unimplemented query %d in control", i_query );
277             return VLC_EGENERIC;
278
279     }
280     return VLC_SUCCESS;
281 }
282
283 /*****************************************************************************
284  * open_file: Opens a specific file
285  *****************************************************************************/
286 static int open_file( access_t *p_access, const char *path )
287 {
288     int fd = vlc_open( path, O_RDONLY | O_NONBLOCK );
289     if( fd == -1 )
290     {
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 );
294         return -1;
295     }
296
297 #if defined( HAVE_FCNTL )
298     fcntl( fd, F_SETFD, fcntl( fd, F_GETFD ) | FD_CLOEXEC );
299
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 );
304 # endif
305 # if defined( F_NOCACHE )
306     fcntl( fd, F_NOCACHE, 1 );
307 # endif
308 #endif
309
310     return fd;
311 }