]> git.sesse.net Git - vlc/blob - modules/services_discovery/mtp.c
The upnp module is called upnp_intel
[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_playlist.h>
29 #include <vlc_plugin.h>
30 #include <vlc_interface.h>
31 #include <vlc_services_discovery.h>
32
33 #include "libmtp.h"
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int Open( vlc_object_t * );
39 static void Close( vlc_object_t * );
40
41 VLC_SD_PROBE_HELPER("mtp", "MTP devices", SD_CAT_DEVICES)
42
43 vlc_module_begin()
44     set_shortname( "MTP" )
45     set_description( N_( "MTP devices" ) )
46     set_category( CAT_PLAYLIST )
47     set_subcategory( SUBCAT_PLAYLIST_SD )
48     set_capability( "services_discovery", 0 )
49     set_callbacks( Open, Close )
50     cannot_unload_broken_library()
51
52     VLC_SD_PROBE_SUBMODULE
53 vlc_module_end()
54
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59
60 static void *Run( void * );
61
62 static int AddDevice( services_discovery_t *, LIBMTP_raw_device_t * );
63 static void AddTrack( services_discovery_t *, LIBMTP_track_t *);
64 static void CloseDevice( services_discovery_t * );
65 static int CountTracks( uint64_t const, uint64_t const, void const * const );
66
67 /*****************************************************************************
68  * Local structures
69  *****************************************************************************/
70
71 struct services_discovery_sys_t
72 {
73     int i_tracks_num;
74     input_item_t **pp_items;
75     int i_count;
76     char *psz_name;
77     uint32_t i_bus;
78     uint8_t i_dev;
79     uint16_t i_product_id;
80     vlc_thread_t thread;
81 };
82
83 static vlc_mutex_t mtp_lock = VLC_STATIC_MUTEX;
84 static bool b_mtp_initialized = false;
85
86 /*****************************************************************************
87  * Open: initialize and create stuff
88  *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
90 {
91     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
92     services_discovery_sys_t *p_sys;
93
94     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
95         return VLC_ENOMEM;
96     p_sd->p_sys = p_sys;
97     p_sys->psz_name = NULL;
98
99     vlc_mutex_lock( &mtp_lock );
100     if( !b_mtp_initialized )
101     {
102         LIBMTP_Init();
103         b_mtp_initialized = true;
104     }
105     vlc_mutex_unlock( &mtp_lock );
106
107     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
108     {
109         free (p_sys);
110         return VLC_EGENERIC;
111     }
112     return VLC_SUCCESS;
113 }
114
115 /*****************************************************************************
116  * Close: cleanup
117  *****************************************************************************/
118 static void Close( vlc_object_t *p_this )
119 {
120     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
121
122     free( p_sd->p_sys->psz_name );
123     vlc_cancel (p_sd->p_sys->thread);
124     vlc_join (p_sd->p_sys->thread, NULL);
125     free( p_sd->p_sys );
126 }
127
128 /*****************************************************************************
129  * Run: main thread
130  *****************************************************************************/
131 static void *Run( void *data )
132 {
133     LIBMTP_raw_device_t *p_rawdevices;
134     int i_numrawdevices;
135     int i_ret;
136     int i_status = 0;
137     services_discovery_t *p_sd = data;
138
139     for(;;)
140     {
141         int canc = vlc_savecancel();
142         i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
143         if ( i_ret == 0 && i_numrawdevices > 0 && p_rawdevices != NULL &&
144              i_status == 0 )
145         {
146             /* Found a new device, add it */
147             msg_Dbg( p_sd, "New device found" );
148             if( AddDevice( p_sd, &p_rawdevices[0] ) == VLC_SUCCESS )
149                 i_status = 1;
150             else
151                 i_status = 2;
152         }
153         else
154         {
155             if ( ( i_ret != 0 || i_numrawdevices == 0 || p_rawdevices == NULL )
156                  && i_status == 1)
157             {
158                 /* The device is not connected anymore, delete it */
159                 msg_Info( p_sd, "Device disconnected" );
160                 CloseDevice( p_sd );
161                 i_status = 0;
162             }
163         }
164         free( p_rawdevices );
165         vlc_restorecancel(canc);
166         if( i_status == 2 )
167         {
168             msleep( 5000000 );
169             i_status = 0;
170         }
171         else
172             msleep( 500000 );
173     }
174     return NULL;
175 }
176
177 /*****************************************************************************
178  * Everything else
179  *****************************************************************************/
180 static int AddDevice( services_discovery_t *p_sd,
181                       LIBMTP_raw_device_t *p_raw_device )
182 {
183     char *psz_name = NULL;
184     LIBMTP_mtpdevice_t *p_device;
185     LIBMTP_track_t *p_track, *p_tmp;
186
187     if( ( p_device = LIBMTP_Open_Raw_Device( p_raw_device ) ) != NULL )
188     {
189         if( !( psz_name = LIBMTP_Get_Friendlyname( p_device ) ) )
190             if( !( psz_name = LIBMTP_Get_Modelname( p_device ) ) )
191                 if( !( psz_name = strdup( N_( "MTP Device" ) ) ) )
192                     return VLC_ENOMEM;
193         msg_Info( p_sd, "Found device: %s", psz_name );
194         p_sd->p_sys->i_bus = p_raw_device->bus_location;
195         p_sd->p_sys->i_dev = p_raw_device->devnum;
196         p_sd->p_sys->i_product_id = p_raw_device->device_entry.product_id;
197         if( ( p_track = LIBMTP_Get_Tracklisting_With_Callback( p_device,
198                             CountTracks, p_sd ) ) == NULL )
199         {
200             msg_Warn( p_sd, "No tracks on the device" );
201         }
202         else
203         {
204             if( !( p_sd->p_sys->pp_items = calloc( p_sd->p_sys->i_tracks_num,
205                                                    sizeof( input_item_t * ) ) ) )
206             {
207                 free( psz_name );
208                 return VLC_ENOMEM;
209             }
210             p_sd->p_sys->i_count = 0;
211             while( p_track != NULL )
212             {
213                 msg_Dbg( p_sd, "Track found: %s - %s", p_track->artist,
214                          p_track->title );
215                 AddTrack( p_sd, p_track );
216                 p_tmp = p_track;
217                 p_track = p_track->next;
218                 LIBMTP_destroy_track_t( p_tmp );
219             }
220         }
221         p_sd->p_sys->psz_name = psz_name;
222         LIBMTP_Release_Device( p_device );
223         return VLC_SUCCESS;
224     }
225     else
226     {
227         msg_Info( p_sd, "The device seems to be mounted, unmount it first" );
228         return VLC_EGENERIC;
229     }
230 }
231
232 static void AddTrack( services_discovery_t *p_sd, LIBMTP_track_t *p_track )
233 {
234     input_item_t *p_input;
235     char *psz_string;
236     char *extension;
237
238     extension = rindex( p_track->filename, '.' );
239     if( asprintf( &psz_string, "mtp://%"PRIu32":%"PRIu8":%"PRIu16":%d%s",
240                   p_sd->p_sys->i_bus, p_sd->p_sys->i_dev,
241                   p_sd->p_sys->i_product_id, p_track->item_id,
242                   extension ) == -1 )
243     {
244         msg_Err( p_sd, "Error adding %s, skipping it", p_track->filename );
245         return;
246     }
247     if( ( p_input = input_item_New( p_sd, psz_string,
248                                     p_track->title ) ) == NULL )
249     {
250         msg_Err( p_sd, "Error adding %s, skipping it", p_track->filename );
251         free( psz_string );
252         return;
253     }
254     free( psz_string );
255
256     input_item_SetArtist( p_input, p_track->artist );
257     input_item_SetGenre( p_input, p_track->genre );
258     input_item_SetAlbum( p_input, p_track->album );
259     if( asprintf( &psz_string, "%d", p_track->tracknumber ) != -1 )
260     {
261         input_item_SetTrackNum( p_input, psz_string );
262         free( psz_string );
263     }
264     if( asprintf( &psz_string, "%d", p_track->rating ) != -1 )
265     {
266         input_item_SetRating( p_input, psz_string );
267         free( psz_string );
268     }
269     input_item_SetDate( p_input, p_track->date );
270     input_item_SetDuration( p_input, p_track->duration * 1000 );
271     services_discovery_AddItem( p_sd, p_input, NULL );
272     p_sd->p_sys->pp_items[p_sd->p_sys->i_count++] = p_input;
273 }
274
275 static void CloseDevice( services_discovery_t *p_sd )
276 {
277     input_item_t **pp_items = p_sd->p_sys->pp_items;
278     int i_i;
279
280     if( pp_items != NULL )
281     {
282         for( i_i = 0; i_i < p_sd->p_sys->i_count; i_i++ )
283         {
284             if( pp_items[i_i] != NULL )
285             {
286                 services_discovery_RemoveItem( p_sd, pp_items[i_i] );
287                 vlc_gc_decref( pp_items[i_i] );
288             }
289         }
290         free( pp_items );
291     }
292 }
293
294 static int CountTracks( uint64_t const sent, uint64_t const total,
295                         void const * const data )
296 {
297     VLC_UNUSED( sent );
298     services_discovery_t *p_sd = (services_discovery_t *)data;
299     p_sd->p_sys->i_tracks_num = total;
300     return 0;
301 }