Advertisement
Guest User

Untitled

a guest
Jan 25th, 2024
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let localStream;
  2. let localVideo;
  3. let peerConnection;
  4. let remoteVideo;
  5. let serverConnection;
  6. let uuid;
  7.  
  8.  
  9.  
  10. const peerConnectionConfig = {
  11.     'iceServers': [
  12.         {'urls': 'stun:stun.stunprotocol.org:3478'},
  13.         {'urls': 'stun:stun.l.google.com:19302'},
  14.     ]
  15. };
  16.  
  17. console.log("Constructor??", WebRTCStats);
  18.  
  19. function startRender() {
  20.     let canvas = document.querySelector("#displaySurface");
  21.     let context = canvas.getContext("2d");
  22.     let startTime = Date.now();
  23.     setInterval(() => {
  24.         context.clearRect(0, 0, canvas.width, canvas.height);
  25.         let ellapsedTime = Date.now() - startTime;
  26.         let rotation = 360 * ((ellapsedTime + 1.0) / 100) * Math.PI / 180
  27.         context.fillStyle = 'white';
  28.         context.fillRect(0, 0, canvas.width, canvas.height)
  29.         //context.save();
  30.         context.translate(canvas.width / 2, canvas.height / 2)
  31.         context.rotate(rotation * Math.PI / 180);
  32.         context.translate(-canvas.width / 2, -canvas.height / 2)
  33.         context.fillStyle = 'blue';
  34.         context.fillRect(50, 50, canvas.width - 100, canvas.height - 100)
  35.         //context.translate(-canvas.width / 2, -canvas.height / 2)
  36.         context.translate(canvas.width / 2, canvas.height / 2)
  37.         context.rotate(-rotation * Math.PI / 180);
  38.         context.translate(-canvas.width / 2, -canvas.height / 2)
  39.         //context.restore();
  40.     }, 1000 / 25);
  41. }
  42.  
  43. async function pageReady() {
  44.     uuid = createUUID();
  45.     startRender();
  46.  
  47.     localVideo = document.getElementById('localVideo');
  48.     remoteVideo = document.getElementById('remoteVideo');
  49.  
  50.     serverConnection = new WebSocket(`wss://...`);
  51.     serverConnection.onmessage = gotMessageFromServer;
  52.  
  53.     const constraints = {
  54.         video: true,
  55.     };
  56.  
  57.     localStream = document.querySelector("#displaySurface").captureStream(30);
  58.     //localVideo.srcObject = stream;
  59.  
  60.     // navigator.webkitGetUserMedia({
  61.     //     video: {
  62.     //         displaySurface: "browser",
  63.     //     },
  64.     //     audio: {
  65.     //         suppressLocalAudioPlayback: false,
  66.     //     },
  67.     //     preferCurrentTab: false,
  68.     //     selfBrowserSurface: "exclude",
  69.     //     systemAudio: "include",
  70.     //     surfaceSwitching: "include",
  71.     //     monitorTypeSurfaces: "include",
  72.     // }, function (stream) {
  73.     //     localStream = stream;
  74.     //     localVideo.srcObject = stream;
  75.     //     //video.src = window.webkitURL.createObjectURL(stream);
  76.     //     //video.play();
  77.     // }, error => {
  78.     //     console.error("Error happened during the local video stream creation: ", error);
  79.     // });
  80.  
  81.     // if(!navigator.mediaDevices.getDisplayMedia) {
  82.     //   alert('Your browser does not support getUserMedia API');
  83.     //   return;
  84.     // }
  85.  
  86.  
  87.     try {
  88.         // let useStream = confirm("Use stream?");
  89.         // if(useStream){
  90.         //   //const stream = await navigator.mediaDevices.getDisplayMedia(constraints);
  91.         //
  92.         //   localStream = stream;
  93.         //   localVideo.srcObject = stream;
  94.         // }
  95.         // localStream = stream;
  96.         // localVideo.srcObject = stream;
  97.     } catch (error) {
  98.         errorHandler(error);
  99.     }
  100. }
  101.  
  102. window.onload = () => {
  103.     pageReady();
  104.     setTimeout(() => start(true), 1000);
  105. };
  106. var channel;
  107. function start(isCaller) {
  108.     let webrtcStats = new WebRTCStats({
  109.         getStatsInterval: 1000,
  110.         debug: true,
  111.         logLevel: 'info'
  112.     });
  113.     webrtcStats.on('stats', (ev) => {
  114.         console.log('stats', ev)
  115.     })
  116.  
  117.     peerConnection = new RTCPeerConnection(peerConnectionConfig);
  118.     channel = peerConnection.createDataChannel("Something");
  119.     console.log("Channel: ", channel);
  120.     channel.onopen = (event) => {
  121.         console.log("Channel opened?");
  122.         channel.send("Some shit event from the shit Tizen");
  123.     };
  124.  
  125.     channel.onerror = (error) => {
  126.         console.error("Error during the data channel creation??: ", channel);
  127.     };
  128.     channel.onbufferedamountlow = () => {
  129.         console.error("noBufferAmountLow");
  130.     };
  131.     channel.onclose = () => {
  132.         console.error("onClose");
  133.     };
  134.     channel.onclosing = () => {
  135.         console.log("onClosing");
  136.     };
  137.     webrtcStats.addConnection({
  138.         pc: peerConnection,
  139.         peerId: '1',
  140.     });
  141.     console.log("Sender capabilities: ", RTCRtpSender.getCapabilities("video"));
  142.     console.log("Receiver capabilities: ", RTCRtpReceiver.getCapabilities("video"));
  143.     for (const track of localStream.getTracks()) {
  144.         peerConnection.addTrack(track, localVideo.captureStream(30));
  145.     }
  146.     console.log(peerConnection.getTransceivers());
  147.     peerConnection.getTransceivers()[0].setCodecPreferences(RTCRtpSender.getCapabilities("video").codecs.filter(item => item.mimeType.includes("VP8")))
  148.  
  149.     peerConnection.onicecandidate = gotIceCandidate;
  150.     peerConnection.ontrack = gotRemoteStream;
  151.  
  152.     if (isCaller) {
  153.         peerConnection.createOffer().then(createdDescription).catch(errorHandler);
  154.     }
  155. }
  156.  
  157. function gotMessageFromServer(message) {
  158.     if (!peerConnection) start(false);
  159.  
  160.     const signal = JSON.parse(message.data);
  161.  
  162.     // Ignore messages from ourself
  163.     if (signal.uuid == uuid) return;
  164.  
  165.     if (signal.sdp) {
  166.         peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(() => {
  167.             // Only create answers in response to offers
  168.             if (signal.sdp.type !== 'offer') return;
  169.  
  170.             peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
  171.         }).catch(errorHandler);
  172.     } else if (signal.ice) {
  173.         peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
  174.     }
  175. }
  176.  
  177. function gotIceCandidate(event) {
  178.     if (event.candidate != null) {
  179.         serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid}));
  180.     }
  181. }
  182.  
  183. function createdDescription(description) {
  184.     console.log('got description');
  185.  
  186.     peerConnection.setLocalDescription(description).then(() => {
  187.         serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid}));
  188.     }).catch(errorHandler);
  189. }
  190.  
  191. function gotRemoteStream(event) {
  192.     console.log('got remote stream');
  193.     remoteVideo.srcObject = event.streams[0];
  194. }
  195.  
  196. function errorHandler(error) {
  197.     console.log(error);
  198. }
  199.  
  200. // Taken from http://stackoverflow.com/a/105074/515584
  201. // Strictly speaking, it's not a real UUID, but it gets the job done here
  202. function createUUID() {
  203.     function s4() {
  204.         return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
  205.     }
  206.  
  207.     return `${s4() + s4()}-cond browser window then cli${s4()}-${s4()}-${s4()}-${s4() + s4() + s4()}`;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement