7

I am using webRTC for video calling. Everything is running smooth but I am struggling with aspect ratio of Remote video on iPhoneX, XSMax. I am seeing lot of zoom in video. Can you please help me out how I can manage remote video on devices that have notch. Below is the code where I am handling remote size.

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

}
Raj Shekhar
  • 714
  • 1
  • 4
  • 15
  • If your problem is only with devices that have the notch, my guess is you need to be using `self.view.safeAreaLayoutGuide` with constraints instead of just `self.view.frame.width`. Using only the frame width and height is going to put your video 'behind' the notch. Read the docs [here](https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/adaptivity-and-layout/). – zwilf Jul 05 '19 at 12:11
  • Since I am using size classes safeAreaLayoutGuide won't work for me. Can you please suggest me alternative way. – Raj Shekhar Jul 08 '19 at 07:20

3 Answers3

7

According to @Eysner's answer, what is work for me, the final code (written using 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
    }
}

There is aspectFitScale function, it is simply to describe logic, you can refactor it if you want to.

I'm using this approach in the demo: https://github.com/Maxatma/Walkie-Talkie/

Zaporozhchenko Oleksandr
  • 4,660
  • 3
  • 26
  • 48
0

You can make some class with show video -

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

With init method

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

Handle delegate method videoView:didChangeVideoSize like this (we don't need change frame in UI, only mark needsLayout)

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

And overwrite layoutSubviews method

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
    }

    ...
}

And set up SomeVideoView is full screen (this link can be help with it)
Safe Area of Xcode 9

Eysner
  • 584
  • 4
  • 15
0

just simple using 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
 }

for sometimes user may have call from website, so i didnt make fill from it

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;
    }
famfamfam
  • 396
  • 3
  • 8
  • 31