VLCMedia.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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)mediaWithPath:(NSString *)aPath;
  103. {
  104. return [[[VLCMedia alloc] initWithPath:(id)aPath] autorelease];
  105. }
  106. + (id)mediaWithURL:(NSURL *)aURL;
  107. {
  108. return [[[VLCMedia alloc] initWithPath:(id)[aURL path] autorelease];
  109. }
  110. - (id)initWithPath:(NSString *)aPath
  111. {
  112. if (self = [super init])
  113. {
  114. libvlc_exception_t ex;
  115. libvlc_exception_init(&ex);
  116. p_md = libvlc_media_descriptor_new([VLCLibrary sharedInstance],
  117. [aURL cString],
  118. &ex);
  119. quit_on_exception(&ex);
  120. url = [aURL copy];
  121. delegate = nil;
  122. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  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. return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor:md] autorelease];
  241. }
  242. - (id)initWithLibVLCMediaDescriptor:(void *)md
  243. {
  244. if (self = [super init])
  245. {
  246. libvlc_exception_t ex;
  247. libvlc_exception_init( &ex );
  248. char * p_url;
  249. p_url = libvlc_media_descriptor_get_mrl( md, &ex );
  250. quit_on_exception( &ex );
  251. url = [[NSString stringWithCString:p_url] retain];
  252. libvlc_media_descriptor_retain( md );
  253. p_md = md;
  254. [self initInternalMediaDescriptor];
  255. }
  256. return self;
  257. }
  258. - (void *)libVLCMediaDescriptor
  259. {
  260. return p_md;
  261. }
  262. @end
  263. @implementation VLCMedia (Private)
  264. + (libvlc_meta_t)stringToMetaType:(NSString *)string
  265. {
  266. #define VLCStringToMeta( name, string ) if ([VLCMetaInformation##name compare:string] == NSOrderedSame) return libvlc_meta_##name;
  267. VLCStringToMeta(Title, string);
  268. VLCStringToMeta(Artist, string);
  269. VLCStringToMeta(Genre, string);
  270. VLCStringToMeta(Copyright, string);
  271. VLCStringToMeta(Album, string);
  272. VLCStringToMeta(TrackNumber, string);
  273. VLCStringToMeta(Description, string);
  274. VLCStringToMeta(Rating, string);
  275. VLCStringToMeta(Date, string);
  276. VLCStringToMeta(Setting, string);
  277. VLCStringToMeta(URL, string);
  278. VLCStringToMeta(Language, string);
  279. VLCStringToMeta(NowPlaying, string);
  280. VLCStringToMeta(Publisher, string);
  281. VLCStringToMeta(ArtworkURL, string);
  282. VLCStringToMeta(TrackID, string);
  283. #undef VLCStringToMeta
  284. return -1;
  285. }
  286. + (NSString *)metaTypeToString:(libvlc_meta_t)type
  287. {
  288. #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
  289. VLCMetaToString(Title, type);
  290. VLCMetaToString(Artist, type);
  291. VLCMetaToString(Genre, type);
  292. VLCMetaToString(Copyright, type);
  293. VLCMetaToString(Album, type);
  294. VLCMetaToString(TrackNumber, type);
  295. VLCMetaToString(Description, type);
  296. VLCMetaToString(Rating, type);
  297. VLCMetaToString(Date, type);
  298. VLCMetaToString(Setting, type);
  299. VLCMetaToString(URL, type);
  300. VLCMetaToString(Language, type);
  301. VLCMetaToString(NowPlaying, type);
  302. VLCMetaToString(Publisher, type);
  303. VLCMetaToString(ArtworkURL, type);
  304. VLCMetaToString(TrackID, type);
  305. #undef VLCMetaToString
  306. return nil;
  307. }
  308. - (void)initInternalMediaDescriptor
  309. {
  310. libvlc_exception_t ex;
  311. libvlc_exception_init( &ex );
  312. libvlc_media_descriptor_set_user_data( p_md, (void*)self, &ex );
  313. quit_on_exception( &ex );
  314. // TODO: Should these events be caught by VLCMediaList's notification hooks?
  315. libvlc_event_manager_t *p_em = libvlc_media_descriptor_event_manager( p_md, &ex );
  316. // libvlc_event_attach(p_em, libvlc_MediaDescriptorSubItemAdded, HandleMediaSubItemAdded, self, &ex);
  317. // libvlc_event_attach(p_em, libvlc_MediaDescriptorSubItemRemoved, HandleMediaSubItemRemoved, self, &ex);
  318. libvlc_event_attach(p_em, libvlc_MediaDescriptorMetaChanged, HandleMediaMetaChanged, self, &ex);
  319. libvlc_event_attach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, &ex);
  320. quit_on_exception( &ex );
  321. libvlc_media_list_t *p_mlist = libvlc_media_descriptor_subitems( p_md, NULL );
  322. if (!p_mlist)
  323. subitems = nil;
  324. else
  325. {
  326. [subitems release];
  327. subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
  328. libvlc_media_list_release( p_mlist );
  329. }
  330. [self fetchMetaInformation];
  331. }
  332. - (BOOL)setMetaValue:(char *)value forKey:(NSString *)key
  333. {
  334. BOOL result = NO;
  335. NSString *oldValue = [metaDictionary valueForKey:key];
  336. if ((!oldValue && value) || (oldValue && !value) || (oldValue && value && [oldValue compare:[NSString stringWithCString:value]] != NSOrderedSame))
  337. {
  338. if (!metaDictionary)
  339. metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
  340. if (value)
  341. [metaDictionary setValue:[NSString stringWithCString:value] forKeyPath:key];
  342. else
  343. [metaDictionary setValue:nil forKeyPath:key];
  344. if ([key compare:VLCMetaInformationArtworkURL] == NSOrderedSame)
  345. {
  346. if ([metaDictionary valueForKey:VLCMetaInformationArtworkURL])
  347. {
  348. // Initialize a new thread
  349. [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
  350. toTarget:self
  351. withObject:[metaDictionary valueForKey:VLCMetaInformationArtworkURL]];
  352. }
  353. }
  354. result = YES;
  355. }
  356. free( value );
  357. return result;
  358. }
  359. - (void)fetchMetaInformation
  360. {
  361. // TODO: Only fetch meta data that has been requested. Just don't fetch
  362. // it, just because.
  363. [self setMetaValue:libvlc_media_descriptor_get_meta( p_md, libvlc_meta_Title, NULL ) forKey:[VLCMedia metaTypeToString:libvlc_meta_Title]];
  364. [self setMetaValue:libvlc_media_descriptor_get_meta( p_md, libvlc_meta_Artist, NULL ) forKey:[VLCMedia metaTypeToString:libvlc_meta_Artist]];
  365. [self setMetaValue:libvlc_media_descriptor_get_meta( p_md, libvlc_meta_ArtworkURL, NULL ) forKey:[VLCMedia metaTypeToString:libvlc_meta_ArtworkURL]];
  366. }
  367. - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
  368. {
  369. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  370. @try
  371. {
  372. // Go ahead and load up the art work
  373. NSURL *artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  374. NSImage *art = [[[NSImage alloc] initWithContentsOfURL:artUrl] autorelease];
  375. // If anything was found, lets save it to the meta data dictionary
  376. if (art)
  377. {
  378. @synchronized(metaDictionary)
  379. {
  380. [metaDictionary setObject:art forKey:VLCMetaInformationArtwork];
  381. }
  382. // Let the world know that there is new art work available
  383. [self notifyChangeForKey:VLCMetaInformationArtwork withOldValue:nil];
  384. }
  385. }
  386. @finally
  387. {
  388. [pool release];
  389. }
  390. }
  391. - (void)notifyChangeForKey:(NSString *)key withOldValue:(id)oldValue
  392. {
  393. // Send out a formal notification
  394. [[NSNotificationCenter defaultCenter] postNotificationName:VLCMediaMetaChanged
  395. object:self
  396. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  397. key, @"key",
  398. oldValue, @"oldValue",
  399. nil]];
  400. // Now notify the delegate
  401. if (delegate && [delegate respondsToSelector:@selector(media:metaValueChangedFrom:forKey:)])
  402. [delegate media:self metaValueChangedFrom:oldValue forKey:key];
  403. }
  404. //- (void)subItemAdded:(libvlc_media_descriptor_t *)child
  405. //{
  406. // // TODO: SubItemAdded Notification
  407. //// if (!subitems)
  408. //// {
  409. //// subitems = [[VLCMediaList alloc] initWithLibVLCMediaList:];
  410. //// }
  411. //}
  412. //
  413. //- (void)subItemRemoved:(libvlc_media_descriptor_t *)child
  414. //{
  415. // // TODO: SubItemAdded Notification
  416. // // if (!subitems)
  417. // // {
  418. // // subitems = [[VLCMediaList alloc] initWithLibVLCMediaList:];
  419. // // }
  420. //}
  421. - (void)metaChanged:(NSNumber *)metaType
  422. {
  423. // TODO: Only retrieve the meta that was changed
  424. // Can we figure out what piece was changed instead of retreiving everything?
  425. NSString *key = [VLCMedia metaTypeToString:[metaType intValue]];
  426. id oldValue = (metaDictionary ? [metaDictionary valueForKey:key] : nil);
  427. // Update the meta data
  428. if ([self setMetaValue:libvlc_media_descriptor_get_meta(p_md, [metaType intValue], NULL) forKey:key])
  429. // There was actually a change, send out the notifications
  430. [self notifyChangeForKey:key withOldValue:oldValue];
  431. }
  432. @end
  433. @implementation VLCMedia (VLCMediaPlayerBridging)
  434. - (void)setLength:(VLCTime *)value
  435. {
  436. if (length != value)
  437. {
  438. if (length && value && [length compare:value] == NSOrderedSame)
  439. return;
  440. [self willChangeValueForKey:@"length"];
  441. if (length) {
  442. [length release];
  443. length = nil;
  444. }
  445. if (value)
  446. length = [value retain];
  447. [self didChangeValueForKey:@"length"];
  448. }
  449. }
  450. @end