forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia.c
1351 lines (1172 loc) · 46 KB
/
media.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* media.c: Libvlc API media descripor management
*****************************************************************************
* Copyright (C) 2007 VLC authors and VideoLAN
*
* Authors: Pierre d'Herbemont <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <errno.h>
#include <vlc/libvlc.h>
#include <vlc/libvlc_picture.h>
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_media_list.h> // For the subitems, here for convenience
#include <vlc/libvlc_events.h>
#include <vlc_common.h>
#include <vlc_input.h>
#include <vlc_meta.h>
#include <vlc_playlist_legacy.h> /* For the preparser */
#include <vlc_url.h>
#include <vlc_thumbnailer.h>
#include "../src/libvlc.h"
#include "libvlc_internal.h"
#include "media_internal.h"
#include "media_list_internal.h"
#include "picture_internal.h"
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,
[libvlc_meta_TrackTotal] = vlc_meta_TrackTotal,
[libvlc_meta_Director] = vlc_meta_Director,
[libvlc_meta_Season] = vlc_meta_Season,
[libvlc_meta_Episode] = vlc_meta_Episode,
[libvlc_meta_ShowName] = vlc_meta_ShowName,
[libvlc_meta_Actors] = vlc_meta_Actors,
[libvlc_meta_AlbumArtist] = vlc_meta_AlbumArtist,
[libvlc_meta_DiscNumber] = vlc_meta_DiscNumber,
[libvlc_meta_DiscTotal] = vlc_meta_DiscTotal
};
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_ESNowPlaying] = 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,
[vlc_meta_TrackTotal] = libvlc_meta_TrackTotal,
[vlc_meta_Director] = libvlc_meta_Director,
[vlc_meta_Season] = libvlc_meta_Season,
[vlc_meta_Episode] = libvlc_meta_Episode,
[vlc_meta_ShowName] = libvlc_meta_ShowName,
[vlc_meta_Actors] = libvlc_meta_Actors,
[vlc_meta_AlbumArtist] = libvlc_meta_AlbumArtist,
[vlc_meta_DiscNumber] = libvlc_meta_DiscNumber,
[vlc_meta_DiscTotal] = libvlc_meta_DiscTotal
};
static_assert(
ORIENT_TOP_LEFT == (int) libvlc_video_orient_top_left &&
ORIENT_TOP_RIGHT == (int) libvlc_video_orient_top_right &&
ORIENT_BOTTOM_LEFT == (int) libvlc_video_orient_bottom_left &&
ORIENT_BOTTOM_RIGHT == (int) libvlc_video_orient_bottom_right &&
ORIENT_LEFT_TOP == (int) libvlc_video_orient_left_top &&
ORIENT_LEFT_BOTTOM == (int) libvlc_video_orient_left_bottom &&
ORIENT_RIGHT_TOP == (int) libvlc_video_orient_right_top &&
ORIENT_RIGHT_BOTTOM == (int) libvlc_video_orient_right_bottom,
"Mismatch between libvlc_video_orient_t and video_orientation_t" );
static_assert(
PROJECTION_MODE_RECTANGULAR == (int) libvlc_video_projection_rectangular &&
PROJECTION_MODE_EQUIRECTANGULAR == (int) libvlc_video_projection_equirectangular &&
PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD == (int) libvlc_video_projection_cubemap_layout_standard,
"Mismatch between libvlc_video_projection_t and video_projection_mode_t" );
static_assert(
MULTIVIEW_2D == (int) libvlc_video_multiview_2d &&
MULTIVIEW_STEREO_SBS == (int) libvlc_video_multiview_stereo_sbs &&
MULTIVIEW_STEREO_TB == (int) libvlc_video_multiview_stereo_tb &&
MULTIVIEW_STEREO_ROW == (int) libvlc_video_multiview_stereo_row &&
MULTIVIEW_STEREO_COL == (int) libvlc_video_multiview_stereo_col &&
MULTIVIEW_STEREO_FRAME == (int) libvlc_video_multiview_stereo_frame &&
MULTIVIEW_STEREO_CHECKERBOARD == (int) libvlc_video_multiview_stereo_checkerboard,
"Mismatch between libvlc_video_multiview_t and video_multiview_mode_t");
static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
bool b_create )
{
libvlc_media_list_t *p_subitems = NULL;
vlc_mutex_lock( &p_md->subitems_lock );
if( p_md->p_subitems == NULL && b_create )
{
p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
if( p_md->p_subitems != NULL )
{
p_md->p_subitems->b_read_only = true;
p_md->p_subitems->p_internal_md = p_md;
}
}
p_subitems = p_md->p_subitems;
vlc_mutex_unlock( &p_md->subitems_lock );
return p_subitems;
}
static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
input_item_t *item )
{
libvlc_media_t * p_md_child;
libvlc_media_list_t *p_subitems;
libvlc_event_t event;
p_md_child = libvlc_media_new_from_input_item( p_md->p_libvlc_instance,
item );
/* Add this to our media list */
p_subitems = media_get_subitems( p_md, true );
if( p_subitems != NULL )
{
libvlc_media_list_lock( p_subitems );
libvlc_media_list_internal_add_media( p_subitems, p_md_child );
libvlc_media_list_unlock( p_subitems );
}
/* Construct the event */
event.type = libvlc_MediaSubItemAdded;
event.u.media_subitem_added.new_child = p_md_child;
/* Send the event */
libvlc_event_send( &p_md->event_manager, &event );
return p_md_child;
}
static void input_item_add_subnode( libvlc_media_t *md,
input_item_node_t *node )
{
for( int i = 0; i < node->i_children; i++ )
{
input_item_node_t *child = node->pp_children[i];
libvlc_media_t *md_child = input_item_add_subitem( md, child->p_item );
if( md_child != NULL )
{
input_item_add_subnode( md_child, child );
libvlc_media_release( md_child );
}
}
}
/**************************************************************************
* input_item_subitemtree_added (Private) (vlc event Callback)
**************************************************************************/
static void input_item_subtree_added(input_item_t *item,
input_item_node_t *node,
void *user_data)
{
VLC_UNUSED(item);
libvlc_media_t * p_md = user_data;
libvlc_media_add_subtree(p_md, node);
}
void libvlc_media_add_subtree(libvlc_media_t *p_md, input_item_node_t *node)
{
/* FIXME FIXME FIXME
* Recursive function calls seem much simpler for this. But playlists are
* untrusted and can be arbitrarily deep (e.g. with XSPF). So recursion can
* potentially lead to plain old stack overflow. */
input_item_add_subnode( p_md, node );
/* Construct the event */
libvlc_event_t event;
event.type = libvlc_MediaSubItemTreeAdded;
event.u.media_subitemtree_added.item = p_md;
/* Send the event */
libvlc_event_send( &p_md->event_manager, &event );
}
/**************************************************************************
* input_item_meta_changed (Private) (vlc event Callback)
**************************************************************************/
static void input_item_meta_changed( const vlc_event_t *p_event,
void * user_data )
{
libvlc_media_t * p_md = user_data;
libvlc_event_t event;
/* Construct the event */
event.type = libvlc_MediaMetaChanged;
event.u.media_meta_changed.meta_type =
vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
/* Send the event */
libvlc_event_send( &p_md->event_manager, &event );
}
/**************************************************************************
* input_item_duration_changed (Private) (vlc event Callback)
**************************************************************************/
static void input_item_duration_changed( const vlc_event_t *p_event,
void * user_data )
{
libvlc_media_t * p_md = user_data;
libvlc_event_t event;
/* Construct the event */
event.type = libvlc_MediaDurationChanged;
event.u.media_duration_changed.new_duration =
from_mtime(p_event->u.input_item_duration_changed.new_duration);
/* Send the event */
libvlc_event_send( &p_md->event_manager, &event );
}
static void send_parsed_changed( libvlc_media_t *p_md,
libvlc_media_parsed_status_t new_status )
{
libvlc_event_t event;
vlc_mutex_lock( &p_md->parsed_lock );
if( p_md->parsed_status == new_status )
{
vlc_mutex_unlock( &p_md->parsed_lock );
return;
}
/* Legacy: notify libvlc_media_parse */
if( !p_md->is_parsed )
{
p_md->is_parsed = true;
vlc_cond_broadcast( &p_md->parsed_cond );
}
p_md->parsed_status = new_status;
if( p_md->parsed_status == libvlc_media_parsed_status_skipped )
p_md->has_asked_preparse = false;
vlc_mutex_unlock( &p_md->parsed_lock );
if( new_status == libvlc_media_parsed_status_done )
{
libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
if( p_subitems != NULL )
{
/* notify the media list */
libvlc_media_list_lock( p_subitems );
libvlc_media_list_internal_end_reached( p_subitems );
libvlc_media_list_unlock( p_subitems );
}
}
/* Construct the event */
event.type = libvlc_MediaParsedChanged;
event.u.media_parsed_changed.new_status = new_status;
/* Send the event */
libvlc_event_send( &p_md->event_manager, &event );
}
/**************************************************************************
* input_item_preparse_ended (Private) (vlc event Callback)
**************************************************************************/
static void input_item_preparse_ended(input_item_t *item,
enum input_item_preparse_status status,
void *user_data)
{
VLC_UNUSED(item);
libvlc_media_t * p_md = user_data;
libvlc_media_parsed_status_t new_status;
switch( status )
{
case ITEM_PREPARSE_SKIPPED:
new_status = libvlc_media_parsed_status_skipped;
break;
case ITEM_PREPARSE_FAILED:
new_status = libvlc_media_parsed_status_failed;
break;
case ITEM_PREPARSE_TIMEOUT:
new_status = libvlc_media_parsed_status_timeout;
break;
case ITEM_PREPARSE_DONE:
new_status = libvlc_media_parsed_status_done;
break;
default:
return;
}
send_parsed_changed( p_md, new_status );
}
/**************************************************************************
* Install event handler (Private)
**************************************************************************/
static void install_input_item_observer( libvlc_media_t *p_md )
{
vlc_event_attach( &p_md->p_input_item->event_manager,
vlc_InputItemMetaChanged,
input_item_meta_changed,
p_md );
vlc_event_attach( &p_md->p_input_item->event_manager,
vlc_InputItemDurationChanged,
input_item_duration_changed,
p_md );
}
/**************************************************************************
* Uninstall event handler (Private)
**************************************************************************/
static void uninstall_input_item_observer( libvlc_media_t *p_md )
{
vlc_event_detach( &p_md->p_input_item->event_manager,
vlc_InputItemMetaChanged,
input_item_meta_changed,
p_md );
vlc_event_detach( &p_md->p_input_item->event_manager,
vlc_InputItemDurationChanged,
input_item_duration_changed,
p_md );
}
/**************************************************************************
* Create a new media descriptor object from an input_item
* (libvlc internal)
* That's the generic constructor
**************************************************************************/
libvlc_media_t * libvlc_media_new_from_input_item(
libvlc_instance_t *p_instance,
input_item_t *p_input_item )
{
libvlc_media_t * p_md;
if (!p_input_item)
{
libvlc_printerr( "No input item given" );
return NULL;
}
p_md = calloc( 1, sizeof(libvlc_media_t) );
if( !p_md )
{
libvlc_printerr( "Not enough memory" );
return NULL;
}
p_md->p_libvlc_instance = p_instance;
p_md->p_input_item = p_input_item;
p_md->i_refcount = 1;
vlc_cond_init(&p_md->parsed_cond);
vlc_mutex_init(&p_md->parsed_lock);
vlc_mutex_init(&p_md->subitems_lock);
p_md->state = libvlc_NothingSpecial;
/* 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;
libvlc_event_manager_init( &p_md->event_manager, p_md );
input_item_Hold( p_md->p_input_item );
install_input_item_observer( p_md );
libvlc_retain( p_instance );
return p_md;
}
/**************************************************************************
* Create a new media descriptor object
**************************************************************************/
libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
const char * psz_mrl )
{
input_item_t * p_input_item;
libvlc_media_t * p_md;
p_input_item = input_item_New( psz_mrl, NULL );
if (!p_input_item)
{
libvlc_printerr( "Not enough memory" );
return NULL;
}
p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
/* The p_input_item is retained in libvlc_media_new_from_input_item */
input_item_Release( p_input_item );
return p_md;
}
libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
const char *path )
{
char *mrl = vlc_path2uri( path, NULL );
if( unlikely(mrl == NULL) )
{
libvlc_printerr( "%s", vlc_strerror_c(errno) );
return NULL;
}
libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
free( mrl );
return m;
}
libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
{
char mrl[16];
snprintf( mrl, sizeof(mrl), "fd://%d", fd );
return libvlc_media_new_location( p_instance, mrl );
}
libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
libvlc_media_open_cb open_cb,
libvlc_media_read_cb read_cb,
libvlc_media_seek_cb seek_cb,
libvlc_media_close_cb close_cb,
void *opaque)
{
libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
if (unlikely(m == NULL))
return NULL;
assert(read_cb != NULL);
input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
return m;
}
/**************************************************************************
* Create a new media descriptor object
**************************************************************************/
libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
const char * psz_name )
{
input_item_t * p_input_item;
libvlc_media_t * p_md;
libvlc_media_list_t * p_subitems;
p_input_item = input_item_New( INPUT_ITEM_URI_NOP, psz_name );
if (!p_input_item)
{
libvlc_printerr( "Not enough memory" );
return NULL;
}
p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
input_item_Release( p_input_item );
p_subitems = media_get_subitems( p_md, true );
if( p_subitems == NULL) {
libvlc_media_release( p_md );
return NULL;
}
return p_md;
}
/**************************************************************************
* Add an option to the media descriptor,
* that will be used to determine how the media_player will read the
* media. This allow to use VLC advanced reading/streaming
* options in a per-media basis
*
* The options are detailled in vlc --long-help, for instance "--sout-all"
**************************************************************************/
void libvlc_media_add_option( libvlc_media_t * p_md,
const char * psz_option )
{
libvlc_media_add_option_flag( p_md, psz_option,
VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
}
/**************************************************************************
* Same as libvlc_media_add_option but with configurable flags.
**************************************************************************/
void libvlc_media_add_option_flag( libvlc_media_t * p_md,
const char * ppsz_option,
unsigned i_flags )
{
input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
}
/**************************************************************************
* Delete a media descriptor object
**************************************************************************/
void libvlc_media_release( libvlc_media_t *p_md )
{
if (!p_md)
return;
p_md->i_refcount--;
if( p_md->i_refcount > 0 )
return;
uninstall_input_item_observer( p_md );
/* Cancel asynchronous parsing (if any) */
libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
if( p_md->p_subitems )
libvlc_media_list_release( p_md->p_subitems );
input_item_Release( p_md->p_input_item );
vlc_cond_destroy( &p_md->parsed_cond );
vlc_mutex_destroy( &p_md->parsed_lock );
vlc_mutex_destroy( &p_md->subitems_lock );
/* Construct the event */
libvlc_event_t event;
event.type = libvlc_MediaFreed;
event.u.media_freed.md = p_md;
/* Send the event */
libvlc_event_send( &p_md->event_manager, &event );
libvlc_event_manager_destroy( &p_md->event_manager );
libvlc_release( p_md->p_libvlc_instance );
free( p_md );
}
/**************************************************************************
* Retain a media descriptor object
**************************************************************************/
void libvlc_media_retain( libvlc_media_t *p_md )
{
assert (p_md);
p_md->i_refcount++;
}
/**************************************************************************
* Duplicate a media descriptor object
**************************************************************************/
libvlc_media_t *
libvlc_media_duplicate( libvlc_media_t *p_md_orig )
{
return libvlc_media_new_from_input_item(
p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
}
/**************************************************************************
* Get mrl from a media descriptor object
**************************************************************************/
char *
libvlc_media_get_mrl( libvlc_media_t * p_md )
{
assert( p_md );
return input_item_GetURI( p_md->p_input_item );
}
/**************************************************************************
* Getter for meta information
**************************************************************************/
char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
{
char *psz_meta = NULL;
if( e_meta == libvlc_meta_NowPlaying )
{
psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
}
else
{
psz_meta = input_item_GetMeta( p_md->p_input_item,
libvlc_to_vlc_meta[e_meta] );
/* Should be integrated in core */
if( psz_meta == NULL && e_meta == libvlc_meta_Title
&& p_md->p_input_item->psz_name != NULL )
psz_meta = strdup( p_md->p_input_item->psz_name );
}
return psz_meta;
}
/**************************************************************************
* Setter for meta information
**************************************************************************/
void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
{
assert( p_md );
input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
}
int libvlc_media_save_meta( libvlc_media_t *p_md )
{
assert( p_md );
vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
}
/**************************************************************************
* Getter for state information
* Can be error, playing, buffering, NothingSpecial.
**************************************************************************/
libvlc_state_t
libvlc_media_get_state( libvlc_media_t *p_md )
{
assert( p_md );
return p_md->state;
}
/**************************************************************************
* Setter for state information (LibVLC Internal)
**************************************************************************/
void
libvlc_media_set_state( libvlc_media_t *p_md,
libvlc_state_t state )
{
libvlc_event_t event;
p_md->state = state;
/* Construct the event */
event.type = libvlc_MediaStateChanged;
event.u.media_state_changed.new_state = state;
/* Send the event */
libvlc_event_send( &p_md->event_manager, &event );
}
/**************************************************************************
* subitems
**************************************************************************/
libvlc_media_list_t *
libvlc_media_subitems( libvlc_media_t * p_md )
{
libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
if( p_subitems )
libvlc_media_list_retain( p_subitems );
return p_subitems;
}
/**************************************************************************
* Getter for statistics information
**************************************************************************/
int libvlc_media_get_stats( libvlc_media_t *p_md,
libvlc_media_stats_t *p_stats )
{
input_item_t *item = p_md->p_input_item;
if( !p_md->p_input_item )
return false;
vlc_mutex_lock( &item->lock );
input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
if( p_itm_stats == NULL )
{
vlc_mutex_unlock( &item->lock );
return false;
}
p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
p_stats->i_sent_packets = 0;
p_stats->i_sent_bytes = 0;
p_stats->f_send_bitrate = 0.;
vlc_mutex_unlock( &item->lock );
return true;
}
/**************************************************************************
* event_manager
**************************************************************************/
libvlc_event_manager_t *
libvlc_media_event_manager( libvlc_media_t * p_md )
{
assert( p_md );
return &p_md->event_manager;
}
/**************************************************************************
* Get duration of media object (in ms)
**************************************************************************/
int64_t
libvlc_media_get_duration( libvlc_media_t * p_md )
{
assert( p_md );
if( !p_md->p_input_item )
{
libvlc_printerr( "No input item" );
return -1;
}
if (!input_item_IsPreparsed( p_md->p_input_item ))
return -1;
return from_mtime(input_item_GetDuration( p_md->p_input_item ));
}
static const input_preparser_callbacks_t input_preparser_callbacks = {
.on_preparse_ended = input_item_preparse_ended,
.on_subtree_added = input_item_subtree_added,
};
static int media_parse(libvlc_media_t *media, bool b_async,
libvlc_media_parse_flag_t parse_flag, int timeout)
{
bool needed;
vlc_mutex_lock(&media->parsed_lock);
needed = !media->has_asked_preparse;
media->has_asked_preparse = true;
if (needed)
media->is_parsed = false;
vlc_mutex_unlock(&media->parsed_lock);
if (needed)
{
libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
input_item_t *item = media->p_input_item;
input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
int ret;
/* Ignore libvlc_media_fetch_local flag since local art will be fetched
* by libvlc_MetadataRequest */
if (parse_flag & libvlc_media_fetch_network)
{
ret = libvlc_ArtRequest(libvlc, item,
META_REQUEST_OPTION_SCOPE_NETWORK,
NULL, NULL);
if (ret != VLC_SUCCESS)
return ret;
}
if (parse_flag & libvlc_media_parse_network)
parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
if (parse_flag & libvlc_media_do_interact)
parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
ret = libvlc_MetadataRequest(libvlc, item, parse_scope, &input_preparser_callbacks, media, timeout, media);
if (ret != VLC_SUCCESS)
return ret;
}
else
return VLC_EGENERIC;
if (!b_async)
{
vlc_mutex_lock(&media->parsed_lock);
while (!media->is_parsed)
vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
vlc_mutex_unlock(&media->parsed_lock);
}
return VLC_SUCCESS;
}
/**************************************************************************
* Parse the media and wait.
**************************************************************************/
void
libvlc_media_parse(libvlc_media_t *media)
{
media_parse( media, false, libvlc_media_fetch_local, -1 );
}
/**************************************************************************
* Parse the media but do not wait.
**************************************************************************/
void
libvlc_media_parse_async(libvlc_media_t *media)
{
media_parse( media, true, libvlc_media_fetch_local, -1 );
}
/**************************************************************************
* Parse the media asynchronously with options.
**************************************************************************/
int
libvlc_media_parse_with_options( libvlc_media_t *media,
libvlc_media_parse_flag_t parse_flag,
int timeout )
{
return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
}
void
libvlc_media_parse_stop( libvlc_media_t *media )
{
libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
}
/**************************************************************************
* Get parsed status for media object.
**************************************************************************/
int
libvlc_media_is_parsed(libvlc_media_t *media)
{
bool parsed;
vlc_mutex_lock(&media->parsed_lock);
parsed = media->is_parsed;
vlc_mutex_unlock(&media->parsed_lock);
return parsed;
}
libvlc_media_parsed_status_t
libvlc_media_get_parsed_status(libvlc_media_t *media)
{
libvlc_media_parsed_status_t status;
vlc_mutex_lock(&media->parsed_lock);
status = media->parsed_status;
vlc_mutex_unlock(&media->parsed_lock);
return status;
}
/**************************************************************************
* Sets media descriptor's user_data. user_data is specialized data
* accessed by the host application, VLC.framework uses it as a pointer to
* an native object that references a libvlc_media_t pointer
**************************************************************************/
void
libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
{
assert( 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
* an native object that references a libvlc_media_t pointer
**************************************************************************/
void *
libvlc_media_get_user_data( libvlc_media_t * p_md )
{
assert( p_md );
return p_md->p_user_data;
}
unsigned
libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
{
assert( p_md );
input_item_t *p_input_item = p_md->p_input_item;
vlc_mutex_lock( &p_input_item->lock );
const int i_es = p_input_item->i_es;
*pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
if( !*pp_es ) /* no ES, or OOM */
{
vlc_mutex_unlock( &p_input_item->lock );
return 0;
}
/* Fill array */
for( int i = 0; i < i_es; i++ )
{
libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
if ( p_mes )
{
p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
sizeof(*p_mes->video)),
sizeof(*p_mes->subtitle)) );
}
if ( !p_mes || !p_mes->audio )
{
libvlc_media_tracks_release( *pp_es, i_es );
*pp_es = NULL;
free( p_mes );
vlc_mutex_unlock( &p_input_item->lock );
return 0;
}
(*pp_es)[i] = p_mes;
const es_format_t *p_es = p_input_item->es[i];
p_mes->i_codec = p_es->i_codec;
p_mes->i_original_fourcc = p_es->i_original_fourcc;
p_mes->i_id = p_es->i_id;
p_mes->i_profile = p_es->i_profile;
p_mes->i_level = p_es->i_level;
p_mes->i_bitrate = p_es->i_bitrate;
p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
switch(p_es->i_cat)
{
case UNKNOWN_ES:
default:
p_mes->i_type = libvlc_track_unknown;
break;
case VIDEO_ES:
p_mes->i_type = libvlc_track_video;
p_mes->video->i_height = p_es->video.i_visible_height;
p_mes->video->i_width = p_es->video.i_visible_width;
p_mes->video->i_sar_num = p_es->video.i_sar_num;
p_mes->video->i_sar_den = p_es->video.i_sar_den;
p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
assert( p_es->video.orientation >= ORIENT_TOP_LEFT &&
p_es->video.orientation <= ORIENT_RIGHT_BOTTOM );
p_mes->video->i_orientation = (int) p_es->video.orientation;
assert( ( p_es->video.projection_mode >= PROJECTION_MODE_RECTANGULAR &&
p_es->video.projection_mode <= PROJECTION_MODE_EQUIRECTANGULAR ) ||
( p_es->video.projection_mode == PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD ) );
p_mes->video->i_projection = (int) p_es->video.projection_mode;
p_mes->video->pose.f_yaw = p_es->video.pose.yaw;
p_mes->video->pose.f_pitch = p_es->video.pose.pitch;
p_mes->video->pose.f_roll = p_es->video.pose.roll;
p_mes->video->pose.f_field_of_view = p_es->video.pose.fov;
assert( p_es->video.multiview_mode >= MULTIVIEW_2D &&
p_es->video.multiview_mode <= MULTIVIEW_STEREO_CHECKERBOARD );
p_mes->video->i_multiview = (int) p_es->video.multiview_mode;
break;
case AUDIO_ES:
p_mes->i_type = libvlc_track_audio;
p_mes->audio->i_channels = p_es->audio.i_channels;
p_mes->audio->i_rate = p_es->audio.i_rate;