[gstreamermm] Examples: add more basic examples



commit 97e984cc5117de5bfc718adea84ca7aff338c30c
Author: Marcin Kolny <marcin kolny gmail com>
Date:   Wed Aug 31 22:48:23 2016 +0200

    Examples: add more basic examples

 .gitignore             |    2 +
 examples/Makefile.am   |    4 ++
 examples/basics/bin.cc |   66 +++++++++++++++++++++++++++++++
 examples/basics/bus.cc |  101 ++++++++++++++++++++++++++++++++++++++++++++++++
 gstreamer/src/bin.hg   |    4 ++
 gstreamer/src/bus.hg   |    4 ++
 6 files changed, 181 insertions(+), 0 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 7e79bd9..117ef26 100644
--- a/.gitignore
+++ b/.gitignore
@@ -39,6 +39,8 @@ stamp-h?
 
 #examples/
 /examples/*/example
+/examples/basics/bin
+/examples/basics/bus
 /examples/basics/element_factory
 /examples/basics/init_gstreamermm
 
diff --git a/examples/Makefile.am b/examples/Makefile.am
index dd9b47a..7ebec62 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -55,6 +55,8 @@ noinst_PROGRAMS =                                             \
        ogg_rewriter/example                            \
        typefind/example                                        \
        optiongroup/example                                     \
+       basics/bin                                                      \
+       basics/bus                                                      \
        basics/element_factory                          \
        basics/init_gstreamermm                         \
        $(gl_examples)                                          \
@@ -78,5 +80,7 @@ optiongroup_example_SOURCES                                   = optiongroup/main.cc
 typefind_example_SOURCES                                       = typefind/main.cc
 
 # basic examples
+basics_bin_SOURCES                                                     = basics/bin.cc
+basics_bus_SOURCES                                                     = basics/bus.cc
 basics_element_factory_SOURCES                         = basics/element_factory.cc
 basics_init_gstreamermm_SOURCES                                = basics/init_gstreamermm.cc
diff --git a/examples/basics/bin.cc b/examples/basics/bin.cc
new file mode 100644
index 0000000..74132c7
--- /dev/null
+++ b/examples/basics/bin.cc
@@ -0,0 +1,66 @@
+/*
+ * This example presents basic usage of the Gst::Bin class.
+ */
+#include <gstreamermm.h>
+
+#include <iostream>
+
+int main(int argc, char *argv[])
+{
+  Gst::init(argc, argv);
+
+  // Create some elements
+  Glib::RefPtr<Gst::Element> fakesrc = Gst::ElementFactory::create_element("fakesrc", "source");
+  Glib::RefPtr<Gst::Element> fakesink = Gst::ElementFactory::create_element("fakesink", "sink");
+
+  // Create empty bin
+  Glib::RefPtr<Gst::Bin> bin = Gst::Bin::create("my-bin");
+
+  // Add elements to a bin
+  try
+  {
+    bin->add(fakesrc)->add(fakesink);
+  }
+  catch (const std::runtime_error& ex)
+  {
+    std::cerr << "Exception while adding: " << ex.what() << std::endl;
+    return 1;
+  }
+
+  // Some of the elements are actually a bins, so we can cast
+  // them.
+  Glib::RefPtr<Gst::Element> playbin = Gst::ElementFactory::create_element("playbin");
+  Glib::RefPtr<Gst::Bin> playbin_bin = playbin_bin.cast_static(playbin);
+
+  if (!playbin_bin)
+  {
+    std::cerr << "Cannot find playbin element" << std::endl;
+    return 1;
+  }
+
+
+  // We can also iterate through the elements in the bin
+  Gst::Iterator<Gst::Element> it = playbin_bin->iterate_recurse();
+  std::cout << "List of elements in the container: " << std::endl;
+  while (it.next())
+  {
+    std::cout << " * " << it->get_name() << std::endl;
+  }
+
+  // We can also connect to signals emitted by bin, e.g. when
+  // the element has been removed.
+  bin->signal_element_removed().connect(
+    [] (const Glib::RefPtr<Gst::Element>& removed_element)
+    {
+      std::cout << "Element '" << removed_element->get_name()
+                << "' has been removed from the bin" << std::endl;
+    }
+  );
+
+  bin->remove(fakesink);
+
+  std::cout << "returning from application..." << std::endl;
+
+  return 0;
+}
+
diff --git a/examples/basics/bus.cc b/examples/basics/bus.cc
new file mode 100644
index 0000000..3d542e6
--- /dev/null
+++ b/examples/basics/bus.cc
@@ -0,0 +1,101 @@
+/*
+ * This example presents basic usage of the Gst::Bus class.
+ */
+#include <gstreamermm.h>
+#include <glibmm.h>
+
+#include <iostream>
+
+Glib::RefPtr<Glib::MainLoop> main_loop;
+
+// 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
+  case Gst::MESSAGE_ERROR:
+  {
+    auto error_msg = Glib::RefPtr<Gst::MessageError>::cast_static(message);
+    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
+  case Gst::MESSAGE_EOS:
+    main_loop->quit();
+    break;
+  // Handle state changed message - print details
+  case Gst::MESSAGE_STATE_CHANGED:
+  {
+    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
+  Glib::RefPtr<Gst::Element> source = Gst::ElementFactory::create_element("videotestsrc", "source");
+  Glib::RefPtr<Gst::Element> sink = Gst::ElementFactory::create_element("autovideosink", "sink");
+
+  // Create pipeline
+  Glib::RefPtr<Gst::Pipeline> pipeline = Gst::Pipeline::create("my-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
+  pipeline->set_state(Gst::STATE_PLAYING);
+
+  main_loop = Glib::MainLoop::create();
+  main_loop->run();
+
+  // Stop playing the pipeline
+  pipeline->set_state(Gst::STATE_NULL);
+
+  return 0;
+}
+
diff --git a/gstreamer/src/bin.hg b/gstreamer/src/bin.hg
index 6fc8afd..dab844e 100644
--- a/gstreamer/src/bin.hg
+++ b/gstreamer/src/bin.hg
@@ -303,4 +303,8 @@ public:
   _WRAP_VFUNC(void handle_message(const Glib::RefPtr<Gst::Message>& message), "handle_message")
 };
 
+/*! A gstreamermm Gst::Bin example.
+ * @example basics/bin.cc
+ */
+
 } //namespace Gst
diff --git a/gstreamer/src/bus.hg b/gstreamer/src/bus.hg
index dadb5bd..27f6b39 100644
--- a/gstreamer/src/bus.hg
+++ b/gstreamer/src/bus.hg
@@ -166,4 +166,8 @@ public:
   _WRAP_SIGNAL(void sync_message(const Glib::RefPtr<Gst::Message>& message), "sync-message")
 };
 
+/*! A gstreamermm Gst::Bus example.
+ * @example basics/bus.cc
+ */
+
 } //namespace Gst


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