VLCMedia.m 18 KB

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