From 353f5b35df7eb9e917a1835cfdaca89c14652682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 25 Dec 2020 15:54:13 +0100 Subject: [PATCH] Fix screen-sharing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/preload.js | 145 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 144 insertions(+), 1 deletion(-) diff --git a/src/preload.js b/src/preload.js index 0862ec6..85328a1 100644 --- a/src/preload.js +++ b/src/preload.js @@ -14,7 +14,150 @@ See the License for the specific language governing permissions and limitations under the License. */ -const { ipcRenderer } = require('electron'); +const { ipcRenderer, desktopCapturer } = require('electron'); // expose ipcRenderer to the renderer process window.ipcRenderer = ipcRenderer; + +// This is a fix for screen-sharing in Electron +window.navigator.mediaDevices.getDisplayMedia = () => { + return new Promise(async (resolve, reject) => { + try { + const sources = await desktopCapturer.getSources({ + types: ["screen", "window"], + }); + const screens = sources.filter((source) => { + return source.id.startsWith("screen"); + }); + const windows = sources.filter((source) => { + return source.id.startsWith("window"); + }); + console.log(screens); + console.log(windows); + + const selectionElem = document.createElement("div"); + selectionElem.classList = "desktop-capturer-selection"; + selectionElem.innerHTML = ` +
+
    + ${ + sources.map(({ + id, + name, + thumbnail, + displayId, + appIcon, + }) => ` +
  • + +
  • + `).join("") + } +
+
+ + + `; + document.body.appendChild(selectionElem); + + document.querySelectorAll(".desktop-capturer-selection-button").forEach((button) => { + button.addEventListener("click", async () => { + console.log("Click"); + try { + const id = button.getAttribute("data-id"); + const source = sources.find((source) => source.id === id); + if (! source) { + throw new Error(`Source with id ${id} does not exist`); + } + + const stream = await window.navigator.mediaDevices.getUserMedia({ + audio: false, + video: { + mandatory: { + chromeMediaSource: "desktop", + chromeMediaSourceId: source.id, + }, + }, + }); + resolve(stream); + + selectionElem.remove(); + } catch (err) { + console.error("Error selecting desktop capture source:", err); + reject(err); + } + }); + }); + } catch (err) { + console.error("Error displaying desktop capture sources:", err); + reject(err); + } + }); +};