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
Next Next commit
Log webrtc statistics on host received over datachannel
  • Loading branch information
PMohanJ committed Apr 6, 2024
commit 42297706682f4a0f242fe405105cd082f0c60d24
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;

var jsonString = JSON.stringify(stats.allReports);
webrtc.sendDataChannelMessage("_stats," + jsonString)

// Stats refresh loop.
setTimeout(statsLoop, 1000);
});
Expand Down
40 changes: 40 additions & 0 deletions src/selkies_gstreamer/webrtc_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
from PIL import Image
from gamepad import SelkiesGamepad

from datetime import datetime
import csv
import json

import logging
logger = logging.getLogger("webrtc_input")
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -113,6 +117,8 @@ def __init__(self, uinput_mouse_socket_path="", js_socket_path="", enable_clipbo
self.button_mask = 0

self.ping_start = None
self.stats_file_path = "/tmp/stats.csv"
self.written_headers = None

self.on_video_encoder_bit_rate = lambda bitrate: logger.warn(
'unhandled on_video_encoder_bit_rate')
Expand Down Expand Up @@ -719,5 +725,39 @@ def on_message(self, msg):
except:
logger.error(
"failed to parse latency report from client" + str(toks))
elif toks[0] == "_stats":
# Webrtc Statistics API data from client
try:
stats_obj = json.loads(",".join(toks[1:]))
self.write_webrtc_stats(stats_obj)
except:
logger.error("failed to deserialize JSON object to python object")
else:
logger.info('unknown data channel message: %s' % msg)

def write_webrtc_stats(self, obj_list):
"""Writes the webrtc statistics to a CSV file.

Arguments:
obj_list {[list of object]} -- list of python objects/dict.
"""

dt = datetime.now()
timestamp = dt.strftime("%d/%B/%Y:%H:%M:%S")

headers = ["timestamp"]
values = [timestamp]
try:
with open(self.stats_file_path, "a") as stats_file:
csv_writer = csv.writer(stats_file)
for obj in obj_list:
headers.extend(list(obj.keys()))
values.extend(list(obj.values()))

if not self.written_headers:
csv_writer.writerow(headers)
self.written_headers = True

csv_writer.writerow(values)
except Exception as e:
logger.error("Error writing the webrtc-stats to CSV file "+ str(e))
Loading