VLCMedia.m 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*****************************************************************************
  2. * VLCMedia.m: VLCKit.framework VLCMedia implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2013 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. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation; either version 2.1 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with this program; if not, write to the Free Software Foundation,
  24. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25. *****************************************************************************/
  26. #import "VLCMedia.h"
  27. #import "VLCMediaList.h"
  28. #import "VLCEventManager.h"
  29. #import "VLCLibrary.h"
  30. #import "VLCLibVLCBridging.h"
  31. #import <vlc/libvlc.h>
  32. #import <sys/sysctl.h> // for sysctlbyname
  33. /* Meta Dictionary Keys */
  34. NSString * VLCMetaInformationTitle = @"title";
  35. NSString * VLCMetaInformationArtist = @"artist";
  36. NSString * VLCMetaInformationGenre = @"genre";
  37. NSString * VLCMetaInformationCopyright = @"copyright";
  38. NSString * VLCMetaInformationAlbum = @"album";
  39. NSString * VLCMetaInformationTrackNumber = @"trackNumber";
  40. NSString * VLCMetaInformationDescription = @"description";
  41. NSString * VLCMetaInformationRating = @"rating";
  42. NSString * VLCMetaInformationDate = @"date";
  43. NSString * VLCMetaInformationSetting = @"setting";
  44. NSString * VLCMetaInformationURL = @"url";
  45. NSString * VLCMetaInformationLanguage = @"language";
  46. NSString * VLCMetaInformationNowPlaying = @"nowPlaying";
  47. NSString * VLCMetaInformationPublisher = @"publisher";
  48. NSString * VLCMetaInformationEncodedBy = @"encodedBy";
  49. NSString * VLCMetaInformationArtworkURL = @"artworkURL";
  50. NSString * VLCMetaInformationArtwork = @"artwork";
  51. NSString * VLCMetaInformationTrackID = @"trackID";
  52. /* Notification Messages */
  53. NSString * VLCMediaMetaChanged = @"VLCMediaMetaChanged";
  54. /******************************************************************************
  55. * @property (readwrite)
  56. */
  57. @interface VLCMedia ()
  58. @property (readwrite) VLCMediaState state;
  59. @end
  60. /******************************************************************************
  61. * Interface (Private)
  62. */
  63. // TODO: Documentation
  64. @interface VLCMedia (Private)
  65. /* Statics */
  66. + (libvlc_meta_t)stringToMetaType:(NSString *)string;
  67. + (NSString *)metaTypeToString:(libvlc_meta_t)type;
  68. /* Initializers */
  69. - (void)initInternalMediaDescriptor;
  70. /* Operations */
  71. - (void)fetchMetaInformationFromLibVLCWithType:(NSString*)metaType;
  72. #if !TARGET_OS_IPHONE
  73. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL;
  74. - (void)setArtwork:(NSImage *)art;
  75. #endif
  76. - (void)parseIfNeeded;
  77. /* Callback Methods */
  78. - (void)parsedChanged:(NSNumber *)isParsedAsNumber;
  79. - (void)metaChanged:(NSString *)metaType;
  80. - (void)subItemAdded;
  81. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber;
  82. @end
  83. static VLCMediaState libvlc_state_to_media_state[] =
  84. {
  85. [libvlc_NothingSpecial] = VLCMediaStateNothingSpecial,
  86. [libvlc_Stopped] = VLCMediaStateNothingSpecial,
  87. [libvlc_Opening] = VLCMediaStateNothingSpecial,
  88. [libvlc_Buffering] = VLCMediaStateBuffering,
  89. [libvlc_Ended] = VLCMediaStateNothingSpecial,
  90. [libvlc_Error] = VLCMediaStateError,
  91. [libvlc_Playing] = VLCMediaStatePlaying,
  92. [libvlc_Paused] = VLCMediaStatePlaying,
  93. };
  94. static inline VLCMediaState LibVLCStateToMediaState( libvlc_state_t state )
  95. {
  96. return libvlc_state_to_media_state[state];
  97. }
  98. /******************************************************************************
  99. * LibVLC Event Callback
  100. */
  101. static void HandleMediaMetaChanged(const libvlc_event_t * event, void * self)
  102. {
  103. if (event->u.media_meta_changed.meta_type == libvlc_meta_Publisher ||
  104. event->u.media_meta_changed.meta_type == libvlc_meta_NowPlaying) {
  105. /* Skip those meta. We don't really care about them for now.
  106. * And they occure a lot */
  107. return;
  108. }
  109. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  110. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  111. withMethod:@selector(metaChanged:)
  112. withArgumentAsObject:[VLCMedia metaTypeToString:event->u.media_meta_changed.meta_type]];
  113. [pool release];
  114. }
  115. static void HandleMediaDurationChanged(const libvlc_event_t * event, void * self)
  116. {
  117. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  118. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  119. withMethod:@selector(setLength:)
  120. withArgumentAsObject:[VLCTime timeWithNumber:
  121. @(event->u.media_duration_changed.new_duration)]];
  122. [pool release];
  123. }
  124. static void HandleMediaStateChanged(const libvlc_event_t * event, void * self)
  125. {
  126. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  127. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  128. withMethod:@selector(setStateAsNumber:)
  129. withArgumentAsObject:@(LibVLCStateToMediaState(event->u.media_state_changed.new_state))];
  130. [pool release];
  131. }
  132. static void HandleMediaSubItemAdded(const libvlc_event_t * event, void * self)
  133. {
  134. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  135. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  136. withMethod:@selector(subItemAdded)
  137. withArgumentAsObject:nil];
  138. [pool release];
  139. }
  140. static void HandleMediaParsedChanged(const libvlc_event_t * event, void * self)
  141. {
  142. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  143. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  144. withMethod:@selector(parsedChanged:)
  145. withArgumentAsObject:@((BOOL)event->u.media_parsed_changed.new_status)];
  146. [pool release];
  147. }
  148. /******************************************************************************
  149. * Implementation
  150. */
  151. @implementation VLCMedia
  152. + (id)mediaWithURL:(NSURL *)anURL;
  153. {
  154. return [[[VLCMedia alloc] initWithURL:anURL] autorelease];
  155. }
  156. + (id)mediaWithPath:(NSString *)aPath;
  157. {
  158. return [[[VLCMedia alloc] initWithPath:aPath] autorelease];
  159. }
  160. + (id)mediaAsNodeWithName:(NSString *)aName;
  161. {
  162. return [[[VLCMedia alloc] initAsNodeWithName:aName] autorelease];
  163. }
  164. - (id)initWithPath:(NSString *)aPath
  165. {
  166. return [self initWithURL:[NSURL fileURLWithPath:aPath isDirectory:NO]];
  167. }
  168. - (id)initWithURL:(NSURL *)anURL
  169. {
  170. if (self = [super init]) {
  171. p_md = libvlc_media_new_location([VLCLibrary sharedInstance],
  172. [[anURL absoluteString] UTF8String]);
  173. delegate = nil;
  174. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  175. // This value is set whenever the demuxer figures out what the length is.
  176. // TODO: Easy way to tell the length of the movie without having to instiate the demuxer. Maybe cached info?
  177. length = nil;
  178. [self initInternalMediaDescriptor];
  179. }
  180. return self;
  181. }
  182. - (id)initAsNodeWithName:(NSString *)aName
  183. {
  184. if (self = [super init]) {
  185. p_md = libvlc_media_new_as_node([VLCLibrary sharedInstance],
  186. [aName UTF8String]);
  187. delegate = nil;
  188. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  189. // This value is set whenever the demuxer figures out what the length is.
  190. // TODO: Easy way to tell the length of the movie without having to instiate the demuxer. Maybe cached info?
  191. length = nil;
  192. [self initInternalMediaDescriptor];
  193. }
  194. return self;
  195. }
  196. - (void)dealloc
  197. {
  198. libvlc_event_manager_t * p_em = libvlc_media_event_manager(p_md);
  199. libvlc_event_detach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, self);
  200. libvlc_event_detach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, self);
  201. libvlc_event_detach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, self);
  202. libvlc_event_detach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, self);
  203. libvlc_event_detach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, self);
  204. [[VLCEventManager sharedManager] cancelCallToObject:self];
  205. // Testing to see if the pointer exists is not required, if the pointer is null
  206. // then the release message is not sent to it.
  207. delegate = nil;
  208. [length release];
  209. [url release];
  210. [subitems release];
  211. [metaDictionary release];
  212. libvlc_media_release( p_md );
  213. [super dealloc];
  214. }
  215. - (NSString *)description
  216. {
  217. NSString * result = metaDictionary[VLCMetaInformationTitle];
  218. return [NSString stringWithFormat:@"<%@ %p> %@", [self class], self, (result ? result : [url absoluteString])];
  219. }
  220. - (NSComparisonResult)compare:(VLCMedia *)media
  221. {
  222. if (self == media)
  223. return NSOrderedSame;
  224. if (!media)
  225. return NSOrderedDescending;
  226. return p_md == [media libVLCMediaDescriptor] ? NSOrderedSame : NSOrderedAscending;
  227. }
  228. @synthesize delegate;
  229. - (VLCTime *)length
  230. {
  231. if (!length) {
  232. // Try figuring out what the length is
  233. long long duration = libvlc_media_get_duration( p_md );
  234. if (duration > -1) {
  235. length = [[VLCTime timeWithNumber:@(duration)] retain];
  236. return [[length retain] autorelease];
  237. }
  238. return [VLCTime nullTime];
  239. }
  240. return [[length retain] autorelease];
  241. }
  242. - (VLCTime *)lengthWaitUntilDate:(NSDate *)aDate
  243. {
  244. static const long long thread_sleep = 10000;
  245. if (!length) {
  246. // Force parsing of this item.
  247. [self parseIfNeeded];
  248. // wait until we are preparsed
  249. while (!length && !libvlc_media_is_parsed(p_md) && [aDate timeIntervalSinceNow] > 0)
  250. usleep( thread_sleep );
  251. // So we're done waiting, but sometimes we trap the fact that the parsing
  252. // was done before the length gets assigned, so lets go ahead and assign
  253. // it ourselves.
  254. if (!length)
  255. return [self length];
  256. }
  257. return [[length retain] autorelease];
  258. }
  259. - (BOOL)isParsed
  260. {
  261. return isParsed;
  262. }
  263. - (void)parse
  264. {
  265. if (p_md)
  266. libvlc_media_parse_async(p_md);
  267. }
  268. - (void)synchronousParse
  269. {
  270. if (p_md)
  271. libvlc_media_parse(p_md);
  272. }
  273. - (void)addOptions:(NSDictionary*)options
  274. {
  275. if (p_md) {
  276. for (NSString * key in [options allKeys]) {
  277. if (options[key] != [NSNull null])
  278. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, options[key]] UTF8String]);
  279. else
  280. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
  281. }
  282. }
  283. }
  284. - (NSDictionary*) stats
  285. {
  286. if (!p_md)
  287. return NULL;
  288. NSMutableDictionary *d = [NSMutableDictionary dictionary];
  289. libvlc_media_stats_t p_stats;
  290. libvlc_media_get_stats(p_md, &p_stats);
  291. d[@"demuxBitrate"] = @(p_stats.f_demux_bitrate);
  292. d[@"inputBitrate"] = @(p_stats.f_input_bitrate);
  293. d[@"sendBitrate"] = @(p_stats.f_send_bitrate);
  294. d[@"decodedAudio"] = @(p_stats.i_decoded_audio);
  295. d[@"decodedVideo"] = @(p_stats.i_decoded_video);
  296. d[@"demuxCorrupted"] = @(p_stats.i_demux_corrupted);
  297. d[@"demuxDiscontinuity"] = @(p_stats.i_demux_discontinuity);
  298. d[@"demuxReadBytes"] = @(p_stats.i_demux_read_bytes);
  299. d[@"displayedPictures"] = @(p_stats.i_displayed_pictures);
  300. d[@"lostAbuffers"] = @(p_stats.i_lost_abuffers);
  301. d[@"lostPictures"] = @(p_stats.i_lost_pictures);
  302. d[@"playedAbuffers"] = @(p_stats.i_played_abuffers);
  303. d[@"readBytes"] = @(p_stats.i_read_bytes);
  304. d[@"sentBytes"] = @(p_stats.i_sent_bytes);
  305. d[@"sentPackets"] = @(p_stats.i_sent_packets);
  306. return d;
  307. }
  308. - (NSInteger)numberOfReadBytesOnInput
  309. {
  310. if (!p_md)
  311. return 0;
  312. libvlc_media_stats_t p_stats;
  313. libvlc_media_get_stats(p_md, &p_stats);
  314. return p_stats.i_read_bytes;
  315. }
  316. - (float)inputBitrate
  317. {
  318. if (!p_md)
  319. return .0;
  320. libvlc_media_stats_t p_stats;
  321. libvlc_media_get_stats(p_md, &p_stats);
  322. return p_stats.f_input_bitrate;
  323. }
  324. - (NSInteger)numberOfReadBytesOnDemux
  325. {
  326. if (!p_md)
  327. return 0;
  328. libvlc_media_stats_t p_stats;
  329. libvlc_media_get_stats(p_md, &p_stats);
  330. return p_stats.i_demux_read_bytes;
  331. }
  332. - (float)demuxBitrate
  333. {
  334. if (!p_md)
  335. return .0;
  336. libvlc_media_stats_t p_stats;
  337. libvlc_media_get_stats(p_md, &p_stats);
  338. return p_stats.f_demux_bitrate;
  339. }
  340. - (NSInteger)numberOfDecodedVideoBlocks
  341. {
  342. if (!p_md)
  343. return 0;
  344. libvlc_media_stats_t p_stats;
  345. libvlc_media_get_stats(p_md, &p_stats);
  346. return p_stats.i_decoded_video;
  347. }
  348. - (NSInteger)numberOfDecodedAudioBlocks
  349. {
  350. if (!p_md)
  351. return 0;
  352. libvlc_media_stats_t p_stats;
  353. libvlc_media_get_stats(p_md, &p_stats);
  354. return p_stats.i_decoded_audio;
  355. }
  356. - (NSInteger)numberOfDisplayedPictures
  357. {
  358. if (!p_md)
  359. return 0;
  360. libvlc_media_stats_t p_stats;
  361. libvlc_media_get_stats(p_md, &p_stats);
  362. return p_stats.i_displayed_pictures;
  363. }
  364. - (NSInteger)numberOfLostPictures
  365. {
  366. if (!p_md)
  367. return 0;
  368. libvlc_media_stats_t p_stats;
  369. libvlc_media_get_stats(p_md, &p_stats);
  370. return p_stats.i_lost_pictures;
  371. }
  372. - (NSInteger)numberOfPlayedAudioBuffers
  373. {
  374. if (!p_md)
  375. return 0;
  376. libvlc_media_stats_t p_stats;
  377. libvlc_media_get_stats(p_md, &p_stats);
  378. return p_stats.i_played_abuffers;
  379. }
  380. - (NSInteger)numberOfLostAudioBuffers
  381. {
  382. if (!p_md)
  383. return 0;
  384. libvlc_media_stats_t p_stats;
  385. libvlc_media_get_stats(p_md, &p_stats);
  386. return p_stats.i_lost_abuffers;
  387. }
  388. - (NSInteger)numberOfSentPackets
  389. {
  390. if (!p_md)
  391. return 0;
  392. libvlc_media_stats_t p_stats;
  393. libvlc_media_get_stats(p_md, &p_stats);
  394. return p_stats.i_sent_packets;
  395. }
  396. - (NSInteger)numberOfSentBytes
  397. {
  398. if (!p_md)
  399. return 0;
  400. libvlc_media_stats_t p_stats;
  401. libvlc_media_get_stats(p_md, &p_stats);
  402. return p_stats.i_sent_bytes;
  403. }
  404. - (float)streamOutputBitrate
  405. {
  406. if (!p_md)
  407. return .0;
  408. libvlc_media_stats_t p_stats;
  409. libvlc_media_get_stats(p_md, &p_stats);
  410. return p_stats.f_send_bitrate;
  411. }
  412. - (NSInteger)numberOfCorruptedDataPackets
  413. {
  414. if (!p_md)
  415. return 0;
  416. libvlc_media_stats_t p_stats;
  417. libvlc_media_get_stats(p_md, &p_stats);
  418. return p_stats.i_demux_corrupted;
  419. }
  420. - (NSInteger)numberOfDiscontinuties
  421. {
  422. if (!p_md)
  423. return 0;
  424. libvlc_media_stats_t p_stats;
  425. libvlc_media_get_stats(p_md, &p_stats);
  426. return p_stats.i_demux_discontinuity;
  427. }
  428. NSString *VLCMediaTracksInformationCodec = @"codec"; // NSNumber
  429. NSString *VLCMediaTracksInformationId = @"id"; // NSNumber
  430. NSString *VLCMediaTracksInformationType = @"type"; // NSString
  431. NSString *VLCMediaTracksInformationCodecProfile = @"profile"; // NSNumber
  432. NSString *VLCMediaTracksInformationCodecLevel = @"level"; // NSNumber
  433. NSString *VLCMediaTracksInformationTypeAudio = @"audio";
  434. NSString *VLCMediaTracksInformationTypeVideo = @"video";
  435. NSString *VLCMediaTracksInformationTypeText = @"text";
  436. NSString *VLCMediaTracksInformationTypeUnknown = @"unknown";
  437. NSString *VLCMediaTracksInformationBitrate = @"bitrate"; // NSNumber
  438. NSString *VLCMediaTracksInformationLanguage = @"language"; // NSString
  439. NSString *VLCMediaTracksInformationDescription = @"description"; // NSString
  440. NSString *VLCMediaTracksInformationAudioChannelsNumber = @"channelsNumber"; // NSNumber
  441. NSString *VLCMediaTracksInformationAudioRate = @"rate"; // NSNumber
  442. NSString *VLCMediaTracksInformationVideoHeight = @"height"; // NSNumber
  443. NSString *VLCMediaTracksInformationVideoWidth = @"width"; // NSNumber
  444. NSString *VLCMediaTracksInformationSourceAspectRatio = @"sar_num"; // NSNumber
  445. NSString *VLCMediaTracksInformationSourceAspectDenominator = @"sar_den"; // NSNumber
  446. NSString *VLCMediaTracksInformationFrameRate = @"frame_rate_num"; // NSNumber
  447. NSString *VLCMediaTracksInformationFrameRateDenominator = @"frame_rate_den"; // NSNumber
  448. NSString *VLCMediaTracksInformationTextEncoding = @"encoding"; // NSString
  449. - (NSArray *)tracksInformation
  450. {
  451. [self synchronousParse];
  452. libvlc_media_track_t **tracksInfo;
  453. unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
  454. NSMutableArray *array = [NSMutableArray array];
  455. for (NSUInteger i = 0; i < count; i++) {
  456. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  457. @(tracksInfo[i]->i_codec),
  458. VLCMediaTracksInformationCodec,
  459. @(tracksInfo[i]->i_id),
  460. VLCMediaTracksInformationId,
  461. @(tracksInfo[i]->i_profile),
  462. VLCMediaTracksInformationCodecProfile,
  463. @(tracksInfo[i]->i_level),
  464. VLCMediaTracksInformationCodecLevel,
  465. @(tracksInfo[i]->i_bitrate),
  466. VLCMediaTracksInformationBitrate,
  467. nil];
  468. if (tracksInfo[i]->psz_language)
  469. dictionary[VLCMediaTracksInformationLanguage] = [NSString stringWithFormat:@"%s",tracksInfo[i]->psz_language];
  470. if (tracksInfo[i]->psz_description)
  471. dictionary[VLCMediaTracksInformationDescription] = [NSString stringWithFormat:@"%s",tracksInfo[i]->psz_description];
  472. NSString *type;
  473. switch (tracksInfo[i]->i_type) {
  474. case libvlc_track_audio:
  475. type = VLCMediaTracksInformationTypeAudio;
  476. dictionary[VLCMediaTracksInformationAudioChannelsNumber] = @(tracksInfo[i]->audio->i_channels);
  477. dictionary[VLCMediaTracksInformationAudioRate] = @(tracksInfo[i]->audio->i_rate);
  478. break;
  479. case libvlc_track_video:
  480. type = VLCMediaTracksInformationTypeVideo;
  481. dictionary[VLCMediaTracksInformationVideoWidth] = @(tracksInfo[i]->video->i_width);
  482. dictionary[VLCMediaTracksInformationVideoHeight] = @(tracksInfo[i]->video->i_height);
  483. dictionary[VLCMediaTracksInformationSourceAspectRatio] = @(tracksInfo[i]->video->i_sar_num);
  484. dictionary[VLCMediaTracksInformationSourceAspectDenominator] = @(tracksInfo[i]->video->i_sar_den);
  485. dictionary[VLCMediaTracksInformationFrameRate] = @(tracksInfo[i]->video->i_frame_rate_num);
  486. dictionary[VLCMediaTracksInformationFrameRateDenominator] = @(tracksInfo[i]->video->i_frame_rate_den);
  487. break;
  488. case libvlc_track_text:
  489. type = VLCMediaTracksInformationTypeText;
  490. if (tracksInfo[i]->subtitle->psz_encoding)
  491. dictionary[VLCMediaTracksInformationTextEncoding] = [NSString stringWithFormat:@"%s", tracksInfo[i]->subtitle->psz_encoding];
  492. break;
  493. case libvlc_track_unknown:
  494. default:
  495. type = VLCMediaTracksInformationTypeUnknown;
  496. break;
  497. }
  498. [dictionary setValue:type forKey:VLCMediaTracksInformationType];
  499. [array addObject:dictionary];
  500. }
  501. libvlc_media_tracks_release(tracksInfo, count);
  502. return array;
  503. }
  504. - (BOOL)isMediaSizeSuitableForDevice
  505. {
  506. #if TARGET_OS_IPHONE
  507. // Trigger parsing if needed
  508. if (![self isParsed])
  509. [self synchronousParse];
  510. NSUInteger biggestWidth = 0;
  511. NSUInteger biggestHeight = 0;
  512. libvlc_media_track_t **tracksInfo;
  513. unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
  514. for (NSUInteger i = 0; i < count; i++) {
  515. switch (tracksInfo[i]->i_type) {
  516. case libvlc_track_video:
  517. if (tracksInfo[i]->video->i_width > biggestWidth)
  518. biggestWidth = tracksInfo[i]->video->i_width;
  519. if (tracksInfo[i]->video->i_height > biggestHeight)
  520. biggestHeight = tracksInfo[i]->video->i_height;
  521. break;
  522. default:
  523. break;
  524. }
  525. }
  526. if (biggestHeight > 0 && biggestWidth > 0) {
  527. size_t size;
  528. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  529. char *answer = malloc(size);
  530. sysctlbyname("hw.machine", answer, &size, NULL, 0);
  531. NSString *currentMachine = @(answer);
  532. free(answer);
  533. NSUInteger totalNumberOfPixels = biggestWidth * biggestHeight;
  534. if ([currentMachine hasPrefix:@"iPhone2"] || [currentMachine hasPrefix:@"iPhone3"] || [currentMachine hasPrefix:@"iPad1"] || [currentMachine hasPrefix:@"iPod3"] || [currentMachine hasPrefix:@"iPod4"]) {
  535. // iPhone 3GS, iPhone 4, first gen. iPad, 3rd and 4th generation iPod touch
  536. return (totalNumberOfPixels < 600000); // between 480p and 720p
  537. } 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"]) {
  538. // iPhone 4S, iPad 2 and 3, iPod 4 and 5
  539. return (totalNumberOfPixels < 922000); // 720p
  540. } else {
  541. // iPhone 5, iPad 4
  542. return (totalNumberOfPixels < 2074000); // 1080p
  543. }
  544. }
  545. #endif
  546. return YES;
  547. }
  548. @synthesize url;
  549. @synthesize subitems;
  550. - (NSString *)metadataForKey:(NSString *)key
  551. {
  552. if (!p_md)
  553. return NULL;
  554. if (![self isParsed])
  555. [self synchronousParse];
  556. char *returnValue = libvlc_media_get_meta(p_md, [VLCMedia stringToMetaType:key]);
  557. if (!returnValue)
  558. return NULL;
  559. NSString *actualReturnValue = [NSString stringWithUTF8String:returnValue];
  560. free(returnValue);
  561. return actualReturnValue;
  562. }
  563. - (void)setMetadata:(NSString *)data forKey:(NSString *)key
  564. {
  565. if (!p_md)
  566. return;
  567. libvlc_media_set_meta(p_md, [VLCMedia stringToMetaType:key], [data UTF8String]);
  568. }
  569. - (BOOL)saveMetadata
  570. {
  571. if (p_md)
  572. return libvlc_media_save_meta(p_md);
  573. return NO;
  574. }
  575. @synthesize metaDictionary;
  576. @synthesize state;
  577. @end
  578. /******************************************************************************
  579. * Implementation VLCMedia (LibVLCBridging)
  580. */
  581. @implementation VLCMedia (LibVLCBridging)
  582. + (id)mediaWithLibVLCMediaDescriptor:(void *)md
  583. {
  584. return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor:md] autorelease];
  585. }
  586. - (id)initWithLibVLCMediaDescriptor:(void *)md
  587. {
  588. if (self = [super init]) {
  589. libvlc_media_retain(md);
  590. p_md = md;
  591. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  592. [self initInternalMediaDescriptor];
  593. }
  594. return self;
  595. }
  596. - (void *)libVLCMediaDescriptor
  597. {
  598. return p_md;
  599. }
  600. + (id)mediaWithMedia:(VLCMedia *)media andLibVLCOptions:(NSDictionary *)options
  601. {
  602. libvlc_media_t * p_md;
  603. p_md = libvlc_media_duplicate([media libVLCMediaDescriptor]);
  604. for (NSString * key in [options allKeys]) {
  605. if (options[key] != [NSNull null])
  606. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, options[key]] UTF8String]);
  607. else
  608. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
  609. }
  610. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  611. }
  612. @end
  613. /******************************************************************************
  614. * Implementation VLCMedia (Private)
  615. */
  616. @implementation VLCMedia (Private)
  617. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  618. {
  619. static NSDictionary * stringToMetaDictionary = nil;
  620. // TODO: Thread safe-ize
  621. if (!stringToMetaDictionary) {
  622. #define VLCStringToMeta( name ) [NSNumber numberWithInt: libvlc_meta_##name], VLCMetaInformation##name
  623. stringToMetaDictionary =
  624. [[NSDictionary dictionaryWithObjectsAndKeys:
  625. VLCStringToMeta(Title),
  626. VLCStringToMeta(Artist),
  627. VLCStringToMeta(Genre),
  628. VLCStringToMeta(Copyright),
  629. VLCStringToMeta(Album),
  630. VLCStringToMeta(TrackNumber),
  631. VLCStringToMeta(Description),
  632. VLCStringToMeta(Rating),
  633. VLCStringToMeta(Date),
  634. VLCStringToMeta(Setting),
  635. VLCStringToMeta(URL),
  636. VLCStringToMeta(Language),
  637. VLCStringToMeta(NowPlaying),
  638. VLCStringToMeta(Publisher),
  639. VLCStringToMeta(ArtworkURL),
  640. VLCStringToMeta(TrackID),
  641. nil] retain];
  642. #undef VLCStringToMeta
  643. }
  644. NSNumber * number = stringToMetaDictionary[string];
  645. return number ? [number intValue] : -1;
  646. }
  647. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  648. {
  649. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  650. VLCMetaToString(Title, type);
  651. VLCMetaToString(Artist, type);
  652. VLCMetaToString(Genre, type);
  653. VLCMetaToString(Copyright, type);
  654. VLCMetaToString(Album, type);
  655. VLCMetaToString(TrackNumber, type);
  656. VLCMetaToString(Description, type);
  657. VLCMetaToString(Rating, type);
  658. VLCMetaToString(Date, type);
  659. VLCMetaToString(Setting, type);
  660. VLCMetaToString(URL, type);
  661. VLCMetaToString(Language, type);
  662. VLCMetaToString(NowPlaying, type);
  663. VLCMetaToString(Publisher, type);
  664. VLCMetaToString(ArtworkURL, type);
  665. VLCMetaToString(TrackID, type);
  666. #undef VLCMetaToString
  667. return nil;
  668. }
  669. - (void)initInternalMediaDescriptor
  670. {
  671. char * p_url = libvlc_media_get_mrl( p_md );
  672. url = [[NSURL URLWithString:@(p_url)] retain];
  673. if (!url) /* Attempt to interpret as a file path then */
  674. url = [[NSURL fileURLWithPath:@(p_url)] retain];
  675. free(p_url);
  676. libvlc_media_set_user_data(p_md, (void*)self);
  677. libvlc_event_manager_t * p_em = libvlc_media_event_manager( p_md );
  678. libvlc_event_attach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, self);
  679. libvlc_event_attach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, self);
  680. libvlc_event_attach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, self);
  681. libvlc_event_attach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, self);
  682. libvlc_event_attach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, self);
  683. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  684. if (!p_mlist)
  685. subitems = nil;
  686. else {
  687. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  688. libvlc_media_list_release( p_mlist );
  689. }
  690. isParsed = libvlc_media_is_parsed(p_md);
  691. state = LibVLCStateToMediaState(libvlc_media_get_state( p_md ));
  692. }
  693. - (void)fetchMetaInformationFromLibVLCWithType:(NSString *)metaType
  694. {
  695. char * psz_value = libvlc_media_get_meta( p_md, [VLCMedia stringToMetaType:metaType] );
  696. NSString * newValue = psz_value ? @(psz_value) : nil;
  697. NSString * oldValue = [metaDictionary valueForKey:metaType];
  698. free(psz_value);
  699. if (newValue != oldValue && !(oldValue && newValue && [oldValue compare:newValue] == NSOrderedSame)) {
  700. // Only fetch the art if needed. (ie, create the NSImage, if it was requested before)
  701. if (isArtFetched && [metaType isEqualToString:VLCMetaInformationArtworkURL]) {
  702. [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
  703. toTarget:self
  704. withObject:newValue];
  705. }
  706. [metaDictionary setValue:newValue forKeyPath:metaType];
  707. }
  708. }
  709. #if !TARGET_OS_IPHONE
  710. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
  711. {
  712. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  713. NSImage * art = nil;
  714. if (anURL) {
  715. // Go ahead and load up the art work
  716. NSURL * artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  717. // Don't attempt to fetch artwork from remote. Core will do that alone
  718. if ([artUrl isFileURL])
  719. art = [[[NSImage alloc] initWithContentsOfURL:artUrl] autorelease];
  720. }
  721. // If anything was found, lets save it to the meta data dictionary
  722. [self performSelectorOnMainThread:@selector(setArtwork:) withObject:art waitUntilDone:NO];
  723. [pool release];
  724. }
  725. - (void)setArtwork:(NSImage *)art
  726. {
  727. if (!art) {
  728. [metaDictionary removeObjectForKey:@"artwork"];
  729. return;
  730. }
  731. [metaDictionary setObject:art forKey:@"artwork"];
  732. }
  733. #endif
  734. - (void)parseIfNeeded
  735. {
  736. if (![self isParsed])
  737. [self parse];
  738. }
  739. - (void)metaChanged:(NSString *)metaType
  740. {
  741. [self fetchMetaInformationFromLibVLCWithType:metaType];
  742. }
  743. - (void)subItemAdded
  744. {
  745. if (subitems)
  746. return; /* Nothing to do */
  747. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  748. NSAssert( p_mlist, @"The mlist shouldn't be nil, we are receiving a subItemAdded");
  749. [self willChangeValueForKey:@"subitems"];
  750. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  751. [self didChangeValueForKey:@"subitems"];
  752. libvlc_media_list_release( p_mlist );
  753. }
  754. - (void)parsedChanged:(NSNumber *)isParsedAsNumber
  755. {
  756. [self willChangeValueForKey:@"parsed"];
  757. isParsed = [isParsedAsNumber boolValue];
  758. [self didChangeValueForKey:@"parsed"];
  759. // FIXME: Probably don't even call this if there is no delegate.
  760. if (!delegate || !isParsed)
  761. return;
  762. if ([delegate respondsToSelector:@selector(mediaDidFinishParsing:)])
  763. [delegate mediaDidFinishParsing:self];
  764. }
  765. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber
  766. {
  767. [self setState: [newStateAsNumber intValue]];
  768. }
  769. #if TARGET_OS_IPHONE
  770. - (NSDictionary *)metaDictionary
  771. {
  772. if (!areOthersMetaFetched) {
  773. areOthersMetaFetched = YES;
  774. /* Force VLCMetaInformationTitle, that will trigger preparsing
  775. * And all the other meta will be added through the libvlc event system */
  776. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  777. }
  778. if (!isArtURLFetched) {
  779. isArtURLFetched = YES;
  780. /* Force isArtURLFetched, that will trigger artwork download eventually
  781. * And all the other meta will be added through the libvlc event system */
  782. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  783. }
  784. return metaDictionary;
  785. }
  786. #else
  787. - (id)valueForKeyPath:(NSString *)keyPath
  788. {
  789. if (!isArtFetched && [keyPath isEqualToString:@"metaDictionary.artwork"]) {
  790. isArtFetched = YES;
  791. /* Force the retrieval of the artwork now that someone asked for it */
  792. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  793. } else if (!areOthersMetaFetched && [keyPath hasPrefix:@"metaDictionary."]) {
  794. areOthersMetaFetched = YES;
  795. /* Force VLCMetaInformationTitle, that will trigger preparsing
  796. * And all the other meta will be added through the libvlc event system */
  797. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  798. } else if (!isArtURLFetched && [keyPath hasPrefix:@"metaDictionary.artworkURL"]) {
  799. isArtURLFetched = YES;
  800. /* Force isArtURLFetched, that will trigger artwork download eventually
  801. * And all the other meta will be added through the libvlc event system */
  802. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  803. }
  804. return [super valueForKeyPath:keyPath];
  805. }
  806. #endif
  807. @end
  808. /******************************************************************************
  809. * Implementation VLCMedia (VLCMediaPlayerBridging)
  810. */
  811. @implementation VLCMedia (VLCMediaPlayerBridging)
  812. - (void)setLength:(VLCTime *)value
  813. {
  814. if (length && value && [length compare:value] == NSOrderedSame)
  815. return;
  816. [length release];
  817. length = value ? [value retain] : nil;
  818. }
  819. @end