从这个例子还可以看出来,使用QtGstreamer的方法很简单,首先聚合一个QGst::Ui::VideoWidget。这是一个QtWidget,可以作为普通的组件在Qt界面显示,另一方面,它可以绑定一个普通的sink,例如屏幕上的xvideosink,这样pipeline播放的结果就会显示在这个组件里,而不是其他的窗口。从官方文档(http://gstreamer.freedesktop.org/data/doc/gstreamer/head/qt-gstreamer/html/classQGst_1_1Ui_1_1VideoWidget.html)可以看到,绑定的方法有两种,
void setVideoSink (const ElementPtr &sink) void watchPipeline (const PipelinePtr &pipeline)对应的释放方法为:
void releaseVideoSink () void stopPipelineWatch ()除了player这个例子之外还有一个叫做recorder的例子,界面过于简单,不能预览,使用不便,所以要重新写一个简单的recorder。recorder的pipeline自然不能用playbin2来搭建。在例子用到的pipeline是:
录制的pipeline为:
queue ! xvimagesinkplayer的源代码在QtGstreamer源码的例子里面会有,我们不改动。Recorder的源码贴在下面:
#include "recorder.h" #include <QGst/ElementFactory> Recorder::Recorder(QWidget *parent) : QGst::Ui::VideoWidget(parent) { camera_src = QGst::ElementFactory::make("autovideosrc"); tee = QGst::ElementFactory::make("tee"); queue0 = QGst::ElementFactory::make("queue"); queue1 = QGst::ElementFactory::make("queue"); ffmpegcolorspace = QGst::ElementFactory::make("ffmpegcolorspace"); theoraenc = QGst::ElementFactory::make("theoraenc"); oggmux = QGst::ElementFactory::make("oggmux"); filesink = QGst::ElementFactory::make("filesink"); x_sink = QGst::ElementFactory::make("xvimagesink"); m_pipeline = QGst::Pipeline::create(); m_pipeline->add(camera_src, tee, queue0, x_sink, queue1, ffmpegcolorspace, theoraenc, oggmux, filesink); camera_src->link(tee); QGst::Element::linkMany(tee, queue0, x_sink); QGst::Element::linkMany(tee, queue1, ffmpegcolorspace, theoraenc, oggmux, filesink); } Recorder::~Recorder() { if (m_pipeline) { m_pipeline->setState(QGst::StateNull); releaseVideoSink(); } } void Recorder::preview() { m_pipeline->setState(QGst::StateNull); releaseVideoSink(); m_pipeline->remove(queue1); m_pipeline->remove(ffmpegcolorspace); m_pipeline->remove(theoraenc); m_pipeline->remove(oggmux); m_pipeline->remove(filesink); setVideoSink(x_sink); m_pipeline->setState(QGst::StatePlaying); } void Recorder::record(const QString &uri) { m_pipeline->setState(QGst::StateNull); releaseVideoSink(); filesink->setProperty("location", uri); m_pipeline->add(queue1, ffmpegcolorspace, theoraenc, oggmux, filesink); // camera_src->link(tee); // QGst::Element::linkMany(tee, queue0, x_sink); QGst::Element::linkMany(tee, queue1, ffmpegcolorspace, theoraenc, oggmux, filesink); setVideoSink(x_sink); m_pipeline->setState(QGst::StatePlaying); }