-
Notifications
You must be signed in to change notification settings - Fork 21
/
video_stream.js
131 lines (110 loc) · 3.64 KB
/
video_stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { isSafari } from './helpers';
const videoStyles = {
display: 'flex',
};
class VideoStream extends Component {
stream = null;
streamWidth = 0;
streamHeight = 0;
video = null;
canvasContext = null;
async componentDidMount() {
let initSuccess = true;
let message = '';
try {
await this.startCamera();
} catch (e) {
message = `Browser camera init error: ${e}`;
initSuccess = false;
}
if (typeof this.props.onInit === 'function') {
this.props.onInit({ error: initSuccess, message }, this.drawFrame);
}
}
componentWillUnmount() {
this.stopCamera();
}
stopCamera = () => {
if (!this.stream) return;
this.stream.getTracks().map(t => t.stop());
this.stream = null;
this.streamWidth = 0;
this.streamHeight = 0;
this.canvasContext = null;
};
startCamera = async () => {
this.stopCamera();
if (!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia)) {
throw new Error('WebRTC API not supported in this browser');
}
const devices = await navigator.mediaDevices.enumerateDevices();
const cameras = devices.filter(device => device.kind === 'videoinput');
let videoMode = { facingMode: 'user' };
if (cameras.length > 1) {
const cameraIndex = this.props.rearCamera ? 1 : 0;
const cameraEnv = this.props.rearCamera ? 'environment' : 'user';
videoMode = isSafari() ? { facingMode: { exact: cameraEnv } } : { deviceId: cameras[cameraIndex].deviceId };
}
this.stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: videoMode,
});
if (this.video.srcObject !== undefined) {
this.video.srcObject = this.stream;
} else if (this.video.mozSrcObject !== undefined) {
this.video.mozSrcObject = this.stream;
} else if (window.URL.createObjectURL) {
this.video.src = window.URL.createObjectURL(this.stream);
} else if (window.webkitURL) {
this.video.src = window.webkitURL.createObjectURL(this.stream);
} else {
this.video.src = this.stream;
}
this.video.playsInline = true;
this.video.play(); // firefox does not emit `loadeddata` if video not playing
await this.streamLoadedPromise();
this.streamWidth = this.video.videoWidth;
this.streamHeight = this.video.videoHeight;
if (!this.canvasContext) {
const canvas = document.createElement('canvas');
canvas.width = this.streamWidth;
canvas.height = this.streamHeight;
this.canvasContext = canvas.getContext('2d');
}
};
streamLoadedPromise = () => new Promise((resolve, reject) => {
this.video.addEventListener('loadeddata', resolve, { once: true });
this.video.addEventListener('error', reject, { once: true });
});
captureFrame = () => {
this.canvasContext.drawImage(this.video, 0, 0, this.streamWidth, this.streamHeight);
return this.canvasContext.getImageData(0, 0, this.streamWidth, this.streamHeight);
}
drawFrame = () => {
window.requestAnimationFrame(() => {
if (!this.canvasContext) return;
const { data } = this.captureFrame();
this.props.onFrame({
data,
width: this.streamWidth,
height: this.streamHeight,
});
});
};
render() {
return <video style={{...videoStyles, ...this.props.style}} ref={v => (this.video = v)} />
}
};
VideoStream.propTypes = {
onInit: PropTypes.func.isRequired,
onFrame: PropTypes.func.isRequired,
style: PropTypes.object,
rearCamera: PropTypes.bool,
};
VideoStream.defaultProps = {
style: {},
rearCamera: true,
};
export default VideoStream;