A Element is linked to other elements via "pads", which are extremely light-weight
generic link points.
Pads have a PadDirection, source pads produce data, sink pads consume data.
Pads are typically created from a PadTemplate with
Pad.from_template and are then added to a
Element. This usually happens when the element is created but it can also happen
dynamically based on the data that the element is processing or based on the pads that the application requests.
Pads without pad templates can be created with Pad, which takes a direction and a
name as an argument. If the name is null
, then a guaranteed unique name will be assigned to it.
A Element creating a pad will typically use the various gst_pad_set_*_function\()
calls to register callbacks for events, queries or dataflow on the pads.
gst_pad_get_parent
will retrieve the Element that owns the pad.
After two pads are retrieved from an element by get_static_pad, the pads can
be linked with gst_pad_link
. (For quick links, you can also use link,
which will make the obvious link for you if it's straightforward.). Pads can be unlinked again with
unlink. get_peer can be used to check
what the pad is linked to.
Before dataflow is possible on the pads, they need to be activated with set_active.
query and peer_query can be used to
query various properties of the pad and the stream.
To send a Event on a pad, use send_event
and push_event. Some events will be sticky on the pad, meaning that after they pass
on the pad they can be queried later with get_sticky_event and
sticky_events_foreach.
get_current_caps and
has_current_caps are convenience functions to query the current sticky CAPS event on a pad.
GstElements will use push and pull_range
to push out or pull in a buffer.
The dataflow, events and queries that happen on a pad can be monitored with probes that can be installed with
add_probe. is_blocked can be used
to check if a block probe is installed on the pad. is_blocking checks if the
blocking probe is currently blocking the pad. remove_probe is used to remove a
previously installed probe and unblock blocking probes if any.
Pad have an offset that can be retrieved with get_offset. This offset will be
applied to the running_time of all data passing over the pad. set_offset can be used
to change the offset.
Convenience functions exist to start, pause and stop the task on a pad with start_task
, pause_task and stop_task
respectively.
Example: Dynamic pipelines:
// See http://docs.gstreamer.com/x/aAAF
// for a detailed description
public class Main {
private Gst.Pipeline pipeline;
private Gst.Element source;
private Gst.Element convert;
private Gst.Element sink;
//
// Handler for the pad-added signal.
//
// This function will be called by the pad-added signal.
//
private void pad_added_handler (Gst.Element src, Gst.Pad new_pad) {
Gst.Pad sink_pad = this.convert.get_static_pad ("sink");
print ("Received new pad '%s' from '%s':\n", new_pad.name, src.name);
// If our converter is already linked, we have nothing to do here:
if (sink_pad.is_linked ()) {
print (" We are already linked. Ignoring.\n");
return ;
}
// Check the new pad's type:
Gst.Caps new_pad_caps = new_pad.query_caps (null);
weak Gst.Structure new_pad_struct = new_pad_caps.get_structure (0);
string new_pad_type = new_pad_struct.get_name ();
if (!new_pad_type.has_prefix ("audio/x-raw")) {
print (" It has type '%s' which is not raw audio. Ignoring.\n", new_pad_type);
return ;
}
// Attempt the link:
Gst.PadLinkReturn ret = new_pad.link (sink_pad);
if (ret != Gst.PadLinkReturn.OK) {
print (" Type is '%s' but link failed.\n", new_pad_type);
} else {
print (" Link succeeded (type '%s').\n", new_pad_type);
}
}
//
// Example runner
//
public int run () {
// Create the elements:
this.source = Gst.ElementFactory.make ("uridecodebin", "source");
this.convert = Gst.ElementFactory.make ("audioconvert", "convert");
this.sink = Gst.ElementFactory.make ("autoaudiosink", "sink");
// Create the empty pipeline:
this.pipeline = new Gst.Pipeline ("test-pipeline");
if (this.pipeline == null || this.source == null || this.convert == null || this.sink == null) {
print ("Not all elements could be created.\n");
return -1;
}
// Build the pipeline. Note that we are NOT linking the source at this
// point. We will do it later.
this.pipeline.add_many (this.source, this.convert , this.sink);
if (!this.convert.link (this.sink)) {
print ("Elements could not be linked.\n");
return -1;
}
// Set the URI to play:
this.source.set ("uri", "http://docs.gstreamer.com/media/sintel_trailer-480p.webm");
// Connect to the pad-added signal:
this.source.pad_added.connect (pad_added_handler);
// Start playing:
Gst.StateChangeReturn ret = this.pipeline.set_state (Gst.State.PLAYING);
if (ret == Gst.StateChangeReturn.FAILURE) {
stderr.puts ("Unable to set the pipeline to the playing state.\n");
return -1;
}
// Listen to the bus:
Gst.Bus bus = this.pipeline.get_bus ();
bool terminate = false;
do {
Gst.Message msg = bus.timed_pop_filtered (Gst.CLOCK_TIME_NONE,
Gst.MessageType.STATE_CHANGED | Gst.MessageType.ERROR | Gst.MessageType.EOS);
// Parse message:
if (msg != null) {
switch (msg.type) {
case Gst.MessageType.ERROR:
GLib.Error err;
string debug_info;
msg.parse_error (out err, out debug_info);
stderr.printf ("Error received from element %s: %s\n", msg.src.name, err.message);
stderr.printf ("Debugging information: %s\n", (debug_info != null)? debug_info : "none");
terminate = true;
break;
case Gst.MessageType.EOS:
print ("End-Of-Stream reached.\n");
terminate = true;
break;
case Gst.MessageType.STATE_CHANGED:
// We are only interested in state-changed messages from the pipeline:
if (msg.src == this.pipeline) {
Gst.State old_state;
Gst.State new_state;
Gst.State pending_state;
msg.parse_state_changed (out old_state, out new_state, out pending_state);
print ("Pipeline state changed from %s to %s:\n",
Gst.Element.state_get_name (old_state),
Gst.Element.state_get_name (new_state));
}
break;
default:
//We should not reach here:
assert_not_reached ();
}
}
} while (!terminate);
// Free resources:
bus = null;
this.pipeline.set_state (Gst.State.NULL);
this.pipeline = null;
return 0;
}
public static int main (string[] args) {
// Initialize GStreamer:
Gst.init (ref args);
// Run the example:
return new Main ().run ();
}
}
valac --pkg gstreamer-1.0 dynamic-pipelines.vala
- public bool activate_mode (PadMode mode, bool active)
Activates or deactivates the given pad in mode
via
dispatching to the pad's activatemodefunc.
- public ulong add_probe (PadProbeType mask, owned PadProbeCallback callback)
Be notified of different states of pads.
- public bool can_link (Pad sinkpad)
Checks if the source pad and the sink pad are compatible so they can
be linked.
- public FlowReturn chain (owned Buffer buffer)
Chain a buffer to this.
- public FlowReturn chain_list (owned BufferList list)
Chain a bufferlist to this.
- public bool check_reconfigure ()
Check and clear the
NEED_RECONFIGURE flag on this and
return true
if the flag was set.
- public string create_stream_id (Element parent, string? stream_id)
Creates a stream-id for the source Pad
this by combining the upstream information with the optional stream_id
of the stream of
this.
- public string create_stream_id_printf (Element parent, string? stream_id, ...)
Creates a stream-id for the source Pad
this by combining the upstream information with the optional stream_id
of the stream of
this.
- public string create_stream_id_printf_valist (Element parent, string? stream_id, va_list var_args)
Creates a stream-id for the source Pad
this by combining the upstream information with the optional stream_id
of the stream of
this.
- public bool event_default (Object? parent, owned Event event)
Invokes the default event handler for the given pad.
- public bool forward (PadForwardFunction forward)
Calls forward
for all internally linked pads of
this.
- public Caps? get_allowed_caps ()
Gets the capabilities of the allowed media types that can flow through
this and its peer.
- public Caps? get_current_caps ()
Gets the capabilities currently configured on
this with the last CAPS event.
- public PadDirection get_direction ()
Gets the direction of the pad.
- public void* get_element_private ()
Gets the private data of a pad.
- public FlowReturn get_last_flow_return ()
Gets the FlowReturn
return from the last data passed by this pad.
- public int64 get_offset ()
Get the offset applied to the running time of
this.
- public PadTemplate? get_pad_template ()
Gets the template for this.
- public Caps get_pad_template_caps ()
Gets the capabilities for this's
template.
- public Element? get_parent_element ()
Gets the parent of this, cast to a
Element.
- public Pad? get_peer ()
Gets the peer of this.
- public FlowReturn get_range (uint64 offset, uint size, out Buffer buffer)
When this is flushing this function
returns FLUSHING immediately and buffer
is null
.
- public Pad? get_single_internal_link ()
If there is a single internal link of the given pad, this function
will return it.
- public Event? get_sticky_event (EventType event_type, uint idx)
Returns a new reference of the sticky event of type event_type
from the event.
- public Stream? get_stream ()
Returns the current Stream
for the this, or null
if none has been set yet, i.e. the pad has not received a
stream-start event yet.
- public string? get_stream_id ()
Returns the current stream-id for the this
, or null
if none has been set yet, i.e. the pad has not received a stream-start event yet.
- public TaskState get_task_state ()
Get this task state.
- public bool has_current_caps ()
Check if this has caps set on it
with a CAPS event.
- public bool is_active ()
Query if a pad is active
- public bool is_blocked ()
Checks if the pad is blocked or not.
- public bool is_blocking ()
Checks if the pad is blocking or not.
- public bool is_linked ()
Checks if a this is linked to
another pad or not.
- public Iterator? iterate_internal_links ()
Gets an iterator for the pads to which the given pad is linked to
inside of the parent element.
- public Iterator? iterate_internal_links_default (Object? parent)
Iterate the list of pads to which the given pad is linked to inside of
the parent element.
- public PadLinkReturn link (Pad sinkpad, PadLinkCheck flags = DEFAULT)
Links the source pad and the sink pad.
- public bool link_maybe_ghosting (Pad sink)
Links this to sink
,
creating any GhostPad's in between as necessary.
- public bool link_maybe_ghosting_full (Pad sink, PadLinkCheck flags)
Links this to sink
,
creating any GhostPad's in between as necessary.
- public void mark_reconfigure ()
Mark a pad for needing reconfiguration.
- public bool needs_reconfigure ()
- public bool pause_task ()
Pause the task of this.
- public bool peer_query (Query query)
Performs query on the
peer of this.
- public bool peer_query_accept_caps (Caps caps)
Check if the peer of this accepts
caps
.
- public Caps peer_query_caps (Caps? filter)
Gets the capabilities of the peer connected to this pad.
- public bool peer_query_convert (Format src_format, int64 src_val, Format dest_format, out int64 dest_val)
Queries the peer pad of a given sink pad to convert src_val
in src_format
to dest_format
.
- public bool peer_query_duration (Format format, out int64 duration)
Queries the peer pad of a given sink pad for the total stream
duration.
- public bool peer_query_position (Format format, out int64 cur)
Queries the peer of a given sink pad for the stream position.
- public bool proxy_query_accept_caps (Query query)
Checks if all internally linked pads of
this accepts the caps in query
and returns the intersection of the results.
- public bool proxy_query_caps (Query query)
Calls query_caps
for all internally linked pads of this and returns the intersection of the results.
- public FlowReturn pull_range (uint64 offset, uint size, out Buffer buffer)
Pulls a buffer
from the peer pad or fills up a provided
buffer.
- public FlowReturn push (owned Buffer buffer)
Pushes a buffer to the peer of this.
- public bool push_event (owned Event event)
Sends the event to the peer of the given pad.
- public FlowReturn push_list (owned BufferList list)
Pushes a buffer list to the peer of this
.
- public bool query (Query query)
Dispatches a query to a pad.
- public bool query_accept_caps (Caps caps)
Check if the given pad accepts the caps.
- public Caps query_caps (Caps? filter)
Gets the capabilities this pad can produce or consume.
- public bool query_convert (Format src_format, int64 src_val, Format dest_format, out int64 dest_val)
Queries a pad to convert src_val
in src_format
to dest_format
.
- public bool query_default (Object? parent, Query query)
Invokes the default query handler for the given pad.
- public bool query_duration (Format format, out int64 duration)
Queries a pad for the total stream duration.
- public bool query_position (Format format, out int64 cur)
Queries a pad for the stream position.
- public void remove_probe (ulong id)
Remove the probe with id
from
this.
- public bool send_event (owned Event event)
Sends the event to the pad.
- public void set_activate_function (PadActivateFunction activate, void* user_data = null, DestroyNotify? notify = null)
Sets the given activate function for this
.
- public void set_activatemode_function (PadActivateModeFunction activatemode, void* user_data = null, DestroyNotify? notify = null)
Sets the given activate_mode function for the pad.
- public bool set_active (bool active)
Activates or deactivates the given pad.
- public void set_chain_function (PadChainFunction chain, void* user_data = null, DestroyNotify? notify = null)
Sets the given chain function for the pad.
- public void set_chain_list_function (PadChainListFunction chainlist, void* user_data = null, DestroyNotify? notify = null)
Sets the given chain list function for the pad.
- public void set_element_private (void* priv)
Set the given private data gpointer on the pad.
- public void set_event_full_function_full (PadEventFullFunction event, void* user_data = null, DestroyNotify? notify = null)
Sets the given event handler for the pad.
- public void set_event_function (PadEventFunction event, void* user_data = null, DestroyNotify? notify = null)
Sets the given event handler for the pad.
- public void set_getrange_function (PadGetRangeFunction @get, void* user_data = null, DestroyNotify? notify = null)
Sets the given getrange function for the pad.
- public void set_iterate_internal_links_function (PadIterIntLinkFunction iterintlink, void* user_data = null, DestroyNotify? notify = null)
Sets the given internal link iterator function for the pad.
- public void set_link_function (PadLinkFunction link, void* user_data = null, DestroyNotify? notify = null)
Sets the given link function for the pad.
- public void set_offset (int64 offset)
Set the offset that will be applied to the running time of
this.
- public void set_query_function (PadQueryFunction query, void* user_data = null, DestroyNotify? notify = null)
Set the given query function for the pad.
- public void set_unlink_function (PadUnlinkFunction unlink, void* user_data = null, DestroyNotify? notify = null)
Sets the given unlink function for the pad.
- public bool start_task (owned TaskFunction func)
Starts a task that repeatedly calls func
with
user_data
.
- public void sticky_events_foreach (PadStickyEventsForeachFunction foreach_func)
Iterates all sticky events on this
and calls foreach_func
for every event.
- public bool stop_task ()
Stop the task of this.
- public FlowReturn store_sticky_event (Event event)
Store the sticky event
on this
- public bool unlink (Pad sinkpad)
Unlinks the source pad from the sink pad.
- public void use_fixed_caps ()
A helper function you can use that sets the FIXED_CAPS flag This way
the default CAPS query will always return the negotiated caps or in case the pad is not negotiated, the padtemplate caps.