VLCMedia.m 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. - (BOOL)isEqual:(id)other
  313. {
  314. return ([other isKindOfClass: [VLCMedia class]] &&
  315. [other libVLCMediaDescriptor] == p_md);
  316. }
  317. - (VLCTime *)length
  318. {
  319. if (!_length) {
  320. // Try figuring out what the length is
  321. long long duration = libvlc_media_get_duration( p_md );
  322. if (duration < 0)
  323. return [VLCTime nullTime];
  324. _length = [VLCTime timeWithNumber:@(duration)];
  325. }
  326. return _length;
  327. }
  328. - (VLCTime *)lengthWaitUntilDate:(NSDate *)aDate
  329. {
  330. static const long long thread_sleep = 10000;
  331. if (!_length) {
  332. // Force parsing of this item.
  333. [self parseIfNeeded];
  334. // wait until we are preparsed
  335. libvlc_media_parsed_status_t status = libvlc_media_get_parsed_status(p_md);
  336. while (!_length && !(status == VLCMediaParsedStatusFailed || status == VLCMediaParsedStatusDone) && [aDate timeIntervalSinceNow] > 0) {
  337. usleep( thread_sleep );
  338. status = libvlc_media_get_parsed_status(p_md);
  339. }
  340. // So we're done waiting, but sometimes we trap the fact that the parsing
  341. // was done before the length gets assigned, so lets go ahead and assign
  342. // it ourselves.
  343. if (!_length)
  344. return [self length];
  345. }
  346. return _length;
  347. }
  348. - (BOOL)isParsed
  349. {
  350. VLCMediaParsedStatus status = [self parsedStatus];
  351. return (status == VLCMediaParsedStatusFailed || status == VLCMediaParsedStatusDone) ? YES:NO;
  352. }
  353. - (VLCMediaParsedStatus)parsedStatus
  354. {
  355. if (!p_md)
  356. return VLCMediaParsedStatusFailed;
  357. libvlc_media_parsed_status_t status = libvlc_media_get_parsed_status(p_md);
  358. return (VLCMediaParsedStatus)status;
  359. }
  360. - (void)parse
  361. {
  362. if (p_md)
  363. libvlc_media_parse_async(p_md);
  364. }
  365. - (void)synchronousParse
  366. {
  367. if (p_md)
  368. libvlc_media_parse(p_md);
  369. }
  370. - (int)parseWithOptions:(VLCMediaParsingOptions)options timeout:(int)timeoutValue
  371. {
  372. if (!p_md)
  373. return -1;
  374. // we are using the default time-out value
  375. return libvlc_media_parse_with_options(p_md,
  376. options,
  377. timeoutValue);
  378. }
  379. - (int)parseWithOptions:(VLCMediaParsingOptions)options
  380. {
  381. if (!p_md)
  382. return -1;
  383. // we are using the default time-out value
  384. return libvlc_media_parse_with_options(p_md,
  385. options,
  386. -1);
  387. }
  388. - (void)addOptions:(NSDictionary*)options
  389. {
  390. if (p_md) {
  391. [options enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  392. if (![obj isKindOfClass:[NSNull class]])
  393. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, obj] UTF8String]);
  394. else
  395. libvlc_media_add_option(p_md, [key UTF8String]);
  396. }];
  397. }
  398. }
  399. - (int)storeCookie:(NSString * _Nonnull)cookie
  400. forHost:(NSString *_Nonnull)host
  401. path:(NSString *_Nonnull)path
  402. {
  403. if (!p_md || cookie == NULL || host == NULL || path == NULL) {
  404. return -1;
  405. }
  406. #if TARGET_OS_IPHONE
  407. return libvlc_media_cookie_jar_store(p_md,
  408. [cookie UTF8String],
  409. [host UTF8String],
  410. [path UTF8String]);
  411. #else
  412. return -1;
  413. #endif
  414. }
  415. - (void)clearStoredCookies
  416. {
  417. if (!p_md) {
  418. return;
  419. }
  420. #if TARGET_OS_IPHONE
  421. libvlc_media_cookie_jar_clear(p_md);
  422. #endif
  423. }
  424. - (NSDictionary *)stats
  425. {
  426. if (!p_md)
  427. return nil;
  428. libvlc_media_stats_t p_stats;
  429. libvlc_media_get_stats(p_md, &p_stats);
  430. return @{
  431. @"demuxBitrate" : @(p_stats.f_demux_bitrate),
  432. @"inputBitrate" : @(p_stats.f_input_bitrate),
  433. @"sendBitrate" : @(p_stats.f_send_bitrate),
  434. @"decodedAudio" : @(p_stats.i_decoded_audio),
  435. @"decodedVideo" : @(p_stats.i_decoded_video),
  436. @"demuxCorrupted" : @(p_stats.i_demux_corrupted),
  437. @"demuxDiscontinuity" : @(p_stats.i_demux_discontinuity),
  438. @"demuxReadBytes" : @(p_stats.i_demux_read_bytes),
  439. @"displayedPictures" : @(p_stats.i_displayed_pictures),
  440. @"lostAbuffers" : @(p_stats.i_lost_abuffers),
  441. @"lostPictures" : @(p_stats.i_lost_pictures),
  442. @"playedAbuffers" : @(p_stats.i_played_abuffers),
  443. @"readBytes" : @(p_stats.i_read_bytes),
  444. @"sentBytes" : @(p_stats.i_sent_bytes),
  445. @"sentPackets" : @(p_stats.i_sent_packets)
  446. };
  447. }
  448. - (NSInteger)numberOfReadBytesOnInput
  449. {
  450. if (!p_md)
  451. return 0;
  452. libvlc_media_stats_t p_stats;
  453. libvlc_media_get_stats(p_md, &p_stats);
  454. return p_stats.i_read_bytes;
  455. }
  456. - (float)inputBitrate
  457. {
  458. if (!p_md)
  459. return .0;
  460. libvlc_media_stats_t p_stats;
  461. libvlc_media_get_stats(p_md, &p_stats);
  462. return p_stats.f_input_bitrate;
  463. }
  464. - (NSInteger)numberOfReadBytesOnDemux
  465. {
  466. if (!p_md)
  467. return 0;
  468. libvlc_media_stats_t p_stats;
  469. libvlc_media_get_stats(p_md, &p_stats);
  470. return p_stats.i_demux_read_bytes;
  471. }
  472. - (float)demuxBitrate
  473. {
  474. if (!p_md)
  475. return .0;
  476. libvlc_media_stats_t p_stats;
  477. libvlc_media_get_stats(p_md, &p_stats);
  478. return p_stats.f_demux_bitrate;
  479. }
  480. - (NSInteger)numberOfDecodedVideoBlocks
  481. {
  482. if (!p_md)
  483. return 0;
  484. libvlc_media_stats_t p_stats;
  485. libvlc_media_get_stats(p_md, &p_stats);
  486. return p_stats.i_decoded_video;
  487. }
  488. - (NSInteger)numberOfDecodedAudioBlocks
  489. {
  490. if (!p_md)
  491. return 0;
  492. libvlc_media_stats_t p_stats;
  493. libvlc_media_get_stats(p_md, &p_stats);
  494. return p_stats.i_decoded_audio;
  495. }
  496. - (NSInteger)numberOfDisplayedPictures
  497. {
  498. if (!p_md)
  499. return 0;
  500. libvlc_media_stats_t p_stats;
  501. libvlc_media_get_stats(p_md, &p_stats);
  502. return p_stats.i_displayed_pictures;
  503. }
  504. - (NSInteger)numberOfLostPictures
  505. {
  506. if (!p_md)
  507. return 0;
  508. libvlc_media_stats_t p_stats;
  509. libvlc_media_get_stats(p_md, &p_stats);
  510. return p_stats.i_lost_pictures;
  511. }
  512. - (NSInteger)numberOfPlayedAudioBuffers
  513. {
  514. if (!p_md)
  515. return 0;
  516. libvlc_media_stats_t p_stats;
  517. libvlc_media_get_stats(p_md, &p_stats);
  518. return p_stats.i_played_abuffers;
  519. }
  520. - (NSInteger)numberOfLostAudioBuffers
  521. {
  522. if (!p_md)
  523. return 0;
  524. libvlc_media_stats_t p_stats;
  525. libvlc_media_get_stats(p_md, &p_stats);
  526. return p_stats.i_lost_abuffers;
  527. }
  528. - (NSInteger)numberOfSentPackets
  529. {
  530. if (!p_md)
  531. return 0;
  532. libvlc_media_stats_t p_stats;
  533. libvlc_media_get_stats(p_md, &p_stats);
  534. return p_stats.i_sent_packets;
  535. }
  536. - (NSInteger)numberOfSentBytes
  537. {
  538. if (!p_md)
  539. return 0;
  540. libvlc_media_stats_t p_stats;
  541. libvlc_media_get_stats(p_md, &p_stats);
  542. return p_stats.i_sent_bytes;
  543. }
  544. - (float)streamOutputBitrate
  545. {
  546. if (!p_md)
  547. return .0;
  548. libvlc_media_stats_t p_stats;
  549. libvlc_media_get_stats(p_md, &p_stats);
  550. return p_stats.f_send_bitrate;
  551. }
  552. - (NSInteger)numberOfCorruptedDataPackets
  553. {
  554. if (!p_md)
  555. return 0;
  556. libvlc_media_stats_t p_stats;
  557. libvlc_media_get_stats(p_md, &p_stats);
  558. return p_stats.i_demux_corrupted;
  559. }
  560. - (NSInteger)numberOfDiscontinuties
  561. {
  562. if (!p_md)
  563. return 0;
  564. libvlc_media_stats_t p_stats;
  565. libvlc_media_get_stats(p_md, &p_stats);
  566. return p_stats.i_demux_discontinuity;
  567. }
  568. NSString *const VLCMediaTracksInformationCodec = @"codec"; // NSNumber
  569. NSString *const VLCMediaTracksInformationId = @"id"; // NSNumber
  570. NSString *const VLCMediaTracksInformationType = @"type"; // NSString
  571. NSString *const VLCMediaTracksInformationCodecProfile = @"profile"; // NSNumber
  572. NSString *const VLCMediaTracksInformationCodecLevel = @"level"; // NSNumber
  573. NSString *const VLCMediaTracksInformationTypeAudio = @"audio";
  574. NSString *const VLCMediaTracksInformationTypeVideo = @"video";
  575. NSString *const VLCMediaTracksInformationTypeText = @"text";
  576. NSString *const VLCMediaTracksInformationTypeUnknown = @"unknown";
  577. NSString *const VLCMediaTracksInformationBitrate = @"bitrate"; // NSNumber
  578. NSString *const VLCMediaTracksInformationLanguage = @"language"; // NSString
  579. NSString *const VLCMediaTracksInformationDescription = @"description"; // NSString
  580. NSString *const VLCMediaTracksInformationAudioChannelsNumber = @"channelsNumber"; // NSNumber
  581. NSString *const VLCMediaTracksInformationAudioRate = @"rate"; // NSNumber
  582. NSString *const VLCMediaTracksInformationVideoHeight = @"height"; // NSNumber
  583. NSString *const VLCMediaTracksInformationVideoWidth = @"width"; // NSNumber
  584. NSString *const VLCMediaTracksInformationVideoOrientation = @"orientation"; // NSNumber
  585. NSString *const VLCMediaTracksInformationVideoProjection = @"projection"; // NSNumber
  586. NSString *const VLCMediaTracksInformationSourceAspectRatio = @"sar_num"; // NSNumber
  587. NSString *const VLCMediaTracksInformationSourceAspectRatioDenominator = @"sar_den"; // NSNumber
  588. NSString *const VLCMediaTracksInformationFrameRate = @"frame_rate_num"; // NSNumber
  589. NSString *const VLCMediaTracksInformationFrameRateDenominator = @"frame_rate_den"; // NSNumber
  590. NSString *const VLCMediaTracksInformationTextEncoding = @"encoding"; // NSString
  591. - (NSArray *)tracksInformation
  592. {
  593. libvlc_media_track_t **tracksInfo;
  594. unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
  595. NSMutableArray *array = [NSMutableArray array];
  596. for (NSUInteger i = 0; i < count; i++) {
  597. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  598. @(tracksInfo[i]->i_codec),
  599. VLCMediaTracksInformationCodec,
  600. @(tracksInfo[i]->i_id),
  601. VLCMediaTracksInformationId,
  602. @(tracksInfo[i]->i_profile),
  603. VLCMediaTracksInformationCodecProfile,
  604. @(tracksInfo[i]->i_level),
  605. VLCMediaTracksInformationCodecLevel,
  606. @(tracksInfo[i]->i_bitrate),
  607. VLCMediaTracksInformationBitrate,
  608. nil];
  609. if (tracksInfo[i]->psz_language)
  610. dictionary[VLCMediaTracksInformationLanguage] = [NSString stringWithUTF8String:tracksInfo[i]->psz_language];
  611. if (tracksInfo[i]->psz_description)
  612. dictionary[VLCMediaTracksInformationDescription] = [NSString stringWithUTF8String:tracksInfo[i]->psz_description];
  613. NSString *type;
  614. switch (tracksInfo[i]->i_type) {
  615. case libvlc_track_audio:
  616. type = VLCMediaTracksInformationTypeAudio;
  617. dictionary[VLCMediaTracksInformationAudioChannelsNumber] = @(tracksInfo[i]->audio->i_channels);
  618. dictionary[VLCMediaTracksInformationAudioRate] = @(tracksInfo[i]->audio->i_rate);
  619. break;
  620. case libvlc_track_video:
  621. type = VLCMediaTracksInformationTypeVideo;
  622. dictionary[VLCMediaTracksInformationVideoWidth] = @(tracksInfo[i]->video->i_width);
  623. dictionary[VLCMediaTracksInformationVideoHeight] = @(tracksInfo[i]->video->i_height);
  624. dictionary[VLCMediaTracksInformationVideoOrientation] = @(tracksInfo[i]->video->i_orientation);
  625. dictionary[VLCMediaTracksInformationVideoProjection] = @(tracksInfo[i]->video->i_projection);
  626. dictionary[VLCMediaTracksInformationSourceAspectRatio] = @(tracksInfo[i]->video->i_sar_num);
  627. dictionary[VLCMediaTracksInformationSourceAspectRatioDenominator] = @(tracksInfo[i]->video->i_sar_den);
  628. dictionary[VLCMediaTracksInformationFrameRate] = @(tracksInfo[i]->video->i_frame_rate_num);
  629. dictionary[VLCMediaTracksInformationFrameRateDenominator] = @(tracksInfo[i]->video->i_frame_rate_den);
  630. break;
  631. case libvlc_track_text:
  632. type = VLCMediaTracksInformationTypeText;
  633. if (tracksInfo[i]->subtitle->psz_encoding)
  634. dictionary[VLCMediaTracksInformationTextEncoding] = [NSString stringWithUTF8String: tracksInfo[i]->subtitle->psz_encoding];
  635. break;
  636. case libvlc_track_unknown:
  637. default:
  638. type = VLCMediaTracksInformationTypeUnknown;
  639. break;
  640. }
  641. [dictionary setValue:type forKey:VLCMediaTracksInformationType];
  642. [array addObject:dictionary];
  643. }
  644. libvlc_media_tracks_release(tracksInfo, count);
  645. return array;
  646. }
  647. - (BOOL)isMediaSizeSuitableForDevice
  648. {
  649. #if TARGET_OS_IPHONE
  650. // Trigger parsing if needed
  651. VLCMediaParsedStatus parsedStatus = [self parsedStatus];
  652. if (parsedStatus == VLCMediaParsedStatusSkipped || parsedStatus == VLCMediaParsedStatusInit) {
  653. [self parseWithOptions:VLCMediaParseLocal|VLCMediaParseNetwork];
  654. sleep(2);
  655. }
  656. NSUInteger biggestWidth = 0;
  657. NSUInteger biggestHeight = 0;
  658. libvlc_media_track_t **tracksInfo;
  659. unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
  660. for (NSUInteger i = 0; i < count; i++) {
  661. switch (tracksInfo[i]->i_type) {
  662. case libvlc_track_video:
  663. if (tracksInfo[i]->video->i_width > biggestWidth)
  664. biggestWidth = tracksInfo[i]->video->i_width;
  665. if (tracksInfo[i]->video->i_height > biggestHeight)
  666. biggestHeight = tracksInfo[i]->video->i_height;
  667. break;
  668. default:
  669. break;
  670. }
  671. }
  672. if (biggestHeight > 0 && biggestWidth > 0) {
  673. size_t size;
  674. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  675. char *answer = malloc(size);
  676. sysctlbyname("hw.machine", answer, &size, NULL, 0);
  677. NSString *currentMachine = @(answer);
  678. free(answer);
  679. NSUInteger totalNumberOfPixels = biggestWidth * biggestHeight;
  680. if ([currentMachine hasPrefix:@"iPhone2"] || [currentMachine hasPrefix:@"iPhone3"] || [currentMachine hasPrefix:@"iPad1"] || [currentMachine hasPrefix:@"iPod3"] || [currentMachine hasPrefix:@"iPod4"]) {
  681. // iPhone 3GS, iPhone 4, first gen. iPad, 3rd and 4th generation iPod touch
  682. return (totalNumberOfPixels < 600000); // between 480p and 720p
  683. } 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"]) {
  684. // iPhone 4S, iPad 2 and 3, iPod 4 and 5
  685. return (totalNumberOfPixels < 922000); // 720p
  686. } else {
  687. // iPhone 5, iPad 4
  688. return (totalNumberOfPixels < 2074000); // 1080p
  689. }
  690. }
  691. #endif
  692. return YES;
  693. }
  694. - (NSString *)metadataForKey:(NSString *)key
  695. {
  696. if (!p_md)
  697. return nil;
  698. char *returnValue = libvlc_media_get_meta(p_md, [VLCMedia stringToMetaType:key]);
  699. if (!returnValue)
  700. return nil;
  701. NSString *actualReturnValue = @(returnValue);
  702. free(returnValue);
  703. return actualReturnValue;
  704. }
  705. - (void)setMetadata:(NSString *)data forKey:(NSString *)key
  706. {
  707. if (!p_md)
  708. return;
  709. libvlc_media_set_meta(p_md, [VLCMedia stringToMetaType:key], [data UTF8String]);
  710. }
  711. - (BOOL)saveMetadata
  712. {
  713. if (p_md)
  714. return libvlc_media_save_meta(p_md) != 0;
  715. return NO;
  716. }
  717. /******************************************************************************
  718. * Implementation VLCMedia ()
  719. */
  720. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  721. {
  722. static NSDictionary * stringToMetaDictionary = nil;
  723. // TODO: Thread safe-ize
  724. if (!stringToMetaDictionary) {
  725. #define VLCStringToMeta( name ) [NSNumber numberWithInt: libvlc_meta_##name], VLCMetaInformation##name
  726. stringToMetaDictionary =
  727. [NSDictionary dictionaryWithObjectsAndKeys:
  728. VLCStringToMeta(Title),
  729. VLCStringToMeta(Artist),
  730. VLCStringToMeta(Genre),
  731. VLCStringToMeta(Copyright),
  732. VLCStringToMeta(Album),
  733. VLCStringToMeta(TrackNumber),
  734. VLCStringToMeta(Description),
  735. VLCStringToMeta(Rating),
  736. VLCStringToMeta(Date),
  737. VLCStringToMeta(Setting),
  738. VLCStringToMeta(URL),
  739. VLCStringToMeta(Language),
  740. VLCStringToMeta(NowPlaying),
  741. VLCStringToMeta(Publisher),
  742. VLCStringToMeta(ArtworkURL),
  743. VLCStringToMeta(TrackID),
  744. VLCStringToMeta(TrackTotal),
  745. VLCStringToMeta(Director),
  746. VLCStringToMeta(Season),
  747. VLCStringToMeta(Episode),
  748. VLCStringToMeta(ShowName),
  749. VLCStringToMeta(Actors),
  750. VLCStringToMeta(AlbumArtist),
  751. VLCStringToMeta(DiscNumber),
  752. nil];
  753. #undef VLCStringToMeta
  754. }
  755. NSNumber * number = stringToMetaDictionary[string];
  756. return (libvlc_meta_t) (number ? [number intValue] : -1);
  757. }
  758. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  759. {
  760. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  761. VLCMetaToString(Title, type);
  762. VLCMetaToString(Artist, type);
  763. VLCMetaToString(Genre, type);
  764. VLCMetaToString(Copyright, type);
  765. VLCMetaToString(Album, type);
  766. VLCMetaToString(TrackNumber, type);
  767. VLCMetaToString(Description, type);
  768. VLCMetaToString(Rating, type);
  769. VLCMetaToString(Date, type);
  770. VLCMetaToString(Setting, type);
  771. VLCMetaToString(URL, type);
  772. VLCMetaToString(Language, type);
  773. VLCMetaToString(NowPlaying, type);
  774. VLCMetaToString(Publisher, type);
  775. VLCMetaToString(ArtworkURL, type);
  776. VLCMetaToString(TrackID, type);
  777. VLCMetaToString(TrackTotal, type);
  778. VLCMetaToString(Director, type);
  779. VLCMetaToString(Season, type);
  780. VLCMetaToString(Episode, type);
  781. VLCMetaToString(ShowName, type);
  782. VLCMetaToString(Actors, type);
  783. VLCMetaToString(AlbumArtist, type);
  784. VLCMetaToString(DiscNumber, type);
  785. #undef VLCMetaToString
  786. return nil;
  787. }
  788. - (void)initInternalMediaDescriptor
  789. {
  790. char * p_url = libvlc_media_get_mrl( p_md );
  791. if (!p_url)
  792. return;
  793. NSString *urlString = [NSString stringWithUTF8String:p_url];
  794. if (!urlString) {
  795. free(p_url);
  796. return;
  797. }
  798. urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  799. if (!urlString) {
  800. free(p_url);
  801. return;
  802. }
  803. _url = [NSURL URLWithString:urlString];
  804. if (!_url) /* Attempt to interpret as a file path then */ {
  805. _url = [NSURL fileURLWithPath:urlString];
  806. if(!_url) {
  807. free(p_url);
  808. return;
  809. }
  810. }
  811. free(p_url);
  812. libvlc_media_set_user_data(p_md, (__bridge void*)self);
  813. libvlc_event_manager_t * p_em = libvlc_media_event_manager( p_md );
  814. if (p_em) {
  815. libvlc_event_attach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, (__bridge void *)(self));
  816. libvlc_event_attach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, (__bridge void *)(self));
  817. libvlc_event_attach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, (__bridge void *)(self));
  818. libvlc_event_attach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, (__bridge void *)(self));
  819. libvlc_event_attach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, (__bridge void *)(self));
  820. }
  821. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  822. if (p_mlist) {
  823. self.subitems = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  824. libvlc_media_list_release( p_mlist );
  825. }
  826. self.state = LibVLCStateToMediaState(libvlc_media_get_state( p_md ));
  827. }
  828. - (void)fetchMetaInformationFromLibVLCWithType:(NSString *)metaType
  829. {
  830. char * psz_value = libvlc_media_get_meta( p_md, [VLCMedia stringToMetaType:metaType] );
  831. NSString * newValue = psz_value ? @(psz_value) : nil;
  832. NSString * oldValue = [_metaDictionary valueForKey:metaType];
  833. free(psz_value);
  834. if (newValue != oldValue && !(oldValue && newValue && [oldValue compare:newValue] == NSOrderedSame)) {
  835. #if !TARGET_OS_IPHONE
  836. // Only fetch the art if needed. (ie, create the NSImage, if it was requested before)
  837. if (isArtFetched && [metaType isEqualToString:VLCMetaInformationArtworkURL]) {
  838. [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
  839. toTarget:self
  840. withObject:newValue];
  841. }
  842. #endif
  843. [_metaDictionary setValue:newValue forKeyPath:metaType];
  844. }
  845. }
  846. #if !TARGET_OS_IPHONE
  847. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
  848. {
  849. @autoreleasepool {
  850. NSImage * art = nil;
  851. if (anURL) {
  852. // Go ahead and load up the art work
  853. NSURL * artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  854. // Don't attempt to fetch artwork from remote. Core will do that alone
  855. if ([artUrl isFileURL])
  856. art = [[NSImage alloc] initWithContentsOfURL:artUrl];
  857. }
  858. // If anything was found, lets save it to the meta data dictionary
  859. [self performSelectorOnMainThread:@selector(setArtwork:) withObject:art waitUntilDone:NO];
  860. }
  861. }
  862. - (void)setArtwork:(NSImage *)art
  863. {
  864. if (!art)
  865. [(NSMutableDictionary *)_metaDictionary removeObjectForKey:@"artwork"];
  866. else
  867. ((NSMutableDictionary *)_metaDictionary)[@"artwork"] = art;
  868. }
  869. #endif
  870. - (void)parseIfNeeded
  871. {
  872. VLCMediaParsedStatus parsedStatus = [self parsedStatus];
  873. if (parsedStatus == VLCMediaParsedStatusSkipped || parsedStatus == VLCMediaParsedStatusInit)
  874. [self parseWithOptions:VLCMediaParseLocal | VLCMediaFetchLocal];
  875. }
  876. - (void)metaChanged:(NSString *)metaType
  877. {
  878. [self fetchMetaInformationFromLibVLCWithType:metaType];
  879. if ([_delegate respondsToSelector:@selector(mediaMetaDataDidChange:)])
  880. [_delegate mediaMetaDataDidChange:self];
  881. }
  882. - (void)subItemAdded
  883. {
  884. if (_subitems)
  885. return; /* Nothing to do */
  886. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  887. NSAssert( p_mlist, @"The mlist shouldn't be nil, we are receiving a subItemAdded");
  888. self.subitems = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  889. libvlc_media_list_release( p_mlist );
  890. }
  891. - (void)parsedChanged:(NSNumber *)isParsedAsNumber
  892. {
  893. [self willChangeValueForKey:@"parsedStatus"];
  894. [self parsedStatus];
  895. [self didChangeValueForKey:@"parsedStatus"];
  896. if (!_delegate)
  897. return;
  898. if ([_delegate respondsToSelector:@selector(mediaDidFinishParsing:)])
  899. [_delegate mediaDidFinishParsing:self];
  900. }
  901. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber
  902. {
  903. [self setState: [newStateAsNumber intValue]];
  904. }
  905. #if TARGET_OS_IPHONE
  906. - (NSDictionary *)metaDictionary
  907. {
  908. if (!areOthersMetaFetched) {
  909. areOthersMetaFetched = YES;
  910. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  911. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtist];
  912. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationAlbum];
  913. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationDate];
  914. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationGenre];
  915. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTrackNumber];
  916. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationDiscNumber];
  917. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationNowPlaying];
  918. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationLanguage];
  919. }
  920. if (!isArtURLFetched) {
  921. isArtURLFetched = YES;
  922. /* Force isArtURLFetched, that will trigger artwork download eventually
  923. * And all the other meta will be added through the libvlc event system */
  924. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  925. }
  926. return [NSDictionary dictionaryWithDictionary:_metaDictionary];
  927. }
  928. #else
  929. - (NSDictionary *)metaDictionary
  930. {
  931. return [NSDictionary dictionaryWithDictionary:_metaDictionary];
  932. }
  933. - (id)valueForKeyPath:(NSString *)keyPath
  934. {
  935. if (!isArtFetched && [keyPath isEqualToString:@"metaDictionary.artwork"]) {
  936. isArtFetched = YES;
  937. /* Force the retrieval of the artwork now that someone asked for it */
  938. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  939. } else if (!areOthersMetaFetched && [keyPath hasPrefix:@"metaDictionary."]) {
  940. areOthersMetaFetched = YES;
  941. /* Force VLCMetaInformationTitle, that will trigger preparsing
  942. * And all the other meta will be added through the libvlc event system */
  943. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  944. } else if (!isArtURLFetched && [keyPath hasPrefix:@"metaDictionary.artworkURL"]) {
  945. isArtURLFetched = YES;
  946. /* Force isArtURLFetched, that will trigger artwork download eventually
  947. * And all the other meta will be added through the libvlc event system */
  948. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  949. }
  950. return [super valueForKeyPath:keyPath];
  951. }
  952. #endif
  953. @end
  954. /******************************************************************************
  955. * Implementation VLCMedia (LibVLCBridging)
  956. */
  957. @implementation VLCMedia (LibVLCBridging)
  958. + (id)mediaWithLibVLCMediaDescriptor:(void *)md
  959. {
  960. return [[VLCMedia alloc] initWithLibVLCMediaDescriptor:md];
  961. }
  962. + (id)mediaWithMedia:(VLCMedia *)media andLibVLCOptions:(NSDictionary *)options
  963. {
  964. libvlc_media_t * p_md;
  965. p_md = libvlc_media_duplicate([media libVLCMediaDescriptor]);
  966. for (NSString * key in [options allKeys]) {
  967. if (options[key] != [NSNull null])
  968. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, options[key]] UTF8String]);
  969. else
  970. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
  971. }
  972. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  973. }
  974. - (id)initWithLibVLCMediaDescriptor:(void *)md
  975. {
  976. if (self = [super init]) {
  977. libvlc_media_retain(md);
  978. p_md = md;
  979. _metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  980. [self initInternalMediaDescriptor];
  981. }
  982. return self;
  983. }
  984. - (void *)libVLCMediaDescriptor
  985. {
  986. return p_md;
  987. }
  988. @end