forked from timvideos/gst-switch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
104 lines (90 loc) · 3.2 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import sys
import signal
import subprocess
from exception import *
from time import sleep
class Server(object):
"""Controls all Server operations
:param path: Path where all exceutables gst-switch-srv, gst-launch-1.0, etc are located
:param video_port: The video port number - default = 3000
:param audio_port: The audio port number - default = 4000
:param control_port: The control port number - default = 5000
:param record_file: The record file format
:returns: nothing
"""
SLEEP_TIME = 0.5
def __init__(self, path, video_port=3000, audio_port=4000, control_port=5000, record_file='record.data'):
super(Server, self).__init__()
self.path = path
self.video_port = video_port
self.audio_port = audio_port
self.control_port = control_port
self.record_file = record_file
def run(self, gst_option=''):
"""Launches the server process
:param: None
:gst-option: Any gstreamer option. Refer to http://www.linuxmanpages.com/man1/gst-launch-0.8.1.php#lbAF.
Should be added with spaces between them
:returns: nothing
"""
self.proc = None
self.pid = -1
self.gst_option_string = gst_option
print "Starting server"
self.proc = self._run_process()
self.pid = self.proc.pid
# TODO: Sleep time may vary
sleep(self.SLEEP_TIME)
def _run_process(self):
"""Private method for running gst-switch-srv process
"""
cmd = self.path
# cmd = ''
cmd += """gst-switch-srv \
%s \
--video-input-port=%s \
--audio-input-port=%s \
--control-port=%s \
--record=%s """ % (self.gst_option_string,
self.video_port,
self.audio_port,
self.control_port,
self.record_file)
proc = self._start_process(cmd)
return proc
def _start_process(self, cmd):
"""Private method for starting a process
:param cmd: The command which needs to be excecuted
:returns: process created
"""
print 'Creating process %s' % (cmd)
try:
tempf = open(os.devnull, 'w')
process = subprocess.Popen(cmd.split(), stdout=tempf, stderr=tempf, bufsize=-1, shell=False)
except IOError as e:
print "I/O error: os.devnull: {0}".format(e.strerror)
except OSError as e:
print "OS error: {0}: {1}".format(cmd, e.strerror)
return process
def terminate(self):
"""Terminates the server
:param: None
:returns: True when success
"""
print 'Killing server'
proc = self.proc
ret = True
try:
proc.terminate()
print 'Server Killed'
except:
print 'Error killing server'
ret = False
return ret
def kill(self):
"""Kills the server process by sending SIGKILL
:param: None
:returns: nothing
"""
os.kill(self.pid, signal.SIGKILL)