2015-10-04 01:35:00 +00:00
|
|
|
# desktop-capturer
|
|
|
|
|
|
|
|
The `desktop-capturer` is a renderer module used to capture the content of
|
|
|
|
screen and individual app windows.
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
// In the renderer process.
|
|
|
|
var desktopCapturer = require('desktop-capturer');
|
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
desktopCapturer.on('source-added', function(id, name) {
|
|
|
|
console.log("source " + name + " is added.");
|
|
|
|
// navigator.webkitGetUserMedia is not ready for use now.
|
2015-10-04 01:35:00 +00:00
|
|
|
});
|
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
desktopCapturer.on('source-thumbnail-changed', function(id, name, thumbnail) {
|
|
|
|
if (name == "Electron") {
|
2015-10-04 01:35:00 +00:00
|
|
|
// stopUpdating since we have found the window that we want to capture.
|
|
|
|
desktopCapturer.stopUpdating();
|
|
|
|
|
|
|
|
// It's ready to use webkitGetUserMedia right now.
|
|
|
|
navigator.webkitGetUserMedia({
|
|
|
|
audio: false,
|
|
|
|
video: {
|
|
|
|
mandatory: {
|
|
|
|
chromeMediaSource: 'desktop',
|
2015-10-05 03:32:12 +00:00
|
|
|
chromeMediaSourceId: id,
|
2015-10-04 01:35:00 +00:00
|
|
|
minWidth: 1280,
|
|
|
|
maxWidth: 1280,
|
|
|
|
minHeight: 720,
|
|
|
|
maxHeight: 720
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, gotStream, getUserMediaError);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Let's start updating after setting all event of `desktopCapturer`
|
|
|
|
desktopCapturer.startUpdating();
|
|
|
|
|
|
|
|
function gotStream(stream) {
|
|
|
|
document.querySelector('video').src = URL.createObjectURL(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUserMediaError(e) {
|
|
|
|
console.log('getUserMediaError');
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Events
|
|
|
|
|
|
|
|
### Event: 'source-added'
|
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
* `id` String - The id of the captured window or screen used in
|
|
|
|
`navigator.webkitGetUserMedia`. The format looks like 'window:XX' or
|
|
|
|
'screen:XX' where XX is a random generated number.
|
|
|
|
* `name` String - The descriped name of the capturing screen or window. If the
|
|
|
|
source is a screen, the name will be 'Entire Screen' or 'Screen <index>'; if
|
|
|
|
it is a window, the name will be the window's title.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
Emits when there is a new source added, usually a new window is created or a new
|
|
|
|
screen is attached.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
**Note:** `navigator.webkitGetUserMedia` is not ready for use in this event.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
|
|
|
### Event: 'source-removed'
|
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
* `id` String - The id of the captured window or screen used in
|
|
|
|
`navigator.webkitGetUserMedia`.
|
|
|
|
* `name` String - The descriped name of the capturing screen or window.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
|
|
|
Emits when there is a source removed.
|
|
|
|
|
|
|
|
### Event: 'source-name-changed'
|
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
* `id` String - The id of the captured window or screen used in
|
|
|
|
`navigator.webkitGetUserMedia`.
|
|
|
|
* `name` String - The descriped name of the capturing screen or window.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
|
|
|
Emits when the name of source is changed.
|
|
|
|
|
|
|
|
### Event: 'source-thumbnail-changed'
|
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
* `id` String - The id of the captured window or screen used in
|
|
|
|
`navigator.webkitGetUserMedia`.
|
|
|
|
* `name` String - The descriped name of the capturing screen or window.
|
|
|
|
* `thumbnail` [NativeImage](NativeImage.md) - A thumbnail image.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
|
|
|
Emits when the thumbnail of source is changed. `desktopCapturer` will refresh
|
|
|
|
all sources every second.
|
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
### Event: 'refresh-finished'
|
|
|
|
|
|
|
|
Emits when `desktopCapturer` finishes a refresh.
|
|
|
|
|
2015-10-04 01:35:00 +00:00
|
|
|
## Methods
|
|
|
|
|
|
|
|
The `desktopCapturer` module has the following methods:
|
|
|
|
|
|
|
|
### `desktopCapturer.startUpdating(options)`
|
|
|
|
|
2015-10-05 03:32:12 +00:00
|
|
|
`options` Object, properties:
|
|
|
|
|
|
|
|
* `types` Array - An array of String that enums the types of desktop sources.
|
2015-10-04 01:35:00 +00:00
|
|
|
* `screen` String - Screen
|
|
|
|
* `window` String - Individual window
|
2015-10-05 03:32:12 +00:00
|
|
|
* `updatePeriod` Integer (optional) - The update period in milliseconds. By
|
|
|
|
default, `desktopCapturer` updates every second.
|
|
|
|
* `thumbnailWidth` Integer (optional) - The width of thumbnail. By default, it
|
|
|
|
is 150px.
|
|
|
|
* `thumbnailHeight` Integer (optional) - The height of thumbnail. By default, it
|
|
|
|
is 150px.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
|
|
|
Starts updating desktopCapturer. The events of `desktopCapturer` will only be
|
|
|
|
emitted after `startUpdating` API is invoked.
|
|
|
|
|
|
|
|
**Note:** At beginning, the desktopCapturer is initially empty, so the
|
|
|
|
`source-added` event will be emitted for each existing source as it is
|
|
|
|
enumrated.
|
|
|
|
On Windows, you will see the screen ficker when desktopCapturer starts updating.
|
|
|
|
This is normal because the desktop effects(e.g. Aero) will be disabled when
|
|
|
|
desktop capturer is working. The ficker will disappear once
|
|
|
|
`desktopCapturer.stopUpdating()` is invoked.
|
|
|
|
|
|
|
|
### `desktopCapturer.stopUpdating()`
|
|
|
|
|
|
|
|
Stops updating desktopCapturer. The events of `desktopCapturer` will not be
|
|
|
|
emitted after the API is invoked.
|
|
|
|
|
|
|
|
**Note:** It is a good practice to call `stopUpdating` when you do not need
|
2015-10-05 03:32:12 +00:00
|
|
|
getting any infomation of sources, usually after passing a id to
|
2015-10-04 01:35:00 +00:00
|
|
|
`navigator.webkitGetUserMedia`.
|