VLCMovieViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //
  2. // VLCMovieViewController.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 27.02.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. #import "VLCMovieViewController.h"
  9. #import "VLCExternalDisplayController.h"
  10. #define INPUT_RATE_DEFAULT 1000.
  11. @interface VLCMovieViewController () <UIGestureRecognizerDelegate>
  12. @property (nonatomic, strong) UIPopoverController *masterPopoverController;
  13. @property (nonatomic, strong) UIWindow *externalWindow;
  14. @end
  15. @implementation VLCMovieViewController
  16. - (void)dealloc
  17. {
  18. [[NSNotificationCenter defaultCenter] removeObserver:self];
  19. }
  20. #pragma mark - Managing the media item
  21. - (void)setMediaItem:(id)newMediaItem
  22. {
  23. if (_mediaItem != newMediaItem)
  24. _mediaItem = newMediaItem;
  25. if (self.masterPopoverController != nil)
  26. [self.masterPopoverController dismissPopoverAnimated:YES];
  27. }
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. self.wantsFullScreenLayout = YES;
  32. _mediaPlayer = [[VLCMediaPlayer alloc] init];
  33. [_mediaPlayer setDelegate:self];
  34. [_mediaPlayer setDrawable:self.movieView];
  35. self.videoFilterView.hidden = YES;
  36. _videoFiltersHidden = YES;
  37. _hueLabel.text = NSLocalizedString(@"Hue", @"");
  38. _contrastLabel.text = NSLocalizedString(@"Contrast", @"");
  39. _brightnessLabel.text = NSLocalizedString(@"Brightness", @"");
  40. _saturationLabel.text = NSLocalizedString(@"Saturation", @"");
  41. _gammaLabel.text = NSLocalizedString(@"Gamma", @"");
  42. self.playbackView.hidden = YES;
  43. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  44. [center addObserver:self selector:@selector(handleExternalScreenDidConnect:)
  45. name:UIScreenDidConnectNotification object:nil];
  46. [center addObserver:self selector:@selector(handleExternalScreenDidDisconnect:)
  47. name:UIScreenDidDisconnectNotification object:nil];
  48. [center addObserver:self selector:@selector(appWillResign:) name:UIApplicationWillResignActiveNotification object:nil];
  49. _playingExternallyTitle.text = NSLocalizedString(@"PLAYING_EXTERNALLY_TITLE", @"");
  50. _playingExternallyDescription.text = NSLocalizedString(@"PLAYING_EXTERNALLY_DESC", @"");
  51. if ([self hasExternalDisplay])
  52. [self showOnExternalDisplay];
  53. _movieView.userInteractionEnabled = NO;
  54. UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toogleControlsVisible)];
  55. recognizer.delegate = self;
  56. [self.view addGestureRecognizer:recognizer];
  57. [self resetIdleTimer];
  58. }
  59. - (void)viewWillAppear:(BOOL)animated
  60. {
  61. [super viewWillAppear:animated];
  62. [self.navigationController setNavigationBarHidden:YES animated:YES];
  63. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  64. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
  65. if (!self.mediaItem && !self.url)
  66. return;
  67. if (self.mediaItem) {
  68. self.title = [self.mediaItem title];
  69. [_mediaPlayer setMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:self.mediaItem.url]]];
  70. } else {
  71. [_mediaPlayer setMedia:[VLCMedia mediaWithURL:self.url]];
  72. self.title = @"Network Stream";
  73. }
  74. [_mediaPlayer play];
  75. if (self.mediaItem.lastPosition && [self.mediaItem.lastPosition floatValue] < 0.99)
  76. [_mediaPlayer setPosition:[self.mediaItem.lastPosition floatValue]];
  77. self.playbackSpeedSlider.value = [self _playbackSpeed];
  78. [self _updatePlaybackSpeedIndicator];
  79. }
  80. - (void)viewWillDisappear:(BOOL)animated
  81. {
  82. if (_idleTimer)
  83. [_idleTimer invalidate];
  84. [self.navigationController setNavigationBarHidden:NO animated:YES];
  85. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
  86. [_mediaPlayer pause];
  87. [super viewWillDisappear:animated];
  88. self.mediaItem.lastPosition = @([_mediaPlayer position]);
  89. [_mediaPlayer stop];
  90. }
  91. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  92. {
  93. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  94. if (self)
  95. self.title = @"Video Playback";
  96. return self;
  97. }
  98. #pragma mark - controls visibility
  99. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  100. {
  101. if (touch.view != self.view)
  102. return NO;
  103. return YES;
  104. }
  105. - (void)toogleControlsVisible
  106. {
  107. _controlsHidden = !_controlsHidden;
  108. CGFloat alpha = _controlsHidden? 0.0f: 1.0f;
  109. if (!_controlsHidden) {
  110. _controllerPanel.alpha = 0.0f;
  111. _controllerPanel.hidden = !_videoFiltersHidden || !_playbackViewHidden;
  112. _toolbar.alpha = 0.0f;
  113. _toolbar.hidden = NO;
  114. _videoFilterView.alpha = 0.0f;
  115. _videoFilterView.hidden = _videoFiltersHidden;
  116. _videoFilterButton.alpha = 0.0f;
  117. _videoFilterButton.hidden = NO;
  118. _playbackView.alpha = 0.0f;
  119. _playbackView.hidden = _playbackViewHidden;
  120. _playbackButton.alpha = 0.0f;
  121. _playbackButton.hidden = NO;
  122. }
  123. void (^animationBlock)() = ^() {
  124. _controllerPanel.alpha = alpha;
  125. _toolbar.alpha = alpha;
  126. _videoFilterView.alpha = alpha;
  127. _videoFilterButton.alpha = alpha;
  128. _playbackView.alpha = alpha;
  129. _playbackButton.alpha = alpha;
  130. };
  131. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  132. if (_videoFiltersHidden && _playbackViewHidden)
  133. _controllerPanel.hidden = _controlsHidden;
  134. else
  135. _controllerPanel.hidden = YES;
  136. _toolbar.hidden = _controlsHidden;
  137. _videoFilterView.hidden = _videoFiltersHidden;
  138. _videoFilterButton.hidden = _controlsHidden;
  139. _playbackView.hidden = _playbackViewHidden;
  140. _playbackButton.hidden = _controlsHidden;
  141. };
  142. [UIView animateWithDuration:0.3f animations:animationBlock completion:completionBlock];
  143. [[UIApplication sharedApplication] setStatusBarHidden:_controlsHidden withAnimation:UIStatusBarAnimationFade];
  144. }
  145. - (void)resetIdleTimer
  146. {
  147. if (!_idleTimer)
  148. _idleTimer = [NSTimer scheduledTimerWithTimeInterval:2.
  149. target:self
  150. selector:@selector(idleTimerExceeded)
  151. userInfo:nil
  152. repeats:NO];
  153. else {
  154. if (fabs([_idleTimer.fireDate timeIntervalSinceNow]) < 2.)
  155. [_idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.]];
  156. }
  157. }
  158. - (void)idleTimerExceeded
  159. {
  160. _idleTimer = nil;
  161. if (!_controlsHidden)
  162. [self toogleControlsVisible];
  163. }
  164. - (UIResponder *)nextResponder
  165. {
  166. [self resetIdleTimer];
  167. return [super nextResponder];
  168. }
  169. #pragma mark - controls
  170. - (IBAction)closePlayback:(id)sender
  171. {
  172. [self.navigationController popViewControllerAnimated:YES];
  173. }
  174. - (IBAction)positionSliderAction:(UISlider *)sender
  175. {
  176. _mediaPlayer.position = sender.value;
  177. [self resetIdleTimer];
  178. }
  179. - (void)mediaPlayerTimeChanged:(NSNotification *)aNotification {
  180. self.positionSlider.value = [_mediaPlayer position];
  181. self.timeDisplay.title = [[_mediaPlayer remainingTime] stringValue];
  182. }
  183. - (void)mediaPlayerStateChanged:(NSNotification *)aNotification
  184. {
  185. // TODO
  186. }
  187. - (IBAction)play:(id)sender
  188. {
  189. if ([_mediaPlayer isPlaying]) {
  190. [_mediaPlayer pause];
  191. _playPauseButton.titleLabel.text = @"Pse";
  192. } else {
  193. [_mediaPlayer play];
  194. _playPauseButton.titleLabel.text = @"Play";
  195. }
  196. }
  197. - (IBAction)forward:(id)sender
  198. {
  199. [_mediaPlayer mediumJumpForward];
  200. }
  201. - (IBAction)backward:(id)sender
  202. {
  203. [_mediaPlayer mediumJumpBackward];
  204. }
  205. - (IBAction)switchAudioTrack:(id)sender
  206. {
  207. _audiotrackActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Choose Audio Track", @"audio track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  208. NSArray *audioTracks = [_mediaPlayer audioTrackNames];
  209. NSUInteger count = [audioTracks count];
  210. for (NSUInteger i = 0; i < count; i++)
  211. [_audiotrackActionSheet addButtonWithTitle:audioTracks[i]];
  212. [_audiotrackActionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"audio track selector")];
  213. [_audiotrackActionSheet setCancelButtonIndex:[_audiotrackActionSheet numberOfButtons] - 1];
  214. [_audiotrackActionSheet showFromRect:[self.audioSwitcherButton frame] inView:self.audioSwitcherButton animated:YES];
  215. }
  216. - (IBAction)switchSubtitleTrack:(id)sender
  217. {
  218. NSArray *spuTracks = [_mediaPlayer videoSubTitlesNames];
  219. NSUInteger count = [spuTracks count];
  220. if (count <= 1)
  221. return;
  222. _subtitleActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Choose Subtitle Track", @"subtitle track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  223. for (NSUInteger i = 0; i < count; i++)
  224. [_subtitleActionSheet addButtonWithTitle:spuTracks[i]];
  225. [_subtitleActionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"subtitle track selector")];
  226. [_subtitleActionSheet setCancelButtonIndex:[_subtitleActionSheet numberOfButtons] - 1];
  227. [_subtitleActionSheet showFromRect:[self.subtitleSwitcherButton frame] inView:self.subtitleSwitcherButton animated:YES];
  228. }
  229. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  230. NSUInteger arrayIndex = 0;
  231. NSArray *indexArray;
  232. NSArray *namesArray;
  233. if (actionSheet == _subtitleActionSheet) {
  234. namesArray = _mediaPlayer.videoSubTitlesNames;
  235. arrayIndex = [namesArray indexOfObject:[actionSheet buttonTitleAtIndex:buttonIndex]];
  236. if (arrayIndex != NSNotFound) {
  237. indexArray = _mediaPlayer.videoSubTitlesIndexes;
  238. _mediaPlayer.currentVideoSubTitleIndex = [indexArray[arrayIndex] intValue];
  239. }
  240. } else if (actionSheet == _audiotrackActionSheet) {
  241. namesArray = _mediaPlayer.audioTrackNames;
  242. arrayIndex = [namesArray indexOfObject:[actionSheet buttonTitleAtIndex:buttonIndex]];
  243. if (arrayIndex != NSNotFound) {
  244. indexArray = _mediaPlayer.audioTrackIndexes;
  245. _mediaPlayer.currentAudioTrackIndex = [indexArray[arrayIndex] intValue];
  246. }
  247. } else if (actionSheet == _aspectRatioActionSheet) {
  248. if (actionSheet.cancelButtonIndex != buttonIndex) {
  249. if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Default"])
  250. _mediaPlayer.videoAspectRatio = NULL;
  251. else
  252. _mediaPlayer.videoAspectRatio = (char *)[[actionSheet buttonTitleAtIndex:buttonIndex] UTF8String];
  253. }
  254. } else if (actionSheet == _cropActionSheet) {
  255. if (actionSheet.cancelButtonIndex != buttonIndex) {
  256. if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Default"])
  257. _mediaPlayer.videoCropGeometry = NULL;
  258. else
  259. _mediaPlayer.videoCropGeometry = (char *)[[actionSheet buttonTitleAtIndex:buttonIndex] UTF8String];
  260. }
  261. }
  262. }
  263. #pragma mark - Video Filter UI
  264. - (IBAction)videoFilterToggle:(id)sender
  265. {
  266. if (!_playbackViewHidden)
  267. self.playbackView.hidden = _playbackViewHidden = YES;
  268. self.videoFilterView.hidden = !_videoFiltersHidden;
  269. _videoFiltersHidden = self.videoFilterView.hidden;
  270. self.controllerPanel.hidden = !_videoFiltersHidden;
  271. }
  272. - (IBAction)videoFilterSliderAction:(id)sender
  273. {
  274. if (sender == self.hueSlider)
  275. _mediaPlayer.hue = (int)self.hueSlider.value;
  276. else if (sender == self.contrastSlider)
  277. _mediaPlayer.contrast = self.contrastSlider.value;
  278. else if (sender == self.brightnessSlider) {
  279. if ([self hasExternalDisplay])
  280. _mediaPlayer.brightness = self.brightnessSlider.value;
  281. else
  282. [[UIScreen mainScreen] setBrightness:(self.brightnessSlider.value / 2.)];
  283. } else if (sender == self.saturationSlider)
  284. _mediaPlayer.saturation = self.saturationSlider.value;
  285. else if (sender == self.gammaSlider)
  286. _mediaPlayer.gamma = self.gammaSlider.value;
  287. else if (sender == self.resetVideoFilterButton) {
  288. _mediaPlayer.hue = self.hueSlider.value = 0.;
  289. _mediaPlayer.contrast = self.contrastSlider.value = 1.;
  290. _mediaPlayer.brightness = self.brightnessSlider.value = 1.;
  291. _mediaPlayer.saturation = self.saturationSlider.value = 1.;
  292. _mediaPlayer.gamma = self.gammaSlider.value = 1.;
  293. } else
  294. APLog(@"unknown sender for videoFilterSliderAction");
  295. [self resetIdleTimer];
  296. }
  297. #pragma mark - playback view
  298. - (IBAction)playbackSpeedSliderAction:(UISlider *)sender
  299. {
  300. double speed = pow(2, sender.value / 17.);
  301. float rate = INPUT_RATE_DEFAULT / speed;
  302. if (_currentPlaybackRate != rate)
  303. [_mediaPlayer setRate:INPUT_RATE_DEFAULT / rate];
  304. _currentPlaybackRate = rate;
  305. [self _updatePlaybackSpeedIndicator];
  306. [self resetIdleTimer];
  307. }
  308. - (void)_updatePlaybackSpeedIndicator
  309. {
  310. float f_value = self.playbackSpeedSlider.value;
  311. double speed = pow(2, f_value / 17.);
  312. self.playbackSpeedIndicator.text = [NSString stringWithFormat:@"%.2fx", speed];
  313. }
  314. - (float)_playbackSpeed
  315. {
  316. float f_rate = _mediaPlayer.rate;
  317. double value = 17 * log(f_rate) / log(2.);
  318. float returnValue = (int) ((value > 0) ? value + .5 : value - .5);
  319. if (returnValue < -34.)
  320. returnValue = -34.;
  321. else if (returnValue > 34.)
  322. returnValue = 34.;
  323. _currentPlaybackRate = returnValue;
  324. return returnValue;
  325. }
  326. - (IBAction)videoDimensionAction:(id)sender
  327. {
  328. if (sender == self.playbackButton) {
  329. if (!_videoFiltersHidden)
  330. self.videoFilterButton.hidden = _videoFiltersHidden = YES;
  331. self.playbackView.hidden = !_playbackViewHidden;
  332. _playbackViewHidden = self.playbackView.hidden;
  333. self.controllerPanel.hidden = !_playbackViewHidden;
  334. } else if (sender == self.aspectRatioButton) {
  335. NSArray *ratios = @[@"Default", @"1:1", @"4:3", @"16:9", @"16:10", @"2.21:1", @"2:35:1", @"2.39:1", @"5:4"];
  336. NSUInteger count = [ratios count];
  337. _aspectRatioActionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose Aspect Ratio" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  338. for (NSUInteger i = 0; i < count; i++)
  339. [_aspectRatioActionSheet addButtonWithTitle:ratios[i]];
  340. [_aspectRatioActionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"subtitle track selector")];
  341. [_aspectRatioActionSheet setCancelButtonIndex:[_aspectRatioActionSheet numberOfButtons] - 1];
  342. [_aspectRatioActionSheet showFromRect:[self.aspectRatioButton frame] inView:self.aspectRatioButton animated:YES];
  343. } else if (sender == self.cropButton) {
  344. NSArray *ratios = @[@"Default", @"16:10", @"16:9", @"1.85:1", @"2.21:1", @"2.35:1", @"2:39:1", @"5:3", @"4:3", @"5:4", @"1:1"];
  345. NSUInteger count = [ratios count];
  346. _cropActionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose Aspect Ratio" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  347. for (NSUInteger i = 0; i < count; i++)
  348. [_cropActionSheet addButtonWithTitle:ratios[i]];
  349. [_cropActionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"subtitle track selector")];
  350. [_cropActionSheet setCancelButtonIndex:[_cropActionSheet numberOfButtons] - 1];
  351. [_cropActionSheet showFromRect:[self.cropButton frame] inView:self.cropButton animated:YES];
  352. }
  353. }
  354. #pragma mark -
  355. - (void)appWillResign:(NSNotification *)aNotification
  356. {
  357. self.mediaItem.lastPosition = @([_mediaPlayer position]);
  358. }
  359. #pragma mark - autorotation
  360. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  361. return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  362. || toInterfaceOrientation != UIInterfaceOrientationMaskPortraitUpsideDown;
  363. }
  364. #pragma mark - External Display
  365. - (BOOL)hasExternalDisplay
  366. {
  367. return ([[UIScreen screens] count] > 1);
  368. }
  369. - (void)showOnExternalDisplay
  370. {
  371. UIScreen *screen = [UIScreen screens][1];
  372. screen.overscanCompensation = UIScreenOverscanCompensationInsetApplicationFrame;
  373. self.externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
  374. UIViewController *controller = [[VLCExternalDisplayController alloc] init];
  375. self.externalWindow.rootViewController = controller;
  376. [controller.view addSubview:_movieView];
  377. controller.view.frame = screen.bounds;
  378. _movieView.frame = screen.bounds;
  379. self.playingExternallyView.hidden = NO;
  380. self.externalWindow.screen = screen;
  381. self.externalWindow.hidden = NO;
  382. }
  383. - (void)hideFromExternalDisplay
  384. {
  385. [self.view addSubview:_movieView];
  386. [self.view sendSubviewToBack:_movieView];
  387. _movieView.frame = self.view.frame;
  388. self.playingExternallyView.hidden = YES;
  389. self.externalWindow.hidden = YES;
  390. self.externalWindow = nil;
  391. }
  392. - (void)handleExternalScreenDidConnect:(NSNotification *)notification
  393. {
  394. [self showOnExternalDisplay];
  395. }
  396. - (void)handleExternalScreenDidDisconnect:(NSNotification *)notification
  397. {
  398. [self hideFromExternalDisplay];
  399. }
  400. @end