mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
5.9 KiB
Matlab
166 lines
5.9 KiB
Matlab
8 years ago
|
//
|
||
6 years ago
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
8 years ago
|
//
|
||
|
|
||
|
#import "RemoteVideoView.h"
|
||
8 years ago
|
#import "UIFont+OWS.h"
|
||
|
#import "UIView+OWS.h"
|
||
8 years ago
|
#import <MetalKit/MetalKit.h>
|
||
|
#import <PureLayout/PureLayout.h>
|
||
5 years ago
|
#import <SignalCoreKit/Threading.h>
|
||
8 years ago
|
#import <WebRTC/RTCEAGLVideoView.h>
|
||
|
#import <WebRTC/RTCMTLVideoView.h>
|
||
|
#import <WebRTC/RTCVideoRenderer.h>
|
||
|
|
||
8 years ago
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
6 years ago
|
@interface RemoteVideoView () <RTCVideoViewDelegate>
|
||
8 years ago
|
|
||
8 years ago
|
@property (nonatomic, readonly) __kindof UIView<RTCVideoRenderer> *videoRenderer;
|
||
8 years ago
|
|
||
8 years ago
|
// Used for legacy EAGLVideoView
|
||
7 years ago
|
@property (nullable, nonatomic) NSArray<NSLayoutConstraint *> *remoteVideoConstraints;
|
||
8 years ago
|
|
||
8 years ago
|
@end
|
||
|
|
||
|
@implementation RemoteVideoView
|
||
8 years ago
|
|
||
8 years ago
|
- (instancetype)init
|
||
|
{
|
||
|
self = [super init];
|
||
|
if (!self) {
|
||
|
return self;
|
||
|
}
|
||
|
|
||
7 years ago
|
_remoteVideoConstraints = @[];
|
||
|
|
||
8 years ago
|
// Currently RTC only supports metal on 64bit machines
|
||
6 years ago
|
#if defined(__arm64__)
|
||
8 years ago
|
// On 64-bit, iOS9+: uses the MetalKit backed view for improved battery/rendering performance.
|
||
|
if (_videoRenderer == nil) {
|
||
|
|
||
|
// It is insufficient to check the RTC_SUPPORTS_METAL macro to determine Metal support.
|
||
|
// RTCMTLVideoView requires the MTKView class, available only in iOS9+
|
||
|
// So check that it exists before proceeding.
|
||
|
if ([MTKView class]) {
|
||
7 years ago
|
RTCMTLVideoView *rtcMetalView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
|
||
|
rtcMetalView.videoContentMode = UIViewContentModeScaleAspectFill;
|
||
|
_videoRenderer = rtcMetalView;
|
||
8 years ago
|
[self addSubview:_videoRenderer];
|
||
|
[_videoRenderer autoPinEdgesToSuperviewEdges];
|
||
8 years ago
|
// HACK: Although RTCMTLVideo view is positioned to the top edge of the screen
|
||
|
// It's inner (private) MTKView is below the status bar.
|
||
|
for (UIView *subview in [_videoRenderer subviews]) {
|
||
|
if ([subview isKindOfClass:[MTKView class]]) {
|
||
7 years ago
|
[subview autoPinEdgesToSuperviewEdges];
|
||
8 years ago
|
} else {
|
||
7 years ago
|
OWSFailDebug(@"New subviews added to MTLVideoView. Reconsider this hack.");
|
||
8 years ago
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
#endif
|
||
8 years ago
|
|
||
8 years ago
|
// On 32-bit iOS9+ systems, use the legacy EAGL backed view.
|
||
8 years ago
|
if (_videoRenderer == nil) {
|
||
8 years ago
|
RTCEAGLVideoView *eaglVideoView = [RTCEAGLVideoView new];
|
||
8 years ago
|
eaglVideoView.delegate = self;
|
||
8 years ago
|
_videoRenderer = eaglVideoView;
|
||
8 years ago
|
[self addSubview:_videoRenderer];
|
||
|
// Pinning legacy RTCEAGL view discards aspect ratio.
|
||
|
// So we have a more verbose layout in the RTCEAGLVideoViewDelegate methods
|
||
|
// [_videoRenderer autoPinEdgesToSuperviewEdges];
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// We want the rendered video to go edge-to-edge.
|
||
8 years ago
|
_videoRenderer.layoutMargins = UIEdgeInsetsZero;
|
||
8 years ago
|
|
||
8 years ago
|
return self;
|
||
8 years ago
|
}
|
||
|
|
||
|
#pragma mark - RTCVideoRenderer
|
||
|
|
||
|
/** The size of the frame. */
|
||
|
- (void)setSize:(CGSize)size
|
||
|
{
|
||
8 years ago
|
[self.videoRenderer setSize:size];
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
#pragma mark - RTCVideoViewDelegate
|
||
8 years ago
|
|
||
6 years ago
|
- (void)videoView:(id<RTCVideoRenderer>)videoRenderer didChangeVideoSize:(CGSize)remoteVideoSize
|
||
8 years ago
|
{
|
||
7 years ago
|
OWSAssertIsOnMainThread();
|
||
6 years ago
|
|
||
|
if (![videoRenderer isKindOfClass:[RTCEAGLVideoView class]]) {
|
||
|
OWSFailDebug(@"Unexpected videoRenderer: %@", videoRenderer);
|
||
|
return;
|
||
|
}
|
||
|
RTCEAGLVideoView *videoView = (RTCEAGLVideoView *)videoRenderer;
|
||
|
|
||
8 years ago
|
if (remoteVideoSize.height <= 0) {
|
||
7 years ago
|
OWSFailDebug(@"Illegal video height: %f", remoteVideoSize.height);
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
CGFloat aspectRatio = remoteVideoSize.width / remoteVideoSize.height;
|
||
7 years ago
|
OWSLogVerbose(@"Remote video size: width: %f height: %f ratio: %f",
|
||
8 years ago
|
remoteVideoSize.width,
|
||
|
remoteVideoSize.height,
|
||
|
aspectRatio);
|
||
|
|
||
|
UIView *containingView = self.superview;
|
||
|
if (containingView == nil) {
|
||
7 years ago
|
OWSLogDebug(@"Cannot layout video view without superview");
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
[NSLayoutConstraint deactivateConstraints:self.remoteVideoConstraints];
|
||
|
|
||
|
NSMutableArray<NSLayoutConstraint *> *constraints = [NSMutableArray new];
|
||
|
if (remoteVideoSize.width > 0 && remoteVideoSize.height > 0 && containingView.bounds.size.width > 0
|
||
|
&& containingView.bounds.size.height > 0) {
|
||
|
|
||
8 years ago
|
// to approximate "scale to fill" contentMode
|
||
|
// - Pin aspect ratio
|
||
|
// - Width and height is *at least* as wide as superview
|
||
6 years ago
|
[constraints addObject:[videoView autoPinToAspectRatioWithSize:remoteVideoSize]];
|
||
8 years ago
|
[constraints addObject:[videoView autoSetDimension:ALDimensionWidth
|
||
|
toSize:containingView.width
|
||
|
relation:NSLayoutRelationGreaterThanOrEqual]];
|
||
|
[constraints addObject:[videoView autoSetDimension:ALDimensionHeight
|
||
|
toSize:containingView.height
|
||
|
relation:NSLayoutRelationGreaterThanOrEqual]];
|
||
|
[constraints addObjectsFromArray:[videoView autoCenterInSuperview]];
|
||
8 years ago
|
|
||
|
// Low priority constraints force view to be no larger than necessary.
|
||
|
[NSLayoutConstraint autoSetPriority:UILayoutPriorityDefaultLow
|
||
|
forConstraints:^{
|
||
|
[constraints addObjectsFromArray:[videoView autoPinEdgesToSuperviewEdges]];
|
||
|
}];
|
||
|
|
||
8 years ago
|
} else {
|
||
|
[constraints addObjectsFromArray:[videoView autoPinEdgesToSuperviewEdges]];
|
||
|
}
|
||
|
|
||
7 years ago
|
self.remoteVideoConstraints = [constraints copy];
|
||
8 years ago
|
// We need to force relayout to occur immediately (and not
|
||
|
// wait for a UIKit layout/render pass) or the remoteVideoView
|
||
|
// (which presumably is updating its CALayer directly) will
|
||
|
// ocassionally appear to have bad frames.
|
||
|
[videoView setNeedsLayout];
|
||
|
[[videoView superview] setNeedsLayout];
|
||
|
[videoView layoutIfNeeded];
|
||
|
[[videoView superview] layoutIfNeeded];
|
||
|
}
|
||
|
|
||
8 years ago
|
/** The frame to be displayed. */
|
||
|
- (void)renderFrame:(nullable RTCVideoFrame *)frame
|
||
|
{
|
||
8 years ago
|
[self.videoRenderer renderFrame:frame];
|
||
8 years ago
|
}
|
||
|
|
||
|
@end
|
||
8 years ago
|
|
||
|
NS_ASSUME_NONNULL_END
|