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
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( stream_t *, uint64_t );
71 static ssize_t Read( stream_t *, void *, size_t );
72 static int  Control( stream_t *, int, va_list );
73 
74 /*****************************************************************************
75  * Open: open the file
76  *****************************************************************************/
Open(vlc_object_t * p_this)77 static int Open( vlc_object_t *p_this )
78 {
79     stream_t     *p_access = ( stream_t* )p_this;
80     uint32_t i_bus;
81     uint8_t i_dev;
82     uint16_t i_product_id;
83     int i_track_id;
84     LIBMTP_raw_device_t *p_rawdevices;
85     int i_numrawdevices;
86 
87     if( sscanf( p_access->psz_location, "%"SCNu32":%"SCNu8":%"SCNu16":%d",
88                 &i_bus, &i_dev, &i_product_id, &i_track_id ) != 4 )
89         return VLC_EGENERIC;
90 
91     if( LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices ) )
92         return VLC_EGENERIC;
93 
94     int fd = -1;
95 
96     for( int i = 0; i < i_numrawdevices; i++ )
97     {
98         if( i_bus == p_rawdevices[i].bus_location &&
99             i_dev == p_rawdevices[i].devnum &&
100             i_product_id == p_rawdevices[i].device_entry.product_id )
101         {
102             LIBMTP_mtpdevice_t *p_device;
103 
104             p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] );
105             if( p_device == NULL )
106                 break;
107 
108             fd = vlc_memfd();
109             if( unlikely(fd == -1) )
110                 break;
111 
112             msg_Dbg( p_access, "copying to memory" );
113             LIBMTP_Get_File_To_File_Descriptor( p_device, i_track_id, fd,
114                                                 NULL, NULL );
115             LIBMTP_Release_Device( p_device );
116             break;
117         }
118     }
119     free( p_rawdevices );
120 
121     if( fd == -1 )
122     {
123         msg_Err( p_access, "cannot find %s", p_access->psz_location );
124         return VLC_EGENERIC;
125     }
126 
127     if( lseek( fd, 0, SEEK_SET ) ) /* Reset file descriptor offset */
128     {
129         close( fd );
130         return VLC_EGENERIC;
131     }
132 
133     p_access->p_sys = (void *)(intptr_t)fd;
134     ACCESS_SET_CALLBACKS( Read, NULL, Control, Seek );
135     return VLC_SUCCESS;
136 }
137 
138 /*****************************************************************************
139  * Close: close the target
140  *****************************************************************************/
Close(vlc_object_t * p_this)141 static void Close( vlc_object_t * p_this )
142 {
143     stream_t *p_access = ( stream_t* )p_this;
144     int fd = (intptr_t)p_access->p_sys;
145 
146     vlc_close ( fd );
147 }
148 
149 /*****************************************************************************
150  * Read: standard read on a file descriptor.
151  *****************************************************************************/
Read(stream_t * p_access,void * p_buffer,size_t i_len)152 static ssize_t Read( stream_t *p_access, void *p_buffer, size_t i_len )
153 {
154     int fd = (intptr_t)p_access->p_sys;
155     ssize_t i_ret = read( fd, p_buffer, i_len );
156 
157     if( i_ret < 0 )
158     {
159         switch( errno )
160         {
161             case EINTR:
162             case EAGAIN:
163                 break;
164 
165             default:
166                 msg_Err( p_access, "read failed: %s", vlc_strerror_c(errno) );
167                 vlc_dialog_display_error( p_access, _( "File reading failed" ),
168                     _( "VLC could not read the file: %s" ),
169                     vlc_strerror(errno) );
170                 return 0;
171         }
172     }
173 
174     return i_ret;
175 }
176 
177 
178 /*****************************************************************************
179  * Seek: seek to a specific location in a file
180  *****************************************************************************/
Seek(stream_t * p_access,uint64_t i_pos)181 static int Seek( stream_t *p_access, uint64_t i_pos )
182 {
183     int fd = (intptr_t)p_access->p_sys;
184 
185     if (lseek( fd, i_pos, SEEK_SET ) == (off_t)-1)
186         return VLC_EGENERIC;
187     return VLC_SUCCESS;
188 }
189 
190 /*****************************************************************************
191  * Control:
192  *****************************************************************************/
Control(stream_t * p_access,int i_query,va_list args)193 static int Control( stream_t *p_access, int i_query, va_list args )
194 {
195     int fd = (intptr_t)p_access->p_sys;
196     bool   *pb_bool;
197     int64_t      *pi_64;
198 
199     switch( i_query )
200     {
201         case STREAM_CAN_SEEK:
202         case STREAM_CAN_FASTSEEK:
203             pb_bool = va_arg( args, bool * );
204             *pb_bool = true;
205             break;
206 
207         case STREAM_CAN_PAUSE:
208         case STREAM_CAN_CONTROL_PACE:
209             pb_bool = va_arg( args, bool * );
210             *pb_bool = true;
211             break;
212 
213         case STREAM_GET_SIZE:
214         {
215             uint64_t *s = va_arg( args, uint64_t * );
216             struct stat st;
217             if( fstat( fd, &st ) )
218             {
219                 msg_Err( p_access, "fstat error: %s", vlc_strerror_c(errno) );
220                 return VLC_EGENERIC;
221             }
222             *s = st.st_size;
223             break;
224         }
225 
226         case STREAM_GET_PTS_DELAY:
227             pi_64 = va_arg( args, int64_t * );
228             *pi_64 = INT64_C(1000)
229                    * var_InheritInteger( p_access, "file-caching" );
230             break;
231 
232         case STREAM_SET_PAUSE_STATE:
233             /* Nothing to do */
234             break;
235 
236         default:
237             return VLC_EGENERIC;
238 
239     }
240     return VLC_SUCCESS;
241 }
242