@@ -20,7 +20,7 @@ export class AudioTrackConstraints {
2020 // eslint-disable-next-line require-jsdoc
2121 constructor ( source ) {
2222 if ( ! Object . values ( MediaFormatModule . AudioSourceInfo )
23- . some ( ( v ) => v === source ) ) {
23+ . some ( ( v ) => v === source ) ) {
2424 throw new TypeError ( 'Invalid source.' ) ;
2525 }
2626 /**
@@ -52,7 +52,7 @@ export class VideoTrackConstraints {
5252 // eslint-disable-next-line require-jsdoc
5353 constructor ( source ) {
5454 if ( ! Object . values ( MediaFormatModule . VideoSourceInfo )
55- . some ( ( v ) => v === source ) ) {
55+ . some ( ( v ) => v === source ) ) {
5656 throw new TypeError ( 'Invalid source.' ) ;
5757 }
5858 /**
@@ -123,7 +123,7 @@ export class StreamConstraints {
123123// eslint-disable-next-line require-jsdoc
124124function isVideoConstrainsForScreenCast ( constraints ) {
125125 return ( typeof constraints . video === 'object' && constraints . video . source ===
126- MediaFormatModule . VideoSourceInfo . SCREENCAST ) ;
126+ MediaFormatModule . VideoSourceInfo . SCREENCAST ) ;
127127}
128128
129129/**
@@ -147,146 +147,86 @@ export class MediaStreamFactory {
147147 */
148148 static createMediaStream ( constraints ) {
149149 if ( typeof constraints !== 'object' ||
150- ( ! constraints . audio && ! constraints . video ) ) {
150+ ( ! constraints . audio && ! constraints . video ) ) {
151151 return Promise . reject ( new TypeError ( 'Invalid constrains' ) ) ;
152152 }
153153 if ( ! isVideoConstrainsForScreenCast ( constraints ) &&
154- ( typeof constraints . audio === 'object' ) &&
155- constraints . audio . source ===
156- MediaFormatModule . AudioSourceInfo . SCREENCAST ) {
154+ ( typeof constraints . audio === 'object' ) &&
155+ constraints . audio . source ===
156+ MediaFormatModule . AudioSourceInfo . SCREENCAST ) {
157157 return Promise . reject (
158- new TypeError ( 'Cannot share screen without video.' ) ) ;
158+ new TypeError ( 'Cannot share screen without video.' ) ) ;
159159 }
160160 if ( isVideoConstrainsForScreenCast ( constraints ) && ! utils . isChrome ( ) &&
161- ! utils . isFirefox ( ) ) {
161+ ! utils . isFirefox ( ) ) {
162162 return Promise . reject (
163- new TypeError ( 'Screen sharing only supports Chrome and Firefox.' ) ) ;
163+ new TypeError ( 'Screen sharing only supports Chrome and Firefox.' ) ) ;
164164 }
165165 if ( isVideoConstrainsForScreenCast ( constraints ) &&
166- typeof constraints . audio === 'object' &&
167- constraints . audio . source !==
168- MediaFormatModule . AudioSourceInfo . SCREENCAST ) {
166+ typeof constraints . audio === 'object' &&
167+ constraints . audio . source !==
168+ MediaFormatModule . AudioSourceInfo . SCREENCAST ) {
169169 return Promise . reject ( new TypeError (
170- 'Cannot capture video from screen cast while capture audio from'
171- + ' other source.' ) ) ;
170+ 'Cannot capture video from screen cast while capture audio from'
171+ + ' other source.' ) ) ;
172172 }
173- // Screen sharing on Chrome does not work with the latest constraints
174- // format.
175- if ( isVideoConstrainsForScreenCast ( constraints ) && utils . isChrome ( ) ) {
176- if ( ! constraints . extensionId ) {
177- return Promise . reject ( new TypeError (
178- 'Extension ID must be specified for screen sharing on Chrome.' ) ) ;
179- }
180- const desktopCaptureSources = [ 'screen' , 'window' , 'tab' ] ;
181- if ( constraints . audio ) {
182- desktopCaptureSources . push ( 'audio' ) ;
173+
174+ // check and convert constraints
175+ if ( ! constraints . audio && ! constraints . video ) {
176+ return Promise . reject ( new TypeError (
177+ 'At least one of audio and video must be requested.' ) ) ;
178+ }
179+ const mediaConstraints = Object . create ( { } ) ;
180+ if ( typeof constraints . audio === 'object' &&
181+ constraints . audio . source === MediaFormatModule . AudioSourceInfo . MIC ) {
182+ mediaConstraints . audio = Object . create ( { } ) ;
183+ if ( utils . isEdge ( ) ) {
184+ mediaConstraints . audio . deviceId = constraints . audio . deviceId ;
185+ } else {
186+ mediaConstraints . audio . deviceId = {
187+ exact : constraints . audio . deviceId ,
188+ } ;
183189 }
184- return new Promise ( ( resolve , reject ) => {
185- chrome . runtime . sendMessage (
186- constraints . extensionId , {
187- getStream : desktopCaptureSources ,
188- } ,
189- function ( response ) {
190- if ( response === undefined ) {
191- return reject ( new Error ( chrome . runtime . lastError . message ) ) ;
192- }
193- if ( constraints . audio && typeof response . options !== 'object' ) {
194- Logger . warning (
195- 'Desktop sharing with audio requires the latest Chrome' +
196- ' extension. Your audio constraints will be ignored.' ) ;
197- }
198- const mediaConstraints = Object . create ( { } ) ;
199- if ( constraints . audio && ( typeof response . options === 'object' ) ) {
200- if ( response . options . canRequestAudioTrack ) {
201- mediaConstraints . audio = {
202- mandatory : {
203- chromeMediaSource : 'desktop' ,
204- chromeMediaSourceId : response . streamId ,
205- } ,
206- } ;
207- } else {
208- Logger . warning (
209- 'Sharing screen with audio was not selected by user.' ) ;
210- }
211- }
212- mediaConstraints . video = Object . create ( { } ) ;
213- mediaConstraints . video . mandatory = Object . create ( { } ) ;
214- mediaConstraints . video . mandatory . chromeMediaSource = 'desktop' ;
215- mediaConstraints . video . mandatory . chromeMediaSourceId =
216- response . streamId ;
217- // Transform new constraint format to the old style. Because
218- // chromeMediaSource only supported in the old style, and mix new
219- // and old style will result type error: "Cannot use both
220- // optional/mandatory and specific or advanced constraints.".
221- if ( constraints . video . resolution ) {
222- mediaConstraints . video . mandatory . maxHeight =
223- mediaConstraints . video . mandatory . minHeight =
224- constraints . video . resolution . height ;
225- mediaConstraints . video . mandatory . maxWidth =
226- mediaConstraints . video . mandatory . minWidth =
227- constraints . video . resolution . width ;
228- }
229- if ( constraints . video . frameRate ) {
230- mediaConstraints . video . mandatory . minFrameRate =
231- constraints . video . frameRate ;
232- mediaConstraints . video . mandatory . maxFrameRate =
233- constraints . video . frameRate ;
234- }
235- resolve ( navigator . mediaDevices . getUserMedia ( mediaConstraints ) ) ;
236- } ) ;
237- } ) ;
238190 } else {
239- if ( ! constraints . audio && ! constraints . video ) {
240- return Promise . reject ( new TypeError (
241- 'At least one of audio and video must be requested.' ) ) ;
191+ mediaConstraints . audio = constraints . audio ;
192+ }
193+ if ( typeof constraints . audio === 'object' &&
194+ constraints . audio . source ===
195+ MediaFormatModule . AudioSourceInfo . SCREENCAST ) {
196+ Logger . warning (
197+ 'Screen sharing with audio is not supported in Firefox.' ) ;
198+ mediaConstraints . audio = false ;
199+ }
200+ if ( typeof constraints . video === 'object' ) {
201+ mediaConstraints . video = Object . create ( { } ) ;
202+ if ( typeof constraints . video . frameRate === 'number' ) {
203+ mediaConstraints . video . frameRate = constraints . video . frameRate ;
242204 }
243- const mediaConstraints = Object . create ( { } ) ;
244- if ( typeof constraints . audio === 'object' &&
245- constraints . audio . source === MediaFormatModule . AudioSourceInfo . MIC ) {
246- mediaConstraints . audio = Object . create ( { } ) ;
247- if ( utils . isEdge ( ) ) {
248- mediaConstraints . audio . deviceId = constraints . audio . deviceId ;
249- } else {
250- mediaConstraints . audio . deviceId = {
251- exact : constraints . audio . deviceId ,
252- } ;
253- }
254- } else {
255- mediaConstraints . audio = constraints . audio ;
205+ if ( constraints . video . resolution &&
206+ constraints . video . resolution . width &&
207+ constraints . video . resolution . height ) {
208+ mediaConstraints . video . width = Object . create ( { } ) ;
209+ mediaConstraints . video . width . exact =
210+ constraints . video . resolution . width ;
211+ mediaConstraints . video . height = Object . create ( { } ) ;
212+ mediaConstraints . video . height . exact =
213+ constraints . video . resolution . height ;
256214 }
257- if ( typeof constraints . audio === 'object' &&
258- constraints . audio . source ===
259- MediaFormatModule . AudioSourceInfo . SCREENCAST ) {
260- Logger . warning (
261- 'Screen sharing with audio is not supported in Firefox.' ) ;
262- mediaConstraints . audio = false ;
215+ if ( typeof constraints . video . deviceId === 'string' ) {
216+ mediaConstraints . video . deviceId = { exact : constraints . video . deviceId } ;
263217 }
264- if ( typeof constraints . video === 'object' ) {
265- mediaConstraints . video = Object . create ( { } ) ;
266- if ( typeof constraints . video . frameRate === 'number' ) {
267- mediaConstraints . video . frameRate = constraints . video . frameRate ;
268- }
269- if ( constraints . video . resolution &&
270- constraints . video . resolution . width &&
271- constraints . video . resolution . height ) {
272- mediaConstraints . video . width = Object . create ( { } ) ;
273- mediaConstraints . video . width . exact =
274- constraints . video . resolution . width ;
275- mediaConstraints . video . height = Object . create ( { } ) ;
276- mediaConstraints . video . height . exact =
277- constraints . video . resolution . height ;
278- }
279- if ( typeof constraints . video . deviceId === 'string' ) {
280- mediaConstraints . video . deviceId = { exact : constraints . video . deviceId } ;
281- }
282- if ( utils . isFirefox ( ) &&
283- constraints . video . source ===
284- MediaFormatModule . VideoSourceInfo . SCREENCAST ) {
285- mediaConstraints . video . mediaSource = 'screen' ;
286- }
287- } else {
288- mediaConstraints . video = constraints . video ;
218+ if ( utils . isFirefox ( ) &&
219+ constraints . video . source ===
220+ MediaFormatModule . VideoSourceInfo . SCREENCAST ) {
221+ mediaConstraints . video . mediaSource = 'screen' ;
289222 }
223+ } else {
224+ mediaConstraints . video = constraints . video ;
225+ }
226+
227+ if ( isVideoConstrainsForScreenCast ( constraints ) ) {
228+ return navigator . mediaDevices . getDisplayMedia ( mediaConstraints ) ;
229+ } else {
290230 return navigator . mediaDevices . getUserMedia ( mediaConstraints ) ;
291231 }
292232 }
0 commit comments