Through this sample code, you can learn:
This sample shows how to generate a simple pipeline equivalent to:
gst-launch-1.0 videotestsrc pattern=18 ! autovideosink
Gst.parse_launch is fast and useful when you don’t want to handle connections between plugins/elements manually and just want to launch some existing pipeline. Creating pipeline with Gst.parse_launch is very simple and flexible solution.
There are seven main steps to form the application of generate-a-basic-pipeline.c:
• Step 1: Initialize GStreamer
• Step 2: Define gstreamer command pipeline
• Step 3: Create pipeline via parse_launch
• Step 4: Allow bus to emit messages to main thread
• Step 5: Start pipeline
• Step 6: Message handling
• Step 7: Free resources
Here, we will take generate-a-pipeline-with-parse-launch.c for explanation.
In this step, it is required and must to initialize the GStreamer by init for the application.
Gst.init(sys.argv)
Next, define the correct pipeline command. Make sure the command can successfulll execute in terminal.
pipeline_command = "videotestsrc pattern=18 ! autovideosink"
Call Gst.parse_launch with the command just defined:
pipeline = Gst.parse_launch(pipeline_command)
Get the bus from pipeline instance and allow bus to emit message to main thread:
bus = pipeline.get_bus()
bus.add_signal_watch()
Playing the pipeline we created:
pipeline.set_state(Gst.State.PLAYING)
loop = GObject.MainLoop()
# Add handler to specific signal
bus.connect("message", on_message, loop)
try:
print("Start to run the pipeline.\n")
loop.run()
except Exception:
print("in exception")
traceback.print_exc()
loop.quit()
This step makes sure the message be thrown by the pipeline we created through the GstBus. This message handling could customize based on different event thrown.
def on_message(bus: Gst.Bus, message: Gst.Message, loop: GObject.MainLoop):
mtype = message.type
if mtype == Gst.MessageType.EOS:
print("End of stream")
loop.quit()
elif mtype == Gst.MessageType.ERROR:
err, debug = message.parse_error()
print("Gst.MessageType.ERROR catched in on_message:")
print(err, debug)
loop.quit()
elif mtype == Gst.MessageType.ANY:
err, debug = message.parse_warning()
print(err, debug)
return True
Finally, release all used GStreamer instances:
# Stop Pipeline
pipeline.set_state(Gst.State.NULL)
del pipeline
Go to the folder of this sample source in terminal or cmd:
$ python3 generate-a-pipeline-with-parse-launch.py
and you will see the moving ball windows displayed:
