11const { desktopCapturer } = require ( 'electron' )
2+ const { ipcRenderer } = require ( 'electron' )
23
34// The following example shows how to capture video from
45// the screen. It also grabs each window, so you could
@@ -12,31 +13,49 @@ function startCapture () {
1213 for ( let i = 0 ; i < sources . length ; ++ i ) {
1314 console . log ( sources [ i ] )
1415 if ( sources [ i ] . id . startsWith ( 'screen' ) ) {
15- navigator . mediaDevices . getUserMedia ( {
16- audio : false ,
17- video : {
18- mandatory : {
19- chromeMediaSource : 'desktop' ,
20- chromeMediaSourceId : sources [ i ] . id ,
21- minWidth : 1280 ,
22- maxWidth : 1280 ,
23- minHeight : 720 ,
24- maxHeight : 720
25- }
26- }
27- } ) . then ( ( stream ) => handleStream ( stream ) )
28- . catch ( ( error ) => console . log ( error ) )
16+ showStream ( sources [ i ] . id )
2917 }
3018 }
3119 } )
3220}
3321
22+ async function showStream ( sourceId ) {
23+ try {
24+ const stream = await navigator . mediaDevices . getUserMedia ( {
25+ audio : false ,
26+ video : {
27+ mandatory : {
28+ chromeMediaSource : 'desktop' ,
29+ chromeMediaSourceId : sourceId ,
30+ minWidth : 1280 ,
31+ maxWidth : 1280 ,
32+ minHeight : 720 ,
33+ maxHeight : 720
34+ }
35+ }
36+ } )
37+ handleStream ( stream )
38+ } catch ( e ) {
39+ handleError ( e )
40+ }
41+ }
42+
3443function handleStream ( stream ) {
3544 const video = document . querySelector ( 'video' )
3645 video . srcObject = stream
3746 video . onloadedmetadata = ( e ) => video . play ( )
3847}
3948
40- window . addEventListener ( 'DOMContentLoaded' , ( ) => {
41- startCapture ( )
42- } )
49+ function handleError ( e ) {
50+ console . log ( e )
51+ }
52+
53+ if ( parseInt ( process . versions . electron ) >= 17 ) {
54+ ipcRenderer . on ( 'SET_SOURCE' , async ( event , sourceId ) => {
55+ showStream ( sourceId )
56+ } )
57+ } else {
58+ window . addEventListener ( 'DOMContentLoaded' , ( ) => {
59+ startCapture ( )
60+ } )
61+ }
0 commit comments