]> git.sesse.net Git - vlc/blob - src/control/media_instance.c
control/media_instance.c: Never fail when requesting the event manager.
[vlc] / src / control / media_instance.c
1 /*****************************************************************************
2  * media_instance.c: Libvlc API Media Instance management functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
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"
29
30 /*
31  * Release the associated input thread
32  *
33  * Object lock is NOT held.
34  */
35 static void release_input_thread( libvlc_media_instance_t *p_mi ) 
36 {
37     input_thread_t *p_input_thread;
38     vlc_bool_t should_destroy;
39
40     if( !p_mi || p_mi->i_input_id == -1 )
41         return;
42
43     p_input_thread = (input_thread_t*)vlc_object_get(
44                                              p_mi->p_libvlc_instance->p_libvlc_int,
45                                              p_mi->i_input_id );
46
47     p_mi->i_input_id = -1;
48
49     if( !p_input_thread )
50         return;
51     
52     /* release for previous vlc_object_get */
53     vlc_object_release( p_input_thread );
54
55     should_destroy = p_input_thread->i_refcount == 1;
56
57     /* release for initial p_input_thread yield (see _new()) */
58     vlc_object_release( p_input_thread );
59
60     /* No one is tracking this input_thread appart us. Destroy it */
61     if( should_destroy )
62         input_DestroyThread( p_input_thread );
63     else
64     {
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 );
69     }
70 }
71
72 /*
73  * Retrieve the input thread. Be sure to release the object
74  * once you are done with it. (libvlc Internal)
75  *
76  * Object lock is held.
77  */
78 input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
79                                          libvlc_exception_t *p_e ) 
80 {
81     input_thread_t *p_input_thread;
82
83     vlc_mutex_lock( &p_mi->object_lock );
84
85     if( !p_mi || p_mi->i_input_id == -1 )
86     {
87         vlc_mutex_unlock( &p_mi->object_lock );
88         RAISENULL( "Input is NULL" );
89     }
90
91     p_input_thread = (input_thread_t*)vlc_object_get(
92                                              p_mi->p_libvlc_instance->p_libvlc_int,
93                                              p_mi->i_input_id );
94     if( !p_input_thread )
95     {
96         vlc_mutex_unlock( &p_mi->object_lock );
97         RAISENULL( "Input does not exist" );
98     }
99
100     vlc_mutex_unlock( &p_mi->object_lock );
101     return p_input_thread;
102 }
103
104 /*
105  * input_state_changed (Private) (input var "state" Callback)
106  */
107 static int
108 input_state_changed( vlc_object_t * p_this, char const * psz_cmd,
109                      vlc_value_t oldval, vlc_value_t newval,
110                      void * p_userdata )
111 {
112     libvlc_media_instance_t * p_mi = p_userdata;
113     libvlc_event_t event;
114
115     if( newval.i_int == oldval.i_int )
116         return VLC_SUCCESS; /* No change since last time, don't propagate */
117
118     switch ( newval.i_int )
119     {
120         case END_S:
121             event.type = libvlc_MediaInstanceReachedEnd;
122             break;
123         default:
124             return VLC_SUCCESS;
125     }
126
127     libvlc_event_send( p_mi->p_event_manager, &event );
128     return VLC_SUCCESS;
129 }
130
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 )
137 {
138     libvlc_media_instance_t * p_mi;
139
140     if( !p_libvlc_instance )
141     {
142         libvlc_exception_raise( p_e, "invalid libvlc instance" );
143         return NULL;
144     }
145
146     p_mi = malloc( sizeof(libvlc_media_instance_t) );
147     p_mi->p_md = NULL;
148     p_mi->drawable = 0;
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 ) )
166     {
167         free( p_mi );
168         return NULL;
169     }
170  
171     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
172             libvlc_MediaInstanceReachedEnd, p_e );
173
174     return p_mi;
175 }
176
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 )
184 {
185     libvlc_media_instance_t * p_mi;
186     p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
187
188     if( !p_mi )
189         return NULL;
190
191     p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
192
193     return p_mi;
194 }
195
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 )
203 {
204     libvlc_media_instance_t * p_mi;
205
206     if( !p_input )
207     {
208         libvlc_exception_raise( p_e, "invalid input thread" );
209         return NULL;
210     }
211
212     p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
213
214     if( !p_mi )
215         return NULL;
216
217     p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
218                     p_libvlc_instance,
219                     p_input->p->input.p_item, p_e );
220
221     if( !p_mi->p_md )
222     {
223         libvlc_media_instance_destroy( p_mi );
224         return NULL;
225     }
226
227     p_mi->i_input_id = p_input->i_object_id;
228     
229     /* will be released in media_instance_release() */
230     vlc_object_yield( p_input );
231
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 );
235
236     return p_mi;
237 }
238
239 /**************************************************************************
240  * Destroy a Media Instance object (libvlc internal)
241  *
242  * Warning: No lock held here, but hey, this is internal.
243  **************************************************************************/
244 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
245 {
246     input_thread_t *p_input_thread;
247     libvlc_exception_t p_e;
248
249     libvlc_exception_init( &p_e );
250
251     if( !p_mi )
252         return;
253
254     p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
255
256     if( libvlc_exception_raised( &p_e ) )
257     {
258         libvlc_event_manager_release( p_mi->p_event_manager );
259         free( p_mi );
260         return; /* no need to worry about no input thread */
261     }
262
263     input_DestroyThread( p_input_thread );
264
265     libvlc_media_descriptor_release( p_mi->p_md );
266
267     free( p_mi );
268 }
269
270 /**************************************************************************
271  * Release a Media Instance object
272  **************************************************************************/
273 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
274 {
275     if( !p_mi )
276         return;
277
278     vlc_mutex_lock( &p_mi->object_lock );
279     
280     p_mi->i_refcount--;
281
282     if( p_mi->i_refcount > 0 )
283     {
284         vlc_mutex_unlock( &p_mi->object_lock );
285         return;
286     }
287     vlc_mutex_unlock( &p_mi->object_lock );
288
289     libvlc_event_manager_release( p_mi->p_event_manager );
290     
291     release_input_thread( p_mi );
292
293     libvlc_media_descriptor_release( p_mi->p_md );
294
295     free( p_mi );
296 }
297
298 /**************************************************************************
299  * Retain a Media Instance object
300  **************************************************************************/
301 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
302 {
303     if( !p_mi )
304         return;
305
306     p_mi->i_refcount++;
307 }
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 )
315 {
316     (void)p_e;
317
318     if( !p_mi )
319         return;
320
321     vlc_mutex_lock( &p_mi->object_lock );
322     
323     release_input_thread( p_mi );
324
325     libvlc_media_descriptor_release( p_mi->p_md );
326
327     if( !p_md )
328     {
329         p_mi->p_md = NULL;
330         vlc_mutex_unlock( &p_mi->object_lock );
331         return; /* It is ok to pass a NULL md */
332     }
333
334     p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
335     
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;
339
340     vlc_mutex_unlock( &p_mi->object_lock );
341 }
342
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 )
350 {
351     (void)p_e;
352
353     if( !p_mi->p_md )
354         return NULL;
355
356     return libvlc_media_descriptor_duplicate( p_mi->p_md );
357 }
358
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 )
366 {
367     (void)p_e;
368
369     return p_mi->p_event_manager;
370 }
371
372 /**************************************************************************
373  * Play
374  **************************************************************************/
375 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
376                                  libvlc_exception_t *p_e )
377 {
378     input_thread_t * p_input_thread;
379
380     if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) ) 
381     {
382         /* A thread alread exists, send it a play message */        
383         vlc_value_t val;
384         val.i_int = PLAYING_S;
385
386         input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, PLAYING_S );
387         vlc_object_release( p_input_thread );
388         return;
389     }
390
391     vlc_mutex_lock( &p_mi->object_lock );
392     
393     if( !p_mi->p_md )
394     {
395         libvlc_exception_raise( p_e, "no associated media descriptor" );
396         vlc_mutex_unlock( &p_mi->object_lock );
397         return;
398     }
399
400     p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
401                                          p_mi->p_md->p_input_item );
402     p_mi->i_input_id = p_input_thread->i_object_id;
403
404     if( p_mi->drawable )
405     {
406         vlc_value_t val;
407         val.i_int = p_mi->drawable;
408         var_Set( p_input_thread, "drawable", val );
409     }
410     var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
411
412     /* will be released in media_instance_release() */
413     vlc_object_yield( p_input_thread );
414
415     vlc_mutex_unlock( &p_mi->object_lock );
416 }
417
418 /**************************************************************************
419  * Pause
420  **************************************************************************/
421 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
422                                   libvlc_exception_t *p_e )
423 {
424     input_thread_t * p_input_thread;
425     vlc_value_t val;
426     val.i_int = PAUSE_S;
427
428     p_input_thread = libvlc_get_input_thread( p_mi, p_e );
429
430     if( !p_input_thread )
431         return;
432
433     input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, val );
434     vlc_object_release( p_input_thread );
435 }
436
437 /**************************************************************************
438  * Stop
439  **************************************************************************/
440 void libvlc_media_instance_stop( libvlc_media_instance_t *p_mi,
441                                  libvlc_exception_t *p_e )
442 {
443     libvlc_exception_raise( p_e, "Not implemented" );
444 }
445
446 /**************************************************************************
447  * Set Drawable
448  **************************************************************************/
449 void libvlc_media_instance_set_drawable( libvlc_media_instance_t *p_mi,
450                                          libvlc_drawable_t drawable,
451                                          libvlc_exception_t *p_e )
452 {
453     p_mi->drawable = drawable;
454 }
455
456 /**************************************************************************
457  * Getters for stream information
458  **************************************************************************/
459 vlc_int64_t libvlc_media_instance_get_length(
460                              libvlc_media_instance_t *p_mi,
461                              libvlc_exception_t *p_e )
462 {
463     input_thread_t *p_input_thread;
464     vlc_value_t val;
465
466     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
467     if( !p_input_thread )
468         return -1;
469
470     var_Get( p_input_thread, "length", &val );
471     vlc_object_release( p_input_thread );
472
473     return (val.i_time+500LL)/1000LL;
474 }
475
476 vlc_int64_t libvlc_media_instance_get_time(
477                                    libvlc_media_instance_t *p_mi,
478                                    libvlc_exception_t *p_e )
479 {
480     input_thread_t *p_input_thread;
481     vlc_value_t val;
482
483     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
484     if( !p_input_thread )
485         return -1;
486
487     var_Get( p_input_thread , "time", &val );
488     vlc_object_release( p_input_thread );
489     return (val.i_time+500LL)/1000LL;
490 }
491
492 void libvlc_media_instance_set_time(
493                                  libvlc_media_instance_t *p_mi,
494                                  vlc_int64_t time,
495                                  libvlc_exception_t *p_e )
496 {
497     input_thread_t *p_input_thread;
498     vlc_value_t value;
499
500     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
501     if( !p_input_thread )
502         return;
503
504     value.i_time = time*1000LL;
505     var_Set( p_input_thread, "time", value );
506     vlc_object_release( p_input_thread );
507 }
508
509 void libvlc_media_instance_set_position(
510                                 libvlc_media_instance_t *p_mi,
511                                 float position,
512                                 libvlc_exception_t *p_e ) 
513 {
514     input_thread_t *p_input_thread;
515     vlc_value_t val;
516     val.f_float = position;
517
518     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
519     if( !p_input_thread )
520         return;
521
522     var_Set( p_input_thread, "position", val );
523     vlc_object_release( p_input_thread );
524 }
525
526 float libvlc_media_instance_get_position(
527                                  libvlc_media_instance_t *p_mi,
528                                  libvlc_exception_t *p_e )
529 {
530     input_thread_t *p_input_thread;
531     vlc_value_t val;
532
533     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
534     if( !p_input_thread )
535         return -1.0;
536
537     var_Get( p_input_thread, "position", &val );
538     vlc_object_release( p_input_thread );
539
540     return val.f_float;
541 }
542
543 float libvlc_media_instance_get_fps(
544                                  libvlc_media_instance_t *p_mi,
545                                  libvlc_exception_t *p_e) 
546 {
547     double f_fps = 0.0;
548     input_thread_t *p_input_thread;
549
550     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
551     if( !p_input_thread )
552         return 0.0;
553
554     if( (NULL == p_input_thread->p->input.p_demux)
555         || demux2_Control( p_input_thread->p->input.p_demux, DEMUX_GET_FPS, &f_fps )
556         || f_fps < 0.1 )
557     {
558         vlc_object_release( p_input_thread );
559         return 0.0;
560     }
561     else
562     {
563         vlc_object_release( p_input_thread );
564         return( f_fps );
565     }
566 }
567
568 vlc_bool_t libvlc_media_instance_will_play(
569                                  libvlc_media_instance_t *p_mi,
570                                  libvlc_exception_t *p_e) 
571 {
572     input_thread_t *p_input_thread =
573                             libvlc_get_input_thread ( p_mi, p_e);
574     if ( !p_input_thread )
575         return VLC_FALSE;
576
577     if ( !p_input_thread->b_die && !p_input_thread->b_dead ) 
578     {
579         vlc_object_release( p_input_thread );
580         return VLC_TRUE;
581     }
582     vlc_object_release( p_input_thread );
583     return VLC_FALSE;
584 }
585
586 void libvlc_media_instance_set_rate(
587                                  libvlc_media_instance_t *p_mi,
588                                  float rate,
589                                  libvlc_exception_t *p_e ) 
590 {
591     input_thread_t *p_input_thread;
592     vlc_value_t val;
593
594     if( rate <= 0 )
595         RAISEVOID( "Rate value is invalid" );
596
597     val.i_int = 1000.0f/rate;
598
599     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
600     if ( !p_input_thread )
601         return;
602
603     var_Set( p_input_thread, "rate", val );
604     vlc_object_release( p_input_thread );
605 }
606
607 float libvlc_media_instance_get_rate(
608                                  libvlc_media_instance_t *p_mi,
609                                  libvlc_exception_t *p_e )
610 {
611     input_thread_t *p_input_thread;
612     vlc_value_t val;
613
614     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
615     if ( !p_input_thread )
616         return -1.0;
617
618     var_Get( p_input_thread, "rate", &val );
619     vlc_object_release( p_input_thread );
620
621     return (float)1000.0f/val.i_int;
622 }
623
624 int libvlc_media_instance_get_state(
625                                  libvlc_media_instance_t *p_mi,
626                                  libvlc_exception_t *p_e )
627 {
628     input_thread_t *p_input_thread;
629     vlc_value_t val;
630
631     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
632     if ( !p_input_thread )
633         return 0;
634
635     var_Get( p_input_thread, "state", &val );
636     vlc_object_release( p_input_thread );
637
638     return val.i_int;
639 }
640