Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Stadia Stream Monitor
- // @version 0.2
- // @author AquaRegia (Modif by Simdrom)
- // @match https://stadia.google.com/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- function formatBytes(a,b){if(0==a)return"0 Bytes";var c=1024,d=b||2,e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],f=Math.floor(Math.log(a)/Math.log(c));return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f]}
- var peerConnections = [];
- (function(original) {
- RTCPeerConnection = function() {
- var connection = new original(arguments);
- peerConnections.push(connection);
- return connection;
- };
- RTCPeerConnection.prototype = original.prototype;
- })(RTCPeerConnection);
- var infoBox = document.createElement("div");
- infoBox.id = "infoBox";
- infoBox.innerHTML = "Start a game to monitor traffic";
- infoBox.style.position = "fixed";
- infoBox.style.top = 0;
- infoBox.style.left = 0;
- infoBox.style.width = "215px";
- infoBox.style.opacity = 0.5;
- infoBox.style.zIndex = 1000;
- infoBox.style.backgroundColor = "black";
- infoBox.style.padding = "5px";
- infoBox.location = 1;
- document.body.appendChild(infoBox);
- window.addEventListener("keydown", function(e)
- {
- if(e.ctrlKey && e.key == "m")
- {
- infoBox.location = (infoBox.location + 1) % 5;
- switch(infoBox.location)
- {
- case 0:
- infoBox.style.display = "none";
- break;
- case 1:
- infoBox.style.top = 0;
- infoBox.style.right = "";
- infoBox.style.bottom = "";
- infoBox.style.left = 0;
- infoBox.style.display = "block";
- break;
- case 2:
- infoBox.style.top = 0;
- infoBox.style.right = 0;
- infoBox.style.bottom = "";
- infoBox.style.left = "";
- infoBox.style.display = "block";
- break;
- case 3:
- infoBox.style.top = "";
- infoBox.style.right = 0;
- infoBox.style.bottom = 0;
- infoBox.style.left = "";
- infoBox.style.display = "block";
- break;
- case 4:
- infoBox.style.top = "";
- infoBox.style.right = "";
- infoBox.style.bottom = 0;
- infoBox.style.left = 0;
- infoBox.style.display = "block";
- break;
- }
- }
- });
- var lastBytes = 0;
- var lastFrames = 0;
- var staticTime = new Date();
- var staticTimeChanged = false;
- setInterval(function()
- {
- if(document.location.href.indexOf("/player/") == -1)
- {
- peerConnections = [];
- lastBytes = 0;
- lastFrames = 0;
- staticTimeChanged = false;
- infoBox.innerHTML = "Start a game to monitor traffic";
- }
- else if(peerConnections.length == 3)
- {
- peerConnections[2].getStats().then(function(stats)
- {
- for(var key of stats.keys())
- {
- if(key.indexOf("RTCInboundRTPVideoStream") != -1)
- {
- var tmp1 = stats.get(key);
- var tmp2 = stats.get(tmp1.trackId);
- var time = new Date();
- if(!staticTimeChanged){
- staticTime = new Date();
- staticTimeChanged = true;
- }
- var timeStart = staticTime.getTime();
- var timeEnd =time.getTime();
- var hourDiff = timeEnd - timeStart; //in ms
- var secDiff = hourDiff / 1000; //in s
- var minDiff = hourDiff / 60 / 1000; //in minutes
- var hDiff = hourDiff / 3600 / 1000; //in hours
- var humanReadable = {};
- humanReadable.hours = Math.floor(hDiff);
- humanReadable.minutes = Math.floor(minDiff - 60 * humanReadable.hours);
- humanReadable.seconds = Math.floor(secDiff);
- console.log(humanReadable); //{hours: 0, minutes: 30}*/
- var sessionTime = humanReadable.seconds;
- time = new Date(time - time.getTimezoneOffset() * 60 * 1000).toISOString().replace("T", " ").split(".")[0];
- var resolution = tmp2.frameWidth + "x" + tmp2.frameHeight;
- var framesReceived = tmp2.framesReceived;
- var framesReceivedPerSecond = (framesReceived - lastFrames);
- var codec = peerConnections[2].getReceivers()[1].getParameters().codecs[0].mimeType.split("/")[1];
- var bytesReceived = tmp1.bytesReceived;
- var bytesReceivedPerSecond = (bytesReceived - lastBytes);
- var packetsLost = tmp1.packetsLost;
- var framesDropped = tmp2.framesDropped;
- lastFrames = framesReceived;
- lastBytes = bytesReceived;
- if(framesReceived > 0)
- {
- var html = "";
- html += "<b>" + time + "</b>";
- html += "<br/>";
- if(sessionTime < 60)
- {
- html += "Session: " + humanReadable.seconds +" sec";
- }
- else if (sessionTime < 3600)
- {
- html += "Session: " + humanReadable.minutes +" min";
- }
- else
- {
- html += "Session: " + humanReadable.hours +" hours";
- }
- html += "<br/>";
- html += "Resolution: " + resolution;
- html += "<br/>";
- html += "FPS: " + framesReceivedPerSecond;
- html += "<br/>";
- html += "Codec: " + codec;
- html += "<br/>";
- html += "Session traffic: " + formatBytes(bytesReceived, 2);
- html += "<br/>";
- html += "Current traffic: " + formatBytes(bytesReceivedPerSecond*8, 2).slice(0, -1) + "b/s";
- html += "<br/>";
- html += "Packets lost: " + packetsLost;
- html += "<br/>";
- html += "Frames dropped: " + framesDropped;
- html += "<br/>";
- infoBox.innerHTML = html;
- }
- }
- }
- });
- }
- }, 1000);
- })();
Advertisement
Add Comment
Please, Sign In to add comment