VLCMedia.m 17 KB

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