media.c 19.9 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
25
#include "libvlc_internal.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
218
219
        playlist_PreparseEnqueue(
                p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
                p_md->p_input_item );
220
        p_md->b_preparsed = VLC_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
    p_md->p_libvlc_instance = p_instance;
    p_md->p_input_item      = p_input_item;
245
    p_md->b_preparsed       = VLC_FALSE;
246
    p_md->i_refcount        = 1;
247
248
249
    p_md->p_user_data       = NULL;

    p_md->state = libvlc_NothingSpecial;
250

251
252
253
254
    /* 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;

255
    p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
256
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
257
        libvlc_MediaMetaChanged, p_e );
258
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
259
        libvlc_MediaSubItemAdded, p_e );
260
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
261
        libvlc_MediaFreed, p_e );
262
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
263
        libvlc_MediaDurationChanged, p_e );
264
    libvlc_event_manager_register_event_type( p_md->p_event_manager,
265
        libvlc_MediaStateChanged, p_e );
266

267
    vlc_gc_incref( p_md->p_input_item );
268

269
270
    install_input_item_observer( p_md );

271
    return p_md;
272
273
}

274
/**************************************************************************
275
 * Create a new media descriptor object
276
 **************************************************************************/
277
libvlc_media_t * libvlc_media_new(
278
                                   libvlc_instance_t *p_instance,
279
                                   const char * psz_mrl,
280
                                   libvlc_exception_t *p_e )
281
{
282
    input_item_t * p_input_item;
283
    libvlc_media_t * p_md;
284

285
    p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
286

287
    if (!p_input_item)
288
    {
289
        libvlc_exception_raise( p_e, "Can't create md's input_item" );
290
291
        return NULL;
    }
292

293
    p_md = libvlc_media_new_from_input_item( p_instance,
294
                p_input_item, p_e );
295

296
    /* The p_input_item is retained in libvlc_media_new_from_input_item */
297
298
    vlc_gc_decref( p_input_item );
    
299
    return p_md;
300
301
}

302
303
304
/**************************************************************************
 * Create a new media descriptor object
 **************************************************************************/
305
libvlc_media_t * libvlc_media_new_as_node(
306
307
308
309
310
                                   libvlc_instance_t *p_instance,
                                   const char * psz_name,
                                   libvlc_exception_t *p_e )
{
    input_item_t * p_input_item;
311
    libvlc_media_t * p_md;
312

313
    p_input_item = input_ItemNew( p_instance->p_libvlc_int, "vlc://nop", psz_name );
314
315
316
317
318
319
320

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

321
    p_md = libvlc_media_new_from_input_item( p_instance,
322
323
324
325
326
327
328
                p_input_item, p_e );

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

    return p_md;
}

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

347
348
349
/**************************************************************************
 * Delete a media descriptor object
 **************************************************************************/
350
void libvlc_media_release( libvlc_media_t *p_md )
351
{
352
    if (!p_md)
353
354
        return;

355
356
357
358
    p_md->i_refcount--;

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

360
361
    if( p_md->p_subitems )
        libvlc_media_list_release( p_md->p_subitems );
362

363
364
365
    uninstall_input_item_observer( p_md );
    vlc_gc_decref( p_md->p_input_item );

366
367
    /* Construct the event */
    libvlc_event_t event;
368
    event.type = libvlc_MediaFreed;
369
    event.u.media_freed.md = p_md;
370
371
372
373

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

374
375
    libvlc_event_manager_release( p_md->p_event_manager );

376
377
378
379
    free( p_md );
}

/**************************************************************************
380
381
 * Retain a media descriptor object
 **************************************************************************/
382
void libvlc_media_retain( libvlc_media_t *p_md )
383
384
385
386
387
388
389
390
391
{
    if (!p_md)
        return;

    p_md->i_refcount++;
}

/**************************************************************************
 * Duplicate a media descriptor object
392
 **************************************************************************/
393
394
libvlc_media_t *
libvlc_media_duplicate( libvlc_media_t *p_md_orig )
395
{
396
    return libvlc_media_new_from_input_item(
397
        p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
398
399
}

400
401
402
403
/**************************************************************************
 * Retain a media descriptor object
 **************************************************************************/
char *
404
libvlc_media_get_mrl( libvlc_media_t * p_md,
405
406
407
                                 libvlc_exception_t * p_e )
{
    (void)p_e;
408
    return input_item_GetURI( p_md->p_input_item );
409
410
}

411
/**************************************************************************
412
 * Getter for meta information
413
414
 **************************************************************************/

415
char * libvlc_media_get_meta( libvlc_media_t *p_md,
416
417
418
                                         libvlc_meta_t e_meta,
                                         libvlc_exception_t *p_e )
{
419
420
    VLC_UNUSED(p_e);

421
    char * psz_meta;
422
423
424

    /* XXX: locking */

425
    preparse_if_needed( p_md );
426

427
428
    psz_meta = input_item_GetMeta( p_md->p_input_item,
                                   libvlc_to_vlc_meta[e_meta] );
429
430
431
432
433
434
435
    
    if( e_meta == libvlc_meta_ArtworkURL && !psz_meta )
    {
        playlist_AskForArtEnqueue(
                p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
                p_md->p_input_item );
    }
436

437
438
    /* Should be integrated in core */
    if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
439
440
    {
        free( psz_meta );
441
        return strdup( p_md->p_input_item->psz_name );
442
    }
443

444
    return psz_meta;
445
}
446

447
448
449
450
451
452
/**************************************************************************
 * Getter for state information
 * Can be error, playing, buffering, NothingSpecial.
 **************************************************************************/

libvlc_state_t
453
libvlc_media_get_state( libvlc_media_t *p_md,
454
455
456
457
458
459
460
461
462
463
464
                                   libvlc_exception_t *p_e )
{
    (void)p_e;
    return p_md->state;
}

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

void
465
libvlc_media_set_state( libvlc_media_t *p_md,
466
467
468
469
470
471
472
473
474
                                   libvlc_state_t state,
                                   libvlc_exception_t *p_e )
{
    (void)p_e;
    libvlc_event_t event;

    p_md->state = state;

    /* Construct the event */
475
    event.type = libvlc_MediaStateChanged;
476
    event.u.media_state_changed.new_state = state;
477
478
479
480
481

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

482
483
484
485
/**************************************************************************
 * subitems
 **************************************************************************/
libvlc_media_list_t *
486
libvlc_media_subitems( libvlc_media_t * p_md,
487
488
                                  libvlc_exception_t * p_e )
{
489
490
    VLC_UNUSED(p_e);

491
492
    if( p_md->p_subitems )
        libvlc_media_list_retain( p_md->p_subitems );
493
494
    return p_md->p_subitems;
}
495
496
497
498
499

/**************************************************************************
 * event_manager
 **************************************************************************/
libvlc_event_manager_t *
500
libvlc_media_event_manager( libvlc_media_t * p_md,
501
502
                                       libvlc_exception_t * p_e )
{
503
504
    VLC_UNUSED(p_e);

505
506
    return p_md->p_event_manager;
}
507
508

/**************************************************************************
509
 * Get duration of media object.
510
511
 **************************************************************************/
vlc_int64_t
512
libvlc_media_get_duration( libvlc_media_t * p_md,
513
514
                                      libvlc_exception_t * p_e )
{
515
516
    VLC_UNUSED(p_e);

517
518
519
520
521
522
523
524
525
526
527
    if( p_md && p_md->p_input_item)
    {
        return input_item_GetDuration( p_md->p_input_item );
    }
    else
    {
        return -1;
    }
}

/**************************************************************************
528
 * Get preparsed status for media object.
529
 **************************************************************************/
530
int
531
libvlc_media_is_preparsed( libvlc_media_t * p_md,
532
533
                                       libvlc_exception_t * p_e )
{
534
535
    VLC_UNUSED(p_e);

536
537
538
539
540
541
542
543
544
545
546
547
548
    if( p_md && p_md->p_input_item)
    {
        return input_item_IsPreparsed( p_md->p_input_item );
    }
    else
    {
        return VLC_FALSE;
    }
}

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

558
559
560
561
562
563
564
565
566
    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 
567
 * an native object that references a libvlc_media_t pointer
568
569
 **************************************************************************/
void *
570
libvlc_media_get_user_data( libvlc_media_t * p_md,
571
572
                                       libvlc_exception_t * p_e )
{
573
574
    VLC_UNUSED(p_e);

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