| 1 | /* |
| 2 | * This example presents basic usage of the Gst::Bus class. |
| 3 | */ |
| 4 | #include <gstreamermm.h> |
| 5 | #include <glibmm/main.h> |
| 6 | |
| 7 | #include <iostream> |
| 8 | |
| 9 | Glib::RefPtr<Glib::MainLoop> main_loop; |
| 10 | |
| 11 | // Message watch function |
| 12 | bool bus_message_watch(const Glib::RefPtr<Gst::Bus>& /* bus */, const Glib::RefPtr<Gst::Message>& message) |
| 13 | { |
| 14 | // Print type of the message posted on the bus, and the source object name. |
| 15 | std::cout << "Got message of type " << Gst::Enums::get_name(message->get_message_type()) << std::endl; |
| 16 | |
| 17 | if (message->get_source()) |
| 18 | { |
| 19 | std::cout << "Source object: " << message->get_source()->get_name() << std::endl; |
| 20 | } |
| 21 | |
| 22 | switch (message->get_message_type()) |
| 23 | { |
| 24 | // Handle ERROR message - print error and debug information |
| 25 | case Gst::MESSAGE_ERROR: |
| 26 | { |
| 27 | auto error_msg = Glib::RefPtr<Gst::MessageError>::cast_static(message); |
| 28 | std::cout << "Error: " << error_msg->parse_error().what() << std::endl; |
| 29 | std::cout << "Debug: " << error_msg->parse_debug() << std::endl; |
| 30 | break; |
| 31 | } |
| 32 | // Handle EOS message - quit the loop |
| 33 | case Gst::MESSAGE_EOS: |
| 34 | main_loop->quit(); |
| 35 | break; |
| 36 | // Handle state changed message - print details |
| 37 | case Gst::MESSAGE_STATE_CHANGED: |
| 38 | { |
| 39 | auto state_changed_msg = Glib::RefPtr<Gst::MessageStateChanged>::cast_static(message); |
| 40 | std::cout << "Old state: " << Gst::Enums::get_name(state_changed_msg->parse_old_state()) << std::endl; |
| 41 | std::cout << "New state: " << Gst::Enums::get_name(state_changed_msg->parse_new_state()) << std::endl; |
| 42 | break; |
| 43 | } |
| 44 | // Unhanlded messages |
| 45 | default: |
| 46 | break; |
| 47 | } |
| 48 | |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | int main(int argc, char *argv[]) |
| 53 | { |
| 54 | Gst::init(argc, argv); |
| 55 | |
| 56 | // Create some elements |
| 57 | Glib::RefPtr<Gst::Element> source = Gst::ElementFactory::create_element("videotestsrc", "source"); |
| 58 | Glib::RefPtr<Gst::Element> sink = Gst::ElementFactory::create_element("autovideosink", "sink"); |
| 59 | |
| 60 | // Create pipeline |
| 61 | Glib::RefPtr<Gst::Pipeline> pipeline = Gst::Pipeline::create("my-pipeline"); |
| 62 | |
| 63 | // Add elements to a pipeline |
| 64 | try |
| 65 | { |
| 66 | pipeline->add(source)->add(sink); |
| 67 | } |
| 68 | catch (const std::runtime_error& ex) |
| 69 | { |
| 70 | std::cerr << "Exception while adding: " << ex.what() << std::endl; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | // Link elements |
| 75 | try |
| 76 | { |
| 77 | source->link(sink); |
| 78 | } |
| 79 | catch (const std::runtime_error& ex) |
| 80 | { |
| 81 | std::cerr << "Exception while linking: " << ex.what() << std::endl; |
| 82 | } |
| 83 | |
| 84 | // Get a bus object of the pipeline |
| 85 | Glib::RefPtr<Gst::Bus> bus = pipeline->get_bus(); |
| 86 | |
| 87 | // Add watch to the bus |
| 88 | bus->add_watch(sigc::ptr_fun(bus_message_watch)); |
| 89 | |
| 90 | // Start pipeline |
| 91 | pipeline->set_state(Gst::STATE_PLAYING); |
| 92 | |
| 93 | main_loop = Glib::MainLoop::create(); |
| 94 | main_loop->run(); |
| 95 | |
| 96 | // Stop playing the pipeline |
| 97 | pipeline->set_state(Gst::STATE_NULL); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 |