From 8d55cb8d9cbfe6ae327766259e266291f84c5253 Mon Sep 17 00:00:00 2001 From: Karel Juricka Date: Mon, 30 Nov 2020 23:14:38 +0100 Subject: [PATCH] Fixing paused video doesn't react on changing device orientation without overhead Fixes #194 --- shaka/src/media/apple_video_renderer.cc | 3 +-- shaka/src/media/video_renderer_common.h | 7 +++-- shaka/src/public/ShakaPlayerView.mm | 36 +++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/shaka/src/media/apple_video_renderer.cc b/shaka/src/media/apple_video_renderer.cc index f46a49d4..c2a926bc 100644 --- a/shaka/src/media/apple_video_renderer.cc +++ b/shaka/src/media/apple_video_renderer.cc @@ -59,8 +59,7 @@ CGImageRef AppleVideoRenderer::Impl::Render( if (delay) *delay = loc_delay; - const bool is_paused = player_->PlaybackState() == VideoPlaybackState::Paused; - if (!frame || (frame == prev_frame_ && !is_paused)) + if (!frame || frame == prev_frame_) return nullptr; if (sample_aspect_ratio) diff --git a/shaka/src/media/video_renderer_common.h b/shaka/src/media/video_renderer_common.h index 0c8a8d6f..0462f237 100644 --- a/shaka/src/media/video_renderer_common.h +++ b/shaka/src/media/video_renderer_common.h @@ -51,13 +51,12 @@ class VideoRendererCommon : public VideoRenderer, MediaPlayer::Client { struct VideoPlaybackQuality VideoPlaybackQuality() const override; bool SetVideoFillMode(VideoFillMode mode) override; - protected: - mutable Mutex mutex_; - const MediaPlayer* player_; - private: void OnSeeking() override; + mutable Mutex mutex_; + + const MediaPlayer* player_; const DecodedStream* input_; struct VideoPlaybackQuality quality_; std::atomic fill_mode_; diff --git a/shaka/src/public/ShakaPlayerView.mm b/shaka/src/public/ShakaPlayerView.mm index 6091c0ff..508195e3 100644 --- a/shaka/src/public/ShakaPlayerView.mm +++ b/shaka/src/public/ShakaPlayerView.mm @@ -28,6 +28,7 @@ @interface ShakaPlayerView () { CALayer *_avPlayerLayer; ShakaPlayer *_player; shaka::VideoFillMode _gravity; + CGImageRef _image; NSMutableDictionary *> *_cues; } @@ -121,6 +122,12 @@ - (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 { @@ -167,6 +174,7 @@ - (void)renderLoop { shaka::Rational aspect_ratio; if (CGImageRef image = _player.videoRenderer->Render(nullptr, &aspect_ratio)) { + _image = image; _imageLayer.contents = (__bridge_transfer id)image; // Fit image in frame. @@ -175,8 +183,8 @@ - (void)renderLoop { shaka::ShakaRect dest_bounds = { 0, 0, - static_cast(self.bounds.size.width), - static_cast(self.bounds.size.height), + static_cast(self.superview.bounds.size.width), + static_cast(self.superview.bounds.size.height), }; shaka::ShakaRect src; shaka::ShakaRect dest; @@ -300,4 +308,28 @@ - (BOOL)remakeTextCues:(BOOL)sizeChanged { return YES; } +- (void) orientationChanged:(NSNotification *)note { + if (_imageLayer.contents == nil) { + return; + } + // Fit image in frame. + shaka::ShakaRect image_bounds = {0, 0, CGImageGetWidth(_image), + CGImageGetHeight(_image)}; + shaka::ShakaRect dest_bounds = { + 0, + 0, + static_cast(self.superview.bounds.size.width), + static_cast(self.superview.bounds.size.height), + }; + shaka::Rational aspect_ratio; + 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