Advertisement
Guest User

gst

a guest
Jan 29th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. import asyncio
  2. import os
  3. import sys
  4.  
  5. import gi
  6. gi.require_version('Gst', '1.0')
  7. from gi.repository import Gst
  8.  
  9.  
  10. class WebRTCClient:
  11. def __init__(self):
  12. self.pipe = None
  13. def start_pipeline(self):
  14. Gst.init(None)
  15.  
  16. self.pipeline = Gst.Pipeline()
  17.  
  18. # setup 2 video test sources
  19. self.vts_left = Gst.ElementFactory.make("videotestsrc", "video1")
  20. self.vts_left.set_property('pattern', 'green')
  21. self.vts_left.set_property('is-live', True)
  22.  
  23. self.vts_right = Gst.ElementFactory.make("v4l2src", "video2")
  24.  
  25.  
  26. self.vconv_left = Gst.ElementFactory.make('videoconvert')
  27. self.vconv_right = Gst.ElementFactory.make('videoconvert')
  28.  
  29.  
  30. self.enc = Gst.ElementFactory.make('vp8enc')
  31. self.mux = Gst.ElementFactory.make('webmmux')
  32.  
  33. # queue probably not needed
  34. # self.q_left = Gst.ElementFactory.make("queue")
  35. # self.q_right = Gst.ElementFactory.make("queue")
  36.  
  37. # initialize input-selecor and sink
  38. self.video_switch = Gst.ElementFactory.make("input-selector", "video_switch")
  39. self.sink = Gst.ElementFactory.make("filesink", "sink")
  40. self.sink.set_property('location', 'left.webm')
  41. self.sink.set_property('sync', True)
  42.  
  43. self.capsfilter_left = Gst.ElementFactory.make("capsfilter")
  44. caps = Gst.Caps.from_string('video/x-raw,width=320,height=240,framerate=30/1')
  45. self.capsfilter_left.set_property('caps', caps)
  46.  
  47. self.capsfilter_right = Gst.ElementFactory.make("capsfilter")
  48. caps2 = Gst.Caps.from_string('video/x-raw,width=320,height=240,framerate=30/1')
  49. self.capsfilter_right.set_property('caps', caps2)
  50.  
  51. # add all to pipeline
  52. self.pipeline.add(self.vts_left)
  53. self.pipeline.add(self.vts_right)
  54. #self.pipeline.add(self.q_left)
  55. #self.pipeline.add(self.q_right)
  56. self.pipeline.add(self.capsfilter_left)
  57. self.pipeline.add(self.capsfilter_right)
  58. self.pipeline.add(self.vconv_left)
  59. self.pipeline.add(self.vconv_right)
  60. self.pipeline.add(self.enc)
  61. self.pipeline.add(self.mux)
  62. self.pipeline.add(self.video_switch)
  63. self.pipeline.add(self.sink)
  64.  
  65.  
  66.  
  67. # source -> capsfilter -> videoconvert -> input-selector
  68. self.vts_right.link(self.capsfilter_right)
  69. self.capsfilter_right.link(self.vconv_right)
  70.  
  71. self.vts_left.link(self.capsfilter_left)
  72. self.capsfilter_left.link(self.vconv_left)
  73.  
  74. # vconv_right -> vid_switch
  75. # vconv_left -> vid_switch
  76. self.vconv_right.link(self.video_switch)
  77. self.vconv_left.link(self.video_switch)
  78.  
  79. # switch -> enc -> mux -> sink
  80. self.video_switch.link(self.enc)
  81. self.enc.link(self.mux)
  82. self.mux.link(self.sink)
  83.  
  84. self.pipeline.set_state(Gst.State.PLAYING)
  85. Gst.debug_bin_to_dot_file(self.pipeline, Gst.DebugGraphDetails(15), 'initial')
  86.  
  87. print('playing')
  88.  
  89. async def loop(self):
  90. self.start_pipeline()
  91. i=0
  92.  
  93. while True:
  94. # please forgive my lack of flow control
  95. if i == 70000000:
  96. # use this instead of sink_0 gst_pad_get_name
  97. new_pad = self.video_switch.get_static_pad('sink_1')
  98. self.video_switch.set_property('active-pad', new_pad)
  99. self.pipeline.set_state(Gst.State.PLAYING)
  100.  
  101. Gst.debug_bin_to_dot_file(self.pipeline, Gst.DebugGraphDetails(15), 'final')
  102.  
  103.  
  104. if i == 80000000:
  105. # switch back to first vidsrc
  106. new_pad = self.video_switch.get_static_pad('sink_0')
  107. self.video_switch.set_property('active-pad', new_pad)
  108. self.pipeline.set_state(Gst.State.PLAYING)
  109.  
  110. if i == 90000000:
  111. # switch back to first vidsrc
  112. new_pad = self.video_switch.get_static_pad('sink_1')
  113. self.video_switch.set_property('active-pad', new_pad)
  114. self.pipeline.set_state(Gst.State.PLAYING)
  115.  
  116. i=i+1
  117.  
  118.  
  119. return 0
  120.  
  121.  
  122. def check_plugins():
  123. needed = ["opus", "vpx", "nice", "webrtc", "dtls", "srtp", "rtp",
  124. "rtpmanager", "videotestsrc", "audiotestsrc"]
  125. missing = list(filter(lambda p: Gst.Registry.get().find_plugin(p) is None, needed))
  126. if len(missing):
  127. print('Missing gstreamer plugins:', missing)
  128. return False
  129. return True
  130.  
  131.  
  132. if __name__=='__main__':
  133. Gst.init(None)
  134. if not check_plugins():
  135. sys.exit(1)
  136.  
  137. c = WebRTCClient()
  138. res = asyncio.get_event_loop().run_until_complete(c.loop())
  139. print('result exitint')
  140. sys.exit(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement