1 /*****************************************************************************
2 * dbus.c : D-Bus control interface
3 *****************************************************************************
4 * Copyright © 2006-2008 Rafaël Carré
5 * Copyright © 2007 Mirsal Ennaime
8 * Authors: Rafaël Carré <funman at videolanorg>
9 * Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
27 * D-Bus Specification:
28 * http://dbus.freedesktop.org/doc/dbus-specification.html
29 * D-Bus low-level C API (libdbus)
30 * http://dbus.freedesktop.org/doc/dbus/api/html/index.html
32 * "If you use this low-level API directly, you're signing up for some pain."
34 * MPRIS Specification (still drafting on Jan, 23 of 2008):
35 * http://wiki.xmms2.xmms.se/index.php/MPRIS
38 /*****************************************************************************
40 *****************************************************************************/
42 #include <dbus/dbus.h>
52 #include <vlc_interface.h>
54 #include <vlc_input.h>
55 #include <vlc_playlist.h>
56 #include <vlc_demux.h>
57 #include <vlc_access.h>
59 /*****************************************************************************
61 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
65 static void Run ( intf_thread_t * );
67 static int StateChange( vlc_object_t *, const char *, vlc_value_t,
68 vlc_value_t, void * );
70 static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
71 vlc_value_t, void * );
73 static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
74 vlc_value_t, void * );
76 static int GetInputMeta ( input_item_t *, DBusMessageIter * );
77 static int MarshalStatus ( intf_thread_t *, DBusMessageIter *, vlc_bool_t );
79 /* GetCaps() capabilities */
83 CAPS_CAN_GO_NEXT = 1 << 0,
84 CAPS_CAN_GO_PREV = 1 << 1,
85 CAPS_CAN_PAUSE = 1 << 2,
86 CAPS_CAN_PLAY = 1 << 3,
87 CAPS_CAN_SEEK = 1 << 4,
88 CAPS_CAN_PROVIDE_METADATA = 1 << 5
93 DBusConnection *p_conn;
94 vlc_bool_t b_meta_read;
98 /*****************************************************************************
100 *****************************************************************************/
103 set_shortname( _("dbus"));
104 set_category( CAT_INTERFACE );
105 set_subcategory( SUBCAT_INTERFACE_CONTROL );
106 set_description( _("D-Bus control interface") );
107 set_capability( "interface", 0 );
108 set_callbacks( Open, Close );
111 /*****************************************************************************
113 *****************************************************************************/
120 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
121 playlist_Stop( p_playlist );
122 pl_Release( ((vlc_object_t*) p_this) );
123 vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
127 DBUS_METHOD( PositionGet )
128 { /* returns position in milliseconds */
131 vlc_value_t position;
134 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
136 input_thread_t *p_input = p_playlist->p_input;
142 var_Get( p_input, "time", &position );
143 i_pos = position.i_time / 1000;
146 pl_Release( ((vlc_object_t*) p_this) );
151 DBUS_METHOD( PositionSet )
152 { /* set position in milliseconds */
155 vlc_value_t position;
156 playlist_t* p_playlist = NULL;
160 dbus_error_init( &error );
162 dbus_message_get_args( p_from, &error,
163 DBUS_TYPE_INT32, &i_pos,
166 if( dbus_error_is_set( &error ) )
168 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
170 dbus_error_free( &error );
171 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
173 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
175 input_thread_t *p_input = p_playlist->p_input;
179 position.i_time = i_pos * 1000;
180 var_Set( p_input, "time", position );
183 pl_Release( ((vlc_object_t*) p_this) );
187 DBUS_METHOD( VolumeGet )
188 { /* returns volume in percentage */
191 dbus_int32_t i_dbus_vol;
192 audio_volume_t i_vol;
193 /* 2nd argument of aout_VolumeGet is int32 */
194 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
195 i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
196 ADD_INT32( &i_dbus_vol );
200 DBUS_METHOD( VolumeSet )
201 { /* set volume in percentage */
205 dbus_error_init( &error );
207 dbus_int32_t i_dbus_vol;
208 audio_volume_t i_vol;
210 dbus_message_get_args( p_from, &error,
211 DBUS_TYPE_INT32, &i_dbus_vol,
214 if( dbus_error_is_set( &error ) )
216 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
218 dbus_error_free( &error );
219 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
222 i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
223 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
229 { /* next playlist item */
231 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
232 playlist_Next( p_playlist );
233 pl_Release( ((vlc_object_t*) p_this) );
238 { /* previous playlist item */
240 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
241 playlist_Prev( p_playlist );
242 pl_Release( ((vlc_object_t*) p_this) );
249 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
250 playlist_Stop( p_playlist );
251 pl_Release( ((vlc_object_t*) p_this) );
255 DBUS_METHOD( GetStatus )
256 { /* returns the current status as a struct of 4 ints */
258 First 0 = Playing, 1 = Paused, 2 = Stopped.
259 Second 0 = Playing linearly , 1 = Playing randomly.
260 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
261 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
266 MarshalStatus( p_this, &args, VLC_TRUE );
274 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
275 playlist_Pause( p_playlist );
276 pl_Release( p_playlist );
283 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
284 playlist_Play( p_playlist );
285 pl_Release( p_playlist );
289 DBUS_METHOD( GetCurrentMetadata )
293 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
295 if( p_playlist->status.p_item )
296 GetInputMeta( p_playlist->status.p_item->p_input, &args );
298 pl_Release( p_playlist );
302 DBUS_METHOD( GetCaps )
306 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
309 dbus_int32_t i_caps = CAPS_NONE;
312 * Every capability should be checked in a callback, modifying p_sys->i_caps
313 * so we can send a signal whenever it changes.
314 * When it is done, GetCaps method will just return p_sys->i_caps
317 if( p_playlist->items.i_size > 0 )
318 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
320 if( p_playlist->p_input )
322 access_t *p_access = (access_t*)vlc_object_find( p_playlist->p_input,
323 VLC_OBJECT_ACCESS, FIND_CHILD );
326 vlc_bool_t b_can_pause;
327 if( !access2_Control( p_access, ACCESS_CAN_PAUSE, &b_can_pause ) &&
329 i_caps |= CAPS_CAN_PAUSE;
330 vlc_object_release( p_access );
332 demux_t *p_demux = (demux_t*)vlc_object_find( p_playlist->p_input,
333 VLC_OBJECT_DEMUX, FIND_CHILD );
335 { /* XXX: is: demux can seek and access can not a possibility ? */
336 vlc_bool_t b_can_seek;
337 if( !stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek ) &&
339 i_caps |= CAPS_CAN_SEEK;
340 vlc_object_release( p_demux );
344 if( ((intf_thread_t*)p_this)->p_sys->b_meta_read )
345 i_caps |= CAPS_CAN_PROVIDE_METADATA;
349 ADD_INT32( &i_caps );
354 /* Media Player information */
356 DBUS_METHOD( Identity )
362 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
364 ADD_STRING( &psz_identity );
365 free( psz_identity );
368 return DBUS_HANDLER_RESULT_NEED_MEMORY;
375 DBUS_METHOD( AddTrack )
376 { /* add the string to the playlist, and play it if the boolean is true */
380 dbus_error_init( &error );
381 playlist_t* p_playlist = NULL;
386 dbus_message_get_args( p_from, &error,
387 DBUS_TYPE_STRING, &psz_mrl,
388 DBUS_TYPE_BOOLEAN, &b_play,
391 if( dbus_error_is_set( &error ) )
393 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
395 dbus_error_free( &error );
396 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
399 p_playlist = pl_Yield( (vlc_object_t*) p_this );
400 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
401 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
402 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
403 pl_Release( p_playlist );
408 DBUS_METHOD( GetCurrentTrack )
413 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
414 dbus_int32_t i_position = p_playlist->i_current_index;
415 pl_Release( p_playlist );
417 ADD_INT32( &i_position );
421 DBUS_METHOD( GetMetadata )
426 dbus_error_init( &error );
428 dbus_int32_t i_position;
430 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
433 dbus_message_get_args( p_from, &error,
434 DBUS_TYPE_INT32, &i_position,
437 if( dbus_error_is_set( &error ) )
440 pl_Release( p_playlist );
441 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
443 dbus_error_free( &error );
444 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
447 if( i_position <= p_playlist->items.i_size / 2 )
449 GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->p_input, &args );
453 pl_Release( p_playlist );
457 DBUS_METHOD( GetLength )
462 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
463 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
464 pl_Release( p_playlist );
466 ADD_INT32( &i_elements );
470 DBUS_METHOD( DelTrack )
475 dbus_error_init( &error );
477 dbus_int32_t i_position;
478 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
480 dbus_message_get_args( p_from, &error,
481 DBUS_TYPE_INT32, &i_position,
484 if( dbus_error_is_set( &error ) )
486 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
488 dbus_error_free( &error );
489 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
492 if( i_position <= p_playlist->items.i_size / 2 )
494 playlist_DeleteFromInput( p_playlist,
495 p_playlist->items.p_elems[i_position*2-1]->i_id,
499 pl_Release( p_playlist );
512 playlist_t* p_playlist = NULL;
514 dbus_error_init( &error );
515 dbus_message_get_args( p_from, &error,
516 DBUS_TYPE_BOOLEAN, &b_loop,
519 if( dbus_error_is_set( &error ) )
521 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
523 dbus_error_free( &error );
524 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
527 val.b_bool = ( b_loop == TRUE ) ? VLC_TRUE : VLC_FALSE ;
528 p_playlist = pl_Yield( (vlc_object_t*) p_this );
529 var_Set ( p_playlist, "loop", val );
530 pl_Release( ((vlc_object_t*) p_this) );
535 DBUS_METHOD( Repeat )
541 dbus_bool_t b_repeat;
543 playlist_t* p_playlist = NULL;
545 dbus_error_init( &error );
546 dbus_message_get_args( p_from, &error,
547 DBUS_TYPE_BOOLEAN, &b_repeat,
550 if( dbus_error_is_set( &error ) )
552 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
554 dbus_error_free( &error );
555 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
558 val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
560 p_playlist = pl_Yield( (vlc_object_t*) p_this );
561 var_Set ( p_playlist, "repeat", val );
562 pl_Release( ((vlc_object_t*) p_this) );
567 DBUS_METHOD( Random )
573 dbus_bool_t b_random;
575 playlist_t* p_playlist = NULL;
577 dbus_error_init( &error );
578 dbus_message_get_args( p_from, &error,
579 DBUS_TYPE_BOOLEAN, &b_random,
582 if( dbus_error_is_set( &error ) )
584 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
586 dbus_error_free( &error );
587 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
590 val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
592 p_playlist = pl_Yield( (vlc_object_t*) p_this );
593 var_Set ( p_playlist, "random", val );
594 pl_Release( ((vlc_object_t*) p_this) );
598 /*****************************************************************************
599 * Introspection method
600 *****************************************************************************/
602 DBUS_METHOD( handle_introspect_root )
603 { /* handles introspection of root object */
607 ADD_STRING( &psz_introspection_xml_data_root );
611 DBUS_METHOD( handle_introspect_player )
616 ADD_STRING( &psz_introspection_xml_data_player );
620 DBUS_METHOD( handle_introspect_tracklist )
625 ADD_STRING( &psz_introspection_xml_data_tracklist );
629 /*****************************************************************************
630 * handle_*: answer to incoming messages
631 *****************************************************************************/
633 #define METHOD_FUNC( method, function ) \
634 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
635 return function( p_conn, p_from, p_this )
637 DBUS_METHOD( handle_root )
640 if( dbus_message_is_method_call( p_from,
641 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
642 return handle_introspect_root( p_conn, p_from, p_this );
644 /* here D-Bus method's names are associated to an handler */
646 METHOD_FUNC( "Identity", Identity );
648 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
652 DBUS_METHOD( handle_player )
654 if( dbus_message_is_method_call( p_from,
655 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
656 return handle_introspect_player( p_conn, p_from, p_this );
658 /* here D-Bus method's names are associated to an handler */
660 METHOD_FUNC( "Prev", Prev );
661 METHOD_FUNC( "Next", Next );
662 METHOD_FUNC( "Quit", Quit );
663 METHOD_FUNC( "Stop", Stop );
664 METHOD_FUNC( "Play", Play );
665 METHOD_FUNC( "Pause", Pause );
666 METHOD_FUNC( "Repeat", Repeat );
667 METHOD_FUNC( "VolumeSet", VolumeSet );
668 METHOD_FUNC( "VolumeGet", VolumeGet );
669 METHOD_FUNC( "PositionSet", PositionSet );
670 METHOD_FUNC( "PositionGet", PositionGet );
671 METHOD_FUNC( "GetStatus", GetStatus );
672 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
673 METHOD_FUNC( "GetCaps", GetCaps );
675 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
678 DBUS_METHOD( handle_tracklist )
680 if( dbus_message_is_method_call( p_from,
681 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
682 return handle_introspect_tracklist( p_conn, p_from, p_this );
684 /* here D-Bus method's names are associated to an handler */
686 METHOD_FUNC( "GetMetadata", GetMetadata );
687 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
688 METHOD_FUNC( "GetLength", GetLength );
689 METHOD_FUNC( "AddTrack", AddTrack );
690 METHOD_FUNC( "DelTrack", DelTrack );
691 METHOD_FUNC( "Loop", Loop );
692 METHOD_FUNC( "Random", Random );
694 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
697 /*****************************************************************************
698 * Open: initialize interface
699 *****************************************************************************/
701 static int Open( vlc_object_t *p_this )
702 { /* initialisation of the connection */
703 intf_thread_t *p_intf = (intf_thread_t*)p_this;
704 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
705 playlist_t *p_playlist;
706 DBusConnection *p_conn;
712 p_sys->b_meta_read = VLC_FALSE;
713 p_sys->i_caps = CAPS_NONE;
715 dbus_error_init( &error );
717 /* connect to the session bus */
718 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
721 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
723 dbus_error_free( &error );
728 /* register a well-known name on the bus */
729 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
730 if( dbus_error_is_set( &error ) )
732 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
733 ": %s", error.message );
734 dbus_error_free( &error );
739 /* we register the objects */
740 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
741 &vlc_dbus_root_vtable, p_this );
742 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
743 &vlc_dbus_player_vtable, p_this );
744 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
745 &vlc_dbus_tracklist_vtable, p_this );
747 dbus_connection_flush( p_conn );
749 p_playlist = pl_Yield( p_intf );
751 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
752 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
753 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
754 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
756 pl_Release( p_playlist );
758 p_intf->pf_run = Run;
759 p_intf->p_sys = p_sys;
760 p_sys->p_conn = p_conn;
765 /*****************************************************************************
766 * Close: destroy interface
767 *****************************************************************************/
769 static void Close ( vlc_object_t *p_this )
771 intf_thread_t *p_intf = (intf_thread_t*) p_this;
772 playlist_t *p_playlist = pl_Yield( p_intf );;
773 input_thread_t *p_input;
775 p_this->b_dead = VLC_TRUE;
778 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
779 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
780 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
781 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
783 p_input = p_playlist->p_input;
786 vlc_object_yield( p_input );
787 var_DelCallback( p_input, "state", StateChange, p_intf );
788 vlc_object_release( p_input );
792 pl_Release( p_playlist );
794 dbus_connection_unref( p_intf->p_sys->p_conn );
796 free( p_intf->p_sys );
799 /*****************************************************************************
801 *****************************************************************************/
803 static void Run ( intf_thread_t *p_intf )
805 while( !intf_ShouldDie( p_intf ) )
807 msleep( INTF_IDLE_SLEEP );
808 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
812 /*****************************************************************************
813 * TrackChange: Playlist item change callback
814 *****************************************************************************/
816 DBUS_SIGNAL( TrackChangeSignal )
817 { /* emit the metadata of the new item */
818 SIGNAL_INIT( "TrackChange" );
821 input_item_t *p_item = (input_item_t*) p_data;
822 GetInputMeta ( p_item, &args );
827 /*****************************************************************************
828 * StatusChange: Player status change signal
829 *****************************************************************************/
831 DBUS_SIGNAL( StatusChangeSignal )
832 { /* send the updated status info on the bus */
833 SIGNAL_INIT( "StatusChange" );
836 /* we're called from a callback of input_thread_t, so it can not be
837 * destroyed before we return */
838 MarshalStatus( (intf_thread_t*) p_data, &args, VLC_FALSE );
843 /*****************************************************************************
844 * StateChange: callback on input "state"
845 *****************************************************************************/
846 static int StateChange( vlc_object_t *p_this, const char* psz_var,
847 vlc_value_t oldval, vlc_value_t newval, void *p_data )
849 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
850 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
851 intf_sys_t *p_sys = p_intf->p_sys;
856 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
858 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
861 p_sys->b_meta_read = VLC_TRUE;
862 TrackChangeSignal( p_sys->p_conn, p_item );
866 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
867 newval.i_int == END_S )
869 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
875 /*****************************************************************************
876 * StatusChangeEmit: Emits the StatusChange signal
877 *****************************************************************************/
878 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
879 vlc_value_t oldval, vlc_value_t newval, void *p_data )
881 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
882 VLC_UNUSED(oldval); VLC_UNUSED(newval);
883 intf_thread_t *p_intf = p_data;
888 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
892 /*****************************************************************************
893 * TrackChange: callback on playlist "playlist-current"
894 *****************************************************************************/
895 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
896 vlc_value_t oldval, vlc_value_t newval, void *p_data )
898 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
899 intf_sys_t *p_sys = p_intf->p_sys;
900 playlist_t *p_playlist;
901 input_thread_t *p_input = NULL;
902 input_item_t *p_item = NULL;
903 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
904 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
909 p_sys->b_meta_read = VLC_FALSE;
911 p_playlist = pl_Yield( p_intf );
913 p_input = p_playlist->p_input;
918 pl_Release( p_playlist );
922 vlc_object_yield( p_input );
924 pl_Release( p_playlist );
926 p_item = input_GetItem( p_input );
929 vlc_object_release( p_input );
933 if( input_item_IsPreparsed( p_item ) )
935 p_sys->b_meta_read = VLC_TRUE;
936 TrackChangeSignal( p_sys->p_conn, p_item );
939 dbus_int32_t i_caps = CAPS_NONE;
941 if( p_playlist->items.i_size > 0 )
942 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
944 if( p_playlist->p_input )
946 access_t *p_access = (access_t*)vlc_object_find( p_playlist->p_input,
947 VLC_OBJECT_ACCESS, FIND_CHILD );
950 vlc_bool_t b_can_pause;
951 if( !access2_Control( p_access, ACCESS_CAN_PAUSE, &b_can_pause ) &&
953 i_caps |= CAPS_CAN_PAUSE;
954 vlc_object_release( p_access );
956 demux_t *p_demux = (demux_t*)vlc_object_find( p_playlist->p_input,
957 VLC_OBJECT_DEMUX, FIND_CHILD );
959 { /* XXX: is: demux can seek and access can not a possibility ? */
960 vlc_bool_t b_can_seek;
961 if( !stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek ) &&
963 i_caps |= CAPS_CAN_SEEK;
964 vlc_object_release( p_demux );
968 if( ((intf_thread_t*)p_this)->p_sys->b_meta_read )
969 i_caps |= CAPS_CAN_PROVIDE_METADATA;
972 var_AddCallback( p_input, "state", StateChange, p_intf );
974 vlc_object_release( p_input );
978 /*****************************************************************************
979 * GetInputMeta: Fill a DBusMessage with the given input item metadata
980 *****************************************************************************/
982 #define ADD_META( entry, type, data ) \
984 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
985 NULL, &dict_entry ); \
986 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
987 &ppsz_meta_items[entry] ); \
988 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
989 type##_AS_STRING, &variant ); \
990 dbus_message_iter_append_basic( &variant, \
993 dbus_message_iter_close_container( &dict_entry, &variant ); \
994 dbus_message_iter_close_container( &dict, &dict_entry ); }
996 #define ADD_VLC_META_STRING( entry, item ) \
998 char * psz = input_item_Get##item( p_input );\
999 ADD_META( entry, DBUS_TYPE_STRING, \
1004 static int GetInputMeta( input_item_t* p_input,
1005 DBusMessageIter *args )
1007 DBusMessageIter dict, dict_entry, variant;
1008 /* We need the track length to be expressed in milli-seconds
1009 * instead of µ-seconds */
1010 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1012 const char* ppsz_meta_items[] =
1014 "title", "artist", "genre", "copyright", "album", "tracknum",
1015 "description", "rating", "date", "setting", "url", "language",
1016 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1017 "status", "URI", "length", "video-codec", "audio-codec",
1018 "video-bitrate", "audio-bitrate", "audio-samplerate"
1021 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1023 ADD_VLC_META_STRING( 0, Title );
1024 ADD_VLC_META_STRING( 1, Artist );
1025 ADD_VLC_META_STRING( 2, Genre );
1026 ADD_VLC_META_STRING( 3, Copyright );
1027 ADD_VLC_META_STRING( 4, Album );
1028 ADD_VLC_META_STRING( 5, TrackNum );
1029 ADD_VLC_META_STRING( 6, Description );
1030 ADD_VLC_META_STRING( 7, Rating );
1031 ADD_VLC_META_STRING( 8, Date );
1032 ADD_VLC_META_STRING( 9, Setting );
1033 ADD_VLC_META_STRING( 10, URL );
1034 ADD_VLC_META_STRING( 11, Language );
1035 ADD_VLC_META_STRING( 12, NowPlaying );
1036 ADD_VLC_META_STRING( 13, Publisher );
1037 ADD_VLC_META_STRING( 14, EncodedBy );
1038 ADD_VLC_META_STRING( 15, ArtURL );
1039 ADD_VLC_META_STRING( 16, TrackID );
1041 vlc_mutex_lock( &p_input->lock );
1042 if( p_input->p_meta )
1043 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1044 vlc_mutex_unlock( &p_input->lock );
1046 ADD_VLC_META_STRING( 18, URI );
1047 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1049 dbus_message_iter_close_container( args, &dict );
1054 #undef ADD_VLC_META_STRING
1056 /*****************************************************************************
1057 * MarshalStatus: Fill a DBusMessage with the current player status
1058 *****************************************************************************/
1060 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1062 { /* This is NOT the right way to do that, it would be better to sore
1063 the status information in p_sys and update it on change, thus
1064 avoiding a long lock */
1066 DBusMessageIter status;
1067 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1069 playlist_t* p_playlist = NULL;
1070 input_thread_t* p_input = NULL;
1072 p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1078 p_input = p_playlist->p_input;
1081 var_Get( p_input, "state", &val );
1082 if( val.i_int >= END_S )
1084 else if( val.i_int == PAUSE_S )
1086 else if( val.i_int <= PLAYING_S )
1090 var_Get( p_playlist, "random", &val );
1091 i_random = val.i_int;
1093 var_Get( p_playlist, "repeat", &val );
1094 i_repeat = val.i_int;
1096 var_Get( p_playlist, "loop", &val );
1101 pl_Release( p_playlist );
1103 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1104 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1105 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1106 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1107 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1108 dbus_message_iter_close_container( args, &status );