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"
31 * Release the associated input thread
33 * Object lock is NOT held.
35 static void release_input_thread( libvlc_media_instance_t *p_mi )
37 input_thread_t *p_input_thread;
39 if( !p_mi || p_mi->i_input_id == -1 )
42 p_input_thread = (input_thread_t*)vlc_object_get(
43 p_mi->p_libvlc_instance->p_libvlc_int,
46 p_mi->i_input_id = -1;
51 /* release for previous vlc_object_get */
52 vlc_object_release( p_input_thread );
54 /* release for initial p_input_thread yield (see _new()) */
55 vlc_object_release( p_input_thread );
57 /* No one is tracking this input_thread appart us. Destroy it */
58 if( p_mi->b_own_its_input_thread )
60 /* We owned this one */
61 input_StopThread( p_input_thread );
62 var_Destroy( p_input_thread, "drawable" );
63 input_DestroyThread( p_input_thread );
67 /* XXX: hack the playlist doesn't retain the input thread,
68 * so we did it for the playlist (see _new_from_input_thread),
69 * revert that here. This will be deleted with the playlist API */
70 vlc_object_release( p_input_thread );
75 * Retrieve the input thread. Be sure to release the object
76 * once you are done with it. (libvlc Internal)
78 * Object lock is held.
80 input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
81 libvlc_exception_t *p_e )
83 input_thread_t *p_input_thread;
85 vlc_mutex_lock( &p_mi->object_lock );
87 if( !p_mi || p_mi->i_input_id == -1 )
89 vlc_mutex_unlock( &p_mi->object_lock );
90 RAISENULL( "Input is NULL" );
93 p_input_thread = (input_thread_t*)vlc_object_get(
94 p_mi->p_libvlc_instance->p_libvlc_int,
98 vlc_mutex_unlock( &p_mi->object_lock );
99 RAISENULL( "Input does not exist" );
102 vlc_mutex_unlock( &p_mi->object_lock );
103 return p_input_thread;
107 * input_state_changed (Private) (input var "state" Callback)
110 input_state_changed( vlc_object_t * p_this, char const * psz_cmd,
111 vlc_value_t oldval, vlc_value_t newval,
114 libvlc_media_instance_t * p_mi = p_userdata;
115 libvlc_event_t event;
117 if( newval.i_int == oldval.i_int )
118 return VLC_SUCCESS; /* No change since last time, don't propagate */
120 switch ( newval.i_int )
123 event.type = libvlc_MediaInstanceReachedEnd;
126 event.type = libvlc_MediaInstancePaused;
129 event.type = libvlc_MediaInstancePlayed;
135 libvlc_event_send( p_mi->p_event_manager, &event );
140 * input_position_changed (Private) (input var "intf-change" Callback)
143 input_position_changed( vlc_object_t * p_this, char const * psz_cmd,
144 vlc_value_t oldval, vlc_value_t newval,
147 libvlc_media_instance_t * p_mi = p_userdata;
150 if (!strcmp(psz_cmd, "intf" /* "-change" no need to go further */))
152 input_thread_t * p_input = (input_thread_t *)p_this;
153 var_Get( p_input, "position", &val );
155 if ((val.i_time % I64C(500000)) != 0)
156 return VLC_SUCCESS; /* No need to have a better precision */
157 var_Get( p_input, "state", &val );
159 if( val.i_int != PLAYING_S )
160 return VLC_SUCCESS; /* Don't send the position while stopped */
163 val.i_time = newval.i_time;
165 libvlc_event_t event;
166 event.type = libvlc_MediaInstancePositionChanged;
167 event.u.media_instance_position_changed.new_position = val.i_time;
169 libvlc_event_send( p_mi->p_event_manager, &event );
173 /**************************************************************************
174 * Create a Media Instance object
175 **************************************************************************/
176 libvlc_media_instance_t *
177 libvlc_media_instance_new( libvlc_instance_t * p_libvlc_instance,
178 libvlc_exception_t * p_e )
180 libvlc_media_instance_t * p_mi;
182 if( !p_libvlc_instance )
184 libvlc_exception_raise( p_e, "invalid libvlc instance" );
188 p_mi = malloc( sizeof(libvlc_media_instance_t) );
191 p_mi->p_libvlc_instance = p_libvlc_instance;
192 p_mi->i_input_id = -1;
193 /* refcount strategy:
194 * - All items created by _new start with a refcount set to 1
195 * - Accessor _release decrease the refcount by 1, if after that
196 * operation the refcount is 0, the object is destroyed.
197 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
198 p_mi->i_refcount = 1;
199 p_mi->b_own_its_input_thread = VLC_TRUE;
200 /* object_lock strategy:
201 * - No lock held in constructor
202 * - Lock when accessing all variable this lock is held
203 * - Lock when attempting to destroy the object the lock is also held */
204 vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
205 &p_mi->object_lock );
206 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
207 p_libvlc_instance, p_e );
208 if( libvlc_exception_raised( p_e ) )
214 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
215 libvlc_MediaInstanceReachedEnd, p_e );
216 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
217 libvlc_MediaInstancePaused, p_e );
218 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
219 libvlc_MediaInstancePlayed, p_e );
220 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
221 libvlc_MediaInstancePositionChanged, p_e );
226 /**************************************************************************
227 * Create a Media Instance object with a media descriptor
228 **************************************************************************/
229 libvlc_media_instance_t *
230 libvlc_media_instance_new_from_media_descriptor(
231 libvlc_media_descriptor_t * p_md,
232 libvlc_exception_t *p_e )
234 libvlc_media_instance_t * p_mi;
235 p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
240 libvlc_media_descriptor_retain( p_md );
246 /**************************************************************************
247 * Create a new media instance object from an input_thread (Libvlc Internal)
248 **************************************************************************/
249 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
250 struct libvlc_instance_t *p_libvlc_instance,
251 input_thread_t *p_input,
252 libvlc_exception_t *p_e )
254 libvlc_media_instance_t * p_mi;
258 libvlc_exception_raise( p_e, "invalid input thread" );
262 p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
267 p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
269 input_GetItem( p_input ), p_e );
273 libvlc_media_instance_destroy( p_mi );
277 p_mi->i_input_id = p_input->i_object_id;
278 p_mi->b_own_its_input_thread = VLC_FALSE;
280 /* will be released in media_instance_release() */
281 vlc_object_yield( p_input );
283 /* XXX: Hack as the playlist doesn't yield the input thread we retain
284 * the input for the playlist. (see corresponding hack in _release) */
285 vlc_object_yield( p_input );
290 /**************************************************************************
291 * Destroy a Media Instance object (libvlc internal)
293 * Warning: No lock held here, but hey, this is internal.
294 **************************************************************************/
295 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
297 input_thread_t *p_input_thread;
298 libvlc_exception_t p_e;
300 libvlc_exception_init( &p_e );
305 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
307 if( libvlc_exception_raised( &p_e ) )
309 libvlc_event_manager_release( p_mi->p_event_manager );
311 return; /* no need to worry about no input thread */
313 vlc_mutex_destroy( &p_mi->object_lock );
315 input_DestroyThread( p_input_thread );
317 libvlc_media_descriptor_release( p_mi->p_md );
322 /**************************************************************************
323 * Release a Media Instance object
324 **************************************************************************/
325 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
330 vlc_mutex_lock( &p_mi->object_lock );
334 if( p_mi->i_refcount > 0 )
336 vlc_mutex_unlock( &p_mi->object_lock );
339 vlc_mutex_unlock( &p_mi->object_lock );
340 vlc_mutex_destroy( &p_mi->object_lock );
342 release_input_thread( p_mi );
344 libvlc_event_manager_release( p_mi->p_event_manager );
346 libvlc_media_descriptor_release( p_mi->p_md );
351 /**************************************************************************
352 * Retain a Media Instance object
353 **************************************************************************/
354 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
362 /**************************************************************************
363 * Set the Media descriptor associated with the instance
364 **************************************************************************/
365 void libvlc_media_instance_set_media_descriptor(
366 libvlc_media_instance_t *p_mi,
367 libvlc_media_descriptor_t *p_md,
368 libvlc_exception_t *p_e )
375 vlc_mutex_lock( &p_mi->object_lock );
377 release_input_thread( p_mi );
379 libvlc_media_descriptor_release( p_mi->p_md );
384 vlc_mutex_unlock( &p_mi->object_lock );
385 return; /* It is ok to pass a NULL md */
388 libvlc_media_descriptor_retain( p_md );
391 /* The policy here is to ignore that we were created using a different
392 * libvlc_instance, because we don't really care */
393 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
395 vlc_mutex_unlock( &p_mi->object_lock );
398 /**************************************************************************
399 * Get the Media descriptor associated with the instance
400 **************************************************************************/
401 libvlc_media_descriptor_t *
402 libvlc_media_instance_get_media_descriptor(
403 libvlc_media_instance_t *p_mi,
404 libvlc_exception_t *p_e )
411 libvlc_media_descriptor_retain( p_mi->p_md );
415 /**************************************************************************
416 * Get the event Manager
417 **************************************************************************/
418 libvlc_event_manager_t *
419 libvlc_media_instance_event_manager(
420 libvlc_media_instance_t *p_mi,
421 libvlc_exception_t *p_e )
425 return p_mi->p_event_manager;
428 /**************************************************************************
430 **************************************************************************/
431 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
432 libvlc_exception_t *p_e )
434 input_thread_t * p_input_thread;
436 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
438 /* A thread alread exists, send it a play message */
439 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
440 vlc_object_release( p_input_thread );
444 /* Ignore previous exception */
445 libvlc_exception_clear( p_e );
447 vlc_mutex_lock( &p_mi->object_lock );
451 libvlc_exception_raise( p_e, "no associated media descriptor" );
452 vlc_mutex_unlock( &p_mi->object_lock );
456 p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
457 p_mi->p_md->p_input_item );
458 p_mi->i_input_id = p_input_thread->i_object_id;
463 val.i_int = p_mi->drawable;
464 var_Create( p_input_thread, "drawable", VLC_VAR_DOINHERIT );
465 var_Set( p_input_thread, "drawable", val );
467 var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
468 var_AddCallback( p_input_thread, "intf-change", input_position_changed, p_mi );
470 /* will be released in media_instance_release() */
471 vlc_object_yield( p_input_thread );
473 vlc_mutex_unlock( &p_mi->object_lock );
476 /**************************************************************************
478 **************************************************************************/
479 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
480 libvlc_exception_t *p_e )
482 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
484 if( !p_input_thread )
487 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
488 vlc_object_release( p_input_thread );
491 /**************************************************************************
493 **************************************************************************/
494 void libvlc_media_instance_stop( libvlc_media_instance_t *p_mi,
495 libvlc_exception_t *p_e )
497 //libvlc_exception_raise( p_e, "Not implemented" );
500 /**************************************************************************
502 **************************************************************************/
503 void libvlc_media_instance_set_drawable( libvlc_media_instance_t *p_mi,
504 libvlc_drawable_t drawable,
505 libvlc_exception_t *p_e )
507 p_mi->drawable = drawable;
510 /**************************************************************************
511 * Getters for stream information
512 **************************************************************************/
513 vlc_int64_t libvlc_media_instance_get_length(
514 libvlc_media_instance_t *p_mi,
515 libvlc_exception_t *p_e )
517 input_thread_t *p_input_thread;
520 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
521 if( !p_input_thread )
524 var_Get( p_input_thread, "length", &val );
525 vlc_object_release( p_input_thread );
527 return (val.i_time+500LL)/1000LL;
530 vlc_int64_t libvlc_media_instance_get_time(
531 libvlc_media_instance_t *p_mi,
532 libvlc_exception_t *p_e )
534 input_thread_t *p_input_thread;
537 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
538 if( !p_input_thread )
541 var_Get( p_input_thread , "time", &val );
542 vlc_object_release( p_input_thread );
543 return (val.i_time+500LL)/1000LL;
546 void libvlc_media_instance_set_time(
547 libvlc_media_instance_t *p_mi,
549 libvlc_exception_t *p_e )
551 input_thread_t *p_input_thread;
554 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
555 if( !p_input_thread )
558 value.i_time = time*1000LL;
559 var_Set( p_input_thread, "time", value );
560 vlc_object_release( p_input_thread );
563 void libvlc_media_instance_set_position(
564 libvlc_media_instance_t *p_mi,
566 libvlc_exception_t *p_e )
568 input_thread_t *p_input_thread;
570 val.f_float = position;
572 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
573 if( !p_input_thread )
576 var_Set( p_input_thread, "position", val );
577 vlc_object_release( p_input_thread );
580 float libvlc_media_instance_get_position(
581 libvlc_media_instance_t *p_mi,
582 libvlc_exception_t *p_e )
584 input_thread_t *p_input_thread;
587 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
588 if( !p_input_thread )
591 var_Get( p_input_thread, "position", &val );
592 vlc_object_release( p_input_thread );
597 float libvlc_media_instance_get_fps(
598 libvlc_media_instance_t *p_mi,
599 libvlc_exception_t *p_e)
601 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
606 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
608 vlc_object_release( p_input_thread );
613 vlc_bool_t libvlc_media_instance_will_play(
614 libvlc_media_instance_t *p_mi,
615 libvlc_exception_t *p_e)
617 input_thread_t *p_input_thread =
618 libvlc_get_input_thread ( p_mi, p_e);
619 if ( !p_input_thread )
622 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
624 vlc_object_release( p_input_thread );
627 vlc_object_release( p_input_thread );
631 void libvlc_media_instance_set_rate(
632 libvlc_media_instance_t *p_mi,
634 libvlc_exception_t *p_e )
636 input_thread_t *p_input_thread;
640 RAISEVOID( "Rate value is invalid" );
642 val.i_int = 1000.0f/rate;
644 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
645 if ( !p_input_thread )
648 var_Set( p_input_thread, "rate", val );
649 vlc_object_release( p_input_thread );
652 float libvlc_media_instance_get_rate(
653 libvlc_media_instance_t *p_mi,
654 libvlc_exception_t *p_e )
656 input_thread_t *p_input_thread;
659 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
660 if ( !p_input_thread )
663 var_Get( p_input_thread, "rate", &val );
664 vlc_object_release( p_input_thread );
666 return (float)1000.0f/val.i_int;
669 static libvlc_state_t vlc_to_libvlc_state[] =
671 [INIT_S] = libvlc_Opening,
672 [OPENING_S] = libvlc_Opening,
673 [BUFFERING_S] = libvlc_Buffering,
674 [PLAYING_S] = libvlc_Playing,
675 [PAUSE_S] = libvlc_Paused,
676 [END_S] = libvlc_Ended,
677 [ERROR_S] = libvlc_Error,
680 libvlc_state_t libvlc_media_instance_get_state(
681 libvlc_media_instance_t *p_mi,
682 libvlc_exception_t *p_e )
684 input_thread_t *p_input_thread;
687 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
688 if ( !p_input_thread )
689 return libvlc_Stopped;
691 var_Get( p_input_thread, "state", &val );
692 vlc_object_release( p_input_thread );
694 if( val.i_int < 0 || val.i_int > 6 )
695 return libvlc_Stopped;
697 return vlc_to_libvlc_state[val.i_int];