WorieN
WorieN

Reputation: 1296

IOS WebRTC - Can't show local videotrack on the screen

Trying to setup webrtc video chat in the ios app and faced such problem - after creating videoTrack and add renderer to it - nothing happens. The renderer view still black and there are no video. Here is the code:

        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!)

Upvotes: 0

Views: 1642

Answers (1)

Roxxy
Roxxy

Reputation: 57

You can use the RTCCameraPreviewView. As per documentation "RTCCameraPreviewView is a view that renders local video from an 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)
}

Upvotes: 1

Related Questions