May 25, 2021 Electron
desktopCapturer
module can be used
to obtain available resources, which
getUserMedia
// 在渲染进程中.
var desktopCapturer = require('electron').desktopCapturer;
desktopCapturer.getSources({types: ['window', 'screen']}, function(error, sources) {
if (error) throw error;
for (var i = 0; i < sources.length; ++i) {
if (sources[i].name == "Electron") {
navigator.webkitGetUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sources[i].id,
minWidth: 1280,
maxWidth: 1280,
minHeight: 720,
maxHeight: 720
}
}
}, gotStream, getUserMediaError);
return;
}
}
});
function gotStream(stream) {
document.querySelector('video').src = URL.createObjectURL(stream);
}
function getUserMediaError(e) {
console.log('getUserMediaError');
}
When
navigator.webkitGetUserMedia
is called, a constraint object is created,
desktopCapturer
you must
chromeMediaSource
"desktop"
audio
to
false
.
If you want to capture audio
and video
across the desktop, you can set
chromeMediaSource
to
"screen"
and
audio
to
true
.
When using this method, you
can't specify
chromeMediaSourceId
.
desktopCapturer
has the following approach:
desktopCapturer.getSources(options, callback)
options
Object
types
Array - An array of Strings that lists
the
types of desktop resources that can be captured, available types are
screen
and
window
.
thumbnailSize
Object (optional) - Suggested acronym
scalable size, default to s
{width: 150, height: 150}
.
callback
Function
Make a request, get all the desktop
resources, and
callback(error, sources)
callback
.
sources
is
an array
Source
Source objects, each
Source
a captured screen or individual window, with the following properties:
id
String
- the id of the capture window or screen
navigator.webkitGetUserMedia
The
format
window:XX
screen:XX
XX
is a random number.
name
String - The description name of the capture window or screen . I
f the resource is
a
screen, the name is Entire
Entire Screen
or
Screen <index>
.
If the resource is a window, the name is the title of the window.
thumbnail
NativeImage
- Thumbnail.
Note:
There
is no guarantee that
source.thumbnail
size
and
thumnbailSize
options
will always be the same.
It also depends on the scale of the screen or window.