mtp.c 9.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*****************************************************************************
 * mtp.c: mtp input (mtp: access plug-in)
 *****************************************************************************
 * Copyright (C) 2001-2006 the VideoLAN team
 * Copyright © 2006-2008 Rémi Denis-Courmont
 *
 * Authors: Fabio Ritrovato <exsephiroth87@gmail.com>
 * Original file.c: Christophe Massiot <massiot@via.ecp.fr>
 *                  Rémi Denis-Courmont <rem # videolan # org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_input.h>
#include <vlc_access.h>
37
#include <vlc_dialog.h>
38
39
40

#include <assert.h>
#include <errno.h>
41
#include <sys/types.h>
42
43
44
45
46
47
48
49
50
51
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif

#include <unistd.h>
#include <poll.h>

52
#include <vlc_fs.h>
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#include "libmtp.h"

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

#define CACHING_TEXT N_("Caching value in ms")
#define CACHING_LONGTEXT N_( \
    "Caching value for files. This " \
    "value should be set in milliseconds." )

vlc_module_begin()
    set_description( N_("MTP input") )
    set_shortname( N_("MTP") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
    set_capability( "access", 0 )
    add_shortcut( "mtp" )
    set_callbacks( Open, Close )
vlc_module_end()

/*****************************************************************************
 * Exported prototypes
 *****************************************************************************/

82
static int  Seek( access_t *, uint64_t );
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
static ssize_t Read( access_t *, uint8_t *, size_t );
static int  Control( access_t *, int, va_list );

static int  open_file( access_t *, const char * );

struct access_sys_t
{
    unsigned int i_nb_reads;
    int fd;
};

/*****************************************************************************
 * Open: open the file
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    access_t     *p_access = ( access_t* )p_this;
    access_sys_t *p_sys;
    uint32_t i_bus;
    uint8_t i_dev;
    uint16_t i_product_id;
    int i_track_id;
    LIBMTP_raw_device_t *p_rawdevices;
    LIBMTP_mtpdevice_t *p_device;
    int i_numrawdevices;
    int i_ret;

    /* Update default_pts to a suitable value for file access */
    var_Create( p_access, "file-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );

    if( sscanf( p_access->psz_path, "%"SCNu32":%"SCNu8":%"SCNu16":%d", &i_bus,
                &i_dev, &i_product_id, &i_track_id ) != 4 )
        return VLC_EGENERIC;
    i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
    if( i_ret != 0 || i_numrawdevices <= 0 || !p_rawdevices )
        return VLC_EGENERIC;

    for( int i = 0; i < i_numrawdevices; i++ )
    {
        if( i_bus == p_rawdevices[i].bus_location &&
            i_dev == p_rawdevices[i].devnum &&
            i_product_id == p_rawdevices[i].device_entry.product_id )
        {
            if( ( p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] )
                ) != NULL )
            {
                free( p_access->psz_path );
                if( ( p_access->psz_path = tempnam( NULL, "vlc" ) ) == NULL )
                {
                    LIBMTP_Release_Device( p_device );
                    free( p_rawdevices );
                    return VLC_ENOMEM;
                }
                else
                {
                    msg_Dbg( p_access, "About to write %s", p_access->psz_path );
                    LIBMTP_Get_File_To_File( p_device, i_track_id,
                                             p_access->psz_path, NULL, NULL );
                    LIBMTP_Release_Device( p_device );
                    i = i_numrawdevices;
                }
            }
            else
            {
                free( p_rawdevices );
                return VLC_EGENERIC;
            }
        }
    }
    free( p_rawdevices );

    STANDARD_READ_ACCESS_INIT;
    p_sys->i_nb_reads = 0;
    int fd = p_sys->fd = -1;

    /* Open file */
    msg_Dbg( p_access, "opening file `%s'", p_access->psz_path );
    fd = open_file( p_access, p_access->psz_path );

    if( fd == -1 )
    {
        free( p_sys );
        return VLC_EGENERIC;
    }
    p_sys->fd = fd;

#ifdef HAVE_SYS_STAT_H
    struct stat st;
    if( fstat( fd, &st ) )
        msg_Err( p_access, "fstat(%d): %m", fd );
    p_access->info.i_size = st.st_size;
#else
# warning File size not known!
#endif

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
    access_t     *p_access = ( access_t* )p_this;
    access_sys_t *p_sys = p_access->p_sys;

    close ( p_sys->fd );
    if(	utf8_unlink( p_access->psz_path ) != 0 )
        msg_Err( p_access, "Error deleting file %s, %m", p_access->psz_path );
    free( p_sys );
}

/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
{
    access_sys_t *p_sys = p_access->p_sys;
    ssize_t i_ret;
    int fd = p_sys->fd;

    i_ret = read( fd, p_buffer, i_len );

    if( i_ret < 0 )
    {
        switch( errno )
        {
            case EINTR:
            case EAGAIN:
                break;

            default:
                msg_Err( p_access, "read failed (%m)" );
216
                dialog_Fatal( p_access, _( "File reading failed" ), "%s",
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
                                _( "VLC could not read the file." ) );
                p_access->info.b_eof = true;
                return 0;
        }
    }
    else if( i_ret > 0 )
        p_access->info.i_pos += i_ret;
    else
        p_access->info.b_eof = true;

    p_sys->i_nb_reads++;

    return i_ret;
}


/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
236
static int Seek( access_t *p_access, uint64_t i_pos )
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{
    p_access->info.i_pos = i_pos;
    p_access->info.b_eof = false;

    lseek( p_access->p_sys->fd, i_pos, SEEK_SET );
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    bool   *pb_bool;
    int64_t      *pi_64;

    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
            pb_bool = ( bool* )va_arg( args, bool* );
            *pb_bool = true;
            break;

        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
            pb_bool = ( bool* )va_arg( args, bool* );
            *pb_bool = true;
            break;

        case ACCESS_GET_PTS_DELAY:
            pi_64 = ( int64_t* )va_arg( args, int64_t * );
            *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C( 1000 );
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
        case ACCESS_SET_PRIVATE_ID_STATE:
        case ACCESS_GET_META:
        case ACCESS_GET_PRIVATE_ID_STATE:
        case ACCESS_GET_CONTENT_TYPE:
            return VLC_EGENERIC;

        default:
            msg_Warn( p_access, "unimplemented query %d in control", i_query );
            return VLC_EGENERIC;

    }
    return VLC_SUCCESS;
}

/*****************************************************************************
 * open_file: Opens a specific file
 *****************************************************************************/
static int open_file( access_t *p_access, const char *path )
{
300
    int fd = utf8_open( path, O_RDONLY | O_NONBLOCK );
301
302
303
    if( fd == -1 )
    {
        msg_Err( p_access, "cannot open file %s (%m)", path );
304
        dialog_Fatal( p_access, _( "File reading failed" ),
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
                        _( "VLC could not open the file \"%s\"." ), path );
        return -1;
    }

#if defined( HAVE_FCNTL )
    fcntl( fd, F_SETFD, fcntl( fd, F_GETFD ) | FD_CLOEXEC );

    /* We'd rather use any available memory for reading ahead
     * than for caching what we've already seen/heard */
# if defined( F_RDAHEAD )
    fcntl( fd, F_RDAHEAD, 1 );
# endif
# if defined( F_NOCACHE )
    fcntl( fd, F_NOCACHE, 1 );
# endif
#endif

    return fd;
}