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 "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26 #include <vlc_demux.h>
27 #include <vlc_input.h>
28 #include "input/input_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->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 */
62 input_DestroyThread( p_input_thread );
65 /* XXX: hack the playlist doesn't retain the input thread,
66 * so we did it for the playlist (see _new_from_input_thread),
67 * revert that here. */
68 vlc_object_release( p_input_thread );
73 * Retrieve the input thread. Be sure to release the object
74 * once you are done with it. (libvlc Internal)
76 * Object lock is held.
78 input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
79 libvlc_exception_t *p_e )
81 input_thread_t *p_input_thread;
83 vlc_mutex_lock( &p_mi->object_lock );
85 if( !p_mi || p_mi->i_input_id == -1 )
87 vlc_mutex_unlock( &p_mi->object_lock );
88 RAISENULL( "Input is NULL" );
91 p_input_thread = (input_thread_t*)vlc_object_get(
92 p_mi->p_libvlc_instance->p_libvlc_int,
96 vlc_mutex_unlock( &p_mi->object_lock );
97 RAISENULL( "Input does not exist" );
100 vlc_mutex_unlock( &p_mi->object_lock );
101 return p_input_thread;
105 * input_state_changed (Private) (input var "state" Callback)
108 input_state_changed( vlc_object_t * p_this, char const * psz_cmd,
109 vlc_value_t oldval, vlc_value_t newval,
112 libvlc_media_instance_t * p_mi = p_userdata;
113 libvlc_event_t event;
115 if( newval.i_int == oldval.i_int )
116 return VLC_SUCCESS; /* No change since last time, don't propagate */
118 switch ( newval.i_int )
121 event.type = libvlc_MediaInstanceReachedEnd;
127 libvlc_event_send( p_mi->p_event_manager, &event );
131 /**************************************************************************
132 * Create a Media Instance object
133 **************************************************************************/
134 libvlc_media_instance_t *
135 libvlc_media_instance_new( libvlc_instance_t * p_libvlc_instance,
136 libvlc_exception_t * p_e )
138 libvlc_media_instance_t * p_mi;
140 if( !p_libvlc_instance )
142 libvlc_exception_raise( p_e, "invalid libvlc instance" );
146 p_mi = malloc( sizeof(libvlc_media_instance_t) );
149 p_mi->p_libvlc_instance = p_libvlc_instance;
150 p_mi->i_input_id = -1;
151 /* refcount strategy:
152 * - All items created by _new start with a refcount set to 1
153 * - Accessor _release decrease the refcount by 1, if after that
154 * operation the refcount is 0, the object is destroyed.
155 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
156 p_mi->i_refcount = 1;
157 /* object_lock strategy:
158 * - No lock held in constructor
159 * - Lock when accessing all variable this lock is held
160 * - Lock when attempting to destroy the object the lock is also held */
161 vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
162 &p_mi->object_lock );
163 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
164 p_libvlc_instance, p_e );
165 if( libvlc_exception_raised( p_e ) )
171 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
172 libvlc_MediaInstanceReachedEnd, p_e );
177 /**************************************************************************
178 * Create a Media Instance object with a media descriptor
179 **************************************************************************/
180 libvlc_media_instance_t *
181 libvlc_media_instance_new_from_media_descriptor(
182 libvlc_media_descriptor_t * p_md,
183 libvlc_exception_t *p_e )
185 libvlc_media_instance_t * p_mi;
186 p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
191 p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
196 /**************************************************************************
197 * Create a new media instance object from an input_thread (Libvlc Internal)
198 **************************************************************************/
199 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
200 struct libvlc_instance_t *p_libvlc_instance,
201 input_thread_t *p_input,
202 libvlc_exception_t *p_e )
204 libvlc_media_instance_t * p_mi;
208 libvlc_exception_raise( p_e, "invalid input thread" );
212 p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
217 p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
219 p_input->p->input.p_item, p_e );
223 libvlc_media_instance_destroy( p_mi );
227 p_mi->i_input_id = p_input->i_object_id;
229 /* will be released in media_instance_release() */
230 vlc_object_yield( p_input );
232 /* XXX: Hack as the playlist doesn't yield the input thread we retain
233 * the input for the playlist. (see corresponding hack in _release) */
234 vlc_object_yield( p_input );
239 /**************************************************************************
240 * Destroy a Media Instance object (libvlc internal)
242 * Warning: No lock held here, but hey, this is internal.
243 **************************************************************************/
244 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
246 input_thread_t *p_input_thread;
247 libvlc_exception_t p_e;
249 libvlc_exception_init( &p_e );
254 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
256 if( libvlc_exception_raised( &p_e ) )
258 libvlc_event_manager_release( p_mi->p_event_manager );
260 return; /* no need to worry about no input thread */
263 input_DestroyThread( p_input_thread );
265 libvlc_media_descriptor_release( p_mi->p_md );
270 /**************************************************************************
271 * Release a Media Instance object
272 **************************************************************************/
273 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
278 vlc_mutex_lock( &p_mi->object_lock );
282 if( p_mi->i_refcount > 0 )
284 vlc_mutex_unlock( &p_mi->object_lock );
287 vlc_mutex_unlock( &p_mi->object_lock );
289 libvlc_event_manager_release( p_mi->p_event_manager );
291 release_input_thread( p_mi );
293 libvlc_media_descriptor_release( p_mi->p_md );
298 /**************************************************************************
299 * Retain a Media Instance object
300 **************************************************************************/
301 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
308 /**************************************************************************
309 * Set the Media descriptor associated with the instance
310 **************************************************************************/
311 void libvlc_media_instance_set_media_descriptor(
312 libvlc_media_instance_t *p_mi,
313 libvlc_media_descriptor_t *p_md,
314 libvlc_exception_t *p_e )
321 vlc_mutex_lock( &p_mi->object_lock );
323 release_input_thread( p_mi );
325 libvlc_media_descriptor_release( p_mi->p_md );
330 vlc_mutex_unlock( &p_mi->object_lock );
331 return; /* It is ok to pass a NULL md */
334 p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
336 /* The policy here is to ignore that we were created using a different
337 * libvlc_instance, because we don't really care */
338 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
340 vlc_mutex_unlock( &p_mi->object_lock );
343 /**************************************************************************
344 * Get the Media descriptor associated with the instance
345 **************************************************************************/
346 libvlc_media_descriptor_t *
347 libvlc_media_instance_get_media_descriptor(
348 libvlc_media_instance_t *p_mi,
349 libvlc_exception_t *p_e )
356 return libvlc_media_descriptor_duplicate( p_mi->p_md );
359 /**************************************************************************
360 * Get the event Manager
361 **************************************************************************/
362 libvlc_event_manager_t *
363 libvlc_media_instance_event_manager(
364 libvlc_media_instance_t *p_mi,
365 libvlc_exception_t *p_e )
369 return p_mi->p_event_manager;
372 /**************************************************************************
374 **************************************************************************/
375 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
376 libvlc_exception_t *p_e )
378 input_thread_t * p_input_thread;
380 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
382 /* A thread alread exists, send it a play message */
384 val.i_int = PLAYING_S;
386 input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, PLAYING_S );
387 vlc_object_release( p_input_thread );
391 /* Ignore previous exception */
392 libvlc_exception_clear( p_e );
394 vlc_mutex_lock( &p_mi->object_lock );
398 libvlc_exception_raise( p_e, "no associated media descriptor" );
399 vlc_mutex_unlock( &p_mi->object_lock );
403 p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
404 p_mi->p_md->p_input_item );
405 p_mi->i_input_id = p_input_thread->i_object_id;
410 val.i_int = p_mi->drawable;
411 var_Set( p_input_thread, "drawable", val );
413 var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
415 /* will be released in media_instance_release() */
416 vlc_object_yield( p_input_thread );
418 vlc_mutex_unlock( &p_mi->object_lock );
421 /**************************************************************************
423 **************************************************************************/
424 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
425 libvlc_exception_t *p_e )
427 input_thread_t * p_input_thread;
431 p_input_thread = libvlc_get_input_thread( p_mi, p_e );
433 if( !p_input_thread )
436 input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, val );
437 vlc_object_release( p_input_thread );
440 /**************************************************************************
442 **************************************************************************/
443 void libvlc_media_instance_stop( libvlc_media_instance_t *p_mi,
444 libvlc_exception_t *p_e )
446 libvlc_exception_raise( p_e, "Not implemented" );
449 /**************************************************************************
451 **************************************************************************/
452 void libvlc_media_instance_set_drawable( libvlc_media_instance_t *p_mi,
453 libvlc_drawable_t drawable,
454 libvlc_exception_t *p_e )
456 p_mi->drawable = drawable;
459 /**************************************************************************
460 * Getters for stream information
461 **************************************************************************/
462 vlc_int64_t libvlc_media_instance_get_length(
463 libvlc_media_instance_t *p_mi,
464 libvlc_exception_t *p_e )
466 input_thread_t *p_input_thread;
469 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
470 if( !p_input_thread )
473 var_Get( p_input_thread, "length", &val );
474 vlc_object_release( p_input_thread );
476 return (val.i_time+500LL)/1000LL;
479 vlc_int64_t libvlc_media_instance_get_time(
480 libvlc_media_instance_t *p_mi,
481 libvlc_exception_t *p_e )
483 input_thread_t *p_input_thread;
486 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
487 if( !p_input_thread )
490 var_Get( p_input_thread , "time", &val );
491 vlc_object_release( p_input_thread );
492 return (val.i_time+500LL)/1000LL;
495 void libvlc_media_instance_set_time(
496 libvlc_media_instance_t *p_mi,
498 libvlc_exception_t *p_e )
500 input_thread_t *p_input_thread;
503 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
504 if( !p_input_thread )
507 value.i_time = time*1000LL;
508 var_Set( p_input_thread, "time", value );
509 vlc_object_release( p_input_thread );
512 void libvlc_media_instance_set_position(
513 libvlc_media_instance_t *p_mi,
515 libvlc_exception_t *p_e )
517 input_thread_t *p_input_thread;
519 val.f_float = position;
521 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
522 if( !p_input_thread )
525 var_Set( p_input_thread, "position", val );
526 vlc_object_release( p_input_thread );
529 float libvlc_media_instance_get_position(
530 libvlc_media_instance_t *p_mi,
531 libvlc_exception_t *p_e )
533 input_thread_t *p_input_thread;
536 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
537 if( !p_input_thread )
540 var_Get( p_input_thread, "position", &val );
541 vlc_object_release( p_input_thread );
546 float libvlc_media_instance_get_fps(
547 libvlc_media_instance_t *p_mi,
548 libvlc_exception_t *p_e)
551 input_thread_t *p_input_thread;
553 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
554 if( !p_input_thread )
557 if( (NULL == p_input_thread->p->input.p_demux)
558 || demux2_Control( p_input_thread->p->input.p_demux, DEMUX_GET_FPS, &f_fps )
561 vlc_object_release( p_input_thread );
566 vlc_object_release( p_input_thread );
571 vlc_bool_t libvlc_media_instance_will_play(
572 libvlc_media_instance_t *p_mi,
573 libvlc_exception_t *p_e)
575 input_thread_t *p_input_thread =
576 libvlc_get_input_thread ( p_mi, p_e);
577 if ( !p_input_thread )
580 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
582 vlc_object_release( p_input_thread );
585 vlc_object_release( p_input_thread );
589 void libvlc_media_instance_set_rate(
590 libvlc_media_instance_t *p_mi,
592 libvlc_exception_t *p_e )
594 input_thread_t *p_input_thread;
598 RAISEVOID( "Rate value is invalid" );
600 val.i_int = 1000.0f/rate;
602 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
603 if ( !p_input_thread )
606 var_Set( p_input_thread, "rate", val );
607 vlc_object_release( p_input_thread );
610 float libvlc_media_instance_get_rate(
611 libvlc_media_instance_t *p_mi,
612 libvlc_exception_t *p_e )
614 input_thread_t *p_input_thread;
617 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
618 if ( !p_input_thread )
621 var_Get( p_input_thread, "rate", &val );
622 vlc_object_release( p_input_thread );
624 return (float)1000.0f/val.i_int;
627 int libvlc_media_instance_get_state(
628 libvlc_media_instance_t *p_mi,
629 libvlc_exception_t *p_e )
631 input_thread_t *p_input_thread;
634 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
635 if ( !p_input_thread )
638 var_Get( p_input_thread, "state", &val );
639 vlc_object_release( p_input_thread );