|
@@ -88,21 +88,28 @@ export default class JitsiStreamBackgroundEffect {
|
88
|
88
|
|
89
|
89
|
const track = this._stream.getVideoTracks()[0];
|
90
|
90
|
const { height, width } = track.getSettings() ?? track.getConstraints();
|
|
91
|
+ const { backgroundType } = this._options.virtualBackground;
|
91
|
92
|
|
92
|
93
|
this._outputCanvasElement.height = height;
|
93
|
94
|
this._outputCanvasElement.width = width;
|
94
|
95
|
this._outputCanvasCtx.globalCompositeOperation = 'copy';
|
95
|
96
|
|
96
|
97
|
// Draw segmentation mask.
|
97
|
|
- //
|
98
|
98
|
|
99
|
99
|
// Smooth out the edges.
|
100
|
|
- if (this._options.virtualBackground.backgroundType === VIRTUAL_BACKGROUND_TYPE.IMAGE) {
|
|
100
|
+ if (backgroundType === VIRTUAL_BACKGROUND_TYPE.IMAGE) {
|
101
|
101
|
this._outputCanvasCtx.filter = 'blur(4px)';
|
102
|
102
|
} else {
|
103
|
103
|
this._outputCanvasCtx.filter = 'blur(8px)';
|
104
|
104
|
}
|
|
105
|
+ if (backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
|
106
|
+ // Save current context before applying transformations.
|
|
107
|
+ this._outputCanvasCtx.save();
|
105
|
108
|
|
|
109
|
+ // Flip the canvas and prevent mirror behaviour.
|
|
110
|
+ this._outputCanvasCtx.scale(-1, 1);
|
|
111
|
+ this._outputCanvasCtx.translate(-this._outputCanvasElement.width, 0);
|
|
112
|
+ }
|
106
|
113
|
this._outputCanvasCtx.drawImage(
|
107
|
114
|
this._segmentationMaskCanvas,
|
108
|
115
|
0,
|
|
@@ -114,45 +121,39 @@ export default class JitsiStreamBackgroundEffect {
|
114
|
121
|
this._inputVideoElement.width,
|
115
|
122
|
this._inputVideoElement.height
|
116
|
123
|
);
|
|
124
|
+ if (backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
|
125
|
+ this._outputCanvasCtx.restore();
|
|
126
|
+ }
|
117
|
127
|
this._outputCanvasCtx.globalCompositeOperation = 'source-in';
|
118
|
128
|
this._outputCanvasCtx.filter = 'none';
|
119
|
129
|
|
120
|
130
|
// Draw the foreground video.
|
121
|
|
- //
|
|
131
|
+ if (backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
|
132
|
+ // Save current context before applying transformations.
|
|
133
|
+ this._outputCanvasCtx.save();
|
122
|
134
|
|
|
135
|
+ // Flip the canvas and prevent mirror behaviour.
|
|
136
|
+ this._outputCanvasCtx.scale(-1, 1);
|
|
137
|
+ this._outputCanvasCtx.translate(-this._outputCanvasElement.width, 0);
|
|
138
|
+ }
|
123
|
139
|
this._outputCanvasCtx.drawImage(this._inputVideoElement, 0, 0);
|
|
140
|
+ if (backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
|
141
|
+ this._outputCanvasCtx.restore();
|
|
142
|
+ }
|
124
|
143
|
|
125
|
144
|
// Draw the background.
|
126
|
|
- //
|
127
|
145
|
|
128
|
146
|
this._outputCanvasCtx.globalCompositeOperation = 'destination-over';
|
129
|
|
- if (this._options.virtualBackground.backgroundType === VIRTUAL_BACKGROUND_TYPE.IMAGE) {
|
|
147
|
+ if (backgroundType === VIRTUAL_BACKGROUND_TYPE.IMAGE
|
|
148
|
+ || backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
130
|
149
|
this._outputCanvasCtx.drawImage(
|
131
|
|
- this._virtualImage,
|
132
|
|
- 0,
|
133
|
|
- 0,
|
134
|
|
- this._inputVideoElement.width,
|
135
|
|
- this._inputVideoElement.height
|
136
|
|
- );
|
137
|
|
- }
|
138
|
|
- if (this._options.virtualBackground.backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
139
|
|
-
|
140
|
|
- // save current context before applying transformations
|
141
|
|
- this._outputCanvasCtx.save();
|
142
|
|
-
|
143
|
|
- // flip the canvas and prevent mirror behaviour
|
144
|
|
- this._outputCanvasCtx.scale(-1, 1);
|
145
|
|
- this._outputCanvasCtx.translate(-this._outputCanvasElement.width, 0);
|
146
|
|
- this._outputCanvasCtx.drawImage(
|
147
|
|
- this._virtualVideo,
|
|
150
|
+ backgroundType === VIRTUAL_BACKGROUND_TYPE.IMAGE
|
|
151
|
+ ? this._virtualImage : this._virtualVideo,
|
148
|
152
|
0,
|
149
|
153
|
0,
|
150
|
154
|
this._outputCanvasElement.width,
|
151
|
155
|
this._outputCanvasElement.height
|
152
|
156
|
);
|
153
|
|
-
|
154
|
|
- // restore the canvas
|
155
|
|
- this._outputCanvasCtx.restore();
|
156
|
157
|
} else {
|
157
|
158
|
this._outputCanvasCtx.filter = `blur(${this._options.virtualBackground.blurValue}px)`;
|
158
|
159
|
this._outputCanvasCtx.drawImage(this._inputVideoElement, 0, 0);
|