From 3ab6fc97e92993cc29f4cb341abf8e3fbdd195e7 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Mon, 20 Jun 2022 20:03:55 +0200 Subject: [PATCH] Switch direction of link sharing --- package.json | 28 +++++++++---------- public/index.html | 6 ++-- public/main.mjs | 71 ++++++++++++++++++++++++++++++----------------- 3 files changed, 62 insertions(+), 43 deletions(-) diff --git a/package.json b/package.json index 233f79b..3049e71 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,16 @@ { - "name": "ScreenSharingThing", - "packageManager": "yarn@3.1.1", - "scripts": { - "start": "ts-node src/index.ts" - }, - "devDependencies": { - "@types/node": "^16.11.11", - "ts-node": "^10.4.0", - "typescript": "^4.5.2" - }, - "dependencies": { - "express": "^4.17.1", - "peer": "^0.6.1" - } + "name": "ScreenSharingThing", + "packageManager": "yarn@3.1.1", + "scripts": { + "dev": "ts-node src/index.ts" + }, + "devDependencies": { + "@types/node": "^16.11.11", + "ts-node": "^10.4.0", + "typescript": "^4.5.2" + }, + "dependencies": { + "express": "^4.17.1", + "peer": "^0.6.1" + } } diff --git a/public/index.html b/public/index.html index 31adbac..b5d5108 100644 --- a/public/index.html +++ b/public/index.html @@ -26,14 +26,14 @@

Your ID:

Connect URL:

+

Connecting to:

-
- + @@ -42,4 +42,4 @@ - + \ No newline at end of file diff --git a/public/main.mjs b/public/main.mjs index 6fbaf3c..f586c95 100644 --- a/public/main.mjs +++ b/public/main.mjs @@ -78,6 +78,7 @@ var peer = new Peer({ }); function bitrateTransform(sdp) { + // return sdp; var arr = sdp.split("\r\n"); arr.forEach((str, i) => { if (/^a=fmtp:\d*/.test(str)) { @@ -96,47 +97,65 @@ function bitrateTransform(sdp) { return res; } - let currentStream = undefined; + +let connections = []; + +function sendStream(id) { + const conn = peer.call(id, currentStream, { + sdpTransform: bitrateTransform, + }); + + conn.on("stream", console.log); + conn.on("close", console.log); + conn.on("error", console.log); +} + +if (!connectToId) { + sf.startStramBTN_click = () => { + navigator.mediaDevices + .getDisplayMedia({ + video: true, + }) + .then((stream) => { + if (currentStream) { + currentStream.getTracks().forEach((track) => track.stop()); + } + currentStream = stream; + let v = sf.localVideo; + v.srcObject = stream; + v.play(); + + connections.forEach((id) => { + sendStream(id); + }); + }); + }; +} + peer.on("open", (id) => { console.log("ID", id); sf.streamId_text = id; + let url = new URL(window.location.href); url.searchParams.set("id", id); sf.streamURL_text = url.href; sf.streamURL_href = url.href; - let con = peer.connect(connectToId); - con.on("data", console.log); - con.on("open", () => con.send("Hello")); - if (connectToId) { - sf.startStramBTN_click = () => { - navigator.mediaDevices - .getDisplayMedia({ - video: true, - }) - .then((stream) => { - if(currentStream) { - currentStream.getTracks().forEach(track => track.stop()) - } - currentStream = stream; - let v = sf.localVideo; - v.srcObject = stream; - v.play(); - const conn = peer.call(connectToId, stream, { - sdpTransform: bitrateTransform, - }); - conn.on("stream", console.log); - conn.on("close", console.log); - conn.on("error", console.log); - }); - }; + let con = peer.connect(connectToId); + con.on("data", console.log); + con.on("open", () => con.send("Hello")); } }); + peer.on("connection", (conn) => { console.log("connection", conn); conn.on("data", console.log); + connections.push(conn.peer); + if (currentStream) { + sendStream(conn.peer); + } }); peer.on("error", console.error);