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