diff --git a/src/selkies_gstreamer/__main__.py b/src/selkies_gstreamer/__main__.py index 0e33db4..e16b9b3 100644 --- a/src/selkies_gstreamer/__main__.py +++ b/src/selkies_gstreamer/__main__.py @@ -471,6 +471,12 @@ def main(): parser.add_argument('--metrics_http_port', default=os.environ.get('SELKIES_METRICS_HTTP_PORT', '8000'), help='Port to start the Prometheus metrics server on') + parser.add_argument('--start-after-connect', + default=os.environ.get('SELKIES_START_AFTER_CONNECT', ''), + help='Program or command to run on server after the first client has connected') + parser.add_argument('--start-after-disconnect', + default=os.environ.get('SELKIES_START_AFTER_DISCONNECT', ''), + help='Program or command to run on server after the last client has disconnected') parser.add_argument('--debug', action='store_true', help='Enable debug logging') args = parser.parse_args() @@ -516,6 +522,12 @@ def main(): using_webrtc_csv = args.enable_webrtc_statistics.lower() == 'true' metrics = Metrics(int(args.metrics_http_port), using_webrtc_csv) + # Setup commands to run on connect and disconnect + if args.start_after_connect != '': + os.environ['SELKIES_START_AFTER_CONNECT'] = args.start_after_connect + if args.start_after_disconnect != '': + os.environ['SELKIES_START_AFTER_DISCONNECT'] = args.start_after_disconnect + # Initialize the signalling client using_https = args.enable_https.lower() == 'true' using_basic_auth = args.enable_basic_auth.lower() == 'true' diff --git a/src/selkies_gstreamer/signalling_web.py b/src/selkies_gstreamer/signalling_web.py index 4f7008a..93de117 100644 --- a/src/selkies_gstreamer/signalling_web.py +++ b/src/selkies_gstreamer/signalling_web.py @@ -14,7 +14,6 @@ # Author: Nirbheek Chauhan import os -import base64 import sys import ssl import logging @@ -32,6 +31,7 @@ import hashlib import hmac import base64 +import subprocess from pathlib import Path from http import HTTPStatus @@ -277,6 +277,15 @@ async def cleanup_session(self, uid): wso, oaddr, _, _ = self.peers[other_id] del self.peers[other_id] await wso.close() + # Run application after last session disconnected + try: + if 'SELKIES_START_AFTER_DISCONNECT' in os.environ and len(self.sessions) == 0: + if os.environ['SELKIES_START_AFTER_DISCONNECT'] != '': + command = os.environ['SELKIES_START_AFTER_DISCONNECT'].split(' ') + subprocess.Popen(command, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + except Exception as e: + logger.error('Failed to run SELKIES_START_AFTER_DISCONNECT: {}'.format(e)) async def cleanup_room(self, uid, room_id): room_peers = self.rooms[room_id] @@ -367,6 +376,15 @@ async def connection_handler(self, ws, uid, meta=None): wsc = self.peers[callee_id][0] logger.info('Session from {!r} ({!r}) to {!r} ({!r})' ''.format(uid, raddr, callee_id, wsc.remote_address)) + # Run application on first session connection + try: + if 'SELKIES_START_AFTER_CONNECT' in os.environ and len(self.sessions) == 0: + if os.environ['SELKIES_START_AFTER_CONNECT'] != '': + command = os.environ['SELKIES_START_AFTER_CONNECT'].split(' ') + subprocess.Popen(command, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + except Exception as e: + logger.error('Failed to run SELKIES_START_AFTER_CONNECT: {}'.format(e)) # Register session self.peers[uid][2] = peer_status = 'session' self.sessions[uid] = callee_id