【问题标题】:Custom video capture native webrtc自定义视频采集原生 webrtc
【发布时间】:2018-03-06 12:51:36
【问题描述】:

根据 webrtc 在 google cricket::VideoCapture 的讨论组主题,很快将被弃用。要自定义视频源,我们应该实现 VideoTrackSourceInterface。我尝试实现接口并没有工作。当我有一个框架时,我实现了接口,然后调用事件 OnFrame(const webrtc::VideoFrame& frame) 如下:

void StreamSource::OnFrame(const webrtc::VideoFrame& frame)
{
 rtc::scoped_refptr<webrtc::VideoFrameBuffer buffer(frame.video_frame_buffer());
 broadcaster_.OnFrame(frame);

} 在事件 AddStreams() 的conductor.cc 中,我通过以下代码创建了一个视频源:

rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
peer_connection_factory_->CreateVideoTrack( kVideoLabel,new mystream::StreamSource()));

我的视频无法在浏览器中播放。我做错了什么?

【问题讨论】:

    标签: c++ webrtc


    【解决方案1】:

    我使用了基类 AdaptedVideoTrackSource 并创建了一个 FrameCaptured 方法,它是从我的线程中调用的,在这个方法中我调用了 OnFrame 方法。很好用!!!

     class StreamSource : public rtc::AdaptedVideoTrackSource
     {
       void OnFrameCaptured(const webrtc::VideoFrame& frame);
     }
    
     void StreamSource::OnFrameCaptured(const webrtc::VideoFrame& frame) 
     {
      OnFrame(frame);
     }
    

    【讨论】:

    • 你能详细说明你做了什么让它工作吗?或者你是如何到达那里的,也许是对你有帮助的东西?我有同样的问题,我不知道从哪里开始。
    • 我也在尝试在 C++ 中实现相同的目标。我对此很陌生,并且为此苦苦挣扎了一段时间。如果你不介意,可以请你给我一个样品吗?
    【解决方案2】:

    我在google group得到答案

    VideoFrame 有枚举类型,如:

    class VideoFrameBuffer : public rtc::RefCountInterface {
     public:
      // New frame buffer types will be added conservatively when there is an
      // opportunity to optimize the path between some pair of video source and
      // video sink.
      enum class Type {
        kNative,
        kI420,
        kI420A,
        kI444,
        kI010,
      };
     ...
     }

    然后,在创建 Videoframe 时,将类型设置为 kNative。 如果你发现其他好方法,分享它。

    【讨论】:

      【解决方案3】:

      详细说明user1658843的回答: 创建一个自定义视频源类并定义所有抽象方法。这是一个例子:

      class CustomVideoSource : public rtc::AdaptedVideoTrackSource  {
      
      public:
          void OnFrameCaptured(const webrtc::VideoFrame& frame);
          void AddRef() const override;
          rtc::RefCountReleaseStatus Release() const override;
          SourceState state() const override;
          bool remote() const override;
          bool is_screencast() const override;
          absl::optional<bool> needs_denoising() const override;
      private:
          mutable volatile int ref_count_;
      };
      

      以及实现:

      void CustomVideoSource::OnFrameCaptured(const webrtc::VideoFrame& frame) {
        OnFrame(frame);
      }
      
      void CustomVideoSource::AddRef() const {
        rtc::AtomicOps::Increment(&ref_count_);
      }
      
      rtc::RefCountReleaseStatus CustomVideoSource::Release() const {
        const int count = rtc::AtomicOps::Decrement(&ref_count_);
        if (count == 0) {
          return rtc::RefCountReleaseStatus::kDroppedLastRef;
        }
        return rtc::RefCountReleaseStatus::kOtherRefsRemained;
      }
      
      webrtc::MediaSourceInterface::SourceState CustomVideoSource::state() const {
        return kLive;
      }
      
      bool CustomVideoSource::remote() const {
        return false;
      }
      
      bool CustomVideoSource::is_screencast() const {
        return false;
      }
      
      absl::optional<bool> CustomVideoSource::needs_denoising() const {
        return false;
      

      请记住,这只是为了让它工作,而不是完整的实现。您应该正确实现抽象方法,而不是返回硬编码值。 要发送帧,只需使用该帧调用 OnFrameCaptured。

      添加流:

      custom_source= new rtc::RefCountedObject<CustomVideoSource>();
      // create video track from our custom source
      rtc::scoped_refptr<webrtc::VideoTrackInterface> custom_video_track(
      g_peer_connection_factory->CreateVideoTrack( kVideoLabel, custom_source));
      //add to stream
      stream->AddTrack(custom_video_track);
      

      我不是专家,但我自己做一个项目并在此过程中实施一些东西。请随时纠正我或添加到此代码中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-24
        • 2021-10-19
        • 1970-01-01
        • 2021-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多