VLCMedia.m 18 KB

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