Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging webrtc statistics on host received over datachannel #141

Merged
merged 17 commits into from
Apr 15, 2024
Merged
3 changes: 3 additions & 0 deletions addons/gst-web/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ function onBothStreamConnected() {

statsStart = now;

webrtc.sendDataChannelMessage("_stats_video," + JSON.stringify(stats.allReports));
webrtc.sendDataChannelMessage("_stats_audio," + JSON.stringify(audioStats.allReports));

// Stats refresh loop.
setTimeout(statsLoop, 1000);
});
Expand Down
42 changes: 31 additions & 11 deletions src/selkies_gstreamer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ def main():
parser.add_argument('--video_bitrate',
default=os.environ.get('SELKIES_VIDEO_BITRATE', '2000'),
help='Default video bitrate')
parser.add_argument('--keyframe_distance',
default=os.environ.get('SELKIES_KEYFRAME_DISTANCE', '3'),
help='Distance between video GOP frames/Keyframes in seconds, use "-1" for infinite distance')
parser.add_argument('--packetloss_percent',
default=os.environ.get('SELKIES_PACKETLOSS_PERCENT', '5'),
help='Expected packet loss percentage (%) for ULP/RED Forward Error Correction (FEC) in video and audio, use "0" to disable FEC')
parser.add_argument('--audio_bitrate',
default=os.environ.get('SELKIES_AUDIO_BITRATE', '48000'),
help='Default audio bitrate')
Expand All @@ -417,11 +423,17 @@ def main():
parser.add_argument('--cursor_size',
default=os.environ.get('SELKIES_CURSOR_SIZE', os.environ.get('XCURSOR_SIZE', '-1')),
help='Cursor size in points for the local cursor, set instead XCURSOR_SIZE without of this argument to configure the cursor size for both the local and remote cursors')
parser.add_argument('--enable_metrics',
default=os.environ.get('SELKIES_ENABLE_METRICS', 'false'),
help='Enable the Prometheus metrics port')
parser.add_argument('--metrics_port',
default=os.environ.get('SELKIES_METRICS_PORT', '8000'),
parser.add_argument('--enable_webrtc_statistics',
default=os.environ.get('SELKIES_ENABLE_WEBRTC_STATISTICS', 'false'),
help='Enable WebRTC Statistics CSV dumping to the directory --webrtc_statistics_dir with filenames selkies-stats-video-[timestamp].csv and selkies-stats-audio-[timestamp].csv')
parser.add_argument('--webrtc_statistics_dir',
default=os.environ.get('SELKIES_WEBRTC_STATISTICS_DIR', '/tmp'),
help='Directory to save WebRTC Statistics CSV from client with filenames selkies-stats-video-[timestamp].csv and selkies-stats-audio-[timestamp].csv')
parser.add_argument('--enable_metrics_http',
default=os.environ.get('SELKIES_ENABLE_METRICS_HTTP', 'false'),
help='Enable the Prometheus HTTP metrics port')
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('--debug', action='store_true',
help='Enable debug logging')
Expand Down Expand Up @@ -464,8 +476,9 @@ def main():
audio_peer_id = 3

# Initialize metrics server.
using_metrics = args.enable_metrics.lower() == 'true'
metrics = Metrics(int(args.metrics_port))
using_metrics_http = args.enable_metrics_http.lower() == 'true'
using_webrtc_csv = args.enable_webrtc_statistics.lower() == 'true'
metrics = Metrics(int(args.metrics_http_port), using_webrtc_csv)

# Initialize the signalling client
using_https = args.enable_https.lower() == 'true'
Expand Down Expand Up @@ -564,10 +577,12 @@ async def on_audio_signalling_error(e):
enable_cursors = args.enable_cursors.lower() == "true"
cursor_debug = args.debug_cursors.lower() == "true"
cursor_size = int(args.cursor_size)
keyframe_distance = float(args.keyframe_distance)
packetloss_percent = float(args.packetloss_percent)

# Create instance of app
app = GSTWebRTCApp(stun_servers, turn_servers, audio_channels, curr_fps, args.encoder, curr_video_bitrate, curr_audio_bitrate)
audio_app = GSTWebRTCApp(stun_servers, turn_servers, audio_channels, curr_fps, args.encoder, curr_video_bitrate, curr_audio_bitrate)
app = GSTWebRTCApp(stun_servers, turn_servers, audio_channels, curr_fps, args.encoder, curr_video_bitrate, curr_audio_bitrate, keyframe_distance, packetloss_percent)
audio_app = GSTWebRTCApp(stun_servers, turn_servers, audio_channels, curr_fps, args.encoder, curr_video_bitrate, curr_audio_bitrate, keyframe_distance, packetloss_percent)

# [END main_setup]

Expand Down Expand Up @@ -719,6 +734,9 @@ def enable_resize_handler(enabled, enable_res):
# Send client latency to metrics
webrtc_input.on_client_latency = lambda latency_ms: metrics.set_latency(latency_ms)

# Send WebRTC stats to metrics
webrtc_input.on_client_webrtc_stats = lambda webrtc_stat_type, webrtc_stats: metrics.set_webrtc_stats(webrtc_stat_type, webrtc_stats)

# Initialize GPU monitor
gpu_mon = GPUMonitor(enabled=args.encoder.startswith("nv"))

Expand Down Expand Up @@ -808,8 +826,8 @@ def mon_rtc_config(stun_servers, turn_servers, rtc_config):

try:
asyncio.ensure_future(server.run(), loop=loop)
if using_metrics:
metrics.start()
if using_metrics_http:
metrics.start_http()
loop.run_until_complete(webrtc_input.connect())
loop.run_in_executor(None, lambda: webrtc_input.start_clipboard())
loop.run_in_executor(None, lambda: webrtc_input.start_cursor_monitor())
Expand All @@ -820,6 +838,8 @@ def mon_rtc_config(stun_servers, turn_servers, rtc_config):
loop.run_in_executor(None, lambda: system_mon.start())

while True:
if using_webrtc_csv:
metrics.initialize_webrtc_csv_file(args.webrtc_statistics_dir)
asyncio.ensure_future(app.handle_bus_calls(), loop=loop)
asyncio.ensure_future(audio_app.handle_bus_calls(), loop=loop)

Expand Down
Loading