美文网首页
webrtc之封装视频源

webrtc之封装视频源

作者: hijiang | 来源:发表于2019-07-19 13:55 被阅读0次

webrtc视频设备采集,需要先创建VideoCapturer

cricket::WebRtcVideoDeviceCapturerFactory factory;
std::unique_ptr<cricket::VideoCapturer> capturer;//这里这个轨道用来采集,不输入,输入创建一个新的ExternalCapturer;
capturer = factory.Create(cricket::Device(video_device->device_name, video_device->device_id));
if (!capturer) {
    return nullptr;
}

然后设置采集的限制:

MediaConstraints constraints;
constraints.SetMandatory(webrtc::MediaConstraintsInterface::kMinWidth, width-2);
constraints.SetMandatory(webrtc::MediaConstraintsInterface::kMaxWidth, width);
constraints.SetMandatory(webrtc::MediaConstraintsInterface::kMinHeight, height-2);
constraints.SetMandatory(webrtc::MediaConstraintsInterface::kMaxHeight, height);
constraints.SetMandatory(webrtc::MediaConstraintsInterface::kMinFrameRate, frame_rate);
constraints.SetMandatory(webrtc::MediaConstraintsInterface::kMaxFrameRate, frame_rate);

然后使用PeerConnectionFactory的CreateVideoSource
创建视频源:

rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_connection_factory = XXX::PeerConnectionManager::getInstance()->getPeerConnectionFactory();

接下来,设置视频源的数据回调:

std::shared_ptr<XXX::VideoSource> video_source = std::make_shared<XXX::VideoSource>();
video_source->video_source_ = peer_connection_factory->CreateVideoSource(std::move(capturer), &constraints);
video_source->video_source_->AddOrUpdateSink(video_source.get(), rtc::VideoSinkWants());

这里,我们在webrtc的VideoSource对象外面再包了一层自己的video_source,是为了对外屏蔽webrtc的头文件。

对视频源的内部封装:

class VideoSource : public rtc::VideoSinkInterface<webrtc::VideoFrame>
{
public:
    static std::shared_ptr<VideoSource> createVideoDeviceSource(std::shared_ptr<XXX_PUBLIC::VideoDevice> video_device, 
                                                                    size_t width,
                                                                    size_t height,
                                                                    size_t frame_rate);
    VideoSource();
    virtual ~VideoSource();
    void OnFrame(const std::function<void(const XXXVideoFrame &video_frame)> &video_frame_cb);
    /*************VideoSinkInterface************/
    void OnFrame(const webrtc::VideoFrame& frame);
    void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame> *sink);
    void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame> * sink, const rtc::VideoSinkWants & wants);
protected:
    std::shared_ptr<std::function<void(const XXXVideoFrame &video_frame)>> video_frame_cb_;
    rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> video_source_;
    webrtc::VideoTrackSourceInterface *video_source_ptr;
    friend class ErizoStream;
    friend class XXXVideoSource;
};

对视频源的外部封装:

class XXXVideoSource {
public:
    static std::shared_ptr<XXXVideoSource> createVideoDeviceSource(
                                                    std::shared_ptr<XXX_PUBLIC::VideoDevice> video_device,
                                                    size_t width,
                                                    size_t height,
                                                    size_t frame_rate
                                                );
    xxxVideoSource();
    ~xxxVideoSource();
    void onVideoFrame(const std::function<void(const XXXVideoFrame &video_frame)> &cb);
public:
    std::shared_ptr<XXX::VideoSource> video_source_;

    friend class XXXStream;
    friend class Stream;
};

相关文章

网友评论

      本文标题:webrtc之封装视频源

      本文链接:https://www.haomeiwen.com/subject/wnwjlctx.html