[gstreamermm: 86/167] [peper0] added some unit examples
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gstreamermm: 86/167] [peper0] added some unit examples
- Date: Tue, 3 Sep 2013 19:26:18 +0000 (UTC)
commit d36d87803b3250381eeabca2630a9d51e55135c7
Author: Marcin Kolny at Flytronic <marcin kolny flytronic pl>
Date: Tue Jul 30 13:47:39 2013 +0200
[peper0] added some unit examples
tests/Makefile.am | 5 +-
tests/test-plugin-appsink.cc | 91 ++++++++++++++++++++++++++
tests/test-plugin-appsrc.cc | 80 +++++++++++++++++++++++
tests/test-plugin-pushsrc.cc | 140 ++++++++++++++++++++++++++++++++++++++++
tests/test-plugin-register.cc | 142 +++++++++++++++++++++++-----------------
5 files changed, 397 insertions(+), 61 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 30ea1f5..9abaa02 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -28,7 +28,7 @@ check_PROGRAMS = test-caps test-create-element test-pipeline-add-element \
test-ghost-pad test-init-check test-init \
test-init-check-noargs test-init-noargs test-iterator \
test-property-caps test-plugin-gen test-plugin-signals \
- test-plugin-register test-buffer-list-iterator test-base-src
+ test-plugin-register test-plugin-appsrc test-plugin-appsink test-plugin-pushsrc
test-buffer-list-iterator test-base-src
# Include run of test programs in check:
TESTS = $(check_PROGRAMS)
@@ -56,6 +56,9 @@ test_init_check_noargs_SOURCES = test-init-check-noargs.cc
test_init_noargs_SOURCES = test-init-noargs.cc
test_iterator_SOURCES = test-iterator.cc
test_property_caps_SOURCES = test-property-caps.cc
+test_plugin_appsrc_SOURCES = test-plugin-appsrc.cc
+test_plugin_appsink_SOURCES = test-plugin-appsink.cc
+test_plugin_pushsrc_SOURCES = test-plugin-pushsrc.cc
test_plugin_gen_SOURCES = test-plugin-gen.cc
test_plugin_signals_SOURCES = test-plugin-signals.cc
test_plugin_register_SOURCES = test-plugin-register.cc
diff --git a/tests/test-plugin-appsink.cc b/tests/test-plugin-appsink.cc
new file mode 100644
index 0000000..bc35cce
--- /dev/null
+++ b/tests/test-plugin-appsink.cc
@@ -0,0 +1,91 @@
+/* gstreamermm - a C++ wrapper for gstreamer
+ *
+ * Copyright 2008 The gstreamermm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <gstreamermm.h>
+#include <iostream>
+#include <cassert>
+#include <gstreamermm/appsrc.h>
+#include <gstreamermm/appsink.h>
+#include <cstring>
+
+int main(int argc, char** argv)
+{
+ Gst::init(argc, argv);
+
+ {
+ // gstreamermm plugins don't register in the mm system (why?)
+ // get_type() calls "register" indirectly, however we must assure that return value is used or it won't
be realy called (because of attribute((const)) )
+ volatile GType dummy = Gst::AppSrc::get_type();
+ dummy = Gst::AppSink::get_type();
+ }
+
+ Glib::RefPtr<Gst::Pipeline> pipeline;
+ Glib::RefPtr<Gst::Element> source, sink;
+
+ pipeline = Gst::Pipeline::create();
+
+ source = Gst::ElementFactory::create_element("appsrc", "source");
+ sink = Gst::ElementFactory::create_element("appsink", "sink");
+
+ assert(source);
+ assert(sink);
+
+ Glib::RefPtr<Gst::AppSrc> appsrc = appsrc.cast_dynamic(source);
+ assert(appsrc);
+
+ Glib::RefPtr<Gst::AppSink> appsink = appsink.cast_dynamic(sink);
+ assert(appsink);
+
+ pipeline->add(source)->add(sink);
+
+ source->link(sink);
+
+ pipeline->set_state(Gst::STATE_PLAYING);
+
+ std::string data = "hello world";
+ Glib::RefPtr<Gst::Buffer> buf = Gst::Buffer::create(data.length() + 1);
+ strcpy((char *)buf->get_data(), data.c_str());
+ appsrc->push_buffer(buf);
+
+ {
+ std::cout << "waiting for state change" << std::endl;
+ Gst::State state;
+ Gst::StateChangeReturn ret=pipeline->get_state(state, state, 1*Gst::SECOND);
+ assert(ret == Gst::STATE_CHANGE_SUCCESS);
+ }
+
+ Glib::RefPtr<Gst::Buffer> buf_out;
+ buf_out = appsink->pull_buffer();
+ assert(buf_out);
+ assert(buf_out->get_data());
+ assert(std::string((char *)buf_out->get_data()) == data);
+
+ appsrc->end_of_stream();
+
+ std::cout << "waiting for eos or error" << std::endl;
+ Glib::RefPtr<Gst::Message> msg = pipeline->get_bus()->poll((Gst::MessageType)(Gst::MESSAGE_EOS |
Gst::MESSAGE_ERROR) , 1*Gst::SECOND);
+ assert(msg);
+ assert(msg->get_message_type() == Gst::MESSAGE_EOS);
+ std::cout << "shutting down" << std::endl;
+
+ pipeline->set_state(Gst::STATE_NULL);
+
+
+ return 0;
+}
diff --git a/tests/test-plugin-appsrc.cc b/tests/test-plugin-appsrc.cc
new file mode 100644
index 0000000..55621d7
--- /dev/null
+++ b/tests/test-plugin-appsrc.cc
@@ -0,0 +1,80 @@
+/* gstreamermm - a C++ wrapper for gstreamer
+ *
+ * Copyright 2008 The gstreamermm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <gstreamermm.h>
+#include <iostream>
+#include <cassert>
+#include <gstreamermm/appsrc.h>
+#include <cstring>
+
+int main(int argc, char** argv)
+{
+ Gst::init(argc, argv);
+
+ {
+ // gstreamermm plugins don't register in the mm system (why?)
+ // get_type() calls "register" indirectly, however we must assure that return value is used or it won't
be realy called (because of attribute((const)) )
+ volatile GType dummy = Gst::AppSrc::get_type();
+ }
+
+ Glib::RefPtr<Gst::Pipeline> pipeline;
+ Glib::RefPtr<Gst::Element> source, sink;
+
+ pipeline = Gst::Pipeline::create();
+
+ source = Gst::ElementFactory::create_element("appsrc", "source");
+ sink = Gst::ElementFactory::create_element("fakesink", "sink");
+
+ assert(source);
+ assert(sink);
+
+ Glib::RefPtr<Gst::AppSrc> appsrc = appsrc.cast_dynamic(source);
+ assert(appsrc);
+
+ pipeline->add(source)->add(sink);
+
+ source->link(sink);
+
+ pipeline->set_state(Gst::STATE_PLAYING);
+
+ std::string data = "hello world";
+ Glib::RefPtr<Gst::Buffer> buf = Gst::Buffer::create(data.length() + 1);
+ strcpy((char *)buf->get_data(), data.c_str());
+ appsrc->push_buffer(buf);
+
+ {
+ std::cout << "waiting for state change" << std::endl;
+ Gst::State state;
+ Gst::StateChangeReturn ret=pipeline->get_state(state, state, 1*Gst::SECOND);
+ assert(ret == Gst::STATE_CHANGE_SUCCESS);
+ }
+
+ appsrc->end_of_stream();
+
+ std::cout << "waiting for eos or error" << std::endl;
+ Glib::RefPtr<Gst::Message> msg = pipeline->get_bus()->poll((Gst::MessageType)(Gst::MESSAGE_EOS |
Gst::MESSAGE_ERROR) , 1*Gst::SECOND);
+ assert(msg);
+ assert(msg->get_message_type() == Gst::MESSAGE_EOS);
+ std::cout << "shutting down" << std::endl;
+
+ pipeline->set_state(Gst::STATE_NULL);
+
+
+ return 0;
+}
diff --git a/tests/test-plugin-pushsrc.cc b/tests/test-plugin-pushsrc.cc
new file mode 100644
index 0000000..c1161c7
--- /dev/null
+++ b/tests/test-plugin-pushsrc.cc
@@ -0,0 +1,140 @@
+/*
+ * test2.cpp
+ *
+ * Created on: Feb 22, 2013
+ * Author: t.lakota
+ */
+
+/* gstreamermm - a C++ wrapper for gstreamer
+ *
+ * Copyright 2008 The gstreamermm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <gstreamermm.h>
+#include <iostream>
+#include <cassert>
+#include <algorithm>
+#include <gstreamermm/appsrc.h>
+#include <gstreamermm/appsink.h>
+#include <cstring>
+
+//this is a bit hacky, but for now necessary for Gst::Element_Class::class_init_function which is used by
register_mm_type
+#include <gstreamermm/private/pushsrc_p.h>
+
+class FooSrc : public Gst::PushSrc
+{
+ int count_left;
+
+public:
+ static const int COUNT = 5;
+ static void base_init(BaseClassType *klass)
+ {
+ /* This is another hack.
+ * For now it uses pure C functions, which should be wrapped then.
+ */
+ gst_element_class_set_details_simple(GST_ELEMENT_CLASS(klass),
+ "foosrc_longname", "foosrc_classification", "foosrc_detail_description",
+ "foosrc_detail_author");
+
+ gst_element_class_add_pad_template(GST_ELEMENT_CLASS(klass),
+ Gst::PadTemplate::create("src", Gst::PAD_SRC, Gst::PAD_ALWAYS,
+ Gst::Caps::create_from_string("x-application/x-foo1"))->gobj());
+ }
+ explicit FooSrc(GstPushSrc *gobj) :
+ Gst::PushSrc(gobj)
+ {
+ set_format(Gst::FORMAT_TIME);
+ }
+ Gst::FlowReturn create_vfunc(guint64 offset, guint size, Glib::RefPtr<Gst::Buffer>& buffer)
+ {
+ if (count_left-- <= 0)
+ return Gst::FLOW_UNEXPECTED;
+ std::stringstream ss;
+ ss << COUNT - count_left << "";
+ std::string s = ss.str();
+ buffer = Gst::Buffer::create(s.size());
+ std::copy(s.begin(), s.end(), buffer->get_data());
+ return Gst::FLOW_OK;
+ }
+ virtual bool negotiate_vfunc()
+ {
+ return true;
+ }
+ virtual bool start_vfunc()
+ {
+ count_left = COUNT;
+ return true;
+ }
+};
+
+bool register_foo(Glib::RefPtr<Gst::Plugin> plugin)
+{
+ Gst::ElementFactory::register_element(plugin, "foosrcmm", 10,
+ Gst::register_mm_type<FooSrc>("foosrcmm"));
+ return true;
+
+}
+
+int main(int argc, char** argv)
+{
+ Gst::init(argc, argv);
+ Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR, "foo",
+ "foo is example of C++ element", sigc::ptr_fun(register_foo), "0.123",
+ "LGPL", "source?", "package?", "http://example.com");
+
+ Glib::RefPtr<Gst::Pipeline> pipeline;
+
+ pipeline = Gst::Pipeline::create("my-pipeline");
+
+ Glib::RefPtr<Gst::Element> source = Gst::ElementFactory::create_element(
+ "foosrcmm", "src");
+ Glib::RefPtr<Gst::AppSink> sink = Gst::AppSink::create("sink");
+
+ assert(source);
+ assert(sink);
+
+ pipeline->add(source)->add(sink);
+ source->link(sink);
+
+ pipeline->set_state(Gst::STATE_PLAYING);
+
+ for (int i = 0; i < FooSrc::COUNT; ++i)
+ {
+ std::cout << "pulling buffer " << i + 1 << std::endl;
+ Glib::RefPtr<Gst::Buffer> buf_out;
+ buf_out = sink->pull_buffer();
+ assert(buf_out);
+ assert(buf_out->get_data());
+ assert(buf_out->get_data()[0]=='1'+i);
+ }
+
+ std::cout << "waiting for eos or error" << std::endl;
+ Glib::RefPtr<Gst::Message> msg = pipeline->get_bus()->poll(
+ (Gst::MessageType) (Gst::MESSAGE_EOS | Gst::MESSAGE_ERROR),
+ 1 * Gst::SECOND);
+ assert(msg);
+ assert(msg->get_message_type() == Gst::MESSAGE_EOS);
+ std::cout << "shutting down" << std::endl;
+
+ pipeline->set_state(Gst::STATE_NULL);
+ pipeline.reset();
+ source.reset();
+ sink.reset();
+
+ return 0;
+}
+
diff --git a/tests/test-plugin-register.cc b/tests/test-plugin-register.cc
index f25050b..5da04fe 100644
--- a/tests/test-plugin-register.cc
+++ b/tests/test-plugin-register.cc
@@ -28,79 +28,80 @@
#include <iostream>
#include <cassert>
#include <algorithm>
+#include <gstreamermm/appsrc.h>
+#include <gstreamermm/appsink.h>
+#include <cstring>
//this is a bit hacky, but for now necessary for Gst::Element_Class::class_init_function which is used by
register_mm_type
#include <gstreamermm/private/element_p.h>
-class Foo: public Gst::Element
+class Foo : public Gst::Element
{
- Glib::RefPtr<Gst::Pad> sinkpad;
- Glib::RefPtr<Gst::Pad> srcpad;
+ Glib::RefPtr<Gst::Pad> sinkpad;
+ Glib::RefPtr<Gst::Pad> srcpad;
public:
- static void base_init(BaseClassType *klass)
- {
- /* This is another hack.
- * For now it uses pure C functions, which should be wrapped then.
- */
- gst_element_class_set_details_simple(klass,
- "foo_longname",
- "foo_classification",
- "foo_detail_description",
- "foo_detail_author");
-
-
- gst_element_class_add_pad_template(klass, Gst::PadTemplate::create("sink", Gst::PAD_SINK,
Gst::PAD_ALWAYS, Gst::Caps::create_any())->gobj());
- gst_element_class_add_pad_template(klass, Gst::PadTemplate::create("src", Gst::PAD_SRC,
Gst::PAD_ALWAYS, Gst::Caps::create_any())->gobj());
- }
-
- Gst::FlowReturn chain(const Glib::RefPtr<Gst::Pad> &pad, Glib::RefPtr<Gst::Buffer> &buf)
- {
- assert(buf->gobj()->mini_object.refcount==1);
- buf=buf->create_writable();
- //run some C++ function
- std::sort(buf->get_data(), buf->get_data() + buf->get_size());
- assert(buf->gobj()->mini_object.refcount==1);
- return srcpad->push(buf);
- }
- explicit Foo(GstElement *gobj) :
- Gst::Element(gobj)
- {
- add_pad(sinkpad=Gst::Pad::create(get_pad_template("sink"), "sink"));
- add_pad(srcpad=Gst::Pad::create(get_pad_template("src"), "src"));
- sinkpad->set_chain_function(sigc::mem_fun(*this, &Foo::chain));
- }
- ~Foo()
- {
- printf("destroying foo\n");
- }
+ static void base_init(BaseClassType *klass)
+ {
+ /* This is another hack.
+ * For now it uses pure C functions, which should be wrapped then.
+ */
+ gst_element_class_set_details_simple(klass, "foo_longname",
+ "foo_classification", "foo_detail_description", "foo_detail_author");
+
+ gst_element_class_add_pad_template(klass,
+ Gst::PadTemplate::create("sink", Gst::PAD_SINK, Gst::PAD_ALWAYS,
+ Gst::Caps::create_any())->gobj());
+ gst_element_class_add_pad_template(klass,
+ Gst::PadTemplate::create("src", Gst::PAD_SRC, Gst::PAD_ALWAYS,
+ Gst::Caps::create_any())->gobj());
+ }
+
+ Gst::FlowReturn chain(const Glib::RefPtr<Gst::Pad> &pad, Glib::RefPtr<Gst::Buffer> &buf)
+ {
+ assert(buf->gobj()->mini_object.refcount==1);
+ buf = buf->create_writable();
+ //run some C++ function
+ std::sort(buf->get_data(), buf->get_data() + buf->get_size());
+ assert(buf->gobj()->mini_object.refcount==1);
+ return srcpad->push(buf);
+ }
+ explicit Foo(GstElement *gobj) :
+ Gst::Element(gobj)
+ {
+ add_pad(sinkpad = Gst::Pad::create(get_pad_template("sink"), "sink"));
+ add_pad(srcpad = Gst::Pad::create(get_pad_template("src"), "src"));
+ sinkpad->set_chain_function(sigc::mem_fun(*this, &Foo::chain));
+ }
+ ~Foo()
+ {
+ printf("destroying foo\n");
+ }
};
bool register_foo(Glib::RefPtr<Gst::Plugin> plugin)
{
- Gst::ElementFactory::register_element(plugin, "foomm", 10, Gst::register_mm_type<Foo>("foomm"));
- return true;
+ Gst::ElementFactory::register_element(plugin, "foomm", 10,
+ Gst::register_mm_type<Foo>("foomm"));
+ return true;
}
int main(int argc, char** argv)
{
Gst::init(argc, argv);
- Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
- "foo", "foo is example of C++ element",
- sigc::ptr_fun(register_foo), "0.123",
- "LGPL", "source?",
- "package?", "http://example.com"
- );
+ Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR, "foo",
+ "foo is example of C++ element", sigc::ptr_fun(register_foo), "0.123",
+ "LGPL", "source?", "package?", "http://example.com");
Glib::RefPtr<Gst::Pipeline> pipeline;
- Glib::RefPtr<Gst::Element> source, filter1, filter2, sink;
pipeline = Gst::Pipeline::create("my-pipeline");
- source = Gst::ElementFactory::create_element("fdsrc", "source");
- filter1 = Gst::ElementFactory::create_element("foomm", "filter1");
- sink = Gst::ElementFactory::create_element("fdsink", "sink");
+ Glib::RefPtr<Gst::AppSrc> source = Gst::AppSrc::create("source");
+ Glib::RefPtr<Gst::Element> filter1 = Gst::ElementFactory::create_element(
+ "foomm", "filter1");
+ Glib::RefPtr<Gst::AppSink> sink = Gst::AppSink::create("sink");
assert(source);
assert(filter1);
@@ -109,20 +110,41 @@ int main(int argc, char** argv)
pipeline->add(source)->add(filter1)->add(sink);
source->link(filter1)->link(sink);
- std::cout << "Successfully linked elements '" << source->get_name() <<
- "', '" << filter1->get_name() << "' and '" << sink->get_name() <<
- "'." << std::endl;
-
pipeline->set_state(Gst::STATE_PLAYING);
- pipeline->get_bus()->poll(Gst::MESSAGE_EOS, -1);
+
+ std::cout << "pushing buffer" << std::endl;
+ std::vector<guint8> data;
+ data.push_back(1);
+ data.push_back(5);
+ data.push_back(2);
+ data.push_back(4);
+ Glib::RefPtr<Gst::Buffer> buf = Gst::Buffer::create(data.size());
+ std::copy(data.begin(), data.end(), buf->get_data());
+ source->push_buffer(buf);
+
+ std::cout << "pulling buffer" << std::endl;
+ Glib::RefPtr<Gst::Buffer> buf_out;
+ buf_out = sink->pull_buffer();
+ assert(buf_out);
+ assert(buf_out->get_data());
+ std::sort(data.begin(), data.end());
+ assert(std::equal(data.begin(), data.end(), buf_out->get_data()));
+
+ std::cout << "finishing stream" << std::endl;
+ source->end_of_stream();
+
+ std::cout << "waiting for eos or error" << std::endl;
+ Glib::RefPtr<Gst::Message> msg = pipeline->get_bus()->poll(
+ (Gst::MessageType) (Gst::MESSAGE_EOS | Gst::MESSAGE_ERROR),
+ 1 * Gst::SECOND);
+ assert(msg);
+ assert(msg->get_message_type() == Gst::MESSAGE_EOS);
+ std::cout << "shutting down" << std::endl;
+
pipeline->set_state(Gst::STATE_NULL);
pipeline.reset();
filter1.reset();
- filter2.reset();
return 0;
}
-
-
-
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]