VLCMedia.m 15 KB

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