VLCMovieViewController.m 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCMovieViewController.h"
  11. #import "VLCExternalDisplayController.h"
  12. #import <AVFoundation/AVFoundation.h>
  13. #import <CommonCrypto/CommonDigest.h>
  14. #import "UIDevice+SpeedCategory.h"
  15. #import "VLCBugreporter.h"
  16. #import <MediaPlayer/MediaPlayer.h>
  17. #define INPUT_RATE_DEFAULT 1000.
  18. @interface VLCMovieViewController () <UIGestureRecognizerDelegate, AVAudioSessionDelegate>
  19. {
  20. VLCMediaPlayer *_mediaPlayer;
  21. BOOL _controlsHidden;
  22. BOOL _videoFiltersHidden;
  23. BOOL _playbackSpeedViewHidden;
  24. UIActionSheet *_subtitleActionSheet;
  25. UIActionSheet *_audiotrackActionSheet;
  26. float _currentPlaybackRate;
  27. NSArray *_aspectRatios;
  28. NSUInteger _currentAspectRatioMask;
  29. NSTimer *_idleTimer;
  30. BOOL _shouldResumePlaying;
  31. BOOL _viewAppeared;
  32. BOOL _displayRemainingTime;
  33. BOOL _positionSet;
  34. BOOL _playerIsSetup;
  35. }
  36. @property (nonatomic, strong) UIPopoverController *masterPopoverController;
  37. @property (nonatomic, strong) UIWindow *externalWindow;
  38. @end
  39. @implementation VLCMovieViewController
  40. + (void)initialize
  41. {
  42. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  43. NSDictionary *appDefaults = @{kVLCShowRemainingTime : @(YES)};
  44. [defaults registerDefaults:appDefaults];
  45. }
  46. - (void)dealloc
  47. {
  48. [[NSNotificationCenter defaultCenter] removeObserver:self];
  49. }
  50. #pragma mark - Managing the media item
  51. - (void)setMediaItem:(id)newMediaItem
  52. {
  53. if (_mediaItem != newMediaItem) {
  54. [self _stopPlayback];
  55. _mediaItem = newMediaItem;
  56. if (_viewAppeared)
  57. [self _startPlayback];
  58. }
  59. if (self.masterPopoverController != nil)
  60. [self.masterPopoverController dismissPopoverAnimated:YES];
  61. }
  62. - (void)setUrl:(NSURL *)url
  63. {
  64. if (_url != url) {
  65. [self _stopPlayback];
  66. _url = url;
  67. if (_viewAppeared)
  68. [self _startPlayback];
  69. }
  70. }
  71. - (void)viewDidLoad
  72. {
  73. [super viewDidLoad];
  74. self.wantsFullScreenLayout = YES;
  75. self.videoFilterView.hidden = YES;
  76. _videoFiltersHidden = YES;
  77. _hueLabel.text = NSLocalizedString(@"VFILTER_HUE", @"");
  78. _contrastLabel.text = NSLocalizedString(@"VFILTER_CONTRAST", @"");
  79. _brightnessLabel.text = NSLocalizedString(@"VFILTER_BRIGHTNESS", @"");
  80. _saturationLabel.text = NSLocalizedString(@"VFILTER_SATURATION", @"");
  81. _gammaLabel.text = NSLocalizedString(@"VFILTER_GAMMA", @"");
  82. _playbackSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SPEED", @"");
  83. _scrubHelpLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HELP", @"");
  84. self.playbackSpeedView.hidden = YES;
  85. _playbackSpeedViewHidden = YES;
  86. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  87. [center addObserver:self selector:@selector(handleExternalScreenDidConnect:)
  88. name:UIScreenDidConnectNotification object:nil];
  89. [center addObserver:self selector:@selector(handleExternalScreenDidDisconnect:)
  90. name:UIScreenDidDisconnectNotification object:nil];
  91. [center addObserver:self selector:@selector(applicationWillResignActive:)
  92. name:UIApplicationWillResignActiveNotification object:nil];
  93. [center addObserver:self selector:@selector(applicationDidBecomeActive:)
  94. name:UIApplicationDidBecomeActiveNotification object:nil];
  95. [center addObserver:self selector:@selector(applicationDidEnterBackground:)
  96. name:UIApplicationDidEnterBackgroundNotification object:nil];
  97. _playingExternallyTitle.text = NSLocalizedString(@"PLAYING_EXTERNALLY_TITLE", @"");
  98. _playingExternallyDescription.text = NSLocalizedString(@"PLAYING_EXTERNALLY_DESC", @"");
  99. if ([self hasExternalDisplay])
  100. [self showOnExternalDisplay];
  101. _movieView.userInteractionEnabled = NO;
  102. UITapGestureRecognizer *tapOnVideoRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleControlsVisible)];
  103. tapOnVideoRecognizer.delegate = self;
  104. [self.view addGestureRecognizer:tapOnVideoRecognizer];
  105. _displayRemainingTime = [[[NSUserDefaults standardUserDefaults] objectForKey:kVLCShowRemainingTime] boolValue];
  106. UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
  107. pinchRecognizer.delegate = self;
  108. [self.view addGestureRecognizer:pinchRecognizer];
  109. #if 0 // FIXME: trac #8742
  110. UISwipeGestureRecognizer *leftSwipeRecognizer = [[VLCHorizontalSwipeGestureRecognizer alloc] initWithTarget:self action:nil];
  111. leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
  112. leftSwipeRecognizer.delegate = self;
  113. [self.view addGestureRecognizer:leftSwipeRecognizer];
  114. UISwipeGestureRecognizer *rightSwipeRecognizer = [[VLCHorizontalSwipeGestureRecognizer alloc] initWithTarget:self action:nil];
  115. rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
  116. rightSwipeRecognizer.delegate = self;
  117. [self.view addGestureRecognizer:rightSwipeRecognizer];
  118. UISwipeGestureRecognizer *upSwipeRecognizer = [[VLCVerticalSwipeGestureRecognizer alloc] initWithTarget:self action:nil];
  119. upSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
  120. upSwipeRecognizer.delegate = self;
  121. [self.view addGestureRecognizer:upSwipeRecognizer];
  122. UISwipeGestureRecognizer *downSwipeRecognizer = [[VLCVerticalSwipeGestureRecognizer alloc] initWithTarget:self action:nil];
  123. downSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
  124. downSwipeRecognizer.delegate = self;
  125. [self.view addGestureRecognizer:downSwipeRecognizer];
  126. #endif
  127. _aspectRatios = @[@"DEFAULT", @"4:3", @"16:9", @"16:10", @"2.21:1", @"FILL_TO_SCREEN"];
  128. [self.aspectRatioButton setBackgroundImage:[UIImage imageNamed:@"ratioButton"] forState:UIControlStateNormal];
  129. [self.aspectRatioButton setBackgroundImage:[UIImage imageNamed:@"ratioButtonHighlight"] forState:UIControlStateHighlighted];
  130. [self.aspectRatioButton setImage:[UIImage imageNamed:@"ratioIcon"] forState:UIControlStateNormal];
  131. [self.toolbar setBackgroundImage:[UIImage imageNamed:@"seekbarBg"] forBarMetrics:UIBarMetricsDefault];
  132. [self.backButton setBackgroundImage:[UIImage imageNamed:@"playbackDoneButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  133. [self.backButton setBackgroundImage:[UIImage imageNamed:@"playbackDoneButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  134. /* this looks a bit weird, but we need to support iOS 5 and should show the same appearance */
  135. UISlider *volumeSlider = nil;
  136. for (id aView in self.volumeView.subviews){
  137. if ([[[aView class] description] isEqualToString:@"MPVolumeSlider"]){
  138. volumeSlider = (UISlider *)aView;
  139. break;
  140. }
  141. }
  142. [volumeSlider setMinimumTrackImage:[[UIImage imageNamed:@"sliderminiValue"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 0)] forState:UIControlStateNormal];
  143. [volumeSlider setMaximumTrackImage:[[UIImage imageNamed:@"slidermaxValue"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 4)] forState:UIControlStateNormal];
  144. [volumeSlider setThumbImage:[UIImage imageNamed:@"volumeballslider"] forState:UIControlStateNormal];
  145. [volumeSlider addTarget:self
  146. action:@selector(volumeSliderAction:)
  147. forControlEvents:UIControlEventValueChanged];
  148. [[AVAudioSession sharedInstance] setDelegate:self];
  149. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  150. self.positionSlider.scrubbingSpeedChangePositions = @[@(0.), @(100.), @(200.), @(300)];
  151. _playerIsSetup = NO;
  152. [self.movieView setAccessibilityLabel:NSLocalizedString(@"VO_VIDEOPLAYER_TITLE", @"")];
  153. [self.movieView setAccessibilityHint:NSLocalizedString(@"VO_VIDEOPLAYER_DOUBLETAP", @"")];
  154. }
  155. - (BOOL)_blobCheck
  156. {
  157. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  158. NSString *directoryPath = searchPaths[0];
  159. if (![[NSFileManager defaultManager] fileExistsAtPath:[directoryPath stringByAppendingPathComponent:@"blob.bin"]])
  160. return NO;
  161. NSData *data = [NSData dataWithContentsOfFile:[directoryPath stringByAppendingPathComponent:@"blob.bin"]];
  162. uint8_t digest[CC_SHA1_DIGEST_LENGTH];
  163. CC_SHA1(data.bytes, data.length, digest);
  164. NSMutableString *hash = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
  165. for (unsigned int u = 0; u < CC_SHA1_DIGEST_LENGTH; u++)
  166. [hash appendFormat:@"%02x", digest[u]];
  167. if ([hash isEqualToString:kBlobHash])
  168. return YES;
  169. else
  170. return NO;
  171. }
  172. - (void)viewWillAppear:(BOOL)animated
  173. {
  174. [super viewWillAppear:animated];
  175. [self.navigationController setNavigationBarHidden:YES animated:YES];
  176. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  177. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
  178. [self _startPlayback];
  179. [self setControlsHidden:NO animated:YES];
  180. _viewAppeared = YES;
  181. }
  182. - (void)_startPlayback
  183. {
  184. if (_playerIsSetup)
  185. return;
  186. _mediaPlayer = [[VLCMediaPlayer alloc] init];
  187. [_mediaPlayer setDelegate:self];
  188. [_mediaPlayer setDrawable:self.movieView];
  189. if (!self.mediaItem && !self.url) {
  190. [self _stopPlayback];
  191. return;
  192. }
  193. VLCMedia *media;
  194. if (self.mediaItem) {
  195. self.title = [self.mediaItem title];
  196. media = [VLCMedia mediaWithURL:[NSURL URLWithString:self.mediaItem.url]];
  197. self.mediaItem.unread = @(NO);
  198. } else {
  199. media = [VLCMedia mediaWithURL:self.url];
  200. self.title = @"Network Stream";
  201. }
  202. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  203. [media addOptions:
  204. @{kVLCSettingStretchAudio :
  205. [[defaults objectForKey:kVLCSettingStretchAudio] boolValue] ? kVLCSettingStretchAudioOnValue : kVLCSettingStretchAudioOffValue, kVLCSettingTextEncoding : [defaults objectForKey:kVLCSettingTextEncoding], kVLCSettingSkipLoopFilter : [defaults objectForKey:kVLCSettingSkipLoopFilter]}];
  206. [NSTimeZone resetSystemTimeZone];
  207. NSString *tzName = [[NSTimeZone systemTimeZone] name];
  208. NSArray *tzNames = @[@"America/Adak", @"America/Anchorage", @"America/Boise", @"America/Chicago", @"America/Denver", @"America/Detroit", @"America/Indiana/Indianapolis", @"America/Indiana/Knox", @"America/Indiana/Marengo", @"America/Indiana/Petersburg", @"America/Indiana/Tell_City", @"America/Indiana/Vevay", @"America/Indiana/Vincennes", @"America/Indiana/Winamac", @"America/Juneau", @"America/Kentucky/Louisville", @"America/Kentucky/Monticello", @"America/Los_Angeles", @"America/Menominee", @"America/Metlakatla", @"America/New_York", @"America/Nome", @"America/North_Dakota/Beulah", @"America/North_Dakota/Center", @"America/North_Dakota/New_Salem", @"America/Phoenix", @"America/Puerto_Rico", @"America/Shiprock", @"America/Sitka", @"America/St_Thomas", @"America/Thule", @"America/Yakutat", @"Pacific/Guam", @"Pacific/Honolulu", @"Pacific/Johnston", @"Pacific/Kwajalein", @"Pacific/Midway", @"Pacific/Pago_Pago", @"Pacific/Saipan", @"Pacific/Wake"];
  209. if ([tzNames containsObject:tzName] || [[tzName stringByDeletingLastPathComponent] isEqualToString:@"US"]) {
  210. NSArray *tracksInfo = media.tracksInformation;
  211. for (NSUInteger x = 0; x < tracksInfo.count; x++) {
  212. if ([[tracksInfo[x] objectForKey:VLCMediaTracksInformationType] isEqualToString:VLCMediaTracksInformationTypeAudio])
  213. {
  214. NSInteger fourcc = [[tracksInfo[x] objectForKey:VLCMediaTracksInformationCodec] integerValue];
  215. switch (fourcc) {
  216. case 540161377:
  217. case 1647457633:
  218. case 858612577:
  219. case 862151027:
  220. case 2126701:
  221. case 544437348:
  222. case 542331972:
  223. case 1651733604:
  224. case 1668510820:
  225. case 1702065252:
  226. case 1752396900:
  227. case 1819505764:
  228. case 18903917:
  229. case 862151013:
  230. {
  231. if (![self _blobCheck]) {
  232. [media addOptions:@{@"no-audio" : [NSNull null]}];
  233. APLog(@"audio playback disabled because an unsupported codec was found");
  234. }
  235. break;
  236. }
  237. default:
  238. break;
  239. }
  240. }
  241. }
  242. }
  243. [_mediaPlayer setMedia:media];
  244. self.positionSlider.value = 0.;
  245. [self.timeDisplay setTitle:@"" forState:UIControlStateNormal];
  246. if (![self _isMediaSuitableForDevice]) {
  247. 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];
  248. [alert show];
  249. } else
  250. [self _playNewMedia];
  251. if (![self hasExternalDisplay])
  252. self.brightnessSlider.value = [UIScreen mainScreen].brightness * 2.;
  253. }
  254. - (BOOL)_isMediaSuitableForDevice
  255. {
  256. if (!self.mediaItem)
  257. return YES;
  258. NSUInteger totalNumberOfPixels = [[[self.mediaItem videoTrack] valueForKey:@"width"] doubleValue] * [[[self.mediaItem videoTrack] valueForKey:@"height"] doubleValue];
  259. NSInteger speedCategory = [[UIDevice currentDevice] speedCategory];
  260. if (speedCategory == 1) {
  261. // iPhone 3GS, iPhone 4, first gen. iPad, 3rd and 4th generation iPod touch
  262. return (totalNumberOfPixels < 600000); // between 480p and 720p
  263. } else if (speedCategory == 2) {
  264. // iPhone 4S, iPad 2 and 3, iPod 4 and 5
  265. return (totalNumberOfPixels < 922000); // 720p
  266. } else if (speedCategory == 3) {
  267. // iPhone 5, iPad 4
  268. return (totalNumberOfPixels < 2074000); // 1080p
  269. }
  270. return YES;
  271. }
  272. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  273. {
  274. if (buttonIndex == 1)
  275. [self _playNewMedia];
  276. else {
  277. [self _stopPlayback];
  278. [self closePlayback:nil];
  279. }
  280. }
  281. - (void)_playNewMedia
  282. {
  283. NSNumber *playbackPositionInTime = @(0);
  284. if (self.mediaItem.lastPosition && [self.mediaItem.lastPosition floatValue] < .95) {
  285. if (self.mediaItem.duration.intValue != 0)
  286. playbackPositionInTime = @(self.mediaItem.lastPosition.floatValue * (self.mediaItem.duration.intValue / 1000.));
  287. }
  288. [_mediaPlayer.media addOptions:@{@"start-time": playbackPositionInTime}];
  289. APLog(@"set starttime to %i", playbackPositionInTime.intValue);
  290. [_mediaPlayer play];
  291. if (self.mediaItem) {
  292. if (self.mediaItem.lastAudioTrack.intValue > 0)
  293. _mediaPlayer.currentAudioTrackIndex = self.mediaItem.lastAudioTrack.intValue;
  294. if (self.mediaItem.lastSubtitleTrack.intValue > 0)
  295. _mediaPlayer.currentVideoSubTitleIndex = self.mediaItem.lastSubtitleTrack.intValue;
  296. }
  297. self.playbackSpeedSlider.value = [self _playbackSpeed];
  298. [self _updatePlaybackSpeedIndicator];
  299. [self performSelectorInBackground:@selector(_updateExportedPlaybackInformation) withObject:nil];
  300. _currentAspectRatioMask = 0;
  301. _mediaPlayer.videoAspectRatio = NULL;
  302. [self _resetIdleTimer];
  303. _playerIsSetup = YES;
  304. }
  305. - (void)viewWillDisappear:(BOOL)animated
  306. {
  307. [self _stopPlayback];
  308. _viewAppeared = NO;
  309. if (_idleTimer) {
  310. [_idleTimer invalidate];
  311. _idleTimer = nil;
  312. }
  313. [self.navigationController setNavigationBarHidden:NO animated:YES];
  314. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
  315. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  316. [super viewWillDisappear:animated];
  317. // hide filter UI for next run
  318. if (!_videoFiltersHidden)
  319. _videoFiltersHidden = YES;
  320. if (!_playbackSpeedViewHidden)
  321. _playbackSpeedViewHidden = YES;
  322. }
  323. - (void)_stopPlayback
  324. {
  325. if (_mediaPlayer) {
  326. [_mediaPlayer pause];
  327. [self _saveCurrentState];
  328. [_mediaPlayer stop];
  329. _mediaPlayer = nil; // save memory and some CPU time
  330. }
  331. if (_mediaItem)
  332. _mediaItem = nil;
  333. _playerIsSetup = NO;
  334. }
  335. - (void)_saveCurrentState
  336. {
  337. if (self.mediaItem) {
  338. self.mediaItem.lastPosition = @([_mediaPlayer position]);
  339. self.mediaItem.lastAudioTrack = @(_mediaPlayer.currentAudioTrackIndex);
  340. self.mediaItem.lastSubtitleTrack = @(_mediaPlayer.currentVideoSubTitleIndex);
  341. }
  342. }
  343. #pragma mark - remote events
  344. - (void)viewDidAppear:(BOOL)animated
  345. {
  346. [super viewDidAppear:animated];
  347. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  348. [self becomeFirstResponder];
  349. }
  350. - (void)viewDidDisappear:(BOOL)animated
  351. {
  352. [super viewDidDisappear:animated];
  353. [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
  354. [self resignFirstResponder];
  355. [[NSUserDefaults standardUserDefaults] setBool:_displayRemainingTime forKey:kVLCShowRemainingTime];
  356. }
  357. - (BOOL)canBecomeFirstResponder
  358. {
  359. return YES;
  360. }
  361. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  362. {
  363. if (motion == UIEventSubtypeMotionShake)
  364. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  365. }
  366. - (void)remoteControlReceivedWithEvent:(UIEvent *)event
  367. {
  368. switch (event.subtype) {
  369. case UIEventSubtypeRemoteControlPlay:
  370. [_mediaPlayer play];
  371. break;
  372. case UIEventSubtypeRemoteControlPause:
  373. [_mediaPlayer pause];
  374. break;
  375. case UIEventSubtypeRemoteControlTogglePlayPause:
  376. [self playPause];
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. #pragma mark - controls visibility
  383. - (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer
  384. {
  385. if (recognizer.velocity < 0.)
  386. [self closePlayback:nil];
  387. }
  388. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  389. {
  390. if (touch.view != self.view)
  391. return NO;
  392. return YES;
  393. }
  394. - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated
  395. {
  396. _controlsHidden = hidden;
  397. CGFloat alpha = _controlsHidden? 0.0f: 1.0f;
  398. if (!_controlsHidden) {
  399. _controllerPanel.alpha = 0.0f;
  400. _controllerPanel.hidden = !_videoFiltersHidden;
  401. _toolbar.alpha = 0.0f;
  402. _toolbar.hidden = NO;
  403. _videoFilterView.alpha = 0.0f;
  404. _videoFilterView.hidden = _videoFiltersHidden;
  405. _playbackSpeedView.alpha = 0.0f;
  406. _playbackSpeedView.hidden = _playbackSpeedViewHidden;
  407. }
  408. void (^animationBlock)() = ^() {
  409. _controllerPanel.alpha = alpha;
  410. _toolbar.alpha = alpha;
  411. _videoFilterView.alpha = alpha;
  412. _playbackSpeedView.alpha = alpha;
  413. };
  414. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  415. if (_videoFiltersHidden)
  416. _controllerPanel.hidden = _controlsHidden;
  417. else
  418. _controllerPanel.hidden = NO;
  419. _toolbar.hidden = _controlsHidden;
  420. _videoFilterView.hidden = _videoFiltersHidden;
  421. _playbackSpeedView.hidden = _playbackSpeedViewHidden;
  422. };
  423. UIStatusBarAnimation animationType = animated? UIStatusBarAnimationFade: UIStatusBarAnimationNone;
  424. NSTimeInterval animationDuration = animated? 0.3: 0.0;
  425. [[UIApplication sharedApplication] setStatusBarHidden:_viewAppeared ? _controlsHidden : NO withAnimation:animationType];
  426. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  427. _volumeView.hidden = _controllerPanel.hidden;
  428. }
  429. - (void)toggleControlsVisible
  430. {
  431. if (_controlsHidden && !_videoFiltersHidden)
  432. _videoFiltersHidden = YES;
  433. [self setControlsHidden:!_controlsHidden animated:YES];
  434. }
  435. - (void)_resetIdleTimer
  436. {
  437. if (!_idleTimer)
  438. _idleTimer = [NSTimer scheduledTimerWithTimeInterval:4.
  439. target:self
  440. selector:@selector(idleTimerExceeded)
  441. userInfo:nil
  442. repeats:NO];
  443. else {
  444. if (fabs([_idleTimer.fireDate timeIntervalSinceNow]) < 4.)
  445. [_idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:4.]];
  446. }
  447. }
  448. - (void)idleTimerExceeded
  449. {
  450. _idleTimer = nil;
  451. if (!_controlsHidden)
  452. [self toggleControlsVisible];
  453. if (!_videoFiltersHidden)
  454. _videoFiltersHidden = YES;
  455. if (!_playbackSpeedViewHidden)
  456. _playbackSpeedViewHidden = YES;
  457. if (self.scrubIndicatorView.hidden == NO)
  458. self.scrubIndicatorView.hidden = YES;
  459. }
  460. - (UIResponder *)nextResponder
  461. {
  462. [self _resetIdleTimer];
  463. return [super nextResponder];
  464. }
  465. #pragma mark - controls
  466. - (IBAction)closePlayback:(id)sender
  467. {
  468. [self setControlsHidden:NO animated:NO];
  469. [self.navigationController popViewControllerAnimated:YES];
  470. }
  471. - (IBAction)positionSliderAction:(UISlider *)sender
  472. {
  473. /* we need to limit the number of events sent by the slider, since otherwise, the user
  474. * wouldn't see the I-frames when seeking on current mobile devices. This isn't a problem
  475. * within the Simulator, but especially on older ARMv7 devices, it's clearly noticeable. */
  476. [self performSelector:@selector(_setPositionForReal) withObject:nil afterDelay:0.3];
  477. VLCTime *newPosition = [VLCTime timeWithInt:(int)(_positionSlider.value * self.mediaItem.duration.intValue)];
  478. [self.timeDisplay setTitle:newPosition.stringValue forState:UIControlStateNormal];
  479. _positionSet = NO;
  480. [self _resetIdleTimer];
  481. }
  482. - (void)_setPositionForReal
  483. {
  484. if (!_positionSet) {
  485. _mediaPlayer.position = _positionSlider.value;
  486. _positionSet = YES;
  487. }
  488. }
  489. - (IBAction)positionSliderTouchDown:(id)sender
  490. {
  491. [self _updateScrubLabel];
  492. self.scrubIndicatorView.hidden = NO;
  493. }
  494. - (IBAction)positionSliderTouchUp:(id)sender
  495. {
  496. self.scrubIndicatorView.hidden = YES;
  497. }
  498. - (void)_updateScrubLabel
  499. {
  500. float speed = self.positionSlider.scrubbingSpeed;
  501. if (speed == 1.)
  502. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HIGH", @"");
  503. else if (speed == .5)
  504. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HALF", @"");
  505. else if (speed == .25)
  506. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_QUARTER", @"");
  507. else
  508. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_FINE", @"");
  509. [self _resetIdleTimer];
  510. }
  511. - (IBAction)positionSliderDrag:(id)sender
  512. {
  513. [self _updateScrubLabel];
  514. }
  515. - (IBAction)volumeSliderAction:(id)sender
  516. {
  517. [self _resetIdleTimer];
  518. }
  519. - (void)mediaPlayerTimeChanged:(NSNotification *)aNotification {
  520. self.positionSlider.value = [_mediaPlayer position];
  521. if (_displayRemainingTime)
  522. [self.timeDisplay setTitle:[[_mediaPlayer remainingTime] stringValue] forState:UIControlStateNormal];
  523. else
  524. [self.timeDisplay setTitle:[[_mediaPlayer time] stringValue] forState:UIControlStateNormal];
  525. }
  526. - (void)mediaPlayerStateChanged:(NSNotification *)aNotification
  527. {
  528. VLCMediaPlayerState currentState = _mediaPlayer.state;
  529. if (currentState == VLCMediaPlayerStateError) {
  530. [self.statusLabel showStatusMessage:NSLocalizedString(@"PLAYBACK_FAILED", @"")];
  531. [self performSelector:@selector(closePlayback:) withObject:nil afterDelay:2.];
  532. }
  533. if (currentState == VLCMediaPlayerStateEnded || currentState == VLCMediaPlayerStateStopped)
  534. [self performSelector:@selector(closePlayback:) withObject:nil afterDelay:2.];
  535. UIImage *playPauseImage = [_mediaPlayer isPlaying]? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
  536. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  537. if ([[_mediaPlayer audioTrackIndexes] count] > 2)
  538. self.audioSwitcherButton.hidden = NO;
  539. else
  540. self.audioSwitcherButton.hidden = YES;
  541. if ([[_mediaPlayer videoSubTitlesIndexes] count] > 1)
  542. self.subtitleSwitcherButton.hidden = NO;
  543. else
  544. self.subtitleSwitcherButton.hidden = YES;
  545. }
  546. - (IBAction)playPause
  547. {
  548. if ([_mediaPlayer isPlaying])
  549. [_mediaPlayer pause];
  550. else
  551. [_mediaPlayer play];
  552. }
  553. - (IBAction)forward:(id)sender
  554. {
  555. [_mediaPlayer mediumJumpForward];
  556. }
  557. - (IBAction)backward:(id)sender
  558. {
  559. [_mediaPlayer mediumJumpBackward];
  560. }
  561. - (IBAction)switchAudioTrack:(id)sender
  562. {
  563. _audiotrackActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"CHOOSE_AUDIO_TRACK", @"audio track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  564. NSArray *audioTracks = [_mediaPlayer audioTrackNames];
  565. NSArray *audioTrackIndexes = [_mediaPlayer audioTrackIndexes];
  566. NSUInteger count = [audioTracks count];
  567. for (NSUInteger i = 0; i < count; i++) {
  568. NSString *indexIndicator = ([audioTrackIndexes[i] intValue] == [_mediaPlayer currentAudioTrackIndex])? @"\u2713": @"";
  569. NSString *buttonTitle = [NSString stringWithFormat:@"%@ %@", indexIndicator, audioTracks[i]];
  570. [_audiotrackActionSheet addButtonWithTitle:buttonTitle];
  571. }
  572. [_audiotrackActionSheet addButtonWithTitle:NSLocalizedString(@"BUTTON_CANCEL", @"cancel button")];
  573. [_audiotrackActionSheet setCancelButtonIndex:[_audiotrackActionSheet numberOfButtons] - 1];
  574. [_audiotrackActionSheet showInView:self.audioSwitcherButton];
  575. }
  576. - (IBAction)switchSubtitleTrack:(id)sender
  577. {
  578. NSArray *spuTracks = [_mediaPlayer videoSubTitlesNames];
  579. NSArray *spuTrackIndexes = [_mediaPlayer videoSubTitlesIndexes];
  580. NSUInteger count = [spuTracks count];
  581. if (count <= 1)
  582. return;
  583. _subtitleActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"CHOOSE_SUBTITLE_TRACK", @"subtitle track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  584. for (NSUInteger i = 0; i < count; i++) {
  585. NSString *indexIndicator = ([spuTrackIndexes[i] intValue] == [_mediaPlayer currentVideoSubTitleIndex])? @"\u2713": @"";
  586. NSString *buttonTitle = [NSString stringWithFormat:@"%@ %@", indexIndicator, spuTracks[i]];
  587. [_subtitleActionSheet addButtonWithTitle:buttonTitle];
  588. }
  589. [_subtitleActionSheet addButtonWithTitle:NSLocalizedString(@"BUTTON_CANCEL", @"cancel button")];
  590. [_subtitleActionSheet setCancelButtonIndex:[_subtitleActionSheet numberOfButtons] - 1];
  591. [_subtitleActionSheet showInView: self.subtitleSwitcherButton];
  592. }
  593. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  594. if (buttonIndex == [actionSheet cancelButtonIndex])
  595. return;
  596. NSArray *indexArray;
  597. if (actionSheet == _subtitleActionSheet) {
  598. indexArray = _mediaPlayer.videoSubTitlesIndexes;
  599. if (buttonIndex <= indexArray.count) {
  600. _mediaPlayer.currentVideoSubTitleIndex = [indexArray[buttonIndex] intValue];
  601. }
  602. } else if (actionSheet == _audiotrackActionSheet) {
  603. indexArray = _mediaPlayer.audioTrackIndexes;
  604. if (buttonIndex <= indexArray.count) {
  605. _mediaPlayer.currentAudioTrackIndex = [indexArray[buttonIndex] intValue];
  606. }
  607. }
  608. }
  609. - (IBAction)toggleTimeDisplay:(id)sender
  610. {
  611. _displayRemainingTime = !_displayRemainingTime;
  612. [self _resetIdleTimer];
  613. }
  614. #pragma mark - swipe gestures
  615. - (void)horizontalSwipePercentage:(CGFloat)percentage inView:(UIView *)view
  616. {
  617. if (percentage != 0.) {
  618. _mediaPlayer.position = _mediaPlayer.position + percentage;
  619. }
  620. }
  621. - (void)verticalSwipePercentage:(CGFloat)percentage inView:(UIView *)view half:(NSUInteger)half
  622. {
  623. if (percentage != 0.) {
  624. if (half > 0) {
  625. CGFloat currentValue = self.brightnessSlider.value;
  626. currentValue = currentValue + percentage;
  627. self.brightnessSlider.value = currentValue;
  628. if ([self hasExternalDisplay])
  629. _mediaPlayer.brightness = currentValue;
  630. else
  631. [[UIScreen mainScreen] setBrightness:currentValue / 2];
  632. } else
  633. NSLog(@"volume setting through swipe not implemented");//_mediaPlayer.audio.volume = percentage * 200;
  634. }
  635. }
  636. #pragma mark - Video Filter UI
  637. - (IBAction)videoFilterToggle:(id)sender
  638. {
  639. if (!_playbackSpeedViewHidden)
  640. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  641. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  642. if (!_controlsHidden)
  643. self.controllerPanel.hidden = _controlsHidden = YES;
  644. }
  645. self.videoFilterView.hidden = !_videoFiltersHidden;
  646. _videoFiltersHidden = self.videoFilterView.hidden;
  647. }
  648. - (IBAction)videoFilterSliderAction:(id)sender
  649. {
  650. if (sender == self.hueSlider)
  651. _mediaPlayer.hue = (int)self.hueSlider.value;
  652. else if (sender == self.contrastSlider)
  653. _mediaPlayer.contrast = self.contrastSlider.value;
  654. else if (sender == self.brightnessSlider) {
  655. if ([self hasExternalDisplay])
  656. _mediaPlayer.brightness = self.brightnessSlider.value;
  657. else
  658. [[UIScreen mainScreen] setBrightness:(self.brightnessSlider.value / 2.)];
  659. } else if (sender == self.saturationSlider)
  660. _mediaPlayer.saturation = self.saturationSlider.value;
  661. else if (sender == self.gammaSlider)
  662. _mediaPlayer.gamma = self.gammaSlider.value;
  663. else if (sender == self.resetVideoFilterButton) {
  664. _mediaPlayer.hue = self.hueSlider.value = 0.;
  665. _mediaPlayer.contrast = self.contrastSlider.value = 1.;
  666. _mediaPlayer.brightness = self.brightnessSlider.value = 1.;
  667. [[UIScreen mainScreen] setBrightness:(self.brightnessSlider.value / 2.)];
  668. _mediaPlayer.saturation = self.saturationSlider.value = 1.;
  669. _mediaPlayer.gamma = self.gammaSlider.value = 1.;
  670. } else
  671. APLog(@"unknown sender for videoFilterSliderAction");
  672. [self _resetIdleTimer];
  673. }
  674. #pragma mark - playback view
  675. - (IBAction)playbackSpeedSliderAction:(UISlider *)sender
  676. {
  677. double speed = pow(2, sender.value / 17.);
  678. float rate = INPUT_RATE_DEFAULT / speed;
  679. if (_currentPlaybackRate != rate)
  680. [_mediaPlayer setRate:INPUT_RATE_DEFAULT / rate];
  681. _currentPlaybackRate = rate;
  682. [self _updatePlaybackSpeedIndicator];
  683. [self _resetIdleTimer];
  684. }
  685. - (void)_updatePlaybackSpeedIndicator
  686. {
  687. float f_value = self.playbackSpeedSlider.value;
  688. double speed = pow(2, f_value / 17.);
  689. self.playbackSpeedIndicator.text = [NSString stringWithFormat:@"%.2fx", speed];
  690. /* rate changed, so update the exported info */
  691. [self performSelectorInBackground:@selector(_updateExportedPlaybackInformation) withObject:nil];
  692. }
  693. - (float)_playbackSpeed
  694. {
  695. float f_rate = _mediaPlayer.rate;
  696. double value = 17 * log(f_rate) / log(2.);
  697. float returnValue = (int) ((value > 0) ? value + .5 : value - .5);
  698. if (returnValue < -34.)
  699. returnValue = -34.;
  700. else if (returnValue > 34.)
  701. returnValue = 34.;
  702. _currentPlaybackRate = returnValue;
  703. return returnValue;
  704. }
  705. - (IBAction)videoDimensionAction:(id)sender
  706. {
  707. if (sender == self.playbackSpeedButton) {
  708. if (!_videoFiltersHidden)
  709. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  710. self.playbackSpeedView.hidden = !_playbackSpeedViewHidden;
  711. _playbackSpeedViewHidden = self.playbackSpeedView.hidden;
  712. [self _resetIdleTimer];
  713. } else if (sender == self.aspectRatioButton) {
  714. NSUInteger count = [_aspectRatios count];
  715. if (_currentAspectRatioMask + 1 > count - 1) {
  716. _mediaPlayer.videoAspectRatio = NULL;
  717. _mediaPlayer.videoCropGeometry = NULL;
  718. _currentAspectRatioMask = 0;
  719. [self.statusLabel showStatusMessage:[NSString stringWithFormat:NSLocalizedString(@"AR_CHANGED", @""), NSLocalizedString(@"DEFAULT", @"")]];
  720. } else {
  721. _currentAspectRatioMask++;
  722. if ([_aspectRatios[_currentAspectRatioMask] isEqualToString:@"FILL_TO_SCREEN"]) {
  723. UIScreen *screen;
  724. if (![self hasExternalDisplay])
  725. screen = [UIScreen mainScreen];
  726. else
  727. screen = [UIScreen screens][1];
  728. float f_ar = screen.bounds.size.width / screen.bounds.size.height;
  729. if (f_ar == (float)(640./1136.)) // iPhone 5 aka 16:9.01
  730. _mediaPlayer.videoCropGeometry = "16:9";
  731. else if (f_ar == (float)(2./3.)) // all other iPhones
  732. _mediaPlayer.videoCropGeometry = "16:10"; // libvlc doesn't support 2:3 crop
  733. else if (f_ar == .75) // all iPads
  734. _mediaPlayer.videoCropGeometry = "4:3";
  735. else if (f_ar == .5625) // AirPlay
  736. _mediaPlayer.videoCropGeometry = "16:9";
  737. else
  738. APLog(@"unknown screen format %f, can't crop", f_ar);
  739. [self.statusLabel showStatusMessage:NSLocalizedString(@"FILL_TO_SCREEN", @"")];
  740. return;
  741. }
  742. _mediaPlayer.videoCropGeometry = NULL;
  743. _mediaPlayer.videoAspectRatio = (char *)[_aspectRatios[_currentAspectRatioMask] UTF8String];
  744. [self.statusLabel showStatusMessage:[NSString stringWithFormat:NSLocalizedString(@"AR_CHANGED", @""), _aspectRatios[_currentAspectRatioMask]]];
  745. }
  746. }
  747. }
  748. #pragma mark - background interaction
  749. - (void)applicationWillResignActive:(NSNotification *)aNotification
  750. {
  751. [self _saveCurrentState];
  752. if (![[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingContinueAudioInBackgroundKey] boolValue]) {
  753. [_mediaPlayer pause];
  754. _shouldResumePlaying = YES;
  755. } else
  756. _mediaPlayer.currentVideoTrackIndex = 0;
  757. }
  758. - (void)applicationDidEnterBackground:(NSNotification *)notification
  759. {
  760. _shouldResumePlaying = NO;
  761. }
  762. - (void)applicationDidBecomeActive:(NSNotification *)notification
  763. {
  764. if (_shouldResumePlaying) {
  765. _shouldResumePlaying = NO;
  766. [_mediaPlayer play];
  767. } else
  768. _mediaPlayer.currentVideoTrackIndex = 1;
  769. }
  770. - (void)_updateExportedPlaybackInformation
  771. {
  772. if (!_mediaItem) {
  773. [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nil;
  774. return;
  775. }
  776. MLFile * currentFile = _mediaItem;
  777. /* we omit artwork for now since we had to read it from storage as we can't access
  778. * the artwork cache at the moment - FIXME? */
  779. NSDictionary *currentlyPlayingTrackInfo = @{ MPMediaItemPropertyTitle : currentFile.title,
  780. MPMediaItemPropertyPlaybackDuration : @(currentFile.duration.intValue / 1000.),
  781. MPNowPlayingInfoPropertyElapsedPlaybackTime : @(_mediaPlayer.time.intValue / 1000.),
  782. MPNowPlayingInfoPropertyPlaybackRate : @(_mediaPlayer.rate) };
  783. [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
  784. }
  785. #pragma mark - autorotation
  786. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  787. return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  788. || toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
  789. }
  790. #pragma mark - AVSession delegate
  791. - (void)beginInterruption
  792. {
  793. if ([[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingContinueAudioInBackgroundKey] boolValue])
  794. _shouldResumePlaying = YES;
  795. [_mediaPlayer pause];
  796. }
  797. - (void)endInterruption
  798. {
  799. if (_shouldResumePlaying) {
  800. [_mediaPlayer play];
  801. _shouldResumePlaying = NO;
  802. }
  803. }
  804. #pragma mark - External Display
  805. - (BOOL)hasExternalDisplay
  806. {
  807. return ([[UIScreen screens] count] > 1);
  808. }
  809. - (void)showOnExternalDisplay
  810. {
  811. UIScreen *screen = [UIScreen screens][1];
  812. screen.overscanCompensation = UIScreenOverscanCompensationInsetApplicationFrame;
  813. self.externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
  814. UIViewController *controller = [[VLCExternalDisplayController alloc] init];
  815. self.externalWindow.rootViewController = controller;
  816. [controller.view addSubview:_movieView];
  817. controller.view.frame = screen.bounds;
  818. _movieView.frame = screen.bounds;
  819. self.playingExternallyView.hidden = NO;
  820. self.externalWindow.screen = screen;
  821. self.externalWindow.hidden = NO;
  822. }
  823. - (void)hideFromExternalDisplay
  824. {
  825. [self.view addSubview:_movieView];
  826. [self.view sendSubviewToBack:_movieView];
  827. _movieView.frame = self.view.frame;
  828. self.playingExternallyView.hidden = YES;
  829. self.externalWindow.hidden = YES;
  830. self.externalWindow = nil;
  831. }
  832. - (void)handleExternalScreenDidConnect:(NSNotification *)notification
  833. {
  834. [self showOnExternalDisplay];
  835. }
  836. - (void)handleExternalScreenDidDisconnect:(NSNotification *)notification
  837. {
  838. [self hideFromExternalDisplay];
  839. }
  840. @end