| 1 |
/* gstreamermm - a C++ wrapper for gstreamer |
| 2 |
* |
| 3 |
* Copyright 2008-2016 The gstreamermm Development Team |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free |
| 17 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <gst/base/gstbasetransform.h> |
| 21 |
#include <gstreamermm/element.h> |
| 22 |
#include <gstreamermm/pad.h> |
| 23 |
|
| 24 |
_DEFS(gstreamermm,gst) |
| 25 |
|
| 26 |
namespace Gst |
| 27 |
{ |
| 28 |
|
| 29 |
/** A base class for simple transform filters. |
| 30 |
* This base class is for filter elements that process data. |
| 31 |
* |
| 32 |
* It provides for: |
| 33 |
* - one sinkpad and one srcpad |
| 34 |
* - Possible formats on sink and source pad implemented with custom |
| 35 |
* transform_caps_vfunc() function. By default uses same format on sink and |
| 36 |
* source. |
| 37 |
* - Handles state changes. |
| 38 |
* - Does flushing. |
| 39 |
* - Push mode. |
| 40 |
* - Pull mode if the sub-class transform_vfunc() can operate on arbitrary |
| 41 |
* data. |
| 42 |
* |
| 43 |
* <H3>Use Cases:</H3> |
| 44 |
* - <B>1. Passthrough mode</B> |
| 45 |
* - Element has no interest in modifying the buffer. It may want to |
| 46 |
* inspect it, in which case the element should have a |
| 47 |
* transform_ip_vfunc() function. If there is no transform_ip_vfunc() |
| 48 |
* function in passthrough mode, the buffer is pushed intact. |
| 49 |
* - On the GstBaseTransformClass is the passthrough_on_same_caps |
| 50 |
* variable which will automatically set/unset passthrough based on |
| 51 |
* whether the element negotiates the same caps on both pads. |
| 52 |
* - passthrough_on_same_caps on an element that doesn't implement a |
| 53 |
* transform_caps_vfunc() function is useful for elements that only |
| 54 |
* inspect data (such as level). |
| 55 |
* . |
| 56 |
* <B>Example elements</B> |
| 57 |
* - Level |
| 58 |
* - VideoScale, AudioConvert, FfmpegColorSpace, AudioResample in |
| 59 |
* certain modes. |
| 60 |
* . |
| 61 |
* - <B>2. Modifications in-place - input buffer and output buffer are the |
| 62 |
* same thing.</B> |
| 63 |
* - The element must implement a transform_ip_vfunc() function. |
| 64 |
* - Output buffer size must <= input buffer size. |
| 65 |
* - If the always_in_place flag is set, non-writable buffers will be |
| 66 |
* copied and passed to the transform_ip_vfunc() function, otherwise |
| 67 |
* a new buffer will be created and the transform_vfunc() function |
| 68 |
* called. |
| 69 |
* - Incoming writable buffers will be passed to the |
| 70 |
* transform_ip_vfunc() function immediately. |
| 71 |
* - only implementing transform_ip_vfunc() and not transform_vfunc() |
| 72 |
* implies always_in_place = TRUE. |
| 73 |
* . |
| 74 |
* <B>Example elements</B> |
| 75 |
* - Volume. |
| 76 |
* - AudioConvert in certain modes (signed/unsigned conversion). |
| 77 |
* - FfmpegColorSpace in certain modes (endianness swapping). |
| 78 |
* |
| 79 |
* - <B>3. Modifications only to the caps/metadata of a buffer</B> |
| 80 |
* - The element does not require writable data, but non-writable |
| 81 |
* buffers should be subbuffered so that the meta-information can be |
| 82 |
* replaced. |
| 83 |
* - Elements wishing to operate in this mode should replace the |
| 84 |
* prepare_output_buffer_vfunc() method to create subbuffers of the |
| 85 |
* input buffer and set always_in_place to TRUE. |
| 86 |
* . |
| 87 |
* <B>Example elements</B> |
| 88 |
* - CapsFilter when setting caps on outgoing buffers that have none. |
| 89 |
* - Identity when it is going to re-timestamp buffers by datarate. |
| 90 |
* |
| 91 |
* - <B>4. Normal mode</B> |
| 92 |
* - always_in_place flag is not set, or there is no |
| 93 |
* transform_ip_vfunc() function. |
| 94 |
* - Element will receive an input buffer and output buffer to |
| 95 |
* operate on. |
| 96 |
* - Output buffer is allocated by calling the |
| 97 |
* prepare_output_buffer_vfunc() function. |
| 98 |
* . |
| 99 |
* <B>Example elements</B> |
| 100 |
* - VideoScale, FfmpegColorSpace, AudioConvert when doing |
| 101 |
* scaling/conversions. |
| 102 |
* |
| 103 |
* - <B>5. Special output buffer allocations</B> |
| 104 |
* - Elements which need to do special allocation of their output |
| 105 |
* buffers other than what Gst::Pad::alloc_buffer() allows should |
| 106 |
* implement a prepare_output_buffer_vfunc() method, which calls the |
| 107 |
* parent implementation and passes the newly allocated buffer. |
| 108 |
* . |
| 109 |
* <B>Example elements</B> |
| 110 |
* - Efence. |
| 111 |
* |
| 112 |
* <B>Sub-class settable flags on Gst::BaseTransform</B> |
| 113 |
* |
| 114 |
* - <B>passthrough</B> |
| 115 |
* - Implies that in the current configuration, the sub-class is not |
| 116 |
* interested in modifying the buffers. |
| 117 |
* - Elements which are always in passthrough mode whenever the same |
| 118 |
* caps has been negotiated on both pads can set the class variable |
| 119 |
* passthrough_on_same_caps to have this behaviour automatically. |
| 120 |
* . |
| 121 |
* - <B>always_in_place</B> |
| 122 |
* - Determines whether a non-writable buffer will be copied before |
| 123 |
* passing to the transform_ip function. |
| 124 |
* - Implied TRUE if no transform function is implemented. |
| 125 |
* - Implied FALSE if ONLY transform function is implemented. |
| 126 |
* |
| 127 |
* Last reviewed on 2016-05-16 (1.8.0). |
| 128 |
* |
| 129 |
* @ingroup GstBaseClasses |
| 130 |
**/ |
| 131 |
class BaseTransform |
| 132 |
: public Element |
| 133 |
{ |
| 134 |
_CLASS_GOBJECT(BaseTransform, GstBaseTransform, GST_BASE_TRANSFORM, Element, GstElement) |
| 135 |
|
| 136 |
public: |
| 137 |
/** The name of the templates for the sink pad ("sink"). |
| 138 |
*/ |
| 139 |
static const Glib::ustring SINK_NAME; |
| 140 |
|
| 141 |
/** The name of the templates for the source pad ("src"). |
| 142 |
*/ |
| 143 |
static const Glib::ustring SRC_NAME; |
| 144 |
|
| 145 |
/** A Gst::FlowReturn that can be returned from transform_vfunc and transform_ip_vfunc |
| 146 |
* to indicate that no output buffer was generated. |
| 147 |
*/ |
| 148 |
static constexpr Gst::FlowReturn FLOW_DROPPED = static_cast<Gst::FlowReturn>(GST_BASE_TRANSFORM_FLOW_DROPPED); |
| 149 |
|
| 150 |
_WRAP_METHOD(bool is_passthrough() const, gst_base_transform_is_passthrough) |
| 151 |
_WRAP_METHOD(void set_passthrough(bool passthrough), gst_base_transform_set_passthrough) |
| 152 |
_WRAP_METHOD(bool is_in_place() const, gst_base_transform_is_in_place) |
| 153 |
_WRAP_METHOD(void set_in_place(bool in_place), gst_base_transform_set_in_place) |
| 154 |
_WRAP_METHOD(bool is_qos_enabled() const, gst_base_transform_is_qos_enabled) |
| 155 |
_WRAP_METHOD(void set_qos_enabled(bool enabled), gst_base_transform_set_qos_enabled) |
| 156 |
_WRAP_METHOD(void update_qos(double proportion, Gst::ClockTimeDiff diff, Gst::ClockTime timestamp), gst_base_transform_update_qos) |
| 157 |
_WRAP_METHOD(void set_gap_aware(bool gap_aware), gst_base_transform_set_gap_aware) |
| 158 |
_WRAP_METHOD(void reconfigure_sink(), gst_base_transform_reconfigure_sink) |
| 159 |
_WRAP_METHOD(void reconfigure_src(), gst_base_transform_reconfigure_src) |
| 160 |
_WRAP_METHOD(void set_prefer_passthrough(bool prefer_passthrough), gst_base_transform_set_prefer_passthrough) |
| 161 |
_WRAP_METHOD(bool update_src_caps(const Glib::RefPtr<Gst::Caps>& updated_caps), gst_base_transform_update_src_caps) |
| 162 |
|
| 163 |
/* Return the memory @allocator used by the base class and its @a params. |
| 164 |
* @param params The Gst::AllocationParams of the allocator. |
| 165 |
* @return A Gst::Allocator used. |
| 166 |
*/ |
| 167 |
Glib::RefPtr<Gst::Allocator> get_allocator(Gst::AllocationParams& params); |
| 168 |
_IGNORE(gst_base_transform_get_allocator) |
| 169 |
|
| 170 |
|
| 171 |
/** Gives the refptr to the sink Gst::Pad object of the element. |
| 172 |
*/ |
| 173 |
_MEMBER_GET_GOBJECT(sink_pad, sinkpad, Gst::Pad, GstPad*) |
| 174 |
|
| 175 |
/** Gives the refptr to the source Gst::Pad object of the element. |
| 176 |
*/ |
| 177 |
_MEMBER_GET_GOBJECT(src_pad, srcpad, Gst::Pad, GstPad*) |
| 178 |
|
| 179 |
_WRAP_PROPERTY("qos", bool) |
| 180 |
|
| 181 |
/** Optional. Given the pad in this direction and the given caps, what caps |
| 182 |
* are allowed on the other pad in this element ? |
| 183 |
*/ |
| 184 |
_WRAP_VFUNC(Glib::RefPtr<Gst::Caps> transform_caps(PadDirection direction, const Glib::RefPtr<Gst::Caps>& caps, const Glib::RefPtr<Gst::Caps>& filter), "transform_caps", refreturn_ctype) |
| 185 |
|
| 186 |
/** Optional. Given the pad in this direction and the given caps, fixate the |
| 187 |
* caps on the other pad. |
| 188 |
*/ |
| 189 |
_WRAP_VFUNC(Glib::RefPtr<Gst::Caps> fixate_caps(PadDirection direction, const Glib::RefPtr<Gst::Caps>& caps, const Glib::RefPtr<Gst::Caps>& othercaps), "fixate_caps", refreturn_ctype) |
| 190 |
|
| 191 |
//TODO: This virtual function can't be wrapped without causing execution |
| 192 |
//errors with the ogg_player_gtkmm example. |
| 193 |
///** Optional. Given the size of a buffer in the given direction with the |
| 194 |
//* given caps, calculate the size in bytes of a buffer on the other pad with |
| 195 |
//* the given other caps. The default implementation uses |
| 196 |
//* get_unit_size_vfunc() and keeps the number of units the same. |
| 197 |
//*/ |
| 198 |
//_WRAP_VFUNC(bool transform_size(PadDirection direction, const Glib::RefPtr<Gst::Caps>& caps, guint size, const Glib::RefPtr<Gst::Caps>& othercaps, guint& othersize), "transform_size") |
| 199 |
|
| 200 |
/** Required if the transform is not in-place. Get the size in bytes of one |
| 201 |
* unit for the given caps. |
| 202 |
*/ |
| 203 |
_WRAP_VFUNC(bool get_unit_size(const Glib::RefPtr<Gst::Caps>& caps, gsize& size) const, "get_unit_size") |
| 204 |
|
| 205 |
/** Allows the subclass to be notified of the actual caps set. |
| 206 |
*/ |
| 207 |
_WRAP_VFUNC(bool set_caps(const Glib::RefPtr<Gst::Caps>& incaps, const Glib::RefPtr<Gst::Caps>& outcaps), "set_caps", return_value true) |
| 208 |
|
| 209 |
#m4 _CONVERSION(`GstEvent*', `const Glib::RefPtr<Gst::Event>&', `Glib::wrap($3, true)') |
| 210 |
|
| 211 |
/** Required if the element does not operate in-place. Transforms one |
| 212 |
* incoming buffer to one outgoing buffer. The function is allowed to change |
| 213 |
* size/timestamp/duration of the outgoing buffer. |
| 214 |
*/ |
| 215 |
virtual FlowReturn transform_vfunc(const Glib::RefPtr<Gst::Buffer>& inbuf, const Glib::RefPtr<Gst::Buffer>& outbuf); |
| 216 |
|
| 217 |
/** Required if the element operates in-place. Transform the incoming buffer |
| 218 |
* in-place. |
| 219 |
*/ |
| 220 |
_WRAP_VFUNC(FlowReturn transform_ip(const Glib::RefPtr<Gst::Buffer>& buf), "transform_ip") |
| 221 |
|
| 222 |
/** Optional. Subclasses can override this to do their own allocation of |
| 223 |
* output buffers. Elements that only do analysis can return a subbuffer or |
| 224 |
* even just increment the reference to the input buffer (if in passthrough |
| 225 |
* mode). |
| 226 |
*/ |
| 227 |
virtual FlowReturn prepare_output_buffer_vfunc(const Glib::RefPtr<Gst::Buffer>& input, Glib::RefPtr<Gst::Buffer>& buffer); |
| 228 |
|
| 229 |
/** Optional. Event handler on the source pad. The default implementation |
| 230 |
* handles the event and forwards it downstream. |
| 231 |
*/ |
| 232 |
_WRAP_VFUNC(bool src_event(const Glib::RefPtr<Gst::Event>& event), "src_event") |
| 233 |
|
| 234 |
/** Optional. Event handler on the sink pad. The default implementation handles |
| 235 |
* the event and forwards it downstream. |
| 236 |
*/ |
| 237 |
_WRAP_VFUNC(bool sink_event(const Glib::RefPtr<Gst::Event>& event), "sink_event") |
| 238 |
|
| 239 |
/** Optional. This method is called right before the base class |
| 240 |
* will start processing. Dynamic properties or other delayed configuration |
| 241 |
* could be performed in this method. |
| 242 |
*/ |
| 243 |
_WRAP_VFUNC(void before_transform(const Glib::RefPtr<Gst::Buffer>& buffer), "before_transform") |
| 244 |
|
| 245 |
/** Optional. Subclasses can override this method to check if |
| 246 |
* the caps can be handled by the element. The default implementation might |
| 247 |
* not be the most optimal way to check this in all cases. |
| 248 |
*/ |
| 249 |
_WRAP_VFUNC(bool accept_caps(PadDirection direction, const Glib::RefPtr<const Gst::Caps>& caps), "accept_caps") |
| 250 |
|
| 251 |
/** Optional. Called when the element starts processing. Allows opening |
| 252 |
* external resources. |
| 253 |
*/ |
| 254 |
_WRAP_VFUNC(bool start(), "start", return_value true) |
| 255 |
|
| 256 |
/** Optional. Called when the element starts processing. Allows opening |
| 257 |
* external resources. |
| 258 |
*/ |
| 259 |
_WRAP_VFUNC(bool stop(), "stop", return_value true) |
| 260 |
|
| 261 |
#m4 _CONVERSION(`GstQuery*', `const Glib::RefPtr<Gst::Query>&', `Glib::wrap($3, true)') |
| 262 |
|
| 263 |
/** Setup the allocation parameters for allocating output buffers. The passed |
| 264 |
* in query contains the result of the downstream allocation query.This function |
| 265 |
* is only called when not operating in passthrough mode. The default implementation |
| 266 |
* will remove all memory dependent metadata. If there is a filter_meta method |
| 267 |
* implementation, it will be called for all metadata API in the downstream query, |
| 268 |
* otherwise the metadata API is removed. |
| 269 |
*/ |
| 270 |
virtual bool decide_allocation_vfunc(const Glib::RefPtr<Gst::Query>& query); |
| 271 |
|
| 272 |
/** Return TRUE if the metadata API should be proposed in the upstream allocation query. |
| 273 |
* The default implementation is NULL and will cause all metadata to be removed. |
| 274 |
*/ |
| 275 |
_WRAP_VFUNC(bool filter_meta(const Glib::RefPtr<Gst::Query>& query, GType api, const Gst::Structure& params), "filter_meta", return_value true) |
| 276 |
|
| 277 |
/** Propose buffer allocation parameters for upstream elements. This function must |
| 278 |
* be implemented if the element reads or writes the buffer content. |
| 279 |
* The query that was passed to the decide_allocation is passed in this method (or nullptr |
| 280 |
* when the element is in passthrough mode). The default implementation will pass the |
| 281 |
* query downstream when in passthrough mode and will copy all the filtered metadata API |
| 282 |
* in non-passthrough mode. |
| 283 |
*/ |
| 284 |
virtual bool propose_allocation_vfunc(const Glib::RefPtr<Gst::Query>& decide_query, const Glib::RefPtr<Gst::Query>& query); |
| 285 |
|
| 286 |
#m4 _CONVERSION(`GstBuffer*', `const Glib::RefPtr<Gst::Buffer>&', `Glib::wrap($3, true)') |
| 287 |
|
| 288 |
/** Function which accepts a new input buffer and pre-processes it. The default |
| 289 |
* implementation performs caps (re)negotiation, then QoS if needed, and places |
| 290 |
* the input buffer into the queued_buf member variable. If the buffer is dropped |
| 291 |
* due to QoS, it returns Gst::BASE_TRANSFORM_FLOW_DROPPED. If this input buffer |
| 292 |
* is not contiguous with any previous input buffer, then is_discont is set to TRUE. |
| 293 |
*/ |
| 294 |
_WRAP_VFUNC(Gst::FlowReturn submit_input_buffer(bool is_discont, const Glib::RefPtr<Gst::Buffer>& input), "submit_input_buffer") |
| 295 |
|
| 296 |
/** Called after each new input buffer is submitted repeatedly until it either generates |
| 297 |
* an error or fails to generate an output buffer. The default implementation takes the |
| 298 |
* contents of the queued_buf variable, generates an output buffer if needed by calling |
| 299 |
* the class prepare_output_buffer, and then calls either transform or transform_ip. |
| 300 |
* Elements that don't do 1-to-1 transformations on input to output buffers can either |
| 301 |
* return Gst::BASE_TRANSFORM_FLOW_DROPPED or simply not generate an output buffer until |
| 302 |
* they are ready to do so. |
| 303 |
*/ |
| 304 |
virtual Gst::FlowReturn generate_output_vfunc(Glib::RefPtr<Gst::Buffer>& buffer); |
| 305 |
|
| 306 |
// TODO: wrap GstMeta first. |
| 307 |
//_WRAP_VFUNC(bool transform_meta(const Glib::RefPtr<Gst::Buffer>& outbuf, const Glib::RefPtr<Gst::Meta>& meta, const Glib::RefPtr<Gst::Buffer>& inbuf), "transform_meta") |
| 308 |
|
| 309 |
/** Optional. Copy the metadata from the input buffer to the output buffer. |
| 310 |
* The default implementation will copy the flags, timestamps and offsets of the buffer. |
| 311 |
*/ |
| 312 |
_WRAP_VFUNC(bool copy_metadata(const Glib::RefPtr<Gst::Buffer>& input, const Glib::RefPtr<Gst::Buffer>& outbuf), "copy_metadata") |
| 313 |
|
| 314 |
/** Optional. Handle a requested query. Subclasses that implement this should must chain up to |
| 315 |
* the parent if they didn't handle the query |
| 316 |
*/ |
| 317 |
bool base_transform_query_vfunc(Gst::PadDirection direction, const Glib::RefPtr<Gst::Query>& query); |
| 318 |
|
| 319 |
protected: |
| 320 |
#m4begin |
| 321 |
_PUSH(SECTION_PCC_CLASS_INIT_VFUNCS) |
| 322 |
klass->prepare_output_buffer = &prepare_output_buffer_vfunc_callback; |
| 323 |
klass->transform = &transform_vfunc_callback; |
| 324 |
klass->generate_output = &generate_output_vfunc_callback; |
| 325 |
klass->query = &query_vfunc_callback; |
| 326 |
klass->propose_allocation = &propose_allocation_vfunc_callback; |
| 327 |
klass->decide_allocation = &decide_allocation_vfunc_callback; |
| 328 |
_SECTION(SECTION_PH_VFUNCS) |
| 329 |
static GstFlowReturn prepare_output_buffer_vfunc_callback(GstBaseTransform* self, GstBuffer* input, GstBuffer** buf); |
| 330 |
static GstFlowReturn transform_vfunc_callback(GstBaseTransform* self, GstBuffer* inbuf, GstBuffer* outbuf); |
| 331 |
static GstFlowReturn generate_output_vfunc_callback(GstBaseTransform* self, GstBuffer** outbuf); |
| 332 |
static gboolean query_vfunc_callback(GstBaseTransform* self, GstPadDirection direction, GstQuery* query); |
| 333 |
static gboolean propose_allocation_vfunc_callback(GstBaseTransform* self, GstQuery* decide_query, GstQuery* query); |
| 334 |
static gboolean decide_allocation_vfunc_callback(GstBaseTransform* self, GstQuery* query); |
| 335 |
_POP() |
| 336 |
#m4end |
| 337 |
}; |
| 338 |
|
| 339 |
} //namespace Gst |
| 340 |
|