Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Fixing paused video doesn't react on changing device orientation #199

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
kareljuricka marked this conversation as resolved.
Show resolved Hide resolved

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]];
kareljuricka marked this conversation as resolved.
Show resolved Hide resolved
}

- (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),
kareljuricka marked this conversation as resolved.
Show resolved Hide resolved
};
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.
kareljuricka marked this conversation as resolved.
Show resolved Hide resolved
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;
kareljuricka marked this conversation as resolved.
Show resolved Hide resolved
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