Skip to content

Instantly share code, notes, and snippets.

@mattronix
Created April 20, 2020 17:54
Show Gist options
  • Save mattronix/5c807e85a3f8ee2620f5960ac33bace5 to your computer and use it in GitHub Desktop.
Save mattronix/5c807e85a3f8ee2620f5960ac33bace5 to your computer and use it in GitHub Desktop.
playbin stream to tcp
import traceback
import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject # noqa:F401,F402
from gi.repository import GLib
# Initializes Gstreamer, it's variables, paths
Gst.init(sys.argv)
def on_message(bus: Gst.Bus, message: Gst.Message, loop: GLib.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 = Gst.ElementFactory.make("souphttpsrc")
src.set_property("location", "http://STREAM-URL:8000/mopidy")
serve = Gst.ElementFactory.make("tcpserversink")
serve.set_property("port", 3000)
pipeline.add(src)
pipeline.add(serve)
src.link(serve)
# 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 = GLib.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