[gstreamermm] examples: added example which uses decodebin



commit ebb947ca42c292bb8984edae9b3f48e405ace2fa
Author: Marcin Kolny <marcin kolny gmail com>
Date:   Sat Sep 13 00:18:05 2014 +0200

    examples: added example which uses decodebin
    
            * examples/Makefile.am: added example to a build list
            * examples/all_media_player.cc: simple application, which can play
              all audio/video formats (if specyfic plugins are installed)
              using decodebin. This example shows, how to use decodebin,
              dynamic pads, and how to use non-static class methods in signal
              handlers (sigc::mem_fun).

 examples/Makefile.am              |    2 +
 examples/all_media_player/main.cc |  151 +++++++++++++++++++++++++++++++++++++
 2 files changed, 153 insertions(+), 0 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 1836275..477d784 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -24,6 +24,7 @@ examples_cppflags = $(GSTREAMERMM_CFLAGS)
 endif
 
 noinst_PROGRAMS =                                              \
+       all_media_player/example                        \
        audio_video_muxer/example                       \
        dynamic_changing_element/example        \
        dynamic_changing_source/example         \
@@ -42,6 +43,7 @@ AM_CPPFLAGS = -I$(top_builddir) $(gstreamermm_includes) $(examples_cppflags)
 AM_CXXFLAGS = $(GSTREAMERMM_WXXFLAGS) -std=c++0x
 LDADD = $(GSTREAMERMM_LIBS) $(local_libgstreamermm)
 
+all_media_player_example_SOURCES                       = all_media_player/main.cc
 audio_video_muxer_example_SOURCES                      = audio_video_muxer/main.cc
 dynamic_changing_element_example_SOURCES       = dynamic_changing_element/main.cc
 dynamic_changing_source_example_SOURCES                = dynamic_changing_source/main.cc
diff --git a/examples/all_media_player/main.cc b/examples/all_media_player/main.cc
new file mode 100644
index 0000000..6f0b95d
--- /dev/null
+++ b/examples/all_media_player/main.cc
@@ -0,0 +1,151 @@
+/*
+ * main.cc
+ *
+ *  Created on: Sep 12, 2014
+ *      Author: m.kolny
+ */
+
+#include <gstreamermm.h>
+#include <iostream>
+#include <glibmm.h>
+
+using namespace Gst;
+using Glib::RefPtr;
+
+
+class AllMediaPlayer
+{
+private:
+  RefPtr<Glib::MainLoop> main_loop;
+  RefPtr<Pipeline> pipeline;
+  RefPtr<FileSrc> source;
+  RefPtr<Element> decoder;
+
+  bool on_bus_message(const RefPtr<Bus>&, const RefPtr<Message>& message);
+  void on_decoder_pad_added(const RefPtr<Pad>& pad);
+
+  void init()
+  {
+    source = FileSrc::create();
+    decoder = ElementFactory::create_element("decodebin");
+
+    if (!decoder || !source)
+    {
+      throw std::runtime_error("One element could not be created.");
+    }
+
+    pipeline->add(source)->add(decoder);
+    decoder->signal_pad_added().connect(sigc::mem_fun(*this, &AllMediaPlayer::on_decoder_pad_added));
+
+    source->link(decoder);
+  }
+
+public:
+  AllMediaPlayer()
+  {
+    main_loop = Glib::MainLoop::create();
+    pipeline = Pipeline::create();
+    pipeline->get_bus()->add_watch(sigc::mem_fun(*this, &AllMediaPlayer::on_bus_message));
+  }
+
+  void play_until_eos(const std::string& filename)
+  {
+    init();
+    source->property_location() = filename;
+    pipeline->set_state(STATE_PLAYING);
+    main_loop->run();
+    pipeline->set_state(STATE_NULL);
+  }
+};
+
+bool AllMediaPlayer::on_bus_message(const RefPtr<Bus>&, const RefPtr<Message>& message)
+{
+  switch(message->get_message_type())
+  {
+  case Gst::MESSAGE_EOS:
+    std::cout << std::endl << "End of stream" << std::endl;
+    main_loop->quit();
+    return false;
+  case Gst::MESSAGE_ERROR:
+    std::cerr << "Error." << RefPtr<MessageError>::cast_static(message)->parse_debug() << std::endl;
+    main_loop->quit();
+    return false;
+  default:
+    break;
+  }
+
+  return true;
+}
+
+void AllMediaPlayer::on_decoder_pad_added(const RefPtr<Pad>& pad)
+{
+  Glib::ustring caps_format = pad->get_current_caps()->to_string().substr(0, 5);
+  RefPtr<Bin> parent = parent.cast_dynamic(pad->get_parent()->get_parent());
+
+  if (!parent)
+  {
+    std::cerr << "cannot get parent bin" << std::endl;
+    return;
+  }
+
+  Glib::ustring factory_name;
+
+  if (caps_format == "video")
+  {
+    factory_name = "autovideosink";
+  }
+  else if (caps_format == "audio")
+  {
+    factory_name = "autoaudiosink";
+  }
+  else
+  {
+    std::cerr << "unsupported media type: " << pad->get_current_caps()->to_string() << std::endl;
+    return;
+  }
+
+  RefPtr<Element> element = ElementFactory::create_element(factory_name);
+
+  if (!element)
+  {
+    std::cerr << "cannot create element " << factory_name << std::endl;
+    return;
+  }
+
+  try
+  {
+    parent->add(element);
+    element->set_state(STATE_PLAYING);
+    pad->link(element->get_static_pad("sink"));
+  }
+  catch (const std::runtime_error& err)
+  {
+    std::cerr << "cannot add element to a bin: " << err.what() << std::endl;
+  }
+}
+
+int main(int argc, char** argv)
+{
+  if (argc < 2)
+  {
+    std::cout << "Usage: " << argv[0] << " <multimedia filename>" << std::endl;
+    return 1;
+  }
+
+  init(argc, argv);
+  AllMediaPlayer player;
+
+  try
+  {
+    player.play_until_eos(argv[1]);
+  }
+  catch (const std::runtime_error& err)
+  {
+    std::cerr << "runtime error: " << err.what() << std::endl;
+  }
+
+  return 0;
+}
+
+
+


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