// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // // Copyright (C) 2021 Intel Corporation #ifndef OPENCV_GAPI_STREAMING_GSTREAMER_GSTREAMERPTR_HPP #define OPENCV_GAPI_STREAMING_GSTREAMER_GSTREAMERPTR_HPP #include #include #ifdef HAVE_GSTREAMER #include #include namespace cv { namespace gapi { namespace wip { namespace gst { template static inline void GStreamerPtrUnrefObject(T* ptr) { if (ptr) { gst_object_unref(G_OBJECT(ptr)); } } template static inline void GStreamerPtrRelease(T* ptr); template<> inline void GStreamerPtrRelease(GError* ptr) { if (ptr) { g_error_free(ptr); } } template<> inline void GStreamerPtrRelease(GstElement* ptr) { GStreamerPtrUnrefObject(ptr); } template<> inline void GStreamerPtrRelease(GstElementFactory* ptr) { GStreamerPtrUnrefObject(ptr); } template<> inline void GStreamerPtrRelease(GstPad* ptr) { GStreamerPtrUnrefObject(ptr); } template<> inline void GStreamerPtrRelease(GstBus* ptr) { GStreamerPtrUnrefObject(ptr); } template<> inline void GStreamerPtrRelease(GstAllocator* ptr) { GStreamerPtrUnrefObject(ptr); } template<> inline void GStreamerPtrRelease(GstVideoInfo* ptr) { if (ptr) { gst_video_info_free(ptr); } } template<> inline void GStreamerPtrRelease(GstCaps* ptr) { if (ptr) { gst_caps_unref(ptr); } } template<> inline void GStreamerPtrRelease(GstMemory* ptr) { if (ptr) { gst_memory_unref(ptr); } } template<> inline void GStreamerPtrRelease(GstBuffer* ptr) { if (ptr) { gst_buffer_unref(ptr); } } template<> inline void GStreamerPtrRelease(GstSample* ptr) { if (ptr) { gst_sample_unref(ptr); } } template<> inline void GStreamerPtrRelease(GstMessage* ptr) { if (ptr) { gst_message_unref(ptr); } } template<> inline void GStreamerPtrRelease(GstIterator* ptr) { if (ptr) { gst_iterator_free(ptr); } } template<> inline void GStreamerPtrRelease(GstQuery* ptr) { if (ptr) { gst_query_unref(ptr); } } template<> inline void GStreamerPtrRelease(char* ptr) { if (ptr) { g_free(ptr); } } // NOTE: The main concept of this class is to be owner of some passed to it piece of memory. // (be owner = free this memory or reduce reference count to it after use). // More specifically, GStreamerPtr is designed to own memory returned from GStreamer/GLib // functions, which are marked as [transfer full] in documentation. // [transfer full] means that function fully transfers ownership of returned memory to the // receiving piece of code. // // Memory ownership and ownership transfer concept: // https://developer.gnome.org/programming-guidelines/stable/memory-management.html.en#g-clear-object // NOTE: GStreamerPtr can only own strong references, not floating ones. // For floating references please call g_object_ref_sink(reference) before wrapping // it with GStreamerPtr. // See https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html#floating-ref // for floating references. // NOTE: GStreamerPtr doesn't support pointers to arrays, only pointers to single objects. template class GStreamerPtr : public std::unique_ptr)> { using BaseClass = std::unique_ptr)>; public: constexpr GStreamerPtr() noexcept : BaseClass(nullptr, GStreamerPtrRelease) { } constexpr GStreamerPtr(std::nullptr_t) noexcept : BaseClass(nullptr, GStreamerPtrRelease) { } explicit GStreamerPtr(typename BaseClass::pointer p) noexcept : BaseClass(p, GStreamerPtrRelease) { } GStreamerPtr& operator=(T* p) noexcept { *this = std::move(GStreamerPtr(p)); return *this; } inline operator T*() noexcept { return this->get(); } // There is no const correctness in GStreamer C API inline operator /*const*/ T*() const noexcept { return (T*)this->get(); } }; } // namespace gst } // namespace wip } // namespace gapi } // namespace cv #endif // HAVE_GSTREAMER #endif // OPENCV_GAPI_STREAMING_GSTREAMER_GSTREAMERPTR_HPP