title: iOS Swift - how to get aspect ratio of local and remote video?
body: <p>Scenario:
  I'm building a WebRTC view inside an app
  The container for videos will always have a height of 160.</p>
  <p>In the center of the container there should be displayed the remote video with a max height of 160, width should be scaled to respect the aspect ratio of the video. Width also cannot be bigger than the view width, in that case the width will be equal to view width and the height should be adapted to aspect ratio.</p>
  <p>In top right corner there should be displayed the local video from front camera with a max width of 100 and the height should be adapted to respect the aspect ratio of local video</p>
  <p>my code so far:</p>
  <pre class="lang-swift prettyprint-override"><code>func createPeerConnection () {
      // some other code
  
      self.localStream = self.factory.mediaStream(withStreamId: &quot;stream&quot;)
      let videoSource = self.factory.videoSource()
  
      let devices = RTCCameraVideoCapturer.captureDevices()
      if let camera = devices.last,
          let format = RTCCameraVideoCapturer.supportedFormats(for: camera).last,
          let fps = format.videoSupportedFrameRateRanges.first?.maxFrameRate {
          let intFps = Int(fps)
          self.capturer = RTCCameraVideoCapturer(delegate: videoSource)
          self.capturer?.startCapture(with: camera, format: format, fps: intFps)
          videoSource.adaptOutputFormat(toWidth: 100, height: 160, fps: Int32(fps))
      }
  
      let videoTrack = self.factory.videoTrack(with: videoSource, trackId: &quot;video&quot;)
      self.localStream.addVideoTrack(videoTrack)
  
      DispatchQueue.main.async {
          if self.localView == nil {
              let videoView = RTCEAGLVideoView(frame: CGRect(x: self.view.frame.size.width - 105, y: 5, width: 100, height: 160))
              videoView.backgroundColor = UIColor.red
  
              self.view.addSubview(videoView)
              self.localView = videoView
          }
          videoTrack.add(self.localView!)
      }
  }
  
  func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) {
      self.remoteStream = stream
      if let videoTrack = stream.videoTracks.first {
          DispatchQueue.main.async {
              if self.remoteView == nil {
                  let videoView = RTCEAGLVideoView(frame: CGRect(x: self.view.frame.size.width - 50, y: 0, width: 100, height: 160))
                  videoView.backgroundColor = UIColor.green
                  if let local = self.localView {
                      self.view.insertSubview(videoView, belowSubview: local)
                  } else {
                      self.view.addSubview(videoView)
                  }
                  self.remoteView = videoView
              }
              videoTrack.add(self.remoteView!)
          }
      }
  }
  </code></pre>
  <p>I don't know how to get the aspect ratio of either of the videos, local or remote. If i had that, i could compute the appropriate width and heights for each of them</p>
  <p>// Edit with solution:</p>
  <p>I did not find a way to get the exact size but I found a way to render the video at scale
  All I had to do was:</p>
  <pre><code>let videoView = RTCEAGLVideoView(frame: CGRect(x: self.view.frame.size.width - 105, y: 5, width: 100, height: 134))
  videoView.contentMode = .scaleAspectFill
  </code></pre>
  <p>Now the video scales itself based on the container size</p>