media_instance.c 11.9 KB
Newer Older
Clément Stenac's avatar
Clément Stenac committed
1
/*****************************************************************************
2
 * media_instance.c: Libvlc API Media Instance management functions
Clément Stenac's avatar
Clément Stenac committed
3 4 5 6
 *****************************************************************************
 * Copyright (C) 2005 the VideoLAN team
 * $Id$
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
Clément Stenac's avatar
Clément Stenac committed
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Clément Stenac's avatar
Clément Stenac committed
22 23
 *****************************************************************************/

Clément Stenac's avatar
Clément Stenac committed
24
#include "libvlc_internal.h"
Clément Stenac's avatar
Clément Stenac committed
25
#include <vlc/libvlc.h>
Clément Stenac's avatar
Clément Stenac committed
26 27
#include <vlc_demux.h>
#include <vlc_input.h>
Clément Stenac's avatar
Clément Stenac committed
28
#include "input/input_internal.h"
Clément Stenac's avatar
Clément Stenac committed
29

30 31
/*
 * Retrieve the input thread. Be sure to release the object
32
 * once you are done with it. (libvlc Internal)
33
 */
34
input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
35
                                         libvlc_exception_t *p_e ) 
Clément Stenac's avatar
Clément Stenac committed
36 37 38
{
    input_thread_t *p_input_thread;

39
    if( !p_mi || p_mi->i_input_id == -1 )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
40
        RAISENULL( "Input is NULL" );
Clément Stenac's avatar
Clément Stenac committed
41 42

    p_input_thread = (input_thread_t*)vlc_object_get(
43 44
                                             p_mi->p_libvlc_instance->p_libvlc_int,
                                             p_mi->i_input_id );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
45 46
    if( !p_input_thread )
        RAISENULL( "Input does not exist" );
47 48 49 50

    return p_input_thread;
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/**************************************************************************
 * Create a Media Instance object
 **************************************************************************/
libvlc_media_instance_t *
libvlc_media_instance_new( libvlc_media_descriptor_t *p_md )
{
    libvlc_media_instance_t * p_mi;

    if( !p_md )
        return NULL;

    p_mi = malloc( sizeof(libvlc_media_instance_t) );
    p_mi->p_md = libvlc_media_descriptor_duplicate( p_md );
    p_mi->p_libvlc_instance = p_mi->p_md->p_libvlc_instance;
    p_mi->i_input_id = -1;

    return p_mi;
}

/**************************************************************************
 * Create a new media instance object from an input_thread (Libvlc Internal)
 **************************************************************************/
libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
                                   struct libvlc_instance_t *p_libvlc_instance,
75 76
                                   input_thread_t *p_input,
                                   libvlc_exception_t *p_e )
77 78 79 80 81 82
{
    libvlc_media_instance_t * p_mi;

    p_mi = malloc( sizeof(libvlc_media_instance_t) );
    p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
                    p_libvlc_instance,
83 84 85 86 87 88 89 90
                    p_input->p->input.p_item, p_e );

    if( libvlc_exception_raised( p_e ) )
    {
        free( p_mi );
        return NULL;
    }

91 92 93
    p_mi->p_libvlc_instance = p_libvlc_instance;
    p_mi->i_input_id = p_input->i_object_id;

94
    /* will be released in media_instance_release() */
95
    vlc_object_yield( p_input );
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    return p_mi;
}

/**************************************************************************
 * Destroy a Media Instance object
 **************************************************************************/
void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
{
    input_thread_t *p_input_thread;
    libvlc_exception_t p_e;

    /* XXX: locking */
    libvlc_exception_init( &p_e );

    if( !p_mi )
        return;

    p_input_thread = libvlc_get_input_thread( p_mi, &p_e );

    if( libvlc_exception_raised( &p_e ) )
        return; /* no need to worry about no input thread */
    
    input_DestroyThread( p_input_thread );

    libvlc_media_descriptor_destroy( p_mi->p_md );

    free( p_mi );
}

/**************************************************************************
127
 * Release a Media Instance object
128
 **************************************************************************/
129
void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
130
{
131 132 133 134 135 136
    input_thread_t *p_input_thread;
    libvlc_exception_t p_e;

    /* XXX: locking */
    libvlc_exception_init( &p_e );

137 138 139
    if( !p_mi )
        return;

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    p_input_thread = libvlc_get_input_thread( p_mi, &p_e );

    if( !libvlc_exception_raised( &p_e ) )
    {
        /* release for previous libvlc_get_input_thread */
        vlc_object_release( p_input_thread );

        /* release for initial p_input_thread yield (see _new()) */
        vlc_object_release( p_input_thread );

        /* No one is tracking this input_thread appart us. Destroy it */
        if( p_input_thread->i_refcount <= 0 )
            input_DestroyThread( p_input_thread );
        /* btw, we still have an XXX locking here */
    }

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    libvlc_media_descriptor_destroy( p_mi->p_md );

    free( p_mi );
}

/**************************************************************************
 * Play
 **************************************************************************/
void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
                                 libvlc_exception_t *p_e )
{
    input_thread_t * p_input_thread;

    if( p_mi->i_input_id != -1) 
    {
        vlc_value_t val;
        val.i_int = PLAYING_S;

        /* A thread alread exists, send it a play message */
        p_input_thread = libvlc_get_input_thread( p_mi, p_e );

        if( libvlc_exception_raised( p_e ) )
            return;

        input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, PLAYING_S );
181
        vlc_object_release( p_input_thread );
182 183 184 185 186 187
        return;
    }

    p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
                                         p_mi->p_md->p_input_item );
    p_mi->i_input_id = p_input_thread->i_object_id;
188 189 190

    /* will be released in media_instance_release() */
    vlc_object_yield( p_input_thread );
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
}

/**************************************************************************
 * Pause
 **************************************************************************/
void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
                                  libvlc_exception_t *p_e )
{
    input_thread_t * p_input_thread;
    vlc_value_t val;
    val.i_int = PAUSE_S;

    p_input_thread = libvlc_get_input_thread( p_mi, p_e );

    if( libvlc_exception_raised( p_e ) )
        return;

    input_Control( p_input_thread, INPUT_CONTROL_SET_STATE, val );
209
    vlc_object_release( p_input_thread );
210
}
211 212 213 214

/**************************************************************************
 * Getters for stream information
 **************************************************************************/
215 216
vlc_int64_t libvlc_media_instance_get_length(
                             libvlc_media_instance_t *p_mi,
217
                             libvlc_exception_t *p_e )
218 219 220 221
{
    input_thread_t *p_input_thread;
    vlc_value_t val;

222
    p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
223
    if( !p_input_thread )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
224 225
        return -1;

Clément Stenac's avatar
Clément Stenac committed
226 227 228
    var_Get( p_input_thread, "length", &val );
    vlc_object_release( p_input_thread );

229
    return (val.i_time+500LL)/1000LL;
Clément Stenac's avatar
Clément Stenac committed
230 231
}

232 233
vlc_int64_t libvlc_media_instance_get_time(
                                   libvlc_media_instance_t *p_mi,
234
                                   libvlc_exception_t *p_e )
Clément Stenac's avatar
Clément Stenac committed
235 236 237 238
{
    input_thread_t *p_input_thread;
    vlc_value_t val;

239
    p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
240
    if( !p_input_thread )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
241
        return -1;
242

Clément Stenac's avatar
Clément Stenac committed
243 244
    var_Get( p_input_thread , "time", &val );
    vlc_object_release( p_input_thread );
245
    return (val.i_time+500LL)/1000LL;
Clément Stenac's avatar
Clément Stenac committed
246 247
}

248 249 250 251
void libvlc_media_instance_set_time(
                                 libvlc_media_instance_t *p_mi,
                                 vlc_int64_t time,
                                 libvlc_exception_t *p_e )
252 253 254 255
{
    input_thread_t *p_input_thread;
    vlc_value_t value;

256
    p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
257
    if( !p_input_thread )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
258 259
        return;

260
    value.i_time = time*1000LL;
261 262 263 264
    var_Set( p_input_thread, "time", value );
    vlc_object_release( p_input_thread );
}

265 266 267
void libvlc_media_instance_set_position(
                                libvlc_media_instance_t *p_mi,
                                float position,
268
                                libvlc_exception_t *p_e ) 
269 270 271 272
{
    input_thread_t *p_input_thread;
    vlc_value_t val;
    val.f_float = position;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
273

274
    p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
275
    if( !p_input_thread )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
276
        return;
277 278 279 280 281

    var_Set( p_input_thread, "position", val );
    vlc_object_release( p_input_thread );
}

282 283
float libvlc_media_instance_get_position(
                                 libvlc_media_instance_t *p_mi,
284
                                 libvlc_exception_t *p_e )
Clément Stenac's avatar
Clément Stenac committed
285 286 287 288
{
    input_thread_t *p_input_thread;
    vlc_value_t val;

289
    p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
290
    if( !p_input_thread )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
291
        return -1.0;
292

Clément Stenac's avatar
Clément Stenac committed
293 294 295 296 297
    var_Get( p_input_thread, "position", &val );
    vlc_object_release( p_input_thread );

    return val.f_float;
}
Filippo Carone's avatar
Filippo Carone committed
298

299 300 301
float libvlc_media_instance_get_fps(
                                 libvlc_media_instance_t *p_mi,
                                 libvlc_exception_t *p_e) 
302
{
303
    double f_fps = 0.0;
304 305
    input_thread_t *p_input_thread;

306
    p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
307
    if( !p_input_thread )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
308
        return 0.0;
309

310 311
    if( (NULL == p_input_thread->p->input.p_demux)
        || demux2_Control( p_input_thread->p->input.p_demux, DEMUX_GET_FPS, &f_fps )
Clément Stenac's avatar
Clément Stenac committed
312
        || f_fps < 0.1 )
Filippo Carone's avatar
Filippo Carone committed
313 314
    {
        vlc_object_release( p_input_thread );
315
        return 0.0;
Filippo Carone's avatar
Filippo Carone committed
316
    }
317
    else
Filippo Carone's avatar
Filippo Carone committed
318 319
    {
        vlc_object_release( p_input_thread );
320
        return( f_fps );
Filippo Carone's avatar
Filippo Carone committed
321
    }
322 323
}

324 325 326
vlc_bool_t libvlc_media_instance_will_play(
                                 libvlc_media_instance_t *p_mi,
                                 libvlc_exception_t *p_e) 
Filippo Carone's avatar
Filippo Carone committed
327
{
328
    input_thread_t *p_input_thread =
329
                            libvlc_get_input_thread ( p_mi, p_e);
330
    if ( !p_input_thread )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
331
        return VLC_FALSE;
Filippo Carone's avatar
Filippo Carone committed
332

333
    if ( !p_input_thread->b_die && !p_input_thread->b_dead ) 
Filippo Carone's avatar
Filippo Carone committed
334
    {
335 336
        vlc_object_release( p_input_thread );
        return VLC_TRUE;
Filippo Carone's avatar
Filippo Carone committed
337
    }
338 339
    vlc_object_release( p_input_thread );
    return VLC_FALSE;
Filippo Carone's avatar
Filippo Carone committed
340
}
341

342 343 344 345
void libvlc_media_instance_set_rate(
                                 libvlc_media_instance_t *p_mi,
                                 float rate,
                                 libvlc_exception_t *p_e ) 
346 347 348
{
    input_thread_t *p_input_thread;
    vlc_value_t val;
349 350 351 352 353

    if( rate <= 0 )
        RAISEVOID( "Rate value is invalid" );

    val.i_int = 1000.0f/rate;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
354

355
    p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
356
    if ( !p_input_thread )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
357
        return;
358 359 360 361 362

    var_Set( p_input_thread, "rate", val );
    vlc_object_release( p_input_thread );
}

363 364
float libvlc_media_instance_get_rate(
                                 libvlc_media_instance_t *p_mi,
365 366 367 368 369
                                 libvlc_exception_t *p_e )
{
    input_thread_t *p_input_thread;
    vlc_value_t val;

370
    p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
371
    if ( !p_input_thread )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
372
        return -1.0;
373 374 375 376

    var_Get( p_input_thread, "rate", &val );
    vlc_object_release( p_input_thread );

377
    return (float)1000.0f/val.i_int;
378 379
}

380 381
int libvlc_media_instance_get_state(
                                 libvlc_media_instance_t *p_mi,
382 383 384 385 386
                                 libvlc_exception_t *p_e )
{
    input_thread_t *p_input_thread;
    vlc_value_t val;

387
    p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
388
    if ( !p_input_thread )
389
        return 0;
390 391 392 393 394 395 396

    var_Get( p_input_thread, "state", &val );
    vlc_object_release( p_input_thread );

    return val.i_int;
}