VLCMediaPlayer.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2009 the VideoLAN team
  6. * Partial Copyright (C) 2009 Felix Paul Kühne
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Faustion Osuna <enrique.osuna # gmail.com>
  11. * Felix Paul Kühne <fkuehne # videolan.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  26. *****************************************************************************/
  27. #import "VLCLibrary.h"
  28. #import "VLCMediaPlayer.h"
  29. #import "VLCEventManager.h"
  30. #import "VLCLibVLCBridging.h"
  31. #import "VLCVideoView.h"
  32. #ifdef HAVE_CONFIG_H
  33. # include "config.h"
  34. #endif
  35. /* prevent system sleep */
  36. #import <CoreServices/CoreServices.h>
  37. /* FIXME: Ugly hack! */
  38. #ifdef __x86_64__
  39. #import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
  40. #endif
  41. #include <vlc/vlc.h>
  42. /* Notification Messages */
  43. NSString * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  44. NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  45. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  46. {
  47. static NSString * stateToStrings[] = {
  48. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  49. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  50. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  51. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  52. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  53. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  54. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  55. };
  56. return stateToStrings[state];
  57. }
  58. /* libvlc event callback */
  59. static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
  60. {
  61. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  62. withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
  63. withNotificationName:VLCMediaPlayerVolumeChanged];
  64. }
  65. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  66. {
  67. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  68. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  69. withMethod:@selector(mediaPlayerTimeChanged:)
  70. withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
  71. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  72. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  73. withNotificationName:VLCMediaPlayerTimeChanged];
  74. [pool release];
  75. }
  76. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  77. {
  78. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  79. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  80. withMethod:@selector(mediaPlayerPositionChanged:)
  81. withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
  82. [pool release];
  83. }
  84. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  85. {
  86. VLCMediaPlayerState newState;
  87. if( event->type == libvlc_MediaPlayerPlaying )
  88. newState = VLCMediaPlayerStatePlaying;
  89. else if( event->type == libvlc_MediaPlayerPaused )
  90. newState = VLCMediaPlayerStatePaused;
  91. else if( event->type == libvlc_MediaPlayerEndReached )
  92. newState = VLCMediaPlayerStateStopped;
  93. else if( event->type == libvlc_MediaPlayerEncounteredError )
  94. newState = VLCMediaPlayerStateError;
  95. else
  96. {
  97. NSLog(@"%s: Unknown event", __FUNCTION__);
  98. return;
  99. }
  100. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  101. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  102. withMethod:@selector(mediaPlayerStateChanged:)
  103. withArgumentAsObject:[NSNumber numberWithInt:newState]];
  104. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  105. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  106. withNotificationName:VLCMediaPlayerStateChanged];
  107. [pool release];
  108. }
  109. // TODO: Documentation
  110. @interface VLCMediaPlayer (Private)
  111. - (id)initWithDrawable:(id)aDrawable;
  112. - (void)registerObservers;
  113. - (void)unregisterObservers;
  114. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  115. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  116. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  117. @end
  118. @implementation VLCMediaPlayer
  119. /* Bindings */
  120. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  121. {
  122. static NSDictionary * dict = nil;
  123. NSSet * superKeyPaths;
  124. if( !dict )
  125. {
  126. dict = [[NSDictionary dictionaryWithObjectsAndKeys:
  127. [NSSet setWithObject:@"state"], @"playing",
  128. [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
  129. [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
  130. [NSSet setWithObjects:@"state", @"media", nil], @"description",
  131. nil] retain];
  132. }
  133. if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
  134. {
  135. NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
  136. [ret unionSet:superKeyPaths];
  137. return ret;
  138. }
  139. return [dict objectForKey: key];
  140. }
  141. /* Contructor */
  142. - (id)init
  143. {
  144. return [self initWithDrawable:nil];
  145. }
  146. - (id)initWithVideoView:(VLCVideoView *)aVideoView
  147. {
  148. return [self initWithDrawable: aVideoView];
  149. }
  150. - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  151. {
  152. return [self initWithDrawable: aVideoLayer];
  153. }
  154. - (void)release
  155. {
  156. @synchronized(self)
  157. {
  158. if([self retainCount] <= 1)
  159. {
  160. /* We must make sure we won't receive new event after an upcoming dealloc
  161. * We also may receive a -retain in some event callback that may occcur
  162. * Before libvlc_event_detach. So this can't happen in dealloc */
  163. [self unregisterObservers];
  164. }
  165. [super release];
  166. }
  167. }
  168. - (void)dealloc
  169. {
  170. NSAssert(libvlc_media_player_get_state(instance, NULL) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
  171. // Always get rid of the delegate first so we can stop sending messages to it
  172. // TODO: Should we tell the delegate that we're shutting down?
  173. delegate = nil;
  174. // Clear our drawable as we are going to release it, we don't
  175. // want the core to use it from this point. This won't happen as
  176. // the media player must be stopped.
  177. libvlc_media_player_set_nsobject(instance, nil, NULL);
  178. libvlc_media_player_release(instance);
  179. // Get rid of everything else
  180. [media release];
  181. [cachedTime release];
  182. [drawable release];
  183. [super dealloc];
  184. }
  185. - (void)setDelegate:(id)value
  186. {
  187. delegate = value;
  188. }
  189. - (id)delegate
  190. {
  191. return delegate;
  192. }
  193. - (void)setVideoView:(VLCVideoView *)aVideoView
  194. {
  195. [self setDrawable: aVideoView];
  196. }
  197. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  198. {
  199. [self setDrawable: aVideoLayer];
  200. }
  201. - (void)setDrawable:(id)aDrawable
  202. {
  203. // Make sure that this instance has been associated with the drawing canvas.
  204. libvlc_exception_t ex;
  205. libvlc_exception_init( &ex );
  206. libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
  207. catch_exception( &ex );
  208. }
  209. - (id)drawable
  210. {
  211. libvlc_exception_t ex;
  212. libvlc_exception_init( &ex );
  213. id ret = libvlc_media_player_get_nsobject(instance);
  214. catch_exception( &ex );
  215. return ret;
  216. }
  217. - (VLCAudio *)audio
  218. {
  219. return [[VLCLibrary sharedLibrary] audio];
  220. }
  221. #pragma mark -
  222. #pragma mark Subtitles
  223. - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
  224. {
  225. libvlc_exception_t ex;
  226. libvlc_exception_init( &ex );
  227. libvlc_video_set_spu( instance, (int)index, &ex );
  228. catch_exception( &ex );
  229. }
  230. - (NSUInteger)currentVideoSubTitleIndex
  231. {
  232. libvlc_exception_t ex;
  233. libvlc_exception_init( &ex );
  234. int count = libvlc_video_get_spu_count( instance, &ex );
  235. catch_exception( &ex );
  236. if (count <= 0)
  237. return NSNotFound;
  238. NSUInteger result = libvlc_video_get_spu( instance, &ex );
  239. catch_exception( &ex );
  240. return result;
  241. }
  242. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  243. {
  244. libvlc_exception_t ex;
  245. libvlc_exception_init( &ex );
  246. BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
  247. catch_exception( &ex );
  248. return result;
  249. }
  250. - (NSArray *)videoSubTitles
  251. {
  252. libvlc_exception_t ex;
  253. libvlc_exception_init( &ex );
  254. int count = libvlc_video_get_spu_count( instance, &ex );
  255. catch_exception( &ex );
  256. libvlc_track_description_t *tracks = libvlc_video_get_spu_description( instance, &ex );
  257. NSMutableArray *tempArray = [NSMutableArray array];
  258. NSInteger i;
  259. for (i = 0; i < count; i++)
  260. {
  261. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  262. tracks = tracks->p_next;
  263. }
  264. return [NSArray arrayWithArray: tempArray];
  265. }
  266. #pragma mark -
  267. #pragma mark Video Crop geometry
  268. - (void)setVideoCropGeometry:(char *)value
  269. {
  270. libvlc_exception_t ex;
  271. libvlc_exception_init( &ex );
  272. libvlc_video_set_crop_geometry( instance, value, &ex );
  273. catch_exception( &ex );
  274. }
  275. - (char *)videoCropGeometry
  276. {
  277. libvlc_exception_t ex;
  278. libvlc_exception_init( &ex );
  279. char * result = libvlc_video_get_crop_geometry( instance, &ex );
  280. catch_exception( &ex );
  281. return result;
  282. }
  283. - (void)setVideoAspectRatio:(char *)value
  284. {
  285. libvlc_exception_t ex;
  286. libvlc_exception_init( &ex );
  287. libvlc_video_set_aspect_ratio( instance, value, &ex );
  288. catch_exception( &ex );
  289. }
  290. - (char *)videoAspectRatio
  291. {
  292. libvlc_exception_t ex;
  293. libvlc_exception_init( &ex );
  294. char * result = libvlc_video_get_aspect_ratio( instance, &ex );
  295. catch_exception( &ex );
  296. return result;
  297. }
  298. - (void)setVideoTeleText:(int)value
  299. {
  300. libvlc_exception_t ex;
  301. libvlc_exception_init( &ex );
  302. libvlc_video_set_teletext( instance, value, &ex );
  303. catch_exception( &ex );
  304. }
  305. - (int)videoTeleText
  306. {
  307. libvlc_exception_t ex;
  308. libvlc_exception_init( &ex );
  309. int result = libvlc_video_get_teletext( instance, &ex );
  310. catch_exception( &ex );
  311. return result;
  312. }
  313. - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
  314. {
  315. libvlc_exception_t ex;
  316. libvlc_exception_init( &ex );
  317. libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
  318. catch_exception( &ex );
  319. }
  320. - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
  321. {
  322. libvlc_exception_t ex;
  323. libvlc_exception_init( &ex );
  324. libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
  325. catch_exception( &ex );
  326. }
  327. - (void)setRate:(float)value
  328. {
  329. libvlc_exception_t ex;
  330. libvlc_exception_init( &ex );
  331. libvlc_media_player_set_rate( instance, value, &ex );
  332. catch_exception( &ex );
  333. }
  334. - (float)rate
  335. {
  336. libvlc_exception_t ex;
  337. libvlc_exception_init( &ex );
  338. float result = libvlc_media_player_get_rate( instance, &ex );
  339. catch_exception( &ex );
  340. return result;
  341. }
  342. - (NSSize)videoSize
  343. {
  344. libvlc_exception_t ex;
  345. libvlc_exception_init( &ex );
  346. NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
  347. libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
  348. catch_exception( &ex );
  349. return result;
  350. }
  351. - (BOOL)hasVideoOut
  352. {
  353. libvlc_exception_t ex;
  354. libvlc_exception_init( &ex );
  355. BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
  356. if (libvlc_exception_raised( &ex ))
  357. {
  358. libvlc_exception_clear( &ex );
  359. return NO;
  360. }
  361. else
  362. return result;
  363. }
  364. - (float)framesPerSecond
  365. {
  366. libvlc_exception_t ex;
  367. libvlc_exception_init( &ex );
  368. float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
  369. catch_exception( &ex );
  370. return result;
  371. }
  372. - (void)setTime:(VLCTime *)value
  373. {
  374. libvlc_exception_t ex;
  375. libvlc_exception_init( &ex );
  376. // Time is managed in seconds, while duration is managed in microseconds
  377. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  378. libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
  379. (value ? [[value numberValue] longLongValue] / 1000 : 0),
  380. &ex );
  381. catch_exception( &ex );
  382. }
  383. - (VLCTime *)time
  384. {
  385. return cachedTime;
  386. }
  387. - (VLCTime *)remainingTime
  388. {
  389. double currentTime = [[cachedTime numberValue] doubleValue];
  390. double remaining = currentTime / position * (1 - position);
  391. return [VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]];
  392. }
  393. - (int)fps
  394. {
  395. libvlc_exception_t ex;
  396. libvlc_exception_init( &ex );
  397. int result = libvlc_media_player_get_fps( instance, &ex );
  398. catch_exception( &ex );
  399. return result;
  400. }
  401. #pragma mark -
  402. #pragma mark Chapters
  403. - (void)setCurrentChapterIndex:(NSUInteger)value;
  404. {
  405. libvlc_exception_t ex;
  406. libvlc_exception_init( &ex );
  407. libvlc_media_player_set_chapter( instance, value, &ex );
  408. catch_exception( &ex );
  409. }
  410. - (NSUInteger)currentChapterIndex
  411. {
  412. libvlc_exception_t ex;
  413. libvlc_exception_init( &ex );
  414. int count = libvlc_media_player_get_chapter_count( instance, &ex );
  415. catch_exception( &ex );
  416. if (count <= 0)
  417. return NSNotFound;
  418. NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
  419. catch_exception( &ex );
  420. return result;
  421. }
  422. - (void)nextChapter
  423. {
  424. libvlc_exception_t ex;
  425. libvlc_exception_init( &ex );
  426. libvlc_media_player_next_chapter( instance, &ex );
  427. catch_exception( &ex );
  428. }
  429. - (void)previousChapter
  430. {
  431. libvlc_exception_t ex;
  432. libvlc_exception_init( &ex );
  433. libvlc_media_player_previous_chapter( instance, &ex );
  434. catch_exception( &ex );
  435. }
  436. - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
  437. {
  438. libvlc_exception_t ex;
  439. libvlc_exception_init( &ex );
  440. int count = libvlc_media_player_get_chapter_count(instance, &ex);
  441. if (count <= 0)
  442. return [NSArray array];
  443. libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
  444. NSMutableArray *tempArray = [NSMutableArray array];
  445. NSInteger i;
  446. for (i = 0; i < count ; i++)
  447. {
  448. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  449. tracks = tracks->p_next;
  450. }
  451. return [NSArray arrayWithArray: tempArray];
  452. }
  453. #pragma mark -
  454. #pragma mark Titles
  455. - (void)setCurrentTitleIndex:(NSUInteger)value
  456. {
  457. libvlc_exception_t ex;
  458. libvlc_exception_init( &ex );
  459. libvlc_media_player_set_title( instance, value, &ex );
  460. catch_exception( &ex );
  461. }
  462. - (NSUInteger)currentTitleIndex
  463. {
  464. libvlc_exception_t ex;
  465. libvlc_exception_init( &ex );
  466. int count = libvlc_media_player_get_title_count( instance, &ex );
  467. catch_exception( &ex );
  468. if (count <= 0)
  469. return NSNotFound;
  470. NSUInteger result = libvlc_media_player_get_title( instance, &ex );
  471. catch_exception( &ex );
  472. return result;
  473. }
  474. - (int)countOfTitles
  475. {
  476. libvlc_exception_t ex;
  477. libvlc_exception_init( &ex );
  478. int result = libvlc_media_player_get_title_count( instance, &ex );
  479. catch_exception( &ex );
  480. return result;
  481. }
  482. - (NSArray *)titles
  483. {
  484. libvlc_exception_t ex;
  485. libvlc_exception_init( &ex );
  486. libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
  487. NSMutableArray *tempArray = [NSMutableArray array];
  488. NSInteger i;
  489. for (i = 0; i < [self countOfTitles] ; i++)
  490. {
  491. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  492. tracks = tracks->p_next;
  493. }
  494. return [NSArray arrayWithArray: tempArray];
  495. }
  496. #pragma mark -
  497. #pragma mark Audio tracks
  498. - (void)setCurrentAudioTrackIndex:(NSUInteger)value
  499. {
  500. libvlc_exception_t ex;
  501. libvlc_exception_init( &ex );
  502. libvlc_audio_set_track( instance, (int)value, &ex );
  503. catch_exception( &ex );
  504. }
  505. - (NSUInteger)currentAudioTrackIndex
  506. {
  507. libvlc_exception_t ex;
  508. libvlc_exception_init( &ex );
  509. int count = libvlc_audio_get_track_count( instance, &ex );
  510. catch_exception( &ex );
  511. if (count <= 0)
  512. return NSNotFound;
  513. NSUInteger result = libvlc_audio_get_track( instance, &ex );
  514. catch_exception( &ex );
  515. return result;
  516. }
  517. - (NSArray *)audioTracks
  518. {
  519. libvlc_exception_t ex;
  520. libvlc_exception_init( &ex );
  521. int count = libvlc_audio_get_track_count( instance, &ex );
  522. catch_exception( &ex );
  523. if (count <= 0)
  524. return [NSArray array];
  525. libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
  526. NSMutableArray *tempArray = [NSMutableArray array];
  527. NSInteger i;
  528. for (i = 0; i < count ; i++)
  529. {
  530. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  531. tracks = tracks->p_next;
  532. }
  533. return [NSArray arrayWithArray: tempArray];
  534. }
  535. - (void)setAudioChannel:(int)value
  536. {
  537. libvlc_exception_t ex;
  538. libvlc_exception_init( &ex );
  539. libvlc_audio_set_channel( instance, value, &ex );
  540. catch_exception( &ex );
  541. }
  542. - (int)audioChannel
  543. {
  544. libvlc_exception_t ex;
  545. libvlc_exception_init( &ex );
  546. int result = libvlc_audio_get_channel( instance, &ex );
  547. catch_exception( &ex );
  548. return result;
  549. }
  550. - (void)setMedia:(VLCMedia *)value
  551. {
  552. if (media != value)
  553. {
  554. if (media && [media compare:value] == NSOrderedSame)
  555. return;
  556. [media release];
  557. media = [value retain];
  558. libvlc_exception_t ex;
  559. libvlc_exception_init( &ex );
  560. libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
  561. catch_exception( &ex );
  562. }
  563. }
  564. - (VLCMedia *)media
  565. {
  566. return media;
  567. }
  568. - (BOOL)play
  569. {
  570. libvlc_exception_t ex;
  571. libvlc_exception_init( &ex );
  572. libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
  573. catch_exception( &ex );
  574. return YES;
  575. }
  576. - (void)pause
  577. {
  578. if( [NSThread isMainThread] )
  579. {
  580. /* Hack because we create a dead lock here, when the vout is stopped
  581. * and tries to recontact us on the main thread */
  582. /* FIXME: to do this properly we need to do some locking. We may want
  583. * to move that to libvlc */
  584. [self performSelectorInBackground:@selector(pause) withObject:nil];
  585. return;
  586. }
  587. // Pause the stream
  588. libvlc_exception_t ex;
  589. libvlc_exception_init( &ex );
  590. libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
  591. // fail gracefully
  592. // in most cases, it's just EOF so let's stop
  593. if (libvlc_exception_raised(&ex))
  594. [self stop];
  595. libvlc_exception_clear(&ex);
  596. }
  597. - (void)stop
  598. {
  599. libvlc_exception_t ex;
  600. libvlc_exception_init( &ex );
  601. libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
  602. catch_exception( &ex );
  603. }
  604. - (void)fastForward
  605. {
  606. [self fastForwardAtRate: 2.0];
  607. }
  608. - (void)fastForwardAtRate:(float)rate
  609. {
  610. [self setRate:rate];
  611. }
  612. - (void)rewind
  613. {
  614. [self rewindAtRate: 2.0];
  615. }
  616. - (void)rewindAtRate:(float)rate
  617. {
  618. [self setRate: -rate];
  619. }
  620. - (void)jumpBackward:(NSInteger)interval
  621. {
  622. if( [self isSeekable] )
  623. {
  624. interval = interval * 1000000;
  625. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  626. }
  627. }
  628. - (void)jumpForward:(NSInteger)interval
  629. {
  630. if( [self isSeekable] )
  631. {
  632. interval = interval * 1000000;
  633. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  634. }
  635. }
  636. - (void)extraShortJumpBackward
  637. {
  638. [self jumpBackward:3];
  639. }
  640. - (void)extraShortJumpForward
  641. {
  642. [self jumpForward:3];
  643. }
  644. - (void)shortJumpBackward
  645. {
  646. [self jumpBackward:10];
  647. }
  648. - (void)shortJumpForward
  649. {
  650. [self jumpForward:10];
  651. }
  652. - (void)mediumJumpBackward
  653. {
  654. [self jumpBackward:60];
  655. }
  656. - (void)mediumJumpForward
  657. {
  658. [self jumpForward:60];
  659. }
  660. - (void)longJumpBackward
  661. {
  662. [self jumpBackward:300];
  663. }
  664. - (void)longJumpForward
  665. {
  666. [self jumpForward:300];
  667. }
  668. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  669. {
  670. return [NSSet setWithObjects:@"state", nil];
  671. }
  672. - (BOOL)isPlaying
  673. {
  674. VLCMediaPlayerState state = [self state];
  675. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  676. (state == VLCMediaPlayerStatePlaying));
  677. }
  678. - (BOOL)willPlay
  679. {
  680. libvlc_exception_t ex;
  681. libvlc_exception_init( &ex );
  682. BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
  683. if (libvlc_exception_raised(&ex))
  684. {
  685. libvlc_exception_clear(&ex);
  686. return NO;
  687. }
  688. else
  689. return ret;
  690. }
  691. static const VLCMediaPlayerState libvlc_to_local_state[] =
  692. {
  693. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  694. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  695. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  696. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  697. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  698. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  699. [libvlc_Error] = VLCMediaPlayerStateError
  700. };
  701. - (VLCMediaPlayerState)state
  702. {
  703. return cachedState;
  704. }
  705. - (float)position
  706. {
  707. return position;
  708. }
  709. - (void)setPosition:(float)newPosition
  710. {
  711. libvlc_exception_t ex;
  712. libvlc_exception_init( &ex );
  713. libvlc_media_player_set_position( instance, newPosition, &ex );
  714. catch_exception( &ex );
  715. }
  716. - (BOOL)isSeekable
  717. {
  718. libvlc_exception_t ex;
  719. libvlc_exception_init( &ex );
  720. BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
  721. catch_exception( &ex );
  722. return ret;
  723. }
  724. - (BOOL)canPause
  725. {
  726. libvlc_exception_t ex;
  727. libvlc_exception_init( &ex );
  728. BOOL ret = libvlc_media_player_can_pause( instance, &ex );
  729. catch_exception( &ex );
  730. return ret;
  731. }
  732. - (void *)libVLCMediaPlayer
  733. {
  734. return instance;
  735. }
  736. @end
  737. @implementation VLCMediaPlayer (Private)
  738. - (id)initWithDrawable:(id)aDrawable
  739. {
  740. if (self = [super init])
  741. {
  742. delegate = nil;
  743. media = nil;
  744. cachedTime = [[VLCTime nullTime] retain];
  745. position = 0.0f;
  746. cachedState = VLCMediaPlayerStateStopped;
  747. // Create a media instance, it doesn't matter what library we start off with
  748. // it will change depending on the media descriptor provided to the media
  749. // instance
  750. libvlc_exception_t ex;
  751. libvlc_exception_init( &ex );
  752. instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
  753. catch_exception( &ex );
  754. [self registerObservers];
  755. [self setDrawable:aDrawable];
  756. }
  757. return self;
  758. }
  759. - (void)registerObservers
  760. {
  761. libvlc_exception_t ex;
  762. libvlc_exception_init( &ex );
  763. // Attach event observers into the media instance
  764. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
  765. libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
  766. libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
  767. libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
  768. libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
  769. /* FIXME: We may want to turn that off when none is interested by that */
  770. libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
  771. libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
  772. catch_exception( &ex );
  773. }
  774. - (void)unregisterObservers
  775. {
  776. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
  777. libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
  778. libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
  779. libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
  780. libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
  781. libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
  782. libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
  783. }
  784. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  785. {
  786. [self willChangeValueForKey:@"time"];
  787. [self willChangeValueForKey:@"remainingTime"];
  788. [cachedTime release];
  789. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  790. [self didChangeValueForKey:@"remainingTime"];
  791. [self didChangeValueForKey:@"time"];
  792. }
  793. - (void)delaySleep
  794. {
  795. UpdateSystemActivity(UsrActivity);
  796. }
  797. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  798. {
  799. // This seems to be the most relevant place to delay sleeping and screen saver.
  800. [self delaySleep];
  801. [self willChangeValueForKey:@"position"];
  802. position = [newPosition floatValue];
  803. [self didChangeValueForKey:@"position"];
  804. }
  805. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  806. {
  807. [self willChangeValueForKey:@"state"];
  808. cachedState = [newState intValue];
  809. [self didChangeValueForKey:@"state"];
  810. }
  811. @end