gstreamermm: basics/bus.cc

gstreamermm 1.10.0

basics/bus.cc

A gstreamermm Gst::Bus example.

/*
* This example presents basic usage of the Gst::Bus class.
*/
#include <gstreamermm.h>
#include <glibmm/main.h>
#include <iostream>
Glib::RefPtr<Glib::MainLoop> main_loop;
// Message watch function
bool bus_message_watch(const Glib::RefPtr<Gst::Bus>& /* bus */, const Glib::RefPtr<Gst::Message>& message)
{
// Print type of the message posted on the bus, and the source object name.
std::cout << "Got message of type " << Gst::Enums::get_name(message->get_message_type()) << std::endl;
if (message->get_source())
{
std::cout << "Source object: " << message->get_source()->get_name() << std::endl;
}
switch (message->get_message_type())
{
// Handle ERROR message - print error and debug information
case Gst::MESSAGE_ERROR:
{
auto error_msg = Glib::RefPtr<Gst::MessageError>::cast_static(message);
std::cout << "Error: " << error_msg->parse_error().what() << std::endl;
std::cout << "Debug: " << error_msg->parse_debug() << std::endl;
break;
}
// Handle EOS message - quit the loop
case Gst::MESSAGE_EOS:
main_loop->quit();
break;
// Handle state changed message - print details
case Gst::MESSAGE_STATE_CHANGED:
{
auto state_changed_msg = Glib::RefPtr<Gst::MessageStateChanged>::cast_static(message);
std::cout << "Old state: " << Gst::Enums::get_name(state_changed_msg->parse_old_state()) << std::endl;
std::cout << "New state: " << Gst::Enums::get_name(state_changed_msg->parse_new_state()) << std::endl;
break;
}
// Unhanlded messages
default:
break;
}
return true;
}
int main(int argc, char *argv[])
{
Gst::init(argc, argv);
// Create some elements
Glib::RefPtr<Gst::Element> source = Gst::ElementFactory::create_element("videotestsrc", "source");
Glib::RefPtr<Gst::Element> sink = Gst::ElementFactory::create_element("autovideosink", "sink");
// Create pipeline
Glib::RefPtr<Gst::Pipeline> pipeline = Gst::Pipeline::create("my-pipeline");
// Add elements to a pipeline
try
{
pipeline->add(source)->add(sink);
}
catch (const std::runtime_error& ex)
{
std::cerr << "Exception while adding: " << ex. what() << std::endl;
return 1;
}
// Link elements
try
{
source->link(sink);
}
catch (const std::runtime_error& ex)
{
std::cerr << "Exception while linking: " << ex.what() << std::endl;
}
// Get a bus object of the pipeline
Glib::RefPtr<Gst::Bus> bus = pipeline->get_bus();
// Add watch to the bus
bus->add_watch(sigc::ptr_fun(bus_message_watch));
// Start pipeline
pipeline->set_state(Gst::STATE_PLAYING);
main_loop = Glib::MainLoop::create();
main_loop->run();
// Stop playing the pipeline
pipeline->set_state(Gst::STATE_NULL);
return 0;
}
iostream
Gst::MESSAGE_EOS
@ MESSAGE_EOS
Definition: message.h:223
sigc::ptr_fun
pointer_functor0< T_return > ptr_fun(T_return(*_A_func)())
Gst::STATE_PLAYING
@ STATE_PLAYING
Definition: enums.h:99
std::cout
ostream cout
std::cerr
ostream cerr
Gst::Enums::get_name
Glib::ustring get_name(State state)
Gets a string representing the given state.
Gst::STATE_NULL
@ STATE_NULL
Definition: enums.h:96
Glib::RefPtr::cast_static
static RefPtr cast_static(const RefPtr< T_CastFrom > &src) noexcept
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.
Gst::MESSAGE_STATE_CHANGED
@ MESSAGE_STATE_CHANGED
Definition: message.h:229
Glib::RefPtr
Gst::MESSAGE_ERROR
@ MESSAGE_ERROR
Definition: message.h:224
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)