Simdrom

Stadia Stream Monitor

Jan 2nd, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Stadia Stream Monitor
  3. // @version      0.2
  4. // @author       AquaRegia (Modif by Simdrom)
  5. // @match        https://stadia.google.com/*
  6. // @grant        none
  7. // ==/UserScript==
  8.  
  9. (function() {
  10.     'use strict';
  11.  
  12.     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]}
  13.  
  14.     var peerConnections = [];
  15.  
  16.     (function(original) {
  17.         RTCPeerConnection = function() {
  18.             var connection = new original(arguments);
  19.             peerConnections.push(connection);
  20.             return connection;
  21.         };
  22.         RTCPeerConnection.prototype = original.prototype;
  23.     })(RTCPeerConnection);
  24.  
  25.     var infoBox = document.createElement("div");
  26.     infoBox.id = "infoBox";
  27.     infoBox.innerHTML = "Start a game to monitor traffic";
  28.     infoBox.style.position = "fixed";
  29.     infoBox.style.top = 0;
  30.     infoBox.style.left = 0;
  31.     infoBox.style.width = "215px";
  32.     infoBox.style.opacity = 0.5;
  33.     infoBox.style.zIndex = 1000;
  34.     infoBox.style.backgroundColor = "black";
  35.     infoBox.style.padding = "5px";
  36.  
  37.     infoBox.location = 1;
  38.  
  39.     document.body.appendChild(infoBox);
  40.  
  41.     window.addEventListener("keydown", function(e)
  42.     {
  43.         if(e.ctrlKey && e.key == "m")
  44.         {
  45.             infoBox.location = (infoBox.location + 1) % 5;
  46.  
  47.             switch(infoBox.location)
  48.             {
  49.                 case 0:
  50.                     infoBox.style.display = "none";
  51.                     break;
  52.                 case 1:
  53.                     infoBox.style.top = 0;
  54.                     infoBox.style.right = "";
  55.                     infoBox.style.bottom = "";
  56.                     infoBox.style.left = 0;
  57.                     infoBox.style.display = "block";
  58.                     break;
  59.                 case 2:
  60.                     infoBox.style.top = 0;
  61.                     infoBox.style.right = 0;
  62.                     infoBox.style.bottom = "";
  63.                     infoBox.style.left = "";
  64.                     infoBox.style.display = "block";
  65.                     break;
  66.                 case 3:
  67.                     infoBox.style.top = "";
  68.                     infoBox.style.right = 0;
  69.                     infoBox.style.bottom = 0;
  70.                     infoBox.style.left = "";
  71.                     infoBox.style.display = "block";
  72.                     break;
  73.                 case 4:
  74.                     infoBox.style.top = "";
  75.                     infoBox.style.right = "";
  76.                     infoBox.style.bottom = 0;
  77.                     infoBox.style.left = 0;
  78.                     infoBox.style.display = "block";
  79.                     break;
  80.             }
  81.         }
  82.     });
  83.  
  84.     var lastBytes = 0;
  85.     var lastFrames = 0;
  86.     var staticTime = new Date();
  87.     var staticTimeChanged = false;
  88.     setInterval(function()
  89.     {
  90.         if(document.location.href.indexOf("/player/") == -1)
  91.         {
  92.             peerConnections = [];
  93.             lastBytes = 0;
  94.             lastFrames = 0;
  95.             staticTimeChanged = false;
  96.  
  97.             infoBox.innerHTML = "Start a game to monitor traffic";
  98.  
  99.         }
  100.         else if(peerConnections.length == 3)
  101.         {
  102.             peerConnections[2].getStats().then(function(stats)
  103.             {
  104.                 for(var key of stats.keys())
  105.                 {
  106.                     if(key.indexOf("RTCInboundRTPVideoStream") != -1)
  107.                     {
  108.                         var tmp1 = stats.get(key);
  109.                         var tmp2 = stats.get(tmp1.trackId);
  110.  
  111.                         var time = new Date();
  112.                         if(!staticTimeChanged){
  113.                             staticTime = new Date();
  114.                             staticTimeChanged = true;
  115.                         }
  116.  
  117.  
  118.                         var timeStart = staticTime.getTime();
  119.                         var timeEnd =time.getTime();
  120.                         var hourDiff = timeEnd - timeStart; //in ms
  121.                         var secDiff = hourDiff / 1000; //in s
  122.                         var minDiff = hourDiff / 60 / 1000; //in minutes
  123.                         var hDiff = hourDiff / 3600 / 1000; //in hours
  124.                         var humanReadable = {};
  125.                         humanReadable.hours = Math.floor(hDiff);
  126.                         humanReadable.minutes = Math.floor(minDiff - 60 * humanReadable.hours);
  127.                         humanReadable.seconds = Math.floor(secDiff);
  128.                         console.log(humanReadable); //{hours: 0, minutes: 30}*/
  129.                         var sessionTime = humanReadable.seconds;
  130.  
  131.                         time = new Date(time - time.getTimezoneOffset() * 60 * 1000).toISOString().replace("T", " ").split(".")[0];
  132.                         var resolution = tmp2.frameWidth + "x" + tmp2.frameHeight;
  133.                         var framesReceived = tmp2.framesReceived;
  134.                         var framesReceivedPerSecond = (framesReceived - lastFrames);
  135.                         var codec = peerConnections[2].getReceivers()[1].getParameters().codecs[0].mimeType.split("/")[1];
  136.                         var bytesReceived = tmp1.bytesReceived;
  137.                         var bytesReceivedPerSecond = (bytesReceived - lastBytes);
  138.                         var packetsLost = tmp1.packetsLost;
  139.                         var framesDropped = tmp2.framesDropped;
  140.  
  141.                         lastFrames = framesReceived;
  142.                         lastBytes = bytesReceived;
  143.  
  144.                         if(framesReceived > 0)
  145.                         {
  146.                             var html = "";
  147.  
  148.                             html += "<b>" + time + "</b>";
  149.                             html += "<br/>";
  150.  
  151.                             if(sessionTime < 60)
  152.                             {
  153.                                 html += "Session: " + humanReadable.seconds +" sec";
  154.                             }
  155.                             else if (sessionTime < 3600)
  156.                             {
  157.                                 html += "Session: " + humanReadable.minutes +" min";
  158.                             }
  159.                             else
  160.                             {
  161.                                 html += "Session: " + humanReadable.hours +" hours";
  162.                             }
  163.  
  164.                             html += "<br/>";
  165.  
  166.                             html += "Resolution: " + resolution;
  167.                             html += "<br/>";
  168.  
  169.                             html += "FPS: " + framesReceivedPerSecond;
  170.                             html += "<br/>";
  171.  
  172.                             html += "Codec: " + codec;
  173.                             html += "<br/>";
  174.  
  175.                             html += "Session traffic: " + formatBytes(bytesReceived, 2);
  176.                             html += "<br/>";
  177.  
  178.                             html += "Current traffic: " + formatBytes(bytesReceivedPerSecond*8, 2).slice(0, -1) + "b/s";
  179.                             html += "<br/>";
  180.  
  181.                             html += "Packets lost: " + packetsLost;
  182.                             html += "<br/>";
  183.  
  184.                             html += "Frames dropped: " + framesDropped;
  185.                             html += "<br/>";
  186.  
  187.                             infoBox.innerHTML = html;
  188.                         }
  189.                     }
  190.                 }
  191.             });
  192.         }
  193.     }, 1000);
  194. })();
Advertisement
Add Comment
Please, Sign In to add comment