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 "input/input_internal.h"
28 #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;
38 vlc_bool_t should_destroy;
40 if( !p_mi || p_mi->i_input_id == -1 )
43 p_input_thread = (input_thread_t*)vlc_object_get(
44 p_mi->p_libvlc_instance->p_libvlc_int,
47 p_mi->i_input_id = -1;
52 /* release for previous vlc_object_get */
53 vlc_object_release( p_input_thread );
55 should_destroy = p_input_thread->p_internals->i_refcount == 1;
57 /* release for initial p_input_thread yield (see _new()) */
58 vlc_object_release( p_input_thread );
60 /* No one is tracking this input_thread appart us. Destroy it */
63 /* We owned this one */
64 input_StopThread( p_input_thread );
65 var_Destroy( p_input_thread, "drawable" );
66 input_DestroyThread( p_input_thread );
70 /* XXX: hack the playlist doesn't retain the input thread,
71 * so we did it for the playlist (see _new_from_input_thread),
72 * revert that here. */
73 vlc_object_release( p_input_thread );
78 * Retrieve the input thread. Be sure to release the object
79 * once you are done with it. (libvlc Internal)
81 * Object lock is held.
83 input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
84 libvlc_exception_t *p_e )
86 input_thread_t *p_input_thread;
88 vlc_mutex_lock( &p_mi->object_lock );
90 if( !p_mi || p_mi->i_input_id == -1 )
92 vlc_mutex_unlock( &p_mi->object_lock );
93 RAISENULL( "Input is NULL" );
96 p_input_thread = (input_thread_t*)vlc_object_get(
97 p_mi->p_libvlc_instance->p_libvlc_int,
101 vlc_mutex_unlock( &p_mi->object_lock );
102 RAISENULL( "Input does not exist" );
105 vlc_mutex_unlock( &p_mi->object_lock );
106 return p_input_thread;
110 * input_state_changed (Private) (input var "state" Callback)
113 input_state_changed( vlc_object_t * p_this, char const * psz_cmd,
114 vlc_value_t oldval, vlc_value_t newval,
117 libvlc_media_instance_t * p_mi = p_userdata;
118 libvlc_event_t event;
120 if( newval.i_int == oldval.i_int )
121 return VLC_SUCCESS; /* No change since last time, don't propagate */
123 switch ( newval.i_int )
126 event.type = libvlc_MediaInstanceReachedEnd;
129 event.type = libvlc_MediaInstancePaused;
132 event.type = libvlc_MediaInstancePlayed;
138 libvlc_event_send( p_mi->p_event_manager, &event );
143 * input_position_changed (Private) (input var "intf-change" Callback)
146 input_position_changed( vlc_object_t * p_this, char const * psz_cmd,
147 vlc_value_t oldval, vlc_value_t newval,
150 libvlc_media_instance_t * p_mi = p_userdata;
153 if (!strcmp(psz_cmd, "intf" /* "-change" no need to go further */))
155 input_thread_t * p_input = (input_thread_t *)p_this;
156 var_Get( p_input, "position", &val );
158 if ((val.i_time % I64C(500000)) != 0)
159 return VLC_SUCCESS; /* No need to have a better precision */
160 var_Get( p_input, "state", &val );
162 if( val.i_int != PLAYING_S )
163 return VLC_SUCCESS; /* Don't send the position while stopped */
166 val.i_time = newval.i_time;
168 libvlc_event_t event;
169 event.type = libvlc_MediaInstancePositionChanged;
170 event.u.media_instance_position_changed.new_position = val.i_time;
172 libvlc_event_send( p_mi->p_event_manager, &event );
176 /**************************************************************************
177 * Create a Media Instance object
178 **************************************************************************/
179 libvlc_media_instance_t *
180 libvlc_media_instance_new( libvlc_instance_t * p_libvlc_instance,
181 libvlc_exception_t * p_e )
183 libvlc_media_instance_t * p_mi;
185 if( !p_libvlc_instance )
187 libvlc_exception_raise( p_e, "invalid libvlc instance" );
191 p_mi = malloc( sizeof(libvlc_media_instance_t) );
194 p_mi->p_libvlc_instance = p_libvlc_instance;
195 p_mi->i_input_id = -1;
196 /* refcount strategy:
197 * - All items created by _new start with a refcount set to 1
198 * - Accessor _release decrease the refcount by 1, if after that
199 * operation the refcount is 0, the object is destroyed.
200 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
201 p_mi->i_refcount = 1;
202 /* object_lock strategy:
203 * - No lock held in constructor
204 * - Lock when accessing all variable this lock is held
205 * - Lock when attempting to destroy the object the lock is also held */
206 vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
207 &p_mi->object_lock );
208 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
209 p_libvlc_instance, p_e );
210 if( libvlc_exception_raised( p_e ) )
216 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
217 libvlc_MediaInstanceReachedEnd, p_e );
222 /**************************************************************************
223 * Create a Media Instance object with a media descriptor
224 **************************************************************************/
225 libvlc_media_instance_t *
226 libvlc_media_instance_new_from_media_descriptor(
227 libvlc_media_descriptor_t * p_md,
228 libvlc_exception_t *p_e )
230 libvlc_media_instance_t * p_mi;
231 p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
236 libvlc_media_descriptor_retain( p_md );
242 /**************************************************************************
243 * Create a new media instance object from an input_thread (Libvlc Internal)
244 **************************************************************************/
245 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
246 struct libvlc_instance_t *p_libvlc_instance,
247 input_thread_t *p_input,
248 libvlc_exception_t *p_e )
250 libvlc_media_instance_t * p_mi;
254 libvlc_exception_raise( p_e, "invalid input thread" );
258 p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
263 p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
265 input_GetItem( p_input ), p_e );
269 libvlc_media_instance_destroy( p_mi );
273 p_mi->i_input_id = p_input->i_object_id;
275 /* will be released in media_instance_release() */
276 vlc_object_yield( p_input );
278 /* XXX: Hack as the playlist doesn't yield the input thread we retain
279 * the input for the playlist. (see corresponding hack in _release) */
280 vlc_object_yield( p_input );
285 /**************************************************************************
286 * Destroy a Media Instance object (libvlc internal)
288 * Warning: No lock held here, but hey, this is internal.
289 **************************************************************************/
290 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
292 input_thread_t *p_input_thread;
293 libvlc_exception_t p_e;
295 libvlc_exception_init( &p_e );
300 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
302 if( libvlc_exception_raised( &p_e ) )
304 libvlc_event_manager_release( p_mi->p_event_manager );
306 return; /* no need to worry about no input thread */
308 vlc_mutex_destroy( &p_mi->object_lock );
310 input_DestroyThread( p_input_thread );
312 libvlc_media_descriptor_release( p_mi->p_md );
317 /**************************************************************************
318 * Release a Media Instance object
319 **************************************************************************/
320 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
325 vlc_mutex_lock( &p_mi->object_lock );
329 if( p_mi->i_refcount > 0 )
331 vlc_mutex_unlock( &p_mi->object_lock );
334 vlc_mutex_unlock( &p_mi->object_lock );
335 vlc_mutex_destroy( &p_mi->object_lock );
337 libvlc_event_manager_release( p_mi->p_event_manager );
339 release_input_thread( p_mi );
341 libvlc_media_descriptor_release( p_mi->p_md );
346 /**************************************************************************
347 * Retain a Media Instance object
348 **************************************************************************/
349 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
356 /**************************************************************************
357 * Set the Media descriptor associated with the instance
358 **************************************************************************/
359 void libvlc_media_instance_set_media_descriptor(
360 libvlc_media_instance_t *p_mi,
361 libvlc_media_descriptor_t *p_md,
362 libvlc_exception_t *p_e )
369 vlc_mutex_lock( &p_mi->object_lock );
371 release_input_thread( p_mi );
373 libvlc_media_descriptor_release( p_mi->p_md );
378 vlc_mutex_unlock( &p_mi->object_lock );
379 return; /* It is ok to pass a NULL md */
382 libvlc_media_descriptor_retain( p_md );
385 /* The policy here is to ignore that we were created using a different
386 * libvlc_instance, because we don't really care */
387 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
389 vlc_mutex_unlock( &p_mi->object_lock );
392 /**************************************************************************
393 * Get the Media descriptor associated with the instance
394 **************************************************************************/
395 libvlc_media_descriptor_t *
396 libvlc_media_instance_get_media_descriptor(
397 libvlc_media_instance_t *p_mi,
398 libvlc_exception_t *p_e )
405 libvlc_media_descriptor_retain( p_mi->p_md );
409 /**************************************************************************
410 * Get the event Manager
411 **************************************************************************/
412 libvlc_event_manager_t *
413 libvlc_media_instance_event_manager(
414 libvlc_media_instance_t *p_mi,
415 libvlc_exception_t *p_e )
419 return p_mi->p_event_manager;
422 /**************************************************************************
424 **************************************************************************/
425 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
426 libvlc_exception_t *p_e )
428 input_thread_t * p_input_thread;
430 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
432 /* A thread alread exists, send it a play message */
433 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
434 vlc_object_release( p_input_thread );
438 /* Ignore previous exception */
439 libvlc_exception_clear( p_e );
441 vlc_mutex_lock( &p_mi->object_lock );
445 libvlc_exception_raise( p_e, "no associated media descriptor" );
446 vlc_mutex_unlock( &p_mi->object_lock );
450 p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
451 p_mi->p_md->p_input_item );
452 p_mi->i_input_id = p_input_thread->i_object_id;
457 val.i_int = p_mi->drawable;
458 var_Create( p_input_thread, "drawable", VLC_VAR_DOINHERIT );
459 var_Set( p_input_thread, "drawable", val );
461 var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
462 var_AddCallback( p_input_thread, "intf-change", input_position_changed, p_mi );
464 /* will be released in media_instance_release() */
465 vlc_object_yield( p_input_thread );
467 vlc_mutex_unlock( &p_mi->object_lock );
470 /**************************************************************************
472 **************************************************************************/
473 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
474 libvlc_exception_t *p_e )
476 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
478 if( !p_input_thread )
481 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
482 vlc_object_release( p_input_thread );
485 /**************************************************************************
487 **************************************************************************/
488 void libvlc_media_instance_stop( libvlc_media_instance_t *p_mi,
489 libvlc_exception_t *p_e )
491 //libvlc_exception_raise( p_e, "Not implemented" );
494 /**************************************************************************
496 **************************************************************************/
497 void libvlc_media_instance_set_drawable( libvlc_media_instance_t *p_mi,
498 libvlc_drawable_t drawable,
499 libvlc_exception_t *p_e )
501 p_mi->drawable = drawable;
504 /**************************************************************************
505 * Getters for stream information
506 **************************************************************************/
507 vlc_int64_t libvlc_media_instance_get_length(
508 libvlc_media_instance_t *p_mi,
509 libvlc_exception_t *p_e )
511 input_thread_t *p_input_thread;
514 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
515 if( !p_input_thread )
518 var_Get( p_input_thread, "length", &val );
519 vlc_object_release( p_input_thread );
521 return (val.i_time+500LL)/1000LL;
524 vlc_int64_t libvlc_media_instance_get_time(
525 libvlc_media_instance_t *p_mi,
526 libvlc_exception_t *p_e )
528 input_thread_t *p_input_thread;
531 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
532 if( !p_input_thread )
535 var_Get( p_input_thread , "time", &val );
536 vlc_object_release( p_input_thread );
537 return (val.i_time+500LL)/1000LL;
540 void libvlc_media_instance_set_time(
541 libvlc_media_instance_t *p_mi,
543 libvlc_exception_t *p_e )
545 input_thread_t *p_input_thread;
548 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
549 if( !p_input_thread )
552 value.i_time = time*1000LL;
553 var_Set( p_input_thread, "time", value );
554 vlc_object_release( p_input_thread );
557 void libvlc_media_instance_set_position(
558 libvlc_media_instance_t *p_mi,
560 libvlc_exception_t *p_e )
562 input_thread_t *p_input_thread;
564 val.f_float = position;
566 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
567 if( !p_input_thread )
570 var_Set( p_input_thread, "position", val );
571 vlc_object_release( p_input_thread );
574 float libvlc_media_instance_get_position(
575 libvlc_media_instance_t *p_mi,
576 libvlc_exception_t *p_e )
578 input_thread_t *p_input_thread;
581 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
582 if( !p_input_thread )
585 var_Get( p_input_thread, "position", &val );
586 vlc_object_release( p_input_thread );
591 float libvlc_media_instance_get_fps(
592 libvlc_media_instance_t *p_mi,
593 libvlc_exception_t *p_e)
596 input_thread_t *p_input_thread;
598 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
599 if( !p_input_thread )
602 if( (NULL == p_input_thread->p->input.p_demux)
603 || demux2_Control( p_input_thread->p->input.p_demux, DEMUX_GET_FPS, &f_fps )
606 vlc_object_release( p_input_thread );
611 vlc_object_release( p_input_thread );
616 vlc_bool_t libvlc_media_instance_will_play(
617 libvlc_media_instance_t *p_mi,
618 libvlc_exception_t *p_e)
620 input_thread_t *p_input_thread =
621 libvlc_get_input_thread ( p_mi, p_e);
622 if ( !p_input_thread )
625 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
627 vlc_object_release( p_input_thread );
630 vlc_object_release( p_input_thread );
634 void libvlc_media_instance_set_rate(
635 libvlc_media_instance_t *p_mi,
637 libvlc_exception_t *p_e )
639 input_thread_t *p_input_thread;
643 RAISEVOID( "Rate value is invalid" );
645 val.i_int = 1000.0f/rate;
647 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
648 if ( !p_input_thread )
651 var_Set( p_input_thread, "rate", val );
652 vlc_object_release( p_input_thread );
655 float libvlc_media_instance_get_rate(
656 libvlc_media_instance_t *p_mi,
657 libvlc_exception_t *p_e )
659 input_thread_t *p_input_thread;
662 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
663 if ( !p_input_thread )
666 var_Get( p_input_thread, "rate", &val );
667 vlc_object_release( p_input_thread );
669 return (float)1000.0f/val.i_int;
672 static libvlc_state_t vlc_to_libvlc_state[] =
674 [INIT_S] = libvlc_Opening,
675 [OPENING_S] = libvlc_Opening,
676 [BUFFERING_S] = libvlc_Buffering,
677 [PLAYING_S] = libvlc_Playing,
678 [PAUSE_S] = libvlc_Paused,
679 [END_S] = libvlc_Ended,
680 [ERROR_S] = libvlc_Error,
683 libvlc_state_t libvlc_media_instance_get_state(
684 libvlc_media_instance_t *p_mi,
685 libvlc_exception_t *p_e )
687 input_thread_t *p_input_thread;
690 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
691 if ( !p_input_thread )
692 return libvlc_Stopped;
694 var_Get( p_input_thread, "state", &val );
695 vlc_object_release( p_input_thread );
697 if( val.i_int < 0 || val.i_int > 6 )
698 return libvlc_Stopped;
700 return vlc_to_libvlc_state[val.i_int];