7

我正在使用 webRTC 进行视频通话。一切都运行顺利,但我在 iPhoneX、XSMax 上的远程视频的纵横比上苦苦挣扎。我看到很多放大的视频。你能帮我看看如何在有缺口的设备上管理远程视频吗?下面是我处理远程大小的代码。

func videoView(_ videoView: RTCEAGLVideoView, didChangeVideoSize size: CGSize) {
    print(size)

    let defaultAspectRatio: CGSize = CGSize(width: 4, height: 3)
    let aspectRatio: CGSize = size.equalTo(CGSize.zero) ? defaultAspectRatio : size
    let videoRect: CGRect = self.view.bounds
    let maxFloat = CGFloat.maximum(self.view.frame.width, self.view.frame.height)
    let newAspectRatio = aspectRatio.width / aspectRatio.height
    var frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
    if (aspectRatio.width < aspectRatio.height) {
        frame.size.width = maxFloat;
        frame.size.height = frame.size.width / newAspectRatio;
    } else {
        frame.size.height = maxFloat;
        frame.size.width = frame.size.height * newAspectRatio;
    }
    frame.origin.x = (self.view.frame.width - frame.size.width) / 2
    frame.origin.y = (self.view.frame.height - frame.size.height) / 2

self.remoteView.frame = frame

}
4

3 回答 3

5

根据@Eysner 的回答,什么对我有用,最终代码(使用 swift 5 编写):

import UIKit
import WebRTC


final class WebRTCView: UIView, RTCVideoViewDelegate {
    let videoView = RTCEAGLVideoView(frame: .zero)
    var videoSize = CGSize.zero

    override init(frame: CGRect) {
        super.init(frame: frame)
        videoView.delegate = self
        addSubview(videoView)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        videoView.delegate = self
        addSubview(videoView)
    }

    func videoView(_ videoView: RTCVideoRenderer, didChangeVideoSize size: CGSize) {
        self.videoSize = size
        setNeedsLayout()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        guard videoSize.width > 0 && videoSize.height > 0 else {
            videoView.frame = bounds
            return
        }

        var videoFrame = AVMakeRect(aspectRatio: videoSize, insideRect: bounds)
        let scale = videoFrame.size.aspectFitScale(in: bounds.size)
        videoFrame.size.width = videoFrame.size.width * CGFloat(scale)
        videoFrame.size.height = videoFrame.size.height * CGFloat(scale)

        videoView.frame = videoFrame
        videoView.center = CGPoint(x: bounds.midX, y: bounds.midY)
    }
}


extension CGSize {
    func aspectFitScale(in container: CGSize) -> CGFloat {

        if height <= container.height && width > container.width {
            return container.width / width
        }
        if height > container.height && width > container.width {
            return min(container.width / width, container.height / height)
        }
        if height > container.height && width <= container.width {
            return container.height / height
        }
        if height <= container.height && width <= container.width {
            return min(container.width / width, container.height / height)
        }
        return 1.0
    }
}

有 aspectFitScale 函数,它只是描述逻辑,你可以重构它。

我在演示中使用了这种方法: https ://github.com/Maxatma/Walkie-Talkie/

于 2020-05-03T21:00:03.547 回答
0

您可以使用演示视频制作一些课程 -

class SomeVideoView: UIView, RTCVideoViewDelegate {
    let videoView = RTCEAGLVideoView(frame: .zero)
    var videoSize = CGSize.zero

使用init方法

override init(frame: CGRect) {
    super.init(frame: frame)
    videoView.delegate = self
    self.addSubview(videoView)
   ...

像这样处理委托方法videoView:didChangeVideoSize(我们不需要在 UI 中更改框架,只标记需要布局)

func videoView(_ videoView: RTCEAGLVideoView, didChangeVideoSize size: CGSize) {
    if (self.videoView == videoView) {
        self.videoSize = size
    }
    self.setNeedsLayout()
}

并覆盖layoutSubviews方法

override func layoutSubviews() {
    if (self.videoSize.width > 0 && self.videoSize.height > 0) {
        var videoFrame = AVMakeRect(aspectRatio: self.videoSize, insideRect: bounds)
        var scale = 1.0
        if (videoFrame.size.width > videoFrame.size.height) {
            scale = bounds.size.height / videoFrame.size.height
        } else {
            scale = bounds.size.width / videoFrame.size.width
        }
        videoFrame.size.width = videoFrame.size.width * scale
        videoFrame.size.height = videoFrame.size.height * scale
        self.videoView.frame = videoFrame
        self.videoView.center = CGPointMake(bounds.midX, bounds.midY)
    } else {
        self.videoView.frame = bounds
    }

    ...
}

并设置 SomeVideoView 是全屏的(这个链接可以帮助它)
Xcode 9 的安全区域

于 2019-06-29T19:02:32.797 回答
0

使用 AVMakeRect 很简单

//TODO: Default mobile
if size.width < size.height{
     newSize = aspectFill(aspectRatio: size, minimumSize: UIScreen.main.bounds.size)
}else{
            //Default computer
     newSize = AVMakeRect(aspectRatio: size, insideRect: UIScreen.main.bounds).size
 }

因为有时用户可能有来自网站的电话,所以我没有填写

func aspectFill(aspectRatio : CGSize, minimumSize: CGSize) -> CGSize {
        let mW = minimumSize.width / aspectRatio.width;
        let mH = minimumSize.height / aspectRatio.height;
        
        var temp = minimumSize
        if( mH > mW ) {
            temp.width = minimumSize.height / aspectRatio.height * aspectRatio.width;
        }
        else if( mW > mH ) {
            temp.height = minimumSize.width / aspectRatio.width * aspectRatio.height;
        }
        
        return temp;
    }
于 2021-04-06T08:32:47.900 回答