|
@@ -0,0 +1,176 @@
|
|
1
|
+// @flow
|
|
2
|
+
|
|
3
|
+import pixelmatch from 'pixelmatch';
|
|
4
|
+
|
|
5
|
+import {
|
|
6
|
+ CLEAR_INTERVAL,
|
|
7
|
+ INTERVAL_TIMEOUT,
|
|
8
|
+ PIXEL_LOWER_BOUND,
|
|
9
|
+ POLL_INTERVAL,
|
|
10
|
+ SET_INTERVAL
|
|
11
|
+} from './constants';
|
|
12
|
+
|
|
13
|
+import { getCurrentConference } from '../../base/conference';
|
|
14
|
+import { processScreenshot } from './processScreenshot';
|
|
15
|
+import { timerWorkerScript } from './worker';
|
|
16
|
+
|
|
17
|
+declare var interfaceConfig: Object;
|
|
18
|
+
|
|
19
|
+/**
|
|
20
|
+ * Effect that wraps {@code MediaStream} adding periodic screenshot captures.
|
|
21
|
+ * Manipulates the original desktop stream and performs custom processing operations, if implemented.
|
|
22
|
+ */
|
|
23
|
+export default class ScreenshotCaptureEffect {
|
|
24
|
+ _state: Object;
|
|
25
|
+ _currentCanvas: HTMLCanvasElement;
|
|
26
|
+ _currentCanvasContext: CanvasRenderingContext2D;
|
|
27
|
+ _videoElement: HTMLVideoElement;
|
|
28
|
+ _handleWorkerAction: Function;
|
|
29
|
+ _initScreenshotCapture: Function;
|
|
30
|
+ _streamWorker: Worker;
|
|
31
|
+ _streamHeight: any;
|
|
32
|
+ _streamWidth: any;
|
|
33
|
+ _storedImageData: Uint8ClampedArray;
|
|
34
|
+
|
|
35
|
+ /**
|
|
36
|
+ * Initializes a new {@code ScreenshotCaptureEffect} instance.
|
|
37
|
+ *
|
|
38
|
+ * @param {Object} state - The redux state.
|
|
39
|
+ */
|
|
40
|
+ constructor(state: Object) {
|
|
41
|
+ this._state = state;
|
|
42
|
+ this._currentCanvas = document.createElement('canvas');
|
|
43
|
+ this._currentCanvasContext = this._currentCanvas.getContext('2d');
|
|
44
|
+ this._videoElement = document.createElement('video');
|
|
45
|
+
|
|
46
|
+ // Bind handlers such that they access the same instance.
|
|
47
|
+ this._handleWorkerAction = this._handleWorkerAction.bind(this);
|
|
48
|
+ this._initScreenshotCapture = this._initScreenshotCapture.bind(this);
|
|
49
|
+ this._streamWorker = new Worker(timerWorkerScript);
|
|
50
|
+ this._streamWorker.onmessage = this._handleWorkerAction;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ /**
|
|
54
|
+ * Checks if the local track supports this effect.
|
|
55
|
+ *
|
|
56
|
+ * @param {JitsiLocalTrack} jitsiLocalTrack - Targeted local track.
|
|
57
|
+ * @returns {boolean} - Returns true if this effect can run on the specified track, false otherwise.
|
|
58
|
+ */
|
|
59
|
+ isEnabled(jitsiLocalTrack: Object) {
|
|
60
|
+ return (
|
|
61
|
+ interfaceConfig.ENABLE_SCREENSHOT_CAPTURE
|
|
62
|
+ && jitsiLocalTrack.isVideoTrack()
|
|
63
|
+ && jitsiLocalTrack.videoType === 'desktop'
|
|
64
|
+ );
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ /**
|
|
68
|
+ * Starts the screenshot capture event on a loop.
|
|
69
|
+ *
|
|
70
|
+ * @param {MediaStream} stream - The desktop stream from which screenshots are to be sent.
|
|
71
|
+ * @returns {MediaStream} - The same stream, with the interval set.
|
|
72
|
+ */
|
|
73
|
+ startEffect(stream: MediaStream) {
|
|
74
|
+ const desktopTrack = stream.getVideoTracks()[0];
|
|
75
|
+ const { height, width }
|
|
76
|
+ = desktopTrack.getSettings() ?? desktopTrack.getConstraints();
|
|
77
|
+
|
|
78
|
+ this._streamHeight = height;
|
|
79
|
+ this._streamWidth = width;
|
|
80
|
+ this._currentCanvas.height = parseInt(height, 10);
|
|
81
|
+ this._currentCanvas.width = parseInt(width, 10);
|
|
82
|
+ this._videoElement.height = parseInt(height, 10);
|
|
83
|
+ this._videoElement.width = parseInt(width, 10);
|
|
84
|
+ this._videoElement.srcObject = stream;
|
|
85
|
+ this._videoElement.play();
|
|
86
|
+
|
|
87
|
+ // Store first capture for comparisons in {@code this._handleScreenshot}.
|
|
88
|
+ this._videoElement.addEventListener('loadeddata', this._initScreenshotCapture);
|
|
89
|
+
|
|
90
|
+ return stream;
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ /**
|
|
94
|
+ * Stops the ongoing {@code ScreenshotCaptureEffect} by clearing the {@code Worker} interval.
|
|
95
|
+ *
|
|
96
|
+ * @returns {void}
|
|
97
|
+ */
|
|
98
|
+ stopEffect() {
|
|
99
|
+ this._streamWorker.postMessage({ id: CLEAR_INTERVAL });
|
|
100
|
+ this._videoElement.removeEventListener('loadeddata', this._initScreenshotCapture);
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ /**
|
|
104
|
+ * Method that is called as soon as the first frame of the video loads from stream.
|
|
105
|
+ * The method is used to store the {@code ImageData} object from the first frames
|
|
106
|
+ * in order to use it for future comparisons based on which we can process only certain
|
|
107
|
+ * screenshots.
|
|
108
|
+ *
|
|
109
|
+ * @private
|
|
110
|
+ * @returns {void}
|
|
111
|
+ */
|
|
112
|
+ _initScreenshotCapture() {
|
|
113
|
+ const storedCanvas = document.createElement('canvas');
|
|
114
|
+ const storedCanvasContext = storedCanvas.getContext('2d');
|
|
115
|
+
|
|
116
|
+ storedCanvasContext.drawImage(this._videoElement, 0, 0, this._streamWidth, this._streamHeight);
|
|
117
|
+ const { data } = storedCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
|
|
118
|
+
|
|
119
|
+ this._storedImageData = data;
|
|
120
|
+ this._streamWorker.postMessage({
|
|
121
|
+ id: SET_INTERVAL,
|
|
122
|
+ timeMs: POLL_INTERVAL
|
|
123
|
+ });
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ /**
|
|
127
|
+ * Handler of the {@code EventHandler} message that calls the appropriate method based on the parameter's id.
|
|
128
|
+ *
|
|
129
|
+ * @private
|
|
130
|
+ * @param {EventHandler} message - Message received from the Worker.
|
|
131
|
+ * @returns {void}
|
|
132
|
+ */
|
|
133
|
+ _handleWorkerAction(message: Object) {
|
|
134
|
+ return message.data.id === INTERVAL_TIMEOUT && this._handleScreenshot();
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ /**
|
|
138
|
+ * Method that decides whether an image should be processed based on a preset pixel lower bound.
|
|
139
|
+ *
|
|
140
|
+ * @private
|
|
141
|
+ * @param {integer} nbPixels - The number of pixels of the candidate image.
|
|
142
|
+ * @returns {boolean} - Whether the image should be processed or not.
|
|
143
|
+ */
|
|
144
|
+ _shouldProcessScreenshot(nbPixels: number) {
|
|
145
|
+ return nbPixels >= PIXEL_LOWER_BOUND;
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ /**
|
|
149
|
+ * Screenshot handler.
|
|
150
|
+ *
|
|
151
|
+ * @private
|
|
152
|
+ * @returns {void}
|
|
153
|
+ */
|
|
154
|
+ _handleScreenshot() {
|
|
155
|
+ this._currentCanvasContext.drawImage(this._videoElement, 0, 0, this._streamWidth, this._streamHeight);
|
|
156
|
+ const { data } = this._currentCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
|
|
157
|
+ const diffPixels = pixelmatch(data, this._storedImageData, null, this._streamWidth, this._streamHeight);
|
|
158
|
+
|
|
159
|
+ if (this._shouldProcessScreenshot(diffPixels)) {
|
|
160
|
+ const conference = getCurrentConference(this._state);
|
|
161
|
+ const sessionId = conference.getMeetingUniqueId();
|
|
162
|
+ const { connection, timeEstablished } = this._state['features/base/connection'];
|
|
163
|
+ const jid = connection.getJid();
|
|
164
|
+ const timeLapseSeconds = timeEstablished && Math.floor((Date.now() - timeEstablished) / 1000);
|
|
165
|
+ const { jwt } = this._state['features/base/jwt'];
|
|
166
|
+
|
|
167
|
+ this._storedImageData = data;
|
|
168
|
+ processScreenshot(this._currentCanvas, {
|
|
169
|
+ jid,
|
|
170
|
+ jwt,
|
|
171
|
+ sessionId,
|
|
172
|
+ timeLapseSeconds
|
|
173
|
+ });
|
|
174
|
+ }
|
|
175
|
+ }
|
|
176
|
+}
|