summaryrefslogtreecommitdiff
path: root/voctocore/experiments/binlinktest.py
blob: 69d24ee671a17461e23f4d485b248f64e41979ff (plain)
  1. #!/usr/bin/python3
  2. import gi, time
  3. gi.require_version('Gst', '1.0')
  4. from gi.repository import GLib, Gst, GObject
  5. GObject.threads_init()
  6. Gst.init(None)
  7. class SrcBin(Gst.Bin):
  8. def __init__(self):
  9. super().__init__()
  10. self.src = Gst.ElementFactory.make('videotestsrc', 'src')
  11. self.add(self.src)
  12. self.add_pad(
  13. Gst.GhostPad.new('src', self.src.get_static_pad('src'))
  14. )
  15. class SinkBin(Gst.Bin):
  16. def __init__(self):
  17. super().__init__()
  18. self.sink = Gst.ElementFactory.make('autovideosink', 'sink')
  19. self.add(self.sink)
  20. self.add_pad(
  21. Gst.GhostPad.new('sink', self.sink.get_static_pad('sink'))
  22. )
  23. class MixBin(Gst.Bin):
  24. def __init__(self):
  25. super().__init__()
  26. self.mix = Gst.ElementFactory.make('videomixer', 'src')
  27. self.add(self.mix)
  28. self.add_pad(
  29. Gst.GhostPad.new('src', self.mix.get_static_pad('src'))
  30. )
  31. def create_ghostpad(self, pad):
  32. ghostpad = Gst.GhostPad.new(pad.get_name(), pad)
  33. self.add_pad(ghostpad)
  34. return ghostpad
  35. def add_src(self, src):
  36. sinkpad = self.mix.get_request_pad('sink_%u')
  37. sinkpad.set_property('alpha', 0.75)
  38. srcpad = src.get_static_pad('src')
  39. # works
  40. #print(src.link(self.mix)) # True
  41. # doesn't
  42. #print(srcpad.link(sinkpad)) # Error => GST_PAD_LINK_WRONG_HIERARCHY
  43. # but this does
  44. sinkghostpad = self.create_ghostpad(sinkpad)
  45. print(srcpad.link(sinkghostpad)) # True
  46. class Example:
  47. def __init__(self):
  48. self.mainloop = GObject.MainLoop()
  49. self.pipeline = Gst.Pipeline()
  50. self.src = SrcBin()
  51. self.sink = SinkBin()
  52. self.mix = MixBin()
  53. # Add elements to pipeline
  54. self.pipeline.add(self.src)
  55. self.pipeline.add(self.sink)
  56. self.pipeline.add(self.mix)
  57. self.mix.add_src(self.src)
  58. self.mix.link(self.sink)
  59. def run(self):
  60. self.pipeline.set_state(Gst.State.PLAYING)
  61. self.mainloop.run()
  62. def kill(self):
  63. self.pipeline.set_state(Gst.State.NULL)
  64. self.mainloop.quit()
  65. example = Example()
  66. example.run()