Skip to content

Instantly share code, notes, and snippets.

@mattronix
Created April 20, 2020 14:40
Show Gist options
  • Save mattronix/851b4abfa373b0cfcb5605e2b743ba59 to your computer and use it in GitHub Desktop.
Save mattronix/851b4abfa373b0cfcb5605e2b743ba59 to your computer and use it in GitHub Desktop.
import traceback
import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject # noqa:F401,F402
# Initializes Gstreamer, it's variables, paths
Gst.init(sys.argv)
def on_message(bus: Gst.Bus, message: Gst.Message, loop: GObject.MainLoop):
mtype = message.type
"""
Gstreamer Message Types and how to parse
https://lazka.github.io/pgi-docs/Gst-1.0/flags.html#Gst.MessageType
"""
if mtype == Gst.MessageType.EOS:
print("End of stream")
loop.quit()
elif mtype == Gst.MessageType.ERROR:
err, debug = message.parse_error()
print(err, debug)
loop.quit()
elif mtype == Gst.MessageType.WARNING:
err, debug = message.parse_warning()
print(err, debug)
return True
# Gst.Pipeline https://lazka.github.io/pgi-docs/Gst-1.0/classes/Pipeline.html
pipeline = Gst.Pipeline()
# Creates element by name
# https://lazka.github.io/pgi-docs/Gst-1.0/classes/ElementFactory.html#Gst.ElementFactory.make
src_name = "my_video_test_src"
src = Gst.ElementFactory.make("videotestsrc", "my_video_test_src")
src.set_property("pattern", "ball")
sink = Gst.ElementFactory.make("gtksink")
pipeline.add(src)
pipeline.add(sink)
src.link(sink)
# https://lazka.github.io/pgi-docs/Gst-1.0/classes/Bus.html
bus = pipeline.get_bus()
# allow bus to emit messages to main thread
bus.add_signal_watch()
# Start pipeline
pipeline.set_state(Gst.State.PLAYING)
# Init GObject loop to handle Gstreamer Bus Events
loop = GObject.MainLoop()
# Add handler to specific signal
# https://lazka.github.io/pgi-docs/GObject-2.0/classes/Object.html#GObject.Object.connect
bus.connect("message", on_message, loop)
try:
loop.run()
except Exception:
traceback.print_exc()
loop.quit()
# Stop Pipeline
pipeline.set_state(Gst.State.NULL)
del pipeline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment