desktop-capturer.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict'
  2. const {ipcMain} = require('electron')
  3. const {desktopCapturer} = process.atomBinding('desktop_capturer')
  4. const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
  5. // A queue for holding all requests from renderer process.
  6. let requestsQueue = []
  7. const electronSources = 'ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES'
  8. const capturerResult = (id) => `ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`
  9. ipcMain.on(electronSources, (event, captureWindow, captureScreen, thumbnailSize, id) => {
  10. const request = {
  11. id,
  12. options: {
  13. captureWindow,
  14. captureScreen,
  15. thumbnailSize
  16. },
  17. webContents: event.sender
  18. }
  19. requestsQueue.push(request)
  20. if (requestsQueue.length === 1) {
  21. desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize)
  22. }
  23. // If the WebContents is destroyed before receiving result, just remove the
  24. // reference from requestsQueue to make the module not send the result to it.
  25. event.sender.once('destroyed', () => {
  26. request.webContents = null
  27. })
  28. })
  29. desktopCapturer.emit = (event, name, sources) => {
  30. // Receiving sources result from main process, now send them back to renderer.
  31. const handledRequest = requestsQueue.shift()
  32. const handledWebContents = handledRequest.webContents
  33. const unhandledRequestsQueue = []
  34. const result = sources.map(source => {
  35. return {
  36. id: source.id,
  37. name: source.name,
  38. thumbnail: source.thumbnail.toDataURL(),
  39. display_id: source.display_id
  40. }
  41. })
  42. if (handledWebContents) {
  43. handledWebContents.send(capturerResult(handledRequest.id), result)
  44. }
  45. // Check the queue to see whether there is another identical request & handle
  46. requestsQueue.forEach(request => {
  47. const webContents = request.webContents
  48. if (deepEqual(handledRequest.options, request.options)) {
  49. if (webContents) webContents.send(capturerResult(request.id), result)
  50. } else {
  51. unhandledRequestsQueue.push(request)
  52. }
  53. })
  54. requestsQueue = unhandledRequestsQueue
  55. // If the requestsQueue is not empty, start a new request handling.
  56. if (requestsQueue.length > 0) {
  57. const {captureWindow, captureScreen, thumbnailSize} = requestsQueue[0].options
  58. return desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize)
  59. }
  60. }