VLCMedia.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <Cocoa/Cocoa.h>
  25. #import <VLC/VLCMedia.h>
  26. #import <VLC/VLCPlaylist.h>
  27. #import "VLCEventManager.h"
  28. #import "VLCLibrary.h"
  29. #include <vlc/libvlc.h>
  30. NSString * VLCMetaInformationTitle = @"title";
  31. NSString * VLCMetaInformationAuthor = @"author";
  32. NSString * VLCMetaInformationArtwork = @"artwork";
  33. /* Our notification */
  34. NSString * VLCMediaSubItemAdded = @"VLCMediaSubItemAdded";
  35. NSString * VLCMediaMetaChanged = @"VLCMediaMetaChanged";
  36. /* libvlc event callback */
  37. static void HandleMediaSubItemAdded( const libvlc_event_t * event, void * self)
  38. {
  39. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
  40. withDelegateMethod: @selector(subItemAdded:)
  41. withNotificationName: VLCMediaSubItemAdded];
  42. }
  43. /* libvlc event callback */
  44. static void HandleMediaMetaChanged( const libvlc_event_t * event, void * self)
  45. {
  46. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
  47. withDelegateMethod: @selector(metaChanged:)
  48. withNotificationName: VLCMediaMetaChanged];
  49. }
  50. @interface VLCMedia (Private)
  51. - (void)initializeInternalMediaDescriptor;
  52. - (void)subItemAdded:(NSNotification *)notification;
  53. - (void)metaChanged:(NSNotification *)notification;
  54. - (void)fetchMetaInformation;
  55. @end
  56. @implementation VLCMedia (Private)
  57. - (void)initializeInternalMediaDescriptor
  58. {
  59. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subItemAdded:) name:VLCMediaSubItemAdded object:self];
  60. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(metaChanged:) name:VLCMediaMetaChanged object:self];
  61. libvlc_exception_t ex;
  62. libvlc_exception_init( &ex );
  63. libvlc_event_manager_t * p_em = libvlc_media_descriptor_event_manager( md, &ex );
  64. libvlc_event_attach( p_em, libvlc_MediaDescriptorSubItemAdded, HandleMediaSubItemAdded, self, &ex );
  65. libvlc_event_attach( p_em, libvlc_MediaDescriptorMetaChanged, HandleMediaMetaChanged, self, &ex );
  66. quit_on_exception( &ex );
  67. libvlc_media_list_t * p_mlist = libvlc_media_descriptor_subitems( md, NULL );
  68. if (!p_mlist)
  69. subitems = nil;
  70. else
  71. {
  72. subitems= [[VLCPlaylist playlistWithLibVLCMediaList: p_mlist] retain];
  73. libvlc_media_list_release( p_mlist );
  74. }
  75. [self fetchMetaInformation];
  76. }
  77. - (void)subItemAdded:(NSNotification *)notification
  78. {
  79. if(!subitems)
  80. {
  81. libvlc_media_list_t * p_mlist = libvlc_media_descriptor_subitems( md, NULL );
  82. [self willChangeValueForKey:@"subitems"];
  83. subitems = [[VLCPlaylist playlistWithLibVLCMediaList: p_mlist] retain];
  84. [self didChangeValueForKey:@"subitems"];
  85. libvlc_media_list_release( p_mlist );
  86. }
  87. }
  88. - (void)metaChanged:(NSNotification *)notification
  89. {
  90. [self fetchMetaInformation];
  91. }
  92. - (void)fetchMetaInformation
  93. {
  94. NSMutableDictionary * temp;
  95. libvlc_exception_t ex;
  96. char * title;
  97. char * artist;
  98. char * arturl = NULL;
  99. libvlc_exception_init( &ex );
  100. title = libvlc_media_descriptor_get_meta( md, libvlc_meta_Title, &ex );
  101. quit_on_exception( &ex );
  102. artist = libvlc_media_descriptor_get_meta( md, libvlc_meta_Artist, &ex );
  103. quit_on_exception( &ex );
  104. arturl = libvlc_media_descriptor_get_meta( md, libvlc_meta_ArtworkURL, &ex );
  105. quit_on_exception( &ex );
  106. temp = [NSMutableDictionary dictionaryWithCapacity: 2];
  107. if (title)
  108. [temp setObject: [NSString stringWithUTF8String: title] forKey: VLCMetaInformationTitle];
  109. #if 0
  110. /* We need to perform that in a thread because this takes long. */
  111. if (arturl)
  112. {
  113. NSString *plainStringURL = [NSString stringWithUTF8String:arturl];
  114. NSString *escapedStringURL = [plainStringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  115. NSURL *aURL = [NSURL URLWithString:escapedStringURL];
  116. NSImage * art = [[NSImage alloc] initWithContentsOfURL: aURL];
  117. [art autorelease];
  118. if( art )
  119. [temp setObject: art forKey: VLCMetaInformationArtwork];
  120. }
  121. #endif
  122. free( title );
  123. free( artist );
  124. free( arturl );
  125. [self willChangeValueForKey:@"metaInformation"];
  126. if( metaInformation )
  127. [metaInformation release];
  128. metaInformation = [[NSMutableDictionary alloc] initWithDictionary:temp];
  129. [self didChangeValueForKey:@"metaInformation"];
  130. }
  131. @end
  132. @implementation VLCMedia
  133. - (id)initWithURL:(NSString *)anURL
  134. {
  135. if (self = [super init])
  136. {
  137. libvlc_exception_t ex;
  138. url = [anURL copy];
  139. libvlc_exception_init( &ex );
  140. md = libvlc_media_descriptor_new( [VLCLibrary sharedInstance],
  141. [anURL cString],
  142. &ex );
  143. quit_on_exception( &ex );
  144. metaInformation = nil;
  145. [self initializeInternalMediaDescriptor];
  146. }
  147. return self;
  148. }
  149. + (id)mediaWithURL:(NSString *)anURL;
  150. {
  151. return [[(VLCMedia*)[VLCMedia alloc] initWithURL: anURL ] autorelease];
  152. }
  153. - (void)release
  154. {
  155. @synchronized(self)
  156. {
  157. if([self retainCount] <= 1)
  158. {
  159. /* We must make sure we won't receive new event after an upcoming dealloc
  160. * We also may receive a -retain in some event callback that may occcur
  161. * Before libvlc_event_detach. So this can't happen in dealloc */
  162. libvlc_event_manager_t * p_em = libvlc_media_descriptor_event_manager( md, NULL );
  163. libvlc_event_detach( p_em, libvlc_MediaDescriptorSubItemAdded, HandleMediaSubItemAdded, self, NULL );
  164. libvlc_event_detach( p_em, libvlc_MediaDescriptorMetaChanged, HandleMediaMetaChanged, self, NULL );
  165. }
  166. [super release];
  167. }
  168. }
  169. - (void) dealloc
  170. {
  171. if( subitems )
  172. [subitems release];
  173. if( metaInformation )
  174. [metaInformation release];
  175. libvlc_media_descriptor_release( md );
  176. [url release];
  177. [super dealloc];
  178. }
  179. - (NSString *)url
  180. {
  181. return [[url copy] autorelease];
  182. }
  183. - (VLCPlaylist *)subitems
  184. {
  185. return subitems;
  186. }
  187. /* Returns a dictionary with corresponding object associated with a meta */
  188. - (NSDictionary *)metaInformation
  189. {
  190. return metaInformation;
  191. }
  192. /* Not supported yet */
  193. - (id)delegate
  194. {
  195. return nil;
  196. }
  197. @end
  198. @implementation VLCMedia (LibVLCBridging)
  199. - (id) initWithLibVLCMediaDescriptor: (libvlc_media_descriptor_t *)p_md
  200. {
  201. if (self = [super init])
  202. {
  203. libvlc_exception_t ex;
  204. char * p_url;
  205. libvlc_exception_init( &ex );
  206. p_url = libvlc_media_descriptor_get_mrl( p_md, &ex );
  207. quit_on_exception( &ex );
  208. url = [[NSString stringWithCString: p_url] retain];
  209. libvlc_media_descriptor_retain( p_md );
  210. md = p_md;
  211. [self initializeInternalMediaDescriptor];
  212. }
  213. return self;
  214. }
  215. + (id) mediaWithLibVLCMediaDescriptor: (libvlc_media_descriptor_t *)p_md
  216. {
  217. return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor: p_md] autorelease];
  218. }
  219. - (libvlc_media_descriptor_t *) libVLCMediaDescriptor
  220. {
  221. libvlc_media_descriptor_retain( md );
  222. return md;
  223. }
  224. @end