| 1 | /* |
| 2 | * test-caps.cc |
| 3 | * |
| 4 | * Created on: Aug 1, 2013 |
| 5 | * Author: m.kolny |
| 6 | */ |
| 7 | |
| 8 | #include "mmtest.h" |
| 9 | #include <gstreamermm.h> |
| 10 | |
| 11 | using namespace Gst; |
| 12 | |
| 13 | class CapsTest : public ::testing::Test |
| 14 | { |
| 15 | protected: |
| 16 | Glib::RefPtr<Caps> caps; |
| 17 | |
| 18 | static int width; |
| 19 | static Fraction framerate; |
| 20 | |
| 21 | template<typename T> |
| 22 | void MakeAssert(const T& expected, const T& output) |
| 23 | { |
| 24 | EXPECT_EQ(expected, output); |
| 25 | } |
| 26 | void MakeAssert(const Fraction& expected, const Fraction& output) |
| 27 | { |
| 28 | EXPECT_EQ(expected.denom, output.denom); |
| 29 | EXPECT_EQ(expected.num, output.num); |
| 30 | } |
| 31 | |
| 32 | template<typename T> |
| 33 | void CheckCaps(const Glib::ustring& field_name, T expected, int struct_id = 0) |
| 34 | { |
| 35 | T output; |
| 36 | bool ok = caps->get_structure(struct_id).get_field(field_name, output); |
| 37 | |
| 38 | MM_ASSERT_TRUE(ok); |
| 39 | MakeAssert(expected, output); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | int CapsTest::width = 500; |
| 44 | Fraction CapsTest::framerate = Fraction(25, 1); |
| 45 | |
| 46 | TEST_F(CapsTest, CapsCreateSimple) |
| 47 | { |
| 48 | caps = Caps::create_simple("video/x-raw"); |
| 49 | |
| 50 | MM_ASSERT_TRUE(caps); |
| 51 | } |
| 52 | |
| 53 | TEST_F(CapsTest, CapsCreateUsingStructure) |
| 54 | { |
| 55 | Structure caps_struct("test-struct"); |
| 56 | caps_struct.set_field("width", width); |
| 57 | caps_struct.set_field("framerate", framerate); |
| 58 | |
| 59 | caps = Caps::create(caps_struct); |
| 60 | |
| 61 | CheckCaps("width", width); |
| 62 | CheckCaps("framerate", framerate); |
| 63 | } |
| 64 | |
| 65 | TEST_F(CapsTest, AppendStructureToCaps) |
| 66 | { |
| 67 | caps = Caps::create_simple("video/x-raw"); |
| 68 | |
| 69 | Structure caps_struct("test-struct"); |
| 70 | caps_struct.set_field("width", width); |
| 71 | |
| 72 | caps->set_simple("framerate", framerate); |
| 73 | caps->append_structure(caps_struct); |
| 74 | |
| 75 | CheckCaps("width", width, 1); |
| 76 | CheckCaps("framerate", framerate); |
| 77 | } |
| 78 | |
| 79 | TEST_F(CapsTest, AppendCapsToCaps) |
| 80 | { |
| 81 | caps = Caps::create_simple("video/x-raw"); |
| 82 | caps->set_simple("framerate", framerate); |
| 83 | |
| 84 | Glib::RefPtr<Caps> new_caps = Caps::create_simple("video/x-raw"); |
| 85 | new_caps->set_simple("width", width); |
| 86 | |
| 87 | caps->append(std::move(new_caps)); |
| 88 | |
| 89 | CheckCaps("width", width, 1); |
| 90 | CheckCaps("framerate", framerate); |
| 91 | MM_ASSERT_FALSE(new_caps); |
| 92 | } |
| 93 | |
| 94 | TEST_F(CapsTest, GetNonExistingValue) |
| 95 | { |
| 96 | caps = Caps::create_simple("video/x-raw"); |
| 97 | |
| 98 | int output; |
| 99 | bool ok = caps->get_structure(0).get_field("nonexisting-value", output); |
| 100 | |
| 101 | MM_ASSERT_FALSE(ok); |
| 102 | } |
| 103 | |
| 104 | TEST_F(CapsTest, SetCapsToElement) |
| 105 | { |
| 106 | Glib::RefPtr<Element> element = ElementFactory::create_element("capsfilter", "filter"); |
| 107 | |
| 108 | const char* str_caps = "image/jpeg, framerate=(fraction)2/1"; |
| 109 | |
| 110 | element->set_property("caps", Caps::create_from_string(str_caps)); |
| 111 | element->get_property("caps", caps); |
| 112 | |
| 113 | ASSERT_STREQ(str_caps, caps->to_string().c_str()); |
| 114 | } |
| 115 | |
| 116 | TEST_F(CapsTest, MergeCaps) |
| 117 | { |
| 118 | Glib::RefPtr<Caps> tmp = Caps::create_from_string("video/x-raw, format=RGBA"); |
| 119 | Glib::RefPtr<Caps> tmp2 = Caps::create_from_string("video/x-raw, format=RGB"); |
| 120 | tmp = tmp->merge(std::move(tmp2)); |
| 121 | ASSERT_EQ(1, tmp->get_refcount()); |
| 122 | MM_ASSERT_FALSE(tmp2); |
| 123 | } |
| 124 | |
| 125 | TEST_F(CapsTest, CapsBoxedType) |
| 126 | { |
| 127 | Glib::RefPtr<Gst::Element> filter = Gst::ElementFactory::create_element("capsfilter"); |
| 128 | Glib::RefPtr<Gst::Caps> any_caps = Gst::Caps::create_any(); |
| 129 | Glib::RefPtr<Gst::Caps> any_caps2 = Gst::Caps::create_any(); |
| 130 | ASSERT_EQ(1, any_caps->get_refcount()); |
| 131 | filter->set_property("caps", any_caps); |
| 132 | ASSERT_EQ(2, any_caps->get_refcount()); |
| 133 | filter->set_property("caps", any_caps2); |
| 134 | ASSERT_EQ(1, any_caps->get_refcount()); |
| 135 | ASSERT_EQ(2, any_caps2->get_refcount()); |
| 136 | } |
| 137 | |
| 138 | TEST_F(CapsTest, CreateSimpleWithManyParameters) |
| 139 | { |
| 140 | caps = Caps::create_simple("video/x-raw", "width", width, "framerate", framerate, "test-data", "test-value"); |
| 141 | CheckCaps("width", width); |
| 142 | CheckCaps("framerate", framerate); |
| 143 | CheckCaps<std::string>("test-data", "test-value"); |
| 144 | } |
| 145 | |
| 146 | TEST_F(CapsTest, SetSimpleWithManyParameters) |
| 147 | { |
| 148 | caps = Caps::create_simple("video/x-raw"); |
| 149 | caps->set_simple("width", width, "framerate", framerate, "test-data", "test-value"); |
| 150 | CheckCaps("width", width); |
| 151 | CheckCaps("framerate", framerate); |
| 152 | CheckCaps<std::string>("test-data", "test-value"); |
| 153 | } |
| 154 | |
| 155 | class CapsTestDummy1 |
| 156 | { |
| 157 | public: |
| 158 | int a = 0; |
| 159 | std::string b; |
| 160 | CapsTestDummy1(int a, std::string b) : a(a), b(b) {} |
| 161 | CapsTestDummy1() {} |
| 162 | }; |
| 163 | class CapsTestDummy2 |
| 164 | { |
| 165 | public: |
| 166 | char c = 0; |
| 167 | CapsTestDummy2(char c) : c(c) {} |
| 168 | CapsTestDummy2() {} |
| 169 | }; |
| 170 | |
| 171 | |
| 172 | TEST_F(CapsTest, CreateWithManyCustomTypeParameters) |
| 173 | { |
| 174 | caps = Caps::create_simple("video/x-raw"); |
| 175 | caps->set_simple("d1", CapsTestDummy1(1, "dummy1"), "d2", CapsTestDummy2('x'), "d3", CapsTestDummy1(63, "dummy3")); |
| 176 | Glib::Value<CapsTestDummy1> d1, d3; |
| 177 | Glib::Value<CapsTestDummy2> d2; |
| 178 | caps->get_structure(0).get_field("d1", d1); |
| 179 | caps->get_structure(0).get_field("d2", d2); |
| 180 | caps->get_structure(0).get_field("d3", d3); |
| 181 | |
| 182 | ASSERT_STREQ("dummy1", d1.get().b.c_str()); |
| 183 | ASSERT_EQ(1, d1.get().a); |
| 184 | |
| 185 | ASSERT_EQ('x', d2.get().c); |
| 186 | |
| 187 | ASSERT_STREQ("dummy3", d3.get().b.c_str()); |
| 188 | ASSERT_EQ(63, d3.get().a); |
| 189 | } |