]> git.sesse.net Git - vlc/blob - modules/services_discovery/mtp.c
a0e4ed5105d59b2df72493c4d9ccc77866c43325
[vlc] / modules / services_discovery / mtp.c
1 /*****************************************************************************
2  * mtp.c :  MTP interface module
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  *
6  * Authors: Fabio Ritrovato <exsephiroth87@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_services_discovery.h>
30
31 #include <libmtp.h>
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 static int Open( vlc_object_t * );
37 static void Close( vlc_object_t * );
38
39 VLC_SD_PROBE_HELPER("mtp", "MTP devices", SD_CAT_DEVICES)
40
41 vlc_module_begin()
42     set_shortname( "MTP" )
43     set_description( N_( "MTP devices" ) )
44     set_category( CAT_PLAYLIST )
45     set_subcategory( SUBCAT_PLAYLIST_SD )
46     set_capability( "services_discovery", 0 )
47     set_callbacks( Open, Close )
48     cannot_unload_broken_library()
49
50     VLC_SD_PROBE_SUBMODULE
51 vlc_module_end()
52
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57
58 static void *Run( void * );
59
60 static int AddDevice( services_discovery_t *, LIBMTP_raw_device_t * );
61 static void AddTrack( services_discovery_t *, LIBMTP_track_t *);
62 static void CloseDevice( services_discovery_t * );
63 static int CountTracks( uint64_t const, uint64_t const, void const * const );
64
65 /*****************************************************************************
66  * Local structures
67  *****************************************************************************/
68
69 struct services_discovery_sys_t
70 {
71     int i_tracks_num;
72     input_item_t **pp_items;
73     int i_count;
74     char *psz_name;
75     uint32_t i_bus;
76     uint8_t i_dev;
77     uint16_t i_product_id;
78     vlc_thread_t thread;
79 };
80
81 static vlc_mutex_t mtp_lock = VLC_STATIC_MUTEX;
82 static bool b_mtp_initialized = false;
83
84 /*****************************************************************************
85  * Open: initialize and create stuff
86  *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
88 {
89     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
90     services_discovery_sys_t *p_sys;
91
92     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
93         return VLC_ENOMEM;
94     p_sd->p_sys = p_sys;
95     p_sys->psz_name = NULL;
96
97     vlc_mutex_lock( &mtp_lock );
98     if( !b_mtp_initialized )
99     {
100         LIBMTP_Init();
101         b_mtp_initialized = true;
102     }
103     vlc_mutex_unlock( &mtp_lock );
104
105     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
106     {
107         free (p_sys);
108         return VLC_EGENERIC;
109     }
110     return VLC_SUCCESS;
111 }
112
113 /*****************************************************************************
114  * Close: cleanup
115  *****************************************************************************/
116 static void Close( vlc_object_t *p_this )
117 {
118     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
119
120     free( p_sd->p_sys->psz_name );
121     vlc_cancel( p_sd->p_sys->thread );
122     vlc_join( p_sd->p_sys->thread, NULL );
123     free( p_sd->p_sys );
124 }
125
126 /*****************************************************************************
127  * Run: main thread
128  *****************************************************************************/
129 static void *Run( void *data )
130 {
131     LIBMTP_raw_device_t *p_rawdevices;
132     int i_numrawdevices;
133     int i_ret;
134     int i_status = 0;
135     services_discovery_t *p_sd = data;
136
137     for(;;)
138     {
139         int canc = vlc_savecancel();
140         i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
141         if ( i_ret == 0 && i_numrawdevices > 0 && p_rawdevices != NULL &&
142              i_status == 0 )
143         {
144             /* Found a new device, add it */
145             msg_Dbg( p_sd, "New device found" );
146             if( AddDevice( p_sd, &p_rawdevices[0] ) == VLC_SUCCESS )
147                 i_status = 1;
148             else
149                 i_status = 2;
150         }
151         else
152         {
153             if ( ( i_ret != 0 || i_numrawdevices == 0 || p_rawdevices == NULL )
154                  && i_status == 1)
155             {
156                 /* The device is not connected anymore, delete it */
157                 msg_Info( p_sd, "Device disconnected" );
158                 CloseDevice( p_sd );
159                 i_status = 0;
160             }
161         }
162         free( p_rawdevices );
163         vlc_restorecancel(canc);
164         if( i_status == 2 )
165         {
166             msleep( 5000000 );
167             i_status = 0;
168         }
169         else
170             msleep( 500000 );
171     }
172     return NULL;
173 }
174
175 /*****************************************************************************
176  * Everything else
177  *****************************************************************************/
178 static int AddDevice( services_discovery_t *p_sd,
179                       LIBMTP_raw_device_t *p_raw_device )
180 {
181     char *psz_name = NULL;
182     LIBMTP_mtpdevice_t *p_device;
183     LIBMTP_track_t *p_track, *p_tmp;
184
185     if( ( p_device = LIBMTP_Open_Raw_Device( p_raw_device ) ) != NULL )
186     {
187         if( !( psz_name = LIBMTP_Get_Friendlyname( p_device ) ) )
188             if( !( psz_name = LIBMTP_Get_Modelname( p_device ) ) )
189                 if( !( psz_name = strdup( N_( "MTP Device" ) ) ) )
190                     return VLC_ENOMEM;
191         msg_Info( p_sd, "Found device: %s", psz_name );
192         p_sd->p_sys->i_bus = p_raw_device->bus_location;
193         p_sd->p_sys->i_dev = p_raw_device->devnum;
194         p_sd->p_sys->i_product_id = p_raw_device->device_entry.product_id;
195         if( ( p_track = LIBMTP_Get_Tracklisting_With_Callback( p_device,
196                             CountTracks, p_sd ) ) == NULL )
197         {
198             msg_Warn( p_sd, "No tracks on the device" );
199         }
200         else
201         {
202             if( !( p_sd->p_sys->pp_items = calloc( p_sd->p_sys->i_tracks_num,
203                                                    sizeof( input_item_t * ) ) ) )
204             {
205                 free( psz_name );
206                 return VLC_ENOMEM;
207             }
208             p_sd->p_sys->i_count = 0;
209             while( p_track != NULL )
210             {
211                 msg_Dbg( p_sd, "Track found: %s - %s", p_track->artist,
212                          p_track->title );
213                 AddTrack( p_sd, p_track );
214                 p_tmp = p_track;
215                 p_track = p_track->next;
216                 LIBMTP_destroy_track_t( p_tmp );
217             }
218         }
219         p_sd->p_sys->psz_name = psz_name;
220         LIBMTP_Release_Device( p_device );
221         return VLC_SUCCESS;
222     }
223     else
224     {
225         msg_Info( p_sd, "The device seems to be mounted, unmount it first" );
226         return VLC_EGENERIC;
227     }
228 }
229
230 static void AddTrack( services_discovery_t *p_sd, LIBMTP_track_t *p_track )
231 {
232     input_item_t *p_input;
233     char *psz_string;
234     char *extension;
235
236     extension = rindex( p_track->filename, '.' );
237     if( asprintf( &psz_string, "mtp://%"PRIu32":%"PRIu8":%"PRIu16":%d%s",
238                   p_sd->p_sys->i_bus, p_sd->p_sys->i_dev,
239                   p_sd->p_sys->i_product_id, p_track->item_id,
240                   extension ) == -1 )
241     {
242         msg_Err( p_sd, "Error adding %s, skipping it", p_track->filename );
243         return;
244     }
245     if( ( p_input = input_item_New( psz_string, p_track->title ) ) == NULL )
246     {
247         msg_Err( p_sd, "Error adding %s, skipping it", p_track->filename );
248         free( psz_string );
249         return;
250     }
251     free( psz_string );
252
253     input_item_SetArtist( p_input, p_track->artist );
254     input_item_SetGenre( p_input, p_track->genre );
255     input_item_SetAlbum( p_input, p_track->album );
256     if( asprintf( &psz_string, "%d", p_track->tracknumber ) != -1 )
257     {
258         input_item_SetTrackNum( p_input, psz_string );
259         free( psz_string );
260     }
261     if( asprintf( &psz_string, "%d", p_track->rating ) != -1 )
262     {
263         input_item_SetRating( p_input, psz_string );
264         free( psz_string );
265     }
266     input_item_SetDate( p_input, p_track->date );
267     input_item_SetDuration( p_input, p_track->duration * 1000 );
268     services_discovery_AddItem( p_sd, p_input, NULL );
269     p_sd->p_sys->pp_items[p_sd->p_sys->i_count++] = p_input;
270 }
271
272 static void CloseDevice( services_discovery_t *p_sd )
273 {
274     input_item_t **pp_items = p_sd->p_sys->pp_items;
275
276     if( pp_items != NULL )
277     {
278         for( int i_i = 0; i_i < p_sd->p_sys->i_count; i_i++ )
279         {
280             if( pp_items[i_i] != NULL )
281             {
282                 services_discovery_RemoveItem( p_sd, pp_items[i_i] );
283                 vlc_gc_decref( pp_items[i_i] );
284             }
285         }
286         free( pp_items );
287     }
288 }
289
290 static int CountTracks( uint64_t const sent, uint64_t const total,
291                         void const * const data )
292 {
293     VLC_UNUSED( sent );
294     services_discovery_t *p_sd = (services_discovery_t *)data;
295     p_sd->p_sys->i_tracks_num = total;
296     return 0;
297 }