gstreamermm  1.8.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>
// 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
{
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
main_loop->quit();
break;
// Handle state changed message - print details
{
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
// Create 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
main_loop = Glib::MainLoop::create();
main_loop->run();
// Stop playing the pipeline
return 0;
}