media.c 20.1 KB
Newer Older
1
/*****************************************************************************
2
 * media.c: Libvlc API media descripor management
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
 *
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

24
#include "libvlc_internal.h"
25
#include "libvlc.h"
26 27 28 29
#include <vlc/libvlc.h>
#include <vlc_input.h>
#include <vlc_meta.h>

30 31 32
/* For the preparser */
#include <vlc_playlist.h>

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
static const vlc_meta_type_t libvlc_to_vlc_meta[] =
{
    [libvlc_meta_Title]        = vlc_meta_Title,
    [libvlc_meta_Artist]       = vlc_meta_Artist,
    [libvlc_meta_Genre]        = vlc_meta_Genre,
    [libvlc_meta_Copyright]    = vlc_meta_Copyright,
    [libvlc_meta_Album]        = vlc_meta_Album,
    [libvlc_meta_TrackNumber]  = vlc_meta_TrackNumber,
    [libvlc_meta_Description]  = vlc_meta_Description,
    [libvlc_meta_Rating]       = vlc_meta_Rating,
    [libvlc_meta_Date]         = vlc_meta_Date,
    [libvlc_meta_Setting]      = vlc_meta_Setting,
    [libvlc_meta_URL]          = vlc_meta_URL,
    [libvlc_meta_Language]     = vlc_meta_Language,
    [libvlc_meta_NowPlaying]   = vlc_meta_NowPlaying,
    [libvlc_meta_Publisher]    = vlc_meta_Publisher,
    [libvlc_meta_EncodedBy]    = vlc_meta_EncodedBy,
    [libvlc_meta_ArtworkURL]   = vlc_meta_ArtworkURL,
    [libvlc_meta_TrackID]      = vlc_meta_TrackID
};

static const libvlc_meta_t vlc_to_libvlc_meta[] =
{
    [vlc_meta_Title]        = libvlc_meta_Title,
    [vlc_meta_Artist]       = libvlc_meta_Artist,
    [vlc_meta_Genre]        = libvlc_meta_Genre,
    [vlc_meta_Copyright]    = libvlc_meta_Copyright,
    [vlc_meta_Album]        = libvlc_meta_Album,
    [vlc_meta_TrackNumber]  = libvlc_meta_TrackNumber,
    [vlc_meta_Description]  = libvlc_meta_Description,
    [vlc_meta_Rating]       = libvlc_meta_Rating,
    [vlc_meta_Date]         = libvlc_meta_Date,
    [vlc_meta_Setting]      = libvlc_meta_Setting,
    [vlc_meta_URL]          = libvlc_meta_URL,
    [vlc_meta_Language]     = libvlc_meta_Language,
    [vlc_meta_NowPlaying]   = libvlc_meta_NowPlaying,
    [vlc_meta_Publisher]    = libvlc_meta_Publisher,
    [vlc_meta_EncodedBy]    = libvlc_meta_EncodedBy,
    [vlc_meta_ArtworkURL]   = libvlc_meta_ArtworkURL,
    [vlc_meta_TrackID]      = libvlc_meta_TrackID
};

75 76 77 78
/**************************************************************************
 * input_item_subitem_added (Private) (vlc event Callback)
 **************************************************************************/
static void input_item_subitem_added( const vlc_event_t *p_event,
79
                                       void * user_data )
80
{
81 82
    libvlc_media_t * p_md = user_data;
    libvlc_media_t * p_md_child;
83 84
    libvlc_event_t event;

85
    p_md_child = libvlc_media_new_from_input_item(
86
                p_md->p_libvlc_instance,
87 88
                p_event->u.input_item_subitem_added.p_new_child, NULL );

89 90
    /* Add this to our media list */
    if( !p_md->p_subitems )
91
    {
92
        p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
93
        libvlc_media_list_set_media( p_md->p_subitems, p_md, NULL );
94
    }
95
    if( p_md->p_subitems )
96
    {
97
        libvlc_media_list_add_media( p_md->p_subitems, p_md_child, NULL );
98 99
    }

100
    /* Construct the event */
101
    event.type = libvlc_MediaSubItemAdded;
102
    event.u.media_subitem_added.new_child = p_md_child;
103 104 105

    /* Send the event */
    libvlc_event_send( p_md->p_event_manager, &event );
106
    libvlc_media_release( p_md_child );
107 108
}

109 110 111 112 113 114
/**************************************************************************
 * input_item_meta_changed (Private) (vlc event Callback)
 **************************************************************************/
static void input_item_meta_changed( const vlc_event_t *p_event,
                                     void * user_data )
{
115
    libvlc_media_t * p_md = user_data;
116 117 118
    libvlc_event_t event;

    /* Construct the event */
119
    event.type = libvlc_MediaMetaChanged;
120
    event.u.media_meta_changed.meta_type =
121 122 123 124 125 126
        vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];

    /* Send the event */
    libvlc_event_send( p_md->p_event_manager, &event );
}

127 128 129 130 131 132
/**************************************************************************
 * input_item_duration_changed (Private) (vlc event Callback)
 **************************************************************************/
static void input_item_duration_changed( const vlc_event_t *p_event,
                                         void * user_data )
{
133
    libvlc_media_t * p_md = user_data;
134 135 136
    libvlc_event_t event;

    /* Construct the event */
137
    event.type = libvlc_MediaDurationChanged;
138
    event.u.media_duration_changed.new_duration = 
139 140 141 142 143 144 145 146 147 148 149 150
        p_event->u.input_item_duration_changed.new_duration;

    /* Send the event */
    libvlc_event_send( p_md->p_event_manager, &event );
}

/**************************************************************************
 * input_item_preparsed_changed (Private) (vlc event Callback)
 **************************************************************************/
static void input_item_preparsed_changed( const vlc_event_t *p_event,
                                          void * user_data )
{
151
    libvlc_media_t * p_md = user_data;
152 153 154
    libvlc_event_t event;

    /* Construct the event */
155
    event.type = libvlc_MediaPreparsedChanged;
156
    event.u.media_preparsed_changed.new_status = 
157 158 159 160 161
        p_event->u.input_item_preparsed_changed.new_status;

    /* Send the event */
    libvlc_event_send( p_md->p_event_manager, &event );
}
162 163 164 165

/**************************************************************************
 * Install event handler (Private)
 **************************************************************************/
166
static void install_input_item_observer( libvlc_media_t *p_md )
167
{
168 169 170 171
    vlc_event_attach( &p_md->p_input_item->event_manager,
                      vlc_InputItemSubItemAdded,
                      input_item_subitem_added,
                      p_md );
172 173 174 175
    vlc_event_attach( &p_md->p_input_item->event_manager,
                      vlc_InputItemMetaChanged,
                      input_item_meta_changed,
                      p_md );
176 177 178 179 180 181 182 183
    vlc_event_attach( &p_md->p_input_item->event_manager,
                      vlc_InputItemDurationChanged,
                      input_item_duration_changed,
                      p_md );
    vlc_event_attach( &p_md->p_input_item->event_manager,
                      vlc_InputItemPreparsedChanged,
                      input_item_preparsed_changed,
                      p_md );
184 185 186 187 188
}

/**************************************************************************
 * Uninstall event handler (Private)
 **************************************************************************/
189
static void uninstall_input_item_observer( libvlc_media_t *p_md )
190
{
191 192 193 194
    vlc_event_detach( &p_md->p_input_item->event_manager,
                      vlc_InputItemSubItemAdded,
                      input_item_subitem_added,
                      p_md );
195 196 197 198
    vlc_event_detach( &p_md->p_input_item->event_manager,
                      vlc_InputItemMetaChanged,
                      input_item_meta_changed,
                      p_md );
199 200 201 202 203 204 205 206
    vlc_event_detach( &p_md->p_input_item->event_manager,
                      vlc_InputItemDurationChanged,
                      input_item_duration_changed,
                      p_md );
    vlc_event_detach( &p_md->p_input_item->event_manager,
                      vlc_InputItemPreparsedChanged,
                      input_item_preparsed_changed,
                      p_md );
207
}
208 209

/**************************************************************************
210
 * Preparse if not already done (Private)
211
 **************************************************************************/
212
static void preparse_if_needed( libvlc_media_t *p_md )
213 214
{
    /* XXX: need some locking here */
215
    if (!p_md->b_preparsed)
216
    {
217
        playlist_PreparseEnqueue(
218
                libvlc_priv (p_md->p_libvlc_instance->p_libvlc_int)->p_playlist,
219
                p_md->p_input_item, pl_Unlocked );
220
        p_md->b_preparsed = true;
221 222 223 224
    }
}

/**************************************************************************
225 226 227
 * Create a new media descriptor object from an input_item
 * (libvlc internal)
 * That's the generic constructor
228
 **************************************************************************/
229
libvlc_media_t * libvlc_media_new_from_input_item(
230
                                   libvlc_instance_t *p_instance,
231
                                   input_item_t *p_input_item,
232 233
                                   libvlc_exception_t *p_e )
{
234
    libvlc_media_t * p_md;
235 236

    if (!p_input_item)
237
    {
238
        libvlc_exception_raise( p_e, "No input item given" );
239 240
        return NULL;
    }
241

242
    p_md = malloc( sizeof(libvlc_media_t) );
243 244 245 246 247 248
    if( !p_md )
    {
        libvlc_exception_raise( p_e, "Not enough memory" );
        return NULL;
    }

249 250
    p_md->p_libvlc_instance = p_instance;
    p_md->p_input_item      = p_input_item;
251
    p_md->b_preparsed       = false;
252
    p_md->i_refcount        = 1;
253 254 255
    p_md->p_user_data       = NULL;

    p_md->state = libvlc_NothingSpecial;
256

257 258 259 260
    /* A media descriptor can be a playlist. When you open a playlist
     * It can give a bunch of item to read. */
    p_md->p_subitems        = NULL;

261
    p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
262
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
263
        libvlc_MediaMetaChanged, p_e );
264
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
265
        libvlc_MediaSubItemAdded, p_e );
266
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
267
        libvlc_MediaFreed, p_e );
268
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
269
        libvlc_MediaDurationChanged, p_e );
270
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
271
        libvlc_MediaStateChanged, p_e );
272

273
    vlc_gc_incref( p_md->p_input_item );
274

275 276
    install_input_item_observer( p_md );

277
    return p_md;
278 279
}

280
/**************************************************************************
281
 * Create a new media descriptor object
282
 **************************************************************************/
283
libvlc_media_t * libvlc_media_new(
284
                                   libvlc_instance_t *p_instance,
285
                                   const char * psz_mrl,
286
                                   libvlc_exception_t *p_e )
287
{
288
    input_item_t * p_input_item;
289
    libvlc_media_t * p_md;
290

291
    p_input_item = input_item_New( p_instance->p_libvlc_int, psz_mrl, NULL );
292

293
    if (!p_input_item)
294
    {
295
        libvlc_exception_raise( p_e, "Can't create md's input_item" );
296 297
        return NULL;
    }
298

299
    p_md = libvlc_media_new_from_input_item( p_instance,
300
                p_input_item, p_e );
301

302
    /* The p_input_item is retained in libvlc_media_new_from_input_item */
303
    vlc_gc_decref( p_input_item );
304

305
    return p_md;
306 307
}

308 309 310
/**************************************************************************
 * Create a new media descriptor object
 **************************************************************************/
311
libvlc_media_t * libvlc_media_new_as_node(
312 313 314 315 316
                                   libvlc_instance_t *p_instance,
                                   const char * psz_name,
                                   libvlc_exception_t *p_e )
{
    input_item_t * p_input_item;
317
    libvlc_media_t * p_md;
318

319
    p_input_item = input_item_New( p_instance->p_libvlc_int, "vlc://nop", psz_name );
320 321 322 323 324 325 326

    if (!p_input_item)
    {
        libvlc_exception_raise( p_e, "Can't create md's input_item" );
        return NULL;
    }

327
    p_md = libvlc_media_new_from_input_item( p_instance,
328 329 330 331 332 333 334
                p_input_item, p_e );

    p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );

    return p_md;
}

335 336
/**************************************************************************
 * Add an option to the media descriptor,
337 338
 * that will be used to determine how the media_player will read the
 * media. This allow to use VLC advanced reading/streaming
339 340 341 342
 * options in a per-media basis
 *
 * The options are detailled in vlc --long-help, for instance "--sout-all"
 **************************************************************************/
343 344
void libvlc_media_add_option(
                                   libvlc_media_t * p_md,
345 346 347
                                   const char * ppsz_option,
                                   libvlc_exception_t *p_e )
{
348
    VLC_UNUSED(p_e);
349 350
    input_item_AddOption( p_md->p_input_item, ppsz_option,
                          VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
351 352
}

353 354 355
/**************************************************************************
 * Delete a media descriptor object
 **************************************************************************/
356
void libvlc_media_release( libvlc_media_t *p_md )
357
{
358
    if (!p_md)
359 360
        return;

361 362 363 364
    p_md->i_refcount--;

    if( p_md->i_refcount > 0 )
        return;
365

366 367
    if( p_md->p_subitems )
        libvlc_media_list_release( p_md->p_subitems );
368

369 370 371
    uninstall_input_item_observer( p_md );
    vlc_gc_decref( p_md->p_input_item );

372 373
    /* Construct the event */
    libvlc_event_t event;
374
    event.type = libvlc_MediaFreed;
375
    event.u.media_freed.md = p_md;
376 377 378 379

    /* Send the event */
    libvlc_event_send( p_md->p_event_manager, &event );

380 381
    libvlc_event_manager_release( p_md->p_event_manager );

382 383 384 385
    free( p_md );
}

/**************************************************************************
386 387
 * Retain a media descriptor object
 **************************************************************************/
388
void libvlc_media_retain( libvlc_media_t *p_md )
389 390 391 392 393 394 395 396 397
{
    if (!p_md)
        return;

    p_md->i_refcount++;
}

/**************************************************************************
 * Duplicate a media descriptor object
398
 **************************************************************************/
399 400
libvlc_media_t *
libvlc_media_duplicate( libvlc_media_t *p_md_orig )
401
{
402
    return libvlc_media_new_from_input_item(
403
        p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
404 405
}

406
/**************************************************************************
407
 * Get mrl from a media descriptor object
408 409
 **************************************************************************/
char *
410
libvlc_media_get_mrl( libvlc_media_t * p_md,
411 412
                                 libvlc_exception_t * p_e )
{
413
    VLC_UNUSED(p_e);
414
    return input_item_GetURI( p_md->p_input_item );
415 416
}

417
/**************************************************************************
418
 * Getter for meta information
419 420
 **************************************************************************/

421
char * libvlc_media_get_meta( libvlc_media_t *p_md,
422 423 424
                                         libvlc_meta_t e_meta,
                                         libvlc_exception_t *p_e )
{
425
    char * psz_meta;
426
    VLC_UNUSED(p_e);
427 428 429

    /* XXX: locking */

430
    preparse_if_needed( p_md );
431

432 433
    psz_meta = input_item_GetMeta( p_md->p_input_item,
                                   libvlc_to_vlc_meta[e_meta] );
434

435 436 437
    if( e_meta == libvlc_meta_ArtworkURL && !psz_meta )
    {
        playlist_AskForArtEnqueue(
438
                libvlc_priv(p_md->p_libvlc_instance->p_libvlc_int)->p_playlist,
439
                p_md->p_input_item, pl_Unlocked );
440
    }
441

442 443
    /* Should be integrated in core */
    if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
444 445
    {
        free( psz_meta );
446
        return strdup( p_md->p_input_item->psz_name );
447
    }
448

449
    return psz_meta;
450
}
451

452 453 454 455 456 457
/**************************************************************************
 * Getter for state information
 * Can be error, playing, buffering, NothingSpecial.
 **************************************************************************/

libvlc_state_t
458
libvlc_media_get_state( libvlc_media_t *p_md,
459 460
                                   libvlc_exception_t *p_e )
{
461
    VLC_UNUSED(p_e);
462 463 464 465 466 467 468 469
    return p_md->state;
}

/**************************************************************************
 * Setter for state information (LibVLC Internal)
 **************************************************************************/

void
470
libvlc_media_set_state( libvlc_media_t *p_md,
471 472 473 474
                                   libvlc_state_t state,
                                   libvlc_exception_t *p_e )
{
    libvlc_event_t event;
475
    VLC_UNUSED(p_e);
476 477 478 479

    p_md->state = state;

    /* Construct the event */
480
    event.type = libvlc_MediaStateChanged;
481
    event.u.media_state_changed.new_state = state;
482 483 484 485 486

    /* Send the event */
    libvlc_event_send( p_md->p_event_manager, &event );
}

487 488 489 490
/**************************************************************************
 * subitems
 **************************************************************************/
libvlc_media_list_t *
491
libvlc_media_subitems( libvlc_media_t * p_md,
492 493
                                  libvlc_exception_t * p_e )
{
494 495
    VLC_UNUSED(p_e);

496 497
    if( p_md->p_subitems )
        libvlc_media_list_retain( p_md->p_subitems );
498 499
    return p_md->p_subitems;
}
500 501 502 503 504

/**************************************************************************
 * event_manager
 **************************************************************************/
libvlc_event_manager_t *
505
libvlc_media_event_manager( libvlc_media_t * p_md,
506 507
                                       libvlc_exception_t * p_e )
{
508 509
    VLC_UNUSED(p_e);

510 511
    return p_md->p_event_manager;
}
512 513

/**************************************************************************
514
 * Get duration of media object.
515
 **************************************************************************/
516
int64_t
517
libvlc_media_get_duration( libvlc_media_t * p_md,
518 519
                                      libvlc_exception_t * p_e )
{
520 521
    VLC_UNUSED(p_e);

522
    if( !p_md || !p_md->p_input_item)
523
    {
524
        libvlc_exception_raise( p_e, "No input item" );
525 526
        return -1;
    }
527 528

    return input_item_GetDuration( p_md->p_input_item );
529 530 531
}

/**************************************************************************
532
 * Get preparsed status for media object.
533
 **************************************************************************/
534
int
535
libvlc_media_is_preparsed( libvlc_media_t * p_md,
536 537
                                       libvlc_exception_t * p_e )
{
538 539
    VLC_UNUSED(p_e);

540
    if( !p_md || !p_md->p_input_item)
541
    {
542
        libvlc_exception_raise( p_e, "No input item" );
543
        return false;
544
    }
545 546

    return input_item_IsPreparsed( p_md->p_input_item );
547 548 549 550 551
}

/**************************************************************************
 * Sets media descriptor's user_data. user_data is specialized data 
 * accessed by the host application, VLC.framework uses it as a pointer to 
552
 * an native object that references a libvlc_media_t pointer
553 554
 **************************************************************************/
void 
555
libvlc_media_set_user_data( libvlc_media_t * p_md,
556 557 558
                                       void * p_new_user_data,
                                       libvlc_exception_t * p_e )
{
559 560
    VLC_UNUSED(p_e);

561 562 563 564 565 566 567 568 569
    if( p_md )
    {
        p_md->p_user_data = p_new_user_data;
    }
}

/**************************************************************************
 * Get media descriptor's user_data. user_data is specialized data 
 * accessed by the host application, VLC.framework uses it as a pointer to 
570
 * an native object that references a libvlc_media_t pointer
571 572
 **************************************************************************/
void *
573
libvlc_media_get_user_data( libvlc_media_t * p_md,
574 575
                                       libvlc_exception_t * p_e )
{
576 577
    VLC_UNUSED(p_e);

578 579 580 581 582 583 584 585
    if( p_md )
    {
        return p_md->p_user_data;
    }
    else
    {
        return NULL;
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
586
}