VLCMedia.m 40 KB

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