VLCMovieViewController.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. #import <sys/sysctl.h> // for sysctlbyname
  11. #define INPUT_RATE_DEFAULT 1000.
  12. @interface VLCMovieViewController () <UIGestureRecognizerDelegate>
  13. {
  14. BOOL _shouldResumePlaying;
  15. BOOL _viewAppeared;
  16. }
  17. @property (nonatomic, strong) UIPopoverController *masterPopoverController;
  18. @property (nonatomic, strong) UIWindow *externalWindow;
  19. @end
  20. @implementation VLCMovieViewController
  21. - (void)dealloc
  22. {
  23. [[NSNotificationCenter defaultCenter] removeObserver:self];
  24. }
  25. #pragma mark - Managing the media item
  26. - (void)setMediaItem:(id)newMediaItem
  27. {
  28. if (_mediaItem != newMediaItem)
  29. _mediaItem = newMediaItem;
  30. if (self.masterPopoverController != nil)
  31. [self.masterPopoverController dismissPopoverAnimated:YES];
  32. }
  33. - (void)viewDidLoad
  34. {
  35. [super viewDidLoad];
  36. self.wantsFullScreenLayout = YES;
  37. self.videoFilterView.hidden = YES;
  38. _videoFiltersHidden = YES;
  39. _hueLabel.text = NSLocalizedString(@"VFILTER_HUE", @"");
  40. _contrastLabel.text = NSLocalizedString(@"VFILTER_CONTRAST", @"");
  41. _brightnessLabel.text = NSLocalizedString(@"VFILTER_BRIGHTNESS", @"");
  42. _saturationLabel.text = NSLocalizedString(@"VFILTER_SATURATION", @"");
  43. _gammaLabel.text = NSLocalizedString(@"VFILTER_GAMMA", @"");
  44. self.playbackSpeedView.hidden = YES;
  45. _playbackSpeedViewHidden = YES;
  46. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  47. [center addObserver:self selector:@selector(handleExternalScreenDidConnect:)
  48. name:UIScreenDidConnectNotification object:nil];
  49. [center addObserver:self selector:@selector(handleExternalScreenDidDisconnect:)
  50. name:UIScreenDidDisconnectNotification object:nil];
  51. [center addObserver:self selector:@selector(applicationWillResignActive:)
  52. name:UIApplicationWillResignActiveNotification object:nil];
  53. [center addObserver:self selector:@selector(applicationDidBecomeActive:)
  54. name:UIApplicationDidBecomeActiveNotification object:nil];
  55. [center addObserver:self selector:@selector(applicationDidEnterBackground:)
  56. name:UIApplicationDidEnterBackgroundNotification object:nil];
  57. _playingExternallyTitle.text = NSLocalizedString(@"PLAYING_EXTERNALLY_TITLE", @"");
  58. _playingExternallyDescription.text = NSLocalizedString(@"PLAYING_EXTERNALLY_DESC", @"");
  59. if ([self hasExternalDisplay])
  60. [self showOnExternalDisplay];
  61. _movieView.userInteractionEnabled = NO;
  62. UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleControlsVisible)];
  63. recognizer.delegate = self;
  64. [self.view addGestureRecognizer:recognizer];
  65. #if 0 // FIXME: trac #8742
  66. UISwipeGestureRecognizer *leftSwipeRecognizer = [[VLCHorizontalSwipeGestureRecognizer alloc] initWithTarget:self action:nil];
  67. leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
  68. leftSwipeRecognizer.delegate = self;
  69. [self.view addGestureRecognizer:leftSwipeRecognizer];
  70. UISwipeGestureRecognizer *rightSwipeRecognizer = [[VLCHorizontalSwipeGestureRecognizer alloc] initWithTarget:self action:nil];
  71. rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
  72. rightSwipeRecognizer.delegate = self;
  73. [self.view addGestureRecognizer:rightSwipeRecognizer];
  74. UISwipeGestureRecognizer *upSwipeRecognizer = [[VLCVerticalSwipeGestureRecognizer alloc] initWithTarget:self action:nil];
  75. upSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
  76. upSwipeRecognizer.delegate = self;
  77. [self.view addGestureRecognizer:upSwipeRecognizer];
  78. UISwipeGestureRecognizer *downSwipeRecognizer = [[VLCVerticalSwipeGestureRecognizer alloc] initWithTarget:self action:nil];
  79. downSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
  80. downSwipeRecognizer.delegate = self;
  81. [self.view addGestureRecognizer:downSwipeRecognizer];
  82. #endif
  83. _aspectRatios = @[@"DEFAULT", @"4:3", @"16:9", @"16:10", @"2.21:1", @"FILL_TO_SCREEN"];
  84. }
  85. - (void)viewWillAppear:(BOOL)animated
  86. {
  87. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  88. NSArray *options = @[[[defaults objectForKey:kVLCSettingVerboseOutput] boolValue] ? kVLCSettingVerboseOutputOnValue : kVLCSettingVerboseOutputOffValue,
  89. [[defaults objectForKey:kVLCSettingStretchAudio] boolValue] ? kVLCSettingStretchAudioOnValue : kVLCSettingStretchAudioOffValue,
  90. [NSString stringWithFormat:@"--subsdec-encoding=%@",[defaults objectForKey:kVLCSettingTextEncoding]]];
  91. _mediaPlayer = [[VLCMediaPlayer alloc] initWithOptions:options];
  92. [_mediaPlayer setDelegate:self];
  93. [_mediaPlayer setDrawable:self.movieView];
  94. [self.navigationController setNavigationBarHidden:YES animated:YES];
  95. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  96. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
  97. if (!self.mediaItem && !self.url)
  98. return;
  99. if (self.mediaItem) {
  100. self.title = [self.mediaItem title];
  101. [_mediaPlayer setMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:self.mediaItem.url]]];
  102. self.mediaItem.unread = @(NO);
  103. } else {
  104. [_mediaPlayer setMedia:[VLCMedia mediaWithURL:self.url]];
  105. self.title = @"Network Stream";
  106. }
  107. self.positionSlider.value = 0.;
  108. [super viewWillAppear:animated];
  109. if (![self _isMediaSuitableForDevice]) {
  110. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DEVICE_TOOSLOW_TITLE", @"") message:[NSString stringWithFormat:NSLocalizedString(@"DEVICE_TOOSLOW", @""), [[UIDevice currentDevice] model], self.mediaItem.title] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_OPEN", @""), nil];
  111. [alert show];
  112. } else
  113. [self _playNewMedia];
  114. [self setControlsHidden:NO animated:YES];
  115. _viewAppeared = YES;
  116. }
  117. - (BOOL)_isMediaSuitableForDevice
  118. {
  119. if (!self.mediaItem)
  120. return YES;
  121. NSUInteger totalNumberOfPixels = [[[self.mediaItem videoTrack] valueForKey:@"width"] doubleValue] * [[[self.mediaItem videoTrack] valueForKey:@"height"] doubleValue];
  122. size_t size;
  123. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  124. char *answer = malloc(size);
  125. sysctlbyname("hw.machine", answer, &size, NULL, 0);
  126. NSString *currentMachine = @(answer);
  127. free(answer);
  128. if ([currentMachine hasPrefix:@"iPhone2"] || [currentMachine hasPrefix:@"iPhone3"] || [currentMachine hasPrefix:@"iPad1"] || [currentMachine hasPrefix:@"iPod3"] || [currentMachine hasPrefix:@"iPod4"]) {
  129. // iPhone 3GS, iPhone 4, first gen. iPad, 3rd and 4th generation iPod touch
  130. APLog(@"this is a cat one device");
  131. return (totalNumberOfPixels < 600000); // between 480p and 720p
  132. } else if ([currentMachine hasPrefix:@"iPhone4"] || [currentMachine hasPrefix:@"iPad3,1"] || [currentMachine hasPrefix:@"iPad3,2"] || [currentMachine hasPrefix:@"iPad3,3"] || [currentMachine hasPrefix:@"iPod4"] || [currentMachine hasPrefix:@"iPad2"] || [currentMachine hasPrefix:@"iPod5"]) {
  133. // iPhone 4S, iPad 2 and 3, iPod 4 and 5
  134. APLog(@"this is a cat two device");
  135. return (totalNumberOfPixels < 922000); // 720p
  136. } else {
  137. // iPhone 5, iPad 4
  138. APLog(@"this is a cat three device");
  139. return (totalNumberOfPixels < 2074000); // 1080p
  140. }
  141. return YES;
  142. }
  143. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  144. {
  145. if (buttonIndex == 1)
  146. [self _playNewMedia];
  147. else
  148. [self closePlayback:nil];
  149. }
  150. - (void)_playNewMedia
  151. {
  152. [_mediaPlayer play];
  153. if (self.mediaItem.lastPosition && [self.mediaItem.lastPosition floatValue] < .95)
  154. [_mediaPlayer setPosition:[self.mediaItem.lastPosition floatValue]];
  155. self.playbackSpeedSlider.value = [self _playbackSpeed];
  156. [self _updatePlaybackSpeedIndicator];
  157. _currentAspectRatioMask = 0;
  158. _mediaPlayer.videoAspectRatio = NULL;
  159. [self resetIdleTimer];
  160. }
  161. - (void)viewWillDisappear:(BOOL)animated
  162. {
  163. _viewAppeared = NO;
  164. if (_idleTimer) {
  165. [_idleTimer invalidate];
  166. _idleTimer = nil;
  167. }
  168. [self.navigationController setNavigationBarHidden:NO animated:YES];
  169. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
  170. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  171. [_mediaPlayer pause];
  172. [super viewWillDisappear:animated];
  173. if (self.mediaItem)
  174. self.mediaItem.lastPosition = @([_mediaPlayer position]);
  175. [_mediaPlayer stop];
  176. _mediaPlayer = nil; // save memory and some CPU time
  177. }
  178. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  179. {
  180. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  181. if (self)
  182. self.title = @"Video Playback";
  183. return self;
  184. }
  185. #pragma mark - remote events
  186. - (void)viewDidAppear:(BOOL)animated
  187. {
  188. [super viewDidAppear:animated];
  189. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  190. [self becomeFirstResponder];
  191. }
  192. - (void)viewDidDisappear:(BOOL)animated
  193. {
  194. [super viewDidDisappear:animated];
  195. [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
  196. [self resignFirstResponder];
  197. }
  198. - (BOOL)canBecomeFirstResponder
  199. {
  200. return YES;
  201. }
  202. - (void)remoteControlReceivedWithEvent:(UIEvent *)event
  203. {
  204. switch (event.subtype) {
  205. case UIEventSubtypeRemoteControlPlay:
  206. [_mediaPlayer play];
  207. break;
  208. case UIEventSubtypeRemoteControlPause:
  209. [_mediaPlayer pause];
  210. break;
  211. case UIEventSubtypeRemoteControlTogglePlayPause:
  212. [self playPause];
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. #pragma mark - controls visibility
  219. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  220. {
  221. if (touch.view != self.view)
  222. return NO;
  223. return YES;
  224. }
  225. - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated
  226. {
  227. _controlsHidden = hidden;
  228. CGFloat alpha = _controlsHidden? 0.0f: 1.0f;
  229. if (!_controlsHidden) {
  230. _controllerPanel.alpha = 0.0f;
  231. _controllerPanel.hidden = !_videoFiltersHidden;
  232. _toolbar.alpha = 0.0f;
  233. _toolbar.hidden = NO;
  234. _videoFilterView.alpha = 0.0f;
  235. _videoFilterView.hidden = _videoFiltersHidden;
  236. _playbackSpeedView.alpha = 0.0f;
  237. _playbackSpeedView.hidden = _playbackSpeedViewHidden;
  238. _playbackSpeedButton.alpha = 0.0f;
  239. _playbackSpeedButton.hidden = NO;
  240. _videoFilterButton.alpha = 0.0f;
  241. _videoFilterButton.hidden = NO;
  242. _aspectRatioButton.alpha = 0.0f;
  243. _aspectRatioButton.hidden = NO;
  244. }
  245. void (^animationBlock)() = ^() {
  246. _controllerPanel.alpha = alpha;
  247. _toolbar.alpha = alpha;
  248. _videoFilterView.alpha = alpha;
  249. _videoFilterButton.alpha = alpha;
  250. _playbackSpeedView.alpha = alpha;
  251. _playbackSpeedButton.alpha = alpha;
  252. _videoFilterButton.alpha = alpha;
  253. _aspectRatioButton.alpha = alpha;
  254. };
  255. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  256. if (_videoFiltersHidden) {
  257. _controllerPanel.hidden = _controlsHidden;
  258. _playbackSpeedButton.hidden = _controlsHidden;
  259. _videoFilterButton.hidden = _controlsHidden;
  260. _aspectRatioButton.hidden = _controlsHidden;
  261. } else {
  262. _controllerPanel.hidden = NO;
  263. _playbackSpeedButton.hidden = NO;
  264. _videoFilterButton.hidden = NO;
  265. _aspectRatioButton.hidden = NO;
  266. }
  267. _toolbar.hidden = _controlsHidden;
  268. _videoFilterView.hidden = _videoFiltersHidden;
  269. if (_controlsHidden)
  270. _playbackSpeedView.hidden = YES;
  271. else
  272. _playbackSpeedView.hidden = _playbackSpeedViewHidden;
  273. };
  274. UIStatusBarAnimation animationType = animated? UIStatusBarAnimationFade: UIStatusBarAnimationNone;
  275. NSTimeInterval animationDuration = animated? 0.3: 0.0;
  276. [[UIApplication sharedApplication] setStatusBarHidden:_viewAppeared ? _controlsHidden : NO withAnimation:animationType];
  277. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  278. }
  279. - (void)toggleControlsVisible
  280. {
  281. [self setControlsHidden:!_controlsHidden animated:YES];
  282. }
  283. - (void)resetIdleTimer
  284. {
  285. if (!_idleTimer)
  286. _idleTimer = [NSTimer scheduledTimerWithTimeInterval:2.
  287. target:self
  288. selector:@selector(idleTimerExceeded)
  289. userInfo:nil
  290. repeats:NO];
  291. else {
  292. if (fabs([_idleTimer.fireDate timeIntervalSinceNow]) < 2.)
  293. [_idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.]];
  294. }
  295. }
  296. - (void)idleTimerExceeded
  297. {
  298. _idleTimer = nil;
  299. if (!_controlsHidden)
  300. [self toggleControlsVisible];
  301. }
  302. - (UIResponder *)nextResponder
  303. {
  304. [self resetIdleTimer];
  305. return [super nextResponder];
  306. }
  307. #pragma mark - controls
  308. - (IBAction)closePlayback:(id)sender
  309. {
  310. [self setControlsHidden:NO animated:NO];
  311. [self.navigationController popViewControllerAnimated:YES];
  312. }
  313. - (IBAction)positionSliderAction:(UISlider *)sender
  314. {
  315. _mediaPlayer.position = sender.value;
  316. [self resetIdleTimer];
  317. }
  318. - (void)mediaPlayerTimeChanged:(NSNotification *)aNotification {
  319. self.positionSlider.value = [_mediaPlayer position];
  320. self.timeDisplay.text = [[_mediaPlayer remainingTime] stringValue];
  321. }
  322. - (void)mediaPlayerStateChanged:(NSNotification *)aNotification
  323. {
  324. VLCMediaPlayerState currentState = _mediaPlayer.state;
  325. if (currentState == VLCMediaPlayerStateError) {
  326. [self.statusLabel showStatusMessage:NSLocalizedString(@"PLAYBACK_FAILED", @"")];
  327. [self performSelector:@selector(closePlayback:) withObject:nil afterDelay:2.];
  328. }
  329. if (currentState == VLCMediaPlayerStateEnded || currentState == VLCMediaPlayerStateStopped)
  330. [self performSelector:@selector(closePlayback:) withObject:nil afterDelay:2.];
  331. UIImage *playPauseImage = [_mediaPlayer isPlaying]? [UIImage imageNamed:@"pause"] : [UIImage imageNamed:@"play"];
  332. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  333. if ([[_mediaPlayer audioTrackIndexes] count] > 2)
  334. self.audioSwitcherButton.hidden = NO;
  335. else
  336. self.audioSwitcherButton.hidden = YES;
  337. if ([[_mediaPlayer videoSubTitlesIndexes] count] > 1)
  338. self.subtitleSwitcherButton.hidden = NO;
  339. else
  340. self.subtitleSwitcherButton.hidden = YES;
  341. }
  342. - (IBAction)playPause
  343. {
  344. if ([_mediaPlayer isPlaying])
  345. [_mediaPlayer pause];
  346. else
  347. [_mediaPlayer play];
  348. }
  349. - (IBAction)forward:(id)sender
  350. {
  351. [_mediaPlayer mediumJumpForward];
  352. }
  353. - (IBAction)backward:(id)sender
  354. {
  355. [_mediaPlayer mediumJumpBackward];
  356. }
  357. - (IBAction)switchAudioTrack:(id)sender
  358. {
  359. _audiotrackActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"CHOOSE_AUDIO_TRACK", @"audio track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  360. NSArray *audioTracks = [_mediaPlayer audioTrackNames];
  361. NSUInteger count = [audioTracks count];
  362. for (NSUInteger i = 0; i < count; i++)
  363. [_audiotrackActionSheet addButtonWithTitle:audioTracks[i]];
  364. [_audiotrackActionSheet addButtonWithTitle:NSLocalizedString(@"BUTTON_CANCEL", @"cancel button")];
  365. [_audiotrackActionSheet setCancelButtonIndex:[_audiotrackActionSheet numberOfButtons] - 1];
  366. [_audiotrackActionSheet showInView:self.audioSwitcherButton];
  367. }
  368. - (IBAction)switchSubtitleTrack:(id)sender
  369. {
  370. NSArray *spuTracks = [_mediaPlayer videoSubTitlesNames];
  371. NSUInteger count = [spuTracks count];
  372. if (count <= 1)
  373. return;
  374. _subtitleActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"CHOOSE_SUBTITLE_TRACK", @"subtitle track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  375. for (NSUInteger i = 0; i < count; i++)
  376. [_subtitleActionSheet addButtonWithTitle:spuTracks[i]];
  377. [_subtitleActionSheet addButtonWithTitle:NSLocalizedString(@"BUTTON_CANCEL", @"cancel button")];
  378. [_subtitleActionSheet setCancelButtonIndex:[_subtitleActionSheet numberOfButtons] - 1];
  379. [_subtitleActionSheet showInView: self.subtitleSwitcherButton];
  380. }
  381. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  382. NSUInteger arrayIndex = 0;
  383. NSArray *indexArray;
  384. NSArray *namesArray;
  385. if (actionSheet == _subtitleActionSheet) {
  386. namesArray = _mediaPlayer.videoSubTitlesNames;
  387. arrayIndex = [namesArray indexOfObject:[actionSheet buttonTitleAtIndex:buttonIndex]];
  388. if (arrayIndex != NSNotFound) {
  389. indexArray = _mediaPlayer.videoSubTitlesIndexes;
  390. _mediaPlayer.currentVideoSubTitleIndex = [indexArray[arrayIndex] intValue];
  391. }
  392. } else if (actionSheet == _audiotrackActionSheet) {
  393. namesArray = _mediaPlayer.audioTrackNames;
  394. arrayIndex = [namesArray indexOfObject:[actionSheet buttonTitleAtIndex:buttonIndex]];
  395. if (arrayIndex != NSNotFound) {
  396. indexArray = _mediaPlayer.audioTrackIndexes;
  397. _mediaPlayer.currentAudioTrackIndex = [indexArray[arrayIndex] intValue];
  398. }
  399. }
  400. }
  401. #pragma mark - swipe gestures
  402. - (void)horizontalSwipePercentage:(CGFloat)percentage inView:(UIView *)view
  403. {
  404. if (percentage != 0.) {
  405. _mediaPlayer.position = _mediaPlayer.position + percentage;
  406. }
  407. }
  408. - (void)verticalSwipePercentage:(CGFloat)percentage inView:(UIView *)view half:(NSUInteger)half
  409. {
  410. if (percentage != 0.) {
  411. if (half > 0) {
  412. CGFloat currentValue = self.brightnessSlider.value;
  413. currentValue = currentValue + percentage;
  414. self.brightnessSlider.value = currentValue;
  415. if ([self hasExternalDisplay])
  416. _mediaPlayer.brightness = currentValue;
  417. else
  418. [[UIScreen mainScreen] setBrightness:currentValue / 2];
  419. } else
  420. NSLog(@"volume setting through swipe not implemented");//_mediaPlayer.audio.volume = percentage * 200;
  421. }
  422. }
  423. #pragma mark - Video Filter UI
  424. - (IBAction)videoFilterToggle:(id)sender
  425. {
  426. if (!_playbackSpeedViewHidden)
  427. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  428. self.videoFilterView.hidden = !_videoFiltersHidden;
  429. _videoFiltersHidden = self.videoFilterView.hidden;
  430. self.controllerPanel.hidden = !_videoFiltersHidden;
  431. }
  432. - (IBAction)videoFilterSliderAction:(id)sender
  433. {
  434. if (sender == self.hueSlider)
  435. _mediaPlayer.hue = (int)self.hueSlider.value;
  436. else if (sender == self.contrastSlider)
  437. _mediaPlayer.contrast = self.contrastSlider.value;
  438. else if (sender == self.brightnessSlider) {
  439. if ([self hasExternalDisplay])
  440. _mediaPlayer.brightness = self.brightnessSlider.value;
  441. else
  442. [[UIScreen mainScreen] setBrightness:(self.brightnessSlider.value / 2.)];
  443. } else if (sender == self.saturationSlider)
  444. _mediaPlayer.saturation = self.saturationSlider.value;
  445. else if (sender == self.gammaSlider)
  446. _mediaPlayer.gamma = self.gammaSlider.value;
  447. else if (sender == self.resetVideoFilterButton) {
  448. _mediaPlayer.hue = self.hueSlider.value = 0.;
  449. _mediaPlayer.contrast = self.contrastSlider.value = 1.;
  450. _mediaPlayer.brightness = self.brightnessSlider.value = 1.;
  451. _mediaPlayer.saturation = self.saturationSlider.value = 1.;
  452. _mediaPlayer.gamma = self.gammaSlider.value = 1.;
  453. } else
  454. APLog(@"unknown sender for videoFilterSliderAction");
  455. [self resetIdleTimer];
  456. }
  457. #pragma mark - playback view
  458. - (IBAction)playbackSpeedSliderAction:(UISlider *)sender
  459. {
  460. double speed = pow(2, sender.value / 17.);
  461. float rate = INPUT_RATE_DEFAULT / speed;
  462. if (_currentPlaybackRate != rate)
  463. [_mediaPlayer setRate:INPUT_RATE_DEFAULT / rate];
  464. _currentPlaybackRate = rate;
  465. [self _updatePlaybackSpeedIndicator];
  466. [self resetIdleTimer];
  467. }
  468. - (void)_updatePlaybackSpeedIndicator
  469. {
  470. float f_value = self.playbackSpeedSlider.value;
  471. double speed = pow(2, f_value / 17.);
  472. self.playbackSpeedIndicator.text = [NSString stringWithFormat:@"%.2fx", speed];
  473. }
  474. - (float)_playbackSpeed
  475. {
  476. float f_rate = _mediaPlayer.rate;
  477. double value = 17 * log(f_rate) / log(2.);
  478. float returnValue = (int) ((value > 0) ? value + .5 : value - .5);
  479. if (returnValue < -34.)
  480. returnValue = -34.;
  481. else if (returnValue > 34.)
  482. returnValue = 34.;
  483. _currentPlaybackRate = returnValue;
  484. return returnValue;
  485. }
  486. - (IBAction)videoDimensionAction:(id)sender
  487. {
  488. if (sender == self.playbackSpeedButton) {
  489. if (!_videoFiltersHidden)
  490. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  491. self.playbackSpeedView.hidden = !_playbackSpeedViewHidden;
  492. _playbackSpeedViewHidden = self.playbackSpeedView.hidden;
  493. } else if (sender == self.aspectRatioButton) {
  494. NSUInteger count = [_aspectRatios count];
  495. if (_currentAspectRatioMask + 1 > count - 1) {
  496. _mediaPlayer.videoAspectRatio = NULL;
  497. _mediaPlayer.videoCropGeometry = NULL;
  498. _currentAspectRatioMask = 0;
  499. [self.statusLabel showStatusMessage:[NSString stringWithFormat:NSLocalizedString(@"AR_CHANGED", @""), NSLocalizedString(@"DEFAULT", @"")]];
  500. } else {
  501. _currentAspectRatioMask++;
  502. if ([_aspectRatios[_currentAspectRatioMask] isEqualToString:@"FILL_TO_SCREEN"]) {
  503. UIScreen *screen;
  504. if (![self hasExternalDisplay])
  505. screen = [UIScreen mainScreen];
  506. else
  507. screen = [UIScreen screens][1];
  508. float f_ar = screen.bounds.size.width / screen.bounds.size.height;
  509. if (f_ar == (float)(640./1136.)) // iPhone 5 aka 16:9.01
  510. _mediaPlayer.videoCropGeometry = "16:9";
  511. else if (f_ar == (float)(2./3.)) // all other iPhones
  512. _mediaPlayer.videoCropGeometry = "16:10"; // libvlc doesn't support 2:3 crop
  513. else if (f_ar == .75) // all iPads
  514. _mediaPlayer.videoCropGeometry = "4:3";
  515. else if (f_ar == .5625) // AirPlay
  516. _mediaPlayer.videoCropGeometry = "16:9";
  517. else
  518. APLog(@"unknown screen format %f, can't crop", f_ar);
  519. [self.statusLabel showStatusMessage:NSLocalizedString(@"FILL_TO_SCREEN", @"")];
  520. return;
  521. }
  522. _mediaPlayer.videoCropGeometry = NULL;
  523. _mediaPlayer.videoAspectRatio = (char *)[_aspectRatios[_currentAspectRatioMask] UTF8String];
  524. [self.statusLabel showStatusMessage:[NSString stringWithFormat:NSLocalizedString(@"AR_CHANGED", @""), _aspectRatios[_currentAspectRatioMask]]];
  525. }
  526. }
  527. }
  528. #pragma mark - background interaction
  529. - (void)applicationWillResignActive:(NSNotification *)aNotification
  530. {
  531. if (self.mediaItem)
  532. self.mediaItem.lastPosition = @([_mediaPlayer position]);
  533. if (![[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingContinueAudioInBackgroundKey] boolValue]) {
  534. [_mediaPlayer pause];
  535. _shouldResumePlaying = YES;
  536. } else
  537. _mediaPlayer.currentVideoTrackIndex = 0;
  538. }
  539. - (void)applicationDidEnterBackground:(NSNotification *)notification
  540. {
  541. _shouldResumePlaying = NO;
  542. }
  543. - (void)applicationDidBecomeActive:(NSNotification *)notification
  544. {
  545. if (_shouldResumePlaying) {
  546. _shouldResumePlaying = NO;
  547. [_mediaPlayer play];
  548. } else
  549. _mediaPlayer.currentVideoTrackIndex = 1;
  550. }
  551. #pragma mark - autorotation
  552. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  553. return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  554. || toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
  555. }
  556. #pragma mark - External Display
  557. - (BOOL)hasExternalDisplay
  558. {
  559. return ([[UIScreen screens] count] > 1);
  560. }
  561. - (void)showOnExternalDisplay
  562. {
  563. UIScreen *screen = [UIScreen screens][1];
  564. screen.overscanCompensation = UIScreenOverscanCompensationInsetApplicationFrame;
  565. self.externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
  566. UIViewController *controller = [[VLCExternalDisplayController alloc] init];
  567. self.externalWindow.rootViewController = controller;
  568. [controller.view addSubview:_movieView];
  569. controller.view.frame = screen.bounds;
  570. _movieView.frame = screen.bounds;
  571. self.playingExternallyView.hidden = NO;
  572. self.externalWindow.screen = screen;
  573. self.externalWindow.hidden = NO;
  574. }
  575. - (void)hideFromExternalDisplay
  576. {
  577. [self.view addSubview:_movieView];
  578. [self.view sendSubviewToBack:_movieView];
  579. _movieView.frame = self.view.frame;
  580. self.playingExternallyView.hidden = YES;
  581. self.externalWindow.hidden = YES;
  582. self.externalWindow = nil;
  583. }
  584. - (void)handleExternalScreenDidConnect:(NSNotification *)notification
  585. {
  586. [self showOnExternalDisplay];
  587. }
  588. - (void)handleExternalScreenDidDisconnect:(NSNotification *)notification
  589. {
  590. [self hideFromExternalDisplay];
  591. }
  592. @end