VLCMedia.m 40 KB

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