我有一些代码给你,我已经在我的项目中实现了它并在 google-groups 上讨论了它:
https://groups.google.com/d/msg/discuss-webrtc/jAHCnB12khE/zJEu1vyUAgAJ
我会在这里为下一代转移代码:
首先,我在广播扩展中创建了额外的类来管理 WebRTC 相关代码并将其称为 PeerManager。
使用本地流设置视频轨道,小心,你应该这样做
在生成本地报价之前。
private func setupVideoStreaming() {
localStream = webRTCPeer.peerConnectionFactory.mediaStream(withStreamId: "\(personID)_screen_sharing")
videoSource = webRTCPeer.peerConnectionFactory.videoSource()
videoCapturer = RTCVideoCapturer(delegate: videoSource)
videoSource.adaptOutputFormat(toWidth: 441, height: 736, fps: 15)
let videoTrack = webRTCPeer.peerConnectionFactory.videoTrack(with: videoSource, trackId: "screen_share_track_id")
videoTrack.isEnabled = true
localStream.addVideoTrack(videoTrack)
for localStream in webRTCPeer.localPeerConnection.peerConnection.localStreams {
webRTCPeer.localPeerConnection.peerConnection.remove(localStream)
}
webRTCPeer.localPeerConnection.peerConnection.add(localStream)
}
我从为我提供 CMSampleBuffer 的系统收到回调,我将其转换为 RTCVideoFrame 并发送到 videoSource(模拟
视频捕捉器)
override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
switch sampleBufferType {
case RPSampleBufferType.video:
// Handle video sample buffer
guard peerManager != nil, let imageBuffer: CVImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
break
}
let pixelFormat = CVPixelBufferGetPixelFormatType(imageBuffer) // kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
let timeStampNs: Int64 = Int64(CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) * 1000000000)
let rtcPixlBuffer = RTCCVPixelBuffer(pixelBuffer: imageBuffer)
let rtcVideoFrame = RTCVideoFrame(buffer: rtcPixlBuffer, rotation: ._0, timeStampNs: timeStampNs)
peerManager.push(videoFrame: rtcVideoFrame)
case RPSampleBufferType.audioApp:
break
case RPSampleBufferType.audioMic:
break
}
}
来自 peerManager 的代码,它是来自的推送功能的实现
上面的代码。这里没什么奇怪的,我们使用 Capturer 来模拟 Capturer 的行为
委托。
func push(videoFrame: RTCVideoFrame) {
guard isConnected, videoCapturer != nil, isProcessed else {
return
}
videoSource.capturer(videoCapturer, didCapture: videoFrame)
}
现在您已准备好生成本地报价、发送和传输任何您想要的数据。尝试检查您的本地报价,如果您做的一切正确,您应该会在报价中看到 a=sendonly。
附:按照VladimirTechMan的建议,您也可以在 AppRTCMobile 演示应用中查看广播扩展的示例代码。我为您找到了链接,它是 Objective-C 示例 https://webrtc.googlesource.com/src/+/358f2e076051d28b012529d3ae6a080838d27209
您应该对 ARDBroadcastSampleHandler.m/.h 和 ARDExternalSampleCapturer.m/.h 文件感兴趣。
千万不要忘记,你可以按照https://webrtc.org/native-code/ios/的指令自己搭建