electron/lib/browser/desktop-capturer.ts

63 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
const { createDesktopCapturer } = process.electronBinding('desktop_capturer');
2020-03-20 20:28:31 +00:00
const deepEqual = (a: ElectronInternal.GetSourcesOptions, b: ElectronInternal.GetSourcesOptions) => JSON.stringify(a) === JSON.stringify(b);
let currentlyRunning: {
options: ElectronInternal.GetSourcesOptions;
getSources: Promise<ElectronInternal.GetSourcesResult[]>;
2020-03-20 20:28:31 +00:00
}[] = [];
export const getSources = (event: Electron.IpcMainEvent, options: ElectronInternal.GetSourcesOptions) => {
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;
}
}
const getSources = new Promise<ElectronInternal.GetSourcesResult[]>((resolve, reject) => {
2020-03-20 20:28:31 +00:00
let capturer: ElectronInternal.DesktopCapturer | null = createDesktopCapturer();
const stopRunning = () => {
if (capturer) {
2020-03-20 20:28:31 +00:00
delete capturer._onerror;
delete capturer._onfinished;
capturer = null;
}
// Remove from currentlyRunning once we resolve or reject
2020-03-20 20:28:31 +00:00
currentlyRunning = currentlyRunning.filter(running => running.options !== options);
};
capturer._onerror = (error: string) => {
2020-03-20 20:28:31 +00:00
stopRunning();
reject(error);
};
capturer._onfinished = (sources: Electron.DesktopCapturerSource[], fetchWindowIcons: boolean) => {
2020-03-20 20:28:31 +00:00
stopRunning();
resolve(sources.map(source => ({
id: source.id,
name: source.name,
thumbnail: source.thumbnail.toDataURL(),
display_id: source.display_id,
appIcon: (fetchWindowIcons && source.appIcon) ? source.appIcon.toDataURL() : null
2020-03-20 20:28:31 +00:00
})));
};
2020-03-20 20:28:31 +00:00
capturer.startHandling(options.captureWindow, options.captureScreen, options.thumbnailSize, options.fetchWindowIcons);
// 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-03-20 20:28:31 +00:00
event.sender.once('destroyed', () => stopRunning());
});
currentlyRunning.push({
options,
getSources
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
return getSources;
};