将本地视频添加到对等连接

时间:2017-03-18 15:34:19

标签: swift webrtc

我正在构建一个IOS swift应用程序,我很难将视频流附加到mediaStream。

我使用这个库:https://github.com/Anakros/WebRTC一切都像魅力一样。视频流除外。我不知道如何将视频流绑定到媒体流。代码:

var mediaStream: RTCMediaStream!
@IBOutlet weak var localFrame: UIView!
var peerConnectionFactory = RTCPeerConnectionFactory()
var previewLayer = AVCaptureVideoPreviewLayer()
let g = peerConnectionFactory.avFoundationVideoSource(with: nil);
previewLayer = AVCaptureVideoPreviewLayer(session: g.captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait
localFrame.layer.addSublayer(previewLayer)

previewLayer.position = CGPoint(x: self.localFrame.frame.width / 2, y: self.localFrame.frame.height / 2)
previewLayer.bounds = localFrame.frame

localVideoTrack = peerConnectionFactory.videoTrack(with: g, trackId: VIDEO_TRACK_IDD)
mediaStream.addVideoTrack(localVideoTrack)

mediaStream = peerConnectionFactory.mediaStream(withStreamId: LOCAL_MEDIA_STREAM_IDD)
mediaStream.addVideoTrack(localVideoTrack)

它崩溃了这一行:mediaStream.addVideoTrack(localVideoTrack),出现以下错误:fatal error: unexpectedly found nil while unwrapping an Optional value我不知道我做错了什么,因为所有类型都是对的。

1 个答案:

答案 0 :(得分:1)

我看到

的重复行
mediaStream.addVideoTrack(localVideoTrack)

您需要在向其添加曲目之前创建媒体流。所以,评论第一行并尝试。