讓 WebRTC 使用外部的音視頻轉碼器

來源:互聯網
上載者:User

標籤:nec   param   ports   mode   自己   factory   ecs   send   actor   

WebRTC 支援使用自己的轉碼器(限 native 開發),音頻,視頻都可以。這裡以視頻編碼為例來分析下 WebRTC 中相應的源碼。

CreatePeerConnectionFactory

在 webrtc/api/peerconnectioninterface.h 中有個方法 CreatePeerConnectionFactory,原型如下:

inline rtc::scoped_refptr<PeerConnectionFactoryInterface> CreatePeerConnectionFactory(    rtc::Thread* worker_and_network_thread,    rtc::Thread* signaling_thread,    AudioDeviceModule* default_adm,    rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory,    rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory,    cricket::WebRtcVideoEncoderFactory* video_encoder_factory,    cricket::WebRtcVideoDecoderFactory* video_decoder_factory);

可以看到, CreatePeerConnectionFactory 最後四個參數,允許我們提供自己的編解碼工廠。這樣我們就可以實現自己的 factory ,在 factory 中建立自己的 encoder(decoder),WebRTC 內部就會使用我們的 encoder(decoder)。

程式碼片段類似下面:

    rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> factory = webrtc::CreatePeerConnectionFactory(            current_thread, current_thread,             your_adm,             your_audio_encoder_factory,             your_audio_decoder_factory,            your_video_encoder_factory,            your_video_decoder_factory);    rtc::scoped_refptr<PeerConnectionInterface> peer_connection = factory->CreatePeerConnection(            config, constraints, NULL, NULL, your_observer);
WebRtcVideoEncoderFactory

這個介面在 webrtcvideoencoderfactory.h(webrtc/media/engine) 中定義:

class WebRtcVideoEncoderFactory { public:  // This VideoCodec class is deprecated. Use cricket::VideoCodec directly  // instead and the corresponding factory function. See  // http://crbug/webrtc/6402 for more info.  struct VideoCodec {    webrtc::VideoCodecType type;    std::string name;    VideoCodec(webrtc::VideoCodecType t, const std::string& nm)        : type(t), name(nm) {}    VideoCodec(webrtc::VideoCodecType t,               const std::string& nm,               int w,               int h,               int fr)        : type(t), name(nm) {}  };  virtual ~WebRtcVideoEncoderFactory() {}  // TODO(magjed): Make these functions pure virtual when every external client  // implements it. See http://crbug/webrtc/6402 for more info.  // Caller takes the ownership of the returned object and it should be released  // by calling DestroyVideoEncoder().  virtual webrtc::VideoEncoder* CreateVideoEncoder(      const cricket::VideoCodec& codec);  // Returns a list of supported codecs in order of preference.  virtual const std::vector<cricket::VideoCodec>& supported_codecs() const;  // Caller takes the ownership of the returned object and it should be released  // by calling DestroyVideoEncoder().  // Deprecated: Use cricket::VideoCodec as argument instead. See  // http://crbug/webrtc/6402 for more info.  virtual webrtc::VideoEncoder* CreateVideoEncoder(webrtc::VideoCodecType type);  // Returns a list of supported codecs in order of preference.  // Deprecated: Return cricket::VideoCodecs instead. See  // http://crbug/webrtc/6402 for more info.  virtual const std::vector<VideoCodec>& codecs() const;  // Returns true if encoders created by this factory of the given codec type  // will use internal camera sources, meaning that they don‘t require/expect  // frames to be delivered via webrtc::VideoEncoder::Encode. This flag is used  // as the internal_source parameter to  // webrtc::ViEExternalCodec::RegisterExternalSendCodec.  virtual bool EncoderTypeHasInternalSource(webrtc::VideoCodecType type) const {    return false;  }  virtual void DestroyVideoEncoder(webrtc::VideoEncoder* encoder) = 0; private:  // TODO(magjed): Remove these. They are necessary in order to return a const  // reference to a std::vector in the default implementations of codecs() and  // supported_codecs(). See http://crbug/webrtc/6402 for more info.  mutable std::vector<VideoCodec> encoder_codecs_;  mutable std::vector<cricket::VideoCodec> codecs_;};

實現 WebRtcVideoEncoderFactory 介面,傳遞給 CreatePeerConnectionFactory 即可。可以參考 webrtc/media/engine 目錄下的 internalencoderfactory.h 和 internalencoderfactory.cpp 。

外部轉碼器與內部的關係

以視頻編碼為例, webrtc/media/engine/webrtcvideoengine2.cc 中有段代碼可以說明外部視頻編碼器和內部視頻編碼器之間的關係:

WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoderWebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(    const VideoCodec& codec) {  RTC_DCHECK_RUN_ON(&thread_checker_);  // Do not re-create encoders of the same type.  if (codec == allocated_encoder_.codec &&      allocated_encoder_.encoder != nullptr) {    return allocated_encoder_;  }  // Try creating external encoder.  if (external_encoder_factory_ != nullptr &&      FindMatchingCodec(external_encoder_factory_->supported_codecs(), codec)) {    webrtc::VideoEncoder* encoder =        external_encoder_factory_->CreateVideoEncoder(codec);    if (encoder != nullptr)      return AllocatedEncoder(encoder, codec, true /* is_external */);  }  // Try creating internal encoder.  if (FindMatchingCodec(internal_encoder_factory_->supported_codecs(), codec)) {    if (parameters_.encoder_config.content_type ==            webrtc::VideoEncoderConfig::ContentType::kScreen &&        parameters_.conference_mode && UseSimulcastScreenshare()) {      // TODO(sprang): Remove this adapter once libvpx supports simulcast with      // same-resolution substreams.      WebRtcSimulcastEncoderFactory adapter_factory(          internal_encoder_factory_.get());      return AllocatedEncoder(adapter_factory.CreateVideoEncoder(codec), codec,                              false /* is_external */);    }    return AllocatedEncoder(        internal_encoder_factory_->CreateVideoEncoder(codec), codec,        false /* is_external */);  }  // This shouldn‘t happen, we should not be trying to create something we don‘t  // support.  RTC_NOTREACHED();  return AllocatedEncoder(NULL, cricket::VideoCodec(), false);}

可以看到,這裡會先嘗試調用 external_encoder_factory_ 建立外部的編碼器,如果建立成功,用外部的,如果失敗,再找內部的。

這裡的 external_encoder_factory_ ,就是我們調用 CreatePeerConnectionFactory 時傳入的那個 encoder factory 執行個體。

相關閱讀:

  • WebRTC學習資料大全
  • Ubuntu 14.04下編譯WebRTC
  • WebRTC源碼中turnserver的使用方法
  • 開啟 WebRTC 的日誌(native api)
  • 讓WebRTC支援H264編解碼
  • WebRTC編譯系統之gn和ninja
  • WebRTC編譯系統之gn files
  • How to enable TRACE_EVENT in WebRTC codes

讓 WebRTC 使用外部的音視頻轉碼器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.