#!/usr/bin/python3
import gi

# import GStreamer and GTK-Helper classes
gi.require_version('Gst', '1.0')
from gi.repository import GLib, Gst, GObject

# init GObject before importing local classes
GObject.threads_init()
Gst.init(None)

class Example:
	def __init__(self):
		self.mainloop = GObject.MainLoop()

		# works with videomixer, not with compositor
		pipeline = """
videotestsrc pattern=red !
video/x-raw,width=1920,height=1080,format=I420,framerate=25/1,pixel-aspect-ratio=1/1 !
videoscale !
capsfilter name=caps0 caps=video/x-raw !
mix.

compositor name=mix !
video/x-raw,width=1920,height=1080,format=I420,framerate=25/1,pixel-aspect-ratio=1/1 !
xvimagesink
		"""

		self.pipeline = Gst.parse_launch(pipeline)
		self.pipeline.bus.add_signal_watch()
		self.pipeline.bus.connect("message::eos", self.on_eos)
		self.pipeline.bus.connect("message::error", self.on_error)

		self.caps0 = self.pipeline.get_by_name('caps0')

		GLib.timeout_add(1000, self.reconfigure)

	def reconfigure(self):
		print("reconfigure!")
		# does not work
		self.caps0.set_property('caps', Gst.Caps.from_string('video/x-raw,width=950,height=534'))

		# works
		#self.caps0.set_property('caps', Gst.Caps.from_string('video/x-raw,width=950,height=534,pixel-aspect-ratio=1/1'))
		#self.caps0.set_property('caps', Gst.Caps.from_string('video/x-raw,width=800,height=450'))

	def run(self):
		self.pipeline.set_state(Gst.State.PLAYING)
		self.mainloop.run()

	def on_eos(self, bus, message):
		print('Received End-of-Stream-Signal on Mixing-Pipeline')

	def on_error(self, bus, message):
		print('Received Error-Signal on Mixing-Pipeline')
		(error, debug) = message.parse_error()
		print('Error-Details: #%u: %s' % (error.code, debug))

example = Example()
example.run()
