## GstBaseSrc GstBaseSrc — Base class for getrange based source elements |

## Functions

GstFlowReturn | gst_base_src_wait_playing ()
GstFlowReturn | gst_base_src_start_wait ()
void | gst_base_src_start_complete ()
gboolean | gst_base_src_is_live ()
void | gst_base_src_set_live ()
void | gst_base_src_set_format ()
gboolean | gst_base_src_query_latency ()
guint | gst_base_src_get_blocksize ()
void | gst_base_src_set_blocksize ()
gboolean | gst_base_src_get_do_timestamp ()
void | gst_base_src_set_do_timestamp ()
void | gst_base_src_set_dynamic_size ()
void | gst_base_src_set_automatic_eos ()
gboolean | gst_base_src_new_seamless_segment ()
gboolean | gst_base_src_set_caps ()
void | gst_base_src_get_allocator ()
GstBufferPool * | gst_base_src_get_buffer_pool ()
gboolean | gst_base_src_is_async ()
void | gst_base_src_set_async ()
#define | GST_BASE_SRC_PAD()
#define | GST_BASE_SRC_IS_STARTED()
#define | GST_BASE_SRC_IS_STARTING()

## Properties

guint | blocksize | Read / Write
gboolean | do-timestamp | Read / Write
gint | num-buffers | Read / Write
gboolean | typefind | Read / Write

## Types and Values

struct | GstBaseSrc
struct | GstBaseSrcClass
enum | GstBaseSrcFlags

## Object Hierarchy

        GObject
        ╰── GInitiallyUnowned
            ╰── GstObject
                ╰── GstElement
                    ╰── GstBaseSrc
                        ╰── GstPushSrc

## Includes

    #include <gst/base/gstbasesrc.h>

## Description

This is a generic base class for source elements. The following types of sources are supported:

- random access sources like files

- seekable sources

- live sources

The source can be configured to operate in any GstFormat with the `gst_base_src_set_format()` method. The currently set format determines the format of the internal GstSegment and any `GST_EVENT_SEGMENT` events. The default format for GstBaseSrc is `GST_FORMAT_BYTES`.

GstBaseSrc always supports push mode scheduling. If the following conditions are met, it also supports pull mode scheduling:

- The format is set to `GST_FORMAT_BYTES` (default).

- `GstBaseSrcClass.is_seekable()` returns `TRUE`.

If all the conditions are met for operating in pull mode, GstBaseSrc is automatically seekable in push mode as well. The following conditions must be met to make the element seekable in push mode when the format is not `GST_FORMAT_BYTES`:

- `GstBaseSrcClass.is_seekable()` returns `TRUE`.

- `GstBaseSrcClass.query()` can convert all supported seek formats to the internal format as set with `gst_base_src_set_format()`.

- `GstBaseSrcClass.do_seek()` is implemented, performs the seek and returns `TRUE`.

When the element does not meet the requirements to operate in pull mode, the offset and length in the `GstBaseSrcClass.create()` method should be ignored. It is recommended to subclass GstPushSrc instead, in this situation. If the element can operate in pull mode but only with specific offsets and lengths, it is allowed to generate an error when the wrong values are passed to the `GstBaseSrcClass.create()` function.

GstBaseSrc has support for live sources. Live sources are sources that when paused discard data, such as audio or video capture devices. A typical live source also produces data at a fixed rate and thus provides a clock to publish this rate. Use `gst_base_src_set_live()` to activate the live source mode.

A live source does not produce data in the PAUSED state. This means that the `GstBaseSrcClass.create()` method will not be called in PAUSED but only in PLAYING. To signal the pipeline that the element will not produce data, the return value from the READY to PAUSED state will be `GST_STATE_CHANGE_NO_PREROLL`.

A typical live source will timestamp the buffers it creates with the current running time of the pipeline. This is one reason why a live source can only produce data in the PLAYING state, when the clock is actually distributed and running.

Live sources that synchronize and block on the clock (an audio source, for example) can use `gst_base_src_wait_playing()` when the `GstBaseSrcClass.create()` function was interrupted by a state change to PAUSED.

The `GstBaseSrcClass.get_times()` method can be used to implement pseudo-live sources. It only makes sense to implement the `GstBaseSrcClass.get_times()` function if the source is a live source. The `GstBaseSrcClass.get_times()` function should return timestamps starting from 0, as if it were a non-live source. The base class will make sure that the timestamps are transformed into the current running_time. The base source will then wait for the calculated running_time before pushing out the buffer.

For live sources, the base class will by default report a latency of 0. For pseudo live sources, the base class will by default measure the difference between the first buffer timestamp and the start time of get_times and will report this value as the latency. Subclasses should override the query function when this behaviour is not acceptable.

There is only support in GstBaseSrc for exactly one source pad, which should be named "src". A source implementation (subclass of GstBaseSrc) should install a pad template in its class_init function, like so:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    static void
    my_element_class_init (GstMyElementClass *klass)
    {
      GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
      // srctemplate should be a #GstStaticPadTemplate with direction
      // %GST_PAD_SRC and name "src"
      gst_element_class_add_pad_template (gstelement_class,
          gst_static_pad_template_get (&srctemplate));

      gst_element_class_set_static_metadata (gstelement_class,
         "Source name",
         "Source",
         "My Source element",
         "The author <my.sink@my.email>");
    }

### Controlled shutdown of live sources in applications

Applications that record from a live source may want to stop recording in a controlled way, so that the recording is stopped, but the data already in the pipeline is processed to the end (remember that many live sources would go on recording forever otherwise). For that to happen the application needs to make the source stop recording and send an EOS event down the pipeline. The application would then wait for an EOS message posted on the pipeline's bus to know when all data has been processed and the pipeline can safely be stopped. An application may send an EOS event to a source element to make it perform the EOS logic (send EOS event downstream or post a `GST_MESSAGE_SEGMENT_DONE` on the bus). This can typically be done with the `gst_element_send_event()` function on the element or its parent bin. After the EOS has been sent to the element, the application should wait for an EOS message to be posted on the pipeline's bus. Once this EOS message is received, it may safely shut down the entire pipeline.

## Functions

### gst_base_src_wait_playing ()

    GstFlowReturn
    gst_base_src_wait_playing (GstBaseSrc *src);

If the `GstBaseSrcClass.create()` 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 produce 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_FLUSHING`).

#### Parameters

src | the src |

#### Returns

`GST_FLOW_OK` if `src` is PLAYING and processing can continue. Any other return value should be returned from the create vmethod.

### gst_base_src_start_wait ()

    GstFlowReturn
    gst_base_src_start_wait (GstBaseSrc *basesrc);

Wait until the start operation completes.

#### Parameters

basesrc | base source instance |

#### Returns

a GstFlowReturn.

### gst_base_src_start_complete ()

    void
    gst_base_src_start_complete (GstBaseSrc *basesrc,
                                 GstFlowReturn ret);

Complete an asynchronous start operation. When the subclass overrides the start method, it should call `gst_base_src_start_complete()` when the start operation completes either from the same thread or from an asynchronous helper thread.

#### Parameters

basesrc | base source instance |
ret | a GstFlowReturn |

### gst_base_src_is_live ()

    gboolean
    gst_base_src_is_live (GstBaseSrc *src);

Check if an element is in live mode.

#### Parameters

src | base source instance |

#### Returns

`TRUE` if element is in live mode.

### gst_base_src_set_live ()

    void
    gst_base_src_set_live (GstBaseSrc *src,
                           gboolean live);

If the element listens to a live source, `live` should be set to `TRUE`.

A live source will not produce data in the PAUSED state and will therefore not be able to participate in the PREROLL phase of a pipeline. To signal this fact to the application and the pipeline, the state change return value of the live source will be GST_STATE_CHANGE_NO_PREROLL.

#### Parameters

src | base source instance |
live | new live-mode |

### gst_base_src_set_format ()

    void
    gst_base_src_set_format (GstBaseSrc *src,
                             GstFormat format);

Sets the default format of the source. This will be the format used for sending SEGMENT events and for performing seeks.

If a format of GST_FORMAT_BYTES is set, the element will be able to operate in pull mode if the `GstBaseSrcClass.is_seekable()` returns `TRUE`.

This function must only be called in states < `GST_STATE_PAUSED`.

#### Parameters

src | base source instance |
format | the format to use |

### gst_base_src_query_latency ()

    gboolean
    gst_base_src_query_latency (GstBaseSrc *src,
                                gboolean *live,
                                GstClockTime *min_latency,
                                GstClockTime *max_latency);

Query the source for the latency parameters. `live` will be `TRUE` when `src` is configured as a live source. `min_latency` will be set to the difference between the running time and the timestamp of the first buffer. `max_latency` is always the undefined value of -1.

This function is mostly used by subclasses.

#### Parameters

src | the source |
live | if the source is live. | [out][allow-none]
min_latency | the min latency of the source. | [out][allow-none]
max_latency | the max latency of the source. | [out][allow-none]

#### Returns

`TRUE` if the query succeeded.

### gst_base_src_get_blocksize ()

    guint
    gst_base_src_get_blocksize (GstBaseSrc *src);

Get the number of bytes that `src` will push out with each buffer.

#### Parameters

src | the source |

#### Returns

the number of bytes pushed with each buffer.

### gst_base_src_set_blocksize ()

    void
    gst_base_src_set_blocksize (GstBaseSrc *src,
                                guint blocksize);

Set the number of bytes that `src` will push out with each buffer. When `blocksize` is set to -1, a default length will be used.

#### Parameters

src | the source |
blocksize | the new blocksize in bytes |

### gst_base_src_get_do_timestamp ()

    gboolean
    gst_base_src_get_do_timestamp (GstBaseSrc *src);

Query if `src` timestamps outgoing buffers based on the current running_time.

#### Parameters

src | the source |

#### Returns

`TRUE` if the base class will automatically timestamp outgoing buffers.

### gst_base_src_set_do_timestamp ()

    void
    gst_base_src_set_do_timestamp (GstBaseSrc *src,
                                   gboolean timestamp);

Configure `src` to automatically timestamp outgoing buffers based on the current running_time of the pipeline. This property is mostly useful for live sources.

#### Parameters

src | the source |
timestamp | enable or disable timestamping |

### gst_base_src_set_dynamic_size ()

    void
    gst_base_src_set_dynamic_size (GstBaseSrc *src,
                                   gboolean dynamic);

If not `dynamic` , size is only updated when needed, such as when trying to read past current tracked size. Otherwise, size is checked for upon each read.

#### Parameters

src | base source instance |
dynamic | new dynamic size mode |

### gst_base_src_set_automatic_eos ()

    void
    gst_base_src_set_automatic_eos (GstBaseSrc *src,
                                    gboolean automatic_eos);

If `automatic_eos` is `TRUE`, `src` will automatically go EOS if a buffer after the total size is returned. By default this is `TRUE` but sources that can't return an authoritative size and only know that they're EOS when trying to read more should set this to `FALSE`.

#### Parameters

src | base source instance |
automatic_eos | automatic eos |

Since 1.4

### gst_base_src_new_seamless_segment ()

    gboolean
    gst_base_src_new_seamless_segment (GstBaseSrc *src,
                                       gint64 start,
                                       gint64 stop,
                                       gint64 time);

Prepare a new seamless segment for emission downstream. This function must only be called by derived sub-classes, and only from the `create()` function, as the stream-lock needs to be held.

The format for the new segment will be the current format of the source, as configured with `gst_base_src_set_format()`

#### Parameters

src | The source |
start | The new start value for the segment |
stop | Stop value for the new segment |
time | The new time value for the start of the new segment |

#### Returns

`TRUE` if preparation of the seamless segment succeeded.

### gst_base_src_set_caps ()

    gboolean
    gst_base_src_set_caps (GstBaseSrc *src,
                           GstCaps *caps);

Set new caps on the basesrc source pad.

#### Parameters

src | a GstBaseSrc |
caps | a GstCaps |

#### Returns

`TRUE` if the caps could be set

### gst_base_src_get_allocator ()

    void
    gst_base_src_get_allocator (GstBaseSrc *src,
                                GstAllocator **allocator,
                                GstAllocationParams *params);

Lets GstBaseSrc sub-classes to know the memory `allocator` used by the base class and its `params` .

Unref the `allocator` after use it.

#### Parameters

src | a GstBaseSrc |
allocator | the GstAllocator used. | [out][allow-none][transfer full]
params | the GstAllocationParams of `allocator` . | [out][allow-none][transfer full]

### gst_base_src_get_buffer_pool ()

    GstBufferPool *
    gst_base_src_get_buffer_pool (GstBaseSrc *src);

#### Parameters

src | a GstBaseSrc |

#### Returns

the instance of the GstBufferPool used by the src; free it after use it.

[transfer full]

### gst_base_src_is_async ()

    gboolean
    gst_base_src_is_async (GstBaseSrc *src);

Get the current async behaviour of `src` . See also `gst_base_src_set_async()`.

#### Parameters

src | base source instance |

#### Returns

`TRUE` if `src` is operating in async mode.

### gst_base_src_set_async ()

    void
    gst_base_src_set_async (GstBaseSrc *src,
                            gboolean async);

Configure async behaviour in `src` , no state change will block. The open, close, start, stop, play and pause virtual methods will be executed in a different thread and are thus allowed to perform blocking operations. Any blocking operation should be unblocked with the unlock vmethod.

#### Parameters

src | base source instance |
async | new async mode |

### GST_BASE_SRC_PAD()

    #define GST_BASE_SRC_PAD(obj)                 (GST_BASE_SRC_CAST (obj)->srcpad)

Gives the pointer to the GstPad object of the element.

#### Parameters

obj | base source instance |

### GST_BASE_SRC_IS_STARTED()

    #define GST_BASE_SRC_IS_STARTED(obj)  GST_OBJECT_FLAG_IS_SET ((obj), GST_BASE_SRC_FLAG_STARTED)

### GST_BASE_SRC_IS_STARTING()

    #define GST_BASE_SRC_IS_STARTING(obj) GST_OBJECT_FLAG_IS_SET ((obj), GST_BASE_SRC_FLAG_STARTING)

## Types and Values

### struct GstBaseSrc

    struct GstBaseSrc;

The opaque GstBaseSrc data structure.

### struct GstBaseSrcClass

    struct GstBaseSrcClass {
      GstElementClass parent_class;

      /* virtual methods for subclasses */

      /* get caps from subclass */
      GstCaps*      (*get_caps)     (GstBaseSrc *src, GstCaps *filter);
      /* decide on caps */
      gboolean      (*negotiate)    (GstBaseSrc *src);
      /* called if, in negotiation, caps need fixating */
      GstCaps *     (*fixate)       (GstBaseSrc *src, GstCaps *caps);
      /* notify the subclass of new caps */
      gboolean      (*set_caps)     (GstBaseSrc *src, GstCaps *caps);

      /* setup allocation query */
      gboolean      (*decide_allocation)   (GstBaseSrc *src, GstQuery *query);

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

      /* given a buffer, return start and stop time when it should be pushed
       * out. The base class will sync on the clock using these times. */
      void          (*get_times)    (GstBaseSrc *src, GstBuffer *buffer,
                                     GstClockTime *start, GstClockTime *end);

      /* get the total size of the resource in bytes */
      gboolean      (*get_size)     (GstBaseSrc *src, guint64 *size);

      /* check if the resource is seekable */
      gboolean      (*is_seekable)  (GstBaseSrc *src);

      /* Prepare the segment on which to perform do_seek(), converting to the
       * current basesrc format. */
      gboolean      (*prepare_seek_segment) (GstBaseSrc *src, GstEvent *seek,
                                             GstSegment *segment);
      /* notify subclasses of a seek */
      gboolean      (*do_seek)      (GstBaseSrc *src, GstSegment *segment);

      /* unlock any pending access to the resource. subclasses should unlock
       * any function ASAP. */
      gboolean      (*unlock)       (GstBaseSrc *src);
      /* Clear any pending unlock request, as we succeeded in unlocking */
      gboolean      (*unlock_stop)  (GstBaseSrc *src);

      /* notify subclasses of a query */
      gboolean      (*query)        (GstBaseSrc *src, GstQuery *query);

      /* notify subclasses of an event */
      gboolean      (*event)        (GstBaseSrc *src, GstEvent *event);

      /* ask the subclass to create a buffer with offset and size, the default
       * implementation will call alloc and fill. */
      GstFlowReturn (*create)       (GstBaseSrc *src, guint64 offset, guint size,
                                     GstBuffer **buf);
      /* ask the subclass to allocate an output buffer. The default implementation
       * will use the negotiated allocator. */
      GstFlowReturn (*alloc)        (GstBaseSrc *src, guint64 offset, guint size,
                                     GstBuffer **buf);
      /* ask the subclass to fill the buffer with data from offset and size */
      GstFlowReturn (*fill)         (GstBaseSrc *src, guint64 offset, guint size,
                                     GstBuffer *buf);
    };

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

#### Members

GstElementClass `parent_class`; | Element parent class |
`get_caps` () | Called to get the caps to report |
`negotiate` () | Negotiated the caps with the peer. |
`fixate` () | Called during negotiation if caps need fixating. Implement instead of setting a fixate function on the source pad. |
`set_caps` () | Notify subclass of changed output caps |
`decide_allocation` () | configure the allocation query |
`start` () | Start processing. Subclasses should open resources and prepare to produce data. Implementation should call `gst_base_src_start_complete()` when the operation completes, either from the current thread or any other thread that finishes the start operation asynchronously. |
`stop` () | Stop processing. Subclasses should use this to close resources. |
`get_times` () | Given a buffer, return the start and stop time when it should be pushed out. The base class will sync on the clock using these times. |
`get_size` () | Return the total size of the resource, in the configured format. |
`is_seekable` () | Check if the source can seek |
`prepare_seek_segment` () | Prepare the GstSegment that will be passed to the `GstBaseSrcClass.do_seek()` vmethod for executing a seek request. Sub-classes should override this if they support seeking in formats other than the configured native format. By default, it tries to convert the seek arguments to the configured native format and prepare a segment in that format. |
`do_seek` () | Perform seeking on the resource to the indicated segment. |
`unlock` () | Unlock any pending access to the resource. Subclasses should unblock any blocked function ASAP. In particular, any `create()` function in progress should be unblocked and should return GST_FLOW_FLUSHING. Any future `GstBaseSrcClass.create()` function call should also return GST_FLOW_FLUSHING until the `GstBaseSrcClass.unlock_stop()` function has been called. |
`unlock_stop` () | Clear the previous unlock request. Subclasses should clear any state they set during `GstBaseSrcClass.unlock()`, such as clearing command queues. |
`query` () | Handle a requested query. |
`event` () | Override this to implement custom event handling. |
`create` () | Ask the subclass to create a buffer with offset and size. When the subclass returns GST_FLOW_OK, it MUST return a buffer of the requested size unless fewer bytes are available because an EOS condition is near. No buffer should be returned when the return value is different from GST_FLOW_OK. A return value of GST_FLOW_EOS signifies that the end of stream is reached. The default implementation will call `GstBaseSrcClass.alloc()` and then call `GstBaseSrcClass.fill()`. |
`alloc` () | Ask the subclass to allocate a buffer with for offset and size. The default implementation will create a new buffer from the negotiated allocator. |
`fill` () | Ask the subclass to fill the buffer with data for offset and size. The passed buffer is guaranteed to hold the requested amount of bytes. |

### enum GstBaseSrcFlags

The GstElement flags that a basesrc element may have.

#### Members

GST_BASE_SRC_FLAG_STARTING | has source is starting |
GST_BASE_SRC_FLAG_STARTED | has source been started |
GST_BASE_SRC_FLAG_LAST | offset to define more flags |

## Property Details

### The `“blocksize”` property

      “blocksize”                guint

Size in bytes to read per buffer (-1 = default).

Flags: Read / Write

Default value: 4096

### The `“do-timestamp”` property

      “do-timestamp”             gboolean

Apply current stream time to buffers.

Flags: Read / Write

Default value: FALSE

### The `“num-buffers”` property

      “num-buffers”              gint

Number of buffers to output before sending EOS (-1 = unlimited).

Flags: Read / Write

Allowed values: >= -1

Default value: -1

### The `“typefind”` property

      “typefind”                 gboolean

Run typefind before negotiating.

Flags: Read / Write

Default value: FALSE

## See Also

GstPushSrc, GstBaseTransform, GstBaseSink