VLCMedia.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. url = [aPath copy];
  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. url = [aName copy];
  179. delegate = nil;
  180. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  181. // This value is set whenever the demuxer figures out what the length is.
  182. // TODO: Easy way to tell the length of the movie without having to instiate the demuxer. Maybe cached info?
  183. length = nil;
  184. [self initInternalMediaDescriptor];
  185. }
  186. return self;
  187. }
  188. - (void)release
  189. {
  190. @synchronized(self)
  191. {
  192. if([self retainCount] <= 1)
  193. {
  194. /* We must make sure we won't receive new event after an upcoming dealloc
  195. * We also may receive a -retain in some event callback that may occcur
  196. * Before libvlc_event_detach. So this can't happen in dealloc */
  197. libvlc_event_manager_t * p_em = libvlc_media_descriptor_event_manager(p_md, NULL);
  198. libvlc_event_detach(p_em, libvlc_MediaDescriptorMetaChanged, HandleMediaMetaChanged, self, NULL);
  199. // libvlc_event_detach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, NULL);
  200. libvlc_event_detach(p_em, libvlc_MediaDescriptorStateChanged, HandleMediaStateChanged, self, NULL);
  201. libvlc_event_detach(p_em, libvlc_MediaDescriptorSubItemAdded, HandleMediaSubItemAdded, self, NULL);
  202. }
  203. [super release];
  204. }
  205. }
  206. - (void)dealloc
  207. {
  208. // Testing to see if the pointer exists is not required, if the pointer is null
  209. // then the release message is not sent to it.
  210. delegate = nil;
  211. [self setLength:nil];
  212. [url release];
  213. [subitems release];
  214. [metaDictionary release];
  215. libvlc_media_descriptor_release( p_md );
  216. [super dealloc];
  217. }
  218. - (NSString *)description
  219. {
  220. NSString * result = [metaDictionary objectForKey:VLCMetaInformationTitle];
  221. return [NSString stringWithFormat:@"<%@ %p> %@", [self className], self, (result ? result : url)];
  222. }
  223. - (NSComparisonResult)compare:(VLCMedia *)media
  224. {
  225. if (self == media)
  226. return NSOrderedSame;
  227. else if (!media)
  228. return NSOrderedDescending;
  229. else
  230. return [[self url] compare:[media url]];
  231. }
  232. @synthesize delegate;
  233. - (VLCTime *)length
  234. {
  235. if (!length)
  236. {
  237. // Try figuring out what the length is
  238. long long duration = libvlc_media_descriptor_get_duration( p_md, NULL );
  239. if (duration > -1)
  240. {
  241. [self setLength:[VLCTime timeWithNumber:[NSNumber numberWithLongLong:duration]]];
  242. return [[length retain] autorelease];
  243. }
  244. }
  245. return [VLCTime nullTime];
  246. }
  247. - (VLCTime *)lengthWaitUntilDate:(NSDate *)aDate
  248. {
  249. static const long long thread_sleep = 10000;
  250. if (!length)
  251. {
  252. while (!length && ![self isPreparsed] && [aDate timeIntervalSinceNow] > 0)
  253. {
  254. usleep( thread_sleep );
  255. }
  256. // So we're done waiting, but sometimes we trap the fact that the parsing
  257. // was done before the length gets assigned, so lets go ahead and assign
  258. // it ourselves.
  259. if (!length)
  260. return [self length];
  261. }
  262. return [[length retain] autorelease];
  263. }
  264. - (BOOL)isPreparsed
  265. {
  266. return libvlc_media_descriptor_is_preparsed( p_md, NULL );
  267. }
  268. @synthesize url;
  269. @synthesize subitems;
  270. @synthesize metaDictionary;
  271. @synthesize state;
  272. @end
  273. /******************************************************************************
  274. * Implementation VLCMedia (LibVLCBridging)
  275. */
  276. @implementation VLCMedia (LibVLCBridging)
  277. + (id)mediaWithLibVLCMediaDescriptor:(void *)md
  278. {
  279. return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor:md] autorelease];
  280. }
  281. - (id)initWithLibVLCMediaDescriptor:(void *)md
  282. {
  283. if (self = [super init])
  284. {
  285. libvlc_exception_t ex;
  286. libvlc_exception_init( &ex );
  287. char * p_url;
  288. p_url = libvlc_media_descriptor_get_mrl( md, &ex );
  289. catch_exception( &ex );
  290. url = [[NSString stringWithUTF8String:p_url] retain];
  291. free( p_url );
  292. libvlc_media_descriptor_retain( md );
  293. p_md = md;
  294. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  295. [self initInternalMediaDescriptor];
  296. }
  297. return self;
  298. }
  299. - (void *)libVLCMediaDescriptor
  300. {
  301. return p_md;
  302. }
  303. @end
  304. /******************************************************************************
  305. * Implementation VLCMedia (Private)
  306. */
  307. @implementation VLCMedia (Private)
  308. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  309. {
  310. static NSDictionary * stringToMetaDictionary = nil;
  311. // TODO: Thread safe-ize
  312. if( !stringToMetaDictionary )
  313. {
  314. #define VLCStringToMeta( name ) [NSNumber numberWithInt: libvlc_meta_##name], VLCMetaInformation##name
  315. stringToMetaDictionary =
  316. [[NSDictionary dictionaryWithObjectsAndKeys:
  317. VLCStringToMeta(Title),
  318. VLCStringToMeta(Artist),
  319. VLCStringToMeta(Genre),
  320. VLCStringToMeta(Copyright),
  321. VLCStringToMeta(Album),
  322. VLCStringToMeta(TrackNumber),
  323. VLCStringToMeta(Description),
  324. VLCStringToMeta(Rating),
  325. VLCStringToMeta(Date),
  326. VLCStringToMeta(Setting),
  327. VLCStringToMeta(URL),
  328. VLCStringToMeta(Language),
  329. VLCStringToMeta(NowPlaying),
  330. VLCStringToMeta(Publisher),
  331. VLCStringToMeta(ArtworkURL),
  332. VLCStringToMeta(TrackID),
  333. nil] retain];
  334. #undef VLCStringToMeta
  335. }
  336. NSNumber * number = [stringToMetaDictionary objectForKey:string];
  337. return number ? [number intValue] : -1;
  338. }
  339. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  340. {
  341. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  342. VLCMetaToString(Title, type);
  343. VLCMetaToString(Artist, type);
  344. VLCMetaToString(Genre, type);
  345. VLCMetaToString(Copyright, type);
  346. VLCMetaToString(Album, type);
  347. VLCMetaToString(TrackNumber, type);
  348. VLCMetaToString(Description, type);
  349. VLCMetaToString(Rating, type);
  350. VLCMetaToString(Date, type);
  351. VLCMetaToString(Setting, type);
  352. VLCMetaToString(URL, type);
  353. VLCMetaToString(Language, type);
  354. VLCMetaToString(NowPlaying, type);
  355. VLCMetaToString(Publisher, type);
  356. VLCMetaToString(ArtworkURL, type);
  357. VLCMetaToString(TrackID, type);
  358. #undef VLCMetaToString
  359. return nil;
  360. }
  361. - (void)initInternalMediaDescriptor
  362. {
  363. libvlc_exception_t ex;
  364. libvlc_exception_init( &ex );
  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)setState:(NSNumber *)newStateAsNumber
  448. {
  449. state = [newStateAsNumber intValue];
  450. }
  451. @end