2015-12-08 04:58:39 +00:00
|
|
|
{ipcMain} = require 'electron'
|
|
|
|
{desktopCapturer} = process.atomBinding 'desktop_capturer'
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-12-08 04:58:39 +00:00
|
|
|
deepEqual = (opt1, opt2) ->
|
2015-10-19 10:07:35 +00:00
|
|
|
return JSON.stringify(opt1) is JSON.stringify(opt2)
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-10-06 06:34:54 +00:00
|
|
|
# A queue for holding all requests from renderer process.
|
|
|
|
requestsQueue = []
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-12-08 05:49:26 +00:00
|
|
|
ipcMain.on 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, captureWindow, captureScreen, thumbnailSize, id) ->
|
|
|
|
request = id: id, options: {captureWindow, captureScreen, thumbnailSize}, webContents: event.sender
|
2015-10-06 06:34:54 +00:00
|
|
|
requestsQueue.push request
|
2015-12-08 05:49:26 +00:00
|
|
|
desktopCapturer.startHandling captureWindow, captureScreen, thumbnailSize if requestsQueue.length is 1
|
2015-10-06 06:34:54 +00:00
|
|
|
# If the WebContents is destroyed before receiving result, just remove the
|
|
|
|
# reference from requestsQueue to make the module not send the result to it.
|
2015-12-08 05:49:26 +00:00
|
|
|
event.sender.once 'destroyed', ->
|
2015-10-06 06:34:54 +00:00
|
|
|
request.webContents = null
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-12-08 05:49:26 +00:00
|
|
|
desktopCapturer.emit = (event, name, sources) ->
|
2015-10-06 06:34:54 +00:00
|
|
|
# Receiving sources result from main process, now send them back to renderer.
|
|
|
|
handledRequest = requestsQueue.shift 0
|
|
|
|
result = ({ id: source.id, name: source.name, thumbnail: source.thumbnail.toDataUrl() } for source in sources)
|
2015-12-08 05:49:26 +00:00
|
|
|
handledRequest.webContents?.send "ATOM_RENDERER_DESKTOP_CAPTURER_RESULT_#{handledRequest.id}", result
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2015-10-06 06:34:54 +00:00
|
|
|
# Check the queue to see whether there is other same request. If has, handle
|
|
|
|
# it for reducing redunplicated `desktopCaptuer.startHandling` calls.
|
|
|
|
unhandledRequestsQueue = []
|
|
|
|
for request in requestsQueue
|
2015-12-08 04:58:39 +00:00
|
|
|
if deepEqual handledRequest.options, request.options
|
2015-12-08 05:49:26 +00:00
|
|
|
request.webContents?.send "ATOM_RENDERER_DESKTOP_CAPTURER_RESULT_#{request.id}", errorMessage, result
|
2015-10-06 06:34:54 +00:00
|
|
|
else
|
|
|
|
unhandledRequestsQueue.push request
|
|
|
|
requestsQueue = unhandledRequestsQueue
|
|
|
|
# If the requestsQueue is not empty, start a new request handling.
|
|
|
|
if requestsQueue.length > 0
|
2015-12-08 05:49:26 +00:00
|
|
|
{captureWindow, captureScreen, thumbnailSize} = requestsQueue[0].options
|
|
|
|
desktopCapturer.startHandling captureWindow, captureScreen, thumbnailSize
|