| 1 | /* |
| 2 | * check.cc |
| 3 | * |
| 4 | * Created on: 3 sie 2013 |
| 5 | * Author: loganek |
| 6 | */ |
| 7 | |
| 8 | #include <gstreamermm/check.h> |
| 9 | #include <gstreamermm/elementfactory.h> |
| 10 | #include <algorithm> |
| 11 | #include <string> |
| 12 | |
| 13 | using namespace Gst; |
| 14 | using Glib::RefPtr; |
| 15 | using Glib::ustring; |
| 16 | |
| 17 | void ThrowIf (bool condition, const ustring& message) |
| 18 | { |
| 19 | if (condition) |
| 20 | throw Exception (message); |
| 21 | } |
| 22 | |
| 23 | void ThrowIfNot (bool condition, const ustring& message) |
| 24 | { |
| 25 | if (!condition) |
| 26 | throw Exception (message); |
| 27 | } |
| 28 | |
| 29 | template<typename T> |
| 30 | std::string ToString (const T& obj) |
| 31 | { |
| 32 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
| 33 | return std::to_string(obj); |
| 34 | #else |
| 35 | std::ostringstream convert; |
| 36 | convert << obj; |
| 37 | return convert.str (); |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | template<typename T> |
| 42 | void CheckRefCount (T* object, const ustring& name, int expected_refcount) |
| 43 | { |
| 44 | int refcount = GST_OBJECT_REFCOUNT_VALUE (object); |
| 45 | |
| 46 | ThrowIf ((refcount == expected_refcount), |
| 47 | name + " refcount is " + ToString(refcount) + |
| 48 | " instead of " + ToString(expected_refcount)); |
| 49 | } |
| 50 | |
| 51 | RefPtr<Element> Check::_Check::setup_element (const ustring& factory) |
| 52 | { |
| 53 | RefPtr<Element> element; |
| 54 | |
| 55 | element = ElementFactory::create_element (factory, factory); |
| 56 | |
| 57 | ThrowIf (!!element, "Could not create a '" + factory + "' element"); |
| 58 | CheckRefCount (element->gobj(), factory, 1); |
| 59 | |
| 60 | return element; |
| 61 | } |
| 62 | |
| 63 | void Check::_Check::caps_equal (RefPtr<Caps> caps1, RefPtr<Caps> caps2) |
| 64 | { |
| 65 | ThrowIf (caps1->equals(caps2), |
| 66 | "caps ('" + caps1->to_string() + "') is" |
| 67 | " not equal to caps ('" + caps2->to_string() + "')"); |
| 68 | } |
| 69 | |
| 70 | void buffer_data (RefPtr<Buffer> buffer, gconstpointer data, gsize size) |
| 71 | { |
| 72 | MapInfo info; |
| 73 | |
| 74 | ThrowIfNot (buffer->map(info, MAP_READ), "Error occurs during mapping."); |
| 75 | ThrowIf ((std::equal (info.get_data(), info.get_data() + size, |
| 76 | static_cast<const guint8*>(data)) != 0), "Buffer data not equals."); |
| 77 | // TODO: using gst_util to display/throw/return dump of memory |
| 78 | |
| 79 | buffer->unmap (info); |
| 80 | } |