]> git.sesse.net Git - vlc/blob - src/control/media_instance.c
Libvlc: Start the implementation of the libvlc playlist. Still in progress.
[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->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 ) )
165     {
166         free( p_mi );
167         return NULL;
168     }
169  
170     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
171             libvlc_MediaInstanceReachedEnd, p_e );
172
173     return p_mi;
174 }
175
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 )
183 {
184     libvlc_media_instance_t * p_mi;
185     p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
186
187     if( !p_mi )
188         return NULL;
189
190     p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
191
192     return p_mi;
193 }
194
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 )
202 {
203     libvlc_media_instance_t * p_mi;
204
205     if( !p_input )
206     {
207         libvlc_exception_raise( p_e, "invalid input thread" );
208         return NULL;
209     }
210
211     p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
212
213     if( !p_mi )
214         return NULL;
215
216     p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
217                     p_libvlc_instance,
218                     p_input->p->input.p_item, p_e );
219
220     if( !p_mi->p_md )
221     {
222         libvlc_media_instance_destroy( p_mi );
223         return NULL;
224     }
225
226     p_mi->i_input_id = p_input->i_object_id;
227     
228     /* will be released in media_instance_release() */
229     vlc_object_yield( p_input );
230
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 );
234
235     return p_mi;
236 }
237
238 /**************************************************************************
239  * Destroy a Media Instance object (libvlc internal)
240  *
241  * Warning: No lock held here, but hey, this is internal.
242  **************************************************************************/
243 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
244 {
245     input_thread_t *p_input_thread;
246     libvlc_exception_t p_e;
247
248     libvlc_exception_init( &p_e );
249
250     if( !p_mi )
251         return;
252
253     p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
254
255     if( libvlc_exception_raised( &p_e ) )
256     {
257         libvlc_event_manager_release( p_mi->p_event_manager );
258         free( p_mi );
259         return; /* no need to worry about no input thread */
260     }
261
262     input_DestroyThread( p_input_thread );
263
264     libvlc_media_descriptor_release( p_mi->p_md );
265
266     free( p_mi );
267 }
268
269 /**************************************************************************
270  * Release a Media Instance object
271  **************************************************************************/
272 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
273 {
274     if( !p_mi )
275         return;
276
277     vlc_mutex_lock( &p_mi->object_lock );
278     
279     p_mi->i_refcount--;
280
281     if( p_mi->i_refcount > 0 )
282     {
283         vlc_mutex_unlock( &p_mi->object_lock );
284         return;
285     }
286     vlc_mutex_unlock( &p_mi->object_lock );
287
288     libvlc_event_manager_release( p_mi->p_event_manager );
289     
290     release_input_thread( p_mi );
291
292     libvlc_media_descriptor_release( p_mi->p_md );
293
294     free( p_mi );
295 }
296
297 /**************************************************************************
298  * Retain a Media Instance object
299  **************************************************************************/
300 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
301 {
302     if( !p_mi )
303         return;
304
305     p_mi->i_refcount++;
306 }
307 /**************************************************************************
308  * Set the Media descriptor associated with the instance
309  **************************************************************************/
310 void libvlc_media_instance_set_media_descriptor(
311                             libvlc_media_instance_t *p_mi,
312                             libvlc_media_descriptor_t *p_md,
313                             libvlc_exception_t *p_e )
314 {
315     (void)p_e;
316
317     if( !p_mi )
318         return;
319
320     vlc_mutex_lock( &p_mi->object_lock );
321     
322     release_input_thread( p_mi );
323
324     libvlc_media_descriptor_release( p_mi->p_md );
325
326     if( !p_md )
327     {
328         p_mi->p_md = NULL;
329         vlc_mutex_unlock( &p_mi->object_lock );
330         return; /* It is ok to pass a NULL md */
331     }
332
333     p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
334     
335     /* The policy here is to ignore that we were created using a different
336      * libvlc_instance, because we don't really care */
337     p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
338
339     vlc_mutex_unlock( &p_mi->object_lock );
340 }
341
342 /**************************************************************************
343  * Get the Media descriptor associated with the instance
344  **************************************************************************/
345 libvlc_media_descriptor_t *
346 libvlc_media_instance_get_media_descriptor(
347                             libvlc_media_instance_t *p_mi,
348                             libvlc_exception_t *p_e )
349 {
350     (void)p_e;
351
352     if( !p_mi->p_md )
353         return NULL;
354
355     return libvlc_media_descriptor_duplicate( p_mi->p_md );
356 }
357
358 /**************************************************************************
359  * Get the event Manager
360  **************************************************************************/
361 libvlc_event_manager_t *
362 libvlc_media_instance_event_manager(
363                             libvlc_media_instance_t *p_mi,
364                             libvlc_exception_t *p_e )
365 {
366     (void)p_e;
367
368     if( !p_mi->p_md )
369         return NULL;
370
371     return p_mi->p_event_manager;
372 }
373
374 /**************************************************************************
375  * Play
376  **************************************************************************/
377 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
378                                  libvlc_exception_t *p_e )
379 {
380     input_thread_t * p_input_thread;
381
382     if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) ) 
383     {
384         /* A thread alread exists, send it a play message */        
385         vlc_value_t val;
386         val.i_int = PLAYING_S;
387
388         input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, PLAYING_S );
389         vlc_object_release( p_input_thread );
390         return;
391     }
392
393     vlc_mutex_lock( &p_mi->object_lock );
394     
395     if( !p_mi->p_md )
396     {
397         libvlc_exception_raise( p_e, "no associated media descriptor" );
398         vlc_mutex_unlock( &p_mi->object_lock );
399         return;
400     }
401
402     p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
403                                          p_mi->p_md->p_input_item );
404     p_mi->i_input_id = p_input_thread->i_object_id;
405
406     var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
407
408     /* will be released in media_instance_release() */
409     vlc_object_yield( p_input_thread );
410
411     vlc_mutex_unlock( &p_mi->object_lock );
412 }
413
414 /**************************************************************************
415  * Pause
416  **************************************************************************/
417 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
418                                   libvlc_exception_t *p_e )
419 {
420     input_thread_t * p_input_thread;
421     vlc_value_t val;
422     val.i_int = PAUSE_S;
423
424     p_input_thread = libvlc_get_input_thread( p_mi, p_e );
425
426     if( !p_input_thread )
427         return;
428
429     input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, val );
430     vlc_object_release( p_input_thread );
431 }
432
433 /**************************************************************************
434  * Stop
435  **************************************************************************/
436 void libvlc_media_instance_stop( libvlc_media_instance_t *p_mi,
437                                  libvlc_exception_t *p_e )
438 {
439     libvlc_exception_raise( p_mi, "Not implemented" );
440 }
441
442 /**************************************************************************
443  * Getters for stream information
444  **************************************************************************/
445 vlc_int64_t libvlc_media_instance_get_length(
446                              libvlc_media_instance_t *p_mi,
447                              libvlc_exception_t *p_e )
448 {
449     input_thread_t *p_input_thread;
450     vlc_value_t val;
451
452     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
453     if( !p_input_thread )
454         return -1;
455
456     var_Get( p_input_thread, "length", &val );
457     vlc_object_release( p_input_thread );
458
459     return (val.i_time+500LL)/1000LL;
460 }
461
462 vlc_int64_t libvlc_media_instance_get_time(
463                                    libvlc_media_instance_t *p_mi,
464                                    libvlc_exception_t *p_e )
465 {
466     input_thread_t *p_input_thread;
467     vlc_value_t val;
468
469     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
470     if( !p_input_thread )
471         return -1;
472
473     var_Get( p_input_thread , "time", &val );
474     vlc_object_release( p_input_thread );
475     return (val.i_time+500LL)/1000LL;
476 }
477
478 void libvlc_media_instance_set_time(
479                                  libvlc_media_instance_t *p_mi,
480                                  vlc_int64_t time,
481                                  libvlc_exception_t *p_e )
482 {
483     input_thread_t *p_input_thread;
484     vlc_value_t value;
485
486     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
487     if( !p_input_thread )
488         return;
489
490     value.i_time = time*1000LL;
491     var_Set( p_input_thread, "time", value );
492     vlc_object_release( p_input_thread );
493 }
494
495 void libvlc_media_instance_set_position(
496                                 libvlc_media_instance_t *p_mi,
497                                 float position,
498                                 libvlc_exception_t *p_e ) 
499 {
500     input_thread_t *p_input_thread;
501     vlc_value_t val;
502     val.f_float = position;
503
504     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
505     if( !p_input_thread )
506         return;
507
508     var_Set( p_input_thread, "position", val );
509     vlc_object_release( p_input_thread );
510 }
511
512 float libvlc_media_instance_get_position(
513                                  libvlc_media_instance_t *p_mi,
514                                  libvlc_exception_t *p_e )
515 {
516     input_thread_t *p_input_thread;
517     vlc_value_t val;
518
519     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
520     if( !p_input_thread )
521         return -1.0;
522
523     var_Get( p_input_thread, "position", &val );
524     vlc_object_release( p_input_thread );
525
526     return val.f_float;
527 }
528
529 float libvlc_media_instance_get_fps(
530                                  libvlc_media_instance_t *p_mi,
531                                  libvlc_exception_t *p_e) 
532 {
533     double f_fps = 0.0;
534     input_thread_t *p_input_thread;
535
536     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
537     if( !p_input_thread )
538         return 0.0;
539
540     if( (NULL == p_input_thread->p->input.p_demux)
541         || demux2_Control( p_input_thread->p->input.p_demux, DEMUX_GET_FPS, &f_fps )
542         || f_fps < 0.1 )
543     {
544         vlc_object_release( p_input_thread );
545         return 0.0;
546     }
547     else
548     {
549         vlc_object_release( p_input_thread );
550         return( f_fps );
551     }
552 }
553
554 vlc_bool_t libvlc_media_instance_will_play(
555                                  libvlc_media_instance_t *p_mi,
556                                  libvlc_exception_t *p_e) 
557 {
558     input_thread_t *p_input_thread =
559                             libvlc_get_input_thread ( p_mi, p_e);
560     if ( !p_input_thread )
561         return VLC_FALSE;
562
563     if ( !p_input_thread->b_die && !p_input_thread->b_dead ) 
564     {
565         vlc_object_release( p_input_thread );
566         return VLC_TRUE;
567     }
568     vlc_object_release( p_input_thread );
569     return VLC_FALSE;
570 }
571
572 void libvlc_media_instance_set_rate(
573                                  libvlc_media_instance_t *p_mi,
574                                  float rate,
575                                  libvlc_exception_t *p_e ) 
576 {
577     input_thread_t *p_input_thread;
578     vlc_value_t val;
579
580     if( rate <= 0 )
581         RAISEVOID( "Rate value is invalid" );
582
583     val.i_int = 1000.0f/rate;
584
585     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
586     if ( !p_input_thread )
587         return;
588
589     var_Set( p_input_thread, "rate", val );
590     vlc_object_release( p_input_thread );
591 }
592
593 float libvlc_media_instance_get_rate(
594                                  libvlc_media_instance_t *p_mi,
595                                  libvlc_exception_t *p_e )
596 {
597     input_thread_t *p_input_thread;
598     vlc_value_t val;
599
600     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
601     if ( !p_input_thread )
602         return -1.0;
603
604     var_Get( p_input_thread, "rate", &val );
605     vlc_object_release( p_input_thread );
606
607     return (float)1000.0f/val.i_int;
608 }
609
610 int libvlc_media_instance_get_state(
611                                  libvlc_media_instance_t *p_mi,
612                                  libvlc_exception_t *p_e )
613 {
614     input_thread_t *p_input_thread;
615     vlc_value_t val;
616
617     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
618     if ( !p_input_thread )
619         return 0;
620
621     var_Get( p_input_thread, "state", &val );
622     vlc_object_release( p_input_thread );
623
624     return val.i_int;
625 }
626