GStreamer Interface

GStreamer Pipeline

Installation of GStreamer Plugins

pacman -S gst-plugins-good gst-python gst-plugins-bad
# -good for mp3
# -bad  for m4a/aac

Example for Using Plugins

gst-launch-1.0 filesrc location=in.m4a ! decodebin ! audioconvert ! lamemp3enc target=1 bitrate=320 cbr=true ! filesink location=out.mp3

GStreamerInterface Class

class musicdb.lib.stream.gstreamer.GStreamerInterface(pipelinename='pipeline')[source]

This class provides a simple abstraction to the GStreamer Python module. The class is made to manage one pipeline that can be executed in a thread. Therefore a state machine is implemented providing the following states. The state represent the state of the pipeline execution process. The current state can be checked by calling GetState()

IDLE:

After instantiating the state machine is in IDLE state, as well as after the pipeline was completely executed.

RUNNING:

When the pipeline gets executed, the state machine is in RUNNING state. This happens when the blocking method Execute() got called. This method is thread save an can be executed concurrently.

CANCEL:

When the state machine is in this state, the RUNNING state will be left. This is state that can be forced by calling Cancel().

Error

When an error occures, the state machine goes into the ERROR state. This state will never be left!

digraph finite_state_machine { size="8,12" node [shape = circle, color=black, fontsize=10, label="IDLE" ] idle; node [shape = circle, color=black, fontsize=10, label="RUNNING" ] running; node [shape = circle, color=black, fontsize=10, label="CANCEL" ] cancel; node [shape = circle, color=black, fontsize=10, label="ERROR" ] error; idle -> running [ label = "GStreamerInterface.Execute()\n/ GStreamer Pipeline State := PLAYING" ]; idle -> error [ label = "Starting GStreamer Pipeline failed" ]; running -> cancel [ label = "GStreamer Error Message" ]; running -> cancel [ label = "GStreamer EOS Message" ]; cancel -> idle [ label = "/ GStreamer Pipeline State := NULL" ]; running -> error [ label = "Tried to start pipeline that was not IDLE" ]; cancel -> error [ label = "Tried to start pipeline that was not IDLE" ]; }

These state machine is independent from the GStreamer Pipeline State Machine!

Parameters

pipelinename (str) – Optional name for the pipeline

Cancel()[source]

This method cancels the current running execution of the GStreamer Pipeline. It sets the state to CANCEL only when the current state is RUNNING. Otherwise nothing will be changed.

CreateElement(elementname, name)[source]

This method adds a GStreamer Element to the GStreamer Pipeline. When the element exists and gets added successfully, its instance gets returned. Otherwise None will be returned.

Parameters
  • elementname (str) – name of the GStreamer Element as it is used by GStreamer

  • name (str) – A unique name for the instance of the element that gets added to the Pipeline

Returns

None on error, otherwise the instance of the added GStreamer Element.

Example

Creates a new pipeline named "example". Then two elements from the GStreamer core elements called filesrc and decodebin gets added to the Pipeline. As source location the m4a file test.m4a will be set. Then, the source gets linked to the decoder.

gstreamer = GStreamerInterface("example")
source    = gstreamer.CreateElement("filesrc",   "source")
decoder   = gstreamer.CreateElement("decodebin", "decoder")

source.set_property("location", "./test.m4a")
source.link(decoder)

For more details about the filesrc and decodebin elements, call the following bash commands:

gst-inspect-1.0 filesrc
gst-inspect-1.0 decodebin
Execute()[source]

Only call this method when the pipeline execution is in IDLE state!

Example

Running the pipeline as thread

gstreamer = GStreamerInterface()

# … Setup pipeline …

def StartPipelineThread():
    # Start GStreamer pipeline
    gstreamerthread = Thread(target=gstreamer.Execute)
    gstreamerthread.start()

    # Wait until the pipeline actually started
    while True:
        state = gstreamer.GetState()
        if state == "RUNNING":
            return True
        elif state != "IDLE":
            print("Unexpected gstreamer state: %s"%(gstate))
            return False
GetState()[source]
Returns

The current state of the GStreamer Pipeline execution as string