Custom video capturer in linux

161 views
Skip to first unread message

Tudor Zaharia

unread,
Mar 3, 2020, 4:14:16 AM3/3/20
to discuss-webrtc
Is there an example on how to feed libwebrtc with custom raw video frames?

I have a custom software that provides me with a buffer where the raw frame is placed and I would like to feed this through the libwebrtc (such that I just forward the raw frame and libwebrtc does the encoding, sending etc.)

Alexandre GOUAILLARD

unread,
Mar 3, 2020, 6:50:16 AM3/3/20
to discuss...@googlegroups.com
That should get you started.

#include "modules/video_capture/video_capture.h"
#include "media/base/adapted_video_track_source.h"
#include "rtc_base/logging.h"

//Abstract class
class my_VideoSource : public rtc::AdaptedVideoTrackSource, public rtc::VideoSinkInterface<webrtc::VideoFrame>
{
public:
	my_VideoSource();
	virtual ~my_VideoSource();
	
	virtual webrtc::MediaSourceInterface::SourceState Start(const webrtc::VideoCaptureCapability& requested_cap) = 0;
	virtual void Stop() = 0;
	virtual bool IsRunning() = 0;
	virtual bool IsScreencast() const = 0;
	bool is_screencast() const override { return IsScreencast(); }
	absl::optional<bool> needs_denoising() const override { return false; }
	bool remote() const override { return false; }
	webrtc::MediaSourceInterface::SourceState state() const override;
	
	// If true, run video adaptation. By default, video adaptation is enabled
	// and users must call video_adapter()->OnOutputFormatRequest()
	// to receive frames.
	bool enable_video_adapter() const { return enable_video_adapter_; }
	void set_enable_video_adapter(bool enable_video_adapter) {
		enable_video_adapter_ = enable_video_adapter;
	}
	
	virtual bool adaptVideoFormat(const VideoFormat& format) = 0;
	
protected:
	RTC_DISALLOW_COPY_AND_ASSIGN(VideoSource);
	
	webrtc::MediaSourceInterface::SourceState state_;
	bool enable_video_adapter_;
};

#include <mutex>
#include <thread>
#include "rtc_base/ref_counted_object.h"
#include "rtc_base/thread.h"
#include "modules/desktop_capture/desktop_capturer.h"
#include "VideoSource.h"

namespace webrtc {
  class DesktopFrame;
}  // namespace webrtc

const int kDefaultDpi = 96;
// This class controls the capture of video frames from the desktop and is used
// to construct a my_VideoSource as part of the webrtc PeerConnection API.
// DesktopCapturerAdapter acts as an adapter between webrtc::DesktopCapturer
// and the cricket::VideoCapturer interface, which it implements. It is used
// to construct a cricket::VideoSource for a PeerConnection, to capture frames
// of the desktop. As indicated in the base implementation, Start() and Stop()
// should be called on the same thread.
class my_DesktopSourceAdapter : public my_VideoSource, public webrtc::DesktopCapturer::Callback
{
public:
  my_DesktopSourceAdapter(bool isScreenCapture);
  virtual ~my_DesktopSourceAdapter();
  webrtc::MediaSourceInterface::SourceState Start(const webrtc::VideoCaptureCapability& not_used) override;
  void Stop() override;
  bool IsRunning() override;
  bool IsScreencast() const override { return true; };
  void setSource(uint32_t id);
  bool adaptVideoFormat(const VideoFormat& format) override {
    RTC_LOG(INFO) << "DesktopSourceAdapter::adaptVideoFormat Not implemented yet.";
    return false;
  }
private:
  // webrtc::DesktopCapturer::Callback implementation.
  void OnCaptureResult(webrtc::DesktopCapturer::Result result, std::unique_ptr<webrtc::DesktopFrame> frame) override;
  // Kicks off the next frame capture using |desktop_capturer_|. The captured frame will be passed to OnCaptureCompleted().
  void CaptureNextFrame();
  void OnFrame(const webrtc::VideoFrame& frame) override;
  // This is where the capture is actually happening
  // the timing of the capture (to achieve 30fps) is
  // also handled there.
  bool enable_video_adapter() const { return enable_video_adapter_; }
  void set_enable_video_adapter(bool enable_video_adapter) {
    enable_video_adapter_ = enable_video_adapter;
  }
  bool enable_video_adapter_;
  std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer_;
  volatile bool capture_pending_ = false;
  volatile bool running = false;
  std::mutex  mutex;
  std::thread timer;
  std::condition_variable condition;
  rtc::Thread* desktopCapturerThread;
  void SignalFrameCapturedOnStartThread(const webrtc::VideoFrame& frame);
};


On Tue, Mar 3, 2020 at 10:14 AM Tudor Zaharia <tudor....@gmail.com> wrote:
Is there an example on how to feed libwebrtc with custom raw video frames?

I have a custom software that provides me with a buffer where the raw frame is placed and I would like to feed this through the libwebrtc (such that I just forward the raw frame and libwebrtc does the encoding, sending etc.)

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/f4d98b48-a30d-48a9-b41c-0328c11081b5%40googlegroups.com.


--
Alex. Gouaillard, PhD, PhD, MBA
------------------------------------------------------------------------------------
President - CoSMo Software Consulting, Singapore
------------------------------------------------------------------------------------

Tudor Zaharia

unread,
Mar 4, 2020, 2:21:55 AM3/4/20
to discuss-webrtc
Thanks!
To unsubscribe from this group and stop receiving emails from it, send an email to discuss...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages