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