2020-09-14 17:38:05 +00:00
|
|
|
const { createDesktopCapturer } = process._linkedBinding('electron_browser_desktop_capturer');
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const deepEqual = (a: ElectronInternal.GetSourcesOptions, b: ElectronInternal.GetSourcesOptions) => JSON.stringify(a) === JSON.stringify(b);
|
2019-06-15 10:44:18 +00:00
|
|
|
|
|
|
|
let currentlyRunning: {
|
|
|
|
options: ElectronInternal.GetSourcesOptions;
|
|
|
|
getSources: Promise<ElectronInternal.GetSourcesResult[]>;
|
2020-03-20 20:28:31 +00:00
|
|
|
}[] = [];
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2020-05-21 00:25:49 +00:00
|
|
|
// |options.types| can't be empty and must be an array
|
|
|
|
function isValid (options: Electron.SourcesOptions) {
|
|
|
|
const types = options ? options.types : undefined;
|
|
|
|
return Array.isArray(types);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getSourcesImpl = (event: Electron.IpcMainEvent | null, args: Electron.SourcesOptions) => {
|
|
|
|
if (!isValid(args)) throw new Error('Invalid options');
|
|
|
|
|
|
|
|
const captureWindow = args.types.includes('window');
|
|
|
|
const captureScreen = args.types.includes('screen');
|
|
|
|
|
|
|
|
const { thumbnailSize = { width: 150, height: 150 } } = args;
|
|
|
|
const { fetchWindowIcons = false } = args;
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
captureWindow,
|
|
|
|
captureScreen,
|
|
|
|
thumbnailSize,
|
|
|
|
fetchWindowIcons
|
|
|
|
};
|
|
|
|
|
2019-06-15 10:44:18 +00:00
|
|
|
for (const running of currentlyRunning) {
|
|
|
|
if (deepEqual(running.options, options)) {
|
|
|
|
// If a request is currently running for the same options
|
|
|
|
// return that promise
|
2020-03-20 20:28:31 +00:00
|
|
|
return running.getSources;
|
2019-06-15 10:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getSources = new Promise<ElectronInternal.GetSourcesResult[]>((resolve, reject) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let capturer: ElectronInternal.DesktopCapturer | null = createDesktopCapturer();
|
2019-09-17 16:48:31 +00:00
|
|
|
|
2019-06-15 10:44:18 +00:00
|
|
|
const stopRunning = () => {
|
2019-09-17 16:48:31 +00:00
|
|
|
if (capturer) {
|
2020-03-20 20:28:31 +00:00
|
|
|
delete capturer._onerror;
|
|
|
|
delete capturer._onfinished;
|
|
|
|
capturer = null;
|
2019-09-17 16:48:31 +00:00
|
|
|
}
|
2019-06-15 10:44:18 +00:00
|
|
|
// Remove from currentlyRunning once we resolve or reject
|
2020-03-20 20:28:31 +00:00
|
|
|
currentlyRunning = currentlyRunning.filter(running => running.options !== options);
|
2020-12-17 00:11:39 +00:00
|
|
|
if (event) {
|
|
|
|
event.sender.removeListener('destroyed', stopRunning);
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2020-03-19 18:35:11 +00:00
|
|
|
capturer._onerror = (error: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
stopRunning();
|
|
|
|
reject(error);
|
|
|
|
};
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2020-05-13 17:05:53 +00:00
|
|
|
capturer._onfinished = (sources: Electron.DesktopCapturerSource[]) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
stopRunning();
|
2020-05-13 17:05:53 +00:00
|
|
|
resolve(sources);
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2020-05-21 00:25:49 +00:00
|
|
|
capturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons);
|
2019-06-15 10:44:18 +00:00
|
|
|
|
|
|
|
// If the WebContents is destroyed before receiving result, just remove the
|
|
|
|
// reference to emit and the capturer itself so that it never dispatches
|
|
|
|
// back to the renderer
|
2020-05-21 00:25:49 +00:00
|
|
|
if (event) {
|
2020-12-17 00:11:39 +00:00
|
|
|
event.sender.once('destroyed', stopRunning);
|
2020-05-21 00:25:49 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-15 10:44:18 +00:00
|
|
|
|
|
|
|
currentlyRunning.push({
|
|
|
|
options,
|
|
|
|
getSources
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return getSources;
|
|
|
|
};
|