Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let localStream;
- let localVideo;
- let peerConnection;
- let remoteVideo;
- let serverConnection;
- let uuid;
- const peerConnectionConfig = {
- 'iceServers': [
- {'urls': 'stun:stun.stunprotocol.org:3478'},
- {'urls': 'stun:stun.l.google.com:19302'},
- ]
- };
- console.log("Constructor??", WebRTCStats);
- function startRender() {
- let canvas = document.querySelector("#displaySurface");
- let context = canvas.getContext("2d");
- let startTime = Date.now();
- setInterval(() => {
- context.clearRect(0, 0, canvas.width, canvas.height);
- let ellapsedTime = Date.now() - startTime;
- let rotation = 360 * ((ellapsedTime + 1.0) / 100) * Math.PI / 180
- context.fillStyle = 'white';
- context.fillRect(0, 0, canvas.width, canvas.height)
- //context.save();
- context.translate(canvas.width / 2, canvas.height / 2)
- context.rotate(rotation * Math.PI / 180);
- context.translate(-canvas.width / 2, -canvas.height / 2)
- context.fillStyle = 'blue';
- context.fillRect(50, 50, canvas.width - 100, canvas.height - 100)
- //context.translate(-canvas.width / 2, -canvas.height / 2)
- context.translate(canvas.width / 2, canvas.height / 2)
- context.rotate(-rotation * Math.PI / 180);
- context.translate(-canvas.width / 2, -canvas.height / 2)
- //context.restore();
- }, 1000 / 25);
- }
- async function pageReady() {
- uuid = createUUID();
- startRender();
- localVideo = document.getElementById('localVideo');
- remoteVideo = document.getElementById('remoteVideo');
- serverConnection = new WebSocket(`wss://...`);
- serverConnection.onmessage = gotMessageFromServer;
- const constraints = {
- video: true,
- };
- localStream = document.querySelector("#displaySurface").captureStream(30);
- //localVideo.srcObject = stream;
- // navigator.webkitGetUserMedia({
- // video: {
- // displaySurface: "browser",
- // },
- // audio: {
- // suppressLocalAudioPlayback: false,
- // },
- // preferCurrentTab: false,
- // selfBrowserSurface: "exclude",
- // systemAudio: "include",
- // surfaceSwitching: "include",
- // monitorTypeSurfaces: "include",
- // }, function (stream) {
- // localStream = stream;
- // localVideo.srcObject = stream;
- // //video.src = window.webkitURL.createObjectURL(stream);
- // //video.play();
- // }, error => {
- // console.error("Error happened during the local video stream creation: ", error);
- // });
- // if(!navigator.mediaDevices.getDisplayMedia) {
- // alert('Your browser does not support getUserMedia API');
- // return;
- // }
- try {
- // let useStream = confirm("Use stream?");
- // if(useStream){
- // //const stream = await navigator.mediaDevices.getDisplayMedia(constraints);
- //
- // localStream = stream;
- // localVideo.srcObject = stream;
- // }
- // localStream = stream;
- // localVideo.srcObject = stream;
- } catch (error) {
- errorHandler(error);
- }
- }
- window.onload = () => {
- pageReady();
- setTimeout(() => start(true), 1000);
- };
- var channel;
- function start(isCaller) {
- let webrtcStats = new WebRTCStats({
- getStatsInterval: 1000,
- debug: true,
- logLevel: 'info'
- });
- webrtcStats.on('stats', (ev) => {
- console.log('stats', ev)
- })
- peerConnection = new RTCPeerConnection(peerConnectionConfig);
- channel = peerConnection.createDataChannel("Something");
- console.log("Channel: ", channel);
- channel.onopen = (event) => {
- console.log("Channel opened?");
- channel.send("Some shit event from the shit Tizen");
- };
- channel.onerror = (error) => {
- console.error("Error during the data channel creation??: ", channel);
- };
- channel.onbufferedamountlow = () => {
- console.error("noBufferAmountLow");
- };
- channel.onclose = () => {
- console.error("onClose");
- };
- channel.onclosing = () => {
- console.log("onClosing");
- };
- webrtcStats.addConnection({
- pc: peerConnection,
- peerId: '1',
- });
- console.log("Sender capabilities: ", RTCRtpSender.getCapabilities("video"));
- console.log("Receiver capabilities: ", RTCRtpReceiver.getCapabilities("video"));
- for (const track of localStream.getTracks()) {
- peerConnection.addTrack(track, localVideo.captureStream(30));
- }
- console.log(peerConnection.getTransceivers());
- peerConnection.getTransceivers()[0].setCodecPreferences(RTCRtpSender.getCapabilities("video").codecs.filter(item => item.mimeType.includes("VP8")))
- peerConnection.onicecandidate = gotIceCandidate;
- peerConnection.ontrack = gotRemoteStream;
- if (isCaller) {
- peerConnection.createOffer().then(createdDescription).catch(errorHandler);
- }
- }
- function gotMessageFromServer(message) {
- if (!peerConnection) start(false);
- const signal = JSON.parse(message.data);
- // Ignore messages from ourself
- if (signal.uuid == uuid) return;
- if (signal.sdp) {
- peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(() => {
- // Only create answers in response to offers
- if (signal.sdp.type !== 'offer') return;
- peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
- }).catch(errorHandler);
- } else if (signal.ice) {
- peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
- }
- }
- function gotIceCandidate(event) {
- if (event.candidate != null) {
- serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid}));
- }
- }
- function createdDescription(description) {
- console.log('got description');
- peerConnection.setLocalDescription(description).then(() => {
- serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid}));
- }).catch(errorHandler);
- }
- function gotRemoteStream(event) {
- console.log('got remote stream');
- remoteVideo.srcObject = event.streams[0];
- }
- function errorHandler(error) {
- console.log(error);
- }
- // Taken from http://stackoverflow.com/a/105074/515584
- // Strictly speaking, it's not a real UUID, but it gets the job done here
- function createUUID() {
- function s4() {
- return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
- }
- return `${s4() + s4()}-cond browser window then cli${s4()}-${s4()}-${s4()}-${s4() + s4() + s4()}`;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement