簡體   English   中英

自定義視頻采集原生webrtc

[英]Custom video capture native webrtc

根據 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);

在 conductor.cc 的 AddStreams() 事件中,我通過以下代碼創建了一個視頻源:

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

我的視頻無法在瀏覽器中播放。 我做錯了什么?

我使用了基類 AdaptedVideoTrackSource 並創建了一個方法 FrameCaptured,它是從我的線程中調用的,在此方法中我調用了方法 OnFrame。 它工作正常!

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

 void StreamSource::OnFrameCaptured(const webrtc::VideoFrame& frame) 
 {
  OnFrame(frame);
 }

詳細說明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);

我不是專家,但我自己做一個項目並沿途實施一些東西。隨時糾正我或添加到此代碼中。

我在谷歌群組中得到答案

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。 如果您找到其他好方法,請分享。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM