Skip to main content

desktopCapturer

Access information about media sources that can be used to capture audio and video from the desktop using the navigator.mediaDevices.getUserMedia API.使用navigator.mediaDevices.getUserMedia访问可用于从桌面捕获音频和视频的媒体源的信息。

Process: 进程:Main

The following example shows how to capture video from a desktop window whose title is Electron:以下示例显示如何从标题为Electron的桌面窗口捕获视频:

// In the main process.
const { desktopCapturer } = require('electron')

desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
for (const source of sources) {
if (source.name === 'Electron') {
mainWindow.webContents.send('SET_SOURCE', source.id)
return
}
}
})
// In the preload script.
const { ipcRenderer } = require('electron')

ipcRenderer.on('SET_SOURCE', async (event, sourceId) => {
try {
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sourceId,
minWidth: 1280,
maxWidth: 1280,
minHeight: 720,
maxHeight: 720
}
}
})
handleStream(stream)
} catch (e) {
handleError(e)
}
})

function handleStream (stream) {
const video = document.querySelector('video')
video.srcObject = stream
video.onloadedmetadata = (e) => video.play()
}

function handleError (e) {
console.log(e)
}

To capture video from a source provided by desktopCapturer the constraints passed to navigator.mediaDevices.getUserMedia must include chromeMediaSource: 'desktop', and audio: false.要从desktopCapturer提供的源捕获视频,传递给navigator.mediaDevices.getUserMedia的约束必须包括chromeMediaSource: 'desktop'audio: false

To capture both audio and video from the entire desktop the constraints passed to navigator.mediaDevices.getUserMedia must include chromeMediaSource: 'desktop', for both audio and video, but should not include a chromeMediaSourceId constraint.要从整个桌面捕获音频和视频,传递给navigator.mediaDevices.getUserMedia的约束必须包括audiovideochromeMediaSource: 'desktop',但不应包括chromeMediaSourceId约束。

const constraints = {
audio: {
mandatory: {
chromeMediaSource: 'desktop'
}
},
video: {
mandatory: {
chromeMediaSource: 'desktop'
}
}
}

Methods方法

The desktopCapturer module has the following methods:desktopCapturer模块具有以下方法:

desktopCapturer.getSources(options)

  • options Object
    • types string[] - An array of strings that lists the types of desktop sources to be captured, available types are screen and window.一个字符串数组,列出了要捕获的桌面源的类型,可用类型为screenwindow
    • thumbnailSize Size (optional) - The size that the media source thumbnail should be scaled to. 媒体源缩略图应缩放到的大小。Default is 150 x 150. 默认值为150 x 150Set width or height to 0 when you do not need the thumbnails. 如果不需要缩略图,请将宽度或高度设置为0。This will save the processing time required for capturing the content of each window and screen.这将节省捕获每个窗口和屏幕的内容所需的处理时间。
    • fetchWindowIcons boolean (optional) - Set to true to enable fetching window icons. The default value is false. 设置为true可启用获取窗口图标。默认值为falseWhen false the appIcon property of the sources return null. Same if a source has the type screen.当为false时,源的appIcon属性将返回null。如果源具有类型屏幕,则相同。

Returns 返回Promise<DesktopCapturerSource[]> - Resolves with an array of DesktopCapturerSource objects, each DesktopCapturerSource represents a screen or an individual window that can be captured.使用DesktopCapturerSource对象数组进行解析,每个DesktopCapurerSource代表一个可以捕获的屏幕或单个窗口。

Note Capturing the screen contents requires user consent on macOS 10.15 Catalina or higher, which can detected by systemPreferences.getMediaAccessStatus.捕获屏幕内容需要在macOS 10.15 Catalina或更高版本上获得用户同意,这可以通过systemPreferences.getMediaAccessStatus检测到。

Caveats注意事项

navigator.mediaDevices.getUserMedia does not work on macOS for audio capture due to a fundamental limitation whereby apps that want to access the system's audio require a signed kernel extension. navigator.mediaDevices.getUserMedia无法在macOS上进行音频捕获,因为有一个基本限制,即想要访问系统音频的应用程序需要签名的内核扩展Chromium, and by extension Electron, does not provide this.铬,以及延伸的Electron,并不能提供这一点。

It is possible to circumvent this limitation by capturing system audio with another macOS app like Soundflower and passing it through a virtual audio input device. 可以通过使用另一个macOS应用程序(如Soundflower)捕获系统音频并将其通过虚拟音频输入设备来绕过这一限制。This virtual device can then be queried with navigator.mediaDevices.getUserMedia.然后可以使用navigator.mediaDevices.getUserMedia查询此虚拟设备。