[gstreamermm] Examples: add example for dynamic pads



commit 3106ca19abcee057b5bad7a072e1f9f2c25ffa10
Author: Marcin Kolny <marcin kolny gmail com>
Date:   Thu Sep 15 00:04:09 2016 +0200

    Examples: add example for dynamic pads

 .gitignore                      |    1 +
 examples/Makefile.am            |    2 +
 examples/basics/dynamic_pads.cc |   84 +++++++++++++++++++++++++++++++++++++++
 gstreamer/src/pad.hg            |    4 ++
 4 files changed, 91 insertions(+), 0 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 117ef26..e403d4e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -41,6 +41,7 @@ stamp-h?
 /examples/*/example
 /examples/basics/bin
 /examples/basics/bus
+/examples/basics/dynamic_pads
 /examples/basics/element_factory
 /examples/basics/init_gstreamermm
 
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 7ebec62..58a4aeb 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -57,6 +57,7 @@ noinst_PROGRAMS =                                             \
        optiongroup/example                                     \
        basics/bin                                                      \
        basics/bus                                                      \
+       basics/dynamic_pads                                     \
        basics/element_factory                          \
        basics/init_gstreamermm                         \
        $(gl_examples)                                          \
@@ -82,5 +83,6 @@ typefind_example_SOURCES                                      = typefind/main.cc
 # basic examples
 basics_bin_SOURCES                                                     = basics/bin.cc
 basics_bus_SOURCES                                                     = basics/bus.cc
+basics_dynamic_pads_SOURCES                                    = basics/dynamic_pads.cc
 basics_element_factory_SOURCES                         = basics/element_factory.cc
 basics_init_gstreamermm_SOURCES                                = basics/init_gstreamermm.cc
diff --git a/examples/basics/dynamic_pads.cc b/examples/basics/dynamic_pads.cc
new file mode 100644
index 0000000..9631876
--- /dev/null
+++ b/examples/basics/dynamic_pads.cc
@@ -0,0 +1,84 @@
+/*
+ * This example presents basic usage of dynamics Gst::Pad objects.
+ */
+#include <gstreamermm.h>
+#include <glibmm.h>
+
+#include <iostream>
+
+int main(int argc, char *argv[])
+{
+  Gst::init(argc, argv);
+
+  Glib::RefPtr<Glib::MainLoop> main_loop = Glib::MainLoop::create();
+
+  // Create pipeline
+  Glib::RefPtr<Gst::Pipeline> pipeline = Gst::Pipeline::create("my_pipeline");
+
+  // Create elements
+  Glib::RefPtr<Gst::Element> source = Gst::ElementFactory::create_element("videotestsrc", "source"),
+      decodebin = Gst::ElementFactory::create_element("decodebin", "decoder"),
+      sink = Gst::ElementFactory::create_element("autovideosink", "videosink");
+
+  // Add elements to a pipeline
+  try
+  {
+    pipeline->add(source)->add(decodebin)->add(sink);
+  }
+  catch (const std::runtime_error& ex)
+  {
+    std::cerr << "Exception while adding: " << ex.what() << std::endl;
+    return 1;
+  }
+
+  // Link elements
+  try
+  {
+    // We can't link decodebin with sink, because decodebin
+    // doesn't have any src pad on start up.
+    source->link(decodebin);
+  }
+  catch (const std::runtime_error& ex)
+  {
+    std::cerr << "Exception while linking: " << ex.what() << std::endl;
+  }
+
+  // Handle messages posted on bus
+  pipeline->get_bus()->add_watch([main_loop] (const Glib::RefPtr<Gst::Bus>&,
+                                     const Glib::RefPtr<Gst::Message>& message) {
+    switch (message->get_message_type())
+    {
+    case Gst::MESSAGE_EOS:
+    case Gst::MESSAGE_ERROR:
+      main_loop->quit();
+      break;
+    default:
+      break;
+    }
+  return true;
+  });
+
+  // Listen for newly created pads
+  decodebin->signal_pad_added().connect([decodebin, sink] (const Glib::RefPtr<Gst::Pad>& pad) {
+    std::cout << "New pad added to " << decodebin->get_name() << std::endl;
+    std::cout << "Pad name: " << pad->get_name() << std::endl;
+
+    Gst::PadLinkReturn ret = pad->link(sink->get_static_pad("sink"));
+
+    if (ret != Gst::PAD_LINK_OK)
+    {
+      std::cout << "Cannot link pads. Error: " << ret << std::endl;
+    }
+    else
+    {
+      std::cout << "Pads linked correctly!" << std::endl;
+    }
+  });
+
+  // Start the pipeline
+  pipeline->set_state(Gst::STATE_PLAYING);
+  main_loop->run();
+  pipeline->set_state(Gst::STATE_NULL);
+
+  return 0;
+}
diff --git a/gstreamer/src/pad.hg b/gstreamer/src/pad.hg
index 7acc978..a4b814a 100644
--- a/gstreamer/src/pad.hg
+++ b/gstreamer/src/pad.hg
@@ -372,6 +372,10 @@ private:
   SlotGetrange slot_getrange;
 };
 
+/*! A gstreamermm dynamic Gst::Pad example.
+ * @example basics/dynamic_pads.cc
+ */
+
 class PadProbeInfo
 {
   _CLASS_GENERIC(PadProbeInfo, GstPadProbeInfo)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]