Page 1 of 1

Use fdsrc of GStreamer within Python

Posted: Sun Mar 15, 2015 6:43 pm
by superrei
Hi everyone,

I'm trying to build a simple GStreamer pipeline to play the video captured by the pi camera with Python. With gst-launch, I can do it successfully as below:

Code: Select all

raspivid -n -w 960 -h 540 -b 4500000 -fps 8 -vf -hf -t 0 -o - | gst-launch-1.0 -v fdsrc ! h264parse ! omxh264dec ! videoconvert ! ximagesink
Then I try to implement the same thing with python, the code is like this:

Code: Select all

#!/usr/bin/python3

from os import path
import os
import gi
import time
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst, Gtk

# Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively:
from gi.repository import GdkX11, GstVideo


GObject.threads_init()
Gst.init(None)


class Player(object):
    def __init__(self):
        self.window = Gtk.Window()
        self.window.connect('destroy', self.quit)
        self.window.set_default_size(800, 450)

        self.drawingarea = Gtk.DrawingArea()
        self.window.add(self.drawingarea)

        # Create GStreamer pipeline
        self.pipeline = Gst.Pipeline()

        # Create bus to get events from GStreamer pipeline
        self.bus = self.pipeline.get_bus()
        self.bus.add_signal_watch()
        self.bus.connect('message::eos', self.on_eos)
        self.bus.connect('message::error', self.on_error)

        # This is needed to make the video output in our DrawingArea:
        self.bus.enable_sync_message_emission()
        self.bus.connect('sync-message::element', self.on_sync_message)

        # Create GStreamer elements
        self.src = Gst.ElementFactory.make('fdsrc', None)
        self.parse = Gst.ElementFactory.make('h264parse', None)
        self.dec = Gst.ElementFactory.make('omxh264dec', None)
        self.convert = Gst.ElementFactory.make('videoconvert', None)
        self.sink = Gst.ElementFactory.make('ximagesink', None)

        # Add playbin to the pipeline
        self.pipeline.add(self.src)
        self.pipeline.add(self.parse)
        self.pipeline.add(self.dec)
        self.pipeline.add(self.convert)
        self.pipeline.add(self.sink)

        self.src.link(self.parse)
        self.parse.link(self.dec)
        self.parse.link(self.convert)
        self.convert.link(self.sink)
        
        # Set properties
        self.src.set_property('fd', 0)

    def run(self):
        self.window.show_all()
        # You need to get the XID after window.show_all().  You shouldn't get it
        # in the on_sync_message() handler because threading issues will cause
        # segfaults there.
        self.xid = self.drawingarea.get_property('window').get_xid()
        self.pipeline.set_state(Gst.State.PLAYING)
        Gtk.main()

    def quit(self, window):
        self.pipeline.set_state(Gst.State.NULL)
        Gtk.main_quit()

    def on_sync_message(self, bus, msg):
        if msg.get_structure().get_name() == 'prepare-window-handle':
            print('prepare-window-handle')
            msg.src.set_window_handle(self.xid)

    def on_eos(self, bus, msg):
        print('on_eos(): seeking to start of video')
        self.pipeline.seek_simple(
            Gst.Format.TIME,        
            Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT,
            0
        )

    def on_error(self, bus, msg):
        print('on_error():', msg.parse_error())


p = Player()
p.run()
Then I simply run it with command line like this:

Code: Select all

raspivid -n -w 960 -h 540 -b 4500000 -fps 8 -vf -hf -t 0 -o - | myprog.py
I found that I can only get a black screen. Seem to me the fdsrc plugin cannot get the video data from raspivid.

My question is: How can I get fdsrc working within python?

P.S. I have to use fdsrc since v4l2src is not working on the latest version of GStreamer. If anyone can tell me how to make v4l2src works. I'll happily use it instead. Thanks.

Re: Use fdsrc of GStreamer within Python

Posted: Tue Mar 17, 2015 10:19 pm
by JPelletier
Hi,

Take a look at this post: http://www.raspberrypi.org/forums/viewt ... 61#p719561

I easily configured GStreamer to work with the gst-rpicamsrcproject

Re: Use fdsrc of GStreamer within Python

Posted: Wed Mar 18, 2015 6:36 am
by superrei
Thanks. Though finally I found that the problem is due to a bug in my code that linked the elements incorrectly. I fixed it and it works as expect.

I may try the Rpi camera source later, but right now I need to focus on another problem I have. I need to make gst-rtsp-server works for me. I'll start a new thread on this.

Re: Use fdsrc of GStreamer within Python

Posted: Wed Jul 29, 2015 1:08 pm
by semofa
Thanks. Though finally I found that the problem is due to a bug in my code that linked the elements incorrectly. I fixed it and it works as expect.
hi, I have the same problem here(only get a black screen), can you explain how to fix the problem please?