Search code examples
python-3.xgstreamerdrawingarea

How to display a GStreamer pipeline inside a Gtk window with a specific size?


I'm working on a Python script that creates a GStreamer pipeline and displays the video output inside a Gtk window. The current code opens two windows: one with the title "Video Window" but with no content, and another one with the GStreamer pipeline inside.

However, I'd like to have the GStreamer pipeline displayed inside the first window as a 200x200 box, without opening a second window. I've tried using a Gtk.DrawingArea widget and the GstVideo.VideoOverlay interface, but I'm not sure how to integrate them into my code.

Here's the current code:

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

# Gtk
win = Gtk.Window(title="Video Window")
win.connect("destroy", Gtk.main_quit)
win.set_default_size(600, 400)

# DrawingArea to hold the video
drawingarea = Gtk.DrawingArea()
drawingarea.set_size_request(200, 200)
win.add(drawingarea)

# Gst
Gst.init(None)
pipeline = Gst.parse_launch("videotestsrc ! autovideoconvert ! gtksink")
pipeline.set_state(Gst.State.PLAYING)

# End
win.show_all()
Gtk.main()

Can anyone help me modify this code to achieve my goal? I'm relatively new to Gtk and GStreamer, so a detailed explanation would be appreciated. Thank you in advance!

I may add if the sink is not recommended gtksink please inform me. I'm using Raspberry Pi 4 and need as low latency as possible on this. So the best sink to use would help.


Solution

  • The gtksink has a property widget where it draws the video to. Get that widget from that property and add this widget to your GTK application window hierachy where you want it to be displayed.