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 * Retrieve the input thread. Be sure to release the object
32 * once you are done with it. (libvlc Internal)
34 input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
35 libvlc_exception_t *p_e )
37 input_thread_t *p_input_thread;
39 if( !p_mi || p_mi->i_input_id == -1 )
40 RAISENULL( "Input is NULL" );
42 p_input_thread = (input_thread_t*)vlc_object_get(
43 p_mi->p_libvlc_instance->p_libvlc_int,
46 RAISENULL( "Input does not exist" );
48 return p_input_thread;
51 /**************************************************************************
52 * Create a Media Instance object
53 **************************************************************************/
54 libvlc_media_instance_t *
55 libvlc_media_instance_new( libvlc_media_descriptor_t *p_md )
57 libvlc_media_instance_t * p_mi;
62 p_mi = malloc( sizeof(libvlc_media_instance_t) );
63 p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
64 p_mi->p_libvlc_instance = p_mi->p_md->p_libvlc_instance;
65 p_mi->i_input_id = -1;
70 /**************************************************************************
71 * Create a new media instance object from an input_thread (Libvlc Internal)
72 **************************************************************************/
73 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
74 struct libvlc_instance_t *p_libvlc_instance,
75 input_thread_t *p_input )
77 libvlc_media_instance_t * p_mi;
79 p_mi = malloc( sizeof(libvlc_media_instance_t) );
80 p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
82 p_input->p->input.p_item );
83 p_mi->p_libvlc_instance = p_libvlc_instance;
84 p_mi->i_input_id = p_input->i_object_id;
89 /**************************************************************************
90 * Destroy a Media Instance object
91 **************************************************************************/
92 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
94 input_thread_t *p_input_thread;
95 libvlc_exception_t p_e;
98 libvlc_exception_init( &p_e );
103 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
105 if( libvlc_exception_raised( &p_e ) )
106 return; /* no need to worry about no input thread */
108 input_DestroyThread( p_input_thread );
110 libvlc_media_descriptor_destroy( p_mi->p_md );
115 /**************************************************************************
116 * Free a Media Instance object (libvlc internal)
117 **************************************************************************/
118 void libvlc_media_instance_destroy_and_detach( libvlc_media_instance_t *p_mi )
123 libvlc_media_descriptor_destroy( p_mi->p_md );
128 /**************************************************************************
130 **************************************************************************/
131 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
132 libvlc_exception_t *p_e )
134 input_thread_t * p_input_thread;
136 if( p_mi->i_input_id != -1)
139 val.i_int = PLAYING_S;
141 /* A thread alread exists, send it a play message */
142 p_input_thread = libvlc_get_input_thread( p_mi, p_e );
144 if( libvlc_exception_raised( p_e ) )
147 input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, PLAYING_S );
151 p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
152 p_mi->p_md->p_input_item );
153 p_mi->i_input_id = p_input_thread->i_object_id;
156 /**************************************************************************
158 **************************************************************************/
159 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
160 libvlc_exception_t *p_e )
162 input_thread_t * p_input_thread;
166 p_input_thread = libvlc_get_input_thread( p_mi, p_e );
168 if( libvlc_exception_raised( p_e ) )
171 input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, val );
174 /**************************************************************************
175 * Getters for stream information
176 **************************************************************************/
177 vlc_int64_t libvlc_media_instance_get_length(
178 libvlc_media_instance_t *p_mi,
179 libvlc_exception_t *p_e )
181 input_thread_t *p_input_thread;
184 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
185 if( !p_input_thread )
188 var_Get( p_input_thread, "length", &val );
189 vlc_object_release( p_input_thread );
191 return (val.i_time+500LL)/1000LL;
194 vlc_int64_t libvlc_media_instance_get_time(
195 libvlc_media_instance_t *p_mi,
196 libvlc_exception_t *p_e )
198 input_thread_t *p_input_thread;
201 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
202 if( !p_input_thread )
205 var_Get( p_input_thread , "time", &val );
206 vlc_object_release( p_input_thread );
207 return (val.i_time+500LL)/1000LL;
210 void libvlc_media_instance_set_time(
211 libvlc_media_instance_t *p_mi,
213 libvlc_exception_t *p_e )
215 input_thread_t *p_input_thread;
218 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
219 if( !p_input_thread )
222 value.i_time = time*1000LL;
223 var_Set( p_input_thread, "time", value );
224 vlc_object_release( p_input_thread );
227 void libvlc_media_instance_set_position(
228 libvlc_media_instance_t *p_mi,
230 libvlc_exception_t *p_e )
232 input_thread_t *p_input_thread;
234 val.f_float = position;
236 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
237 if( !p_input_thread )
240 var_Set( p_input_thread, "position", val );
241 vlc_object_release( p_input_thread );
244 float libvlc_media_instance_get_position(
245 libvlc_media_instance_t *p_mi,
246 libvlc_exception_t *p_e )
248 input_thread_t *p_input_thread;
251 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
252 if( !p_input_thread )
255 var_Get( p_input_thread, "position", &val );
256 vlc_object_release( p_input_thread );
261 float libvlc_media_instance_get_fps(
262 libvlc_media_instance_t *p_mi,
263 libvlc_exception_t *p_e)
266 input_thread_t *p_input_thread;
268 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
269 if( !p_input_thread )
272 if( (NULL == p_input_thread->p->input.p_demux)
273 || demux2_Control( p_input_thread->p->input.p_demux, DEMUX_GET_FPS, &f_fps )
276 vlc_object_release( p_input_thread );
281 vlc_object_release( p_input_thread );
286 vlc_bool_t libvlc_media_instance_will_play(
287 libvlc_media_instance_t *p_mi,
288 libvlc_exception_t *p_e)
290 input_thread_t *p_input_thread =
291 libvlc_get_input_thread ( p_mi, p_e);
292 if ( !p_input_thread )
295 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
297 vlc_object_release( p_input_thread );
300 vlc_object_release( p_input_thread );
304 void libvlc_media_instance_set_rate(
305 libvlc_media_instance_t *p_mi,
307 libvlc_exception_t *p_e )
309 input_thread_t *p_input_thread;
313 RAISEVOID( "Rate value is invalid" );
315 val.i_int = 1000.0f/rate;
317 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
318 if ( !p_input_thread )
321 var_Set( p_input_thread, "rate", val );
322 vlc_object_release( p_input_thread );
325 float libvlc_media_instance_get_rate(
326 libvlc_media_instance_t *p_mi,
327 libvlc_exception_t *p_e )
329 input_thread_t *p_input_thread;
332 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
333 if ( !p_input_thread )
336 var_Get( p_input_thread, "rate", &val );
337 vlc_object_release( p_input_thread );
339 return (float)1000.0f/val.i_int;
342 int libvlc_media_instance_get_state(
343 libvlc_media_instance_t *p_mi,
344 libvlc_exception_t *p_e )
346 input_thread_t *p_input_thread;
349 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
350 if ( !p_input_thread )
353 var_Get( p_input_thread, "state", &val );
354 vlc_object_release( p_input_thread );