0

我正在尝试制作一个 iOS 应用程序,对来自相机的视频进行一些预处理,然后通过 webrtc 将其发送出去。我正在使用协议对每个单独的帧进行预处理AVCaptureVideoDataOutputSampleBufferDelegate,然后使用该captureOutput方法捕获帧。

现在我需要弄清楚如何在 WebRTC 上发送它。我正在使用 Google WebRTC 库:https ://webrtc.googlesource.com/src/ 。

有一个名为RTCCameraVideoCapturer[(link)][1] 的类,大多数使用这个库的 iOS 示例应用程序似乎都在使用它。此类访问相机本身,因此我将无法使用它。它使用AVCaptureVideoDataOutputSampleBufferDelegate,并且在captureOutput,它这样做

  RTC_OBJC_TYPE(RTCCVPixelBuffer) *rtcPixelBuffer =
      [[RTC_OBJC_TYPE(RTCCVPixelBuffer) alloc] initWithPixelBuffer:pixelBuffer];
  int64_t timeStampNs = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) *
      kNanosecondsPerSecond;
  RTC_OBJC_TYPE(RTCVideoFrame) *videoFrame =
      [[RTC_OBJC_TYPE(RTCVideoFrame) alloc] initWithBuffer:rtcPixelBuffer
                                                  rotation:_rotation
                                               timeStampNs:timeStampNs];
  [self.delegate capturer:self didCaptureVideoFrame:videoFrame];

[self.delegate capturer:self didCaptureVideoFrame:videoFrame]似乎是将单个帧输入 webRTC 的调用。


How can I write swift code that will allow me to feed frames into webRTC one at a time, similar to how it is done in the `RTCCameraVideoCapturer` class?


  [1]: https://webrtc.googlesource.com/src/+/refs/heads/master/sdk/objc/components/capturer/RTCCameraVideoCapturer.m
4

1 回答 1

1

您只需要创建一个 RTCVideoCapturer 实例(它只是委托的持有者,localVideoTrack.source),并且只要您有要推送的像素缓冲区,就可以使用帧调用委托方法“capturer”。

这是一个示例代码。

    var capturer: RTCVideoCapturer?
    let rtcQueue = DispatchQueue(label: "WebRTC")

    func appClient(_ client: ARDAppClient!, didReceiveLocalVideoTrack localVideoTrack: RTCVideoTrack!) {
        capturer = RTCVideoCapturer(delegate: localVideoTrack.source)
    }

    func render(pixelBuffer: CVPixelBuffer, timesample: CMTime) {
        
        let buffer = RTCCVPixelBuffer(pixelBuffer: pixelBuffer)
        self.rtcQueue.async {
            let frame = RTCVideoFrame(buffer: buffer, rotation: ._0, timeStampNs: Int64(CMTimeGetSeconds(timesample) * Double(NSEC_PER_SEC)))
            self.capturer?.delegate?.capturer(self.capturer!, didCapture: frame)
        }
    }

于 2021-07-18T01:57:34.960 回答