VLCMovieViewController.m 25 KB

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