Skip to content

Commit 12d5fee

Browse files
author
Your Name
committed
More flexible port assignment for streaming
1 parent 85a00f2 commit 12d5fee

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

octoprint_obico/janus.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,26 @@
2020
from .lib import alert_queue
2121
from .janus_config_builder import RUNTIME_JANUS_ETC_DIR
2222

23-
2423
_logger = logging.getLogger('octoprint.plugins.obico')
2524

25+
JANUS_WS_PORT = 17730 # Janus needs to use 17730 up to 17750. Hard-coded for now. may need to make it dynamic if the problem of port conflict is too much
26+
27+
def janus_pid_file_path():
28+
global JANUS_WS_PORT
29+
return '/tmp/obico-janus-{janus_port}.pid'.format(janus_port=JANUS_WS_PORT)
30+
31+
# Make sure the port is available in case there are multiple obico instances running
32+
for i in range(0, 100):
33+
if os.path.exists(janus_pid_file_path()):
34+
JANUS_WS_PORT += 20 # 20 is a big-enough gap for all ports needed for 1 octoprint instance.
35+
36+
JANUS_ADMIN_WS_PORT = JANUS_WS_PORT + 1
37+
2638
class JanusConn:
2739

28-
def __init__(self, plugin, janus_server, janus_port):
40+
def __init__(self, plugin, janus_server):
2941
self.plugin = plugin
3042
self.janus_server = janus_server
31-
self.janus_port = janus_port
3243
self.janus_ws = None
3344
self.shutting_down = False
3445

@@ -43,7 +54,7 @@ def run_janus_forever():
4354
_logger.debug('Popen: {} {}'.format(env, janus_cmd))
4455
janus_proc = subprocess.Popen(janus_cmd.split(), env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
4556

46-
with open(self.janus_pid_file_path(), 'w') as pid_file:
57+
with open(janus_pid_file_path(), 'w') as pid_file:
4758
pid_file.write(str(janus_proc.pid))
4859

4960
while True:
@@ -70,33 +81,35 @@ def pass_to_janus(self, msg):
7081

7182
def wait_for_janus(self):
7283
time.sleep(0.2)
73-
wait_for_port(self.janus_server, self.janus_port)
84+
wait_for_port(self.janus_server, JANUS_WS_PORT)
7485

7586
def start_janus_ws(self):
7687

7788
def on_close(ws, **kwargs):
7889
_logger.warn('Janus WS connection closed!')
7990

8091
self.janus_ws = WebSocketClient(
81-
'ws://{}:{}/'.format(self.janus_server, self.janus_port),
92+
'ws://{}:{}/'.format(self.janus_server, JANUS_WS_PORT),
8293
on_ws_msg=self.process_janus_msg,
8394
on_ws_close=on_close,
8495
subprotocols=['janus-protocol'],
8596
waitsecs=30)
8697

87-
def janus_pid_file_path(self):
88-
return '/tmp/obico-janus-{janus_port}.pid'.format(janus_port=self.janus_port)
89-
9098
def kill_janus_if_running(self):
9199
try:
92100
# It is possible that orphaned janus process is running (maybe previous python process was killed -9?).
93101
# Ensure the process is killed before launching a new one
94-
with open(self.janus_pid_file_path(), 'r') as pid_file:
102+
with open(janus_pid_file_path(), 'r') as pid_file:
95103
subprocess.run(['kill', pid_file.read()], check=True)
96-
wait_for_port_to_close(self.janus_server, self.janus_port)
104+
wait_for_port_to_close(self.janus_server, JANUS_WS_PORT)
97105
except Exception as e:
98106
_logger.warning('Failed to shutdown Janus - ' + str(e))
99107

108+
try:
109+
os.remove(janus_pid_file_path())
110+
except:
111+
pass
112+
100113
def shutdown(self):
101114
self.shutting_down = True
102115

octoprint_obico/webcam_stream.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,14 @@
3434
from .lib import alert_queue
3535
from .webcam_capture import capture_jpeg, webcam_full_url
3636
from .janus_config_builder import build_janus_config
37-
from .janus import JanusConn
37+
from .janus import JanusConn, JANUS_WS_PORT, JANUS_ADMIN_WS_PORT
3838

3939

4040
_logger = logging.getLogger('octoprint.plugins.obico')
4141

4242
FFMPEG_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bin', 'ffmpeg')
4343
FFMPEG = os.path.join(FFMPEG_DIR, 'run.sh')
4444

45-
JANUS_WS_PORT = 17730 # Janus needs to use 17730 up to 17750. Hard-coded for now. may need to make it dynamic if the problem of port conflict is too much
46-
JANUS_ADMIN_WS_PORT = JANUS_WS_PORT + 1
47-
4845
RECODE_RESOLUTIONS_43 = {
4946
'low': (320, 240),
5047
'medium': (640, 480),
@@ -57,7 +54,6 @@
5754
'high': (1280, 720),
5855
}
5956

60-
6157
def bitrate_for_dim(img_w, img_h):
6258
dim = img_w * img_h
6359
if dim <= 480 * 270:
@@ -230,7 +226,7 @@ def start(self, webcam_configs):
230226
self.shutdown()
231227
return
232228

233-
self.janus = JanusConn(self.plugin, janus_server, JANUS_WS_PORT)
229+
self.janus = JanusConn(self.plugin, janus_server)
234230
self.janus.start(janus_bin_path, ld_lib_path)
235231

236232
if not self.wait_for_janus():
@@ -505,6 +501,11 @@ def kill_ffmpeg_if_running(self, rtc_port):
505501
except Exception as e:
506502
_logger.warning('Failed to shutdown ffmpeg - ' + str(e))
507503

504+
try:
505+
os.remove(self.ffmpeg_pid_file_path(rtc_port))
506+
except:
507+
pass
508+
508509
def shutdown_subprocesses(self):
509510
if self.janus:
510511
self.janus.shutdown()

0 commit comments

Comments
 (0)