IOS WebRTC - 无法在屏幕上显示本地视频

时间:2017-12-14 17:32:16

标签: ios swift webrtc

尝试在ios应用中设置webrtc视频聊天并遇到这样的问题 - 在创建videoTrack并添加渲染器后 - 没有任何反应。渲染器视图仍为黑色,没有视频。这是代码:

        let localRenderView = RTCEAGLVideoView.init(frame: CGRect(origin: CGPoint(x: 30, y: 30), size: CGSize(width: 300, height: 300)))
        self.view.addSubview(localRenderView)

        let pcFactory = RTCPeerConnectionFactory()
            let config = RTCConfiguration.init()
            let constraints:[String:String] = ["OfferToReceiveAudio":"true", "OfferToReceiveVideo":"true"]
            rtcMediaConstaints = RTCMediaConstraints.init(mandatoryConstraints: constraints, optionalConstraints: nil)
            peerConnection = pcFactory?.peerConnection(with: config, constraints: rtcMediaConstaints!, delegate: self)
     if let device = getFrontCameraDevice() {
                print("RtcCommunicationChannel: init capturer")
                let videoSource = pcFactory?.videoSource()
                let capturer = RTCCameraVideoCapturer.init(delegate: videoSource!)
                let videoTrack = pcFactory?.videoTrack(with: videoSource!, trackId: "localVideoTrack")
                videoTrack?.isEnabled = true
                localStream?.addVideoTrack(videoTrack!)
                let peerConnectionSender = peerConnection?.sender(withKind: kRTCMediaStreamTrackKindVideo, streamId: "localStreamId")
                peerConnectionSender?.track = videoTrack!
                peerConnection?.add(localStream!)
                sender.localStreamAdded(peerId: peerId, stream: localStream!)
                print("RtcCommunicationChannel: init capturer end")
                capturer.startCapture(with: device, format: self.selectFormatForDevice(device: device), fps: 60)
            }
       localStream.videoTracks.last?.add(localRenderView!)

1 个答案:

答案 0 :(得分:1)

您可以使用RTCCameraPreviewView。根据文件 “RTCCameraPreviewView是一个从AVCaptureSession呈现本地视频的视图。”

func onLocalStreamReadyForRender() {
    let frame = localVideoView!.frame
    let rtcVideoView = RTCCameraPreviewView.init(frame: CGRect.init())
    rtcVideoView.frame = frame
    rtcVideoView.frame.origin.x = 0
    rtcVideoView.frame.origin.y = 0
    self.localVideoView?.addSubview(rtcVideoView)
}