场景:我正在应用程序内部构建WebRTC视图。视频容器的高度始终为160。
在容器的中央应显示最大高度为160的远程视频,宽度应按视频的长宽比进行缩放。宽度也不能大于视图宽度,在这种情况下,宽度将等于视图宽度,并且高度应适合纵横比。
在右上角应显示前摄像头的本地视频,最大宽度为100,高度应调整为尊重本地视频的长宽比
到目前为止,我的代码:
func createPeerConnection () {
// some other code
self.localStream = self.factory.mediaStream(withStreamId: "stream")
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: "video")
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!)
}
}
}
我不知道如何获取本地或远程视频的纵横比。如果有的话,我可以为它们每个计算合适的宽度和高度
我认为此解决方案将帮助您解决问题< stackoverflow.com/questions/10433774/… >
—
Darshan sk