VLCMedia.m 18 KB

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