VLCMedia.m 14 KB

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