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