Class BaseSrc

All Implemented Interfaces:
Proxy
Direct Known Subclasses:
AppSrc, BaseSrc.BaseSrc$Impl, PushSrc

@Generated("org.javagi.JavaGI") public abstract class BaseSrc extends Element

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 EventType.SEGMENT events. The default format for GstBaseSrc is 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 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 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 StateChangeReturn.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:

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_static_pad_template (gstelement_class, &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 MessageType.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.

  • Constructor Details

    • BaseSrc

      public BaseSrc(MemorySegment address)
      Create a BaseSrc instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • BaseSrc

      public BaseSrc()
      Create a new BaseSrc.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the BaseSrc class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected BaseSrc asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class Element
      Returns:
      the instance as if it were its parent type
    • getAllocator

      public void getAllocator(@Nullable Out<Allocator> allocator, @Nullable AllocationParams params)

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

      Unref the allocator after usage.

      Parameters:
      allocator - the GstAllocator used
      params - the GstAllocationParams of allocator
    • getBlocksize

      public int getBlocksize()
      Get the number of bytes that this BaseSrc will push out with each buffer.
      Returns:
      the number of bytes pushed with each buffer.
    • getBufferPool

      public @Nullable BufferPool getBufferPool()
    • getDoTimestamp

      public boolean getDoTimestamp()
      Query if this BaseSrc timestamps outgoing buffers based on the current running_time.
      Returns:
      true if the base class will automatically timestamp outgoing buffers.
    • isAsync

      public boolean isAsync()
      Get the current async behaviour of src. See also gst_base_src_set_async().
      Returns:
      true if this BaseSrc is operating in async mode.
    • isLive

      public boolean isLive()
      Check if an element is in live mode.
      Returns:
      true if element is in live mode.
    • negotiate

      public boolean negotiate()

      Negotiates src pad caps with downstream elements. Unmarks GST_PAD_FLAG_NEED_RECONFIGURE in any case. But marks it again if GstBaseSrcClass::negotiate fails.

      Do not call this in the GstBaseSrcClass::fill vmethod. Call this in GstBaseSrcClass::create or in GstBaseSrcClass::alloc, before any buffer is allocated.

      Returns:
      true if the negotiation succeeded, else false.
      Since:
      1.18
    • newSeamlessSegment

      @Deprecated public boolean newSeamlessSegment(long start, long stop, long time)
      Deprecated.
      Use gst_base_src_new_segment()

      Prepare a new seamless segment for emission downstream. This function must only be called by derived sub-classes, and only from the GstBaseSrcClass::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:
      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.
    • newSegment

      public boolean newSegment(Segment segment)

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

      The format for the segment must be identical with the current format of the source, as configured with gst_base_src_set_format().

      The format of this BaseSrc must not be Format.UNDEFINED and the format should be configured via gst_base_src_set_format() before calling this method.

      Parameters:
      segment - a pointer to a GstSegment
      Returns:
      true if preparation of new segment succeeded.
      Since:
      1.18
    • pushSegment

      public boolean pushSegment(Segment segment)

      Send a new segment downstream. This function must only be called by derived sub-classes, and only from the GstBaseSrcClass::create function, as the stream-lock needs to be held. This method also requires that an out caps has been configured, so gst_base_src_set_caps() needs to have been called before.

      The format for the segment must be identical with the current format of the source, as configured with gst_base_src_set_format().

      The format of this BaseSrc must not be Format.UNDEFINED and the format should be configured via gst_base_src_set_format() before calling this method.

      This is a variant of gst_base_src_new_segment() sending the segment right away, which can be useful to ensure events ordering.

      Parameters:
      segment - a pointer to a GstSegment
      Returns:
      true if sending of new segment succeeded.
      Since:
      1.24
    • queryLatency

      public boolean queryLatency(@Nullable Out<Boolean> live, @Nullable ClockTime minLatency, @Nullable ClockTime maxLatency)

      Query the source for the latency parameters. live will be true when this BaseSrc is configured as a live source. minLatency and maxLatency will be set to the difference between the running time and the timestamp of the first buffer.

      This function is mostly used by subclasses.

      Parameters:
      live - if the source is live
      minLatency - the min latency of the source
      maxLatency - the max latency of the source
      Returns:
      true if the query succeeded.
    • setAsync

      public void setAsync(boolean 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:
      async - new async mode
    • setAutomaticEos

      public void setAutomaticEos(boolean automaticEos)

      If automaticEos is true, this BaseSrc 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.

      When this BaseSrc operates in Format.TIME, GstBaseSrc will send an EOS when a buffer outside of the currently configured segment is pushed if automaticEos is true. Since 1.16, if automaticEos is false an EOS will be pushed only when the GstBaseSrcClass::create implementation returns FlowReturn.EOS.

      Parameters:
      automaticEos - automatic eos
      Since:
      1.4
    • setBlocksize

      public void setBlocksize(int blocksize)
      Set the number of bytes that this BaseSrc will push out with each buffer. When blocksize is set to -1, a default length will be used.
      Parameters:
      blocksize - the new blocksize in bytes
    • setCaps

      public boolean setCaps(Caps caps)
      Set new caps on the basesrc source pad.
      Parameters:
      caps - a GstCaps
      Returns:
      true if the caps could be set
    • setDoTimestamp

      public void setDoTimestamp(boolean timestamp)
      Configure this BaseSrc to automatically timestamp outgoing buffers based on the current running_time of the pipeline. This property is mostly useful for live sources.
      Parameters:
      timestamp - enable or disable timestamping
    • setDynamicSize

      public void setDynamicSize(boolean 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:
      dynamic - new dynamic size mode
    • setFormat

      public void setFormat(Format 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 < State.PAUSED.

      Parameters:
      format - the format to use
    • setLive

      public void setLive(boolean 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:
      live - new live-mode
    • startComplete

      public void startComplete(FlowReturn 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:
      ret - a GstFlowReturn
    • startWait

      public FlowReturn startWait()
      Wait until the start operation completes.
      Returns:
      a GstFlowReturn.
    • submitBufferList

      public void submitBufferList(BufferList bufferList)

      Subclasses can call this from their create virtual method implementation to submit a buffer list to be pushed out later. This is useful in cases where the create function wants to produce multiple buffers to be pushed out in one go in form of a GstBufferList, which can reduce overhead drastically, especially for packetised inputs (for data streams where the packetisation/chunking is not important it is usually more efficient to return larger buffers instead).

      Subclasses that use this function from their create function must return FlowReturn.OK and no buffer from their create virtual method implementation. If a buffer is returned after a buffer list has also been submitted via this function the behaviour is undefined.

      Subclasses must only call this function once per create function call and subclasses must only call this function when the source operates in push mode.

      Parameters:
      bufferList - a GstBufferList
      Since:
      1.14
    • waitPlaying

      public FlowReturn waitPlaying()

      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 FlowReturn.OK) or the processing must be stopped due to a state change to READY or a FLUSH event (in which case this function returns FlowReturn.FLUSHING).

      Returns:
      FlowReturn.OK if this BaseSrc is PLAYING and processing can continue. Any other return value should be returned from the create vmethod.
    • alloc

      protected FlowReturn alloc(long offset, int size, @Nullable Out<Buffer> buf)
      Ask the subclass to allocate an output buffer with offset and size, the default implementation will use the negotiated allocator.
    • create

      protected FlowReturn create(long offset, int size, @Nullable Out<Buffer> buf)
      Ask the subclass to create a buffer with offset and size, the default implementation will call alloc if no allocated buf is provided and then call fill.
    • decideAllocation

      protected boolean decideAllocation(Query query)
      configure the allocation query
    • doSeek

      protected boolean doSeek(Segment segment)
      Perform seeking on the resource to the indicated segment.
    • event

      protected boolean event(Event event)
      Override this to implement custom event handling.
    • fill

      protected FlowReturn fill(long offset, int size, Buffer buf)
      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.
    • fixate

      protected Caps fixate(Caps caps)
      Called if, in negotiation, caps need fixating.
      Returns:
      the fixated caps
    • getCaps

      protected Caps getCaps(@Nullable Caps filter)
      Called to get the caps to report.
    • getSize

      protected boolean getSize(Out<Long> size)
      Get the total size of the resource in the format set by gst_base_src_set_format().
      Returns:
      true if the size is available and has been set.
    • getTimes

      protected void getTimes(Buffer buffer, ClockTime start, ClockTime end)
      Given buffer, return start and end time when it should be pushed out. The base class will sync on the clock using these times.
    • isSeekable

      protected boolean isSeekable()
      Check if the source can seek
    • prepareSeekSegment

      protected boolean prepareSeekSegment(Event seek, Segment 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.
    • query

      public boolean query(Query query)
      Handle a requested query.
      Overrides:
      query in class Element
      Parameters:
      query - the GstQuery.
      Returns:

      true if the query could be performed.

      MT safe.

    • start

      protected boolean 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

      protected boolean stop()
      Stop processing. Subclasses should use this to close resources.
    • unlock

      protected boolean 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.
    • unlockStop

      protected boolean unlockStop()
      Clear the previous unlock request. Subclasses should clear any state they set during GstBaseSrcClass::unlock, such as clearing command queues.