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) );
148 p_mi->p_libvlc_instance = p_libvlc_instance;
149 p_mi->i_input_id = -1;
150 /* refcount strategy:
151 * - All items created by _new start with a refcount set to 1
152 * - Accessor _release decrease the refcount by 1, if after that
153 * operation the refcount is 0, the object is destroyed.
154 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
155 p_mi->i_refcount = 1;
156 /* object_lock strategy:
157 * - No lock held in constructor
158 * - Lock when accessing all variable this lock is held
159 * - Lock when attempting to destroy the object the lock is also held */
160 vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
161 &p_mi->object_lock );
162 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
163 p_libvlc_instance, p_e );
164 if( libvlc_exception_raised( p_e ) )
170 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
171 libvlc_MediaInstanceReachedEnd, p_e );
176 /**************************************************************************
177 * Create a Media Instance object with a media descriptor
178 **************************************************************************/
179 libvlc_media_instance_t *
180 libvlc_media_instance_new_from_media_descriptor(
181 libvlc_media_descriptor_t * p_md,
182 libvlc_exception_t *p_e )
184 libvlc_media_instance_t * p_mi;
185 p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
190 p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
195 /**************************************************************************
196 * Create a new media instance object from an input_thread (Libvlc Internal)
197 **************************************************************************/
198 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
199 struct libvlc_instance_t *p_libvlc_instance,
200 input_thread_t *p_input,
201 libvlc_exception_t *p_e )
203 libvlc_media_instance_t * p_mi;
207 libvlc_exception_raise( p_e, "invalid input thread" );
211 p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
216 p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
218 p_input->p->input.p_item, p_e );
222 libvlc_media_instance_destroy( p_mi );
226 p_mi->i_input_id = p_input->i_object_id;
228 /* will be released in media_instance_release() */
229 vlc_object_yield( p_input );
231 /* XXX: Hack as the playlist doesn't yield the input thread we retain
232 * the input for the playlist. (see corresponding hack in _release) */
233 vlc_object_yield( p_input );
238 /**************************************************************************
239 * Destroy a Media Instance object (libvlc internal)
241 * Warning: No lock held here, but hey, this is internal.
242 **************************************************************************/
243 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
245 input_thread_t *p_input_thread;
246 libvlc_exception_t p_e;
248 libvlc_exception_init( &p_e );
253 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
255 if( libvlc_exception_raised( &p_e ) )
257 libvlc_event_manager_release( p_mi->p_event_manager );
259 return; /* no need to worry about no input thread */
262 input_DestroyThread( p_input_thread );
264 libvlc_media_descriptor_destroy( p_mi->p_md );
269 /**************************************************************************
270 * Release a Media Instance object
271 **************************************************************************/
272 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
277 vlc_mutex_lock( &p_mi->object_lock );
281 /* We hold the mutex, as a waiter to make sure pending operations
282 * are finished. We can't hold it longer as the get_input_thread
283 * function holds a lock. */
285 vlc_mutex_unlock( &p_mi->object_lock );
287 if( p_mi->i_refcount > 0 )
290 libvlc_event_manager_release( p_mi->p_event_manager );
292 release_input_thread( p_mi );
294 libvlc_media_descriptor_destroy( p_mi->p_md );
299 /**************************************************************************
300 * Set the Media descriptor associated with the instance
301 **************************************************************************/
302 void libvlc_media_instance_set_media_descriptor(
303 libvlc_media_instance_t *p_mi,
304 libvlc_media_descriptor_t *p_md,
305 libvlc_exception_t *p_e )
312 vlc_mutex_lock( &p_mi->object_lock );
314 release_input_thread( p_mi );
316 libvlc_media_descriptor_destroy( p_mi->p_md );
321 vlc_mutex_unlock( &p_mi->object_lock );
322 return; /* It is ok to pass a NULL md */
325 p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
327 /* The policy here is to ignore that we were created using a different
328 * libvlc_instance, because we don't really care */
329 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
331 vlc_mutex_unlock( &p_mi->object_lock );
334 /**************************************************************************
335 * Get the Media descriptor associated with the instance
336 **************************************************************************/
337 libvlc_media_descriptor_t *
338 libvlc_media_instance_get_media_descriptor(
339 libvlc_media_instance_t *p_mi,
340 libvlc_exception_t *p_e )
347 return libvlc_media_descriptor_duplicate( p_mi->p_md );
350 /**************************************************************************
351 * Get the event Manager
352 **************************************************************************/
353 libvlc_event_manager_t *
354 libvlc_media_instance_event_manager(
355 libvlc_media_instance_t *p_mi,
356 libvlc_exception_t *p_e )
363 return p_mi->p_event_manager;
366 /**************************************************************************
368 **************************************************************************/
369 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
370 libvlc_exception_t *p_e )
372 input_thread_t * p_input_thread;
374 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
376 /* A thread alread exists, send it a play message */
378 val.i_int = PLAYING_S;
380 input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, PLAYING_S );
381 vlc_object_release( p_input_thread );
385 vlc_mutex_lock( &p_mi->object_lock );
389 libvlc_exception_raise( p_e, "no associated media descriptor" );
390 vlc_mutex_unlock( &p_mi->object_lock );
394 p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
395 p_mi->p_md->p_input_item );
396 p_mi->i_input_id = p_input_thread->i_object_id;
398 var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
400 /* will be released in media_instance_release() */
401 vlc_object_yield( p_input_thread );
403 vlc_mutex_unlock( &p_mi->object_lock );
406 /**************************************************************************
408 **************************************************************************/
409 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
410 libvlc_exception_t *p_e )
412 input_thread_t * p_input_thread;
416 p_input_thread = libvlc_get_input_thread( p_mi, p_e );
418 if( !p_input_thread )
421 input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, val );
422 vlc_object_release( p_input_thread );
425 /**************************************************************************
426 * Getters for stream information
427 **************************************************************************/
428 vlc_int64_t libvlc_media_instance_get_length(
429 libvlc_media_instance_t *p_mi,
430 libvlc_exception_t *p_e )
432 input_thread_t *p_input_thread;
435 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
436 if( !p_input_thread )
439 var_Get( p_input_thread, "length", &val );
440 vlc_object_release( p_input_thread );
442 return (val.i_time+500LL)/1000LL;
445 vlc_int64_t libvlc_media_instance_get_time(
446 libvlc_media_instance_t *p_mi,
447 libvlc_exception_t *p_e )
449 input_thread_t *p_input_thread;
452 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
453 if( !p_input_thread )
456 var_Get( p_input_thread , "time", &val );
457 vlc_object_release( p_input_thread );
458 return (val.i_time+500LL)/1000LL;
461 void libvlc_media_instance_set_time(
462 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 value.i_time = time*1000LL;
474 var_Set( p_input_thread, "time", value );
475 vlc_object_release( p_input_thread );
478 void libvlc_media_instance_set_position(
479 libvlc_media_instance_t *p_mi,
481 libvlc_exception_t *p_e )
483 input_thread_t *p_input_thread;
485 val.f_float = position;
487 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
488 if( !p_input_thread )
491 var_Set( p_input_thread, "position", val );
492 vlc_object_release( p_input_thread );
495 float libvlc_media_instance_get_position(
496 libvlc_media_instance_t *p_mi,
497 libvlc_exception_t *p_e )
499 input_thread_t *p_input_thread;
502 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
503 if( !p_input_thread )
506 var_Get( p_input_thread, "position", &val );
507 vlc_object_release( p_input_thread );
512 float libvlc_media_instance_get_fps(
513 libvlc_media_instance_t *p_mi,
514 libvlc_exception_t *p_e)
517 input_thread_t *p_input_thread;
519 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
520 if( !p_input_thread )
523 if( (NULL == p_input_thread->p->input.p_demux)
524 || demux2_Control( p_input_thread->p->input.p_demux, DEMUX_GET_FPS, &f_fps )
527 vlc_object_release( p_input_thread );
532 vlc_object_release( p_input_thread );
537 vlc_bool_t libvlc_media_instance_will_play(
538 libvlc_media_instance_t *p_mi,
539 libvlc_exception_t *p_e)
541 input_thread_t *p_input_thread =
542 libvlc_get_input_thread ( p_mi, p_e);
543 if ( !p_input_thread )
546 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
548 vlc_object_release( p_input_thread );
551 vlc_object_release( p_input_thread );
555 void libvlc_media_instance_set_rate(
556 libvlc_media_instance_t *p_mi,
558 libvlc_exception_t *p_e )
560 input_thread_t *p_input_thread;
564 RAISEVOID( "Rate value is invalid" );
566 val.i_int = 1000.0f/rate;
568 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
569 if ( !p_input_thread )
572 var_Set( p_input_thread, "rate", val );
573 vlc_object_release( p_input_thread );
576 float libvlc_media_instance_get_rate(
577 libvlc_media_instance_t *p_mi,
578 libvlc_exception_t *p_e )
580 input_thread_t *p_input_thread;
583 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
584 if ( !p_input_thread )
587 var_Get( p_input_thread, "rate", &val );
588 vlc_object_release( p_input_thread );
590 return (float)1000.0f/val.i_int;
593 int libvlc_media_instance_get_state(
594 libvlc_media_instance_t *p_mi,
595 libvlc_exception_t *p_e )
597 input_thread_t *p_input_thread;
600 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
601 if ( !p_input_thread )
604 var_Get( p_input_thread, "state", &val );
605 vlc_object_release( p_input_thread );