## GstBaseSink GstBaseSink — Base class for sink elements |

## Synopsis

    #include <gst/base/gstbasesink.h>

                        GstBaseSink;
                        GstBaseSinkClass;
    gboolean            gst_base_sink_query_latency         (GstBaseSink *sink,
                                                             gboolean *live,
                                                             gboolean *upstream_live,
                                                             GstClockTime *min_latency,
                                                             GstClockTime *max_latency);
    GstClockTime        gst_base_sink_get_latency           (GstBaseSink *sink);
    GstFlowReturn       gst_base_sink_wait_preroll          (GstBaseSink *sink);
    GstFlowReturn       gst_base_sink_wait_eos              (GstBaseSink *sink,
                                                             GstClockTime time,
                                                             GstClockTimeDiff *jitter);
    void                gst_base_sink_set_sync              (GstBaseSink *sink,
                                                             gboolean sync);
    gboolean            gst_base_sink_get_sync              (GstBaseSink *sink);
    void                gst_base_sink_set_max_lateness      (GstBaseSink *sink,
                                                             gint64 max_lateness);
    gint64              gst_base_sink_get_max_lateness      (GstBaseSink *sink);
    void                gst_base_sink_set_qos_enabled       (GstBaseSink *sink,
                                                             gboolean enabled);
    gboolean            gst_base_sink_is_qos_enabled        (GstBaseSink *sink);
    void                gst_base_sink_set_async_enabled     (GstBaseSink *sink,
                                                             gboolean enabled);
    gboolean            gst_base_sink_is_async_enabled      (GstBaseSink *sink);
    void                gst_base_sink_set_ts_offset         (GstBaseSink *sink,
                                                             GstClockTimeDiff offset);
    GstClockTimeDiff    gst_base_sink_get_ts_offset         (GstBaseSink *sink);
    GstBuffer*          gst_base_sink_get_last_buffer       (GstBaseSink *sink);
    #define             GST_BASE_SINK_PAD                   (obj)

## Object Hierarchy

      GObject
       +----GstObject
             +----GstElement
                   +----GstBaseSink

## Properties

      "async"                    gboolean              : Read / Write
      "last-buffer"              GstBuffer             : Read
      "max-lateness"             gint64                : Read / Write
      "preroll-queue-len"        guint                 : Read / Write / Construct
      "qos"                      gboolean              : Read / Write
      "sync"                     gboolean              : Read / Write
      "ts-offset"                gint64                : Read / Write

## Description

GstBaseSink is the base class for sink elements in GStreamer, such as xvimagesink or filesink. It is a layer on top of GstElement that provides a simplified interface to plugin writers. GstBaseSink handles many details for you, for example: preroll, clock synchronization, state changes, activation in push or pull mode, and queries.

In most cases, when writing sink elements, there is no need to implement class methods from GstElement or to set functions on pads, because the GstBaseSink infrastructure should be sufficient.

GstBaseSink provides support for exactly one sink pad, which should be named "sink". A sink implementation (subclass of GstBaseSink) should install a pad template in its base_init function, like so:

    static void
    my_element_base_init (gpointer g_class)
    {
      GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);

      // sinktemplate should be a GstStaticPadTemplate with direction
      // GST_PAD_SINK and name "sink"
      gst_element_class_add_pad_template (gstelement_class,
          gst_static_pad_template_get (&sinktemplate));
      // see GstElementDetails
      gst_element_class_set_details (gstelement_class, &details);
    }

GstBaseSink will handle the prerolling correctly. This means that it will return GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first buffer arrives in this element. The base class will call the GstBaseSink::preroll vmethod with this preroll buffer and will then commit the state change to the next asynchronously pending state.

When the element is set to PLAYING, GstBaseSink will synchronise on the clock using the times returned from ::get_times. If this function returns GST_CLOCK_TIME_NONE for the start time, no synchronisation will be done. Synchronisation can be disabled entirely by setting the object "sync" property to `FALSE`.

After synchronisation the virtual method GstBaseSink::render will be called. Subclasses should minimally implement this method.

Since 0.10.3 subclasses that synchronise on the clock in the ::render method are supported as well. These classes typically receive a buffer in the render method and can then potentially block on the clock while rendering. A typical example is an audiosink. Since 0.10.11 these subclasses can use `gst_base_sink_wait_preroll()` to perform the blocking wait.

Upon receiving the EOS event in the PLAYING state, GstBaseSink will wait for the clock to reach the time indicated by the stop time of the last ::get_times call before posting an EOS message. When the element receives EOS in PAUSED, preroll completes, the event is queued and an EOS message is posted when going to PLAYING.

GstBaseSink will internally use the GST_EVENT_NEWSEGMENT events to schedule synchronisation and clipping of buffers. Buffers that fall completely outside of the current segment are dropped. Buffers that fall partially in the segment are rendered (and prerolled). Subclasses should do any subbuffer clipping themselves when needed.

GstBaseSink will by default report the current playback position in GST_FORMAT_TIME based on the current clock time and segment information. If no clock has been set on the element, the query will be forwarded upstream.

The ::set_caps function will be called when the subclass should configure itself to process a specific media type.

The ::start and ::stop virtual methods will be called when resources should be allocated. Any ::preroll, ::render and ::set_caps function will be called between the ::start and ::stop calls.

The ::event virtual method will be called when an event is received by GstBaseSink. Normally this method should only be overriden by very specific elements (such as file sinks) which need to handle the newsegment event specially.

GstBaseSink provides an overridable ::buffer_alloc function that can be used by sinks that want to do reverse negotiation or to provide custom buffers (hardware buffers for example) to upstream elements.

The ::unlock method is called when the elements should unblock any blocking operations they perform in the ::render method. This is mostly useful when the ::render method performs a blocking write on a file descriptor, for example.

The max-lateness property affects how the sink deals with buffers that arrive too late in the sink. A buffer arrives too late in the sink when the presentation time (as a combination of the last segment, buffer timestamp and element base_time) plus the duration is before the current time of the clock. If the frame is later than max-lateness, the sink will drop the buffer without calling the render method. This feature is disabled if sync is disabled, the ::get-times method does not return a valid start time or max-lateness is set to -1 (the default). Subclasses can use `gst_base_sink_set_max_lateness()` to configure the max-lateness value.

The qos property will enable the quality-of-service features of the basesink which gather statistics about the real-time performance of the clock synchronisation. For each buffer received in the sink, statistics are gathered and a QOS event is sent upstream with these numbers. This information can then be used by upstream elements to reduce their processing rate, for example.

Since 0.10.15 the async property can be used to instruct the sink to never perform an ASYNC state change. This feature is mostly usable when dealing with non-synchronized streams or sparse streams.

Last reviewed on 2007-08-29 (0.10.15)

## Details

### GstBaseSink

    typedef struct _GstBaseSink GstBaseSink;

The opaque GstBaseSink data structure.

### GstBaseSinkClass

    typedef struct {
      GstElementClass parent_class;

      /* get caps from subclass */
      GstCaps*      (*get_caps)     (GstBaseSink *sink);
      /* notify subclass of new caps */
      gboolean      (*set_caps)     (GstBaseSink *sink, GstCaps *caps);

      /* allocate a new buffer with given caps */
      GstFlowReturn (*buffer_alloc) (GstBaseSink *sink, guint64 offset, guint size,
    		                 GstCaps *caps, GstBuffer **buf);

      /* get the start and end times for syncing on this buffer */
      void		(*get_times)    (GstBaseSink *sink, GstBuffer *buffer,
    		                 GstClockTime *start, GstClockTime *end);

      /* start and stop processing, ideal for opening/closing the resource */
      gboolean      (*start)        (GstBaseSink *sink);
      gboolean      (*stop)         (GstBaseSink *sink);

      /* unlock any pending access to the resource. subclasses should unlock
       * any function ASAP. */
      gboolean      (*unlock)       (GstBaseSink *sink);

      /* notify subclass of event, preroll buffer or real buffer */
      gboolean      (*event)        (GstBaseSink *sink, GstEvent *event);
      GstFlowReturn (*preroll)      (GstBaseSink *sink, GstBuffer *buffer);
      GstFlowReturn (*render)       (GstBaseSink *sink, GstBuffer *buffer);

      /* ABI additions */

      /* when an ASYNC state change to PLAYING happens */ /* with LOCK */
      GstStateChangeReturn (*async_play)   (GstBaseSink *sink);

      /* start or stop a pulling thread */
      gboolean	(*activate_pull)(GstBaseSink *sink, gboolean active);

      /* fixate sink caps during pull-mode negotiation */
      void		(*fixate)	(GstBaseSink *sink, GstCaps *caps);

      /* Clear a previously indicated unlock request not that unlocking is
       * complete. Sub-classes should clear any command queue or indicator they
       * set during unlock */
      gboolean      (*unlock_stop)       (GstBaseSink *sink);
    } GstBaseSinkClass;

Subclasses can override any of the available virtual methods or not, as needed. At the minimum, the `render` method should be overridden to output/present buffers.

GstElementClass `parent_class`; | Element parent class
`get_caps` () | Called to get sink pad caps from the subclass
`set_caps` () | Notify subclass of changed caps
`buffer_alloc` () | Subclasses can override to perform custom buffer allocations
`get_times` () | Called to get the start and end times for synchronising the passed buffer to the clock
`start` () | Start processing. Ideal for opening resources in the subclass
`stop` () | Stop processing. Subclasses should use this to close resources.
`unlock` () | Unlock any pending access to the resource. Subclasses should unblock any blocked function ASAP
`event` () | Override this to handle events arriving on the sink pad
`preroll` () | Called to present the preroll buffer if desired
`render` () | Called when a buffer should be presented or output, at the correct moment if the GstBaseSink has been set to sync to the clock.
`async_play` () | Subclasses should override this when they need to perform special processing when changing to the PLAYING state asynchronously. Called with the OBJECT_LOCK held.
`activate_pull` () | Subclasses should override this when they can provide an alternate method of spawning a thread to drive the pipeline in pull mode. Should start or stop the pulling thread, depending on the value of the "active" argument. Called after actually activating the sink pad in pull mode. The default implementation starts a task on the sink pad.
`fixate` () | Only useful in pull mode, this vmethod will be called in response to `gst_pad_fixate_caps()` being called on the sink pad. Implement if you have ideas about what should be the default values for the caps you support.
`unlock_stop` () | Clear the previous unlock request. Subclasses should clear any state they set during `unlock()`, such as clearing command queues.

### gst_base_sink_query_latency ()

    gboolean            gst_base_sink_query_latency         (GstBaseSink *sink,
                                                             gboolean *live,
                                                             gboolean *upstream_live,
                                                             GstClockTime *min_latency,
                                                             GstClockTime *max_latency);

Query the sink for the latency parameters. The latency will be queried from the upstream elements. `live` will be TRUE if `sink` is configured to synchronize against the clock. `upstream_live` will be TRUE if an upstream element is live.

If both `live` and `upstream_live` are TRUE, the sink will want to compensate for the latency introduced by the upstream elements by setting the `min_latency` to a strictly possitive value.

This function is mostly used by subclasses.

`sink` : | the sink
`live` : | if the sink is live
`upstream_live` : | if an upstream element is live
`min_latency` : | the min latency of the upstream elements
`max_latency` : | the max latency of the upstream elements
Returns : | TRUE if the query succeeded.

Since 0.10.12

### gst_base_sink_get_latency ()

    GstClockTime        gst_base_sink_get_latency           (GstBaseSink *sink);

Get the currently configured latency.

`sink` : | the sink
Returns : | The configured latency.

Since 0.10.12

### gst_base_sink_wait_preroll ()

    GstFlowReturn       gst_base_sink_wait_preroll          (GstBaseSink *sink);

If the GstBaseSinkClass::render method performs its own synchronisation against the clock it must unblock when going from PLAYING to the PAUSED state and call this method before continuing to render the remaining data.

This function will block until a state change to PLAYING happens (in which case this function returns GST_FLOW_OK) or the processing must be stopped due to a state change to READY or a FLUSH event (in which case this function returns GST_FLOW_WRONG_STATE).

`sink` : | the sink
Returns : | GST_FLOW_OK if the preroll completed and processing can continue. Any other return value should be returned from the render vmethod.

Since 0.10.11

### gst_base_sink_wait_eos ()

    GstFlowReturn       gst_base_sink_wait_eos              (GstBaseSink *sink,
                                                             GstClockTime time,
                                                             GstClockTimeDiff *jitter);

This function will block until `time` is reached. It is usually called by subclasses that use their own internal synchronisation but want to let the EOS be handled by the base class.

This function should only be called with the PREROLL_LOCK held, like when receiving an EOS event in the ::event vmethod.

Since 0.10.15

`sink` : | the sink
`time` : | the running_time to be reached
`jitter` : | the jitter to be filled with time diff (can be NULL)
Returns : | GstFlowReturn

### gst_base_sink_set_sync ()

    void                gst_base_sink_set_sync              (GstBaseSink *sink,
                                                             gboolean sync);

Configures `sink` to synchronize on the clock or not. When `sync` is FALSE, incomming samples will be played as fast as possible. If `sync` is TRUE, the timestamps of the incomming buffers will be used to schedule the exact render time of its contents.

`sink` : | the sink
`sync` : | the new sync value.

Since 0.10.4

### gst_base_sink_get_sync ()

    gboolean            gst_base_sink_get_sync              (GstBaseSink *sink);

Checks if `sink` is currently configured to synchronize against the clock.

`sink` : | the sink
Returns : | TRUE if the sink is configured to synchronize against the clock.

Since 0.10.4

### gst_base_sink_set_max_lateness ()

    void                gst_base_sink_set_max_lateness      (GstBaseSink *sink,
                                                             gint64 max_lateness);

Sets the new max lateness value to `max_lateness`. This value is used to decide if a buffer should be dropped or not based on the buffer timestamp and the current clock time. A value of -1 means an unlimited time.

`sink` : | the sink
`max_lateness` : | the new max lateness value.

Since 0.10.4

### gst_base_sink_get_max_lateness ()

    gint64              gst_base_sink_get_max_lateness      (GstBaseSink *sink);

Gets the max lateness value. See gst_base_sink_set_max_lateness for more details.

`sink` : | the sink
Returns : | The maximum time in nanoseconds that a buffer can be late before it is dropped and not rendered. A value of -1 means an unlimited time.

Since 0.10.4

### gst_base_sink_set_qos_enabled ()

    void                gst_base_sink_set_qos_enabled       (GstBaseSink *sink,
                                                             gboolean enabled);

Configures `sink` to send Quality-of-Service events upstream.

`sink` : | the sink
`enabled` : | the new qos value.

Since 0.10.5

### gst_base_sink_is_qos_enabled ()

    gboolean            gst_base_sink_is_qos_enabled        (GstBaseSink *sink);

Checks if `sink` is currently configured to send Quality-of-Service events upstream.

`sink` : | the sink
Returns : | TRUE if the sink is configured to perform Quality-of-Service.

Since 0.10.5

### gst_base_sink_set_async_enabled ()

    void                gst_base_sink_set_async_enabled     (GstBaseSink *sink,
                                                             gboolean enabled);

Configures `sink` to perform all state changes asynchronusly. When async is disabled, the sink will immediatly go to PAUSED instead of waiting for a preroll buffer. This feature is usefull if the sink does not synchronize against the clock or when it is dealing with sparse streams.

`sink` : | the sink
`enabled` : | the new async value.

Since 0.10.15

### gst_base_sink_is_async_enabled ()

    gboolean            gst_base_sink_is_async_enabled      (GstBaseSink *sink);

Checks if `sink` is currently configured to perform asynchronous state changes to PAUSED.

`sink` : | the sink
Returns : | TRUE if the sink is configured to perform asynchronous state changes.

Since 0.10.15

### gst_base_sink_set_ts_offset ()

    void                gst_base_sink_set_ts_offset         (GstBaseSink *sink,
                                                             GstClockTimeDiff offset);

Adjust the synchronisation of `sink` with `offset`. A negative value will render buffers earlier than their timestamp. A positive value will delay rendering. This function can be used to fix playback of badly timestamped buffers.

`sink` : | the sink
`offset` : | the new offset

Since 0.10.15

### gst_base_sink_get_ts_offset ()

    GstClockTimeDiff    gst_base_sink_get_ts_offset         (GstBaseSink *sink);

Get the synchronisation offset of `sink`.

`sink` : | the sink
Returns : | The synchronisation offset.

Since 0.10.15

### gst_base_sink_get_last_buffer ()

    GstBuffer*          gst_base_sink_get_last_buffer       (GstBaseSink *sink);

Get the last buffer that arrived in the sink and was used for preroll or for rendering. This property can be used to generate thumbnails.

The GstCaps on the buffer can be used to determine the type of the buffer.

`sink` : | the sink
Returns : | a GstBuffer. `gst_buffer_unref()` after usage. This function returns NULL when no buffer has arrived in the sink yet or when the sink is not in PAUSED or PLAYING.

Since 0.10.15

### GST_BASE_SINK_PAD()

    #define GST_BASE_SINK_PAD(obj)		(GST_BASE_SINK_CAST (obj)->sinkpad)

Gives the pointer to the GstPad object of the element.

`obj` : | base sink instance

## Property Details

### The "`async`" property

      "async"                    gboolean              : Read / Write

If set to TRUE, the basesink will perform asynchronous state changes. When set to FALSE, the sink will not signal the parent when it prerolls. Use this option when dealing with sparse streams or when synchronisation is not required.

Default value: TRUE

Since 0.10.15

### The "`last-buffer`" property

      "last-buffer"              GstBuffer             : Read

The last buffer that arrived in the sink and was used for preroll or for rendering. This property can be used to generate thumbnails. This property can be NULL when the sink has not yet received a bufer.

Since 0.10.15

### The "`max-lateness`" property

      "max-lateness"             gint64                : Read / Write

Maximum number of nanoseconds that a buffer can be late before it is dropped (-1 unlimited).

Allowed values: >= -1

Default value: -1

### The "`preroll-queue-len`" property

      "preroll-queue-len"        guint                 : Read / Write / Construct

Number of buffers to queue during preroll.

Default value: 0

### The "`qos`" property

      "qos"                      gboolean              : Read / Write

Generate Quality-of-Service events upstream.

Default value: FALSE

### The "`sync`" property

      "sync"                     gboolean              : Read / Write

Sync on the clock.

Default value: TRUE

### The "`ts-offset`" property

      "ts-offset"                gint64                : Read / Write

Controls the final synchronisation, a negative value will render the buffer earlier while a positive value delays playback. This property can be used to fix synchronisation in bad files.

Default value: 0

Since 0.10.15

## See Also

GstBaseTransform, GstBaseSource