-
Notifications
You must be signed in to change notification settings - Fork 15k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make desktopCapturer main-process-only (#30720)
* feat: make desktopCapturer main-process-only * remove --enable-api-filtering-logging * remove test * merge lib/browser/api/desktop-capturer.ts with lib/browser/desktop-capturer.ts * remove desktop-capturer-get-sources event * fix specs * getSources needs to be async Co-authored-by: Milan Burda <milan.burda@gmail.com>
- Loading branch information
Showing
17 changed files
with
149 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,72 @@ | ||
import { getSourcesImpl } from '@electron/internal/browser/desktop-capturer'; | ||
const { createDesktopCapturer } = process._linkedBinding('electron_browser_desktop_capturer'); | ||
|
||
export async function getSources (options: Electron.SourcesOptions) { | ||
return getSourcesImpl(null, options); | ||
const deepEqual = (a: ElectronInternal.GetSourcesOptions, b: ElectronInternal.GetSourcesOptions) => JSON.stringify(a) === JSON.stringify(b); | ||
|
||
let currentlyRunning: { | ||
options: ElectronInternal.GetSourcesOptions; | ||
getSources: Promise<ElectronInternal.GetSourcesResult[]>; | ||
}[] = []; | ||
|
||
// |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 async function getSources (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 | ||
}; | ||
|
||
for (const running of currentlyRunning) { | ||
if (deepEqual(running.options, options)) { | ||
// If a request is currently running for the same options | ||
// return that promise | ||
return running.getSources; | ||
} | ||
} | ||
|
||
const getSources = new Promise<ElectronInternal.GetSourcesResult[]>((resolve, reject) => { | ||
let capturer: ElectronInternal.DesktopCapturer | null = createDesktopCapturer(); | ||
|
||
const stopRunning = () => { | ||
if (capturer) { | ||
delete capturer._onerror; | ||
delete capturer._onfinished; | ||
capturer = null; | ||
} | ||
// Remove from currentlyRunning once we resolve or reject | ||
currentlyRunning = currentlyRunning.filter(running => running.options !== options); | ||
}; | ||
|
||
capturer._onerror = (error: string) => { | ||
stopRunning(); | ||
reject(error); | ||
}; | ||
|
||
capturer._onfinished = (sources: Electron.DesktopCapturerSource[]) => { | ||
stopRunning(); | ||
resolve(sources); | ||
}; | ||
|
||
capturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons); | ||
}); | ||
|
||
currentlyRunning.push({ | ||
options, | ||
getSources | ||
}); | ||
|
||
return getSources; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
4fd7c2a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nornagon would it be possible that
getSources
returns also theposition
andsize
of each window whenwindow
is chosen?I currently rely on an external package
node-window-manager
to match window ids and find the position and size of each window, but it doesn't exist on Linux and it would be cleaner to not have to depend on another package.