VLCMedia.m 40 KB

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