()
| 94 | |
| 95 | // Async function to start video stream. |
| 96 | async start(): Promise<void> { |
| 97 | if (this.webcamConfig.facingMode) { |
| 98 | util.assert( |
| 99 | (this.webcamConfig.facingMode === 'user') || |
| 100 | (this.webcamConfig.facingMode === 'environment'), |
| 101 | () => |
| 102 | `Invalid webcam facing mode: ${this.webcamConfig.facingMode}. ` + |
| 103 | `Please provide 'user' or 'environment'`); |
| 104 | } |
| 105 | |
| 106 | try { |
| 107 | this.stream = await navigator.mediaDevices.getUserMedia({ |
| 108 | video: { |
| 109 | deviceId: this.webcamConfig.deviceId, |
| 110 | facingMode: this.webcamConfig.facingMode ? |
| 111 | this.webcamConfig.facingMode : |
| 112 | 'user', |
| 113 | width: this.webcamVideoElement.width, |
| 114 | height: this.webcamVideoElement.height |
| 115 | } |
| 116 | }); |
| 117 | } catch (e) { |
| 118 | // Modify the error message but leave the stack trace intact |
| 119 | e.message = `Error thrown while initializing video stream: ${e.message}`; |
| 120 | throw e; |
| 121 | } |
| 122 | |
| 123 | if (!this.stream) { |
| 124 | throw new Error('Could not obtain video from webcam.'); |
| 125 | } |
| 126 | |
| 127 | // Older browsers may not have srcObject |
| 128 | try { |
| 129 | this.webcamVideoElement.srcObject = this.stream; |
| 130 | } catch (error) { |
| 131 | console.log(error); |
| 132 | this.webcamVideoElement.src = window.URL.createObjectURL( |
| 133 | this.stream as unknown as MediaSource); |
| 134 | } |
| 135 | // Start the webcam video stream |
| 136 | this.webcamVideoElement.play(); |
| 137 | |
| 138 | this.isClosed = false; |
| 139 | |
| 140 | return new Promise<void>(resolve => { |
| 141 | // Add event listener to make sure the webcam has been fully initialized. |
| 142 | this.webcamVideoElement.onloadedmetadata = () => { |
| 143 | resolve(); |
| 144 | }; |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | async next(): Promise<IteratorResult<Tensor3D>> { |
| 149 | if (this.isClosed) { |
no test coverage detected