Search code examples
pythonpipelinegstreamer

Gstreamer Pipeline never setting state to PLAYING


I've been trying to send images I receive from some other program through a gstreamer pipeline that uses appsrc, however the pipeline seems to never reach the PLAYING state. Here is my code. Sorry if I'm doing something wrong, its my first post ever.

#import buscaminas
import time
import VideoReceiver
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject

destination_ip = "127.0.0.1"
destination_port = "7070"

def on_playing(bus, message, pipeline):
    if message.type == Gst.MessageType.ERROR:
        err, debug = message.parse_error()
        print("Error:", err, debug)
        pipeline.set_state(Gst.State.NULL)
    elif message.type == Gst.MessageType.STATE_CHANGED:
        if message.src == pipeline:
            old_state, new_state, pending_state = message.parse_state_changed()
            if new_state == Gst.State.PLAYING:
                print("Pipeline is now in PLAYING state")

def update_image():

    while True:
        if not Receiver.frame_available():
            continue

        frame = Receiver.frame()
        #buscaminas.run(frame)
        image_data = frame.tobytes()
        ret = appsrc.emit("push-buffer", Gst.Buffer.new_wrapped(image_data))
        print(ret)
        time.sleep(1)

if __name__ == '__main__':
    
    Receiver = VideoReceiver.VideoReceiver()
    
    Gst.init(None)

    pipeline = Gst.parse_launch(
        f"appsrc name=source emit-signals=True is-live=True ! videoconvert ! autovideosink "
        #f"appsrc name=source ! videoconvert ! videoscale ! video/x-raw,width=480,height=360 ! tee name=t "
        #f"t. ! queue ! nvvidconv ! omxh264enc ! video/x-h264,stream-format=byte-stream ! rtph264pay ! udpsink host={destination_ip} port={destination_port} "
    )
    appsrc = pipeline.get_by_name("source")

    bus = pipeline.get_bus()
    bus.add_signal_watch()
    bus.connect("message", on_playing, pipeline)

    pipeline.set_state(Gst.State.PLAYING)

    loop = GObject.MainLoop()

    GObject.idle_add(update_image)

    try:
        loop.run()
    except KeyboardInterrupt:
        loop.quit()
        pipeline.set_state(Gst.State.NULL)

   

I tried using different threads, different pipelines, I made sure I was receiving the images from VideoReceiver. Thanks in advance.


Solution

  • Did you try adding queue element after appsrc?

    pipeline = Gst.parse_launch(
        f"appsrc name=source emit-signals=True is-live=True ! queue ! videoconvert ! autovideosink "
    

    Additionally, you could run the code with GST_DEBUG=3 or GST_DEBUG=4 to see detailed logs and find whats going wrong

    GST_DEBUG=3 python3 example.py
    GST_DEBUG=4 python3 example.py