| 1 | /* |
| 2 | * This example presents basic usage of the Gst::Bin class. |
| 3 | */ |
| 4 | #include <gstreamermm.h> |
| 5 | |
| 6 | #include <iostream> |
| 7 | |
| 8 | int main(int argc, char *argv[]) |
| 9 | { |
| 10 | Gst::init(argc, argv); |
| 11 | |
| 12 | // Create some elements |
| 13 | Glib::RefPtr<Gst::Element> fakesrc = Gst::ElementFactory::create_element("fakesrc", "source"); |
| 14 | Glib::RefPtr<Gst::Element> fakesink = Gst::ElementFactory::create_element("fakesink", "sink"); |
| 15 | |
| 16 | // Create empty bin |
| 17 | Glib::RefPtr<Gst::Bin> bin = Gst::Bin::create("my-bin"); |
| 18 | |
| 19 | // Add elements to a bin |
| 20 | try |
| 21 | { |
| 22 | bin->add(fakesrc)->add(fakesink); |
| 23 | } |
| 24 | catch (const std::runtime_error& ex) |
| 25 | { |
| 26 | std::cerr << "Exception while adding: " << ex.what() << std::endl; |
| 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | // Some of the elements are actually a bins, so we can cast |
| 31 | // them. |
| 32 | Glib::RefPtr<Gst::Element> playbin = Gst::ElementFactory::create_element("playbin"); |
| 33 | Glib::RefPtr<Gst::Bin> playbin_bin = playbin_bin.cast_static(playbin); |
| 34 | |
| 35 | if (!playbin_bin) |
| 36 | { |
| 37 | std::cerr << "Cannot find playbin element" << std::endl; |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | // We can also iterate through the elements in the bin |
| 43 | Gst::Iterator<Gst::Element> it = playbin_bin->iterate_recurse(); |
| 44 | std::cout << "List of elements in the container: " << std::endl; |
| 45 | while (it.next()) |
| 46 | { |
| 47 | std::cout << " * " << it->get_name() << std::endl; |
| 48 | } |
| 49 | |
| 50 | // We can also connect to signals emitted by bin, e.g. when |
| 51 | // the element has been removed. |
| 52 | bin->signal_element_removed().connect( |
| 53 | [] (const Glib::RefPtr<Gst::Element>& removed_element) |
| 54 | { |
| 55 | std::cout << "Element '" << removed_element->get_name() |
| 56 | << "' has been removed from the bin" << std::endl; |
| 57 | } |
| 58 | ); |
| 59 | |
| 60 | bin->remove(fakesink); |
| 61 | |
| 62 | std::cout << "returning from application..." << std::endl; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 |