VLCMedia.m 34 KB

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