VLCMediaPlayer.m 26 KB

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