]> git.sesse.net Git - vlc/blob - src/control/media_instance.c
control/media_instance.c: Remove access to p_input_thread->p_internals. (which was...
[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 <vlc/libvlc.h>
25 #include <vlc_demux.h>
26 #include <vlc_input.h>
27 #include "libvlc_internal.h"
28 #include "libvlc.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     /* release for initial p_input_thread yield (see _new()) */
56     vlc_object_release( p_input_thread );
57
58     /* No one is tracking this input_thread appart us. Destroy it */
59     if( p_mi->b_own_its_input_thread )
60     {
61         /* We owned this one */
62         input_StopThread( p_input_thread );
63         var_Destroy( p_input_thread, "drawable" );
64         input_DestroyThread( p_input_thread );
65     }
66     else
67     {
68         /* XXX: hack the playlist doesn't retain the input thread,
69          * so we did it for the playlist (see _new_from_input_thread),
70          * revert that here. This will be deleted with the playlist API */
71         vlc_object_release( p_input_thread );
72     }
73 }
74
75 /*
76  * Retrieve the input thread. Be sure to release the object
77  * once you are done with it. (libvlc Internal)
78  *
79  * Object lock is held.
80  */
81 input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
82                                          libvlc_exception_t *p_e )
83 {
84     input_thread_t *p_input_thread;
85
86     vlc_mutex_lock( &p_mi->object_lock );
87
88     if( !p_mi || p_mi->i_input_id == -1 )
89     {
90         vlc_mutex_unlock( &p_mi->object_lock );
91         RAISENULL( "Input is NULL" );
92     }
93
94     p_input_thread = (input_thread_t*)vlc_object_get(
95                                              p_mi->p_libvlc_instance->p_libvlc_int,
96                                              p_mi->i_input_id );
97     if( !p_input_thread )
98     {
99         vlc_mutex_unlock( &p_mi->object_lock );
100         RAISENULL( "Input does not exist" );
101     }
102
103     vlc_mutex_unlock( &p_mi->object_lock );
104     return p_input_thread;
105 }
106
107 /*
108  * input_state_changed (Private) (input var "state" Callback)
109  */
110 static int
111 input_state_changed( vlc_object_t * p_this, char const * psz_cmd,
112                      vlc_value_t oldval, vlc_value_t newval,
113                      void * p_userdata )
114 {
115     libvlc_media_instance_t * p_mi = p_userdata;
116     libvlc_event_t event;
117
118     if( newval.i_int == oldval.i_int )
119         return VLC_SUCCESS; /* No change since last time, don't propagate */
120
121     switch ( newval.i_int )
122     {
123         case END_S:
124             event.type = libvlc_MediaInstanceReachedEnd;
125             break;
126         case PAUSE_S:
127             event.type = libvlc_MediaInstancePaused;
128             break;
129         case PLAYING_S:
130             event.type = libvlc_MediaInstancePlayed;
131             break;
132         default:
133             return VLC_SUCCESS;
134     }
135
136     libvlc_event_send( p_mi->p_event_manager, &event );
137     return VLC_SUCCESS;
138 }
139
140 /*
141  * input_position_changed (Private) (input var "intf-change" Callback)
142  */
143 static int
144 input_position_changed( vlc_object_t * p_this, char const * psz_cmd,
145                      vlc_value_t oldval, vlc_value_t newval,
146                      void * p_userdata )
147 {
148     libvlc_media_instance_t * p_mi = p_userdata;
149     vlc_value_t val;
150  
151     if (!strcmp(psz_cmd, "intf" /* "-change" no need to go further */))
152     {
153         input_thread_t * p_input = (input_thread_t *)p_this;
154         var_Get( p_input, "position", &val );
155
156         if ((val.i_time % I64C(500000)) != 0)
157             return VLC_SUCCESS; /* No need to have a better precision */
158         var_Get( p_input, "state", &val );
159
160         if( val.i_int != PLAYING_S )
161             return VLC_SUCCESS; /* Don't send the position while stopped */
162     }
163     else
164         val.i_time = newval.i_time;
165
166     libvlc_event_t event;
167     event.type = libvlc_MediaInstancePositionChanged;
168     event.u.media_instance_position_changed.new_position = val.i_time;
169
170     libvlc_event_send( p_mi->p_event_manager, &event );
171     return VLC_SUCCESS;
172 }
173
174 /**************************************************************************
175  * Create a Media Instance object
176  **************************************************************************/
177 libvlc_media_instance_t *
178 libvlc_media_instance_new( libvlc_instance_t * p_libvlc_instance,
179                            libvlc_exception_t * p_e )
180 {
181     libvlc_media_instance_t * p_mi;
182
183     if( !p_libvlc_instance )
184     {
185         libvlc_exception_raise( p_e, "invalid libvlc instance" );
186         return NULL;
187     }
188
189     p_mi = malloc( sizeof(libvlc_media_instance_t) );
190     p_mi->p_md = NULL;
191     p_mi->drawable = 0;
192     p_mi->p_libvlc_instance = p_libvlc_instance;
193     p_mi->i_input_id = -1;
194     /* refcount strategy:
195      * - All items created by _new start with a refcount set to 1
196      * - Accessor _release decrease the refcount by 1, if after that
197      *   operation the refcount is 0, the object is destroyed.
198      * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
199     p_mi->i_refcount = 1;
200     p_mi->b_own_its_input_thread = VLC_TRUE;
201     /* object_lock strategy:
202      * - No lock held in constructor
203      * - Lock when accessing all variable this lock is held
204      * - Lock when attempting to destroy the object the lock is also held */
205     vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
206                     &p_mi->object_lock );
207     p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
208             p_libvlc_instance, p_e );
209     if( libvlc_exception_raised( p_e ) )
210     {
211         free( p_mi );
212         return NULL;
213     }
214  
215     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
216             libvlc_MediaInstanceReachedEnd, p_e );
217
218     return p_mi;
219 }
220
221 /**************************************************************************
222  * Create a Media Instance object with a media descriptor
223  **************************************************************************/
224 libvlc_media_instance_t *
225 libvlc_media_instance_new_from_media_descriptor(
226                                     libvlc_media_descriptor_t * p_md,
227                                     libvlc_exception_t *p_e )
228 {
229     libvlc_media_instance_t * p_mi;
230     p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
231
232     if( !p_mi )
233         return NULL;
234
235     libvlc_media_descriptor_retain( p_md );
236     p_mi->p_md = p_md;
237
238     return p_mi;
239 }
240
241 /**************************************************************************
242  * Create a new media instance object from an input_thread (Libvlc Internal)
243  **************************************************************************/
244 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
245                                    struct libvlc_instance_t *p_libvlc_instance,
246                                    input_thread_t *p_input,
247                                    libvlc_exception_t *p_e )
248 {
249     libvlc_media_instance_t * p_mi;
250
251     if( !p_input )
252     {
253         libvlc_exception_raise( p_e, "invalid input thread" );
254         return NULL;
255     }
256
257     p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
258
259     if( !p_mi )
260         return NULL;
261
262     p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
263                     p_libvlc_instance,
264                     input_GetItem( p_input ), p_e );
265
266     if( !p_mi->p_md )
267     {
268         libvlc_media_instance_destroy( p_mi );
269         return NULL;
270     }
271
272     p_mi->i_input_id = p_input->i_object_id;
273     p_mi->b_own_its_input_thread = VLC_FALSE;
274
275     /* will be released in media_instance_release() */
276     vlc_object_yield( p_input );
277
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 );
281
282     return p_mi;
283 }
284
285 /**************************************************************************
286  * Destroy a Media Instance object (libvlc internal)
287  *
288  * Warning: No lock held here, but hey, this is internal.
289  **************************************************************************/
290 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
291 {
292     input_thread_t *p_input_thread;
293     libvlc_exception_t p_e;
294
295     libvlc_exception_init( &p_e );
296
297     if( !p_mi )
298         return;
299
300     p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
301
302     if( libvlc_exception_raised( &p_e ) )
303     {
304         libvlc_event_manager_release( p_mi->p_event_manager );
305         free( p_mi );
306         return; /* no need to worry about no input thread */
307     }
308     vlc_mutex_destroy( &p_mi->object_lock );
309
310     input_DestroyThread( p_input_thread );
311
312     libvlc_media_descriptor_release( p_mi->p_md );
313
314     free( p_mi );
315 }
316
317 /**************************************************************************
318  * Release a Media Instance object
319  **************************************************************************/
320 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
321 {
322     if( !p_mi )
323         return;
324
325     vlc_mutex_lock( &p_mi->object_lock );
326  
327     p_mi->i_refcount--;
328
329     if( p_mi->i_refcount > 0 )
330     {
331         vlc_mutex_unlock( &p_mi->object_lock );
332         return;
333     }
334     vlc_mutex_unlock( &p_mi->object_lock );
335     vlc_mutex_destroy( &p_mi->object_lock );
336
337     libvlc_event_manager_release( p_mi->p_event_manager );
338  
339     release_input_thread( p_mi );
340
341     libvlc_media_descriptor_release( p_mi->p_md );
342
343     free( p_mi );
344 }
345
346 /**************************************************************************
347  * Retain a Media Instance object
348  **************************************************************************/
349 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
350 {
351     if( !p_mi )
352         return;
353
354     p_mi->i_refcount++;
355 }
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 )
363 {
364     (void)p_e;
365
366     if( !p_mi )
367         return;
368
369     vlc_mutex_lock( &p_mi->object_lock );
370
371     release_input_thread( p_mi );
372
373     libvlc_media_descriptor_release( p_mi->p_md );
374
375     if( !p_md )
376     {
377         p_mi->p_md = NULL;
378         vlc_mutex_unlock( &p_mi->object_lock );
379         return; /* It is ok to pass a NULL md */
380     }
381
382     libvlc_media_descriptor_retain( p_md );
383     p_mi->p_md = p_md;
384  
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;
388
389     vlc_mutex_unlock( &p_mi->object_lock );
390 }
391
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 )
399 {
400     (void)p_e;
401
402     if( !p_mi->p_md )
403         return NULL;
404
405     libvlc_media_descriptor_retain( p_mi->p_md );
406     return p_mi->p_md;
407 }
408
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 )
416 {
417     (void)p_e;
418
419     return p_mi->p_event_manager;
420 }
421
422 /**************************************************************************
423  * Play
424  **************************************************************************/
425 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
426                                  libvlc_exception_t *p_e )
427 {
428     input_thread_t * p_input_thread;
429
430     if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
431     {
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 );
435         return;
436     }
437
438     /* Ignore previous exception */
439     libvlc_exception_clear( p_e );
440
441     vlc_mutex_lock( &p_mi->object_lock );
442  
443     if( !p_mi->p_md )
444     {
445         libvlc_exception_raise( p_e, "no associated media descriptor" );
446         vlc_mutex_unlock( &p_mi->object_lock );
447         return;
448     }
449
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;
453
454     if( p_mi->drawable )
455     {
456         vlc_value_t val;
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 );
460     }
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 );
463
464     /* will be released in media_instance_release() */
465     vlc_object_yield( p_input_thread );
466
467     vlc_mutex_unlock( &p_mi->object_lock );
468 }
469
470 /**************************************************************************
471  * Pause
472  **************************************************************************/
473 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
474                                   libvlc_exception_t *p_e )
475 {
476     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
477
478     if( !p_input_thread )
479         return;
480
481     input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
482     vlc_object_release( p_input_thread );
483 }
484
485 /**************************************************************************
486  * Stop
487  **************************************************************************/
488 void libvlc_media_instance_stop( libvlc_media_instance_t *p_mi,
489                                  libvlc_exception_t *p_e )
490 {
491     //libvlc_exception_raise( p_e, "Not implemented" );
492 }
493
494 /**************************************************************************
495  * Set Drawable
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 )
500 {
501     p_mi->drawable = drawable;
502 }
503
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 )
510 {
511     input_thread_t *p_input_thread;
512     vlc_value_t val;
513
514     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
515     if( !p_input_thread )
516         return -1;
517
518     var_Get( p_input_thread, "length", &val );
519     vlc_object_release( p_input_thread );
520
521     return (val.i_time+500LL)/1000LL;
522 }
523
524 vlc_int64_t libvlc_media_instance_get_time(
525                                    libvlc_media_instance_t *p_mi,
526                                    libvlc_exception_t *p_e )
527 {
528     input_thread_t *p_input_thread;
529     vlc_value_t val;
530
531     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
532     if( !p_input_thread )
533         return -1;
534
535     var_Get( p_input_thread , "time", &val );
536     vlc_object_release( p_input_thread );
537     return (val.i_time+500LL)/1000LL;
538 }
539
540 void libvlc_media_instance_set_time(
541                                  libvlc_media_instance_t *p_mi,
542                                  vlc_int64_t time,
543                                  libvlc_exception_t *p_e )
544 {
545     input_thread_t *p_input_thread;
546     vlc_value_t value;
547
548     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
549     if( !p_input_thread )
550         return;
551
552     value.i_time = time*1000LL;
553     var_Set( p_input_thread, "time", value );
554     vlc_object_release( p_input_thread );
555 }
556
557 void libvlc_media_instance_set_position(
558                                 libvlc_media_instance_t *p_mi,
559                                 float position,
560                                 libvlc_exception_t *p_e )
561 {
562     input_thread_t *p_input_thread;
563     vlc_value_t val;
564     val.f_float = position;
565
566     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
567     if( !p_input_thread )
568         return;
569
570     var_Set( p_input_thread, "position", val );
571     vlc_object_release( p_input_thread );
572 }
573
574 float libvlc_media_instance_get_position(
575                                  libvlc_media_instance_t *p_mi,
576                                  libvlc_exception_t *p_e )
577 {
578     input_thread_t *p_input_thread;
579     vlc_value_t val;
580
581     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
582     if( !p_input_thread )
583         return -1.0;
584
585     var_Get( p_input_thread, "position", &val );
586     vlc_object_release( p_input_thread );
587
588     return val.f_float;
589 }
590
591 float libvlc_media_instance_get_fps(
592                                  libvlc_media_instance_t *p_mi,
593                                  libvlc_exception_t *p_e)
594 {
595     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
596     double f_fps = 0.0;
597
598     if( p_input_thread )
599     {
600         if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
601             f_fps = 0.0;
602         vlc_object_release( p_input_thread );
603     }
604     return f_fps;
605 }
606
607 vlc_bool_t libvlc_media_instance_will_play(
608                                  libvlc_media_instance_t *p_mi,
609                                  libvlc_exception_t *p_e)
610 {
611     input_thread_t *p_input_thread =
612                             libvlc_get_input_thread ( p_mi, p_e);
613     if ( !p_input_thread )
614         return VLC_FALSE;
615
616     if ( !p_input_thread->b_die && !p_input_thread->b_dead )
617     {
618         vlc_object_release( p_input_thread );
619         return VLC_TRUE;
620     }
621     vlc_object_release( p_input_thread );
622     return VLC_FALSE;
623 }
624
625 void libvlc_media_instance_set_rate(
626                                  libvlc_media_instance_t *p_mi,
627                                  float rate,
628                                  libvlc_exception_t *p_e )
629 {
630     input_thread_t *p_input_thread;
631     vlc_value_t val;
632
633     if( rate <= 0 )
634         RAISEVOID( "Rate value is invalid" );
635
636     val.i_int = 1000.0f/rate;
637
638     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
639     if ( !p_input_thread )
640         return;
641
642     var_Set( p_input_thread, "rate", val );
643     vlc_object_release( p_input_thread );
644 }
645
646 float libvlc_media_instance_get_rate(
647                                  libvlc_media_instance_t *p_mi,
648                                  libvlc_exception_t *p_e )
649 {
650     input_thread_t *p_input_thread;
651     vlc_value_t val;
652
653     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
654     if ( !p_input_thread )
655         return -1.0;
656
657     var_Get( p_input_thread, "rate", &val );
658     vlc_object_release( p_input_thread );
659
660     return (float)1000.0f/val.i_int;
661 }
662
663 static libvlc_state_t vlc_to_libvlc_state[] =
664 {
665     [INIT_S]        = libvlc_Opening,
666     [OPENING_S]     = libvlc_Opening,
667     [BUFFERING_S]   = libvlc_Buffering,    
668     [PLAYING_S]     = libvlc_Playing,    
669     [PAUSE_S]       = libvlc_Paused,    
670     [END_S]         = libvlc_Ended,    
671     [ERROR_S]       = libvlc_Error,    
672 };
673
674 libvlc_state_t libvlc_media_instance_get_state(
675                                  libvlc_media_instance_t *p_mi,
676                                  libvlc_exception_t *p_e )
677 {
678     input_thread_t *p_input_thread;
679     vlc_value_t val;
680
681     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
682     if ( !p_input_thread )
683         return libvlc_Stopped;
684
685     var_Get( p_input_thread, "state", &val );
686     vlc_object_release( p_input_thread );
687
688     if( val.i_int < 0 || val.i_int > 6 )
689         return libvlc_Stopped;
690
691     return vlc_to_libvlc_state[val.i_int];
692 }