2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-21 18:09:29 +00:00
|
|
|
|
2019-04-29 20:21:28 +00:00
|
|
|
const { createDesktopCapturer } = process.electronBinding('desktop_capturer')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-24 16:49:37 +00:00
|
|
|
const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-04-29 20:21:28 +00:00
|
|
|
let currentlyRunning = []
|
2017-10-24 16:49:37 +00:00
|
|
|
|
2019-05-01 13:07:57 +00:00
|
|
|
exports.getSources = (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons) => {
|
2019-04-29 20:21:28 +00:00
|
|
|
const options = {
|
|
|
|
captureWindow,
|
|
|
|
captureScreen,
|
|
|
|
thumbnailSize,
|
|
|
|
fetchWindowIcons
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const running of currentlyRunning) {
|
|
|
|
if (deepEqual(running.options, options)) {
|
|
|
|
// If a request is currently running for the same options
|
|
|
|
// return that promise
|
|
|
|
return running.getSources
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getSources = new Promise((resolve, reject) => {
|
|
|
|
const stopRunning = () => {
|
|
|
|
// Remove from currentlyRunning once we resolve or reject
|
|
|
|
currentlyRunning = currentlyRunning.filter(running => running.options !== options)
|
|
|
|
}
|
2019-03-07 23:31:25 +00:00
|
|
|
const request = {
|
2019-04-29 20:21:28 +00:00
|
|
|
options,
|
|
|
|
resolve: (value) => {
|
|
|
|
stopRunning()
|
|
|
|
resolve(value)
|
2019-03-07 23:31:25 +00:00
|
|
|
},
|
2019-04-29 20:21:28 +00:00
|
|
|
reject: (err) => {
|
|
|
|
stopRunning()
|
|
|
|
reject(err)
|
|
|
|
},
|
|
|
|
capturer: createDesktopCapturer()
|
2019-03-07 23:31:25 +00:00
|
|
|
}
|
2019-04-29 20:21:28 +00:00
|
|
|
request.capturer.emit = createCapturerEmitHandler(request.capturer, request)
|
|
|
|
request.capturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-03-07 23:31:25 +00:00
|
|
|
// If the WebContents is destroyed before receiving result, just remove the
|
2019-04-29 20:21:28 +00:00
|
|
|
// reference to resolve, emit and the capturer itself so that it never dispatches
|
|
|
|
// back to the renderer
|
2019-03-07 23:31:25 +00:00
|
|
|
event.sender.once('destroyed', () => {
|
|
|
|
request.resolve = null
|
2019-04-29 20:21:28 +00:00
|
|
|
delete request.capturer.emit
|
|
|
|
delete request.capturer
|
|
|
|
stopRunning()
|
2019-03-07 23:31:25 +00:00
|
|
|
})
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-04-29 20:21:28 +00:00
|
|
|
currentlyRunning.push({
|
|
|
|
options,
|
|
|
|
getSources
|
|
|
|
})
|
|
|
|
|
|
|
|
return getSources
|
2019-05-01 13:07:57 +00:00
|
|
|
}
|
2017-10-24 16:49:37 +00:00
|
|
|
|
2019-04-29 20:21:28 +00:00
|
|
|
const createCapturerEmitHandler = (capturer, request) => {
|
|
|
|
return function handlEmitOnCapturer (event, name, sources, fetchWindowIcons) {
|
|
|
|
// Ensure that this capturer instance can only ever receive a single event
|
|
|
|
// if we get more than one it is a bug but will also cause strange behavior
|
|
|
|
// if we still try to handle it
|
|
|
|
delete capturer.emit
|
2019-04-25 22:12:38 +00:00
|
|
|
|
2019-04-29 20:21:28 +00:00
|
|
|
if (name === 'error') {
|
|
|
|
const error = sources
|
|
|
|
request.reject(error)
|
|
|
|
return
|
2017-10-24 19:47:09 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-04-29 20:21:28 +00:00
|
|
|
const result = sources.map(source => {
|
|
|
|
return {
|
|
|
|
id: source.id,
|
|
|
|
name: source.name,
|
|
|
|
thumbnail: source.thumbnail.toDataURL(),
|
|
|
|
display_id: source.display_id,
|
|
|
|
appIcon: (fetchWindowIcons && source.appIcon) ? source.appIcon.toDataURL() : null
|
2018-10-06 11:48:00 +00:00
|
|
|
}
|
2019-04-29 20:21:28 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-04-29 20:21:28 +00:00
|
|
|
if (request.resolve) {
|
|
|
|
request.resolve(result)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|