]> git.sesse.net Git - vlc/blob - modules/control/dbus/dbus.c
Reorganize the dbus control module code
[vlc] / modules / control / dbus / dbus.c
1 /*****************************************************************************
2  * dbus.c : D-Bus control interface
3  *****************************************************************************
4  * Copyright © 2006-2008 Rafaël Carré
5  * Copyright © 2007-2010 Mirsal Ennaime
6  * Copyright © 2009-2010 The VideoLAN team
7  * $Id$
8  *
9  * Authors:    Rafaël Carré <funman at videolanorg>
10  *             Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*
28  * D-Bus Specification:
29  *      http://dbus.freedesktop.org/doc/dbus-specification.html
30  * D-Bus low-level C API (libdbus)
31  *      http://dbus.freedesktop.org/doc/dbus/api/html/index.html
32  *  extract:
33  *   "If you use this low-level API directly, you're signing up for some pain."
34  *
35  * MPRIS Specification version 1.0
36  *      http://wiki.xmms2.xmms.se/index.php/MPRIS
37  */
38
39 /*****************************************************************************
40  * Preamble
41  *****************************************************************************/
42
43 #ifdef HAVE_CONFIG_H
44 # include "config.h"
45 #endif
46
47 #include <dbus/dbus.h>
48 #include "dbus.h"
49 #include "dbus_common.h"
50 #include "dbus_root.h"
51 #include "dbus_player.h"
52 #include "dbus_tracklist.h"
53
54 #include <vlc_common.h>
55 #include <vlc_plugin.h>
56 #include <vlc_interface.h>
57 #include <vlc_playlist.h>
58 #include <vlc_meta.h>
59
60 #include <assert.h>
61
62 /*****************************************************************************
63  * Local prototypes.
64  *****************************************************************************/
65
66 static int  Open    ( vlc_object_t * );
67 static void Close   ( vlc_object_t * );
68 static void Run     ( intf_thread_t * );
69
70 static int StateChange( intf_thread_t *, int );
71 static int TrackChange( intf_thread_t * );
72 static int AllCallback( vlc_object_t*, const char*, vlc_value_t, vlc_value_t, void* );
73
74 typedef struct
75 {
76     int signal;
77     int i_node;
78     int i_input_state;
79 } callback_info_t;
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 #define DBUS_UNIQUE_TEXT N_("Unique DBUS service id (org.mpris.vlc-<pid>)")
85 #define DBUS_UNIQUE_LONGTEXT N_( \
86     "Use a unique dbus service id to identify this VLC instance on the DBUS bus. " \
87     "The process identifier (PID) is added to the service name: org.mpris.vlc-<pid>" )
88
89 vlc_module_begin ()
90     set_shortname( N_("dbus"))
91     set_category( CAT_INTERFACE )
92     set_subcategory( SUBCAT_INTERFACE_CONTROL )
93     set_description( N_("D-Bus control interface") )
94     set_capability( "interface", 0 )
95     set_callbacks( Open, Close )
96     add_bool( "dbus-unique-service-id", false, NULL,
97               DBUS_UNIQUE_TEXT, DBUS_UNIQUE_LONGTEXT, true )
98 vlc_module_end ()
99
100 /*****************************************************************************
101  * Open: initialize interface
102  *****************************************************************************/
103
104 static int Open( vlc_object_t *p_this )
105 { /* initialisation of the connection */
106     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
107     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
108     playlist_t      *p_playlist;
109     DBusConnection  *p_conn;
110     DBusError       error;
111     char            *psz_service_name = NULL;
112
113     if( !p_sys )
114         return VLC_ENOMEM;
115
116     p_sys->b_meta_read = false;
117     p_sys->i_caps = CAPS_NONE;
118     p_sys->b_dead = false;
119     p_sys->p_input = NULL;
120
121     p_sys->b_unique = var_CreateGetBool( p_intf, "dbus-unique-service-id" );
122     if( p_sys->b_unique )
123     {
124         if( asprintf( &psz_service_name, "%s-%d",
125             DBUS_MPRIS_BUS_NAME, getpid() ) < 0 )
126         {
127             free( p_sys );
128             return VLC_ENOMEM;
129         }
130     }
131     else
132     {
133         psz_service_name = strdup(DBUS_MPRIS_BUS_NAME);
134     }
135
136     dbus_error_init( &error );
137
138     /* connect to the session bus */
139     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
140     if( !p_conn )
141     {
142         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
143                 error.message );
144         dbus_error_free( &error );
145         free( p_sys );
146         return VLC_EGENERIC;
147     }
148
149     /* register a well-known name on the bus */
150     dbus_bus_request_name( p_conn, psz_service_name, 0, &error );
151     if( dbus_error_is_set( &error ) )
152     {
153         msg_Err( p_this, "Error requesting service %s: %s",
154                  psz_service_name, error.message );
155         dbus_error_free( &error );
156         free( psz_service_name );
157         free( p_sys );
158         return VLC_EGENERIC;
159     }
160     msg_Info( p_intf, "listening on dbus as: %s", psz_service_name );
161     free( psz_service_name );
162
163     /* we register the objects */
164     dbus_connection_register_object_path( p_conn, DBUS_MPRIS_ROOT_PATH,
165             &dbus_mpris_root_vtable, p_this );
166     dbus_connection_register_object_path( p_conn, DBUS_MPRIS_PLAYER_PATH,
167             &dbus_mpris_player_vtable, p_this );
168     dbus_connection_register_object_path( p_conn, DBUS_MPRIS_TRACKLIST_PATH,
169             &dbus_mpris_tracklist_vtable, p_this );
170
171     dbus_connection_flush( p_conn );
172
173     p_intf->pf_run = Run;
174     p_intf->p_sys = p_sys;
175     p_sys->p_conn = p_conn;
176     p_sys->p_events = vlc_array_new();
177     vlc_mutex_init( &p_sys->lock );
178
179     p_playlist = pl_Get( p_intf );
180     p_sys->p_playlist = p_playlist;
181
182     var_AddCallback( p_playlist, "item-current", AllCallback, p_intf );
183     var_AddCallback( p_playlist, "intf-change", AllCallback, p_intf );
184     var_AddCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
185     var_AddCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
186     var_AddCallback( p_playlist, "random", AllCallback, p_intf );
187     var_AddCallback( p_playlist, "repeat", AllCallback, p_intf );
188     var_AddCallback( p_playlist, "loop", AllCallback, p_intf );
189
190     UpdateCaps( p_intf );
191
192     return VLC_SUCCESS;
193 }
194
195 /*****************************************************************************
196  * Close: destroy interface
197  *****************************************************************************/
198
199 static void Close   ( vlc_object_t *p_this )
200 {
201     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
202     intf_sys_t      *p_sys      = p_intf->p_sys;
203     playlist_t      *p_playlist = p_sys->p_playlist;
204
205     var_DelCallback( p_playlist, "item-current", AllCallback, p_intf );
206     var_DelCallback( p_playlist, "intf-change", AllCallback, p_intf );
207     var_DelCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
208     var_DelCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
209     var_DelCallback( p_playlist, "random", AllCallback, p_intf );
210     var_DelCallback( p_playlist, "repeat", AllCallback, p_intf );
211     var_DelCallback( p_playlist, "loop", AllCallback, p_intf );
212
213     if( p_sys->p_input )
214     {
215         var_DelCallback( p_sys->p_input, "state", AllCallback, p_intf );
216         vlc_object_release( p_sys->p_input );
217     }
218
219     dbus_connection_unref( p_sys->p_conn );
220
221     // Free the events array
222     for( int i = 0; i < vlc_array_count( p_sys->p_events ); i++ )
223     {
224         callback_info_t* info = vlc_array_item_at_index( p_sys->p_events, i );
225         free( info );
226     }
227     vlc_mutex_destroy( &p_sys->lock );
228     vlc_array_destroy( p_sys->p_events );
229     free( p_sys );
230 }
231
232 /*****************************************************************************
233  * Run: main loop
234  *****************************************************************************/
235
236 static void Run          ( intf_thread_t *p_intf )
237 {
238     for( ;; )
239     {
240         if( dbus_connection_get_dispatch_status(p_intf->p_sys->p_conn)
241                                              == DBUS_DISPATCH_COMPLETE )
242             msleep( INTF_IDLE_SLEEP );
243         int canc = vlc_savecancel();
244         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
245
246         /* Get the list of events to process
247          *
248          * We can't keep the lock on p_intf->p_sys->p_events, else we risk a
249          * deadlock:
250          * The signal functions could lock mutex X while p_events is locked;
251          * While some other function in vlc (playlist) might lock mutex X
252          * and then set a variable which would call AllCallback(), which itself
253          * needs to lock p_events to add a new event.
254          */
255         vlc_mutex_lock( &p_intf->p_sys->lock );
256         int i_events = vlc_array_count( p_intf->p_sys->p_events );
257         callback_info_t* info[i_events];
258         for( int i = i_events - 1; i >= 0; i-- )
259         {
260             info[i] = vlc_array_item_at_index( p_intf->p_sys->p_events, i );
261             vlc_array_remove( p_intf->p_sys->p_events, i );
262         }
263         vlc_mutex_unlock( &p_intf->p_sys->lock );
264
265         for( int i = 0; i < i_events; i++ )
266         {
267             switch( info[i]->signal )
268             {
269             case SIGNAL_ITEM_CURRENT:
270                 TrackChange( p_intf );
271                 break;
272             case SIGNAL_INTF_CHANGE:
273             case SIGNAL_PLAYLIST_ITEM_APPEND:
274             case SIGNAL_PLAYLIST_ITEM_DELETED:
275                 TrackListChangeEmit( p_intf, info[i]->signal, info[i]->i_node );
276                 break;
277             case SIGNAL_RANDOM:
278             case SIGNAL_REPEAT:
279             case SIGNAL_LOOP:
280                 StatusChangeEmit( p_intf );
281                 break;
282             case SIGNAL_STATE:
283                 StateChange( p_intf, info[i]->i_input_state );
284                 break;
285             default:
286                 assert(0);
287             }
288             free( info[i] );
289         }
290         vlc_restorecancel( canc );
291     }
292 }
293
294 /*****************************************************************************
295  * UpdateCaps: update p_sys->i_caps
296  * This function have to be called with the playlist unlocked
297  ****************************************************************************/
298 int UpdateCaps( intf_thread_t* p_intf )
299 {
300     intf_sys_t* p_sys = p_intf->p_sys;
301     dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
302     playlist_t* p_playlist = p_sys->p_playlist;
303
304     PL_LOCK;
305     if( p_playlist->current.i_size > 0 )
306         i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
307     PL_UNLOCK;
308
309     input_thread_t* p_input = playlist_CurrentInput( p_playlist );
310     if( p_input )
311     {
312         /* XXX: if UpdateCaps() is called too early, these are
313          * unconditionnaly true */
314         if( var_GetBool( p_input, "can-pause" ) )
315             i_caps |= CAPS_CAN_PAUSE;
316         if( var_GetBool( p_input, "can-seek" ) )
317             i_caps |= CAPS_CAN_SEEK;
318         vlc_object_release( p_input );
319     }
320
321     if( p_sys->b_meta_read )
322         i_caps |= CAPS_CAN_PROVIDE_METADATA;
323
324     if( i_caps != p_intf->p_sys->i_caps )
325     {
326         p_sys->i_caps = i_caps;
327         CapsChangeEmit( p_intf );
328     }
329
330     return VLC_SUCCESS;
331 }
332
333 // Get all the callbacks
334 static int AllCallback( vlc_object_t *p_this, const char *psz_var,
335                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
336 {
337     (void)p_this;
338     (void)oldval;
339     intf_thread_t *p_intf = (intf_thread_t*)p_data;
340
341     callback_info_t *info = malloc( sizeof( callback_info_t ) );
342     if( !info )
343         return VLC_ENOMEM;
344
345     // Wich event is it ?
346     if( !strcmp( "item-current", psz_var ) )
347         info->signal = SIGNAL_ITEM_CURRENT;
348     else if( !strcmp( "intf-change", psz_var ) )
349         info->signal = SIGNAL_INTF_CHANGE;
350     else if( !strcmp( "playlist-item-append", psz_var ) )
351     {
352         info->signal = SIGNAL_PLAYLIST_ITEM_APPEND;
353         info->i_node = ((playlist_add_t*)newval.p_address)->i_node;
354     }
355     else if( !strcmp( "playlist-item-deleted", psz_var ) )
356         info->signal = SIGNAL_PLAYLIST_ITEM_DELETED;
357     else if( !strcmp( "random", psz_var ) )
358         info->signal = SIGNAL_RANDOM;
359     else if( !strcmp( "repeat", psz_var ) )
360         info->signal = SIGNAL_REPEAT;
361     else if( !strcmp( "loop", psz_var ) )
362         info->signal = SIGNAL_LOOP;
363     else if( !strcmp( "state", psz_var ) )
364     {
365         info->signal = SIGNAL_STATE;
366         info->i_input_state = newval.i_int;
367     }
368     else
369         assert(0);
370
371     // Append the event
372     vlc_mutex_lock( &p_intf->p_sys->lock );
373     vlc_array_append( p_intf->p_sys->p_events, info );
374     vlc_mutex_unlock( &p_intf->p_sys->lock );
375     return VLC_SUCCESS;
376 }
377
378 /*****************************************************************************
379  * StateChange: callback on input "state"
380  *****************************************************************************/
381 //static int StateChange( vlc_object_t *p_this, const char* psz_var,
382 //            vlc_value_t oldval, vlc_value_t newval, void *p_data )
383 static int StateChange( intf_thread_t *p_intf, int i_input_state )
384 {
385     intf_sys_t          *p_sys      = p_intf->p_sys;
386     playlist_t          *p_playlist = p_sys->p_playlist;
387     input_thread_t      *p_input;
388     input_item_t        *p_item;
389
390     if( p_intf->p_sys->b_dead )
391         return VLC_SUCCESS;
392
393     UpdateCaps( p_intf );
394
395     if( !p_sys->b_meta_read && i_input_state == PLAYING_S )
396     {
397         p_input = playlist_CurrentInput( p_playlist );
398         if( p_input )
399         {
400             p_item = input_GetItem( p_input );
401             if( p_item )
402             {
403                 p_sys->b_meta_read = true;
404                 TrackChangeEmit( p_intf, p_item );
405             }
406             vlc_object_release( p_input );
407         }
408     }
409
410     if( i_input_state == PLAYING_S || i_input_state == PAUSE_S ||
411         i_input_state == END_S )
412     {
413         StatusChangeEmit( p_intf );
414     }
415
416     return VLC_SUCCESS;
417 }
418
419 /*****************************************************************************
420  * TrackChange: callback on playlist "item-current"
421  *****************************************************************************/
422 static int TrackChange( intf_thread_t *p_intf )
423 {
424     intf_sys_t          *p_sys      = p_intf->p_sys;
425     playlist_t          *p_playlist = p_sys->p_playlist;
426     input_thread_t      *p_input    = NULL;
427     input_item_t        *p_item     = NULL;
428
429     if( p_intf->p_sys->b_dead )
430         return VLC_SUCCESS;
431
432     if( p_sys->p_input )
433     {
434         var_DelCallback( p_sys->p_input, "state", AllCallback, p_intf );
435         vlc_object_release( p_sys->p_input );
436         p_sys->p_input = NULL;
437     }
438
439     p_sys->b_meta_read = false;
440
441     p_input = playlist_CurrentInput( p_playlist );
442     if( !p_input )
443     {
444         return VLC_SUCCESS;
445     }
446
447     p_item = input_GetItem( p_input );
448     if( !p_item )
449     {
450         vlc_object_release( p_input );
451         return VLC_EGENERIC;
452     }
453
454     if( input_item_IsPreparsed( p_item ) )
455     {
456         p_sys->b_meta_read = true;
457         TrackChangeEmit( p_intf, p_item );
458     }
459
460     p_sys->p_input = p_input;
461     var_AddCallback( p_input, "state", AllCallback, p_intf );
462
463     return VLC_SUCCESS;
464 }
465
466 /*****************************************************************************
467  * GetInputMeta: Fill a DBusMessage with the given input item metadata
468  *****************************************************************************/
469
470 #define ADD_META( entry, type, data ) \
471     if( data ) { \
472         dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
473                 NULL, &dict_entry ); \
474         dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
475                 &ppsz_meta_items[entry] ); \
476         dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
477                 type##_AS_STRING, &variant ); \
478         dbus_message_iter_append_basic( &variant, \
479                 type, \
480                 & data ); \
481         dbus_message_iter_close_container( &dict_entry, &variant ); \
482         dbus_message_iter_close_container( &dict, &dict_entry ); }
483
484 #define ADD_VLC_META_STRING( entry, item ) \
485     { \
486         char * psz = input_item_Get##item( p_input );\
487         ADD_META( entry, DBUS_TYPE_STRING, \
488                   psz ); \
489         free( psz ); \
490     }
491
492 int GetInputMeta( input_item_t* p_input,
493                         DBusMessageIter *args )
494 {
495     DBusMessageIter dict, dict_entry, variant;
496     /** The duration of the track can be expressed in second, milli-seconds and
497         µ-seconds */
498     dbus_int64_t i_mtime = input_item_GetDuration( p_input );
499     dbus_uint32_t i_time = i_mtime / 1000000;
500     dbus_int64_t i_length = i_mtime / 1000;
501
502     const char* ppsz_meta_items[] =
503     {
504     /* Official MPRIS metas */
505     "location", "title", "artist", "album", "tracknumber", "time", "mtime",
506     "genre", "rating", "date", "arturl",
507     "audio-bitrate", "audio-samplerate", "video-bitrate",
508     /* VLC specifics metas */
509     "audio-codec", "copyright", "description", "encodedby", "language", "length",
510     "nowplaying", "publisher", "setting", "status", "trackid", "url",
511     "video-codec"
512     };
513
514     dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
515
516     ADD_VLC_META_STRING( 0,  URI );
517     ADD_VLC_META_STRING( 1,  Title );
518     ADD_VLC_META_STRING( 2,  Artist );
519     ADD_VLC_META_STRING( 3,  Album );
520     ADD_VLC_META_STRING( 4,  TrackNum );
521     ADD_META( 5, DBUS_TYPE_UINT32, i_time );
522     ADD_META( 6, DBUS_TYPE_UINT32, i_mtime );
523     ADD_VLC_META_STRING( 7,  Genre );
524     ADD_VLC_META_STRING( 8,  Rating );
525     ADD_VLC_META_STRING( 9,  Date );
526     ADD_VLC_META_STRING( 10, ArtURL );
527
528     ADD_VLC_META_STRING( 15, Copyright );
529     ADD_VLC_META_STRING( 16, Description );
530     ADD_VLC_META_STRING( 17, EncodedBy );
531     ADD_VLC_META_STRING( 18, Language );
532     ADD_META( 19, DBUS_TYPE_INT64, i_length );
533     ADD_VLC_META_STRING( 20, NowPlaying );
534     ADD_VLC_META_STRING( 21, Publisher );
535     ADD_VLC_META_STRING( 22, Setting );
536     ADD_VLC_META_STRING( 24, TrackID );
537     ADD_VLC_META_STRING( 25, URL );
538
539     vlc_mutex_lock( &p_input->lock );
540     if( p_input->p_meta )
541     {
542         int i_status = vlc_meta_GetStatus( p_input->p_meta );
543         ADD_META( 23, DBUS_TYPE_INT32, i_status );
544     }
545     vlc_mutex_unlock( &p_input->lock );
546
547     dbus_message_iter_close_container( args, &dict );
548     return VLC_SUCCESS;
549 }
550
551 #undef ADD_META
552 #undef ADD_VLC_META_STRING
553
554