VLCMedia.m 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*****************************************************************************
  2. * VLCMedia.m: VLCKit.framework VLCMedia implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2013, 2017 Felix Paul Kühne
  6. * Copyright (C) 2007, 2013 VLC authors and VideoLAN
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Felix Paul Kühne <fkuehne # videolan.org>
  11. * Soomin Lee <TheHungryBu # gmail.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU Lesser General Public License as published by
  15. * the Free Software Foundation; either version 2.1 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  26. *****************************************************************************/
  27. #import "VLCMedia.h"
  28. #import "VLCMediaList.h"
  29. #import "VLCEventManager.h"
  30. #import "VLCLibrary.h"
  31. #import "VLCLibVLCBridging.h"
  32. #import <vlc/libvlc.h>
  33. #import <sys/sysctl.h> // for sysctlbyname
  34. /* Meta Dictionary Keys */
  35. NSString *const VLCMetaInformationTitle = @"title";
  36. NSString *const VLCMetaInformationArtist = @"artist";
  37. NSString *const VLCMetaInformationGenre = @"genre";
  38. NSString *const VLCMetaInformationCopyright = @"copyright";
  39. NSString *const VLCMetaInformationAlbum = @"album";
  40. NSString *const VLCMetaInformationTrackNumber = @"trackNumber";
  41. NSString *const VLCMetaInformationDescription = @"description";
  42. NSString *const VLCMetaInformationRating = @"rating";
  43. NSString *const VLCMetaInformationDate = @"date";
  44. NSString *const VLCMetaInformationSetting = @"setting";
  45. NSString *const VLCMetaInformationURL = @"url";
  46. NSString *const VLCMetaInformationLanguage = @"language";
  47. NSString *const VLCMetaInformationNowPlaying = @"nowPlaying";
  48. NSString *const VLCMetaInformationPublisher = @"publisher";
  49. NSString *const VLCMetaInformationEncodedBy = @"encodedBy";
  50. NSString *const VLCMetaInformationArtworkURL = @"artworkURL";
  51. NSString *const VLCMetaInformationArtwork = @"artwork";
  52. NSString *const VLCMetaInformationTrackID = @"trackID";
  53. NSString *const VLCMetaInformationTrackTotal = @"trackTotal";
  54. NSString *const VLCMetaInformationDirector = @"director";
  55. NSString *const VLCMetaInformationSeason = @"season";
  56. NSString *const VLCMetaInformationEpisode = @"episode";
  57. NSString *const VLCMetaInformationShowName = @"showName";
  58. NSString *const VLCMetaInformationActors = @"actors";
  59. NSString *const VLCMetaInformationAlbumArtist = @"AlbumArtist";
  60. NSString *const VLCMetaInformationDiscNumber = @"discNumber";
  61. /* Notification Messages */
  62. NSString *const VLCMediaMetaChanged = @"VLCMediaMetaChanged";
  63. /******************************************************************************
  64. * VLC callbacks for streaming.
  65. */
  66. int open_cb(void *opaque, void **datap, uint64_t *sizep) {
  67. NSInputStream *stream = (__bridge NSInputStream *)(opaque);
  68. // Once a stream is closed, it cannot be reopened.
  69. if (stream && stream.streamStatus == NSStreamStatusNotOpen) {
  70. [stream open];
  71. *datap = opaque;
  72. *sizep = UINT64_MAX;
  73. return 0;
  74. }
  75. return stream.streamStatus == NSStreamStatusOpen ? 0 : -1;
  76. }
  77. ssize_t read_cb(void *opaque, unsigned char *buf, size_t len) {
  78. NSInputStream *stream = (__bridge NSInputStream *)(opaque);
  79. if (!stream) {
  80. return -1;
  81. }
  82. return [stream read:buf maxLength:len];
  83. }
  84. int seek_cb(void *opaque, uint64_t offset) {
  85. NSInputStream *stream = (__bridge NSInputStream *)(opaque);
  86. if (!stream) {
  87. return -1;
  88. }
  89. /*
  90. By default, NSStream instances that are not file-based are non-seekable, one-way streams (although custom seekable subclasses are possible).
  91. Once the data has been provided or consumed, the data cannot be retrieved from the stream.
  92. However, you may want a peer subclass to NSInputStream whose instances are capable of seeking through a stream.
  93. */
  94. return [stream setProperty:@(offset) forKey:NSStreamFileCurrentOffsetKey] ? 0 : -1;
  95. }
  96. void close_cb(void *opaque) {
  97. NSInputStream *stream = (__bridge NSInputStream *)(opaque);
  98. if (stream && stream.streamStatus != NSStreamStatusClosed && stream.streamStatus != NSStreamStatusNotOpen) {
  99. [stream close];
  100. }
  101. return;
  102. }
  103. /******************************************************************************
  104. * VLCMedia ()
  105. */
  106. @interface VLCMedia()
  107. {
  108. void * p_md; ///< Internal media descriptor instance
  109. BOOL isArtFetched; ///< Value used to determine of the artwork has been parsed
  110. BOOL areOthersMetaFetched; ///< Value used to determine of the other meta has been parsed
  111. BOOL isArtURLFetched; ///< Value used to determine of the other meta has been preparsed
  112. NSMutableDictionary *_metaDictionary; ///< Dictionary to cache metadata read from libvlc
  113. NSInputStream *stream; ///< Stream object if instance is initialized via NSInputStream to pass to callbacks
  114. }
  115. /* Make our properties internally readwrite */
  116. @property (nonatomic, readwrite) VLCMediaState state;
  117. @property (nonatomic, readwrite, strong) VLCMediaList * subitems;
  118. /* Statics */
  119. + (libvlc_meta_t)stringToMetaType:(NSString *)string;
  120. + (NSString *)metaTypeToString:(libvlc_meta_t)type;
  121. /* Initializers */
  122. - (void)initInternalMediaDescriptor;
  123. /* Operations */
  124. - (void)fetchMetaInformationFromLibVLCWithType:(NSString*)metaType;
  125. #if !TARGET_OS_IPHONE
  126. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL;
  127. - (void)setArtwork:(NSImage *)art;
  128. #endif
  129. - (void)parseIfNeeded;
  130. /* Callback Methods */
  131. - (void)parsedChanged:(NSNumber *)isParsedAsNumber;
  132. - (void)metaChanged:(NSString *)metaType;
  133. - (void)subItemAdded;
  134. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber;
  135. @end
  136. static VLCMediaState libvlc_state_to_media_state[] =
  137. {
  138. [libvlc_NothingSpecial] = VLCMediaStateNothingSpecial,
  139. [libvlc_Stopped] = VLCMediaStateNothingSpecial,
  140. [libvlc_Opening] = VLCMediaStateNothingSpecial,
  141. [libvlc_Buffering] = VLCMediaStateBuffering,
  142. [libvlc_Ended] = VLCMediaStateNothingSpecial,
  143. [libvlc_Error] = VLCMediaStateError,
  144. [libvlc_Playing] = VLCMediaStatePlaying,
  145. [libvlc_Paused] = VLCMediaStatePlaying,
  146. };
  147. static inline VLCMediaState LibVLCStateToMediaState( libvlc_state_t state )
  148. {
  149. return libvlc_state_to_media_state[state];
  150. }
  151. /******************************************************************************
  152. * LibVLC Event Callback
  153. */
  154. static void HandleMediaMetaChanged(const libvlc_event_t * event, void * self)
  155. {
  156. @autoreleasepool {
  157. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  158. withMethod:@selector(metaChanged:)
  159. withArgumentAsObject:[VLCMedia metaTypeToString:event->u.media_meta_changed.meta_type]];
  160. }
  161. }
  162. static void HandleMediaDurationChanged(const libvlc_event_t * event, void * self)
  163. {
  164. @autoreleasepool {
  165. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  166. withMethod:@selector(setLength:)
  167. withArgumentAsObject:[VLCTime timeWithNumber:
  168. @(event->u.media_duration_changed.new_duration)]];
  169. }
  170. }
  171. static void HandleMediaStateChanged(const libvlc_event_t * event, void * self)
  172. {
  173. @autoreleasepool {
  174. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  175. withMethod:@selector(setStateAsNumber:)
  176. withArgumentAsObject:@(LibVLCStateToMediaState(event->u.media_state_changed.new_state))];
  177. }
  178. }
  179. static void HandleMediaSubItemAdded(const libvlc_event_t * event, void * self)
  180. {
  181. @autoreleasepool {
  182. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  183. withMethod:@selector(subItemAdded)
  184. withArgumentAsObject:nil];
  185. }
  186. }
  187. static void HandleMediaParsedChanged(const libvlc_event_t * event, void * self)
  188. {
  189. @autoreleasepool {
  190. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  191. withMethod:@selector(parsedChanged:)
  192. withArgumentAsObject:@((BOOL)event->u.media_parsed_changed.new_status)];
  193. }
  194. }
  195. /******************************************************************************
  196. * Implementation
  197. */
  198. @implementation VLCMedia
  199. + (NSString *)codecNameForFourCC:(uint32_t)fourcc trackType:(NSString *)trackType
  200. {
  201. libvlc_track_type_t track_type = libvlc_track_unknown;
  202. if ([trackType isEqualToString:VLCMediaTracksInformationTypeAudio])
  203. track_type = libvlc_track_audio;
  204. else if ([trackType isEqualToString:VLCMediaTracksInformationTypeVideo])
  205. track_type = libvlc_track_video;
  206. else if ([trackType isEqualToString:VLCMediaTracksInformationTypeText])
  207. track_type = libvlc_track_text;
  208. const char *ret = libvlc_media_get_codec_description(track_type, fourcc);
  209. if (ret)
  210. return [NSString stringWithUTF8String:ret];
  211. return @"";
  212. }
  213. + (instancetype)mediaWithURL:(NSURL *)anURL;
  214. {
  215. return [[VLCMedia alloc] initWithURL:anURL];
  216. }
  217. + (instancetype)mediaWithPath:(NSString *)aPath;
  218. {
  219. return [[VLCMedia alloc] initWithPath:aPath];
  220. }
  221. + (instancetype)mediaAsNodeWithName:(NSString *)aName;
  222. {
  223. return [[VLCMedia alloc] initAsNodeWithName:aName];
  224. }
  225. - (instancetype)initWithPath:(NSString *)aPath
  226. {
  227. return [self initWithURL:[NSURL fileURLWithPath:aPath isDirectory:NO]];
  228. }
  229. - (instancetype)initWithURL:(NSURL *)anURL
  230. {
  231. if (self = [super init]) {
  232. const char *url;
  233. VLCLibrary *library = [VLCLibrary sharedLibrary];
  234. NSAssert(library.instance, @"no library instance when creating media");
  235. if (([[anURL absoluteString] hasPrefix:@"sftp://"]) ||
  236. ([[anURL absoluteString] hasPrefix:@"smb://"])) {
  237. url = [[[anURL absoluteString] stringByRemovingPercentEncoding] UTF8String];
  238. } else {
  239. url = [[anURL absoluteString] UTF8String];
  240. }
  241. p_md = libvlc_media_new_location(library.instance, url);
  242. _metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  243. [self initInternalMediaDescriptor];
  244. }
  245. return self;
  246. }
  247. - (instancetype)initWithStream:(NSInputStream *)stream
  248. {
  249. if (self = [super init]) {
  250. VLCLibrary *library = [VLCLibrary sharedLibrary];
  251. NSAssert(library.instance, @"no library instance when creating media");
  252. NSAssert(stream.streamStatus != NSStreamStatusClosed, @"Passing closed stream to VLCMedia.init does not work");
  253. self->stream = stream;
  254. p_md = libvlc_media_new_callbacks(library.instance, open_cb, read_cb, seek_cb, close_cb, (__bridge void *)(stream));
  255. _metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  256. [self initInternalMediaDescriptor];
  257. }
  258. return self;
  259. }
  260. - (instancetype)initAsNodeWithName:(NSString *)aName
  261. {
  262. if (self = [super init]) {
  263. p_md = libvlc_media_new_as_node([VLCLibrary sharedInstance], [aName UTF8String]);
  264. _metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  265. [self initInternalMediaDescriptor];
  266. }
  267. return self;
  268. }
  269. - (void)dealloc
  270. {
  271. libvlc_event_manager_t * p_em = libvlc_media_event_manager(p_md);
  272. if (p_em) {
  273. libvlc_event_detach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, (__bridge void *)(self));
  274. libvlc_event_detach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, (__bridge void *)(self));
  275. libvlc_event_detach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, (__bridge void *)(self));
  276. libvlc_event_detach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, (__bridge void *)(self));
  277. libvlc_event_detach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, (__bridge void *)(self));
  278. }
  279. [[VLCEventManager sharedManager] cancelCallToObject:self];
  280. libvlc_media_release( p_md );
  281. }
  282. - (VLCMediaType)mediaType
  283. {
  284. libvlc_media_type_t libmediatype = libvlc_media_get_type(p_md);
  285. switch (libmediatype) {
  286. case libvlc_media_type_file:
  287. return VLCMediaTypeFile;
  288. case libvlc_media_type_directory:
  289. return VLCMediaTypeDirectory;
  290. case libvlc_media_type_disc:
  291. return VLCMediaTypeDisc;
  292. case libvlc_media_type_stream:
  293. return VLCMediaTypeStream;
  294. case libvlc_media_type_playlist:
  295. return VLCMediaTypePlaylist;
  296. default:
  297. return VLCMediaTypeUnknown;
  298. }
  299. }
  300. - (NSString *)description
  301. {
  302. return [NSString stringWithFormat:@"<%@ %p>, md: %p, url: %@", [self class], self, p_md, [[_url absoluteString] stringByRemovingPercentEncoding]];
  303. }
  304. - (NSComparisonResult)compare:(VLCMedia *)media
  305. {
  306. if (self == media)
  307. return NSOrderedSame;
  308. if (!media)
  309. return NSOrderedDescending;
  310. return p_md == [media libVLCMediaDescriptor] ? NSOrderedSame : NSOrderedAscending;
  311. }
  312. - (VLCTime *)length
  313. {
  314. if (!_length) {
  315. // Try figuring out what the length is
  316. long long duration = libvlc_media_get_duration( p_md );
  317. if (duration < 0)
  318. return [VLCTime nullTime];
  319. _length = [VLCTime timeWithNumber:@(duration)];
  320. }
  321. return _length;
  322. }
  323. - (VLCTime *)lengthWaitUntilDate:(NSDate *)aDate
  324. {
  325. static const long long thread_sleep = 10000;
  326. if (!_length) {
  327. // Force parsing of this item.
  328. [self parseIfNeeded];
  329. // wait until we are preparsed
  330. libvlc_media_parsed_status_t status = libvlc_media_get_parsed_status(p_md);
  331. while (!_length && !(status == VLCMediaParsedStatusFailed || status == VLCMediaParsedStatusDone) && [aDate timeIntervalSinceNow] > 0) {
  332. usleep( thread_sleep );
  333. status = libvlc_media_get_parsed_status(p_md);
  334. }
  335. // So we're done waiting, but sometimes we trap the fact that the parsing
  336. // was done before the length gets assigned, so lets go ahead and assign
  337. // it ourselves.
  338. if (!_length)
  339. return [self length];
  340. }
  341. return _length;
  342. }
  343. - (BOOL)isParsed
  344. {
  345. VLCMediaParsedStatus status = [self parsedStatus];
  346. return (status == VLCMediaParsedStatusFailed || status == VLCMediaParsedStatusDone) ? YES:NO;
  347. }
  348. - (VLCMediaParsedStatus)parsedStatus
  349. {
  350. if (!p_md)
  351. return VLCMediaParsedStatusFailed;
  352. libvlc_media_parsed_status_t status = libvlc_media_get_parsed_status(p_md);
  353. return (VLCMediaParsedStatus)status;
  354. }
  355. - (void)parse
  356. {
  357. if (p_md)
  358. libvlc_media_parse_async(p_md);
  359. }
  360. - (void)synchronousParse
  361. {
  362. if (p_md)
  363. libvlc_media_parse(p_md);
  364. }
  365. - (int)parseWithOptions:(VLCMediaParsingOptions)options timeout:(int)timeoutValue
  366. {
  367. if (!p_md)
  368. return -1;
  369. // we are using the default time-out value
  370. return libvlc_media_parse_with_options(p_md,
  371. options,
  372. timeoutValue);
  373. }
  374. - (int)parseWithOptions:(VLCMediaParsingOptions)options
  375. {
  376. if (!p_md)
  377. return -1;
  378. // we are using the default time-out value
  379. return libvlc_media_parse_with_options(p_md,
  380. options,
  381. -1);
  382. }
  383. - (void)addOptions:(NSDictionary*)options
  384. {
  385. if (p_md) {
  386. [options enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  387. if (![obj isKindOfClass:[NSNull class]])
  388. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, obj] UTF8String]);
  389. else
  390. libvlc_media_add_option(p_md, [key UTF8String]);
  391. }];
  392. }
  393. }
  394. - (int)storeCookie:(NSString * _Nonnull)cookie
  395. forHost:(NSString *_Nonnull)host
  396. path:(NSString *_Nonnull)path
  397. {
  398. if (!p_md || cookie == NULL || host == NULL || path == NULL) {
  399. return -1;
  400. }
  401. #if TARGET_OS_IPHONE
  402. return libvlc_media_cookie_jar_store(p_md,
  403. [cookie UTF8String],
  404. [host UTF8String],
  405. [path UTF8String]);
  406. #else
  407. return -1;
  408. #endif
  409. }
  410. - (void)clearStoredCookies
  411. {
  412. if (!p_md) {
  413. return;
  414. }
  415. #if TARGET_OS_IPHONE
  416. libvlc_media_cookie_jar_clear(p_md);
  417. #endif
  418. }
  419. - (NSDictionary *)stats
  420. {
  421. if (!p_md)
  422. return nil;
  423. libvlc_media_stats_t p_stats;
  424. libvlc_media_get_stats(p_md, &p_stats);
  425. return @{
  426. @"demuxBitrate" : @(p_stats.f_demux_bitrate),
  427. @"inputBitrate" : @(p_stats.f_input_bitrate),
  428. @"sendBitrate" : @(p_stats.f_send_bitrate),
  429. @"decodedAudio" : @(p_stats.i_decoded_audio),
  430. @"decodedVideo" : @(p_stats.i_decoded_video),
  431. @"demuxCorrupted" : @(p_stats.i_demux_corrupted),
  432. @"demuxDiscontinuity" : @(p_stats.i_demux_discontinuity),
  433. @"demuxReadBytes" : @(p_stats.i_demux_read_bytes),
  434. @"displayedPictures" : @(p_stats.i_displayed_pictures),
  435. @"lostAbuffers" : @(p_stats.i_lost_abuffers),
  436. @"lostPictures" : @(p_stats.i_lost_pictures),
  437. @"playedAbuffers" : @(p_stats.i_played_abuffers),
  438. @"readBytes" : @(p_stats.i_read_bytes),
  439. @"sentBytes" : @(p_stats.i_sent_bytes),
  440. @"sentPackets" : @(p_stats.i_sent_packets)
  441. };
  442. }
  443. - (NSInteger)numberOfReadBytesOnInput
  444. {
  445. if (!p_md)
  446. return 0;
  447. libvlc_media_stats_t p_stats;
  448. libvlc_media_get_stats(p_md, &p_stats);
  449. return p_stats.i_read_bytes;
  450. }
  451. - (float)inputBitrate
  452. {
  453. if (!p_md)
  454. return .0;
  455. libvlc_media_stats_t p_stats;
  456. libvlc_media_get_stats(p_md, &p_stats);
  457. return p_stats.f_input_bitrate;
  458. }
  459. - (NSInteger)numberOfReadBytesOnDemux
  460. {
  461. if (!p_md)
  462. return 0;
  463. libvlc_media_stats_t p_stats;
  464. libvlc_media_get_stats(p_md, &p_stats);
  465. return p_stats.i_demux_read_bytes;
  466. }
  467. - (float)demuxBitrate
  468. {
  469. if (!p_md)
  470. return .0;
  471. libvlc_media_stats_t p_stats;
  472. libvlc_media_get_stats(p_md, &p_stats);
  473. return p_stats.f_demux_bitrate;
  474. }
  475. - (NSInteger)numberOfDecodedVideoBlocks
  476. {
  477. if (!p_md)
  478. return 0;
  479. libvlc_media_stats_t p_stats;
  480. libvlc_media_get_stats(p_md, &p_stats);
  481. return p_stats.i_decoded_video;
  482. }
  483. - (NSInteger)numberOfDecodedAudioBlocks
  484. {
  485. if (!p_md)
  486. return 0;
  487. libvlc_media_stats_t p_stats;
  488. libvlc_media_get_stats(p_md, &p_stats);
  489. return p_stats.i_decoded_audio;
  490. }
  491. - (NSInteger)numberOfDisplayedPictures
  492. {
  493. if (!p_md)
  494. return 0;
  495. libvlc_media_stats_t p_stats;
  496. libvlc_media_get_stats(p_md, &p_stats);
  497. return p_stats.i_displayed_pictures;
  498. }
  499. - (NSInteger)numberOfLostPictures
  500. {
  501. if (!p_md)
  502. return 0;
  503. libvlc_media_stats_t p_stats;
  504. libvlc_media_get_stats(p_md, &p_stats);
  505. return p_stats.i_lost_pictures;
  506. }
  507. - (NSInteger)numberOfPlayedAudioBuffers
  508. {
  509. if (!p_md)
  510. return 0;
  511. libvlc_media_stats_t p_stats;
  512. libvlc_media_get_stats(p_md, &p_stats);
  513. return p_stats.i_played_abuffers;
  514. }
  515. - (NSInteger)numberOfLostAudioBuffers
  516. {
  517. if (!p_md)
  518. return 0;
  519. libvlc_media_stats_t p_stats;
  520. libvlc_media_get_stats(p_md, &p_stats);
  521. return p_stats.i_lost_abuffers;
  522. }
  523. - (NSInteger)numberOfSentPackets
  524. {
  525. if (!p_md)
  526. return 0;
  527. libvlc_media_stats_t p_stats;
  528. libvlc_media_get_stats(p_md, &p_stats);
  529. return p_stats.i_sent_packets;
  530. }
  531. - (NSInteger)numberOfSentBytes
  532. {
  533. if (!p_md)
  534. return 0;
  535. libvlc_media_stats_t p_stats;
  536. libvlc_media_get_stats(p_md, &p_stats);
  537. return p_stats.i_sent_bytes;
  538. }
  539. - (float)streamOutputBitrate
  540. {
  541. if (!p_md)
  542. return .0;
  543. libvlc_media_stats_t p_stats;
  544. libvlc_media_get_stats(p_md, &p_stats);
  545. return p_stats.f_send_bitrate;
  546. }
  547. - (NSInteger)numberOfCorruptedDataPackets
  548. {
  549. if (!p_md)
  550. return 0;
  551. libvlc_media_stats_t p_stats;
  552. libvlc_media_get_stats(p_md, &p_stats);
  553. return p_stats.i_demux_corrupted;
  554. }
  555. - (NSInteger)numberOfDiscontinuties
  556. {
  557. if (!p_md)
  558. return 0;
  559. libvlc_media_stats_t p_stats;
  560. libvlc_media_get_stats(p_md, &p_stats);
  561. return p_stats.i_demux_discontinuity;
  562. }
  563. NSString *const VLCMediaTracksInformationCodec = @"codec"; // NSNumber
  564. NSString *const VLCMediaTracksInformationId = @"id"; // NSNumber
  565. NSString *const VLCMediaTracksInformationType = @"type"; // NSString
  566. NSString *const VLCMediaTracksInformationCodecProfile = @"profile"; // NSNumber
  567. NSString *const VLCMediaTracksInformationCodecLevel = @"level"; // NSNumber
  568. NSString *const VLCMediaTracksInformationTypeAudio = @"audio";
  569. NSString *const VLCMediaTracksInformationTypeVideo = @"video";
  570. NSString *const VLCMediaTracksInformationTypeText = @"text";
  571. NSString *const VLCMediaTracksInformationTypeUnknown = @"unknown";
  572. NSString *const VLCMediaTracksInformationBitrate = @"bitrate"; // NSNumber
  573. NSString *const VLCMediaTracksInformationLanguage = @"language"; // NSString
  574. NSString *const VLCMediaTracksInformationDescription = @"description"; // NSString
  575. NSString *const VLCMediaTracksInformationAudioChannelsNumber = @"channelsNumber"; // NSNumber
  576. NSString *const VLCMediaTracksInformationAudioRate = @"rate"; // NSNumber
  577. NSString *const VLCMediaTracksInformationVideoHeight = @"height"; // NSNumber
  578. NSString *const VLCMediaTracksInformationVideoWidth = @"width"; // NSNumber
  579. NSString *const VLCMediaTracksInformationVideoOrientation = @"orientation"; // NSNumber
  580. NSString *const VLCMediaTracksInformationVideoProjection = @"projection"; // NSNumber
  581. NSString *const VLCMediaTracksInformationSourceAspectRatio = @"sar_num"; // NSNumber
  582. NSString *const VLCMediaTracksInformationSourceAspectRatioDenominator = @"sar_den"; // NSNumber
  583. NSString *const VLCMediaTracksInformationFrameRate = @"frame_rate_num"; // NSNumber
  584. NSString *const VLCMediaTracksInformationFrameRateDenominator = @"frame_rate_den"; // NSNumber
  585. NSString *const VLCMediaTracksInformationTextEncoding = @"encoding"; // NSString
  586. - (NSArray *)tracksInformation
  587. {
  588. libvlc_media_track_t **tracksInfo;
  589. unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
  590. NSMutableArray *array = [NSMutableArray array];
  591. for (NSUInteger i = 0; i < count; i++) {
  592. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  593. @(tracksInfo[i]->i_codec),
  594. VLCMediaTracksInformationCodec,
  595. @(tracksInfo[i]->i_id),
  596. VLCMediaTracksInformationId,
  597. @(tracksInfo[i]->i_profile),
  598. VLCMediaTracksInformationCodecProfile,
  599. @(tracksInfo[i]->i_level),
  600. VLCMediaTracksInformationCodecLevel,
  601. @(tracksInfo[i]->i_bitrate),
  602. VLCMediaTracksInformationBitrate,
  603. nil];
  604. if (tracksInfo[i]->psz_language)
  605. dictionary[VLCMediaTracksInformationLanguage] = [NSString stringWithUTF8String:tracksInfo[i]->psz_language];
  606. if (tracksInfo[i]->psz_description)
  607. dictionary[VLCMediaTracksInformationDescription] = [NSString stringWithUTF8String:tracksInfo[i]->psz_description];
  608. NSString *type;
  609. switch (tracksInfo[i]->i_type) {
  610. case libvlc_track_audio:
  611. type = VLCMediaTracksInformationTypeAudio;
  612. dictionary[VLCMediaTracksInformationAudioChannelsNumber] = @(tracksInfo[i]->audio->i_channels);
  613. dictionary[VLCMediaTracksInformationAudioRate] = @(tracksInfo[i]->audio->i_rate);
  614. break;
  615. case libvlc_track_video:
  616. type = VLCMediaTracksInformationTypeVideo;
  617. dictionary[VLCMediaTracksInformationVideoWidth] = @(tracksInfo[i]->video->i_width);
  618. dictionary[VLCMediaTracksInformationVideoHeight] = @(tracksInfo[i]->video->i_height);
  619. dictionary[VLCMediaTracksInformationVideoOrientation] = @(tracksInfo[i]->video->i_orientation);
  620. dictionary[VLCMediaTracksInformationVideoProjection] = @(tracksInfo[i]->video->i_projection);
  621. dictionary[VLCMediaTracksInformationSourceAspectRatio] = @(tracksInfo[i]->video->i_sar_num);
  622. dictionary[VLCMediaTracksInformationSourceAspectRatioDenominator] = @(tracksInfo[i]->video->i_sar_den);
  623. dictionary[VLCMediaTracksInformationFrameRate] = @(tracksInfo[i]->video->i_frame_rate_num);
  624. dictionary[VLCMediaTracksInformationFrameRateDenominator] = @(tracksInfo[i]->video->i_frame_rate_den);
  625. break;
  626. case libvlc_track_text:
  627. type = VLCMediaTracksInformationTypeText;
  628. if (tracksInfo[i]->subtitle->psz_encoding)
  629. dictionary[VLCMediaTracksInformationTextEncoding] = [NSString stringWithUTF8String: tracksInfo[i]->subtitle->psz_encoding];
  630. break;
  631. case libvlc_track_unknown:
  632. default:
  633. type = VLCMediaTracksInformationTypeUnknown;
  634. break;
  635. }
  636. [dictionary setValue:type forKey:VLCMediaTracksInformationType];
  637. [array addObject:dictionary];
  638. }
  639. libvlc_media_tracks_release(tracksInfo, count);
  640. return array;
  641. }
  642. - (BOOL)isMediaSizeSuitableForDevice
  643. {
  644. #if TARGET_OS_IPHONE
  645. // Trigger parsing if needed
  646. VLCMediaParsedStatus parsedStatus = [self parsedStatus];
  647. if (parsedStatus == VLCMediaParsedStatusSkipped || parsedStatus == VLCMediaParsedStatusInit) {
  648. [self parseWithOptions:VLCMediaParseLocal|VLCMediaParseNetwork];
  649. sleep(2);
  650. }
  651. NSUInteger biggestWidth = 0;
  652. NSUInteger biggestHeight = 0;
  653. libvlc_media_track_t **tracksInfo;
  654. unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
  655. for (NSUInteger i = 0; i < count; i++) {
  656. switch (tracksInfo[i]->i_type) {
  657. case libvlc_track_video:
  658. if (tracksInfo[i]->video->i_width > biggestWidth)
  659. biggestWidth = tracksInfo[i]->video->i_width;
  660. if (tracksInfo[i]->video->i_height > biggestHeight)
  661. biggestHeight = tracksInfo[i]->video->i_height;
  662. break;
  663. default:
  664. break;
  665. }
  666. }
  667. if (biggestHeight > 0 && biggestWidth > 0) {
  668. size_t size;
  669. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  670. char *answer = malloc(size);
  671. sysctlbyname("hw.machine", answer, &size, NULL, 0);
  672. NSString *currentMachine = @(answer);
  673. free(answer);
  674. NSUInteger totalNumberOfPixels = biggestWidth * biggestHeight;
  675. if ([currentMachine hasPrefix:@"iPhone2"] || [currentMachine hasPrefix:@"iPhone3"] || [currentMachine hasPrefix:@"iPad1"] || [currentMachine hasPrefix:@"iPod3"] || [currentMachine hasPrefix:@"iPod4"]) {
  676. // iPhone 3GS, iPhone 4, first gen. iPad, 3rd and 4th generation iPod touch
  677. return (totalNumberOfPixels < 600000); // between 480p and 720p
  678. } else if ([currentMachine hasPrefix:@"iPhone4"] || [currentMachine hasPrefix:@"iPad3,1"] || [currentMachine hasPrefix:@"iPad3,2"] || [currentMachine hasPrefix:@"iPad3,3"] || [currentMachine hasPrefix:@"iPod4"] || [currentMachine hasPrefix:@"iPad2"] || [currentMachine hasPrefix:@"iPod5"]) {
  679. // iPhone 4S, iPad 2 and 3, iPod 4 and 5
  680. return (totalNumberOfPixels < 922000); // 720p
  681. } else {
  682. // iPhone 5, iPad 4
  683. return (totalNumberOfPixels < 2074000); // 1080p
  684. }
  685. }
  686. #endif
  687. return YES;
  688. }
  689. - (NSString *)metadataForKey:(NSString *)key
  690. {
  691. if (!p_md)
  692. return nil;
  693. char *returnValue = libvlc_media_get_meta(p_md, [VLCMedia stringToMetaType:key]);
  694. if (!returnValue)
  695. return nil;
  696. NSString *actualReturnValue = @(returnValue);
  697. free(returnValue);
  698. return actualReturnValue;
  699. }
  700. - (void)setMetadata:(NSString *)data forKey:(NSString *)key
  701. {
  702. if (!p_md)
  703. return;
  704. libvlc_media_set_meta(p_md, [VLCMedia stringToMetaType:key], [data UTF8String]);
  705. }
  706. - (BOOL)saveMetadata
  707. {
  708. if (p_md)
  709. return libvlc_media_save_meta(p_md) != 0;
  710. return NO;
  711. }
  712. /******************************************************************************
  713. * Implementation VLCMedia ()
  714. */
  715. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  716. {
  717. static NSDictionary * stringToMetaDictionary = nil;
  718. // TODO: Thread safe-ize
  719. if (!stringToMetaDictionary) {
  720. #define VLCStringToMeta( name ) [NSNumber numberWithInt: libvlc_meta_##name], VLCMetaInformation##name
  721. stringToMetaDictionary =
  722. [NSDictionary dictionaryWithObjectsAndKeys:
  723. VLCStringToMeta(Title),
  724. VLCStringToMeta(Artist),
  725. VLCStringToMeta(Genre),
  726. VLCStringToMeta(Copyright),
  727. VLCStringToMeta(Album),
  728. VLCStringToMeta(TrackNumber),
  729. VLCStringToMeta(Description),
  730. VLCStringToMeta(Rating),
  731. VLCStringToMeta(Date),
  732. VLCStringToMeta(Setting),
  733. VLCStringToMeta(URL),
  734. VLCStringToMeta(Language),
  735. VLCStringToMeta(NowPlaying),
  736. VLCStringToMeta(Publisher),
  737. VLCStringToMeta(ArtworkURL),
  738. VLCStringToMeta(TrackID),
  739. VLCStringToMeta(TrackTotal),
  740. VLCStringToMeta(Director),
  741. VLCStringToMeta(Season),
  742. VLCStringToMeta(Episode),
  743. VLCStringToMeta(ShowName),
  744. VLCStringToMeta(Actors),
  745. VLCStringToMeta(AlbumArtist),
  746. VLCStringToMeta(DiscNumber),
  747. nil];
  748. #undef VLCStringToMeta
  749. }
  750. NSNumber * number = stringToMetaDictionary[string];
  751. return (libvlc_meta_t) (number ? [number intValue] : -1);
  752. }
  753. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  754. {
  755. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  756. VLCMetaToString(Title, type);
  757. VLCMetaToString(Artist, type);
  758. VLCMetaToString(Genre, type);
  759. VLCMetaToString(Copyright, type);
  760. VLCMetaToString(Album, type);
  761. VLCMetaToString(TrackNumber, type);
  762. VLCMetaToString(Description, type);
  763. VLCMetaToString(Rating, type);
  764. VLCMetaToString(Date, type);
  765. VLCMetaToString(Setting, type);
  766. VLCMetaToString(URL, type);
  767. VLCMetaToString(Language, type);
  768. VLCMetaToString(NowPlaying, type);
  769. VLCMetaToString(Publisher, type);
  770. VLCMetaToString(ArtworkURL, type);
  771. VLCMetaToString(TrackID, type);
  772. VLCMetaToString(TrackTotal, type);
  773. VLCMetaToString(Director, type);
  774. VLCMetaToString(Season, type);
  775. VLCMetaToString(Episode, type);
  776. VLCMetaToString(ShowName, type);
  777. VLCMetaToString(Actors, type);
  778. VLCMetaToString(AlbumArtist, type);
  779. VLCMetaToString(DiscNumber, type);
  780. #undef VLCMetaToString
  781. return nil;
  782. }
  783. - (void)initInternalMediaDescriptor
  784. {
  785. char * p_url = libvlc_media_get_mrl( p_md );
  786. if (!p_url)
  787. return;
  788. NSString *urlString = [NSString stringWithUTF8String:p_url];
  789. if (!urlString) {
  790. free(p_url);
  791. return;
  792. }
  793. urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  794. if (!urlString) {
  795. free(p_url);
  796. return;
  797. }
  798. _url = [NSURL URLWithString:urlString];
  799. if (!_url) /* Attempt to interpret as a file path then */ {
  800. _url = [NSURL fileURLWithPath:urlString];
  801. if(!_url) {
  802. free(p_url);
  803. return;
  804. }
  805. }
  806. free(p_url);
  807. libvlc_media_set_user_data(p_md, (__bridge void*)self);
  808. libvlc_event_manager_t * p_em = libvlc_media_event_manager( p_md );
  809. if (p_em) {
  810. libvlc_event_attach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, (__bridge void *)(self));
  811. libvlc_event_attach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, (__bridge void *)(self));
  812. libvlc_event_attach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, (__bridge void *)(self));
  813. libvlc_event_attach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, (__bridge void *)(self));
  814. libvlc_event_attach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, (__bridge void *)(self));
  815. }
  816. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  817. if (p_mlist) {
  818. self.subitems = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  819. libvlc_media_list_release( p_mlist );
  820. }
  821. self.state = LibVLCStateToMediaState(libvlc_media_get_state( p_md ));
  822. }
  823. - (void)fetchMetaInformationFromLibVLCWithType:(NSString *)metaType
  824. {
  825. char * psz_value = libvlc_media_get_meta( p_md, [VLCMedia stringToMetaType:metaType] );
  826. NSString * newValue = psz_value ? @(psz_value) : nil;
  827. NSString * oldValue = [_metaDictionary valueForKey:metaType];
  828. free(psz_value);
  829. if (newValue != oldValue && !(oldValue && newValue && [oldValue compare:newValue] == NSOrderedSame)) {
  830. #if !TARGET_OS_IPHONE
  831. // Only fetch the art if needed. (ie, create the NSImage, if it was requested before)
  832. if (isArtFetched && [metaType isEqualToString:VLCMetaInformationArtworkURL]) {
  833. [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
  834. toTarget:self
  835. withObject:newValue];
  836. }
  837. #endif
  838. [_metaDictionary setValue:newValue forKeyPath:metaType];
  839. }
  840. }
  841. #if !TARGET_OS_IPHONE
  842. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
  843. {
  844. @autoreleasepool {
  845. NSImage * art = nil;
  846. if (anURL) {
  847. // Go ahead and load up the art work
  848. NSURL * artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  849. // Don't attempt to fetch artwork from remote. Core will do that alone
  850. if ([artUrl isFileURL])
  851. art = [[NSImage alloc] initWithContentsOfURL:artUrl];
  852. }
  853. // If anything was found, lets save it to the meta data dictionary
  854. [self performSelectorOnMainThread:@selector(setArtwork:) withObject:art waitUntilDone:NO];
  855. }
  856. }
  857. - (void)setArtwork:(NSImage *)art
  858. {
  859. if (!art)
  860. [(NSMutableDictionary *)_metaDictionary removeObjectForKey:@"artwork"];
  861. else
  862. ((NSMutableDictionary *)_metaDictionary)[@"artwork"] = art;
  863. }
  864. #endif
  865. - (void)parseIfNeeded
  866. {
  867. VLCMediaParsedStatus parsedStatus = [self parsedStatus];
  868. if (parsedStatus == VLCMediaParsedStatusSkipped || parsedStatus == VLCMediaParsedStatusInit)
  869. [self parseWithOptions:VLCMediaParseLocal | VLCMediaFetchLocal];
  870. }
  871. - (void)metaChanged:(NSString *)metaType
  872. {
  873. [self fetchMetaInformationFromLibVLCWithType:metaType];
  874. if ([_delegate respondsToSelector:@selector(mediaMetaDataDidChange:)])
  875. [_delegate mediaMetaDataDidChange:self];
  876. }
  877. - (void)subItemAdded
  878. {
  879. if (_subitems)
  880. return; /* Nothing to do */
  881. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  882. NSAssert( p_mlist, @"The mlist shouldn't be nil, we are receiving a subItemAdded");
  883. self.subitems = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  884. libvlc_media_list_release( p_mlist );
  885. }
  886. - (void)parsedChanged:(NSNumber *)isParsedAsNumber
  887. {
  888. [self willChangeValueForKey:@"parsedStatus"];
  889. [self parsedStatus];
  890. [self didChangeValueForKey:@"parsedStatus"];
  891. if (!_delegate)
  892. return;
  893. if ([_delegate respondsToSelector:@selector(mediaDidFinishParsing:)])
  894. [_delegate mediaDidFinishParsing:self];
  895. }
  896. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber
  897. {
  898. [self setState: [newStateAsNumber intValue]];
  899. }
  900. #if TARGET_OS_IPHONE
  901. - (NSDictionary *)metaDictionary
  902. {
  903. if (!areOthersMetaFetched) {
  904. areOthersMetaFetched = YES;
  905. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  906. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtist];
  907. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationAlbum];
  908. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationDate];
  909. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationGenre];
  910. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTrackNumber];
  911. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationDiscNumber];
  912. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationNowPlaying];
  913. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationLanguage];
  914. }
  915. if (!isArtURLFetched) {
  916. isArtURLFetched = YES;
  917. /* Force isArtURLFetched, that will trigger artwork download eventually
  918. * And all the other meta will be added through the libvlc event system */
  919. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  920. }
  921. return [NSDictionary dictionaryWithDictionary:_metaDictionary];
  922. }
  923. #else
  924. - (NSDictionary *)metaDictionary
  925. {
  926. return [NSDictionary dictionaryWithDictionary:_metaDictionary];
  927. }
  928. - (id)valueForKeyPath:(NSString *)keyPath
  929. {
  930. if (!isArtFetched && [keyPath isEqualToString:@"metaDictionary.artwork"]) {
  931. isArtFetched = YES;
  932. /* Force the retrieval of the artwork now that someone asked for it */
  933. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  934. } else if (!areOthersMetaFetched && [keyPath hasPrefix:@"metaDictionary."]) {
  935. areOthersMetaFetched = YES;
  936. /* Force VLCMetaInformationTitle, that will trigger preparsing
  937. * And all the other meta will be added through the libvlc event system */
  938. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  939. } else if (!isArtURLFetched && [keyPath hasPrefix:@"metaDictionary.artworkURL"]) {
  940. isArtURLFetched = YES;
  941. /* Force isArtURLFetched, that will trigger artwork download eventually
  942. * And all the other meta will be added through the libvlc event system */
  943. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  944. }
  945. return [super valueForKeyPath:keyPath];
  946. }
  947. #endif
  948. @end
  949. /******************************************************************************
  950. * Implementation VLCMedia (LibVLCBridging)
  951. */
  952. @implementation VLCMedia (LibVLCBridging)
  953. + (id)mediaWithLibVLCMediaDescriptor:(void *)md
  954. {
  955. return [[VLCMedia alloc] initWithLibVLCMediaDescriptor:md];
  956. }
  957. + (id)mediaWithMedia:(VLCMedia *)media andLibVLCOptions:(NSDictionary *)options
  958. {
  959. libvlc_media_t * p_md;
  960. p_md = libvlc_media_duplicate([media libVLCMediaDescriptor]);
  961. for (NSString * key in [options allKeys]) {
  962. if (options[key] != [NSNull null])
  963. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, options[key]] UTF8String]);
  964. else
  965. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
  966. }
  967. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  968. }
  969. - (id)initWithLibVLCMediaDescriptor:(void *)md
  970. {
  971. if (self = [super init]) {
  972. libvlc_media_retain(md);
  973. p_md = md;
  974. _metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  975. [self initInternalMediaDescriptor];
  976. }
  977. return self;
  978. }
  979. - (void *)libVLCMediaDescriptor
  980. {
  981. return p_md;
  982. }
  983. @end