VLCMedia.m 40 KB

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