VLCMovieViewController.m 25 KB

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