VLCMedia.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*****************************************************************************
  2. * VLCMedia.m: VLCKit.framework VLCMedia implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 the VideoLAN team
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import "VLCMedia.h"
  25. #import "VLCMediaList.h"
  26. #import "VLCEventManager.h"
  27. #import "VLCLibrary.h"
  28. #import "VLCLibVLCBridging.h"
  29. #include <vlc/libvlc.h>
  30. /* Meta Dictionary Keys */
  31. NSString * VLCMetaInformationTitle = @"title";
  32. NSString * VLCMetaInformationArtist = @"artist";
  33. NSString * VLCMetaInformationGenre = @"genre";
  34. NSString * VLCMetaInformationCopyright = @"copyright";
  35. NSString * VLCMetaInformationAlbum = @"album";
  36. NSString * VLCMetaInformationTrackNumber = @"trackNumber";
  37. NSString * VLCMetaInformationDescription = @"description";
  38. NSString * VLCMetaInformationRating = @"rating";
  39. NSString * VLCMetaInformationDate = @"date";
  40. NSString * VLCMetaInformationSetting = @"setting";
  41. NSString * VLCMetaInformationURL = @"url";
  42. NSString * VLCMetaInformationLanguage = @"language";
  43. NSString * VLCMetaInformationNowPlaying = @"nowPlaying";
  44. NSString * VLCMetaInformationPublisher = @"publisher";
  45. NSString * VLCMetaInformationEncodedBy = @"encodedBy";
  46. NSString * VLCMetaInformationArtworkURL = @"artworkURL";
  47. NSString * VLCMetaInformationArtwork = @"artwork";
  48. NSString * VLCMetaInformationTrackID = @"trackID";
  49. /* Notification Messages */
  50. NSString * VLCMediaMetaChanged = @"VLCMediaMetaChanged";
  51. /******************************************************************************
  52. * @property (readwrite)
  53. */
  54. @interface VLCMedia ()
  55. @property (readwrite) VLCMediaState state;
  56. @end
  57. /******************************************************************************
  58. * Interface (Private)
  59. */
  60. // TODO: Documentation
  61. @interface VLCMedia (Private)
  62. /* Statics */
  63. + (libvlc_meta_t)stringToMetaType:(NSString *)string;
  64. + (NSString *)metaTypeToString:(libvlc_meta_t)type;
  65. /* Initializers */
  66. - (void)initInternalMediaDescriptor;
  67. /* Operations */
  68. - (void)fetchMetaInformationFromLibVLCWithType:(NSString*)metaType;
  69. #if !TARGET_OS_IPHONE
  70. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL;
  71. - (void)setArtwork:(NSImage *)art;
  72. #endif
  73. - (void)parseIfNeeded;
  74. /* Callback Methods */
  75. - (void)parsedChanged:(NSNumber *)isParsedAsNumber;
  76. - (void)metaChanged:(NSString *)metaType;
  77. - (void)subItemAdded;
  78. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber;
  79. @end
  80. static VLCMediaState libvlc_state_to_media_state[] =
  81. {
  82. [libvlc_NothingSpecial] = VLCMediaStateNothingSpecial,
  83. [libvlc_Stopped] = VLCMediaStateNothingSpecial,
  84. [libvlc_Opening] = VLCMediaStateNothingSpecial,
  85. [libvlc_Buffering] = VLCMediaStateBuffering,
  86. [libvlc_Ended] = VLCMediaStateNothingSpecial,
  87. [libvlc_Error] = VLCMediaStateError,
  88. [libvlc_Playing] = VLCMediaStatePlaying,
  89. [libvlc_Paused] = VLCMediaStatePlaying,
  90. };
  91. static inline VLCMediaState LibVLCStateToMediaState( libvlc_state_t state )
  92. {
  93. return libvlc_state_to_media_state[state];
  94. }
  95. /******************************************************************************
  96. * LibVLC Event Callback
  97. */
  98. static void HandleMediaMetaChanged(const libvlc_event_t * event, void * self)
  99. {
  100. if( event->u.media_meta_changed.meta_type == libvlc_meta_Publisher ||
  101. event->u.media_meta_changed.meta_type == libvlc_meta_NowPlaying )
  102. {
  103. /* Skip those meta. We don't really care about them for now.
  104. * And they occure a lot */
  105. return;
  106. }
  107. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  108. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  109. withMethod:@selector(metaChanged:)
  110. withArgumentAsObject:[VLCMedia metaTypeToString:event->u.media_meta_changed.meta_type]];
  111. [pool drain];
  112. }
  113. static void HandleMediaDurationChanged(const libvlc_event_t * event, void * self)
  114. {
  115. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  116. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  117. withMethod:@selector(setLength:)
  118. withArgumentAsObject:[VLCTime timeWithNumber:
  119. [NSNumber numberWithLongLong:event->u.media_duration_changed.new_duration]]];
  120. [pool drain];
  121. }
  122. static void HandleMediaStateChanged(const libvlc_event_t * event, void * self)
  123. {
  124. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  125. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  126. withMethod:@selector(setStateAsNumber:)
  127. withArgumentAsObject:[NSNumber numberWithInt:
  128. LibVLCStateToMediaState(event->u.media_state_changed.new_state)]];
  129. [pool drain];
  130. }
  131. static void HandleMediaSubItemAdded(const libvlc_event_t * event, void * self)
  132. {
  133. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  134. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  135. withMethod:@selector(subItemAdded)
  136. withArgumentAsObject:nil];
  137. [pool drain];
  138. }
  139. static void HandleMediaParsedChanged(const libvlc_event_t * event, void * self)
  140. {
  141. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  142. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  143. withMethod:@selector(parsedChanged:)
  144. withArgumentAsObject:[NSNumber numberWithBool:event->u.media_parsed_changed.new_status]];
  145. [pool release];
  146. }
  147. /******************************************************************************
  148. * Implementation
  149. */
  150. @implementation VLCMedia
  151. + (id)mediaWithURL:(NSURL *)anURL;
  152. {
  153. return [[[VLCMedia alloc] initWithURL:anURL] autorelease];
  154. }
  155. + (id)mediaWithPath:(NSString *)aPath;
  156. {
  157. return [[[VLCMedia alloc] initWithPath:aPath] autorelease];
  158. }
  159. + (id)mediaAsNodeWithName:(NSString *)aName;
  160. {
  161. return [[[VLCMedia alloc] initAsNodeWithName:aName] autorelease];
  162. }
  163. - (id)initWithPath:(NSString *)aPath
  164. {
  165. return [self initWithURL:[NSURL fileURLWithPath:aPath isDirectory:NO]];
  166. }
  167. - (id)initWithURL:(NSURL *)anURL
  168. {
  169. if (self = [super init])
  170. {
  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. {
  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)setValue:(NSString *)value forMeta:(NSString *)meta
  198. {
  199. libvlc_meta_t metaName = [VLCMedia stringToMetaType:meta];
  200. NSAssert(metaName >= 0, @"Invalid meta");
  201. libvlc_media_set_meta(p_md, metaName, [value UTF8String]);
  202. }
  203. - (void)dealloc
  204. {
  205. libvlc_event_manager_t * p_em = libvlc_media_event_manager(p_md);
  206. libvlc_event_detach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, self);
  207. libvlc_event_detach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, self);
  208. libvlc_event_detach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, self);
  209. libvlc_event_detach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, self);
  210. libvlc_event_detach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, self);
  211. [[VLCEventManager sharedManager] cancelCallToObject:self];
  212. // Testing to see if the pointer exists is not required, if the pointer is null
  213. // then the release message is not sent to it.
  214. delegate = nil;
  215. [length release];
  216. [url release];
  217. [subitems release];
  218. [metaDictionary release];
  219. libvlc_media_release( p_md );
  220. [super dealloc];
  221. }
  222. - (NSString *)description
  223. {
  224. NSString * result = [metaDictionary objectForKey:VLCMetaInformationTitle];
  225. return [NSString stringWithFormat:@"<%@ %p> %@", [self class], self, (result ? result : [url absoluteString])];
  226. }
  227. - (NSComparisonResult)compare:(VLCMedia *)media
  228. {
  229. if (self == media)
  230. return NSOrderedSame;
  231. if (!media)
  232. return NSOrderedDescending;
  233. return p_md == [media libVLCMediaDescriptor] ? NSOrderedSame : NSOrderedAscending;
  234. }
  235. @synthesize delegate;
  236. - (VLCTime *)length
  237. {
  238. if (!length)
  239. {
  240. // Try figuring out what the length is
  241. long long duration = libvlc_media_get_duration( p_md );
  242. if (duration > -1)
  243. {
  244. length = [[VLCTime timeWithNumber:[NSNumber numberWithLongLong:duration]] retain];
  245. return [[length retain] autorelease];
  246. }
  247. return [VLCTime nullTime];
  248. }
  249. return [[length retain] autorelease];
  250. }
  251. - (VLCTime *)lengthWaitUntilDate:(NSDate *)aDate
  252. {
  253. static const long long thread_sleep = 10000;
  254. if (!length)
  255. {
  256. // Force parsing of this item.
  257. [self parseIfNeeded];
  258. // wait until we are preparsed
  259. while (!length && !libvlc_media_is_parsed(p_md) && [aDate timeIntervalSinceNow] > 0)
  260. {
  261. usleep( thread_sleep );
  262. }
  263. // So we're done waiting, but sometimes we trap the fact that the parsing
  264. // was done before the length gets assigned, so lets go ahead and assign
  265. // it ourselves.
  266. if (!length)
  267. return [self length];
  268. }
  269. return [[length retain] autorelease];
  270. }
  271. - (BOOL)isParsed
  272. {
  273. return isParsed;
  274. }
  275. - (void)parse
  276. {
  277. libvlc_media_parse_async(p_md);
  278. }
  279. NSString *VLCMediaTracksInformationCodec = @"codec"; // NSNumber
  280. NSString *VLCMediaTracksInformationId = @"id"; // NSNumber
  281. NSString *VLCMediaTracksInformationType = @"type"; // NSString
  282. NSString *VLCMediaTracksInformationTypeAudio = @"audio";
  283. NSString *VLCMediaTracksInformationTypeVideo = @"video";
  284. NSString *VLCMediaTracksInformationTypeText = @"text";
  285. NSString *VLCMediaTracksInformationTypeUnknown = @"unknown";
  286. NSString *VLCMediaTracksInformationCodecProfile = @"profile"; // NSNumber
  287. NSString *VLCMediaTracksInformationCodecLevel = @"level"; // NSNumber
  288. NSString *VLCMediaTracksInformationAudioChannelsNumber = @"channelsNumber"; // NSNumber
  289. NSString *VLCMediaTracksInformationAudioRate = @"rate"; // NSNumber
  290. NSString *VLCMediaTracksInformationVideoHeight = @"height"; // NSNumber
  291. NSString *VLCMediaTracksInformationVideoWidth = @"width"; // NSNumber
  292. - (NSArray *)tracksInformation
  293. {
  294. // Trigger parsing if needed
  295. [self parseIfNeeded];
  296. libvlc_media_track_info_t *tracksInfo;
  297. int count = libvlc_media_get_tracks_info(p_md, &tracksInfo);
  298. NSMutableArray *array = [NSMutableArray array];
  299. for (int i = 0; i < count; i++) {
  300. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  301. [NSNumber numberWithUnsignedInt:tracksInfo[i].i_codec], VLCMediaTracksInformationCodec,
  302. [NSNumber numberWithInt:tracksInfo[i].i_id], VLCMediaTracksInformationId,
  303. [NSNumber numberWithInt:tracksInfo[i].i_profile], VLCMediaTracksInformationCodecProfile,
  304. [NSNumber numberWithInt:tracksInfo[i].i_level], VLCMediaTracksInformationCodecLevel,
  305. nil];
  306. NSString *type;
  307. switch (tracksInfo[i].i_type) {
  308. case libvlc_track_audio:
  309. type = VLCMediaTracksInformationTypeAudio;
  310. NSNumber *level = [NSNumber numberWithUnsignedInt:tracksInfo[i].u.audio.i_channels];
  311. NSNumber *rate = [NSNumber numberWithUnsignedInt:tracksInfo[i].u.audio.i_rate];
  312. [dictionary setObject:level forKey:VLCMediaTracksInformationAudioChannelsNumber];
  313. [dictionary setObject:rate forKey:VLCMediaTracksInformationAudioRate];
  314. break;
  315. case libvlc_track_video:
  316. type = VLCMediaTracksInformationTypeVideo;
  317. NSNumber *width = [NSNumber numberWithUnsignedInt:tracksInfo[i].u.video.i_width];
  318. NSNumber *height = [NSNumber numberWithUnsignedInt:tracksInfo[i].u.video.i_height];
  319. [dictionary setObject:width forKey:VLCMediaTracksInformationVideoWidth];
  320. [dictionary setObject:height forKey:VLCMediaTracksInformationVideoHeight];
  321. break;
  322. case libvlc_track_text:
  323. type = VLCMediaTracksInformationTypeText;
  324. [dictionary setObject:VLCMediaTracksInformationTypeText forKey:VLCMediaTracksInformationType];
  325. break;
  326. case libvlc_track_unknown:
  327. default:
  328. type = VLCMediaTracksInformationTypeUnknown;
  329. break;
  330. }
  331. [dictionary setValue:type forKey:VLCMediaTracksInformationType];
  332. [array addObject:dictionary];
  333. }
  334. free(tracksInfo);
  335. return array;
  336. }
  337. @synthesize url;
  338. @synthesize subitems;
  339. @synthesize metaDictionary;
  340. @synthesize state;
  341. @end
  342. /******************************************************************************
  343. * Implementation VLCMedia (LibVLCBridging)
  344. */
  345. @implementation VLCMedia (LibVLCBridging)
  346. + (id)mediaWithLibVLCMediaDescriptor:(void *)md
  347. {
  348. return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor:md] autorelease];
  349. }
  350. - (id)initWithLibVLCMediaDescriptor:(void *)md
  351. {
  352. if (self = [super init])
  353. {
  354. libvlc_media_retain( md );
  355. p_md = md;
  356. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  357. [self initInternalMediaDescriptor];
  358. }
  359. return self;
  360. }
  361. - (void *)libVLCMediaDescriptor
  362. {
  363. return p_md;
  364. }
  365. + (id)mediaWithMedia:(VLCMedia *)media andLibVLCOptions:(NSDictionary *)options
  366. {
  367. libvlc_media_t * p_md;
  368. p_md = libvlc_media_duplicate( [media libVLCMediaDescriptor] );
  369. for( NSString * key in [options allKeys] )
  370. {
  371. if ( [options objectForKey:key] != [NSNull null] )
  372. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, [options objectForKey:key]] UTF8String]);
  373. else
  374. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
  375. }
  376. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  377. }
  378. @end
  379. /******************************************************************************
  380. * Implementation VLCMedia (Private)
  381. */
  382. @implementation VLCMedia (Private)
  383. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  384. {
  385. static NSDictionary * stringToMetaDictionary = nil;
  386. // TODO: Thread safe-ize
  387. if( !stringToMetaDictionary )
  388. {
  389. #define VLCStringToMeta( name ) [NSNumber numberWithInt: libvlc_meta_##name], VLCMetaInformation##name
  390. stringToMetaDictionary =
  391. [[NSDictionary dictionaryWithObjectsAndKeys:
  392. VLCStringToMeta(Title),
  393. VLCStringToMeta(Artist),
  394. VLCStringToMeta(Genre),
  395. VLCStringToMeta(Copyright),
  396. VLCStringToMeta(Album),
  397. VLCStringToMeta(TrackNumber),
  398. VLCStringToMeta(Description),
  399. VLCStringToMeta(Rating),
  400. VLCStringToMeta(Date),
  401. VLCStringToMeta(Setting),
  402. VLCStringToMeta(URL),
  403. VLCStringToMeta(Language),
  404. VLCStringToMeta(NowPlaying),
  405. VLCStringToMeta(Publisher),
  406. VLCStringToMeta(ArtworkURL),
  407. VLCStringToMeta(TrackID),
  408. nil] retain];
  409. #undef VLCStringToMeta
  410. }
  411. NSNumber * number = [stringToMetaDictionary objectForKey:string];
  412. return number ? [number intValue] : -1;
  413. }
  414. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  415. {
  416. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  417. VLCMetaToString(Title, type);
  418. VLCMetaToString(Artist, type);
  419. VLCMetaToString(Genre, type);
  420. VLCMetaToString(Copyright, type);
  421. VLCMetaToString(Album, type);
  422. VLCMetaToString(TrackNumber, type);
  423. VLCMetaToString(Description, type);
  424. VLCMetaToString(Rating, type);
  425. VLCMetaToString(Date, type);
  426. VLCMetaToString(Setting, type);
  427. VLCMetaToString(URL, type);
  428. VLCMetaToString(Language, type);
  429. VLCMetaToString(NowPlaying, type);
  430. VLCMetaToString(Publisher, type);
  431. VLCMetaToString(ArtworkURL, type);
  432. VLCMetaToString(TrackID, type);
  433. #undef VLCMetaToString
  434. return nil;
  435. }
  436. - (void)initInternalMediaDescriptor
  437. {
  438. char * p_url = libvlc_media_get_mrl( p_md );
  439. url = [[NSURL URLWithString:[NSString stringWithUTF8String:p_url]] retain];
  440. if( !url ) /* Attempt to interpret as a file path then */
  441. url = [[NSURL fileURLWithPath:[NSString stringWithUTF8String:p_url]] retain];
  442. free( p_url );
  443. libvlc_media_set_user_data( p_md, (void*)self );
  444. libvlc_event_manager_t * p_em = libvlc_media_event_manager( p_md );
  445. libvlc_event_attach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, self);
  446. libvlc_event_attach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, self);
  447. libvlc_event_attach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, self);
  448. libvlc_event_attach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, self);
  449. libvlc_event_attach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, self);
  450. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  451. if (!p_mlist)
  452. subitems = nil;
  453. else
  454. {
  455. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  456. libvlc_media_list_release( p_mlist );
  457. }
  458. isParsed = libvlc_media_is_parsed(p_md);
  459. state = LibVLCStateToMediaState(libvlc_media_get_state( p_md ));
  460. }
  461. - (void)fetchMetaInformationFromLibVLCWithType:(NSString *)metaType
  462. {
  463. char * psz_value = libvlc_media_get_meta( p_md, [VLCMedia stringToMetaType:metaType] );
  464. NSString * newValue = psz_value ? [NSString stringWithUTF8String: psz_value] : nil;
  465. NSString * oldValue = [metaDictionary valueForKey:metaType];
  466. free(psz_value);
  467. if ( newValue != oldValue && !(oldValue && newValue && [oldValue compare:newValue] == NSOrderedSame) )
  468. {
  469. // Only fetch the art if needed. (ie, create the NSImage, if it was requested before)
  470. if (isArtFetched && [metaType isEqualToString:VLCMetaInformationArtworkURL])
  471. {
  472. [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
  473. toTarget:self
  474. withObject:newValue];
  475. }
  476. [metaDictionary setValue:newValue forKeyPath:metaType];
  477. }
  478. }
  479. #if !TARGET_OS_IPHONE
  480. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
  481. {
  482. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  483. NSImage * art = nil;
  484. if( anURL )
  485. {
  486. // Go ahead and load up the art work
  487. NSURL * artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  488. // Don't attempt to fetch artwork from remote. Core will do that alone
  489. if ([artUrl isFileURL])
  490. art = [[[NSImage alloc] initWithContentsOfURL:artUrl] autorelease];
  491. }
  492. // If anything was found, lets save it to the meta data dictionary
  493. [self performSelectorOnMainThread:@selector(setArtwork:) withObject:art waitUntilDone:NO];
  494. [pool release];
  495. }
  496. - (void)setArtwork:(NSImage *)art
  497. {
  498. if (!art)
  499. {
  500. [metaDictionary removeObjectForKey:@"artwork"];
  501. return;
  502. }
  503. [metaDictionary setObject:art forKey:@"artwork"];
  504. }
  505. #endif
  506. - (void)parseIfNeeded
  507. {
  508. if (![self isParsed])
  509. [self parse];
  510. }
  511. - (void)metaChanged:(NSString *)metaType
  512. {
  513. [self fetchMetaInformationFromLibVLCWithType:metaType];
  514. }
  515. - (void)subItemAdded
  516. {
  517. if( subitems )
  518. return; /* Nothing to do */
  519. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  520. NSAssert( p_mlist, @"The mlist shouldn't be nil, we are receiving a subItemAdded");
  521. [self willChangeValueForKey:@"subitems"];
  522. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  523. [self didChangeValueForKey:@"subitems"];
  524. libvlc_media_list_release( p_mlist );
  525. }
  526. - (void)parsedChanged:(NSNumber *)isParsedAsNumber
  527. {
  528. [self willChangeValueForKey:@"parsed"];
  529. isParsed = [isParsedAsNumber boolValue];
  530. [self didChangeValueForKey:@"parsed"];
  531. // FIXME: Probably don't even call this if there is no delegate.
  532. if (!delegate || !isParsed)
  533. return;
  534. if ([delegate respondsToSelector:@selector(mediaDidFinishParsing:)]) {
  535. [delegate mediaDidFinishParsing:self];
  536. }
  537. }
  538. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber
  539. {
  540. [self setState: [newStateAsNumber intValue]];
  541. }
  542. #if TARGET_OS_IPHONE
  543. - (NSDictionary *)metaDictionary
  544. {
  545. if (!areOthersMetaFetched) {
  546. areOthersMetaFetched = YES;
  547. /* Force VLCMetaInformationTitle, that will trigger preparsing
  548. * And all the other meta will be added through the libvlc event system */
  549. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  550. }
  551. if (!isArtURLFetched)
  552. {
  553. isArtURLFetched = YES;
  554. /* Force isArtURLFetched, that will trigger artwork download eventually
  555. * And all the other meta will be added through the libvlc event system */
  556. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  557. }
  558. return metaDictionary;
  559. }
  560. #else
  561. - (id)valueForKeyPath:(NSString *)keyPath
  562. {
  563. if( !isArtFetched && [keyPath isEqualToString:@"metaDictionary.artwork"])
  564. {
  565. isArtFetched = YES;
  566. /* Force the retrieval of the artwork now that someone asked for it */
  567. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  568. }
  569. else if( !areOthersMetaFetched && [keyPath hasPrefix:@"metaDictionary."])
  570. {
  571. areOthersMetaFetched = YES;
  572. /* Force VLCMetaInformationTitle, that will trigger preparsing
  573. * And all the other meta will be added through the libvlc event system */
  574. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  575. }
  576. else if( !isArtURLFetched && [keyPath hasPrefix:@"metaDictionary.artworkURL"])
  577. {
  578. isArtURLFetched = YES;
  579. /* Force isArtURLFetched, that will trigger artwork download eventually
  580. * And all the other meta will be added through the libvlc event system */
  581. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  582. }
  583. return [super valueForKeyPath:keyPath];
  584. }
  585. #endif
  586. @end
  587. /******************************************************************************
  588. * Implementation VLCMedia (VLCMediaPlayerBridging)
  589. */
  590. @implementation VLCMedia (VLCMediaPlayerBridging)
  591. - (void)setLength:(VLCTime *)value
  592. {
  593. if (length && value && [length compare:value] == NSOrderedSame)
  594. return;
  595. [length release];
  596. length = value ? [value retain] : nil;
  597. }
  598. @end