Skip to content

Commit

Permalink
Fixing paused video doesn't react on changing device orientation with…
Browse files Browse the repository at this point in the history
…out overhead

Fixes shaka-project#194
  • Loading branch information
kareljuricka committed Nov 30, 2020
1 parent 581e317 commit 8d55cb8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
3 changes: 1 addition & 2 deletions shaka/src/media/apple_video_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions shaka/src/media/video_renderer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<VideoFillMode> fill_mode_;
Expand Down
36 changes: 34 additions & 2 deletions shaka/src/public/ShakaPlayerView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ @interface ShakaPlayerView () {
CALayer *_avPlayerLayer;
ShakaPlayer *_player;
shaka::VideoFillMode _gravity;
CGImageRef _image;

NSMutableDictionary<NSValue *, NSSet<CALayer *> *> *_cues;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -167,6 +174,7 @@ - (void)renderLoop {

shaka::Rational<uint32_t> aspect_ratio;
if (CGImageRef image = _player.videoRenderer->Render(nullptr, &aspect_ratio)) {
_image = image;
_imageLayer.contents = (__bridge_transfer id)image;

// Fit image in frame.
Expand All @@ -175,8 +183,8 @@ - (void)renderLoop {
shaka::ShakaRect<uint32_t> dest_bounds = {
0,
0,
static_cast<uint32_t>(self.bounds.size.width),
static_cast<uint32_t>(self.bounds.size.height),
static_cast<uint32_t>(self.superview.bounds.size.width),
static_cast<uint32_t>(self.superview.bounds.size.height),
};
shaka::ShakaRect<uint32_t> src;
shaka::ShakaRect<uint32_t> dest;
Expand Down Expand Up @@ -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<uint32_t> image_bounds = {0, 0, CGImageGetWidth(_image),
CGImageGetHeight(_image)};
shaka::ShakaRect<uint32_t> dest_bounds = {
0,
0,
static_cast<uint32_t>(self.superview.bounds.size.width),
static_cast<uint32_t>(self.superview.bounds.size.height),
};
shaka::Rational<uint32_t> aspect_ratio;
shaka::ShakaRect<uint32_t> src;
shaka::ShakaRect<uint32_t> dest;
shaka::FitVideoToRegion(image_bounds, dest_bounds, aspect_ratio,
_player.videoRenderer->fill_mode(), &src, &dest);
_imageLayer.contentsRect = CGRectMake(
static_cast<CGFloat>(src.x) / image_bounds.w, static_cast<CGFloat>(src.y) / image_bounds.h,
static_cast<CGFloat>(src.w) / image_bounds.w, static_cast<CGFloat>(src.h) / image_bounds.h);
_imageLayer.frame = CGRectMake(dest.x, dest.y, dest.w, dest.h);
}

@end

0 comments on commit 8d55cb8

Please sign in to comment.