diff --git a/shaka/src/public/ShakaPlayerView.mm b/shaka/src/public/ShakaPlayerView.mm index 6091c0ff..efce8115 100644 --- a/shaka/src/public/ShakaPlayerView.mm +++ b/shaka/src/public/ShakaPlayerView.mm @@ -28,6 +28,8 @@ @interface ShakaPlayerView () { CALayer *_avPlayerLayer; ShakaPlayer *_player; shaka::VideoFillMode _gravity; + shaka::Rational _aspect_ratio; + shaka::ShakaRect _image_bounds; NSMutableDictionary *> *_cues; } @@ -121,6 +123,11 @@ - (void)setup { _gravity = shaka::VideoFillMode::MaintainRatio; _cues = [[NSMutableDictionary alloc] init]; + + [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) + name:UIDeviceOrientationDidChangeNotification + object:[UIDevice currentDevice]]; } - (ShakaPlayer *)player { @@ -165,13 +172,15 @@ - (void)renderLoop { return; } - shaka::Rational aspect_ratio; - if (CGImageRef image = _player.videoRenderer->Render(nullptr, &aspect_ratio)) { + if (CGImageRef image = _player.videoRenderer->Render(nullptr, &_aspect_ratio)) { _imageLayer.contents = (__bridge_transfer id)image; + if (![self layoutImage]) + return; + // Fit image in frame. - shaka::ShakaRect image_bounds = {0, 0, CGImageGetWidth(image), - CGImageGetHeight(image)}; + _image_bounds = {0, 0, CGImageGetWidth(image), + CGImageGetHeight(image)}; shaka::ShakaRect dest_bounds = { 0, 0, @@ -180,11 +189,11 @@ - (void)renderLoop { }; shaka::ShakaRect src; shaka::ShakaRect dest; - shaka::FitVideoToRegion(image_bounds, dest_bounds, aspect_ratio, + shaka::FitVideoToRegion(_image_bounds, dest_bounds, _aspect_ratio, _player.videoRenderer->fill_mode(), &src, &dest); _imageLayer.contentsRect = CGRectMake( - static_cast(src.x) / image_bounds.w, static_cast(src.y) / image_bounds.h, - static_cast(src.w) / image_bounds.w, static_cast(src.h) / image_bounds.h); + static_cast(src.x) / _image_bounds.w, static_cast(src.y) / _image_bounds.h, + static_cast(src.w) / _image_bounds.w, static_cast(src.h) / _image_bounds.h); _imageLayer.frame = CGRectMake(dest.x, dest.y, dest.w, dest.h); } } @@ -300,4 +309,28 @@ - (BOOL)remakeTextCues:(BOOL)sizeChanged { return YES; } +- (bool) layoutImage { + return _imageLayer.contents != nil; +} + +- (void) orientationChanged:(NSNotification *)note { + if (![self layoutImage]) { + return; + } + shaka::ShakaRect dest_bounds = { + 0, + 0, + static_cast(self.bounds.size.width), + static_cast(self.bounds.size.height), + }; + shaka::ShakaRect src; + shaka::ShakaRect dest; + shaka::FitVideoToRegion(_image_bounds, dest_bounds, _aspect_ratio, + _player.videoRenderer->fill_mode(), &src, &dest); + _imageLayer.contentsRect = CGRectMake( + static_cast(src.x) / _image_bounds.w, static_cast(src.y) / _image_bounds.h, + static_cast(src.w) / _image_bounds.w, static_cast(src.h) / _image_bounds.h); + _imageLayer.frame = CGRectMake(dest.x, dest.y, dest.w, dest.h); +} + @end