Skip to content

Commit

Permalink
Created separate helpers class. Removed logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hyades committed Jul 10, 2013
1 parent d0e2cf4 commit 9a9f241
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 94 deletions.
1 change: 0 additions & 1 deletion python-api/gstswitch/gstswitch.py
@@ -1,7 +1,6 @@
# from controller import Controller
# from testsource import VideoSrc
from server import Server

import os
import sys
import signal
Expand Down
76 changes: 76 additions & 0 deletions python-api/gstswitch/helpers.py
@@ -0,0 +1,76 @@
import os
import sys
import signal
import subprocess
from gi.repository import Gio
from testsource import VideoSrc, Preview


class TestSources(object):
"""A Controller of test sources feeding into the
gst-switch-srv"""

def __init__(self, video_port):
super(TestSources, self).__init__()
self.TESTS = []
self.set_video_port(video_port)

def set_video_port(self, video_port):
self.VIDEO_PORT = video_port

def get_video_port(self):
return self.VIDEO_PORT

def new_test_video(self, width=300, height=200, pattern=None, timeoverlay=False, clockoverlay=False):
"""Start a new test source
"""
print 'Adding new test video source'
testsrc = VideoSrc(self.get_video_port(), width, height, pattern, timeoverlay, clockoverlay)
if testsrc is None:
pass
self.TESTS.append(testsrc)

def get_test_video(self):
"""Returns a list of processes acting as test inputs
"""
i = 0
for x in self.TESTS:
print i, "pattern:", x.pattern
i += 1
return self.TESTS

def terminate_index(self, index):
"""
"""
testsrc = self.TESTS[index]
print 'End source with pattern %s' % (str(testsrc.get_pattern()))
testsrc.end()
self.TESTS.remove(self.TESTS[index])

def terminate(self):
"""
"""
print 'TESTS:', self.TESTS
for x in range(len(self.TESTS)):
self.terminate_index(0)


class PreviewSinks(object):
"""docstring for PreviewSinks
"""
def __init__(self):
super(PreviewSinks, self).__init__()
self.PREVIEW_PORT = 3001
# TODO set preview port from dbus call

def start(self):
self.preview = Preview(self.PREVIEW_PORT)
self.preview.run()
print 'start preview'

def terminate(self):
try:
self.preview.end()
print 'end preview'
except:
pass
93 changes: 11 additions & 82 deletions python-api/gstswitch/server.py
Expand Up @@ -2,14 +2,10 @@
import sys
import signal
import subprocess
import logging

from exception import *
from controller import Controller
from testsource import VideoSrc, Preview

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
from time import sleep


class BaseServer(object):
Expand Down Expand Up @@ -60,71 +56,7 @@ def connect_controller(self):
self.establish_connection()


class ServerTestSourceController(object):
"""A Controller of test sources feeding into the
gst-switch-srv"""

def __init__(self):
super(ServerTestSourceController, self).__init__()
self.TESTS = []

def new_test_video(self, width=300, height=200, pattern=None, timeoverlay=False, clockoverlay=False):
"""Start a new test source
"""
logging.info('Adding new test video source')
testsrc = VideoSrc(self.VIDEO_PORT, width, height, pattern, timeoverlay, clockoverlay)
if testsrc is None:
pass
self.TESTS.append(testsrc)

def get_test_video(self):
"""Returns a list of processes acting as test inputs
"""
i = 0
for x in self.TESTS:
print i, "pattern:", x.pattern
i += 1
return self.TESTS

def end_test_video(self, index):
"""
"""
testsrc = self.TESTS[index]
print 'End source with pattern %s' % (str(testsrc.get_pattern()))
logging.info('End source with pattern %s' % (str(testsrc.get_pattern())))
testsrc.end()
self.TESTS.remove(self.TESTS[index])

def endAllTestVideo(self):
"""
"""
print 'TESTS:', self.TESTS
for x in range(len(self.TESTS)):
self.end_test_video(0)


class ServerPreview(object):
"""docstring for ServerPreview"""
def __init__(self):
super(ServerPreview, self).__init__()
self.PREVIEW_PORT = 3001

def start_preview(self):
self.preview = Preview(self.PREVIEW_PORT)
self.preview.run()
logging.info('Starting Preview')
print 'start preview'

def end_preview(self):
try:
self.preview.end()
logging.info('End Preview')
print 'end preview'
except:
pass


class ServerProcess(ServerTestSourceController, ServerPreview):
class ServerProcess(object):
"""Handles all processes created. This includes the server process
and test sources added to the gst-switch-srv
"""
Expand All @@ -138,13 +70,13 @@ def run(self):
"""
self.proc = None
self.pid = -1
logging.info('Starting server')
print "running"
print "Starting server"
self.proc = self.run_process()
if self.proc is None:
pass
else:
self.pid = self.proc.pid
sleep(0.5)

def run_process(self):
cmd = self.PATH
Expand All @@ -157,19 +89,19 @@ def run_process(self):
proc = self.start_process(cmd)
print "process:", proc
if proc is None:
logging.error('Error running server')
print 'ERROR: Server unable to create process'
pass
else:
logging.info('Created process with PID:%s', str(proc.pid))
print 'Created process with PID:%s', str(proc.pid)
return proc

def start_process(self, cmd):
logging.info('Creating process %s' % (cmd))
print 'Creating process %s' % (cmd)
with open(os.devnull, 'w') as tempf:
process = subprocess.Popen(cmd.split(), stdout=tempf, stderr=tempf, bufsize=-1, shell=False)
return process

def end(self):
def terminate(self):
"""Stops the server
Returns:
True on success
Expand All @@ -178,20 +110,17 @@ def end(self):
None
"""
print 'Killing server'
logging.info('Killing server')
self.endAllTestVideo()
self.end_preview()
proc = self.proc
ret = True
try:
proc.terminate()
logging.info('Server killed')
print 'Server Killed'
except:
logging.info('Error killing server')
print 'Error killing server'
ret = False
return ret

def brute_end(self):
def kill(self):
os.kill(self.pid, signal.SIGKILL)

def set_executable_path(self, path):
Expand Down
22 changes: 11 additions & 11 deletions python-api/gstswitch/test.py
@@ -1,24 +1,24 @@
#!/usr/bin/env python
from gstswitch import *
from time import sleep
# import subprocess
from helpers import TestSources, PreviewSinks

# all executables (gst-launch-1.0, gst-switch-srv, gst-switch-ui, gst-switch-cap) at this path
path = '/home/hyades/gst/master/gstreamer/tools/'
# os.chdir(path)
print "starting server"
s = Server(path)
try:
s.run() # launches the server default parameters
sleep(0.5)
cmd = path
port = s.get_video_port()
# connects a gstreamer module to view the output of the gst-switch-srv
s.start_preview()
output = PreviewSinks()
output.start()
# adding two test video sources
s.new_test_video()
s.new_test_video(clockoverlay=True)
sources = TestSources(port)
sources.new_test_video()
sources.new_test_video(timeoverlay=True)
# waiting till user ends the server
raw_input()
s.end()
sources.terminate()
output.terminate()
s.terminate()
finally:
s.brute_end()
s.kill()

0 comments on commit 9a9f241

Please sign in to comment.