2015-12-08 05:09:36 +00:00
|
|
|
# desktopCapturer
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-12-08 05:09:36 +00:00
|
|
|
The `desktopCapturer` module can be used to get available sources that can be
|
|
|
|
used to be captured with `getUserMedia`.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
// In the renderer process.
|
2015-12-08 05:09:36 +00:00
|
|
|
var desktopCapturer = require('electron').desktopCapturer;
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-10-19 02:54:12 +00:00
|
|
|
desktopCapturer.getSources({types: ['window', 'screen']}, function(error, sources) {
|
|
|
|
if (error) throw error;
|
2015-10-06 06:34:54 +00:00
|
|
|
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
|
|
|
|
}
|
2015-10-04 01:35:00 +00:00
|
|
|
}
|
2015-10-06 06:34:54 +00:00
|
|
|
}, gotStream, getUserMediaError);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-04 01:35:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function gotStream(stream) {
|
|
|
|
document.querySelector('video').src = URL.createObjectURL(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUserMediaError(e) {
|
|
|
|
console.log('getUserMediaError');
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-12-15 22:07:55 +00:00
|
|
|
When creating a constraints object for the `navigator.webkitGetUserMedia` call,
|
|
|
|
if you are using a source from `desktopCapturer` your `chromeMediaSource` must
|
|
|
|
be set to `"desktop"` and your `audio` must be set to `false`.
|
|
|
|
|
|
|
|
If you wish to
|
|
|
|
capture the audio and video from the entire desktop you can set
|
|
|
|
`chromeMediaSource` to `"screen"` and `audio` to `true`. When using this method
|
|
|
|
you cannot specify a `chromeMediaSourceId`.
|
|
|
|
|
2015-10-06 06:34:54 +00:00
|
|
|
## Methods
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-10-06 06:34:54 +00:00
|
|
|
The `desktopCapturer` module has the following methods:
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-10-06 06:34:54 +00:00
|
|
|
### `desktopCapturer.getSources(options, callback)`
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-12-08 05:09:36 +00:00
|
|
|
* `options` Object
|
|
|
|
* `types` Array - An array of String that lists the types of desktop sources
|
|
|
|
to be captured, available types are `screen` and `window`.
|
|
|
|
* `thumbnailSize` Object (optional) - The suggested size that thumbnail should
|
|
|
|
be scaled, it is `{width: 150, height: 150}` by default.
|
|
|
|
* `callback` Function
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-12-08 05:09:36 +00:00
|
|
|
Starts a request to get all desktop sources, `callback` will be called with
|
|
|
|
`callback(error, sources)` when the request is completed.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-12-08 05:09:36 +00:00
|
|
|
The `sources` is an array of `Source` objects, each `Source` represents a
|
|
|
|
captured screen or individual window, and has following properties:
|
2016-04-14 22:53:34 +00:00
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
* `id` String - The id of the captured window or screen used in
|
2015-12-08 05:09:36 +00:00
|
|
|
`navigator.webkitGetUserMedia`. The format looks like `window:XX` or
|
|
|
|
`screen:XX` where `XX` is a random generated number.
|
|
|
|
* `name` String - The described name of the capturing screen or window. If the
|
|
|
|
source is a screen, the name will be `Entire Screen` or `Screen <index>`; if
|
2015-10-06 06:34:54 +00:00
|
|
|
it is a window, the name will be the window's title.
|
2016-04-21 22:00:19 +00:00
|
|
|
* `thumbnail` A thumbnail [native image](native-image.md).
|
2015-12-08 05:09:36 +00:00
|
|
|
|
|
|
|
**Note:** There is no guarantee that the size of `source.thumbnail` is always
|
|
|
|
the same as the `thumnbailSize` in `options`. It also depends on the scale of
|
|
|
|
the screen or window.
|