2015-12-08 05:09:36 +00:00
|
|
|
# desktopCapturer
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2016-07-25 10:49:25 +00:00
|
|
|
> Access information about media sources that can be used to capture audio and
|
|
|
|
> video from the desktop using the [`navigator.webkitGetUserMedia`] API.
|
|
|
|
|
2016-11-23 19:20:56 +00:00
|
|
|
Process: [Renderer](../glossary.md#renderer-process)
|
2016-11-01 23:35:31 +00:00
|
|
|
|
2016-07-25 10:49:25 +00:00
|
|
|
The following example shows how to capture video from a desktop window whose
|
|
|
|
title is `Electron`:
|
2015-10-04 01:35:00 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
// In the renderer process.
|
2016-07-26 01:39:25 +00:00
|
|
|
const {desktopCapturer} = require('electron')
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2016-05-04 17:59:02 +00:00
|
|
|
desktopCapturer.getSources({types: ['window', 'screen']}, (error, sources) => {
|
2016-07-26 01:39:25 +00:00
|
|
|
if (error) throw error
|
2016-05-10 17:15:09 +00:00
|
|
|
for (let i = 0; i < sources.length; ++i) {
|
2016-05-10 09:42:11 +00:00
|
|
|
if (sources[i].name === 'Electron') {
|
2015-10-06 06:34:54 +00:00
|
|
|
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
|
|
|
}
|
2016-07-26 01:39:25 +00:00
|
|
|
}, handleStream, handleError)
|
|
|
|
return
|
2015-10-06 06:34:54 +00:00
|
|
|
}
|
2015-10-04 01:35:00 +00:00
|
|
|
}
|
2016-07-26 01:39:25 +00:00
|
|
|
})
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2016-07-26 16:49:02 +00:00
|
|
|
function handleStream (stream) {
|
2016-07-26 01:39:25 +00:00
|
|
|
document.querySelector('video').src = URL.createObjectURL(stream)
|
2015-10-04 01:35:00 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 16:49:02 +00:00
|
|
|
function handleError (e) {
|
2016-07-26 01:39:25 +00:00
|
|
|
console.log(e)
|
2015-10-04 01:35:00 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-07-25 10:49:25 +00:00
|
|
|
To capture video from a source provided by `desktopCapturer` the constraints
|
|
|
|
passed to [`navigator.webkitGetUserMedia`] must include
|
|
|
|
`chromeMediaSource: 'desktop'`, and `audio: false`.
|
2015-12-15 22:07:55 +00:00
|
|
|
|
2016-07-25 10:49:25 +00:00
|
|
|
To capture both audio and video from the entire desktop the constraints passed
|
2016-07-26 04:57:39 +00:00
|
|
|
to [`navigator.webkitGetUserMedia`] must include `chromeMediaSource: 'screen'`,
|
2016-07-25 10:49:25 +00:00
|
|
|
and `audio: true`, but should not include a `chromeMediaSourceId` constraint.
|
2015-12-15 22:07:55 +00:00
|
|
|
|
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
|
2016-09-28 05:28:44 +00:00
|
|
|
* `types` String[] - An array of Strings that lists the types of desktop sources
|
2015-12-08 05:09:36 +00:00
|
|
|
to be captured, available types are `screen` and `window`.
|
2017-03-24 18:14:43 +00:00
|
|
|
* `thumbnailSize` Object (optional) - The size that the media source thumbnail should be scaled to.
|
|
|
|
* `width` Integer - Default is `150`
|
|
|
|
* `height` Integer - Default is `150`
|
2015-12-08 05:09:36 +00:00
|
|
|
* `callback` Function
|
2016-10-13 06:30:57 +00:00
|
|
|
* `error` Error
|
|
|
|
* `sources` [DesktopCapturerSource[]](structures/desktop-capturer-source.md)
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2016-07-25 10:49:25 +00:00
|
|
|
Starts gathering information about all available desktop media sources,
|
|
|
|
and calls `callback(error, sources)` when finished.
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2016-10-13 06:30:57 +00:00
|
|
|
`sources` is an array of [`DesktopCapturerSource`](structures/desktop-capturer-source.md)
|
2016-10-14 02:10:37 +00:00
|
|
|
objects, each `DesktopCapturerSource` represents a screen or an individual window that can be
|
2016-10-13 06:30:57 +00:00
|
|
|
captured.
|
2015-12-08 05:09:36 +00:00
|
|
|
|
2016-07-25 10:49:25 +00:00
|
|
|
[`navigator.webkitGetUserMedia`]: https://developer.mozilla.org/en/docs/Web/API/Navigator/getUserMedia
|