2020from .lib import alert_queue
2121from .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+
2638class 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
0 commit comments