VLCMedia.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*****************************************************************************
  2. * VLCMedia.m: VLC.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. * Interface (Private)
  53. */
  54. // TODO: Documentation
  55. @interface VLCMedia (Private)
  56. /* Statics */
  57. + (libvlc_meta_t)stringToMetaType:(NSString *)string;
  58. + (NSString *)metaTypeToString:(libvlc_meta_t)type;
  59. /* Initializers */
  60. - (void)initInternalMediaDescriptor;
  61. /* Operations */
  62. - (void)fetchMetaInformationFromLibVLCWithType:(NSString*)metaType;
  63. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL;
  64. /* Callback Methods */
  65. - (void)metaChanged:(NSString *)metaType;
  66. - (void)subItemAdded;
  67. @end
  68. static VLCMediaState libvlc_state_to_media_state[] =
  69. {
  70. [libvlc_NothingSpecial] = VLCMediaStateNothingSpecial,
  71. [libvlc_Stopped] = VLCMediaStateNothingSpecial,
  72. [libvlc_Opening] = VLCMediaStateNothingSpecial,
  73. [libvlc_Buffering] = VLCMediaStateBuffering,
  74. [libvlc_Ended] = VLCMediaStateNothingSpecial,
  75. [libvlc_Error] = VLCMediaStateError,
  76. [libvlc_Playing] = VLCMediaStatePlaying,
  77. [libvlc_Paused] = VLCMediaStatePlaying,
  78. };
  79. static inline VLCMediaState LibVLCStateToMediaState( libvlc_state_t state )
  80. {
  81. return libvlc_state_to_media_state[state];
  82. }
  83. /******************************************************************************
  84. * LibVLC Event Callback
  85. */
  86. static void HandleMediaMetaChanged(const libvlc_event_t * event, void * self)
  87. {
  88. if( event->u.media_descriptor_meta_changed.meta_type == libvlc_meta_Publisher ||
  89. event->u.media_descriptor_meta_changed.meta_type == libvlc_meta_NowPlaying )
  90. {
  91. /* Skip those meta. We don't really care about them for now.
  92. * And they occure a lot */
  93. return;
  94. }
  95. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  96. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  97. withMethod:@selector(metaChanged:)
  98. withArgumentAsObject:[VLCMedia metaTypeToString:event->u.media_descriptor_meta_changed.meta_type]];
  99. [pool release];
  100. }
  101. //static void HandleMediaDurationChanged(const libvlc_event_t * event, void * self)
  102. //{
  103. // NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  104. //
  105. // [[VLCEventManager sharedManager] callOnMainThreadObject:self
  106. // withMethod:@selector(setLength:)
  107. // withArgumentAsObject:[VLCTime timeWithNumber:
  108. // [NSNumber numberWithLongLong:event->u.media_descriptor_duration_changed.new_duration]]];
  109. // [pool release];
  110. //}
  111. static void HandleMediaStateChanged(const libvlc_event_t * event, void * self)
  112. {
  113. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  114. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  115. withMethod:@selector(setState:)
  116. withArgumentAsObject:[NSNumber numberWithInt:
  117. LibVLCStateToMediaState(event->u.media_descriptor_state_changed.new_state)]];
  118. [pool release];
  119. }
  120. static void HandleMediaSubItemAdded(const libvlc_event_t * event, void * self)
  121. {
  122. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  123. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  124. withMethod:@selector(subItemAdded)
  125. withArgumentAsObject:nil];
  126. [pool release];
  127. }
  128. /******************************************************************************
  129. * Implementation
  130. */
  131. @implementation VLCMedia
  132. + (id)mediaWithURL:(NSURL *)anURL;
  133. {
  134. return [[[VLCMedia alloc] initWithURL:anURL] autorelease];
  135. }
  136. + (id)mediaWithPath:(NSString *)aPath;
  137. {
  138. return [[[VLCMedia alloc] initWithPath:aPath] autorelease];
  139. }
  140. + (id)mediaAsNodeWithName:(NSString *)aName;
  141. {
  142. return [[[VLCMedia alloc] initAsNodeWithName:aName] autorelease];
  143. }
  144. - (id)initWithURL:(NSURL *)anURL
  145. {
  146. return [self initWithPath:[anURL path]];
  147. }
  148. - (id)initWithPath:(NSString *)aPath
  149. {
  150. if (self = [super init])
  151. {
  152. libvlc_exception_t ex;
  153. libvlc_exception_init(&ex);
  154. p_md = libvlc_media_descriptor_new([VLCLibrary sharedInstance],
  155. [aPath UTF8String],
  156. &ex);
  157. catch_exception(&ex);
  158. delegate = nil;
  159. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  160. // This value is set whenever the demuxer figures out what the length is.
  161. // TODO: Easy way to tell the length of the movie without having to instiate the demuxer. Maybe cached info?
  162. length = nil;
  163. [self initInternalMediaDescriptor];
  164. }
  165. return self;
  166. }
  167. - (id)initAsNodeWithName:(NSString *)aName
  168. {
  169. if (self = [super init])
  170. {
  171. libvlc_exception_t ex;
  172. libvlc_exception_init(&ex);
  173. p_md = libvlc_media_descriptor_new_as_node([VLCLibrary sharedInstance],
  174. [aName UTF8String],
  175. &ex);
  176. catch_exception(&ex);
  177. delegate = nil;
  178. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  179. // This value is set whenever the demuxer figures out what the length is.
  180. // TODO: Easy way to tell the length of the movie without having to instiate the demuxer. Maybe cached info?
  181. length = nil;
  182. [self initInternalMediaDescriptor];
  183. }
  184. return self;
  185. }
  186. - (void)release
  187. {
  188. @synchronized(self)
  189. {
  190. if([self retainCount] <= 1)
  191. {
  192. /* We must make sure we won't receive new event after an upcoming dealloc
  193. * We also may receive a -retain in some event callback that may occcur
  194. * Before libvlc_event_detach. So this can't happen in dealloc */
  195. libvlc_event_manager_t * p_em = libvlc_media_descriptor_event_manager(p_md, NULL);
  196. libvlc_event_detach(p_em, libvlc_MediaDescriptorMetaChanged, HandleMediaMetaChanged, self, NULL);
  197. // libvlc_event_detach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, NULL);
  198. libvlc_event_detach(p_em, libvlc_MediaDescriptorStateChanged, HandleMediaStateChanged, self, NULL);
  199. libvlc_event_detach(p_em, libvlc_MediaDescriptorSubItemAdded, HandleMediaSubItemAdded, self, NULL);
  200. }
  201. [super release];
  202. }
  203. }
  204. - (void)dealloc
  205. {
  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. [self setLength:nil];
  210. [url release];
  211. [subitems release];
  212. [metaDictionary release];
  213. libvlc_media_descriptor_release( p_md );
  214. [super dealloc];
  215. }
  216. - (NSString *)description
  217. {
  218. NSString * result = [metaDictionary objectForKey:VLCMetaInformationTitle];
  219. return [NSString stringWithFormat:@"<%@ %p> %@", [self className], self, (result ? result : [url absoluteString])];
  220. }
  221. - (NSComparisonResult)compare:(VLCMedia *)media
  222. {
  223. if (self == media)
  224. return NSOrderedSame;
  225. else if (!media)
  226. return NSOrderedDescending;
  227. else
  228. return [[[self url] absoluteString] compare:[[media url] absoluteString]];
  229. }
  230. @synthesize delegate;
  231. - (VLCTime *)length
  232. {
  233. if (!length)
  234. {
  235. // Try figuring out what the length is
  236. long long duration = libvlc_media_descriptor_get_duration( p_md, NULL );
  237. if (duration > -1)
  238. {
  239. [self setLength:[VLCTime timeWithNumber:[NSNumber numberWithLongLong:duration]]];
  240. return [[length retain] autorelease];
  241. }
  242. }
  243. return [VLCTime nullTime];
  244. }
  245. - (VLCTime *)lengthWaitUntilDate:(NSDate *)aDate
  246. {
  247. static const long long thread_sleep = 10000;
  248. if (!length)
  249. {
  250. while (!length && ![self isPreparsed] && [aDate timeIntervalSinceNow] > 0)
  251. {
  252. usleep( thread_sleep );
  253. }
  254. // So we're done waiting, but sometimes we trap the fact that the parsing
  255. // was done before the length gets assigned, so lets go ahead and assign
  256. // it ourselves.
  257. if (!length)
  258. return [self length];
  259. }
  260. return [[length retain] autorelease];
  261. }
  262. - (BOOL)isPreparsed
  263. {
  264. return libvlc_media_descriptor_is_preparsed( p_md, NULL );
  265. }
  266. @synthesize url;
  267. @synthesize subitems;
  268. @synthesize metaDictionary;
  269. @synthesize state;
  270. @end
  271. /******************************************************************************
  272. * Implementation VLCMedia (LibVLCBridging)
  273. */
  274. @implementation VLCMedia (LibVLCBridging)
  275. + (id)mediaWithLibVLCMediaDescriptor:(void *)md
  276. {
  277. return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor:md] autorelease];
  278. }
  279. - (id)initWithLibVLCMediaDescriptor:(void *)md
  280. {
  281. if (self = [super init])
  282. {
  283. libvlc_exception_t ex;
  284. libvlc_exception_init( &ex );
  285. libvlc_media_descriptor_retain( md );
  286. p_md = md;
  287. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  288. [self initInternalMediaDescriptor];
  289. }
  290. return self;
  291. }
  292. - (void *)libVLCMediaDescriptor
  293. {
  294. return p_md;
  295. }
  296. @end
  297. /******************************************************************************
  298. * Implementation VLCMedia (Private)
  299. */
  300. @implementation VLCMedia (Private)
  301. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  302. {
  303. static NSDictionary * stringToMetaDictionary = nil;
  304. // TODO: Thread safe-ize
  305. if( !stringToMetaDictionary )
  306. {
  307. #define VLCStringToMeta( name ) [NSNumber numberWithInt: libvlc_meta_##name], VLCMetaInformation##name
  308. stringToMetaDictionary =
  309. [[NSDictionary dictionaryWithObjectsAndKeys:
  310. VLCStringToMeta(Title),
  311. VLCStringToMeta(Artist),
  312. VLCStringToMeta(Genre),
  313. VLCStringToMeta(Copyright),
  314. VLCStringToMeta(Album),
  315. VLCStringToMeta(TrackNumber),
  316. VLCStringToMeta(Description),
  317. VLCStringToMeta(Rating),
  318. VLCStringToMeta(Date),
  319. VLCStringToMeta(Setting),
  320. VLCStringToMeta(URL),
  321. VLCStringToMeta(Language),
  322. VLCStringToMeta(NowPlaying),
  323. VLCStringToMeta(Publisher),
  324. VLCStringToMeta(ArtworkURL),
  325. VLCStringToMeta(TrackID),
  326. nil] retain];
  327. #undef VLCStringToMeta
  328. }
  329. NSNumber * number = [stringToMetaDictionary objectForKey:string];
  330. return number ? [number intValue] : -1;
  331. }
  332. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  333. {
  334. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  335. VLCMetaToString(Title, type);
  336. VLCMetaToString(Artist, type);
  337. VLCMetaToString(Genre, type);
  338. VLCMetaToString(Copyright, type);
  339. VLCMetaToString(Album, type);
  340. VLCMetaToString(TrackNumber, type);
  341. VLCMetaToString(Description, type);
  342. VLCMetaToString(Rating, type);
  343. VLCMetaToString(Date, type);
  344. VLCMetaToString(Setting, type);
  345. VLCMetaToString(URL, type);
  346. VLCMetaToString(Language, type);
  347. VLCMetaToString(NowPlaying, type);
  348. VLCMetaToString(Publisher, type);
  349. VLCMetaToString(ArtworkURL, type);
  350. VLCMetaToString(TrackID, type);
  351. #undef VLCMetaToString
  352. return nil;
  353. }
  354. - (void)initInternalMediaDescriptor
  355. {
  356. libvlc_exception_t ex;
  357. libvlc_exception_init( &ex );
  358. char * p_url = libvlc_media_descriptor_get_mrl( p_md, &ex );
  359. catch_exception( &ex );
  360. url = [[NSURL URLWithString:[NSString stringWithUTF8String:p_url]] retain];
  361. if( !url ) /* Attempt to interpret as a file path then */
  362. url = [[NSURL fileURLWithPath:[NSString stringWithUTF8String:p_url]] retain];
  363. free( p_url );
  364. libvlc_media_descriptor_set_user_data( p_md, (void*)self, &ex );
  365. catch_exception( &ex );
  366. libvlc_event_manager_t * p_em = libvlc_media_descriptor_event_manager( p_md, &ex );
  367. libvlc_event_attach(p_em, libvlc_MediaDescriptorMetaChanged, HandleMediaMetaChanged, self, &ex);
  368. // libvlc_event_attach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, &ex);
  369. libvlc_event_attach(p_em, libvlc_MediaDescriptorStateChanged, HandleMediaStateChanged, self, &ex);
  370. libvlc_event_attach(p_em, libvlc_MediaDescriptorSubItemAdded, HandleMediaSubItemAdded, self, &ex);
  371. catch_exception( &ex );
  372. libvlc_media_list_t * p_mlist = libvlc_media_descriptor_subitems( p_md, NULL );
  373. if (!p_mlist)
  374. subitems = nil;
  375. else
  376. {
  377. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  378. libvlc_media_list_release( p_mlist );
  379. }
  380. state = LibVLCStateToMediaState(libvlc_media_descriptor_get_state( p_md, NULL ));
  381. /* Force VLCMetaInformationTitle, that will trigger preparsing
  382. * And all the other meta will be added through the libvlc event system */
  383. [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
  384. }
  385. - (void)fetchMetaInformationFromLibVLCWithType:(NSString *)metaType
  386. {
  387. char * psz_value = libvlc_media_descriptor_get_meta( p_md, [VLCMedia stringToMetaType:metaType], NULL);
  388. NSString * newValue = psz_value ? [NSString stringWithUTF8String: psz_value] : nil;
  389. NSString * oldValue = [metaDictionary valueForKey:metaType];
  390. free(psz_value);
  391. if ( !(newValue && oldValue && [oldValue compare:newValue] == NSOrderedSame) )
  392. {
  393. if ([metaType isEqualToString:VLCMetaInformationArtworkURL])
  394. {
  395. [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
  396. toTarget:self
  397. withObject:newValue];
  398. return;
  399. }
  400. [metaDictionary setValue:newValue forKeyPath:metaType];
  401. }
  402. }
  403. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
  404. {
  405. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  406. // Go ahead and load up the art work
  407. NSURL * artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  408. NSImage * art = [[[NSImage alloc] initWithContentsOfURL:artUrl] autorelease];
  409. // If anything was found, lets save it to the meta data dictionary
  410. if (art)
  411. {
  412. @synchronized(metaDictionary)
  413. {
  414. [metaDictionary setObject:art forKey:VLCMetaInformationArtwork];
  415. }
  416. }
  417. [pool release];
  418. }
  419. - (void)metaChanged:(NSString *)metaType
  420. {
  421. [self fetchMetaInformationFromLibVLCWithType:metaType];
  422. }
  423. - (void)subItemAdded
  424. {
  425. if( subitems )
  426. return; /* Nothing to do */
  427. libvlc_media_list_t * p_mlist = libvlc_media_descriptor_subitems( p_md, NULL );
  428. NSAssert( p_mlist, @"The mlist shouldn't be nil, we are receiving a subItemAdded");
  429. [self willChangeValueForKey:@"subitems"];
  430. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  431. [self didChangeValueForKey:@"subitems"];
  432. libvlc_media_list_release( p_mlist );
  433. }
  434. @end
  435. /******************************************************************************
  436. * Implementation VLCMedia (VLCMediaPlayerBridging)
  437. */
  438. @implementation VLCMedia (VLCMediaPlayerBridging)
  439. - (void)setLength:(VLCTime *)value
  440. {
  441. if (length && value && [length compare:value] == NSOrderedSame)
  442. return;
  443. [length release];
  444. length = value ? [value retain] : nil;
  445. }
  446. - (void)setState:(NSNumber *)newStateAsNumber
  447. {
  448. state = [newStateAsNumber intValue];
  449. }
  450. @end