VLCMedia.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. - (void)addOptions:(NSDictionary*)options
  280. {
  281. if (p_md)
  282. {
  283. for (NSString * key in [options allKeys])
  284. {
  285. if ([options objectForKey:key] != [NSNull null])
  286. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, [options objectForKey:key]] UTF8String]);
  287. else
  288. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
  289. }
  290. }
  291. }
  292. - (NSDictionary*) stats
  293. {
  294. if(!p_md)
  295. return NULL;
  296. NSMutableDictionary *d = [NSMutableDictionary dictionary];
  297. libvlc_media_stats_t p_stats;
  298. libvlc_media_get_stats(p_md, &p_stats);
  299. [d setObject:[NSNumber numberWithFloat: p_stats.f_demux_bitrate] forKey:@"demuxBitrate"];
  300. [d setObject:[NSNumber numberWithFloat: p_stats.f_input_bitrate] forKey:@"inputBitrate"];
  301. [d setObject:[NSNumber numberWithFloat: p_stats.f_send_bitrate] forKey:@"sendBitrate"];
  302. [d setObject:[NSNumber numberWithInt: p_stats.i_decoded_audio] forKey:@"decodedAudio"];
  303. [d setObject:[NSNumber numberWithInt: p_stats.i_decoded_video] forKey:@"decodedVideo"];
  304. [d setObject:[NSNumber numberWithInt: p_stats.i_demux_corrupted] forKey:@"demuxCorrupted"];
  305. [d setObject:[NSNumber numberWithInt: p_stats.i_demux_discontinuity] forKey:@"demuxDiscontinuity"];
  306. [d setObject:[NSNumber numberWithInt: p_stats.i_demux_read_bytes] forKey:@"demuxReadBytes"];
  307. [d setObject:[NSNumber numberWithInt: p_stats.i_displayed_pictures] forKey:@"displayedPictures"];
  308. [d setObject:[NSNumber numberWithInt: p_stats.i_lost_abuffers] forKey:@"lostAbuffers"];
  309. [d setObject:[NSNumber numberWithInt: p_stats.i_lost_pictures] forKey:@"lostPictures"];
  310. [d setObject:[NSNumber numberWithInt: p_stats.i_played_abuffers] forKey:@"playedAbuffers"];
  311. [d setObject:[NSNumber numberWithInt: p_stats.i_read_bytes] forKey:@"readBytes"];
  312. [d setObject:[NSNumber numberWithInt: p_stats.i_sent_bytes] forKey:@"sentBytes"];
  313. [d setObject:[NSNumber numberWithInt: p_stats.i_sent_packets] forKey:@"sentPackets"];
  314. return d;
  315. }
  316. NSString *VLCMediaTracksInformationCodec = @"codec"; // NSNumber
  317. NSString *VLCMediaTracksInformationId = @"id"; // NSNumber
  318. NSString *VLCMediaTracksInformationType = @"type"; // NSString
  319. NSString *VLCMediaTracksInformationTypeAudio = @"audio";
  320. NSString *VLCMediaTracksInformationTypeVideo = @"video";
  321. NSString *VLCMediaTracksInformationTypeText = @"text";
  322. NSString *VLCMediaTracksInformationTypeUnknown = @"unknown";
  323. NSString *VLCMediaTracksInformationCodecProfile = @"profile"; // NSNumber
  324. NSString *VLCMediaTracksInformationCodecLevel = @"level"; // NSNumber
  325. NSString *VLCMediaTracksInformationAudioChannelsNumber = @"channelsNumber"; // NSNumber
  326. NSString *VLCMediaTracksInformationAudioRate = @"rate"; // NSNumber
  327. NSString *VLCMediaTracksInformationVideoHeight = @"height"; // NSNumber
  328. NSString *VLCMediaTracksInformationVideoWidth = @"width"; // NSNumber
  329. - (NSArray *)tracksInformation
  330. {
  331. // Trigger parsing if needed
  332. [self parseIfNeeded];
  333. libvlc_media_track_info_t *tracksInfo;
  334. int count = libvlc_media_get_tracks_info(p_md, &tracksInfo);
  335. NSMutableArray *array = [NSMutableArray array];
  336. for (int i = 0; i < count; i++) {
  337. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  338. [NSNumber numberWithUnsignedInt:tracksInfo[i].i_codec], VLCMediaTracksInformationCodec,
  339. [NSNumber numberWithInt:tracksInfo[i].i_id], VLCMediaTracksInformationId,
  340. [NSNumber numberWithInt:tracksInfo[i].i_profile], VLCMediaTracksInformationCodecProfile,
  341. [NSNumber numberWithInt:tracksInfo[i].i_level], VLCMediaTracksInformationCodecLevel,
  342. nil];
  343. NSString *type;
  344. switch (tracksInfo[i].i_type) {
  345. case libvlc_track_audio:
  346. type = VLCMediaTracksInformationTypeAudio;
  347. NSNumber *level = [NSNumber numberWithUnsignedInt:tracksInfo[i].u.audio.i_channels];
  348. NSNumber *rate = [NSNumber numberWithUnsignedInt:tracksInfo[i].u.audio.i_rate];
  349. [dictionary setObject:level forKey:VLCMediaTracksInformationAudioChannelsNumber];
  350. [dictionary setObject:rate forKey:VLCMediaTracksInformationAudioRate];
  351. break;
  352. case libvlc_track_video:
  353. type = VLCMediaTracksInformationTypeVideo;
  354. NSNumber *width = [NSNumber numberWithUnsignedInt:tracksInfo[i].u.video.i_width];
  355. NSNumber *height = [NSNumber numberWithUnsignedInt:tracksInfo[i].u.video.i_height];
  356. [dictionary setObject:width forKey:VLCMediaTracksInformationVideoWidth];
  357. [dictionary setObject:height forKey:VLCMediaTracksInformationVideoHeight];
  358. break;
  359. case libvlc_track_text:
  360. type = VLCMediaTracksInformationTypeText;
  361. [dictionary setObject:VLCMediaTracksInformationTypeText forKey:VLCMediaTracksInformationType];
  362. break;
  363. case libvlc_track_unknown:
  364. default:
  365. type = VLCMediaTracksInformationTypeUnknown;
  366. break;
  367. }
  368. [dictionary setValue:type forKey:VLCMediaTracksInformationType];
  369. [array addObject:dictionary];
  370. }
  371. free(tracksInfo);
  372. return array;
  373. }
  374. @synthesize url;
  375. @synthesize subitems;
  376. @synthesize metaDictionary;
  377. @synthesize state;
  378. @end
  379. /******************************************************************************
  380. * Implementation VLCMedia (LibVLCBridging)
  381. */
  382. @implementation VLCMedia (LibVLCBridging)
  383. + (id)mediaWithLibVLCMediaDescriptor:(void *)md
  384. {
  385. return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor:md] autorelease];
  386. }
  387. - (id)initWithLibVLCMediaDescriptor:(void *)md
  388. {
  389. if (self = [super init])
  390. {
  391. libvlc_media_retain( md );
  392. p_md = md;
  393. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  394. [self initInternalMediaDescriptor];
  395. }
  396. return self;
  397. }
  398. - (void *)libVLCMediaDescriptor
  399. {
  400. return p_md;
  401. }
  402. + (id)mediaWithMedia:(VLCMedia *)media andLibVLCOptions:(NSDictionary *)options
  403. {
  404. libvlc_media_t * p_md;
  405. p_md = libvlc_media_duplicate( [media libVLCMediaDescriptor] );
  406. for( NSString * key in [options allKeys] )
  407. {
  408. if ( [options objectForKey:key] != [NSNull null] )
  409. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, [options objectForKey:key]] UTF8String]);
  410. else
  411. libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
  412. }
  413. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  414. }
  415. @end
  416. /******************************************************************************
  417. * Implementation VLCMedia (Private)
  418. */
  419. @implementation VLCMedia (Private)
  420. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  421. {
  422. static NSDictionary * stringToMetaDictionary = nil;
  423. // TODO: Thread safe-ize
  424. if( !stringToMetaDictionary )
  425. {
  426. #define VLCStringToMeta( name ) [NSNumber numberWithInt: libvlc_meta_##name], VLCMetaInformation##name
  427. stringToMetaDictionary =
  428. [[NSDictionary dictionaryWithObjectsAndKeys:
  429. VLCStringToMeta(Title),
  430. VLCStringToMeta(Artist),
  431. VLCStringToMeta(Genre),
  432. VLCStringToMeta(Copyright),
  433. VLCStringToMeta(Album),
  434. VLCStringToMeta(TrackNumber),
  435. VLCStringToMeta(Description),
  436. VLCStringToMeta(Rating),
  437. VLCStringToMeta(Date),
  438. VLCStringToMeta(Setting),
  439. VLCStringToMeta(URL),
  440. VLCStringToMeta(Language),
  441. VLCStringToMeta(NowPlaying),
  442. VLCStringToMeta(Publisher),
  443. VLCStringToMeta(ArtworkURL),
  444. VLCStringToMeta(TrackID),
  445. nil] retain];
  446. #undef VLCStringToMeta
  447. }
  448. NSNumber * number = [stringToMetaDictionary objectForKey:string];
  449. return number ? [number intValue] : -1;
  450. }
  451. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  452. {
  453. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  454. VLCMetaToString(Title, type);
  455. VLCMetaToString(Artist, type);
  456. VLCMetaToString(Genre, type);
  457. VLCMetaToString(Copyright, type);
  458. VLCMetaToString(Album, type);
  459. VLCMetaToString(TrackNumber, type);
  460. VLCMetaToString(Description, type);
  461. VLCMetaToString(Rating, type);
  462. VLCMetaToString(Date, type);
  463. VLCMetaToString(Setting, type);
  464. VLCMetaToString(URL, type);
  465. VLCMetaToString(Language, type);
  466. VLCMetaToString(NowPlaying, type);
  467. VLCMetaToString(Publisher, type);
  468. VLCMetaToString(ArtworkURL, type);
  469. VLCMetaToString(TrackID, type);
  470. #undef VLCMetaToString
  471. return nil;
  472. }
  473. - (void)initInternalMediaDescriptor
  474. {
  475. char * p_url = libvlc_media_get_mrl( p_md );
  476. url = [[NSURL URLWithString:[NSString stringWithUTF8String:p_url]] retain];
  477. if( !url ) /* Attempt to interpret as a file path then */
  478. url = [[NSURL fileURLWithPath:[NSString stringWithUTF8String:p_url]] retain];
  479. free( p_url );
  480. libvlc_media_set_user_data( p_md, (void*)self );
  481. libvlc_event_manager_t * p_em = libvlc_media_event_manager( p_md );
  482. libvlc_event_attach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, self);
  483. libvlc_event_attach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, self);
  484. libvlc_event_attach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, self);
  485. libvlc_event_attach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, self);
  486. libvlc_event_attach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, self);
  487. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  488. if (!p_mlist)
  489. subitems = nil;
  490. else
  491. {
  492. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  493. libvlc_media_list_release( p_mlist );
  494. }
  495. isParsed = libvlc_media_is_parsed(p_md);
  496. state = LibVLCStateToMediaState(libvlc_media_get_state( p_md ));
  497. }
  498. - (void)fetchMetaInformationFromLibVLCWithType:(NSString *)metaType
  499. {
  500. char * psz_value = libvlc_media_get_meta( p_md, [VLCMedia stringToMetaType:metaType] );
  501. NSString * newValue = psz_value ? [NSString stringWithUTF8String: psz_value] : nil;
  502. NSString * oldValue = [metaDictionary valueForKey:metaType];
  503. free(psz_value);
  504. if ( newValue != oldValue && !(oldValue && newValue && [oldValue compare:newValue] == NSOrderedSame) )
  505. {
  506. // Only fetch the art if needed. (ie, create the NSImage, if it was requested before)
  507. if (isArtFetched && [metaType isEqualToString:VLCMetaInformationArtworkURL])
  508. {
  509. [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
  510. toTarget:self
  511. withObject:newValue];
  512. }
  513. [metaDictionary setValue:newValue forKeyPath:metaType];
  514. }
  515. }
  516. #if !TARGET_OS_IPHONE
  517. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
  518. {
  519. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  520. NSImage * art = nil;
  521. if( anURL )
  522. {
  523. // Go ahead and load up the art work
  524. NSURL * artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  525. // Don't attempt to fetch artwork from remote. Core will do that alone
  526. if ([artUrl isFileURL])
  527. art = [[[NSImage alloc] initWithContentsOfURL:artUrl] autorelease];
  528. }
  529. // If anything was found, lets save it to the meta data dictionary
  530. [self performSelectorOnMainThread:@selector(setArtwork:) withObject:art waitUntilDone:NO];
  531. [pool release];
  532. }
  533. - (void)setArtwork:(NSImage *)art
  534. {
  535. if (!art)
  536. {
  537. [metaDictionary removeObjectForKey:@"artwork"];
  538. return;
  539. }
  540. [metaDictionary setObject:art forKey:@"artwork"];
  541. }
  542. #endif
  543. - (void)parseIfNeeded
  544. {
  545. if (![self isParsed])
  546. [self parse];
  547. }
  548. - (void)metaChanged:(NSString *)metaType
  549. {
  550. [self fetchMetaInformationFromLibVLCWithType:metaType];
  551. }
  552. - (void)subItemAdded
  553. {
  554. if( subitems )
  555. return; /* Nothing to do */
  556. libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
  557. NSAssert( p_mlist, @"The mlist shouldn't be nil, we are receiving a subItemAdded");
  558. [self willChangeValueForKey:@"subitems"];
  559. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  560. [self didChangeValueForKey:@"subitems"];
  561. libvlc_media_list_release( p_mlist );
  562. }
  563. - (void)parsedChanged:(NSNumber *)isParsedAsNumber
  564. {
  565. [self willChangeValueForKey:@"parsed"];
  566. isParsed = [isParsedAsNumber boolValue];
  567. [self didChangeValueForKey:@"parsed"];
  568. // FIXME: Probably don't even call this if there is no delegate.
  569. if (!delegate || !isParsed)
  570. return;
  571. if ([delegate respondsToSelector:@selector(mediaDidFinishParsing:)]) {
  572. [delegate mediaDidFinishParsing:self];
  573. }
  574. }
  575. - (void)setStateAsNumber:(NSNumber *)newStateAsNumber
  576. {
  577. [self setState: [newStateAsNumber intValue]];
  578. }
  579. #if TARGET_OS_IPHONE
  580. - (NSDictionary *)metaDictionary
  581. {
  582. if (!areOthersMetaFetched) {
  583. areOthersMetaFetched = YES;
  584. /* Force VLCMetaInformationTitle, that will trigger preparsing
  585. * And all the other meta will be added through the libvlc event system */
  586. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  587. }
  588. if (!isArtURLFetched)
  589. {
  590. isArtURLFetched = YES;
  591. /* Force isArtURLFetched, that will trigger artwork download eventually
  592. * And all the other meta will be added through the libvlc event system */
  593. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  594. }
  595. return metaDictionary;
  596. }
  597. #else
  598. - (id)valueForKeyPath:(NSString *)keyPath
  599. {
  600. if( !isArtFetched && [keyPath isEqualToString:@"metaDictionary.artwork"])
  601. {
  602. isArtFetched = YES;
  603. /* Force the retrieval of the artwork now that someone asked for it */
  604. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  605. }
  606. else if( !areOthersMetaFetched && [keyPath hasPrefix:@"metaDictionary."])
  607. {
  608. areOthersMetaFetched = YES;
  609. /* Force VLCMetaInformationTitle, that will trigger preparsing
  610. * And all the other meta will be added through the libvlc event system */
  611. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  612. }
  613. else if( !isArtURLFetched && [keyPath hasPrefix:@"metaDictionary.artworkURL"])
  614. {
  615. isArtURLFetched = YES;
  616. /* Force isArtURLFetched, that will trigger artwork download eventually
  617. * And all the other meta will be added through the libvlc event system */
  618. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
  619. }
  620. return [super valueForKeyPath:keyPath];
  621. }
  622. #endif
  623. @end
  624. /******************************************************************************
  625. * Implementation VLCMedia (VLCMediaPlayerBridging)
  626. */
  627. @implementation VLCMedia (VLCMediaPlayerBridging)
  628. - (void)setLength:(VLCTime *)value
  629. {
  630. if (length && value && [length compare:value] == NSOrderedSame)
  631. return;
  632. [length release];
  633. length = value ? [value retain] : nil;
  634. }
  635. @end