Demo entry 6362957

aaa

   

Submitted by anonymous on May 11, 2017 at 11:24
Language: Python. Code size: 3.6 kB.

import gi
import sys
gi.require_version('Gst', '1.0')
from gi.repository import Gst

Gst.init(None)

# CREATE THE ELEMENTS
# The firsy parameter of make() is the type of element to be
# created. The second is a name the instance. If not provided,
# the element will receive a guaranteed unique name, consisting
# of the element factory name and a number.
source = Gst.ElementFactory.make("udpsrc", None)
source.set_property("port", 5000)

depay = Gst.ElementFactory.make("rtph264depay", None)

decoder = Gst.ElementFactory.make("avdec_h264", None)

sink = Gst.ElementFactory.make("xvimagesink", None)
sink.set_property("sync", False)

# INFORM TH DEPAY ELEMENT ABOUT THE DATA IT WILL RECEIVE
# This can be done through a caps filter, which is placed between
# the pads of connecting elements.
caps = Gst.caps_from_string("application/x-rtp, payload=127")
source.set_property("caps", caps)

# CREATE AN EMPTY PIPELINE
# The 'video-pipeline' is the name of this particular
# pipeline instance. This name is used in error messages
# and debug log.
pipeline = Gst.Pipeline.new("video-pipeline")

if not pipeline:
    print("Not all elements could be created.")
    exit(-1)

# ADD ELEMENTS TO THE PIPELINE
pipeline.add(source)
pipeline.add(depay)
pipeline.add(decoder)
pipeline.add(sink)

# LINK ELEMENTS
# (when using the gstreamer command line, this linking is
# achieved by the ! character)
if not Gst.Element.link(source, depay):
    print("Elements could not be linked.")
    exit(-1)
if not Gst.Element.link(depay, decoder):
    print("Elements could not be linked.")
    exit(-1)
if not Gst.Element.link(decoder, sink):
    print("Elements could not be linked.")
    exit(-1)

# SET THE PIPELINE TO PLAY/RUN
ret = pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
    print("Unable to set the pipeline to the playing state.")
    exit(-1)

# Wait until error or EOS
bus = pipeline.get_bus()


# Parse message
while True:
    message = bus.timed_pop_filtered(10000, Gst.MessageType.ANY)
    if message:
        if message.type == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print("Error received from element %s: %s" % (
                message.src.get_name(), err))
            print("Debugging information: %s" % debug)
            break
        elif message.type == Gst.MessageType.EOS:
            print("End-Of-Stream reached.")
            break
        elif message.type == Gst.MessageType.NEW_CLOCK:
            print("New CLOCK extracted")
        elif message.type == Gst.MessageType.STATE_CHANGED:
            if isinstance(message.src, Gst.Pipeline):
                old_state, new_state, pending_state = message.parse_state_changed()
                print("Pipeline state changed from %s to %s." %
                       (old_state.value_nick, new_state.value_nick))
        elif message.type == Gst.MessageType.STREAM_STATUS:
            srcClass = message.src.__class__.__name__
            statusType, statusOwner = message.parse_stream_status()
            streamStatus = statusType.value_nick.upper()
            print("%s stream status changed to  %s" % (srcClass, streamStatus))
        elif message.type == Gst.MessageType.STREAM_START:
            print("STREAM_START message created.")
        else:
            print("Unexpected message received.")
	    print("Message from %s", message.src.__class__.__name__)
	    #print(message.type)

# Free resources
pipeline.set_state(Gst.State.NULL)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).