Class Bus
- All Implemented Interfaces:
Proxy
GstBus
is an object responsible for delivering GstMessage
packets in
a first-in first-out way from the streaming threads (see GstTask
) to the
application.
Since the application typically only wants to deal with delivery of these messages from one thread, the GstBus will marshall the messages between different threads. This is important since the actual streaming of media is done in another thread than the application.
The GstBus provides support for GSource
based notifications. This makes it
possible to handle the delivery in the glib GMainLoop
.
The GSource
callback function gst_bus_async_signal_func() can be used to
convert all bus messages into signal emissions.
A message is posted on the bus with the gst_bus_post() method. With the gst_bus_peek() and gst_bus_pop() methods one can look at or retrieve a previously posted message.
The bus can be polled with the gst_bus_poll() method. This methods blocks
up to the specified timeout value until one of the specified messages types
is posted on the bus. The application can then gst_bus_pop() the messages
from the bus to handle them.
Alternatively the application can register an asynchronous bus function
using gst_bus_add_watch_full() or gst_bus_add_watch(). This function will
install a GSource
in the default glib main loop and will deliver messages
a short while after they have been posted. Note that the main loop should
be running for the asynchronous callbacks.
It is also possible to get messages from the bus without any thread marshalling with the gst_bus_set_sync_handler() method. This makes it possible to react to a message in the same thread that posted the message on the bus. This should only be used if the application is able to deal with messages from different threads.
Every GstPipeline
has one bus.
Note that a GstPipeline
will set its bus into flushing state when changing
from READY to NULL state.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Bus.Builder<B extends Bus.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
GStreamer bus class.static interface
Functional interface declaration of theMessageCallback
callback.static interface
Functional interface declaration of theSyncMessageCallback
callback.Nested classes/interfaces inherited from class org.freedesktop.gstreamer.gst.GstObject
GstObject.DeepNotifyCallback, GstObject.ObjectClass, GstObject.ObjectImpl
Nested classes/interfaces inherited from class org.gnome.gobject.InitiallyUnowned
InitiallyUnowned.InitiallyUnownedClass
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback
-
Constructor Summary
ConstructorDescriptionBus()
Creates a newGstBus
instance.Bus
(MemorySegment address) Create a Bus proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a bus signal watch to the default main context with the default priority (G_PRIORITY_DEFAULT
).void
addSignalWatchFull
(int priority) Adds a bus signal watch to the default main context with the givenpriority
(e.g.int
Adds a bus watch to the default main context with the givenpriority
(e.g.protected Bus
asParent()
Returns this instance as if it were its parent type.boolean
asyncSignalFunc
(Message message, @Nullable MemorySegment data) A helperGstBusFunc
that can be used to convert all asynchronous messages into signals.static Bus.Builder
<? extends Bus.Builder> builder()
ABus.Builder
object constructs aBus
with the specified properties.Create watch for this bus.void
Instructs GStreamer to stop emitting the "sync-message" signal for this bus.void
emitMessage
(@Nullable String detail, Message message) Emits the "message" signal.void
emitSyncMessage
(@Nullable String detail, Message message) Emits the "sync-message" signal.void
Instructs GStreamer to emit the "sync-message" signal after running the bus's sync handler.static MemoryLayout
The memory layout of the native struct.void
Gets the file descriptor from the bus which can be used to get notified about messages being available with functions like g_poll(), and allows integration into other event loops based on file descriptors.static Type
getType()
Get the GType of the Bus classboolean
Checks if there are pending messages on the bus that should be handled.protected void
A message has been posted on the bus.onMessage
(@Nullable String detail, Bus.MessageCallback handler) A message has been posted on the bus.onSyncMessage
(@Nullable String detail, Bus.SyncMessageCallback handler) A message has been posted on the bus.peek()
Peeks the message on the top of the bus' queue.poll
(Set<MessageType> events, ClockTime timeout) Polls the bus for messages.poll
(MessageType events, ClockTime timeout) Polls the bus for messages.pop()
Gets a message from the bus.popFiltered
(Set<MessageType> types) Gets a message matchingtype
from the bus.popFiltered
(MessageType... types) Gets a message matchingtype
from the bus.boolean
Posts a message on the given bus.void
Removes a signal watch previously added with gst_bus_add_signal_watch().boolean
Removes an installed bus watch from this Bus.void
setFlushing
(boolean flushing) Ifflushing
, flushes out and unrefs any messages queued in the bus.void
setSyncHandler
(@Nullable BusSyncHandler func) Sets the synchronous handler on the bus.protected void
syncMessage
(Message message) A message has been posted on the bus.syncSignalHandler
(Message message, @Nullable MemorySegment data) A helperGstBusSyncHandler
that can be used to convert all synchronous messages into signals.Gets a message from the bus, waiting up to the specified timeout.timedPopFiltered
(ClockTime timeout, Set<MessageType> types) Gets a message from the bus whose type matches the message type masktypes
, waiting up to the specified timeout (and discarding any messages that do not match the mask provided).timedPopFiltered
(ClockTime timeout, MessageType... types) Gets a message from the bus whose type matches the message type masktypes
, waiting up to the specified timeout (and discarding any messages that do not match the mask provided).Methods inherited from class org.freedesktop.gstreamer.gst.GstObject
addControlBinding, checkUniqueness, deepNotify, defaultDeepNotify, defaultError, emitDeepNotify, getControlBinding, getControlRate, getGValueArray, getName, getParent, getPathString, getValue, getValueArray, hasActiveControlBindings, hasAncestor, hasAsAncestor, hasAsParent, onDeepNotify, ref, refSink, removeControlBinding, replace, setControlBindingDisabled, setControlBindingsDisabled, setControlRate, setName, setParent, suggestNextSync, syncValues, unparent, unref
Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Bus
Create a Bus proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Bus
public Bus()Creates a newGstBus
instance.
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type. -
addSignalWatch
public void addSignalWatch()Adds a bus signal watch to the default main context with the default priority (G_PRIORITY_DEFAULT
). It is also possible to use a non-default main context set up using g_main_context_push_thread_default() (before one had to create a bus watch source and attach it to the desired main context 'manually').After calling this statement, the bus will emit the "message" signal for each message posted on the bus.
This function may be called multiple times. To clean up, the caller is responsible for calling gst_bus_remove_signal_watch() as many times as this function is called.
-
addSignalWatchFull
public void addSignalWatchFull(int priority) Adds a bus signal watch to the default main context with the givenpriority
(e.g.G_PRIORITY_DEFAULT
). It is also possible to use a non-default main context set up using g_main_context_push_thread_default() (before one had to create a bus watch source and attach it to the desired main context 'manually').After calling this statement, the bus will emit the "message" signal for each message posted on the bus when the
GMainLoop
is running.This function may be called multiple times. To clean up, the caller is responsible for calling gst_bus_remove_signal_watch() as many times as this function is called.
There can only be a single bus watch per bus, you must remove any signal watch before you can set another type of watch.
- Parameters:
priority
- The priority of the watch.
-
addWatch
Adds a bus watch to the default main context with the givenpriority
(e.g.G_PRIORITY_DEFAULT
). It is also possible to use a non-default main context set up using g_main_context_push_thread_default() (before one had to create a bus watch source and attach it to the desired main context 'manually').This function is used to receive asynchronous messages in the main loop. There can only be a single bus watch per bus, you must remove it before you can set a new one.
The bus watch will only work if a
GMainLoop
is being run.When
func
is called, the message belongs to the caller; if you want to keep a copy of it, call gst_message_ref() before leavingfunc
.The watch can be removed using gst_bus_remove_watch() or by returning
false
fromfunc
. If the watch was added to the default main context it is also possible to remove the watch using g_source_remove().The bus watch will take its own reference to the this Bus, so it is safe to unref this Bus using gst_object_unref() after setting the bus watch.
- Parameters:
priority
- The priority of the watch.func
- A function to call when a message is received.- Returns:
- The event source id or 0 if this Bus already got an event source.
-
asyncSignalFunc
A helperGstBusFunc
that can be used to convert all asynchronous messages into signals.- Parameters:
message
- theGstMessage
receiveddata
- user data- Returns:
true
-
createWatch
Create watch for this bus. TheGSource
will be dispatched whenever a message is on the bus. After the GSource is dispatched, the message is popped off the bus and unreffed.As with other watches, there can only be one watch on the bus, including any signal watch added with
gst_bus_add_signal_watch
.- Returns:
- a
GSource
that can be added to aGMainLoop
.
-
disableSyncMessageEmission
public void disableSyncMessageEmission()Instructs GStreamer to stop emitting the "sync-message" signal for this bus. See gst_bus_enable_sync_message_emission() for more information.In the event that multiple pieces of code have called gst_bus_enable_sync_message_emission(), the sync-message emissions will only be stopped after all calls to gst_bus_enable_sync_message_emission() were "cancelled" by calling this function. In this way the semantics are exactly the same as gst_object_ref() that which calls enable should also call disable.
-
enableSyncMessageEmission
public void enableSyncMessageEmission()Instructs GStreamer to emit the "sync-message" signal after running the bus's sync handler. This function is here so that code can ensure that they can synchronously receive messages without having to affect what the bin's sync handler is.This function may be called multiple times. To clean up, the caller is responsible for calling gst_bus_disable_sync_message_emission() as many times as this function is called.
While this function looks similar to gst_bus_add_signal_watch(), it is not exactly the same -- this function enables synchronous emission of signals when messages arrive; gst_bus_add_signal_watch() adds an idle callback to pop messages off the bus *asynchronously*. The sync-message signal comes from the thread of whatever object posted the message; the "message" signal is marshalled to the main thread via the
GMainLoop
. -
getPollfd
Gets the file descriptor from the bus which can be used to get notified about messages being available with functions like g_poll(), and allows integration into other event loops based on file descriptors. Whenever a message is available, the POLLIN /IOCondition.IN
event is set.Warning: NEVER read or write anything to the returned fd but only use it for getting notifications via g_poll() or similar and then use the normal GstBus API, e.g. gst_bus_pop().
- Parameters:
fd
- A GPollFD to fill
-
havePending
public boolean havePending()Checks if there are pending messages on the bus that should be handled.- Returns:
true
if there are messages on the bus to be handled,false
otherwise.
-
peek
Peeks the message on the top of the bus' queue. The message will remain on the bus' message queue.- Returns:
- the
GstMessage
that is on the bus, ornull
if the bus is empty.
-
poll
Polls the bus for messages. Will block while waiting for messages to come. You can specify a maximum time to poll with thetimeout
parameter. Iftimeout
is negative, this function will block indefinitely.All messages not in
events
will be popped off the bus and will be ignored. It is not possible to use message enums beyondGST_MESSAGE_EXTENDED
in theevents
maskBecause poll is implemented using the "message" signal enabled by gst_bus_add_signal_watch(), calling gst_bus_poll() will cause the "message" signal to be emitted for every message that poll sees. Thus a "message" signal handler will see the same messages that this function sees -- neither will steal messages from the other.
This function will run a
GMainLoop
from the default main context when polling.You should never use this function, since it is pure evil. This is especially true for GUI applications based on Gtk+ or Qt, but also for any other non-trivial application that uses the GLib main loop. As this function runs a GLib main loop, any callback attached to the default GLib main context may be invoked. This could be timeouts, GUI events, I/O events etc.; even if gst_bus_poll() is called with a 0 timeout. Any of these callbacks may do things you do not expect, e.g. destroy the main application window or some other resource; change other application state; display a dialog and run another main loop until the user clicks it away. In short, using this function may add a lot of complexity to your code through unexpected re-entrancy and unexpected changes to your application's state.
For 0 timeouts use gst_bus_pop_filtered() instead of this function; for other short timeouts use gst_bus_timed_pop_filtered(); everything else is better handled by setting up an asynchronous bus watch and doing things from there.
- Parameters:
events
- a mask ofGstMessageType
, representing the set of message types to poll for (note special handling of extended message types below)timeout
- the poll timeout, as aGstClockTime
, orGST_CLOCK_TIME_NONE
to poll indefinitely.- Returns:
- the message that was received,
or
null
if the poll timed out.
-
poll
Polls the bus for messages. Will block while waiting for messages to come. You can specify a maximum time to poll with thetimeout
parameter. Iftimeout
is negative, this function will block indefinitely.All messages not in
events
will be popped off the bus and will be ignored. It is not possible to use message enums beyondGST_MESSAGE_EXTENDED
in theevents
maskBecause poll is implemented using the "message" signal enabled by gst_bus_add_signal_watch(), calling gst_bus_poll() will cause the "message" signal to be emitted for every message that poll sees. Thus a "message" signal handler will see the same messages that this function sees -- neither will steal messages from the other.
This function will run a
GMainLoop
from the default main context when polling.You should never use this function, since it is pure evil. This is especially true for GUI applications based on Gtk+ or Qt, but also for any other non-trivial application that uses the GLib main loop. As this function runs a GLib main loop, any callback attached to the default GLib main context may be invoked. This could be timeouts, GUI events, I/O events etc.; even if gst_bus_poll() is called with a 0 timeout. Any of these callbacks may do things you do not expect, e.g. destroy the main application window or some other resource; change other application state; display a dialog and run another main loop until the user clicks it away. In short, using this function may add a lot of complexity to your code through unexpected re-entrancy and unexpected changes to your application's state.
For 0 timeouts use gst_bus_pop_filtered() instead of this function; for other short timeouts use gst_bus_timed_pop_filtered(); everything else is better handled by setting up an asynchronous bus watch and doing things from there.
- Parameters:
events
- a mask ofGstMessageType
, representing the set of message types to poll for (note special handling of extended message types below)timeout
- the poll timeout, as aGstClockTime
, orGST_CLOCK_TIME_NONE
to poll indefinitely.- Returns:
- the message that was received,
or
null
if the poll timed out.
-
pop
Gets a message from the bus.- Returns:
- the
GstMessage
that is on the bus, ornull
if the bus is empty.
-
popFiltered
Gets a message matchingtype
from the bus. Will discard all messages on the bus that do not matchtype
and that have been posted before the first message that does matchtype
. If there is no message matchingtype
on the bus, all messages will be discarded. It is not possible to use message enums beyondGST_MESSAGE_EXTENDED
in theevents
mask.- Parameters:
types
- message types to take into account- Returns:
- the next
GstMessage
matchingtype
that is on the bus, ornull
if the bus is empty or there is no message matchingtype
.
-
popFiltered
Gets a message matchingtype
from the bus. Will discard all messages on the bus that do not matchtype
and that have been posted before the first message that does matchtype
. If there is no message matchingtype
on the bus, all messages will be discarded. It is not possible to use message enums beyondGST_MESSAGE_EXTENDED
in theevents
mask.- Parameters:
types
- message types to take into account- Returns:
- the next
GstMessage
matchingtype
that is on the bus, ornull
if the bus is empty or there is no message matchingtype
.
-
post
Posts a message on the given bus. Ownership of the message is taken by the bus.- Parameters:
message
- theGstMessage
to post- Returns:
true
if the message could be posted,false
if the bus is flushing.
-
removeSignalWatch
public void removeSignalWatch()Removes a signal watch previously added with gst_bus_add_signal_watch(). -
removeWatch
public boolean removeWatch()Removes an installed bus watch from this Bus.- Returns:
true
on success orfalse
if this Bus has no event source.
-
setFlushing
public void setFlushing(boolean flushing) Ifflushing
, flushes out and unrefs any messages queued in the bus. Releases references to the message origin objects. Will flush future messages until gst_bus_set_flushing() setsflushing
tofalse
.- Parameters:
flushing
- whether or not to flush the bus
-
setSyncHandler
Sets the synchronous handler on the bus. The function will be called every time a new message is posted on the bus. Note that the function will be called in the same thread context as the posting object. This function is usually only called by the creator of the bus. Applications should handle messages asynchronously using the gst_bus watch and poll functions.Before 1.16.3 it was not possible to replace an existing handler and clearing an existing handler with
null
was not thread-safe.- Parameters:
func
- The handler function to install
-
syncSignalHandler
A helperGstBusSyncHandler
that can be used to convert all synchronous messages into signals.- Parameters:
message
- theGstMessage
receiveddata
- user data- Returns:
BusSyncReply.PASS
-
timedPop
Gets a message from the bus, waiting up to the specified timeout.If
timeout
is 0, this function behaves like gst_bus_pop(). Iftimeout
isGST_CLOCK_TIME_NONE
, this function will block forever until a message was posted on the bus.- Parameters:
timeout
- a timeout- Returns:
- the
GstMessage
that is on the bus after the specified timeout ornull
if the bus is empty after the timeout expired.
-
timedPopFiltered
Gets a message from the bus whose type matches the message type masktypes
, waiting up to the specified timeout (and discarding any messages that do not match the mask provided).If
timeout
is 0, this function behaves like gst_bus_pop_filtered(). Iftimeout
isGST_CLOCK_TIME_NONE
, this function will block forever until a matching message was posted on the bus.- Parameters:
timeout
- a timeout in nanoseconds, orGST_CLOCK_TIME_NONE
to wait forevertypes
- message types to take into account,MessageType.ANY
for any type- Returns:
- a
GstMessage
matching the filter intypes
, ornull
if no matching message was found on the bus until the timeout expired.
-
timedPopFiltered
Gets a message from the bus whose type matches the message type masktypes
, waiting up to the specified timeout (and discarding any messages that do not match the mask provided).If
timeout
is 0, this function behaves like gst_bus_pop_filtered(). Iftimeout
isGST_CLOCK_TIME_NONE
, this function will block forever until a matching message was posted on the bus.- Parameters:
timeout
- a timeout in nanoseconds, orGST_CLOCK_TIME_NONE
to wait forevertypes
- message types to take into account,MessageType.ANY
for any type- Returns:
- a
GstMessage
matching the filter intypes
, ornull
if no matching message was found on the bus until the timeout expired.
-
message
A message has been posted on the bus.- Parameters:
message
- the message that has been posted asynchronously
-
syncMessage
A message has been posted on the bus.- Parameters:
message
- the message that has been posted synchronously
-
onMessage
public SignalConnection<Bus.MessageCallback> onMessage(@Nullable @Nullable String detail, Bus.MessageCallback handler) A message has been posted on the bus. This signal is emitted from aGSource
added to the mainloop. this signal will only be emitted when there is aGMainLoop
running.- Parameters:
detail
- the signal detailhandler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitMessage
Emits the "message" signal. SeeonMessage(java.lang.String, org.freedesktop.gstreamer.gst.Bus.MessageCallback)
. -
onSyncMessage
public SignalConnection<Bus.SyncMessageCallback> onSyncMessage(@Nullable @Nullable String detail, Bus.SyncMessageCallback handler) A message has been posted on the bus. This signal is emitted from the thread that posted the message so one has to be careful with locking.This signal will not be emitted by default, you have to call gst_bus_enable_sync_message_emission() before.
- Parameters:
detail
- the signal detailhandler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitSyncMessage
Emits the "sync-message" signal. SeeonSyncMessage(java.lang.String, org.freedesktop.gstreamer.gst.Bus.SyncMessageCallback)
. -
builder
ABus.Builder
object constructs aBus
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withBus.Builder.build()
.
-