2019-12-06 19:17:34 +01:00
|
|
|
/*
|
|
|
|
Copyright 2018, 2019 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-12-25 15:54:13 +01:00
|
|
|
const { ipcRenderer, desktopCapturer } = require('electron');
|
2019-12-06 19:17:34 +01:00
|
|
|
|
|
|
|
// expose ipcRenderer to the renderer process
|
|
|
|
window.ipcRenderer = ipcRenderer;
|
2020-12-25 15:54:13 +01:00
|
|
|
|
|
|
|
// This is a fix for screen-sharing in Electron
|
|
|
|
window.navigator.mediaDevices.getDisplayMedia = () => {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
2020-12-25 16:24:21 +01:00
|
|
|
// Get screen-sharing sources
|
2020-12-25 15:54:13 +01:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
2020-12-25 16:24:21 +01:00
|
|
|
const streamSelector = document.createElement("div");
|
|
|
|
streamSelector.classList = "stream-selector";
|
|
|
|
streamSelector.innerHTML = `
|
|
|
|
<h1>Screens</h1>
|
2020-12-25 15:54:13 +01:00
|
|
|
<div class="desktop-capturer-selection-scroller">
|
|
|
|
<ul class="desktop-capturer-selection-list">
|
|
|
|
${
|
2020-12-25 16:24:21 +01:00
|
|
|
screens.map(({
|
2020-12-25 15:54:13 +01:00
|
|
|
id,
|
|
|
|
name,
|
|
|
|
thumbnail,
|
|
|
|
displayId,
|
|
|
|
appIcon,
|
|
|
|
}) => `
|
|
|
|
<li class="desktop-capturer-selection-item">
|
|
|
|
<button class="desktop-capturer-selection-button" data-id="${id}" title="${name}">
|
2020-12-25 16:24:21 +01:00
|
|
|
<img class="desktop-capturer-selection-thumbnail" src="${thumbnail.toDataURL()}"/>
|
|
|
|
<span class="desktop-capturer-selection-name">${name}</span>
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
`).join("")
|
|
|
|
}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<h1>Windows</h1>
|
|
|
|
<div class="desktop-capturer-selection-scroller">
|
|
|
|
<ul class="desktop-capturer-selection-list">
|
|
|
|
${
|
|
|
|
windows.map(({
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
thumbnail,
|
|
|
|
displayId,
|
|
|
|
appIcon,
|
|
|
|
}) => `
|
|
|
|
<li class="desktop-capturer-selection-item">
|
|
|
|
<button class="desktop-capturer-selection-button" data-id="${id}" title="${name}">
|
|
|
|
<img class="desktop-capturer-selection-thumbnail" src="${thumbnail.toDataURL()}"/>
|
2020-12-25 15:54:13 +01:00
|
|
|
<span class="desktop-capturer-selection-name">${name}</span>
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
`).join("")
|
|
|
|
}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style>
|
2020-12-25 16:24:21 +01:00
|
|
|
.stream-selector {
|
2020-12-25 15:54:13 +01:00
|
|
|
position: fixed;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
width: 100%;
|
|
|
|
height: 100vh;
|
2020-12-25 16:24:21 +01:00
|
|
|
background: rgba(0,0,0,0.8);
|
2020-12-25 15:54:13 +01:00
|
|
|
color: #fff;
|
|
|
|
z-index: 10000000;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
2020-12-25 16:24:21 +01:00
|
|
|
flex-direction: column;
|
2020-12-25 15:54:13 +01:00
|
|
|
}
|
|
|
|
.desktop-capturer-selection-scroller {
|
|
|
|
width: 100%;
|
|
|
|
max-height: 100vh;
|
|
|
|
overflow-y: auto;
|
|
|
|
}
|
|
|
|
.desktop-capturer-selection-list {
|
|
|
|
max-width: calc(100% - 100px);
|
2020-12-25 16:24:21 +01:00
|
|
|
margin: 0 50px 0 50px;
|
2020-12-25 15:54:13 +01:00
|
|
|
padding: 0;
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
list-style: none;
|
|
|
|
overflow: hidden;
|
|
|
|
justify-content: center;
|
|
|
|
}
|
|
|
|
.desktop-capturer-selection-item {
|
|
|
|
display: flex;
|
|
|
|
margin: 4px;
|
|
|
|
}
|
|
|
|
.desktop-capturer-selection-button {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: stretch;
|
|
|
|
width: 145px;
|
|
|
|
margin: 0;
|
|
|
|
border: 0;
|
|
|
|
border-radius: 4px;
|
|
|
|
padding: 4px;
|
|
|
|
background: #20262b;
|
|
|
|
color: #ffffff;
|
|
|
|
text-align: left;
|
|
|
|
transition: background-color .15s, box-shadow .15s;
|
|
|
|
}
|
|
|
|
.desktop-capturer-selection-button:hover,
|
|
|
|
.desktop-capturer-selection-button:focus {
|
|
|
|
background: #363c43;
|
|
|
|
}
|
|
|
|
.desktop-capturer-selection-thumbnail {
|
|
|
|
width: 100%;
|
|
|
|
height: 81px;
|
|
|
|
object-fit: cover;
|
|
|
|
}
|
|
|
|
.desktop-capturer-selection-name {
|
|
|
|
margin: 6px 0 6px;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
`;
|
2020-12-25 16:24:21 +01:00
|
|
|
document.body.appendChild(streamSelector);
|
2020-12-25 15:54:13 +01:00
|
|
|
|
|
|
|
document.querySelectorAll(".desktop-capturer-selection-button").forEach((button) => {
|
2020-12-25 16:24:21 +01:00
|
|
|
button.addEventListener("click", async (ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2020-12-25 15:54:13 +01:00
|
|
|
try {
|
|
|
|
const id = button.getAttribute("data-id");
|
|
|
|
const source = sources.find((source) => source.id === id);
|
2020-12-25 16:24:21 +01:00
|
|
|
if (!source) {
|
|
|
|
streamSelector.remove();
|
|
|
|
reject(new Error(`Source with id ${id} does not exist`));
|
2020-12-25 15:54:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const stream = await window.navigator.mediaDevices.getUserMedia({
|
|
|
|
audio: false,
|
|
|
|
video: {
|
|
|
|
mandatory: {
|
|
|
|
chromeMediaSource: "desktop",
|
|
|
|
chromeMediaSourceId: source.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-25 16:24:21 +01:00
|
|
|
streamSelector.remove();
|
|
|
|
resolve(stream);
|
2020-12-25 15:54:13 +01:00
|
|
|
} catch (err) {
|
2020-12-25 16:24:21 +01:00
|
|
|
streamSelector.remove();
|
2020-12-25 15:54:13 +01:00
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-12-25 16:24:21 +01:00
|
|
|
|
|
|
|
const background = document.querySelector(".stream-selector");
|
|
|
|
background.addEventListener("click", async (ev) => {
|
|
|
|
streamSelector.remove();
|
|
|
|
reject(new Error("No source selected"));
|
|
|
|
});
|
2020-12-25 15:54:13 +01:00
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|