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 */
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;
132 libvlc_event_send( p_mi->p_event_manager, &event );
136 /**************************************************************************
137 * Create a Media Instance object
138 **************************************************************************/
139 libvlc_media_instance_t *
140 libvlc_media_instance_new( libvlc_instance_t * p_libvlc_instance,
141 libvlc_exception_t * p_e )
143 libvlc_media_instance_t * p_mi;
145 if( !p_libvlc_instance )
147 libvlc_exception_raise( p_e, "invalid libvlc instance" );
151 p_mi = malloc( sizeof(libvlc_media_instance_t) );
154 p_mi->p_libvlc_instance = p_libvlc_instance;
155 p_mi->i_input_id = -1;
156 /* refcount strategy:
157 * - All items created by _new start with a refcount set to 1
158 * - Accessor _release decrease the refcount by 1, if after that
159 * operation the refcount is 0, the object is destroyed.
160 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
161 p_mi->i_refcount = 1;
162 /* object_lock strategy:
163 * - No lock held in constructor
164 * - Lock when accessing all variable this lock is held
165 * - Lock when attempting to destroy the object the lock is also held */
166 vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
167 &p_mi->object_lock );
168 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
169 p_libvlc_instance, p_e );
170 if( libvlc_exception_raised( p_e ) )
176 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
177 libvlc_MediaInstanceReachedEnd, p_e );
182 /**************************************************************************
183 * Create a Media Instance object with a media descriptor
184 **************************************************************************/
185 libvlc_media_instance_t *
186 libvlc_media_instance_new_from_media_descriptor(
187 libvlc_media_descriptor_t * p_md,
188 libvlc_exception_t *p_e )
190 libvlc_media_instance_t * p_mi;
191 p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
196 p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
201 /**************************************************************************
202 * Create a new media instance object from an input_thread (Libvlc Internal)
203 **************************************************************************/
204 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
205 struct libvlc_instance_t *p_libvlc_instance,
206 input_thread_t *p_input,
207 libvlc_exception_t *p_e )
209 libvlc_media_instance_t * p_mi;
213 libvlc_exception_raise( p_e, "invalid input thread" );
217 p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
222 p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
224 p_input->p->input.p_item, p_e );
228 libvlc_media_instance_destroy( p_mi );
232 p_mi->i_input_id = p_input->i_object_id;
234 /* will be released in media_instance_release() */
235 vlc_object_yield( p_input );
237 /* XXX: Hack as the playlist doesn't yield the input thread we retain
238 * the input for the playlist. (see corresponding hack in _release) */
239 vlc_object_yield( p_input );
244 /**************************************************************************
245 * Destroy a Media Instance object (libvlc internal)
247 * Warning: No lock held here, but hey, this is internal.
248 **************************************************************************/
249 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
251 input_thread_t *p_input_thread;
252 libvlc_exception_t p_e;
254 libvlc_exception_init( &p_e );
259 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
261 if( libvlc_exception_raised( &p_e ) )
263 libvlc_event_manager_release( p_mi->p_event_manager );
265 return; /* no need to worry about no input thread */
267 vlc_mutex_destroy( &p_mi->object_lock );
269 input_DestroyThread( p_input_thread );
271 libvlc_media_descriptor_release( p_mi->p_md );
276 /**************************************************************************
277 * Release a Media Instance object
278 **************************************************************************/
279 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
284 vlc_mutex_lock( &p_mi->object_lock );
288 if( p_mi->i_refcount > 0 )
290 vlc_mutex_unlock( &p_mi->object_lock );
293 vlc_mutex_unlock( &p_mi->object_lock );
294 vlc_mutex_destroy( &p_mi->object_lock );
296 libvlc_event_manager_release( p_mi->p_event_manager );
298 release_input_thread( p_mi );
300 libvlc_media_descriptor_release( p_mi->p_md );
305 /**************************************************************************
306 * Retain a Media Instance object
307 **************************************************************************/
308 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
315 /**************************************************************************
316 * Set the Media descriptor associated with the instance
317 **************************************************************************/
318 void libvlc_media_instance_set_media_descriptor(
319 libvlc_media_instance_t *p_mi,
320 libvlc_media_descriptor_t *p_md,
321 libvlc_exception_t *p_e )
328 vlc_mutex_lock( &p_mi->object_lock );
330 release_input_thread( p_mi );
332 libvlc_media_descriptor_release( p_mi->p_md );
337 vlc_mutex_unlock( &p_mi->object_lock );
338 return; /* It is ok to pass a NULL md */
341 p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
343 /* The policy here is to ignore that we were created using a different
344 * libvlc_instance, because we don't really care */
345 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
347 vlc_mutex_unlock( &p_mi->object_lock );
350 /**************************************************************************
351 * Get the Media descriptor associated with the instance
352 **************************************************************************/
353 libvlc_media_descriptor_t *
354 libvlc_media_instance_get_media_descriptor(
355 libvlc_media_instance_t *p_mi,
356 libvlc_exception_t *p_e )
363 return libvlc_media_descriptor_duplicate( p_mi->p_md );
366 /**************************************************************************
367 * Get the event Manager
368 **************************************************************************/
369 libvlc_event_manager_t *
370 libvlc_media_instance_event_manager(
371 libvlc_media_instance_t *p_mi,
372 libvlc_exception_t *p_e )
376 return p_mi->p_event_manager;
379 /**************************************************************************
381 **************************************************************************/
382 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
383 libvlc_exception_t *p_e )
385 input_thread_t * p_input_thread;
387 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
389 /* A thread alread exists, send it a play message */
391 val.i_int = PLAYING_S;
393 input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, PLAYING_S );
394 vlc_object_release( p_input_thread );
398 /* Ignore previous exception */
399 libvlc_exception_clear( p_e );
401 vlc_mutex_lock( &p_mi->object_lock );
405 libvlc_exception_raise( p_e, "no associated media descriptor" );
406 vlc_mutex_unlock( &p_mi->object_lock );
410 p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
411 p_mi->p_md->p_input_item );
412 p_mi->i_input_id = p_input_thread->i_object_id;
417 val.i_int = p_mi->drawable;
418 var_Create( p_input_thread, "drawable", VLC_VAR_DOINHERIT );
419 var_Set( p_input_thread, "drawable", val );
421 var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
423 /* will be released in media_instance_release() */
424 vlc_object_yield( p_input_thread );
426 vlc_mutex_unlock( &p_mi->object_lock );
429 /**************************************************************************
431 **************************************************************************/
432 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
433 libvlc_exception_t *p_e )
435 input_thread_t * p_input_thread;
439 p_input_thread = libvlc_get_input_thread( p_mi, p_e );
441 if( !p_input_thread )
444 input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, val );
445 vlc_object_release( p_input_thread );
448 /**************************************************************************
450 **************************************************************************/
451 void libvlc_media_instance_stop( libvlc_media_instance_t *p_mi,
452 libvlc_exception_t *p_e )
454 //libvlc_exception_raise( p_e, "Not implemented" );
457 /**************************************************************************
459 **************************************************************************/
460 void libvlc_media_instance_set_drawable( libvlc_media_instance_t *p_mi,
461 libvlc_drawable_t drawable,
462 libvlc_exception_t *p_e )
464 p_mi->drawable = drawable;
467 /**************************************************************************
468 * Getters for stream information
469 **************************************************************************/
470 vlc_int64_t libvlc_media_instance_get_length(
471 libvlc_media_instance_t *p_mi,
472 libvlc_exception_t *p_e )
474 input_thread_t *p_input_thread;
477 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
478 if( !p_input_thread )
481 var_Get( p_input_thread, "length", &val );
482 vlc_object_release( p_input_thread );
484 return (val.i_time+500LL)/1000LL;
487 vlc_int64_t libvlc_media_instance_get_time(
488 libvlc_media_instance_t *p_mi,
489 libvlc_exception_t *p_e )
491 input_thread_t *p_input_thread;
494 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
495 if( !p_input_thread )
498 var_Get( p_input_thread , "time", &val );
499 vlc_object_release( p_input_thread );
500 return (val.i_time+500LL)/1000LL;
503 void libvlc_media_instance_set_time(
504 libvlc_media_instance_t *p_mi,
506 libvlc_exception_t *p_e )
508 input_thread_t *p_input_thread;
511 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
512 if( !p_input_thread )
515 value.i_time = time*1000LL;
516 var_Set( p_input_thread, "time", value );
517 vlc_object_release( p_input_thread );
520 void libvlc_media_instance_set_position(
521 libvlc_media_instance_t *p_mi,
523 libvlc_exception_t *p_e )
525 input_thread_t *p_input_thread;
527 val.f_float = position;
529 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
530 if( !p_input_thread )
533 var_Set( p_input_thread, "position", val );
534 vlc_object_release( p_input_thread );
537 float libvlc_media_instance_get_position(
538 libvlc_media_instance_t *p_mi,
539 libvlc_exception_t *p_e )
541 input_thread_t *p_input_thread;
544 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
545 if( !p_input_thread )
548 var_Get( p_input_thread, "position", &val );
549 vlc_object_release( p_input_thread );
554 float libvlc_media_instance_get_fps(
555 libvlc_media_instance_t *p_mi,
556 libvlc_exception_t *p_e)
559 input_thread_t *p_input_thread;
561 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
562 if( !p_input_thread )
565 if( (NULL == p_input_thread->p->input.p_demux)
566 || demux2_Control( p_input_thread->p->input.p_demux, DEMUX_GET_FPS, &f_fps )
569 vlc_object_release( p_input_thread );
574 vlc_object_release( p_input_thread );
579 vlc_bool_t libvlc_media_instance_will_play(
580 libvlc_media_instance_t *p_mi,
581 libvlc_exception_t *p_e)
583 input_thread_t *p_input_thread =
584 libvlc_get_input_thread ( p_mi, p_e);
585 if ( !p_input_thread )
588 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
590 vlc_object_release( p_input_thread );
593 vlc_object_release( p_input_thread );
597 void libvlc_media_instance_set_rate(
598 libvlc_media_instance_t *p_mi,
600 libvlc_exception_t *p_e )
602 input_thread_t *p_input_thread;
606 RAISEVOID( "Rate value is invalid" );
608 val.i_int = 1000.0f/rate;
610 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
611 if ( !p_input_thread )
614 var_Set( p_input_thread, "rate", val );
615 vlc_object_release( p_input_thread );
618 float libvlc_media_instance_get_rate(
619 libvlc_media_instance_t *p_mi,
620 libvlc_exception_t *p_e )
622 input_thread_t *p_input_thread;
625 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
626 if ( !p_input_thread )
629 var_Get( p_input_thread, "rate", &val );
630 vlc_object_release( p_input_thread );
632 return (float)1000.0f/val.i_int;
635 int libvlc_media_instance_get_state(
636 libvlc_media_instance_t *p_mi,
637 libvlc_exception_t *p_e )
639 input_thread_t *p_input_thread;
642 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
643 if ( !p_input_thread )
646 var_Get( p_input_thread, "state", &val );
647 vlc_object_release( p_input_thread );