Created
October 15, 2016 03:58
-
-
Save jerome-diver/b8fe7b1fe5dcac75e7b6bf7f02054e04 to your computer and use it in GitHub Desktop.
try to show a video from camera source... i failed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include "ui_mainwindow.h" | |
#include <QtX11Extras/QX11Info> | |
#include <QVBoxLayout> | |
#include <QGst/Ui/VideoWidget> | |
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { | |
ui->setupUi(this); | |
widgetVideo = new QGst::Ui::VideoWidget; | |
ui->videoVBOX->addWidget(widgetVideo); | |
widgetVideo->show(); | |
populateSourcesCombo(); | |
connect(ui->videoSourceComboBox, SIGNAL(currentIndexChanged(int)), | |
this, SLOT(videoSourceComboBox_currentIndexChanged(int))); | |
ui->videoSourceComboBox->setCurrentIndex(1); | |
} | |
MainWindow::~MainWindow() { | |
delete ui; | |
delete dp; | |
} | |
void MainWindow::populateSourcesCombo() { | |
QGst::ElementFactoryPtr ximagesrc = QGst::ElementFactory::find("ximagesrc"); | |
QGst::ElementFactoryPtr autovideosrc = QGst::ElementFactory::find("autovideosrc"); | |
QGst::ElementFactoryPtr v4l2src = QGst::ElementFactory::find("v4l2src"); | |
if (!ximagesrc) | |
qDebug() << "we don't have ximagesrc, disable the choice to use it for screen"; | |
else | |
ui->videoSourceComboBox->addItem("Screen"); | |
if (!autovideosrc) | |
qDebug() << "we don't have autovideosrc, disable the choice to use it"; | |
else | |
ui->videoSourceComboBox->addItem("Camera with autovideosrc"); | |
if (!v4l2src) | |
qDebug() << "we don't have v4l2src, disable the choice to use it"; | |
else | |
ui->videoSourceComboBox->addItem("Camera with v4linux2"); | |
} | |
void MainWindow::videoSourceComboBox_currentIndexChanged(int index) { | |
pipeline = QGst::Pipeline::create("viewer"); | |
if (!pipeline) | |
QMessageBox::warning(this, "Failed", "no pipeline created"); | |
QGst::BinPtr audioSrcBin = createAudioSrcBin(); | |
QGst::BinPtr videoSrcBin = createVideoSrcBin(); | |
if (!videoSrcBin) | |
QMessageBox::warning(this, "Failed", "no BIN for video source selected"); | |
QGst::ElementPtr mux = QGst::ElementFactory::make("oggmux"); | |
QGst::ElementPtr sink = QGst::ElementFactory::make("qt5videosink"); | |
if (!sink) | |
QMessageBox::warning(this, "Failed", "no qt5videosink"); | |
if (!audioSrcBin || !videoSrcBin || !mux || !sink) { | |
QMessageBox::critical(this, tr("Error"), tr("One or more elements could not be created. " | |
"Verify that you have all the necessary element plugins installed.")); | |
return; } | |
// sink->setProperty("location", dirToRec); | |
// m_pipeline = QGst::Pipeline::create(); | |
pipeline->add(audioSrcBin, videoSrcBin, mux, sink); | |
//link elements | |
QGst::PadPtr audioSinkPad = mux->getRequestPad("audio_%u"); | |
qDebug() << "audio sink pad = " << audioSinkPad->name(); | |
QGst::PadPtr audioSrcPad = audioSrcBin->getStaticPad("src"); | |
qDebug() << "audio source pad = " << audioSrcPad->name(); | |
if(!audioSrcPad->canLink(audioSinkPad)) | |
QMessageBox::warning(this, "Failed", "can not link pads for audio"); | |
else | |
audioSrcPad->link(audioSinkPad); | |
QGst::PadPtr videoSinkPad = mux->getRequestPad("video_%u"); | |
qDebug() << "video sink pad = " << videoSinkPad->name(); | |
QGst::PadPtr videoSrcPad = videoSrcBin->getStaticPad("src"); | |
qDebug() << "video source pad = " << videoSrcPad->name(); | |
if(!videoSrcPad->canLink(videoSinkPad)) | |
QMessageBox::warning(this, "Failed", "can not link pads for video"); | |
else | |
videoSrcPad->link(videoSinkPad); | |
if(!mux->link(sink)) | |
QMessageBox::warning(this, "Failed", "can not link pads for video output"); | |
pipeline->bus()->addSignalWatch(); // connect the bus | |
QGlib::connect(pipeline->bus(), "message", this, &MainWindow::onBusMessage); | |
ui->record->setText(tr("Stop recording")); | |
//link elements | |
widgetVideo->setVideoSink(sink); | |
pipeline->setState(QGst::StatePlaying); | |
} | |
QGst::BinPtr MainWindow::createAudioSrcBin() { | |
QGst::BinPtr audioBin; | |
try { | |
audioBin = QGst::Bin::fromDescription("autoaudiosrc name=\"audiosrc\" ! audioconvert ! " | |
"audioresample ! audiorate ! speexenc ! queue"); | |
} catch (const QGlib::Error & error) { | |
qCritical() << "Failed to create audio source bin:" << error; | |
return QGst::BinPtr(); | |
} | |
QGst::ElementPtr src = audioBin->getElementByName("audiosrc"); | |
//autoaudiosrc creates the actual source in the READY state | |
src->setState(QGst::StateReady); | |
return audioBin; | |
} | |
QGst::BinPtr MainWindow::createVideoSrcBin() { | |
QGst::BinPtr videoBin; | |
try { | |
switch (ui->videoSourceComboBox->currentIndex()) { | |
case 0: //screencast | |
videoBin = QGst::Bin::fromDescription("ximagesrc name=\"videosrc\" ! " | |
"videoconvert ! theoraenc ! queue"); | |
videoBin->getElementByName("videosrc")->setProperty("screen-num", | |
ui->displayNumSpinBox->value()); | |
break; | |
case 1: //camera | |
videoBin = QGst::Bin::fromDescription("autovideosrc name=\"videosrc\" !" | |
"videoconvert ! theoraenc ! queue"); | |
videoBin->getElementByName("videosrc")->setProperty("camera",0); | |
break; | |
case 2: | |
videoBin = QGst::Bin::fromDescription("v4l2src name=\"videosrc\" " | |
" device=\"/dev/video0\" ! " | |
"videoconvert ! theoraenc ! queue"); | |
break; } | |
} catch (const QGlib::Error & error) { | |
qCritical() << "Failed to create video source bin:" << error; | |
return QGst::BinPtr(); | |
} | |
QGst::ElementPtr src = videoBin->getElementByName("videosrc"); | |
//autoaudiosrc creates the actual source in the READY state | |
src->setState(QGst::StateReady); | |
return videoBin; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment