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