summaryrefslogtreecommitdiff
path: root/voctocore/videomix.py
blob: 6d3cca874445534480d2c6331c819713b763a990 (plain)
  1. import sys, inspect, math
  2. from pprint import pprint
  3. from gi.repository import GLib, Gst
  4. class Videomix:
  5. decoder = []
  6. mixerpads = []
  7. monitorSize = (1024, 576)
  8. def __init__(self):
  9. self.pipeline = Gst.Pipeline()
  10. # create audio and video mixer
  11. mixerbin = self.createMixer()
  12. # collection of video-sources to connect to the quadmix
  13. quadmixSources = []
  14. # create camera sources
  15. for camberabin in self.createDummyCamSources():
  16. # link camerasource to audiomixer
  17. camberabin.get_by_name('audio_src').link(self.pipeline.get_by_name('liveaudio'))
  18. # inject a ×2 distributor and link one end to the live-mixer
  19. distributor = self.createDistributor(camberabin.get_by_name('video_src'), camberabin.get_name())
  20. distributor.get_by_name('a').link(self.pipeline.get_by_name('livevideo'))
  21. # collect the other end to add it later to the quadmix
  22. quadmixSources.append(distributor.get_by_name('b'))
  23. # would generate pause & slides with another generator which only
  24. # yields if the respective fil are there and which only have a video-pad
  25. self.addVideosToQuadmix(quadmixSources, self.pipeline.get_by_name('quadmix'))
  26. # demonstrate some control
  27. liveaudio = self.pipeline.get_by_name('liveaudio')
  28. liveaudio.set_property('active-pad', liveaudio.get_static_pad('sink_0'))
  29. livevideo = self.pipeline.get_by_name('livevideo')
  30. pad = livevideo.get_static_pad('sink_1')
  31. pad.set_property('alpha', 0.5)
  32. Gst.debug_bin_to_dot_file(self.pipeline, Gst.DebugGraphDetails.ALL, 'test')
  33. self.pipeline.set_state(Gst.State.PLAYING)
  34. def createMixer(self):
  35. mixerbin = Gst.parse_bin_from_description("""
  36. videomixer name=livevideo ! autovideosink
  37. input-selector name=liveaudio ! autoaudiosink
  38. filesrc location=quadmix-bg.png ! decodebin ! imagefreeze ! videomixer name=quadmix ! autovideosink
  39. """, False)
  40. mixerbin.set_name('mixerbin')
  41. self.pipeline.add(mixerbin)
  42. return mixerbin
  43. def addVideosToQuadmix(self, videosources, quadmix):
  44. count = len(videosources)
  45. place = [0, 0]
  46. grid = [0, 0]
  47. grid[0] = math.ceil(math.sqrt(count))
  48. grid[1] = math.ceil(count / grid[0])
  49. cellSize = (self.monitorSize[0] / grid[0], self.monitorSize[1] / grid[1])
  50. print("showing {} videosources in a {}×{} grid in a {}×{} px window, which gives cells of {}×{} px per videosource".format(
  51. count, grid[0], grid[1], self.monitorSize[0], self.monitorSize[1], cellSize[0], cellSize[1]))
  52. for idx, videosource in enumerate(videosources):
  53. caps = videosource.get_static_pad('src').query_caps(None)
  54. capsstruct = caps.get_structure(0)
  55. srcSize = (
  56. capsstruct.get_int('width')[1],
  57. capsstruct.get_int('height')[1],
  58. )
  59. f = max(srcSize[0] / cellSize[0], srcSize[1] / cellSize[1])
  60. scaleSize = (
  61. srcSize[0] / f,
  62. srcSize[1] / f,
  63. )
  64. coord = (
  65. place[0] * cellSize[0] + (cellSize[0] - scaleSize[0]) / 2,
  66. place[1] * cellSize[1] + (cellSize[1] - scaleSize[1]) / 2,
  67. )
  68. print("placing videosrc {} of size {}×{} scaled by {} to {}×{} in a cell {}×{} px cell ({}/{}) at position ({}/{})".format(
  69. idx, srcSize[0], srcSize[1], f, scaleSize[0], scaleSize[1], cellSize[0], cellSize[1], place[0], place[1], coord[0], coord[1]))
  70. scalecaps = Gst.Caps.new_empty_simple('video/x-raw')
  71. scalecaps.set_value('width', round(scaleSize[0]))
  72. scalecaps.set_value('height', round(scaleSize[1]))
  73. scaler = Gst.ElementFactory.make('videoscale', 'quadmix-scaler({})'.format(idx))
  74. self.pipeline.add(scaler)
  75. videosource.link(scaler)
  76. # define size somewhere, scale and place here
  77. sinkpad = quadmix.get_request_pad('sink_%u')
  78. sinkpad.set_property('xpos', round(coord[0]))
  79. sinkpad.set_property('ypos', round(coord[1]))
  80. scaler.link_filtered(quadmix, scalecaps)
  81. place[0] += 1
  82. if place[0] >= grid[0]:
  83. place[1] += 1
  84. place[0] = 0
  85. def createDistributor(self, videosource, name):
  86. distributor = Gst.parse_bin_from_description("""
  87. tee name=t
  88. t. ! queue name=a
  89. t. ! queue name=b
  90. """, False)
  91. distributor.set_name('distributor({0})'.format(name))
  92. self.pipeline.add(distributor)
  93. videosource.link(distributor.get_by_name('t'))
  94. return distributor
  95. def createDummyCamSources(self):
  96. uris = ('file:///home/peter/122.mp4', 'file:///home/peter/10025.mp4',)
  97. for idx, uri in enumerate(uris):
  98. # create a bin for camera input
  99. camberabin = Gst.parse_bin_from_description("""
  100. uridecodebin name=input
  101. input. ! videoconvert ! videoscale ! videorate ! video/x-raw,width=1024,height=576,framerate=25/1 ! identity name=video_src
  102. input. ! audioconvert name=audio_src
  103. """, False)
  104. camberabin.set_name('dummy-camberabin({0})'.format(uri))
  105. # configure camera input
  106. camberabin.get_by_name('input').set_property('uri', uri)
  107. # pass bin upstream
  108. self.pipeline.add(camberabin)
  109. yield camberabin
  110. def createCamSources(self):
  111. for cam in range(2):
  112. # create a bin for camera input
  113. camberabin = Gst.parse_bin_from_description("""
  114. decklinksrc name=input input=sdi input-mode=1080p25
  115. input. ! videoconvert ! videoscale ! videorate ! video/x-raw,width=1920,height=1080,framerate=25/1 ! identity name=video_src
  116. input. ! audioconvert name=audio_src
  117. """, False)
  118. camberabin.set_name('camberabin({0})'.format(cam))
  119. # configure camera input
  120. camberabin.get_by_name('input').set_property('subdevice', cam)
  121. # pass bin upstream
  122. self.pipeline.add(camberabin)
  123. yield camberabin