1 /*****************************************************************************
2 * media_instance.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include <vlc/libvlc.h>
25 #include <vlc_demux.h>
26 #include <vlc_input.h>
27 #include "libvlc_internal.h"
30 static const libvlc_state_t vlc_to_libvlc_state_array[] =
32 [INIT_S] = libvlc_Opening,
33 [OPENING_S] = libvlc_Opening,
34 [BUFFERING_S] = libvlc_Buffering,
35 [PLAYING_S] = libvlc_Playing,
36 [PAUSE_S] = libvlc_Paused,
37 [END_S] = libvlc_Ended,
38 [ERROR_S] = libvlc_Error,
40 static inline libvlc_state_t vlc_to_libvlc_state( int vlc_state )
42 if( vlc_state < 0 || vlc_state > 6 )
43 return libvlc_Stopped;
45 return vlc_to_libvlc_state_array[vlc_state];
49 * Release the associated input thread
51 * Object lock is NOT held.
53 static void release_input_thread( libvlc_media_instance_t *p_mi )
55 input_thread_t *p_input_thread;
57 if( !p_mi || p_mi->i_input_id == -1 )
60 p_input_thread = (input_thread_t*)vlc_object_get(
61 p_mi->p_libvlc_instance->p_libvlc_int,
64 p_mi->i_input_id = -1;
69 /* release for previous vlc_object_get */
70 vlc_object_release( p_input_thread );
72 /* release for initial p_input_thread yield (see _new()) */
73 vlc_object_release( p_input_thread );
75 /* No one is tracking this input_thread appart us. Destroy it */
76 if( p_mi->b_own_its_input_thread )
78 /* We owned this one */
79 input_StopThread( p_input_thread );
80 var_Destroy( p_input_thread, "drawable" );
81 input_DestroyThread( p_input_thread );
85 /* XXX: hack the playlist doesn't retain the input thread,
86 * so we did it for the playlist (see _new_from_input_thread),
87 * revert that here. This will be deleted with the playlist API */
88 vlc_object_release( p_input_thread );
93 * Retrieve the input thread. Be sure to release the object
94 * once you are done with it. (libvlc Internal)
96 * Object lock is held.
98 input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
99 libvlc_exception_t *p_e )
101 input_thread_t *p_input_thread;
103 vlc_mutex_lock( &p_mi->object_lock );
105 if( !p_mi || p_mi->i_input_id == -1 )
107 vlc_mutex_unlock( &p_mi->object_lock );
108 RAISENULL( "Input is NULL" );
111 p_input_thread = (input_thread_t*)vlc_object_get(
112 p_mi->p_libvlc_instance->p_libvlc_int,
114 if( !p_input_thread )
116 vlc_mutex_unlock( &p_mi->object_lock );
117 RAISENULL( "Input does not exist" );
120 vlc_mutex_unlock( &p_mi->object_lock );
121 return p_input_thread;
125 * input_state_changed (Private) (input var "state" Callback)
128 input_state_changed( vlc_object_t * p_this, char const * psz_cmd,
129 vlc_value_t oldval, vlc_value_t newval,
132 libvlc_media_instance_t * p_mi = p_userdata;
133 libvlc_event_t event;
134 libvlc_event_type_t type = newval.i_int;
136 if( strcmp( psz_cmd, "state" ) )
137 type = var_GetBool( p_this, "state" );
142 libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
143 event.type = libvlc_MediaInstanceReachedEnd;
146 libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_Playing, NULL);
147 event.type = libvlc_MediaInstancePaused;
150 libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_Playing, NULL);
151 event.type = libvlc_MediaInstancePlayed;
154 libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_Error, NULL);
155 event.type = libvlc_MediaInstanceReachedEnd; /* Because ERROR_S is buggy */
161 libvlc_event_send( p_mi->p_event_manager, &event );
166 * input_position_changed (Private) (input var "intf-change" Callback)
169 input_position_changed( vlc_object_t * p_this, char const * psz_cmd,
170 vlc_value_t oldval, vlc_value_t newval,
173 libvlc_media_instance_t * p_mi = p_userdata;
176 if (!strncmp(psz_cmd, "intf", 4 /* "-change" no need to go further */))
178 input_thread_t * p_input = (input_thread_t *)p_this;
180 var_Get( p_input, "state", &val );
181 if( val.i_int != PLAYING_S )
182 return VLC_SUCCESS; /* Don't send the position while stopped */
184 var_Get( p_input, "position", &val );
187 val.i_time = newval.i_time;
189 libvlc_event_t event;
190 event.type = libvlc_MediaInstancePositionChanged;
191 event.u.media_instance_position_changed.new_position = val.f_float;
193 libvlc_event_send( p_mi->p_event_manager, &event );
198 * input_time_changed (Private) (input var "intf-change" Callback)
201 input_time_changed( vlc_object_t * p_this, char const * psz_cmd,
202 vlc_value_t oldval, vlc_value_t newval,
205 libvlc_media_instance_t * p_mi = p_userdata;
208 if (!strncmp(psz_cmd, "intf", 4 /* "-change" no need to go further */))
210 input_thread_t * p_input = (input_thread_t *)p_this;
212 var_Get( p_input, "state", &val );
213 if( val.i_int != PLAYING_S )
214 return VLC_SUCCESS; /* Don't send the position while stopped */
216 var_Get( p_input, "time", &val );
219 val.i_time = newval.i_time;
221 libvlc_event_t event;
222 event.type = libvlc_MediaInstanceTimeChanged;
223 event.u.media_instance_time_changed.new_time = val.i_time;
224 libvlc_event_send( p_mi->p_event_manager, &event );
228 /**************************************************************************
229 * Create a Media Instance object
230 **************************************************************************/
231 libvlc_media_instance_t *
232 libvlc_media_instance_new( libvlc_instance_t * p_libvlc_instance,
233 libvlc_exception_t * p_e )
235 libvlc_media_instance_t * p_mi;
237 if( !p_libvlc_instance )
239 libvlc_exception_raise( p_e, "invalid libvlc instance" );
243 p_mi = malloc( sizeof(libvlc_media_instance_t) );
246 p_mi->p_libvlc_instance = p_libvlc_instance;
247 p_mi->i_input_id = -1;
248 /* refcount strategy:
249 * - All items created by _new start with a refcount set to 1
250 * - Accessor _release decrease the refcount by 1, if after that
251 * operation the refcount is 0, the object is destroyed.
252 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
253 p_mi->i_refcount = 1;
254 p_mi->b_own_its_input_thread = VLC_TRUE;
255 /* object_lock strategy:
256 * - No lock held in constructor
257 * - Lock when accessing all variable this lock is held
258 * - Lock when attempting to destroy the object the lock is also held */
259 vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
260 &p_mi->object_lock );
261 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
262 p_libvlc_instance, p_e );
263 if( libvlc_exception_raised( p_e ) )
269 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
270 libvlc_MediaInstanceReachedEnd, p_e );
271 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
272 libvlc_MediaInstancePaused, p_e );
273 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
274 libvlc_MediaInstancePlayed, p_e );
275 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
276 libvlc_MediaInstancePositionChanged, p_e );
277 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
278 libvlc_MediaInstanceTimeChanged, p_e );
283 /**************************************************************************
284 * Create a Media Instance object with a media descriptor
285 **************************************************************************/
286 libvlc_media_instance_t *
287 libvlc_media_instance_new_from_media_descriptor(
288 libvlc_media_descriptor_t * p_md,
289 libvlc_exception_t *p_e )
291 libvlc_media_instance_t * p_mi;
292 p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
297 libvlc_media_descriptor_retain( p_md );
303 /**************************************************************************
304 * Create a new media instance object from an input_thread (Libvlc Internal)
305 **************************************************************************/
306 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
307 struct libvlc_instance_t *p_libvlc_instance,
308 input_thread_t *p_input,
309 libvlc_exception_t *p_e )
311 libvlc_media_instance_t * p_mi;
315 libvlc_exception_raise( p_e, "invalid input thread" );
319 p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
324 p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
326 input_GetItem( p_input ), p_e );
330 libvlc_media_instance_destroy( p_mi );
334 p_mi->i_input_id = p_input->i_object_id;
335 p_mi->b_own_its_input_thread = VLC_FALSE;
337 /* will be released in media_instance_release() */
338 vlc_object_yield( p_input );
340 /* XXX: Hack as the playlist doesn't yield the input thread we retain
341 * the input for the playlist. (see corresponding hack in _release) */
342 vlc_object_yield( p_input );
347 /**************************************************************************
348 * Destroy a Media Instance object (libvlc internal)
350 * Warning: No lock held here, but hey, this is internal.
351 **************************************************************************/
352 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
354 input_thread_t *p_input_thread;
355 libvlc_exception_t p_e;
357 libvlc_exception_init( &p_e );
362 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
364 if( libvlc_exception_raised( &p_e ) )
366 libvlc_event_manager_release( p_mi->p_event_manager );
367 libvlc_exception_clear( &p_e );
369 return; /* no need to worry about no input thread */
371 vlc_mutex_destroy( &p_mi->object_lock );
373 input_DestroyThread( p_input_thread );
375 libvlc_media_descriptor_release( p_mi->p_md );
380 /**************************************************************************
381 * Release a Media Instance object
382 **************************************************************************/
383 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
388 vlc_mutex_lock( &p_mi->object_lock );
392 if( p_mi->i_refcount > 0 )
394 vlc_mutex_unlock( &p_mi->object_lock );
397 vlc_mutex_unlock( &p_mi->object_lock );
398 vlc_mutex_destroy( &p_mi->object_lock );
400 release_input_thread( p_mi );
402 libvlc_event_manager_release( p_mi->p_event_manager );
404 libvlc_media_descriptor_release( p_mi->p_md );
409 /**************************************************************************
410 * Retain a Media Instance object
411 **************************************************************************/
412 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
420 /**************************************************************************
421 * Set the Media descriptor associated with the instance
422 **************************************************************************/
423 void libvlc_media_instance_set_media_descriptor(
424 libvlc_media_instance_t *p_mi,
425 libvlc_media_descriptor_t *p_md,
426 libvlc_exception_t *p_e )
433 vlc_mutex_lock( &p_mi->object_lock );
435 release_input_thread( p_mi );
437 libvlc_media_descriptor_release( p_mi->p_md );
442 vlc_mutex_unlock( &p_mi->object_lock );
443 return; /* It is ok to pass a NULL md */
446 libvlc_media_descriptor_retain( p_md );
449 /* The policy here is to ignore that we were created using a different
450 * libvlc_instance, because we don't really care */
451 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
453 vlc_mutex_unlock( &p_mi->object_lock );
456 /**************************************************************************
457 * Get the Media descriptor associated with the instance
458 **************************************************************************/
459 libvlc_media_descriptor_t *
460 libvlc_media_instance_get_media_descriptor(
461 libvlc_media_instance_t *p_mi,
462 libvlc_exception_t *p_e )
469 libvlc_media_descriptor_retain( p_mi->p_md );
473 /**************************************************************************
474 * Get the event Manager
475 **************************************************************************/
476 libvlc_event_manager_t *
477 libvlc_media_instance_event_manager(
478 libvlc_media_instance_t *p_mi,
479 libvlc_exception_t *p_e )
483 return p_mi->p_event_manager;
486 /**************************************************************************
488 **************************************************************************/
489 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
490 libvlc_exception_t *p_e )
492 input_thread_t * p_input_thread;
494 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
496 /* A thread alread exists, send it a play message */
497 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
498 vlc_object_release( p_input_thread );
502 /* Ignore previous exception */
503 libvlc_exception_clear( p_e );
505 vlc_mutex_lock( &p_mi->object_lock );
509 libvlc_exception_raise( p_e, "no associated media descriptor" );
510 vlc_mutex_unlock( &p_mi->object_lock );
514 p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
515 p_mi->p_md->p_input_item );
516 p_mi->i_input_id = p_input_thread->i_object_id;
521 val.i_int = p_mi->drawable;
522 var_Create( p_input_thread, "drawable", VLC_VAR_DOINHERIT );
523 var_Set( p_input_thread, "drawable", val );
525 var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
526 var_AddCallback( p_input_thread, "seekable", input_state_changed, p_mi );
527 var_AddCallback( p_input_thread, "pausable", input_state_changed, p_mi );
528 var_AddCallback( p_input_thread, "intf-change", input_position_changed, p_mi );
529 var_AddCallback( p_input_thread, "intf-change", input_time_changed, p_mi );
531 /* will be released in media_instance_release() */
532 vlc_object_yield( p_input_thread );
534 vlc_mutex_unlock( &p_mi->object_lock );
537 /**************************************************************************
539 **************************************************************************/
540 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
541 libvlc_exception_t *p_e )
543 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
545 if( !p_input_thread )
548 int state = var_GetInteger( p_input_thread, "state" );
550 if( state == PLAYING_S )
552 if( libvlc_media_instance_can_pause( p_mi, p_e ) )
553 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
555 libvlc_media_instance_stop( p_mi, p_e );
558 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
560 vlc_object_release( p_input_thread );
563 /**************************************************************************
565 **************************************************************************/
566 void libvlc_media_instance_stop( libvlc_media_instance_t *p_mi,
567 libvlc_exception_t *p_e )
569 if( p_mi->b_own_its_input_thread )
570 release_input_thread( p_mi ); /* This will stop the input thread */
573 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
575 if( !p_input_thread )
578 input_StopThread( p_input_thread );
579 vlc_object_release( p_input_thread );
583 /**************************************************************************
585 **************************************************************************/
586 void libvlc_media_instance_set_drawable( libvlc_media_instance_t *p_mi,
587 libvlc_drawable_t drawable,
588 libvlc_exception_t *p_e )
590 p_mi->drawable = drawable;
593 /**************************************************************************
594 * Getters for stream information
595 **************************************************************************/
596 libvlc_time_t libvlc_media_instance_get_length(
597 libvlc_media_instance_t *p_mi,
598 libvlc_exception_t *p_e )
600 input_thread_t *p_input_thread;
603 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
604 if( !p_input_thread )
607 var_Get( p_input_thread, "length", &val );
608 vlc_object_release( p_input_thread );
610 return (val.i_time+500LL)/1000LL;
613 libvlc_time_t libvlc_media_instance_get_time(
614 libvlc_media_instance_t *p_mi,
615 libvlc_exception_t *p_e )
617 input_thread_t *p_input_thread;
620 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
621 if( !p_input_thread )
624 var_Get( p_input_thread , "time", &val );
625 vlc_object_release( p_input_thread );
626 return (val.i_time+500LL)/1000LL;
629 void libvlc_media_instance_set_time(
630 libvlc_media_instance_t *p_mi,
632 libvlc_exception_t *p_e )
634 input_thread_t *p_input_thread;
637 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
638 if( !p_input_thread )
641 value.i_time = time*1000LL;
642 var_Set( p_input_thread, "time", value );
643 vlc_object_release( p_input_thread );
646 void libvlc_media_instance_set_position(
647 libvlc_media_instance_t *p_mi,
649 libvlc_exception_t *p_e )
651 input_thread_t *p_input_thread;
653 val.f_float = position;
655 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
656 if( !p_input_thread )
659 var_Set( p_input_thread, "position", val );
660 vlc_object_release( p_input_thread );
663 float libvlc_media_instance_get_position(
664 libvlc_media_instance_t *p_mi,
665 libvlc_exception_t *p_e )
667 input_thread_t *p_input_thread;
670 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
671 if( !p_input_thread )
674 var_Get( p_input_thread, "position", &val );
675 vlc_object_release( p_input_thread );
680 void libvlc_media_instance_set_chapter(
681 libvlc_media_instance_t *p_mi,
683 libvlc_exception_t *p_e )
685 input_thread_t *p_input_thread;
689 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
690 if( !p_input_thread )
693 var_Set( p_input_thread, "chapter", val );
694 vlc_object_release( p_input_thread );
697 int libvlc_media_instance_get_chapter(
698 libvlc_media_instance_t *p_mi,
699 libvlc_exception_t *p_e )
701 input_thread_t *p_input_thread;
704 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
705 if( !p_input_thread )
708 var_Get( p_input_thread, "chapter", &val );
709 vlc_object_release( p_input_thread );
714 int libvlc_media_instance_get_chapter_count(
715 libvlc_media_instance_t *p_mi,
716 libvlc_exception_t *p_e )
718 input_thread_t *p_input_thread;
721 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
722 if( !p_input_thread )
725 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
726 vlc_object_release( p_input_thread );
731 float libvlc_media_instance_get_fps(
732 libvlc_media_instance_t *p_mi,
733 libvlc_exception_t *p_e)
735 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
740 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
742 vlc_object_release( p_input_thread );
747 vlc_bool_t libvlc_media_instance_will_play(
748 libvlc_media_instance_t *p_mi,
749 libvlc_exception_t *p_e)
751 input_thread_t *p_input_thread =
752 libvlc_get_input_thread ( p_mi, p_e);
753 if ( !p_input_thread )
756 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
758 vlc_object_release( p_input_thread );
761 vlc_object_release( p_input_thread );
765 void libvlc_media_instance_set_rate(
766 libvlc_media_instance_t *p_mi,
768 libvlc_exception_t *p_e )
770 input_thread_t *p_input_thread;
774 RAISEVOID( "Rate value is invalid" );
776 val.i_int = 1000.0f/rate;
778 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
779 if ( !p_input_thread )
782 var_Set( p_input_thread, "rate", val );
783 vlc_object_release( p_input_thread );
786 float libvlc_media_instance_get_rate(
787 libvlc_media_instance_t *p_mi,
788 libvlc_exception_t *p_e )
790 input_thread_t *p_input_thread;
793 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
794 if ( !p_input_thread )
797 var_Get( p_input_thread, "rate", &val );
798 vlc_object_release( p_input_thread );
800 return (float)1000.0f/val.i_int;
803 libvlc_state_t libvlc_media_instance_get_state(
804 libvlc_media_instance_t *p_mi,
805 libvlc_exception_t *p_e )
807 input_thread_t *p_input_thread;
810 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
811 if ( !p_input_thread )
813 /* We do return the right value, no need to throw an exception */
814 if( libvlc_exception_raised( p_e ) )
815 libvlc_exception_clear( p_e );
816 return libvlc_Stopped;
819 var_Get( p_input_thread, "state", &val );
820 vlc_object_release( p_input_thread );
822 return vlc_to_libvlc_state(val.i_int);
825 vlc_bool_t libvlc_media_instance_is_seekable(
826 libvlc_media_instance_t *p_mi,
827 libvlc_exception_t *p_e )
829 input_thread_t *p_input_thread;
832 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
833 if ( !p_input_thread )
835 /* We do return the right value, no need to throw an exception */
836 if( libvlc_exception_raised( p_e ) )
837 libvlc_exception_clear( p_e );
840 var_Get( p_input_thread, "seekable", &val );
841 vlc_object_release( p_input_thread );
846 vlc_bool_t libvlc_media_instance_can_pause(
847 libvlc_media_instance_t *p_mi,
848 libvlc_exception_t *p_e )
850 input_thread_t *p_input_thread;
853 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
854 if ( !p_input_thread )
856 /* We do return the right value, no need to throw an exception */
857 if( libvlc_exception_raised( p_e ) )
858 libvlc_exception_clear( p_e );
861 var_Get( p_input_thread, "can-pause", &val );
862 vlc_object_release( p_input_thread );