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 要从整个桌面捕获音频和视频,传递给navigator.mediaDevices.getUserMedia的约束必须包括chromeMediaSource: 'desktop'
, for both audio
and video
, but should not include a chromeMediaSourceId
constraint.audio
和video
的chromeMediaSource: 'desktop'
,但不应包括chromeMediaSourceId
约束。
const constraints = {
audio: {
mandatory: {
chromeMediaSource: 'desktop'
}
},
video: {
mandatory: {
chromeMediaSource: 'desktop'
}
}
}
Methods方法
The desktopCapturer
module has the following methods:desktopCapturer
模块具有以下方法:
desktopCapturer.getSources(options)
Returns 返回Promise<DesktopCapturerSource[]>
- Resolves with an array of DesktopCapturerSource objects, each 使用DesktopCapturerSource对象数组进行解析,每个DesktopCapturerSource
represents a screen or an individual window that can be captured.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
查询此虚拟设备。