From c322de4412a3b6edb34cb0314e64537a4871fff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 25 Jun 2020 10:43:27 +0200 Subject: [PATCH] Moving to async/await --- front/src/WebRtc/MediaManager.ts | 42 +++++++++++++++----------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/front/src/WebRtc/MediaManager.ts b/front/src/WebRtc/MediaManager.ts index 50f4bf15..cdee2ae8 100644 --- a/front/src/WebRtc/MediaManager.ts +++ b/front/src/WebRtc/MediaManager.ts @@ -125,35 +125,33 @@ export class MediaManager { } //get camera - getCamera(): Promise { - let promise = null; - + async getCamera(): Promise { if (navigator.mediaDevices === undefined) { - return Promise.reject(new Error('Unable to access your camera or microphone. Your browser is too old (or you are running a development version of WorkAdventure on Firefox)')); + if (window.location.protocol === 'http:') { + throw new Error('Unable to access your camera or microphone. You need to use a HTTPS connection.'); + } else { + throw new Error('Unable to access your camera or microphone. Your browser is too old.'); + } } try { - promise = navigator.mediaDevices.getUserMedia(this.constraintsMedia) - .then((stream: MediaStream) => { - this.localStream = stream; - this.myCamVideo.srcObject = this.localStream; + let stream = await navigator.mediaDevices.getUserMedia(this.constraintsMedia); - //TODO resize remote cam - /*console.log(this.localStream.getTracks()); - let videoMediaStreamTrack = this.localStream.getTracks().find((media : MediaStreamTrack) => media.kind === "video"); - let {width, height} = videoMediaStreamTrack.getSettings(); - console.info(`${width}x${height}`); // 6*/ + this.localStream = stream; + this.myCamVideo.srcObject = this.localStream; - return stream; - }).catch((err) => { - console.info("error get media ", this.constraintsMedia.video, this.constraintsMedia.audio, err); - this.localStream = null; - throw err; - }); - } catch (e) { - promise = Promise.reject(e); + return stream; + + //TODO resize remote cam + /*console.log(this.localStream.getTracks()); + let videoMediaStreamTrack = this.localStream.getTracks().find((media : MediaStreamTrack) => media.kind === "video"); + let {width, height} = videoMediaStreamTrack.getSettings(); + console.info(`${width}x${height}`); // 6*/ + } catch (err) { + console.info("error get media ", this.constraintsMedia.video, this.constraintsMedia.audio, err); + this.localStream = null; + throw err; } - return promise; } setCamera(id: string): Promise {