Skip to content

Instantly share code, notes, and snippets.

@hyades
Last active December 25, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyades/6971692 to your computer and use it in GitHub Desktop.
Save hyades/6971692 to your computer and use it in GitHub Desktop.
Infinite switching of modes for gst-switch. Replace the videotestsrc to v4l2src according to device attributes
#!/usr/bin/env python
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(__file__, ".")))
# should be in PYTHONPATH
from gstswitch.server import Server
from gstswitch.testsource import BasePipeline
from gstswitch.helpers import *
from gstswitch.controller import Controller
import gi
import subprocess
import time
import psutil
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst
GObject.threads_init()
Gst.init(None)
PATH = '/usr/bin/'
INTERVAL = 1.0
s = Server(path=PATH)
v_port = s.video_port
a_port = s.audio_port
try:
s.run()
cmd_vid1 = PATH
cmd_vid2 = PATH
cmd_vid1 += """gst-launch-1.0 videotestsrc pattern=1 ! \
video/x-raw, width=300, height=200 !\
gdppay !
tcpclientsink port={0}
""".format(v_port)
print cmd_vid1
cmd_vid2 += """gst-launch-1.0 videotestsrc ! \
video/x-raw, width=300, height=200 !\
gdppay !
tcpclientsink port={0}
""".format(v_port)
print cmd_vid2
vid1 = subprocess.Popen(
cmd_vid1.split(),
bufsize=-1,
shell=False)
vid2 = subprocess.Popen(
cmd_vid2.split(),
bufsize=-1,
shell=False)
time.sleep(1)
sources = TestSources(audio_port=a_port)
sources.new_test_audio()
sources.new_test_audio()
controller = Controller()
controller.establish_connection()
cmd = PATH
cmd += """gst-switch-ui"""
cmd = " ".join(cmd.split())
with open('ui.log', 'w') as tempf:
ui_process = subprocess.Popen(
cmd.split(),
stdout=tempf,
stderr=tempf,
bufsize=-1,
shell=False)
params = [(65,3004), (65, 3003)]
i = 0
p = psutil.Process(s.pid)
cpu_usage_file = open("cpu.log", "w")
while 1:
# print i
cpu = p.get_cpu_percent(interval=INTERVAL)
mem = p.get_memory_percent()
print cpu
print mem
cpu_usage_file.write(str(cpu) + '\n' + str(mem) + '\n')
controller.switch(params[i][0], params[i][1])
# time.sleep(1)
i+=1
i%=2
#end all
sources.terminate_audio()
vid1.terminate()
vid2.terminate()
s.terminate()
finally:
if s.proc:
s.kill()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment