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