VLCMedia.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. //NSString *VLCMediaSubItemAdded = @"VLCMediaSubItemAdded";
  52. //NSString *VLCMediaSubItemDeleted = @"VLCMediaSubItemDeleted";
  53. /* libvlc event callback */
  54. //static void HandleMediaSubItemAdded(const libvlc_event_t *event, void *self)
  55. //{
  56. // [[VLCEventManager sharedManager] callOnMainThreadObject:self
  57. // withMethod:@selector(subItemAdded:)
  58. // withArgumentAsObject:(id)event->u.media_descriptor_subitem_added.new_child];
  59. //}
  60. //
  61. //static void HandleMediaSubItemDeleted(const libvlc_event_t *event, void *self)
  62. //{
  63. // [[VLCEventManager sharedManager] callOnMainThreadObject:self
  64. // withMethod:@selector(subItemDeleted)
  65. // withArgumentAsObject:nil];
  66. //}
  67. static void HandleMediaMetaChanged(const libvlc_event_t *event, void *self)
  68. {
  69. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  70. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  71. withMethod:@selector(metaChanged:)
  72. withArgumentAsObject:[NSNumber numberWithInt:(int)event->u.media_descriptor_meta_changed.meta_type]];
  73. [pool release];
  74. }
  75. static void HandleMediaDurationChanged(const libvlc_event_t *event, void *self)
  76. {
  77. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  78. //[[VLCEventManager sharedManager] callOnMainThreadObject:self
  79. // withMethod:@selector(setLength:)
  80. // withArgumentAsObject:[VLCTime timeWithNumber:
  81. // [NSNumber numberWithLongLong:event->u.media_descriptor_duration_changed.new_duration]]];
  82. [pool release];
  83. }
  84. // TODO: Documentation
  85. @interface VLCMedia (Private)
  86. /* Statics */
  87. + (libvlc_meta_t)stringToMetaType:(NSString *)string;
  88. + (NSString *)metaTypeToString:(libvlc_meta_t)type;
  89. /* Initializers */
  90. - (void)initInternalMediaDescriptor;
  91. /* Operations */
  92. - (BOOL)setMetaValue:(char *)value forKey:(NSString *)key;
  93. - (void)fetchMetaInformation;
  94. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL;
  95. - (void)notifyChangeForKey:(NSString *)key withOldValue:(id)oldValue;
  96. /* Callback Methods */
  97. //- (void)subItemAdded:(libvlc_media_descriptor_t *)child;
  98. //- (void)subItemRemoved:(libvlc_media_descriptor_t *)child;
  99. - (void)metaChanged:(NSNumber *)metaType;
  100. @end
  101. @implementation VLCMedia
  102. + (id)mediaWithURL:(NSString *)aURL;
  103. {
  104. // For some unknown reason, compiler kept shooting me a warning saying:
  105. // warning: passing argument 1 of 'initWithURL:' from distinct Objective-C type
  106. // Research on the net shows that the error means that the argument passed
  107. // is not compatible with the expected argument. Doesn't make sense, however
  108. // the warning goes away when it is casted it with "id".
  109. return [[[VLCMedia alloc] initWithURL:(id)aURL] autorelease];
  110. }
  111. - (id)initWithURL:(NSString *)aURL
  112. {
  113. if (self = [super init])
  114. {
  115. libvlc_exception_t ex;
  116. libvlc_exception_init(&ex);
  117. p_md = libvlc_media_descriptor_new([VLCLibrary sharedInstance],
  118. [aURL cString],
  119. &ex);
  120. quit_on_exception(&ex);
  121. url = [aURL copy];
  122. delegate = nil;
  123. metaDictionary = nil;
  124. // This value is set whenever the demuxer figures out what the length is.
  125. // TODO: Easy way to tell the length of the movie without having to instiate the demuxer. Maybe cached info?
  126. length = nil;
  127. [self initInternalMediaDescriptor];
  128. }
  129. return self;
  130. }
  131. - (void)release
  132. {
  133. @synchronized(self)
  134. {
  135. if([self retainCount] <= 1)
  136. {
  137. /* We must make sure we won't receive new event after an upcoming dealloc
  138. * We also may receive a -retain in some event callback that may occcur
  139. * Before libvlc_event_detach. So this can't happen in dealloc */
  140. libvlc_event_manager_t *p_em = libvlc_media_descriptor_event_manager(p_md, NULL);
  141. // libvlc_event_detach(p_em, libvlc_MediaDescriptorSubItemAdded, HandleMediaSubItemAdded, self, NULL);
  142. // libvlc_event_detach(p_em, libvlc_MediaDescriptorSubItemDeleted, HandleMediaSubItemAdded, self, NULL);
  143. libvlc_event_detach(p_em, libvlc_MediaDescriptorMetaChanged, HandleMediaMetaChanged, self, NULL);
  144. libvlc_event_detach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, NULL);
  145. }
  146. [super release];
  147. }
  148. }
  149. - (void)dealloc
  150. {
  151. // Testing to see if the pointer exists is not required, if the pointer is null
  152. // then the release message is not sent to it.
  153. [self setDelegate:nil];
  154. [self setLength:nil];
  155. [url release];
  156. [subitems release];
  157. [metaDictionary release];
  158. if( self == libvlc_media_descriptor_get_user_data( p_md, NULL ) )
  159. libvlc_media_descriptor_set_user_data( p_md, nil, NULL );
  160. libvlc_media_descriptor_release( p_md );
  161. [super dealloc];
  162. }
  163. - (NSString *)description
  164. {
  165. NSString *result = nil;
  166. if (metaDictionary)
  167. result = [metaDictionary objectForKey:VLCMetaInformationTitle];
  168. return (result ? result : url);
  169. }
  170. - (NSComparisonResult)compare:(VLCMedia *)media
  171. {
  172. if (self == media)
  173. return NSOrderedSame;
  174. else if (!media)
  175. return NSOrderedDescending;
  176. else
  177. return [[self url] compare:[media url]];
  178. }
  179. - (NSString *)url
  180. {
  181. return [[url copy] autorelease];
  182. }
  183. - (VLCMediaList *)subitems
  184. {
  185. return subitems;
  186. }
  187. - (VLCTime *)length
  188. {
  189. if (!length)
  190. {
  191. // Try figuring out what the length is
  192. long long duration = libvlc_media_descriptor_get_duration( p_md, NULL );
  193. if (duration > -1)
  194. {
  195. [self setLength:[VLCTime timeWithNumber:[NSNumber numberWithLongLong:duration]]];
  196. return [[length retain] autorelease];
  197. }
  198. }
  199. return [VLCTime nullTime];
  200. }
  201. - (VLCTime *)lengthWaitUntilDate:(NSDate *)aDate
  202. {
  203. #define CLOCK_FREQ 1000000
  204. #define THREAD_SLEEP ((long long)(0.010*CLOCK_FREQ))
  205. if (![url hasPrefix:@"file://"])
  206. return [self length];
  207. else if (!length)
  208. {
  209. while (!length && ![self isPreparsed] && [aDate timeIntervalSinceNow] > 0)
  210. {
  211. usleep( THREAD_SLEEP );
  212. }
  213. // So we're done waiting, but sometimes we trap the fact that the parsing
  214. // was done before the length gets assigned, so lets go ahead and assign
  215. // it ourselves.
  216. if (!length)
  217. return [self length];
  218. }
  219. #undef CLOCK_FREQ
  220. #undef THREAD_SLEEP
  221. return [[length retain] autorelease];
  222. }
  223. - (BOOL)isPreparsed
  224. {
  225. return libvlc_media_descriptor_is_preparsed( p_md, NULL );
  226. }
  227. - (NSDictionary *)metaDictionary
  228. {
  229. return metaDictionary;
  230. }
  231. - (void)setDelegate:(id)value
  232. {
  233. delegate = value;
  234. }
  235. - (id)delegate
  236. {
  237. return delegate;
  238. }
  239. @end
  240. @implementation VLCMedia (LibVLCBridging)
  241. + (id)mediaWithLibVLCMediaDescriptor:(void *)md
  242. {
  243. libvlc_exception_t ex;
  244. libvlc_exception_init( &ex );
  245. VLCMedia *media = (VLCMedia *)libvlc_media_descriptor_get_user_data( md, &ex );
  246. if (!media || libvlc_exception_raised( &ex ))
  247. {
  248. libvlc_exception_clear( &ex );
  249. return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor:md] autorelease];
  250. }
  251. return media;
  252. }
  253. - (id)initWithLibVLCMediaDescriptor:(void *)md
  254. {
  255. libvlc_exception_t ex;
  256. libvlc_exception_init( &ex );
  257. // Core hacks that allows for native objects to be paired with core objects. Otherwise, a native object
  258. // would be recreated every time we want to address the media descriptor. This eliminates the need
  259. // for maintaining local copies of core objects.
  260. if ((self = (id)libvlc_media_descriptor_get_user_data(md, &ex)) && !libvlc_exception_raised(&ex))
  261. {
  262. return [self retain];
  263. }
  264. libvlc_exception_clear( &ex ); // Just in case an exception was raised, lets release it
  265. if (self = [super init])
  266. {
  267. char * p_url;
  268. p_url = libvlc_media_descriptor_get_mrl( md, &ex );
  269. quit_on_exception( &ex );
  270. url = [[NSString stringWithCString:p_url] retain];
  271. libvlc_media_descriptor_retain( md );
  272. p_md = md;
  273. [self initInternalMediaDescriptor];
  274. }
  275. return self;
  276. }
  277. - (void *)libVLCMediaDescriptor
  278. {
  279. return p_md;
  280. }
  281. @end
  282. @implementation VLCMedia (Private)
  283. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  284. {
  285. #define VLCStringToMeta( name, string ) if ([VLCMetaInformation##name compare:string] == NSOrderedSame) return libvlc_meta_##name;
  286. VLCStringToMeta(Title, string);
  287. VLCStringToMeta(Artist, string);
  288. VLCStringToMeta(Genre, string);
  289. VLCStringToMeta(Copyright, string);
  290. VLCStringToMeta(Album, string);
  291. VLCStringToMeta(TrackNumber, string);
  292. VLCStringToMeta(Description, string);
  293. VLCStringToMeta(Rating, string);
  294. VLCStringToMeta(Date, string);
  295. VLCStringToMeta(Setting, string);
  296. VLCStringToMeta(URL, string);
  297. VLCStringToMeta(Language, string);
  298. VLCStringToMeta(NowPlaying, string);
  299. VLCStringToMeta(Publisher, string);
  300. VLCStringToMeta(ArtworkURL, string);
  301. VLCStringToMeta(TrackID, string);
  302. #undef VLCStringToMeta
  303. return -1;
  304. }
  305. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  306. {
  307. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  308. VLCMetaToString(Title, type);
  309. VLCMetaToString(Artist, type);
  310. VLCMetaToString(Genre, type);
  311. VLCMetaToString(Copyright, type);
  312. VLCMetaToString(Album, type);
  313. VLCMetaToString(TrackNumber, type);
  314. VLCMetaToString(Description, type);
  315. VLCMetaToString(Rating, type);
  316. VLCMetaToString(Date, type);
  317. VLCMetaToString(Setting, type);
  318. VLCMetaToString(URL, type);
  319. VLCMetaToString(Language, type);
  320. VLCMetaToString(NowPlaying, type);
  321. VLCMetaToString(Publisher, type);
  322. VLCMetaToString(ArtworkURL, type);
  323. VLCMetaToString(TrackID, type);
  324. #undef VLCMetaToString
  325. return nil;
  326. }
  327. - (void)initInternalMediaDescriptor
  328. {
  329. libvlc_exception_t ex;
  330. libvlc_exception_init( &ex );
  331. libvlc_media_descriptor_set_user_data( p_md, (void*)self, &ex );
  332. quit_on_exception( &ex );
  333. // TODO: Should these events be caught by VLCMediaList's notification hooks?
  334. libvlc_event_manager_t *p_em = libvlc_media_descriptor_event_manager( p_md, &ex );
  335. // libvlc_event_attach(p_em, libvlc_MediaDescriptorSubItemAdded, HandleMediaSubItemAdded, self, &ex);
  336. // libvlc_event_attach(p_em, libvlc_MediaDescriptorSubItemRemoved, HandleMediaSubItemRemoved, self, &ex);
  337. libvlc_event_attach(p_em, libvlc_MediaDescriptorMetaChanged, HandleMediaMetaChanged, self, &ex);
  338. libvlc_event_attach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, &ex);
  339. quit_on_exception( &ex );
  340. libvlc_media_list_t *p_mlist = libvlc_media_descriptor_subitems( p_md, NULL );
  341. if (!p_mlist)
  342. subitems = nil;
  343. else
  344. {
  345. [subitems release];
  346. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  347. libvlc_media_list_release( p_mlist );
  348. }
  349. [self fetchMetaInformation];
  350. }
  351. - (BOOL)setMetaValue:(char *)value forKey:(NSString *)key
  352. {
  353. BOOL result = NO;
  354. NSString *oldValue = [metaDictionary valueForKey:key];
  355. if ((!oldValue && value) || (oldValue && !value) || (oldValue && value && [oldValue compare:[NSString stringWithCString:value]] != NSOrderedSame))
  356. {
  357. if (!metaDictionary)
  358. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  359. if (value)
  360. [metaDictionary setValue:[NSString stringWithCString:value] forKeyPath:key];
  361. else
  362. [metaDictionary setValue:nil forKeyPath:key];
  363. if ([key compare:VLCMetaInformationArtworkURL] == NSOrderedSame)
  364. {
  365. if ([metaDictionary valueForKey:VLCMetaInformationArtworkURL])
  366. {
  367. // Initialize a new thread
  368. [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
  369. toTarget:self
  370. withObject:[metaDictionary valueForKey:VLCMetaInformationArtworkURL]];
  371. }
  372. }
  373. result = YES;
  374. }
  375. free( value );
  376. return result;
  377. }
  378. - (void)fetchMetaInformation
  379. {
  380. // TODO: Only fetch meta data that has been requested. Just don't fetch
  381. // it, just because.
  382. [self setMetaValue:libvlc_media_descriptor_get_meta( p_md, libvlc_meta_Title, NULL ) forKey:[VLCMedia metaTypeToString:libvlc_meta_Title]];
  383. [self setMetaValue:libvlc_media_descriptor_get_meta( p_md, libvlc_meta_Artist, NULL ) forKey:[VLCMedia metaTypeToString:libvlc_meta_Artist]];
  384. [self setMetaValue:libvlc_media_descriptor_get_meta( p_md, libvlc_meta_ArtworkURL, NULL ) forKey:[VLCMedia metaTypeToString:libvlc_meta_ArtworkURL]];
  385. }
  386. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
  387. {
  388. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  389. @try
  390. {
  391. // Go ahead and load up the art work
  392. NSURL *artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  393. NSImage *art = [[[NSImage alloc] initWithContentsOfURL:artUrl] autorelease];
  394. // If anything was found, lets save it to the meta data dictionary
  395. if (art)
  396. {
  397. @synchronized(metaDictionary)
  398. {
  399. [metaDictionary setObject:art forKey:VLCMetaInformationArtwork];
  400. }
  401. // Let the world know that there is new art work available
  402. [self notifyChangeForKey:VLCMetaInformationArtwork withOldValue:nil];
  403. }
  404. }
  405. @finally
  406. {
  407. [pool release];
  408. }
  409. }
  410. - (void)notifyChangeForKey:(NSString *)key withOldValue:(id)oldValue
  411. {
  412. // Send out a formal notification
  413. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaMetaChanged
  414. object:self
  415. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  416. key, @"key",
  417. oldValue, @"oldValue",
  418. nil]];
  419. // Now notify the delegate
  420. if (delegate && [delegate respondsToSelector:@selector(media:metaValueChangedFrom:forKey:)])
  421. [delegate media:self metaValueChangedFrom:oldValue forKey:key];
  422. }
  423. //- (void)subItemAdded:(libvlc_media_descriptor_t *)child
  424. //{
  425. // // TODO: SubItemAdded Notification
  426. //// if (!subitems)
  427. //// {
  428. //// subitems = [[VLCMediaList alloc] initWithLibVLCMediaList:];
  429. //// }
  430. //}
  431. //
  432. //- (void)subItemRemoved:(libvlc_media_descriptor_t *)child
  433. //{
  434. // // TODO: SubItemAdded Notification
  435. // // if (!subitems)
  436. // // {
  437. // // subitems = [[VLCMediaList alloc] initWithLibVLCMediaList:];
  438. // // }
  439. //}
  440. - (void)metaChanged:(NSNumber *)metaType
  441. {
  442. // TODO: Only retrieve the meta that was changed
  443. // Can we figure out what piece was changed instead of retreiving everything?
  444. NSString *key = [VLCMedia metaTypeToString:[metaType intValue]];
  445. id oldValue = (metaDictionary ? [metaDictionary valueForKey:key] : nil);
  446. // Update the meta data
  447. if ([self setMetaValue:libvlc_media_descriptor_get_meta(p_md, [metaType intValue], NULL) forKey:key])
  448. // There was actually a change, send out the notifications
  449. [self notifyChangeForKey:key withOldValue:oldValue];
  450. }
  451. @end
  452. @implementation VLCMedia (VLCMediaPlayerBridging)
  453. - (void)setLength:(VLCTime *)value
  454. {
  455. if (length != value)
  456. {
  457. if (length && value && [length compare:value] == NSOrderedSame)
  458. return;
  459. [self willChangeValueForKey:@"length"];
  460. if (length) {
  461. [length release];
  462. length = nil;
  463. }
  464. if (value)
  465. length = [value retain];
  466. [self didChangeValueForKey:@"length"];
  467. }
  468. }
  469. @end