Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import os
- import sys
- import gi
- gi.require_version('Gst', '1.0')
- from gi.repository import Gst
- class WebRTCClient:
- def __init__(self):
- self.pipe = None
- def start_pipeline(self):
- Gst.init(None)
- self.pipeline = Gst.Pipeline()
- # setup 2 video test sources
- self.vts_left = Gst.ElementFactory.make("videotestsrc", "video1")
- self.vts_left.set_property('pattern', 'green')
- self.vts_left.set_property('is-live', True)
- self.vts_right = Gst.ElementFactory.make("v4l2src", "video2")
- self.vconv_left = Gst.ElementFactory.make('videoconvert')
- self.vconv_right = Gst.ElementFactory.make('videoconvert')
- self.enc = Gst.ElementFactory.make('vp8enc')
- self.mux = Gst.ElementFactory.make('webmmux')
- # queue probably not needed
- # self.q_left = Gst.ElementFactory.make("queue")
- # self.q_right = Gst.ElementFactory.make("queue")
- # initialize input-selecor and sink
- self.video_switch = Gst.ElementFactory.make("input-selector", "video_switch")
- self.sink = Gst.ElementFactory.make("filesink", "sink")
- self.sink.set_property('location', 'left.webm')
- self.sink.set_property('sync', True)
- self.capsfilter_left = Gst.ElementFactory.make("capsfilter")
- caps = Gst.Caps.from_string('video/x-raw,width=320,height=240,framerate=30/1')
- self.capsfilter_left.set_property('caps', caps)
- self.capsfilter_right = Gst.ElementFactory.make("capsfilter")
- caps2 = Gst.Caps.from_string('video/x-raw,width=320,height=240,framerate=30/1')
- self.capsfilter_right.set_property('caps', caps2)
- # add all to pipeline
- self.pipeline.add(self.vts_left)
- self.pipeline.add(self.vts_right)
- #self.pipeline.add(self.q_left)
- #self.pipeline.add(self.q_right)
- self.pipeline.add(self.capsfilter_left)
- self.pipeline.add(self.capsfilter_right)
- self.pipeline.add(self.vconv_left)
- self.pipeline.add(self.vconv_right)
- self.pipeline.add(self.enc)
- self.pipeline.add(self.mux)
- self.pipeline.add(self.video_switch)
- self.pipeline.add(self.sink)
- # source -> capsfilter -> videoconvert -> input-selector
- self.vts_right.link(self.capsfilter_right)
- self.capsfilter_right.link(self.vconv_right)
- self.vts_left.link(self.capsfilter_left)
- self.capsfilter_left.link(self.vconv_left)
- # vconv_right -> vid_switch
- # vconv_left -> vid_switch
- self.vconv_right.link(self.video_switch)
- self.vconv_left.link(self.video_switch)
- # switch -> enc -> mux -> sink
- self.video_switch.link(self.enc)
- self.enc.link(self.mux)
- self.mux.link(self.sink)
- self.pipeline.set_state(Gst.State.PLAYING)
- Gst.debug_bin_to_dot_file(self.pipeline, Gst.DebugGraphDetails(15), 'initial')
- print('playing')
- async def loop(self):
- self.start_pipeline()
- i=0
- while True:
- # please forgive my lack of flow control
- if i == 70000000:
- # use this instead of sink_0 gst_pad_get_name
- new_pad = self.video_switch.get_static_pad('sink_1')
- self.video_switch.set_property('active-pad', new_pad)
- self.pipeline.set_state(Gst.State.PLAYING)
- Gst.debug_bin_to_dot_file(self.pipeline, Gst.DebugGraphDetails(15), 'final')
- if i == 80000000:
- # switch back to first vidsrc
- new_pad = self.video_switch.get_static_pad('sink_0')
- self.video_switch.set_property('active-pad', new_pad)
- self.pipeline.set_state(Gst.State.PLAYING)
- if i == 90000000:
- # switch back to first vidsrc
- new_pad = self.video_switch.get_static_pad('sink_1')
- self.video_switch.set_property('active-pad', new_pad)
- self.pipeline.set_state(Gst.State.PLAYING)
- i=i+1
- return 0
- def check_plugins():
- needed = ["opus", "vpx", "nice", "webrtc", "dtls", "srtp", "rtp",
- "rtpmanager", "videotestsrc", "audiotestsrc"]
- missing = list(filter(lambda p: Gst.Registry.get().find_plugin(p) is None, needed))
- if len(missing):
- print('Missing gstreamer plugins:', missing)
- return False
- return True
- if __name__=='__main__':
- Gst.init(None)
- if not check_plugins():
- sys.exit(1)
- c = WebRTCClient()
- res = asyncio.get_event_loop().run_until_complete(c.loop())
- print('result exitint')
- sys.exit(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement