]> git.sesse.net Git - vlc/blob - src/control/media_instance.c
control/media_instance.c: Code factorization.
[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  * Create a Media Instance object
106  **************************************************************************/
107 libvlc_media_instance_t *
108 libvlc_media_instance_new( libvlc_instance_t * p_libvlc_instance,
109                            libvlc_exception_t * p_e )
110 {
111     libvlc_media_instance_t * p_mi;
112
113     if( !p_libvlc_instance )
114     {
115         libvlc_exception_raise( p_e, "invalid libvlc instance" );
116         return NULL;
117     }
118
119     p_mi = malloc( sizeof(libvlc_media_instance_t) );
120     p_mi->p_md = NULL;
121     p_mi->p_libvlc_instance = p_libvlc_instance;
122     p_mi->i_input_id = -1;
123     /* refcount strategy:
124      * - All items created by _new start with a refcount set to 1
125      * - Accessor _release decrease the refcount by 1, if after that
126      *   operation the refcount is 0, the object is destroyed.
127      * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
128     p_mi->i_refcount = 1;
129     /* object_lock strategy:
130      * - No lock held in constructor
131      * - Lock when accessing all variable this lock is held
132      * - Lock when attempting to destroy the object the lock is also held */
133     vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
134                     &p_mi->object_lock );
135
136     return p_mi;
137 }
138
139 /**************************************************************************
140  * Create a Media Instance object with a media descriptor
141  **************************************************************************/
142 libvlc_media_instance_t *
143 libvlc_media_instance_new_from_media_descriptor(
144                                     libvlc_media_descriptor_t * p_md,
145                                     libvlc_exception_t *p_e )
146 {
147     libvlc_media_instance_t * p_mi;
148     p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
149
150     if( !p_mi )
151         return NULL;
152
153     p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
154
155     return p_mi;
156 }
157
158 /**************************************************************************
159  * Create a new media instance object from an input_thread (Libvlc Internal)
160  **************************************************************************/
161 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
162                                    struct libvlc_instance_t *p_libvlc_instance,
163                                    input_thread_t *p_input,
164                                    libvlc_exception_t *p_e )
165 {
166     libvlc_media_instance_t * p_mi;
167
168     if( !p_input )
169     {
170         libvlc_exception_raise( p_e, "invalid input thread" );
171         return NULL;
172     }
173
174     p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
175
176     if( !p_mi )
177         return NULL;
178
179     p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
180                     p_libvlc_instance,
181                     p_input->p->input.p_item, p_e );
182
183     if( !p_mi->p_md )
184     {
185         libvlc_media_instance_destroy( p_mi );
186         return NULL;
187     }
188
189     p_mi->i_input_id = p_input->i_object_id;
190     
191     /* will be released in media_instance_release() */
192     vlc_object_yield( p_input );
193
194     /* XXX: Hack as the playlist doesn't yield the input thread we retain
195      * the input for the playlist. (see corresponding hack in _release) */
196     vlc_object_yield( p_input );
197
198     return p_mi;
199 }
200
201 /**************************************************************************
202  * Destroy a Media Instance object (libvlc internal)
203  *
204  * Warning: No lock held here, but hey, this is internal.
205  **************************************************************************/
206 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
207 {
208     input_thread_t *p_input_thread;
209     libvlc_exception_t p_e;
210
211     libvlc_exception_init( &p_e );
212
213     if( !p_mi )
214         return;
215
216     p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
217
218     if( libvlc_exception_raised( &p_e ) )
219     {
220         free( p_mi );
221         return; /* no need to worry about no input thread */
222     }
223
224     input_DestroyThread( p_input_thread );
225
226     libvlc_media_descriptor_destroy( p_mi->p_md );
227
228     free( p_mi );
229 }
230
231 /**************************************************************************
232  * Release a Media Instance object
233  **************************************************************************/
234 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
235 {
236     if( !p_mi )
237         return;
238
239     vlc_mutex_lock( &p_mi->object_lock );
240     
241     p_mi->i_refcount--;
242
243     /* We hold the mutex, as a waiter to make sure pending operations
244      * are finished. We can't hold it longer as the get_input_thread
245      * function holds a lock.  */
246
247     vlc_mutex_unlock( &p_mi->object_lock );
248     
249     if( p_mi->i_refcount > 0 )
250         return;
251
252     release_input_thread( p_mi );
253
254     libvlc_media_descriptor_destroy( p_mi->p_md );
255
256     free( p_mi );
257 }
258
259 /**************************************************************************
260  * Set the Media descriptor associated with the instance
261  **************************************************************************/
262 void libvlc_media_instance_set_media_descriptor(
263                             libvlc_media_instance_t *p_mi,
264                             libvlc_media_descriptor_t *p_md,
265                             libvlc_exception_t *p_e )
266 {
267     (void)p_e;
268
269     if( !p_mi )
270         return;
271
272     vlc_mutex_lock( &p_mi->object_lock );
273     
274     release_input_thread( p_mi );
275
276     libvlc_media_descriptor_destroy( p_mi->p_md );
277
278     if( !p_md )
279     {
280         p_mi->p_md = NULL;
281         vlc_mutex_unlock( &p_mi->object_lock );
282         return; /* It is ok to pass a NULL md */
283     }
284
285     p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
286     
287     /* The policy here is to ignore that we were created using a different
288      * libvlc_instance, because we don't really care */
289     p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
290
291     vlc_mutex_unlock( &p_mi->object_lock );
292 }
293
294 /**************************************************************************
295  * Set the Media descriptor associated with the instance
296  **************************************************************************/
297 libvlc_media_descriptor_t *
298 libvlc_media_instance_get_media_descriptor(
299                             libvlc_media_instance_t *p_mi,
300                             libvlc_exception_t *p_e )
301 {
302     (void)p_e;
303
304     if( !p_mi->p_md )
305         return NULL;
306
307     return libvlc_media_descriptor_duplicate( p_mi->p_md );
308 }
309
310 /**************************************************************************
311  * Play
312  **************************************************************************/
313 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
314                                  libvlc_exception_t *p_e )
315 {
316     input_thread_t * p_input_thread;
317
318     if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) ) 
319     {
320         /* A thread alread exists, send it a play message */        
321         vlc_value_t val;
322         val.i_int = PLAYING_S;
323
324         input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, PLAYING_S );
325         vlc_object_release( p_input_thread );
326         return;
327     }
328
329     vlc_mutex_lock( &p_mi->object_lock );
330     
331     if( !p_mi->p_md )
332     {
333         libvlc_exception_raise( p_e, "no associated media descriptor" );
334         vlc_mutex_unlock( &p_mi->object_lock );
335         return;
336     }
337
338     p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
339                                          p_mi->p_md->p_input_item );
340     p_mi->i_input_id = p_input_thread->i_object_id;
341
342     /* will be released in media_instance_release() */
343     vlc_object_yield( p_input_thread );
344
345     vlc_mutex_unlock( &p_mi->object_lock );
346 }
347
348 /**************************************************************************
349  * Pause
350  **************************************************************************/
351 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
352                                   libvlc_exception_t *p_e )
353 {
354     input_thread_t * p_input_thread;
355     vlc_value_t val;
356     val.i_int = PAUSE_S;
357
358     p_input_thread = libvlc_get_input_thread( p_mi, p_e );
359
360     if( !p_input_thread )
361         return;
362
363     input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, val );
364     vlc_object_release( p_input_thread );
365 }
366
367 /**************************************************************************
368  * Getters for stream information
369  **************************************************************************/
370 vlc_int64_t libvlc_media_instance_get_length(
371                              libvlc_media_instance_t *p_mi,
372                              libvlc_exception_t *p_e )
373 {
374     input_thread_t *p_input_thread;
375     vlc_value_t val;
376
377     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
378     if( !p_input_thread )
379         return -1;
380
381     var_Get( p_input_thread, "length", &val );
382     vlc_object_release( p_input_thread );
383
384     return (val.i_time+500LL)/1000LL;
385 }
386
387 vlc_int64_t libvlc_media_instance_get_time(
388                                    libvlc_media_instance_t *p_mi,
389                                    libvlc_exception_t *p_e )
390 {
391     input_thread_t *p_input_thread;
392     vlc_value_t val;
393
394     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
395     if( !p_input_thread )
396         return -1;
397
398     var_Get( p_input_thread , "time", &val );
399     vlc_object_release( p_input_thread );
400     return (val.i_time+500LL)/1000LL;
401 }
402
403 void libvlc_media_instance_set_time(
404                                  libvlc_media_instance_t *p_mi,
405                                  vlc_int64_t time,
406                                  libvlc_exception_t *p_e )
407 {
408     input_thread_t *p_input_thread;
409     vlc_value_t value;
410
411     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
412     if( !p_input_thread )
413         return;
414
415     value.i_time = time*1000LL;
416     var_Set( p_input_thread, "time", value );
417     vlc_object_release( p_input_thread );
418 }
419
420 void libvlc_media_instance_set_position(
421                                 libvlc_media_instance_t *p_mi,
422                                 float position,
423                                 libvlc_exception_t *p_e ) 
424 {
425     input_thread_t *p_input_thread;
426     vlc_value_t val;
427     val.f_float = position;
428
429     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
430     if( !p_input_thread )
431         return;
432
433     var_Set( p_input_thread, "position", val );
434     vlc_object_release( p_input_thread );
435 }
436
437 float libvlc_media_instance_get_position(
438                                  libvlc_media_instance_t *p_mi,
439                                  libvlc_exception_t *p_e )
440 {
441     input_thread_t *p_input_thread;
442     vlc_value_t val;
443
444     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
445     if( !p_input_thread )
446         return -1.0;
447
448     var_Get( p_input_thread, "position", &val );
449     vlc_object_release( p_input_thread );
450
451     return val.f_float;
452 }
453
454 float libvlc_media_instance_get_fps(
455                                  libvlc_media_instance_t *p_mi,
456                                  libvlc_exception_t *p_e) 
457 {
458     double f_fps = 0.0;
459     input_thread_t *p_input_thread;
460
461     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
462     if( !p_input_thread )
463         return 0.0;
464
465     if( (NULL == p_input_thread->p->input.p_demux)
466         || demux2_Control( p_input_thread->p->input.p_demux, DEMUX_GET_FPS, &f_fps )
467         || f_fps < 0.1 )
468     {
469         vlc_object_release( p_input_thread );
470         return 0.0;
471     }
472     else
473     {
474         vlc_object_release( p_input_thread );
475         return( f_fps );
476     }
477 }
478
479 vlc_bool_t libvlc_media_instance_will_play(
480                                  libvlc_media_instance_t *p_mi,
481                                  libvlc_exception_t *p_e) 
482 {
483     input_thread_t *p_input_thread =
484                             libvlc_get_input_thread ( p_mi, p_e);
485     if ( !p_input_thread )
486         return VLC_FALSE;
487
488     if ( !p_input_thread->b_die && !p_input_thread->b_dead ) 
489     {
490         vlc_object_release( p_input_thread );
491         return VLC_TRUE;
492     }
493     vlc_object_release( p_input_thread );
494     return VLC_FALSE;
495 }
496
497 void libvlc_media_instance_set_rate(
498                                  libvlc_media_instance_t *p_mi,
499                                  float rate,
500                                  libvlc_exception_t *p_e ) 
501 {
502     input_thread_t *p_input_thread;
503     vlc_value_t val;
504
505     if( rate <= 0 )
506         RAISEVOID( "Rate value is invalid" );
507
508     val.i_int = 1000.0f/rate;
509
510     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
511     if ( !p_input_thread )
512         return;
513
514     var_Set( p_input_thread, "rate", val );
515     vlc_object_release( p_input_thread );
516 }
517
518 float libvlc_media_instance_get_rate(
519                                  libvlc_media_instance_t *p_mi,
520                                  libvlc_exception_t *p_e )
521 {
522     input_thread_t *p_input_thread;
523     vlc_value_t val;
524
525     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
526     if ( !p_input_thread )
527         return -1.0;
528
529     var_Get( p_input_thread, "rate", &val );
530     vlc_object_release( p_input_thread );
531
532     return (float)1000.0f/val.i_int;
533 }
534
535 int libvlc_media_instance_get_state(
536                                  libvlc_media_instance_t *p_mi,
537                                  libvlc_exception_t *p_e )
538 {
539     input_thread_t *p_input_thread;
540     vlc_value_t val;
541
542     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
543     if ( !p_input_thread )
544         return 0;
545
546     var_Get( p_input_thread, "state", &val );
547     vlc_object_release( p_input_thread );
548
549     return val.i_int;
550 }
551