VLCMedia.m 26 KB

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