1

我使用 WebRTC 渲染远程视频失败。对于上下文,我使用的是 Janus 的流插件。

我正在关注到目前为止我一直在阅读的内容。每当peerConnection(_ peerConnection:, didAdd stream:)被调用时RTCPeerConnectionDelegate,我都会创建一个远程渲染器并将其添加到委托提供给我的流的第一个 videoTrack 中,如下所示:

#if arch(arm64)
let remoteRenderer = RTCMTLVideoView(frame: self.view.frame)
remoteRenderer.videoContentMode = .scaleAspectFill
#else
let remoteRenderer = RTCEAGLVideoView(frame: self.view.frame)
#endif
            
stream.videoTracks.first?.add(remoteRenderer)
self.view.addSubview(remoteRenderer)

但是视频不会显示,只有黑屏。

我的代表还打电话peerConnection(_ peerConnection:, didChange newState:)给 newState ,RTCIceConnectionState.connected这让我认为连接很好。

4

1 回答 1

3

当您收到事件“didStartReceivingOnTransceiver”时尝试附加渲染器:

func peerConnection(_ peerConnection: RTCPeerConnection, didStartReceivingOn transceiver: RTCRtpTransceiver) {
    switch transceiver.mediaType {
    case .video:
            DispatchQueue.main.async {[weak self] in
                self?.remoteVideoTrack = transceiver.receiver.track as? RTCVideoTrack
                if let renderer = self?.delegate?.viewForRemoteVideoTrack(){
                    self?.remoteVideoTrack?.add(renderer)
                }
            }
    default:
        break
    }        
}
于 2020-07-05T08:53:03.273 回答