### PlayBin Media Player Example Source: https://context7.com/gnome/gstreamermm/llms.txt This C++ code snippet demonstrates how to use Gst::PlayBin to play a media file or URI. It includes setting up the main loop, handling bus messages for playback events, and managing the playback state. Ensure GStreamer and GStreamerMM are installed. ```cpp #include #include #include #include #include Glib::RefPtr mainloop; bool on_bus_message(const Glib::RefPtr&, const Glib::RefPtr& message) { switch (message->get_message_type()) { case Gst::MESSAGE_EOS: std::cout << "End of stream" << std::endl; mainloop->quit(); return false; case Gst::MESSAGE_ERROR: { auto error_msg = Glib::RefPtr::cast_static(message); if (error_msg) { std::cerr << "Error: " << error_msg->parse_error().what() << std::endl; } mainloop->quit(); return false; } default: break; } return true; } int main(int argc, char* argv[]) { Gst::init(argc, argv); if (argc < 2) { std::cout << "Usage: " << argv[0] << " " << std::endl; return 1; } // Create playbin element #ifndef GSTREAMERMM_DISABLE_DEPRECATED Glib::RefPtr playbin = Gst::PlayBin::create(); #else Glib::RefPtr playbin = Gst::ElementFactory::create_element("playbin"); #endif if (!playbin) { std::cerr << "Could not create playbin element" << std::endl; return 1; } // Convert filename to URI if needed Glib::ustring uri; if (gst_uri_is_valid(argv[1])) { uri = argv[1]; } else { uri = Glib::filename_to_uri(argv[1]); } // Set the URI property playbin->set_property("uri", uri); // Create main loop and setup bus watch mainloop = Glib::MainLoop::create(); Glib::RefPtr bus = playbin->get_bus(); bus->add_watch(sigc::ptr_fun(&on_bus_message)); // Start playback std::cout << "Playing: " << uri << std::endl; playbin->set_state(Gst::STATE_PLAYING); mainloop->run(); // Cleanup playbin->set_state(Gst::STATE_NULL); return 0; } ``` -------------------------------- ### TypeFind Media Detection Example Source: https://context7.com/gnome/gstreamermm/llms.txt Use the typefind element to identify media types by examining stream content. Connect to the 'have-type' signal to receive media type information. This example demonstrates setting up a pipeline with filesrc, typefind, and fakesink. ```cpp #include #include #include Glib::RefPtr mainloop; bool have_type = false; void on_typefind_have_type(guint probability, const Glib::RefPtr& caps) { have_type = true; std::cout << "Type found with probability: " << probability << std::endl; if (!caps || !caps->size()) { std::cerr << "Invalid caps" << std::endl; return; } Gst::Structure structure = caps->get_structure(0); std::cout << "MIME type: " << structure.get_name() << std::endl; // Iterate through structure fields structure.foreach([](const Glib::QueryQuark& id, const Glib::ValueBase& value) { gchar* str_value = g_strdup_value_contents(value.gobj()); std::cout << " " << Glib::ustring(id) << " = " << str_value << std::endl; g_free(str_value); return true; }); if (mainloop) { mainloop->quit(); } } int main(int argc, char* argv[]) { Gst::init(argc, argv); if (argc < 2) { std::cout << "Usage: " << argv[0] << " " << std::endl; return 1; } Glib::RefPtr pipeline = Gst::Pipeline::create("typefind"); #ifndef GSTREAMERMM_DISABLE_DEPRECATED Glib::RefPtr source = Gst::FileSrc::create(); source->property_location() = std::string(argv[1]); Glib::RefPtr sink = Gst::FakeSink::create(); Glib::RefPtr typefind = Gst::TypeFindElement::create(); typefind->signal_have_type().connect(sigc::ptr_fun(&on_typefind_have_type)); #else Glib::RefPtr source = Gst::ElementFactory::create_element("filesrc"); source->set_property("location", std::string(argv[1])); Glib::RefPtr sink = Gst::ElementFactory::create_element("fakesink"); Glib::RefPtr typefind = Gst::ElementFactory::create_element("typefind"); #endif // Add and link elements pipeline->add(source)->add(typefind)->add(sink); source->link(typefind)->link(sink); // Run pipeline mainloop = Glib::MainLoop::create(); pipeline->set_state(Gst::STATE_PLAYING); if (!have_type) { mainloop->run(); } pipeline->set_state(Gst::STATE_NULL); return 0; } ``` -------------------------------- ### Create and Configure a GStreamer Pipeline Source: https://context7.com/gnome/gstreamermm/llms.txt Demonstrates creating a GStreamer pipeline, adding video test source and auto video sink elements, setting properties, linking them, and accessing the bus and clock. Ensure GStreamer and GStreamerMM are installed. ```cpp #include #include int main(int argc, char* argv[]) { Gst::init(argc, argv); // Create a named pipeline Glib::RefPtr pipeline = Gst::Pipeline::create("my-pipeline"); // Create elements Glib::RefPtr source = Gst::ElementFactory::create_element("videotestsrc", "source"); Glib::RefPtr sink = Gst::ElementFactory::create_element("autovideosink", "sink"); if (!pipeline || !source || !sink) { std::cerr << "Failed to create elements" << std::endl; return 1; } // Set element properties source->set_property("pattern", 0); // SMPTE test pattern // Add elements to 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 together try { source->link(sink); } catch (const std::runtime_error& ex) { std::cerr << "Exception while linking: " << ex.what() << std::endl; return 1; } // Get the pipeline's bus for message handling Glib::RefPtr bus = pipeline->get_bus(); // Get/set pipeline clock Glib::RefPtr clock = pipeline->get_clock(); return 0; } ``` -------------------------------- ### GStreamerMM Tee Example with Request Pads Source: https://context7.com/gnome/gstreamermm/llms.txt This C++ code demonstrates using the tee element to split an audio and video stream from a file source. It utilizes request pads to dynamically create output pads for the tee element and links them to separate audio and video processing chains. Ensure you have GStreamer and GStreamerMM libraries installed. ```cpp #include #include #include Glib::RefPtr mainloop; int main(int argc, char* argv[]) { Gst::init(argc, argv); if (argc < 2) { std::cout << "Usage: " << argv[0] << "