VLCMedia.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. /* Meta Dictionary Keys */
  33. NSString * VLCMetaInformationTitle = @"title";
  34. NSString * VLCMetaInformationArtist = @"artist";
  35. NSString * VLCMetaInformationGenre = @"genre";
  36. NSString * VLCMetaInformationCopyright = @"copyright";
  37. NSString * VLCMetaInformationAlbum = @"album";
  38. NSString * VLCMetaInformationTrackNumber = @"trackNumber";
  39. NSString * VLCMetaInformationDescription = @"description";
  40. NSString * VLCMetaInformationRating = @"rating";
  41. NSString * VLCMetaInformationDate = @"date";
  42. NSString * VLCMetaInformationSetting = @"setting";
  43. NSString * VLCMetaInformationURL = @"url";
  44. NSString * VLCMetaInformationLanguage = @"language";
  45. NSString * VLCMetaInformationNowPlaying = @"nowPlaying";
  46. NSString * VLCMetaInformationPublisher = @"publisher";
  47. NSString * VLCMetaInformationEncodedBy = @"encodedBy";
  48. NSString * VLCMetaInformationArtworkURL = @"artworkURL";
  49. NSString * VLCMetaInformationArtwork = @"artwork";
  50. NSString * VLCMetaInformationTrackID = @"trackID";
  51. /* Notification Messages */
  52. NSString * VLCMediaMetaChanged = @"VLCMediaMetaChanged";
  53. /******************************************************************************
  54. * @property (readwrite)
  55. */
  56. @interface VLCMedia ()
  57. @property (readwrite) VLCMediaState state;
  58. @end
  59. /******************************************************************************
  60. * Interface (Private)
  61. */
  62. // TODO: Documentation
  63. @interface VLCMedia (Private)
  64. /* Statics */
  65. + (libvlc_meta_t)stringToMetaType:(NSString *)string;
  66. + (NSString *)metaTypeToString:(libvlc_meta_t)type;
  67. /* Initializers */
  68. - (void)initInternalMediaDescriptor;
  69. /* Operations */
  70. - (void)fetchMetaInformationFromLibVLCWithType:(NSString*)metaType;
  71. #if !TARGET_OS_IPHONE
  72. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL;
  73. - (void)setArtwork:(NSImage *)art;
  74. #endif
  75. - (void)parseIfNeeded;
  76. /* Callback Methods */
  77. - (void)parsedChanged:(NSNumber *)isParsedAsNumber;
  78. - (void)metaChanged:(NSString *)metaType;
  79. - (void)subItemAdded;
  80. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber;
  81. @end
  82. static VLCMediaState libvlc_state_to_media_state[] =
  83. {
  84. [libvlc_NothingSpecial] = VLCMediaStateNothingSpecial,
  85. [libvlc_Stopped] = VLCMediaStateNothingSpecial,
  86. [libvlc_Opening] = VLCMediaStateNothingSpecial,
  87. [libvlc_Buffering] = VLCMediaStateBuffering,
  88. [libvlc_Ended] = VLCMediaStateNothingSpecial,
  89. [libvlc_Error] = VLCMediaStateError,
  90. [libvlc_Playing] = VLCMediaStatePlaying,
  91. [libvlc_Paused] = VLCMediaStatePlaying,
  92. };
  93. static inline VLCMediaState LibVLCStateToMediaState( libvlc_state_t state )
  94. {
  95. return libvlc_state_to_media_state[state];
  96. }
  97. /******************************************************************************
  98. * LibVLC Event Callback
  99. */
  100. static void HandleMediaMetaChanged(const libvlc_event_t * event, void * self)
  101. {
  102. if (event->u.media_meta_changed.meta_type == libvlc_meta_Publisher ||
  103. event->u.media_meta_changed.meta_type == libvlc_meta_NowPlaying) {
  104. /* Skip those meta. We don't really care about them for now.
  105. * And they occure a lot */
  106. return;
  107. }
  108. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  109. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  110. withMethod:@selector(metaChanged:)
  111. withArgumentAsObject:[VLCMedia metaTypeToString:event->u.media_meta_changed.meta_type]];
  112. [pool drain];
  113. }
  114. static void HandleMediaDurationChanged(const libvlc_event_t * event, void * self)
  115. {
  116. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  117. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  118. withMethod:@selector(setLength:)
  119. withArgumentAsObject:[VLCTime timeWithNumber:
  120. [NSNumber numberWithLongLong:event->u.media_duration_changed.new_duration]]];
  121. [pool drain];
  122. }
  123. static void HandleMediaStateChanged(const libvlc_event_t * event, void * self)
  124. {
  125. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  126. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  127. withMethod:@selector(setStateAsNumber:)
  128. withArgumentAsObject:[NSNumber numberWithInt:
  129. LibVLCStateToMediaState(event->u.media_state_changed.new_state)]];
  130. [pool drain];
  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 drain];
  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:[NSNumber numberWithBool: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 objectForKey: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:[NSNumber numberWithLongLong: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. libvlc_media_parse_async(p_md);
  266. }
  267. - (void)addOptions:(NSDictionary*)options
  268. {
  269. if (p_md) {
  270. for (NSString * key in [options allKeys]) {
  271. if ([options objectForKey:key] != [NSNull null])
  272. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, [options objectForKey:key]] UTF8String]);
  273. else
  274. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
  275. }
  276. }
  277. }
  278. - (NSDictionary*) stats
  279. {
  280. if (!p_md)
  281. return NULL;
  282. NSMutableDictionary *d = [NSMutableDictionary dictionary];
  283. libvlc_media_stats_t p_stats;
  284. libvlc_media_get_stats(p_md, &p_stats);
  285. [d setObject:[NSNumber numberWithFloat: p_stats.f_demux_bitrate] forKey:@"demuxBitrate"];
  286. [d setObject:[NSNumber numberWithFloat: p_stats.f_input_bitrate] forKey:@"inputBitrate"];
  287. [d setObject:[NSNumber numberWithFloat: p_stats.f_send_bitrate] forKey:@"sendBitrate"];
  288. [d setObject:[NSNumber numberWithInt: p_stats.i_decoded_audio] forKey:@"decodedAudio"];
  289. [d setObject:[NSNumber numberWithInt: p_stats.i_decoded_video] forKey:@"decodedVideo"];
  290. [d setObject:[NSNumber numberWithInt: p_stats.i_demux_corrupted] forKey:@"demuxCorrupted"];
  291. [d setObject:[NSNumber numberWithInt: p_stats.i_demux_discontinuity] forKey:@"demuxDiscontinuity"];
  292. [d setObject:[NSNumber numberWithInt: p_stats.i_demux_read_bytes] forKey:@"demuxReadBytes"];
  293. [d setObject:[NSNumber numberWithInt: p_stats.i_displayed_pictures] forKey:@"displayedPictures"];
  294. [d setObject:[NSNumber numberWithInt: p_stats.i_lost_abuffers] forKey:@"lostAbuffers"];
  295. [d setObject:[NSNumber numberWithInt: p_stats.i_lost_pictures] forKey:@"lostPictures"];
  296. [d setObject:[NSNumber numberWithInt: p_stats.i_played_abuffers] forKey:@"playedAbuffers"];
  297. [d setObject:[NSNumber numberWithInt: p_stats.i_read_bytes] forKey:@"readBytes"];
  298. [d setObject:[NSNumber numberWithInt: p_stats.i_sent_bytes] forKey:@"sentBytes"];
  299. [d setObject:[NSNumber numberWithInt: p_stats.i_sent_packets] forKey:@"sentPackets"];
  300. return d;
  301. }
  302. NSString *VLCMediaTracksInformationCodec = @"codec"; // NSNumber
  303. NSString *VLCMediaTracksInformationId = @"id"; // NSNumber
  304. NSString *VLCMediaTracksInformationType = @"type"; // NSString
  305. NSString *VLCMediaTracksInformationCodecProfile = @"profile"; // NSNumber
  306. NSString *VLCMediaTracksInformationCodecLevel = @"level"; // NSNumber
  307. NSString *VLCMediaTracksInformationTypeAudio = @"audio";
  308. NSString *VLCMediaTracksInformationTypeVideo = @"video";
  309. NSString *VLCMediaTracksInformationTypeText = @"text";
  310. NSString *VLCMediaTracksInformationTypeUnknown = @"unknown";
  311. NSString *VLCMediaTracksInformationBitrate = @"bitrate"; // NSNumber
  312. NSString *VLCMediaTracksInformationLanguage = @"language"; // NSString
  313. NSString *VLCMediaTracksInformationDescription = @"description"; // NSString
  314. NSString *VLCMediaTracksInformationAudioChannelsNumber = @"channelsNumber"; // NSNumber
  315. NSString *VLCMediaTracksInformationAudioRate = @"rate"; // NSNumber
  316. NSString *VLCMediaTracksInformationVideoHeight = @"height"; // NSNumber
  317. NSString *VLCMediaTracksInformationVideoWidth = @"width"; // NSNumber
  318. NSString *VLCMediaTracksInformationSourceAspectRatio = @"sar_num"; // NSNumber
  319. NSString *VLCMediaTracksInformationSourceAspectDenominator = @"sar_den"; // NSNumber
  320. NSString *VLCMediaTracksInformationFrameRate = @"frame_rate_num"; // NSNumber
  321. NSString *VLCMediaTracksInformationFrameRateDenominator = @"frame_rate_den"; // NSNumber
  322. NSString *VLCMediaTracksInformationTextEncoding = @"encoding"; // NSString
  323. - (NSArray *)tracksInformation
  324. {
  325. // Trigger parsing if needed
  326. [self parseIfNeeded];
  327. libvlc_media_track_t **tracksInfo;
  328. unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
  329. NSMutableArray *array = [NSMutableArray array];
  330. for (NSUInteger i = 0; i < count; i++) {
  331. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  332. [NSNumber numberWithUnsignedInt:tracksInfo[i]->i_codec],
  333. VLCMediaTracksInformationCodec,
  334. [NSNumber numberWithInt:tracksInfo[i]->i_id],
  335. VLCMediaTracksInformationId,
  336. [NSNumber numberWithInt:tracksInfo[i]->i_profile],
  337. VLCMediaTracksInformationCodecProfile,
  338. [NSNumber numberWithInt:tracksInfo[i]->i_level],
  339. VLCMediaTracksInformationCodecLevel,
  340. [NSNumber numberWithUnsignedInt:tracksInfo[i]->i_bitrate],
  341. VLCMediaTracksInformationBitrate,
  342. nil];
  343. if (tracksInfo[i]->psz_language)
  344. [dictionary setObject:[NSString stringWithFormat:@"%s",tracksInfo[i]->psz_language]
  345. forKey:VLCMediaTracksInformationLanguage];
  346. if (tracksInfo[i]->psz_description)
  347. [dictionary setObject:[NSString stringWithFormat:@"%s",tracksInfo[i]->psz_description]
  348. forKey:VLCMediaTracksInformationLanguage];
  349. NSString *type;
  350. switch (tracksInfo[i]->i_type) {
  351. case libvlc_track_audio:
  352. type = VLCMediaTracksInformationTypeAudio;
  353. [dictionary setObject:[NSNumber numberWithUnsignedInt:tracksInfo[i]->audio->i_channels]
  354. forKey:VLCMediaTracksInformationAudioChannelsNumber];
  355. [dictionary setObject:[NSNumber numberWithUnsignedInt:tracksInfo[i]->audio->i_rate]
  356. forKey:VLCMediaTracksInformationAudioRate];
  357. break;
  358. case libvlc_track_video:
  359. type = VLCMediaTracksInformationTypeVideo;
  360. [dictionary setObject:[NSNumber numberWithUnsignedInt:tracksInfo[i]->video->i_width]
  361. forKey:VLCMediaTracksInformationVideoWidth];
  362. [dictionary setObject:[NSNumber numberWithUnsignedInt:tracksInfo[i]->video->i_height]
  363. forKey:VLCMediaTracksInformationVideoHeight];
  364. [dictionary setObject:[NSNumber numberWithUnsignedInt:tracksInfo[i]->video->i_sar_num]
  365. forKey:VLCMediaTracksInformationSourceAspectRatio];
  366. [dictionary setObject:[NSNumber numberWithUnsignedInt:tracksInfo[i]->video->i_sar_den]
  367. forKey:VLCMediaTracksInformationSourceAspectDenominator];
  368. [dictionary setObject:[NSNumber numberWithUnsignedInt:tracksInfo[i]->video->i_frame_rate_num]
  369. forKey:VLCMediaTracksInformationFrameRate];
  370. [dictionary setObject:[NSNumber numberWithUnsignedInt:tracksInfo[i]->video->i_frame_rate_den]
  371. forKey:VLCMediaTracksInformationFrameRateDenominator];
  372. break;
  373. case libvlc_track_text:
  374. type = VLCMediaTracksInformationTypeText;
  375. if (tracksInfo[i]->subtitle->psz_encoding)
  376. [dictionary setObject:[NSString stringWithFormat:@"%s", tracksInfo[i]->subtitle->psz_encoding]
  377. forKey:VLCMediaTracksInformationTextEncoding];
  378. break;
  379. case libvlc_track_unknown:
  380. default:
  381. type = VLCMediaTracksInformationTypeUnknown;
  382. break;
  383. }
  384. [dictionary setValue:type forKey:VLCMediaTracksInformationType];
  385. [array addObject:dictionary];
  386. }
  387. libvlc_media_tracks_release(tracksInfo, count);
  388. return array;
  389. }
  390. @synthesize url;
  391. @synthesize subitems;
  392. @synthesize metaDictionary;
  393. @synthesize state;
  394. @end
  395. /******************************************************************************
  396. * Implementation VLCMedia (LibVLCBridging)
  397. */
  398. @implementation VLCMedia (LibVLCBridging)
  399. + (id)mediaWithLibVLCMediaDescriptor:(void *)md
  400. {
  401. return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor:md] autorelease];
  402. }
  403. - (id)initWithLibVLCMediaDescriptor:(void *)md
  404. {
  405. if (self = [super init]) {
  406. libvlc_media_retain(md);
  407. p_md = md;
  408. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  409. [self initInternalMediaDescriptor];
  410. }
  411. return self;
  412. }
  413. - (void *)libVLCMediaDescriptor
  414. {
  415. return p_md;
  416. }
  417. + (id)mediaWithMedia:(VLCMedia *)media andLibVLCOptions:(NSDictionary *)options
  418. {
  419. libvlc_media_t * p_md;
  420. p_md = libvlc_media_duplicate([media libVLCMediaDescriptor]);
  421. for (NSString * key in [options allKeys]) {
  422. if ([options objectForKey:key] != [NSNull null])
  423. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, [options objectForKey:key]] UTF8String]);
  424. else
  425. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
  426. }
  427. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  428. }
  429. @end
  430. /******************************************************************************
  431. * Implementation VLCMedia (Private)
  432. */
  433. @implementation VLCMedia (Private)
  434. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  435. {
  436. static NSDictionary * stringToMetaDictionary = nil;
  437. // TODO: Thread safe-ize
  438. if (!stringToMetaDictionary) {
  439. #define VLCStringToMeta( name ) [NSNumber numberWithInt: libvlc_meta_##name], VLCMetaInformation##name
  440. stringToMetaDictionary =
  441. [[NSDictionary dictionaryWithObjectsAndKeys:
  442. VLCStringToMeta(Title),
  443. VLCStringToMeta(Artist),
  444. VLCStringToMeta(Genre),
  445. VLCStringToMeta(Copyright),
  446. VLCStringToMeta(Album),
  447. VLCStringToMeta(TrackNumber),
  448. VLCStringToMeta(Description),
  449. VLCStringToMeta(Rating),
  450. VLCStringToMeta(Date),
  451. VLCStringToMeta(Setting),
  452. VLCStringToMeta(URL),
  453. VLCStringToMeta(Language),
  454. VLCStringToMeta(NowPlaying),
  455. VLCStringToMeta(Publisher),
  456. VLCStringToMeta(ArtworkURL),
  457. VLCStringToMeta(TrackID),
  458. nil] retain];
  459. #undef VLCStringToMeta
  460. }
  461. NSNumber * number = [stringToMetaDictionary objectForKey:string];
  462. return number ? [number intValue] : -1;
  463. }
  464. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  465. {
  466. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  467. VLCMetaToString(Title, type);
  468. VLCMetaToString(Artist, type);
  469. VLCMetaToString(Genre, type);
  470. VLCMetaToString(Copyright, type);
  471. VLCMetaToString(Album, type);
  472. VLCMetaToString(TrackNumber, type);
  473. VLCMetaToString(Description, type);
  474. VLCMetaToString(Rating, type);
  475. VLCMetaToString(Date, type);
  476. VLCMetaToString(Setting, type);
  477. VLCMetaToString(URL, type);
  478. VLCMetaToString(Language, type);
  479. VLCMetaToString(NowPlaying, type);
  480. VLCMetaToString(Publisher, type);
  481. VLCMetaToString(ArtworkURL, type);
  482. VLCMetaToString(TrackID, type);
  483. #undef VLCMetaToString
  484. return nil;
  485. }
  486. - (void)initInternalMediaDescriptor
  487. {
  488. char * p_url = libvlc_media_get_mrl( p_md );
  489. url = [[NSURL URLWithString:[NSString stringWithUTF8String:p_url]] retain];
  490. if (!url) /* Attempt to interpret as a file path then */
  491. url = [[NSURL fileURLWithPath:[NSString stringWithUTF8String:p_url]] retain];
  492. free(p_url);
  493. libvlc_media_set_user_data(p_md, (void*)self);
  494. libvlc_event_manager_t * p_em = libvlc_media_event_manager( p_md );
  495. libvlc_event_attach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, self);
  496. libvlc_event_attach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, self);
  497. libvlc_event_attach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, self);
  498. libvlc_event_attach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, self);
  499. libvlc_event_attach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, self);
  500. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  501. if (!p_mlist)
  502. subitems = nil;
  503. else {
  504. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  505. libvlc_media_list_release( p_mlist );
  506. }
  507. isParsed = libvlc_media_is_parsed(p_md);
  508. state = LibVLCStateToMediaState(libvlc_media_get_state( p_md ));
  509. }
  510. - (void)fetchMetaInformationFromLibVLCWithType:(NSString *)metaType
  511. {
  512. char * psz_value = libvlc_media_get_meta( p_md, [VLCMedia stringToMetaType:metaType] );
  513. NSString * newValue = psz_value ? [NSString stringWithUTF8String: psz_value] : nil;
  514. NSString * oldValue = [metaDictionary valueForKey:metaType];
  515. free(psz_value);
  516. if (newValue != oldValue && !(oldValue && newValue && [oldValue compare:newValue] == NSOrderedSame)) {
  517. // Only fetch the art if needed. (ie, create the NSImage, if it was requested before)
  518. if (isArtFetched && [metaType isEqualToString:VLCMetaInformationArtworkURL]) {
  519. [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
  520. toTarget:self
  521. withObject:newValue];
  522. }
  523. [metaDictionary setValue:newValue forKeyPath:metaType];
  524. }
  525. }
  526. #if !TARGET_OS_IPHONE
  527. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
  528. {
  529. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  530. NSImage * art = nil;
  531. if (anURL) {
  532. // Go ahead and load up the art work
  533. NSURL * artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  534. // Don't attempt to fetch artwork from remote. Core will do that alone
  535. if ([artUrl isFileURL])
  536. art = [[[NSImage alloc] initWithContentsOfURL:artUrl] autorelease];
  537. }
  538. // If anything was found, lets save it to the meta data dictionary
  539. [self performSelectorOnMainThread:@selector(setArtwork:) withObject:art waitUntilDone:NO];
  540. [pool release];
  541. }
  542. - (void)setArtwork:(NSImage *)art
  543. {
  544. if (!art) {
  545. [metaDictionary removeObjectForKey:@"artwork"];
  546. return;
  547. }
  548. [metaDictionary setObject:art forKey:@"artwork"];
  549. }
  550. #endif
  551. - (void)parseIfNeeded
  552. {
  553. if (![self isParsed])
  554. [self parse];
  555. }
  556. - (void)metaChanged:(NSString *)metaType
  557. {
  558. [self fetchMetaInformationFromLibVLCWithType:metaType];
  559. }
  560. - (void)subItemAdded
  561. {
  562. if (subitems)
  563. return; /* Nothing to do */
  564. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  565. NSAssert( p_mlist, @"The mlist shouldn't be nil, we are receiving a subItemAdded");
  566. [self willChangeValueForKey:@"subitems"];
  567. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  568. [self didChangeValueForKey:@"subitems"];
  569. libvlc_media_list_release( p_mlist );
  570. }
  571. - (void)parsedChanged:(NSNumber *)isParsedAsNumber
  572. {
  573. [self willChangeValueForKey:@"parsed"];
  574. isParsed = [isParsedAsNumber boolValue];
  575. [self didChangeValueForKey:@"parsed"];
  576. // FIXME: Probably don't even call this if there is no delegate.
  577. if (!delegate || !isParsed)
  578. return;
  579. if ([delegate respondsToSelector:@selector(mediaDidFinishParsing:)])
  580. [delegate mediaDidFinishParsing:self];
  581. }
  582. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber
  583. {
  584. [self setState: [newStateAsNumber intValue]];
  585. }
  586. #if TARGET_OS_IPHONE
  587. - (NSDictionary *)metaDictionary
  588. {
  589. if (!areOthersMetaFetched) {
  590. areOthersMetaFetched = YES;
  591. /* Force VLCMetaInformationTitle, that will trigger preparsing
  592. * And all the other meta will be added through the libvlc event system */
  593. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  594. }
  595. if (!isArtURLFetched) {
  596. isArtURLFetched = YES;
  597. /* Force isArtURLFetched, that will trigger artwork download eventually
  598. * And all the other meta will be added through the libvlc event system */
  599. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  600. }
  601. return metaDictionary;
  602. }
  603. #else
  604. - (id)valueForKeyPath:(NSString *)keyPath
  605. {
  606. if (!isArtFetched && [keyPath isEqualToString:@"metaDictionary.artwork"]) {
  607. isArtFetched = YES;
  608. /* Force the retrieval of the artwork now that someone asked for it */
  609. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  610. } else if (!areOthersMetaFetched && [keyPath hasPrefix:@"metaDictionary."]) {
  611. areOthersMetaFetched = YES;
  612. /* Force VLCMetaInformationTitle, that will trigger preparsing
  613. * And all the other meta will be added through the libvlc event system */
  614. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  615. } else if (!isArtURLFetched && [keyPath hasPrefix:@"metaDictionary.artworkURL"]) {
  616. isArtURLFetched = YES;
  617. /* Force isArtURLFetched, that will trigger artwork download eventually
  618. * And all the other meta will be added through the libvlc event system */
  619. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  620. }
  621. return [super valueForKeyPath:keyPath];
  622. }
  623. #endif
  624. @end
  625. /******************************************************************************
  626. * Implementation VLCMedia (VLCMediaPlayerBridging)
  627. */
  628. @implementation VLCMedia (VLCMediaPlayerBridging)
  629. - (void)setLength:(VLCTime *)value
  630. {
  631. if (length && value && [length compare:value] == NSOrderedSame)
  632. return;
  633. [length release];
  634. length = value ? [value retain] : nil;
  635. }
  636. @end