| 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/gstbasesink.h> |
| 21 |
#include <gstreamermm/element.h> |
| 22 |
#include <gstreamermm/buffer.h> |
| 23 |
#include <gstreamermm/pad.h> |
| 24 |
#include <gstreamermm/caps.h> |
| 25 |
#include <gstreamermm/sample.h> |
| 26 |
|
| 27 |
_DEFS(gstreamermm,gst) |
| 28 |
|
| 29 |
namespace Gst |
| 30 |
{ |
| 31 |
|
| 32 |
class BufferList; |
| 33 |
|
| 34 |
/** The base class for sink elements. |
| 35 |
* Gst::BaseSink is the base class for sink elements in GStreamer, such as |
| 36 |
* xvimagesink or filesink. It is a layer on top of Gst::Element that provides |
| 37 |
* a simplified interface to plugin writers. Gst::BaseSink handles many details |
| 38 |
* for you, for example: preroll, clock synchronization, state changes, |
| 39 |
* activation in push or pull mode, and queries. |
| 40 |
* |
| 41 |
* In most cases, when writing sink elements, there is no need to implement |
| 42 |
* class methods from Gst::Element or to set functions on pads, because the |
| 43 |
* Gst::BaseSink infrastructure should be sufficient. |
| 44 |
* |
| 45 |
* TODO: correct paragraph below for C++ and include example from C API: |
| 46 |
* |
| 47 |
* Gst::BaseSink provides support for exactly one sink pad, which should be |
| 48 |
* named "sink". A sink implementation (subclass of Gst::BaseSink) should |
| 49 |
* install a pad template in its base_init function, like so: |
| 50 |
* |
| 51 |
* Gst::BaseSink will handle the prerolling correctly. This means that it will |
| 52 |
* return Gst::STATE_CHANGE_ASYNC from a state change to PAUSED until the first |
| 53 |
* buffer arrives in this element. The base class will call the |
| 54 |
* Gst::BaseSink::preroll_vfunc() vmethod with this preroll buffer and will |
| 55 |
* then commit the state change to the next asynchronously pending state. |
| 56 |
* |
| 57 |
* When the element is set to PLAYING, Gst::BaseSink will synchronise on the |
| 58 |
* clock using the times returned from get_times_vfunc(). If this function |
| 59 |
* returns Gst::CLOCK_TIME_NONE for the start time, no synchronisation will be |
| 60 |
* done. Synchronisation can be disabled entirely by setting the object "sync" |
| 61 |
* property to FALSE. |
| 62 |
* |
| 63 |
* After synchronisation the virtual method Gst::BaseSink::render_vfunc() will |
| 64 |
* be called. Subclasses should minimally implement this method. |
| 65 |
* |
| 66 |
* Since GStreamer 0.10.3 subclasses that synchronise on the clock in the |
| 67 |
* render_vfunc() vmethod are supported as well. These classes typically |
| 68 |
* receive a buffer in the render method and can then potentially block on the |
| 69 |
* clock while rendering. A typical example is an audiosink. Since GStreamer |
| 70 |
* 0.10.11 these subclasses can use wait_preroll() to perform the blocking |
| 71 |
* wait. |
| 72 |
* |
| 73 |
* Upon receiving the EOS event in the PLAYING state, Gst::BaseSink will wait |
| 74 |
* for the clock to reach the time indicated by the stop time of the last |
| 75 |
* get_times_vfunc() call before posting an EOS message. When the element |
| 76 |
* receives EOS in PAUSED, preroll completes, the event is queued and an EOS |
| 77 |
* message is posted when going to PLAYING. |
| 78 |
* |
| 79 |
* Gst::BaseSink will internally use the Gst::EVENT_NEWSEGMENT events to |
| 80 |
* schedule synchronisation and clipping of buffers. Buffers that fall |
| 81 |
* completely outside of the current segment are dropped. Buffers that fall |
| 82 |
* partially in the segment are rendered (and prerolled). Subclasses should do |
| 83 |
* any subbuffer clipping themselves when needed. |
| 84 |
* |
| 85 |
* Gst::BaseSink will by default report the current playback position in |
| 86 |
* Gst::FORMAT_TIME based on the current clock time and segment information. If |
| 87 |
* no clock has been set on the element, the query will be forwarded upstream. |
| 88 |
* |
| 89 |
* The set_caps_vfunc() function will be called when the subclass should |
| 90 |
* configure itself to process a specific media type. |
| 91 |
* |
| 92 |
* The start_vfunc() and stop_vfunc() virtual methods will be called when |
| 93 |
* resources should be allocated. Any preroll_vfunc(), render_vfunc() and |
| 94 |
* set_caps_vfunc() function will be called between the start_vfunc() and |
| 95 |
* stop_vfunc() calls. |
| 96 |
* |
| 97 |
* The event_vfunc() virtual method will be called when an event is received by |
| 98 |
* Gst::BaseSink. Normally this method should only be overriden by very |
| 99 |
* specific elements (such as file sinks) which need to handle the newsegment |
| 100 |
* event specially. |
| 101 |
* The unlock_vfunc() method is called when the elements should unblock any |
| 102 |
* blocking operations they perform in the render_vfunc() method. This is |
| 103 |
* mostly useful when the render_vfunc() method performs a blocking write on a |
| 104 |
* file descriptor, for example. |
| 105 |
* |
| 106 |
* The max-lateness property affects how the sink deals with buffers that |
| 107 |
* arrive too late in the sink. A buffer arrives too late in the sink when the |
| 108 |
* presentation time (as a combination of the last segment, buffer timestamp |
| 109 |
* and element base_time) plus the duration is before the current time of the |
| 110 |
* clock. If the frame is later than max-lateness, the sink will drop the |
| 111 |
* buffer without calling the render method. This feature is disabled if sync |
| 112 |
* is disabled, the get_times_vfunc() method does not return a valid start time |
| 113 |
* or max-lateness is set to -1 (the default). Subclasses can use |
| 114 |
* set_max_lateness() to configure the max-lateness value. |
| 115 |
* |
| 116 |
* The qos property will enable the quality-of-service features of the basesink |
| 117 |
* which gather statistics about the real-time performance of the clock |
| 118 |
* synchronisation. For each buffer received in the sink, statistics are |
| 119 |
* gathered and a QOS event is sent upstream with these numbers. This |
| 120 |
* information can then be used by upstream elements to reduce their processing |
| 121 |
* rate, for example. |
| 122 |
* |
| 123 |
* The async property can be used to instruct the sink |
| 124 |
* to never perform an ASYNC state change. This feature is mostly usable when |
| 125 |
* dealing with non-synchronized streams or sparse streams. |
| 126 |
* |
| 127 |
* Last reviewed on 2016-04-29 (1.8.0) |
| 128 |
* |
| 129 |
* @ingroup GstBaseClasses |
| 130 |
*/ |
| 131 |
class BaseSink |
| 132 |
: public Element |
| 133 |
{ |
| 134 |
_CLASS_GOBJECT(BaseSink, GstBaseSink, GST_BASE_SINK, Element, GstElement) |
| 135 |
|
| 136 |
public: |
| 137 |
_WRAP_METHOD(bool query_latency(bool& live, bool& upstream_live, Gst::ClockTime& min_latency, Gst::ClockTime& max_latency) const, gst_base_sink_query_latency) |
| 138 |
_WRAP_METHOD(Gst::ClockTime get_latency() const, gst_base_sink_get_latency) |
| 139 |
_WRAP_METHOD(Gst::FlowReturn do_preroll(const Glib::RefPtr<Gst::MiniObject>& obj), gst_base_sink_do_preroll) |
| 140 |
_WRAP_METHOD(Gst::FlowReturn wait_preroll(), gst_base_sink_wait_preroll) |
| 141 |
_WRAP_METHOD(Gst::ClockReturn wait_clock(Gst::ClockTime time, Gst::ClockTimeDiff& jitter{?}), gst_base_sink_wait_clock) |
| 142 |
_WRAP_METHOD(Gst::FlowReturn wait(Gst::ClockTime time, Gst::ClockTimeDiff& jitter{?}), gst_base_sink_wait) |
| 143 |
|
| 144 |
_WRAP_METHOD(void set_sync(bool sync), gst_base_sink_set_sync) |
| 145 |
_WRAP_METHOD(bool get_sync() const, gst_base_sink_get_sync) |
| 146 |
|
| 147 |
_WRAP_METHOD(void set_max_lateness(gint64 max_lateness), gst_base_sink_set_max_lateness) |
| 148 |
_WRAP_METHOD(gint64 get_max_lateness() const, gst_base_sink_get_max_lateness) |
| 149 |
|
| 150 |
_WRAP_METHOD(void set_qos_enabled(bool enabled), gst_base_sink_set_qos_enabled) |
| 151 |
_WRAP_METHOD(bool is_qos_enabled() const, gst_base_sink_is_qos_enabled) |
| 152 |
|
| 153 |
_WRAP_METHOD(void set_async_enabled(bool enabled), gst_base_sink_set_async_enabled) |
| 154 |
_WRAP_METHOD(bool is_async_enabled() const, gst_base_sink_is_async_enabled) |
| 155 |
|
| 156 |
_WRAP_METHOD(void set_ts_offset(Gst::ClockTimeDiff offset), gst_base_sink_set_ts_offset) |
| 157 |
_WRAP_METHOD(Gst::ClockTimeDiff get_ts_offset() const, gst_base_sink_get_ts_offset) |
| 158 |
|
| 159 |
_WRAP_METHOD(void set_render_delay(Gst::ClockTime delay), gst_base_sink_set_render_delay) |
| 160 |
_WRAP_METHOD(Gst::ClockTime get_render_delay() const, gst_base_sink_get_render_delay) |
| 161 |
|
| 162 |
_WRAP_METHOD(void set_blocksize(guint blocksize), gst_base_sink_set_blocksize) |
| 163 |
_WRAP_METHOD(guint get_blocksize() const, gst_base_sink_get_blocksize) |
| 164 |
|
| 165 |
_WRAP_METHOD(guint64 get_throttle_time() const, gst_base_sink_get_throttle_time) |
| 166 |
_WRAP_METHOD(void set_throttle_time(guint64 throttle), gst_base_sink_set_throttle_time) |
| 167 |
|
| 168 |
_WRAP_METHOD(Glib::RefPtr<Gst::Sample> get_last_sample() const, gst_base_sink_get_last_sample) |
| 169 |
|
| 170 |
_WRAP_METHOD(void set_max_bitrate(guint64 max_bitrate), gst_base_sink_set_max_bitrate) |
| 171 |
_WRAP_METHOD(guint64 get_max_bitrate() const, gst_base_sink_get_max_bitrate) |
| 172 |
|
| 173 |
_WRAP_METHOD(void set_last_sample_enabled(bool enabled), gst_base_sink_set_last_sample_enabled) |
| 174 |
_WRAP_METHOD(bool is_last_sample_enabled() const, gst_base_sink_is_last_sample_enabled) |
| 175 |
|
| 176 |
/** Gets the sink Gst::Pad object of the element. |
| 177 |
*/ |
| 178 |
_MEMBER_GET_GOBJECT(sink_pad, sinkpad, Gst::Pad, GstPad*) |
| 179 |
|
| 180 |
// TODO: wrap preroll? |
| 181 |
|
| 182 |
_WRAP_PROPERTY("async", bool) |
| 183 |
_WRAP_PROPERTY("max-lateness", gint64) |
| 184 |
_WRAP_PROPERTY("qos", bool) |
| 185 |
_WRAP_PROPERTY("sync", bool) |
| 186 |
_WRAP_PROPERTY("ts-offset", gint64) |
| 187 |
_WRAP_PROPERTY("render-delay", guint64) |
| 188 |
_WRAP_PROPERTY("throttle-time", guint64) |
| 189 |
_WRAP_PROPERTY("blocksize", guint) |
| 190 |
_WRAP_PROPERTY("last-sample", Glib::RefPtr<Gst::Sample>) |
| 191 |
_WRAP_PROPERTY("enable-last-sample", bool) |
| 192 |
_WRAP_PROPERTY("max-bitrate", guint64) |
| 193 |
|
| 194 |
|
| 195 |
/** Called to get sink pad caps from the subclass. |
| 196 |
*/ |
| 197 |
_WRAP_VFUNC(Glib::RefPtr<Gst::Caps> get_caps(const Glib::RefPtr<Gst::Caps>& caps) const, "get_caps", refreturn_ctype) |
| 198 |
|
| 199 |
/** Notify subclass of changed caps. |
| 200 |
*/ |
| 201 |
_WRAP_VFUNC(bool set_caps(const Glib::RefPtr<Gst::Caps>& caps), "set_caps", return_value true) |
| 202 |
|
| 203 |
#m4 _CONVERSION(`GstBuffer*', `const Glib::RefPtr<Gst::Buffer>&', `Glib::wrap($3, true)') |
| 204 |
#m4 _CONVERSION(`GstClockTime*', `Gst::ClockTime&', `*($3)') |
| 205 |
/** Called to get the start and end times for synchronising the passed buffer |
| 206 |
* to the clock. |
| 207 |
*/ |
| 208 |
_WRAP_VFUNC(void get_times(const Glib::RefPtr<Gst::Buffer>& buffer, Gst::ClockTime& start, Gst::ClockTime& end) const, "get_times") |
| 209 |
|
| 210 |
/** Start processing. Ideal for opening resources in the subclass. |
| 211 |
*/ |
| 212 |
_WRAP_VFUNC(bool start(), "start", return_value true) |
| 213 |
|
| 214 |
/** Stop processing. Subclasses should use this to close resources. |
| 215 |
*/ |
| 216 |
_WRAP_VFUNC(bool stop(), "stop", return_value true) |
| 217 |
|
| 218 |
/** Unlock any pending access to the resource. Subclasses should unblock any |
| 219 |
* blocked function ASAP. |
| 220 |
*/ |
| 221 |
_WRAP_VFUNC(bool unlock(), "unlock", return_value true) |
| 222 |
|
| 223 |
#m4 _CONVERSION(`GstEvent*', `const Glib::RefPtr<Gst::Event>&', `Glib::wrap($3, true)') |
| 224 |
/** Override this to handle events arriving on the sink pad. |
| 225 |
*/ |
| 226 |
_WRAP_VFUNC(bool event(const Glib::RefPtr<Gst::Event>& event), "event", return_value true) |
| 227 |
|
| 228 |
/** Override this to implement custom logic to wait for the event time (for events |
| 229 |
* like EOS and GAP). Subclasses should always first chain up to the default |
| 230 |
* implementation. |
| 231 |
*/ |
| 232 |
_WRAP_VFUNC(FlowReturn wait_event(const Glib::RefPtr<Gst::Event>& event), "wait_event") |
| 233 |
|
| 234 |
/** Called to present the preroll buffer if desired. |
| 235 |
*/ |
| 236 |
_WRAP_VFUNC(FlowReturn preroll(const Glib::RefPtr<Gst::Buffer>& buffer), "preroll") |
| 237 |
|
| 238 |
|
| 239 |
/** Called when a buffer should be presented or output, at the correct moment |
| 240 |
* if the Gst::BaseSink has been set to sync to the clock. |
| 241 |
*/ |
| 242 |
_WRAP_VFUNC(FlowReturn render(const Glib::RefPtr<Gst::Buffer>& buffer), "render") |
| 243 |
|
| 244 |
/** Subclasses should override this when they need to perform special |
| 245 |
* processing when changing to the PLAYING state asynchronously. Called with |
| 246 |
* the OBJECT_LOCK held. |
| 247 |
*/ |
| 248 |
|
| 249 |
/** Subclasses should override this when they can provide an alternate method |
| 250 |
* of spawning a thread to drive the pipeline in pull mode. Should start or |
| 251 |
* stop the pulling thread, depending on the value of the "active" argument. |
| 252 |
* Called after actually activating the sink pad in pull mode. The default |
| 253 |
* implementation starts a task on the sink pad. |
| 254 |
*/ |
| 255 |
_WRAP_VFUNC(bool activate_pull(bool active), "activate_pull", return_value true) |
| 256 |
|
| 257 |
/** Only useful in pull mode, this vmethod will be called in response to |
| 258 |
* Gst::Pad::fixate_caps() being called on the sink pad. Implement if you |
| 259 |
* have ideas about what should be the default values for the caps you |
| 260 |
* support. |
| 261 |
*/ |
| 262 |
_WRAP_VFUNC(Glib::RefPtr<Gst::Caps> fixate(const Glib::RefPtr<Gst::Caps>& caps), "fixate", refreturn_ctype) |
| 263 |
|
| 264 |
/** Clear the previous unlock request. Subclasses should clear any state they |
| 265 |
* set during unlock_vfunc(), such as clearing command queues. |
| 266 |
*/ |
| 267 |
_WRAP_VFUNC(bool unlock_stop(), "unlock_stop", return_value true) |
| 268 |
|
| 269 |
#m4 _CONVERSION(`GstBufferList*', `const Glib::RefPtr<Gst::BufferList>&', `Glib::wrap($3, true)') |
| 270 |
/// Render a BufferList. |
| 271 |
/** Same as render but used with buffer lists instead of buffers. |
| 272 |
*/ |
| 273 |
_WRAP_VFUNC(FlowReturn render_list(const Glib::RefPtr<Gst::BufferList>& buffer_list), "render_list") |
| 274 |
|
| 275 |
/** Called to prepare the buffer for render and preroll. |
| 276 |
* This function is called before synchronisation is performed. |
| 277 |
*/ |
| 278 |
_WRAP_VFUNC(FlowReturn prepare(const Glib::RefPtr<Gst::Buffer>& buffer), "prepare") |
| 279 |
|
| 280 |
/** Called to prepare the buffer list for render_list. |
| 281 |
* This function is called before synchronisation is performed. |
| 282 |
*/ |
| 283 |
_WRAP_VFUNC(FlowReturn prepare_list(const Glib::RefPtr<Gst::BufferList>& buffer_list), "prepare_list") |
| 284 |
|
| 285 |
// Cannot use query_vfunc because the name has been used in the Gst::Element class. |
| 286 |
// We have to provide custom implementation, because query vfunc expects writable |
| 287 |
// query object. |
| 288 |
/** Perform a GstQuery on the element. |
| 289 |
*/ |
| 290 |
virtual bool base_sink_query_vfunc(const Glib::RefPtr<Gst::Query>& query); |
| 291 |
|
| 292 |
/** Configure the allocation query. |
| 293 |
*/ |
| 294 |
#m4 _CONVERSION(`GstQuery*', `const Glib::RefPtr<Gst::Query>&', `Glib::wrap($3, true)') |
| 295 |
_WRAP_VFUNC(bool propose_allocation(const Glib::RefPtr<Gst::Query>& query), "propose_allocation") |
| 296 |
|
| 297 |
protected: |
| 298 |
#m4begin |
| 299 |
_PUSH(SECTION_PCC_CLASS_INIT_VFUNCS) |
| 300 |
klass->query = &query_vfunc_callback; |
| 301 |
_SECTION(SECTION_PH_VFUNCS) |
| 302 |
static gboolean query_vfunc_callback(GstBaseSink* self, GstQuery* query); |
| 303 |
_POP() |
| 304 |
#m4end |
| 305 |
}; |
| 306 |
|
| 307 |
} // namespace Gst |