VLCMovieViewController.m 25 KB

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