如何使RTCEAGLVideoView不变形全屏显示

时间:2019-01-02 02:23:24

标签: ios webrtc

我正在显示带有RTCEAGLVideoView的RTCVideoTrack,并希望它是全屏的,但是它总是变形。我尝试使用AVMakeRectWithAspectRatioInsideRect方法显示此视图,也没有失真但也没有全屏,这是我的代码

RTCEAGLVideoView *remoteVideoView = [[RTCEAGLVideoView alloc] initWithFrame:self.backView.bounds];
remoteVideoView.contentMode = UIViewContentModeScaleAspectFill;
_remoteVideoView = remoteVideoView;
[remoteVideoView renderFrame:nil];
[remoteVideoTrack addRenderer:remoteVideoView];
remoteVideoView.delegate = self;
self.remoteVideoTrack = remoteVideoTrack;
[self.backView addSubview:remoteVideoView];

当didChangeVideoSize委托调用时:

- (void)videoView:(RTCEAGLVideoView*)videoView didChangeVideoSize:(CGSize)size {
    if (videoView == _remoteVideoView) {
        _remoteVideoSize = size;
    }
    CGRect bounds = self.view.bounds;
    if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) {
        // Aspect fill remote video into bounds.
        CGRect remoteVideoFrame =
        AVMakeRectWithAspectRatioInsideRect(_remoteVideoSize, bounds);
        CGFloat scale = 1;
        if (remoteVideoFrame.size.width > remoteVideoFrame.size.height) {
            // Scale by height.
            scale = bounds.size.height / remoteVideoFrame.size.height;
        } else {
            // Scale by width.
            scale = bounds.size.width / remoteVideoFrame.size.width;
        }
        remoteVideoFrame.size.height *= scale;
        remoteVideoFrame.size.width *= scale;
        _remoteVideoView.frame = remoteVideoFrame;
        _remoteVideoView.center =
        CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
    } else {
        _remoteVideoView.frame = bounds;
    }
}

我想知道renderFrame的方法是否可以做些有用的事情,但是我找不到如何创建RTCI420Frame属性的方法。

1 个答案:

答案 0 :(得分:2)

您可以将RTCMTLVideoView用于不同的contentMode(全屏)。

导入:

#import <WebRTC/RTCMTLVideoView.h>

在此处使用此代码:

 #if defined(RTC_SUPPORTS_METAL)

       RTCMTLVideoView *mtlVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
       mtlVideoView.videoContentMode = UIViewContentModeScaleAspectFill;

       #else

  //Here you can create RTCEAGLVideoView