webrtc-sdk / libwebrtc

A C++ wrapper for binary release, mainly used for flutter-webrtc desktop (windows, linux, embedded).
MIT License
390 stars 78 forks source link

Implementing Custom Capture in Libwebrtc? #87

Open szpnygo opened 1 year ago

szpnygo commented 1 year ago

I'm currently working on a project where I need to implement custom capture functionality using Libwebrtc.

As a beginner exploring the vast landscape of WebRTC, I've been trying to customize the VideoCapturer. While attempting to extend the RTCVideoCapturer class, I noticed there doesn't seem to be an 'OnFrame' method that allows me to pass my customized frame.

I'd like to confirm if this would be the appropriate and recommended way to achieve my objective. If not, could you kindly suggest an alternative approach?

szpnygo commented 10 months ago

I added a new function in RTCVideoCapturer

virtual void GenerateFrame(scoped_refptr<RTCVideoFrame> frame) = 0

void GenerateFrame(scoped_refptr<RTCVideoFrame> frame) override {
    if (video_capturer_ != nullptr) {
      VideoFrameBufferImpl* impl =
          static_cast<VideoFrameBufferImpl*>(frame.get());

      auto newFrame = webrtc::VideoFrame::Builder()
                          .set_video_frame_buffer(impl->buffer())
                          .set_rotation(static_cast<webrtc::VideoRotation>(
                              impl->rotation()))
                          .set_timestamp_us(rtc::TimeMicros())
                          .build();

      video_capturer_->GenerateFrame(newFrame);
    }
  }

Additionally, I added a new method in VideoCapturer:

virtual void GenerateFrame(const VideoFrame& frame) { OnFrame(frame); }

After implementing this, I achieved my goal. However, I'm uncertain if this is the correct approach. Will the library continue to support such objectives in the future?