summaryrefslogtreecommitdiff
path: root/voctocore/videomix.py
blob: 7fa392a8a94ffc88261e9bf2b80d6308207610ab (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. videotestsrc pattern="solid-color" foreground-color=0x808080 ! capsfilter name=filter ! videomixer name=quadmix ! autovideosink
  39. """, False)
  40. quadmixcaps = Gst.Caps.new_empty_simple('video/x-raw')
  41. quadmixcaps.set_value('width', round(self.monitorSize[0]))
  42. quadmixcaps.set_value('height', round(self.monitorSize[1]))
  43. mixerbin.get_by_name('filter').set_property('caps', quadmixcaps)
  44. mixerbin.set_name('mixerbin')
  45. self.pipeline.add(mixerbin)
  46. return mixerbin
  47. def addVideosToQuadmix(self, videosources, quadmix):
  48. count = len(videosources)
  49. place = [0, 0]
  50. grid = [0, 0]
  51. grid[0] = math.ceil(math.sqrt(count))
  52. grid[1] = math.ceil(count / grid[0])
  53. cellSize = (self.monitorSize[0] / grid[0], self.monitorSize[1] / grid[1])
  54. print("showing {} videosources in a {}×{} grid in a {}×{} px window, which gives cells of {}×{} px per videosource".format(
  55. count, grid[0], grid[1], self.monitorSize[0], self.monitorSize[1], cellSize[0], cellSize[1]))
  56. for idx, videosource in enumerate(videosources):
  57. caps = videosource.get_static_pad('src').query_caps(None)
  58. capsstruct = caps.get_structure(0)
  59. srcSize = (
  60. capsstruct.get_int('width')[1],
  61. capsstruct.get_int('height')[1],
  62. )
  63. f = max(srcSize[0] / cellSize[0], srcSize[1] / cellSize[1])
  64. scaleSize = (
  65. srcSize[0] / f,
  66. srcSize[1] / f,
  67. )
  68. coord = (
  69. place[0] * cellSize[0] + (cellSize[0] - scaleSize[0]) / 2,
  70. place[1] * cellSize[1] + (cellSize[1] - scaleSize[1]) / 2,
  71. )
  72. print("placing videosrc {} of size {}×{} scaled by {} to {}×{} in a cell {}×{} px cell ({}/{}) at position ({}/{})".format(
  73. idx, srcSize[0], srcSize[1], f, scaleSize[0], scaleSize[1], cellSize[0], cellSize[1], place[0], place[1], coord[0], coord[1]))
  74. scalecaps = Gst.Caps.new_empty_simple('video/x-raw')
  75. scalecaps.set_value('width', round(scaleSize[0]))
  76. scalecaps.set_value('height', round(scaleSize[1]))
  77. scaler = Gst.ElementFactory.make('videoscale', 'quadmix-scaler({})'.format(idx))
  78. self.pipeline.add(scaler)
  79. videosource.link(scaler)
  80. # define size somewhere, scale and place here
  81. sinkpad = quadmix.get_request_pad('sink_%u')
  82. sinkpad.set_property('xpos', round(coord[0]))
  83. sinkpad.set_property('ypos', round(coord[1]))
  84. scaler.link_filtered(quadmix, scalecaps)
  85. place[0] += 1
  86. if place[0] >= grid[0]:
  87. place[1] += 1
  88. place[0] = 0
  89. def createDistributor(self, videosource, name):
  90. distributor = Gst.parse_bin_from_description("""
  91. tee name=t
  92. t. ! queue name=a
  93. t. ! queue name=b
  94. """, False)
  95. distributor.set_name('distributor({0})'.format(name))
  96. self.pipeline.add(distributor)
  97. videosource.link(distributor.get_by_name('t'))
  98. return distributor
  99. def createDummyCamSources(self):
  100. uris = ('file:///home/peter/122.mp4', 'file:///home/peter/10025.mp4',)
  101. for idx, uri in enumerate(uris):
  102. # create a bin for camera input
  103. camberabin = Gst.parse_bin_from_description("""
  104. uridecodebin name=input
  105. input. ! videoconvert ! videoscale ! videorate ! video/x-raw,width=1024,height=576,framerate=25/1 ! identity name=video_src
  106. input. ! audioconvert name=audio_src
  107. """, False)
  108. camberabin.set_name('dummy-camberabin({0})'.format(uri))
  109. # configure camera input
  110. camberabin.get_by_name('input').set_property('uri', uri)
  111. # pass bin upstream
  112. self.pipeline.add(camberabin)
  113. yield camberabin
  114. def createCamSources(self):
  115. for cam in range(2):
  116. # create a bin for camera input
  117. camberabin = Gst.parse_bin_from_description("""
  118. decklinksrc name=input input=sdi input-mode=1080p25
  119. input. ! videoconvert ! videoscale ! videorate ! video/x-raw,width=1920,height=1080,framerate=25/1 ! identity name=video_src
  120. input. ! audioconvert name=audio_src
  121. """, False)
  122. camberabin.set_name('camberabin({0})'.format(cam))
  123. # configure camera input
  124. camberabin.get_by_name('input').set_property('subdevice', cam)
  125. # pass bin upstream
  126. self.pipeline.add(camberabin)
  127. yield camberabin