gstreamermm  1.10.0
basics/dynamic_pads.cc

A gstreamermm dynamic Gst::Pad example.

/*
* This example presents basic usage of dynamics Gst::Pad objects.
*/
#include <gstreamermm.h>
#include <glibmm/main.h>
#include <iostream>
int main(int argc, char *argv[])
{
Gst::init(argc, argv);
// Create pipeline
// Create elements
decodebin = Gst::ElementFactory::create_element("decodebin", "decoder"),
sink = Gst::ElementFactory::create_element("autovideosink", "videosink");
// Add elements to a pipeline
try
{
pipeline->add(source)->add(decodebin)->add(sink);
}
catch (const std::runtime_error& ex)
{
std::cerr << "Exception while adding: " << ex.what() << std::endl;
return 1;
}
// Link elements
try
{
// We can't link decodebin with sink, because decodebin
// doesn't have any src pad on start up.
source->link(decodebin);
}
catch (const std::runtime_error& ex)
{
std::cerr << "Exception while linking: " << ex.what() << std::endl;
}
// Handle messages posted on bus
pipeline->get_bus()->add_watch([main_loop] (const Glib::RefPtr<Gst::Bus>&,
const Glib::RefPtr<Gst::Message>& message) {
switch (message->get_message_type())
{
case Gst::MESSAGE_EOS:
case Gst::MESSAGE_ERROR:
main_loop->quit();
break;
default:
break;
}
return true;
});
// Listen for newly created pads
decodebin->signal_pad_added().connect([decodebin, sink] (const Glib::RefPtr<Gst::Pad>& pad) {
std::cout << "New pad added to " << decodebin->get_name() << std::endl;
std::cout << "Pad name: " << pad->get_name() << std::endl;
Gst::PadLinkReturn ret = pad->link(sink->get_static_pad("sink"));
if (ret != Gst::PAD_LINK_OK)
{
std::cout << "Cannot link pads. Error: " << ret << std::endl;
}
else
{
std::cout << "Pads linked correctly!" << std::endl;
}
});
// Start the pipeline
pipeline->set_state(Gst::STATE_PLAYING);
main_loop->run();
pipeline->set_state(Gst::STATE_NULL);
return 0;
}
iostream
Gst::STATE_PLAYING
@ STATE_PLAYING
Definition: enums.h:99
std::cout
ostream cout
std::cerr
ostream cerr
Gst::PadLinkReturn
PadLinkReturn
Definition: pad.h:413
Gst::STATE_NULL
@ STATE_NULL
Definition: enums.h:96
Gst::ElementFactory::create_element
static Glib::RefPtr< Gst::Element > create_element(const Glib::ustring &factory_name, const Glib::ustring &name)
Create a new element of the type defined by the given element factory.
Glib::RefPtr
Gst::PAD_LINK_OK
@ PAD_LINK_OK
Definition: pad.h:414
Gst::init
void init(int &argc, char **&argv)
Initializes gstreamermm parsing command line arguments.
Gst::Pipeline::create
static Glib::RefPtr< Pipeline > create()
Create a new pipeline with a unique generic name.
std::endl
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
std::runtime_error::what
virtual const char * what() const _GLIBCXX_TXN_SAFE_DYN noexcept
std::runtime_error
Glib::MainLoop::create
static Glib::RefPtr< MainLoop > create(bool is_running=false)