浏览代码

feat(virtual-background): Desktop share as virtual background

master
tudordan7 4 年前
父节点
当前提交
99f61ca2cd

+ 1
- 0
react/features/app/middlewares.web.js 查看文件

@@ -14,5 +14,6 @@ import '../prejoin/middleware';
14 14
 import '../remote-control/middleware';
15 15
 import '../shared-video/middleware';
16 16
 import '../talk-while-muted/middleware';
17
+import '../virtual-background/middleware';
17 18
 
18 19
 import './middlewares.any';

+ 3
- 17
react/features/stream-effects/virtual-background/JitsiStreamBackgroundEffect.js 查看文件

@@ -1,5 +1,4 @@
1 1
 // @flow
2
-import { JitsiTrackEvents } from '../../base/lib-jitsi-meet';
3 2
 
4 3
 import {
5 4
     CLEAR_TIMEOUT,
@@ -16,7 +15,6 @@ import {
16 15
 export default class JitsiStreamBackgroundEffect {
17 16
     _model: Object;
18 17
     _options: Object;
19
-    _screenSharing: Object;
20 18
     _segmentationPixelCount: number;
21 19
     _inputVideoElement: HTMLVideoElement;
22 20
     _onMaskFrameTimer: Function;
@@ -39,21 +37,19 @@ export default class JitsiStreamBackgroundEffect {
39 37
      * @class
40 38
      * @param {Object} model - Meet model.
41 39
      * @param {Object} options - Segmentation dimensions.
42
-     * @param {Object} screenSharing - Desktop track for displaying desktop share as virtual background.
43 40
      */
44
-    constructor(model: Object, options: Object, screenSharing: Object) {
41
+    constructor(model: Object, options: Object) {
45 42
         this._options = options;
46
-        this._screenSharing = screenSharing;
47 43
 
48 44
         if (this._options.virtualBackground.backgroundType === 'image') {
49 45
             this._virtualImage = document.createElement('img');
50 46
             this._virtualImage.crossOrigin = 'anonymous';
51 47
             this._virtualImage.src = this._options.virtualBackground.virtualSource;
52 48
         }
53
-        if (this._options.virtualBackground.backgroundType === 'desktop-share' && this._screenSharing) {
49
+        if (this._options.virtualBackground.backgroundType === 'desktop-share') {
54 50
             this._virtualVideo = document.createElement('video');
55 51
             this._virtualVideo.autoplay = true;
56
-            this._virtualVideo.srcObject = this._screenSharing.stream;
52
+            this._virtualVideo.srcObject = this._options?.virtualBackground?.virtualSource?.stream;
57 53
         }
58 54
         this._model = model;
59 55
         this._options = options;
@@ -252,15 +248,6 @@ export default class JitsiStreamBackgroundEffect {
252 248
         this._inputVideoElement.height = parseInt(height, 10);
253 249
         this._inputVideoElement.autoplay = true;
254 250
         this._inputVideoElement.srcObject = stream;
255
-        this._screenSharing && this._screenSharing.on(
256
-            JitsiTrackEvents.LOCAL_TRACK_STOPPED,
257
-            () => {
258
-                this._options.virtualBackground.enabled = false;
259
-                this._options.virtualBackground.backgroundType = 'none';
260
-                this._options.virtualBackground.selectedThumbnail = 'none';
261
-                this._options.virtualBackground.backgroundEffectEnabled = false;
262
-                this._options.virtualBackground.enabled = false;
263
-            });
264 251
         this._inputVideoElement.onloadeddata = () => {
265 252
             this._maskFrameTimerWorker.postMessage({
266 253
                 id: SET_TIMEOUT,
@@ -282,6 +269,5 @@ export default class JitsiStreamBackgroundEffect {
282 269
         });
283 270
 
284 271
         this._maskFrameTimerWorker.terminate();
285
-        this._screenSharing && this._screenSharing.dispose();
286 272
     }
287 273
 }

+ 1
- 8
react/features/stream-effects/virtual-background/index.js 查看文件

@@ -2,8 +2,6 @@
2 2
 
3 3
 import * as wasmCheck from 'wasm-check';
4 4
 
5
-import { createLocalTrack } from '../../base/lib-jitsi-meet/functions';
6
-
7 5
 import JitsiStreamBackgroundEffect from './JitsiStreamBackgroundEffect';
8 6
 import createTFLiteModule from './vendor/tflite/tflite';
9 7
 import createTFLiteSIMDModule from './vendor/tflite/tflite-simd';
@@ -37,7 +35,6 @@ export async function createVirtualBackgroundEffect(virtualBackground: Object) {
37 35
         throw new Error('JitsiStreamBackgroundEffect not supported!');
38 36
     }
39 37
     let tflite;
40
-    let screenSharing;
41 38
 
42 39
     if (wasmCheck.feature.simd) {
43 40
         tflite = await createTFLiteSIMDModule();
@@ -58,14 +55,10 @@ export async function createVirtualBackgroundEffect(virtualBackground: Object) {
58 55
 
59 56
     tflite._loadModel(model.byteLength);
60 57
 
61
-    if (virtualBackground.backgroundType === 'desktop-share') {
62
-        screenSharing = await createLocalTrack('desktop', '');
63
-    }
64
-
65 58
     const options = {
66 59
         ...wasmCheck.feature.simd ? segmentationDimensions.model144 : segmentationDimensions.model96,
67 60
         virtualBackground
68 61
     };
69 62
 
70
-    return new JitsiStreamBackgroundEffect(tflite, options, screenSharing);
63
+    return new JitsiStreamBackgroundEffect(tflite, options);
71 64
 }

+ 5
- 1
react/features/virtual-background/components/VirtualBackgroundDialog.js 查看文件

@@ -8,6 +8,7 @@ import uuid from 'uuid';
8 8
 import { Dialog, hideDialog } from '../../base/dialog';
9 9
 import { translate } from '../../base/i18n';
10 10
 import { Icon, IconCloseSmall, IconPlusCircle, IconShareDesktop } from '../../base/icons';
11
+import { createLocalTrack } from '../../base/lib-jitsi-meet/functions';
11 12
 import { connect } from '../../base/redux';
12 13
 import { getLocalVideoTrack } from '../../base/tracks';
13 14
 import { toggleBackgroundEffect } from '../actions';
@@ -120,10 +121,13 @@ function VirtualBackground({ _jitsiTrack, _selectedThumbnail, dispatch, t }: Pro
120 121
     };
121 122
 
122 123
     const shareDesktop = async selection => {
124
+        const url = await createLocalTrack('desktop', '');
125
+
123 126
         setOptions({
124 127
             backgroundType: 'desktop-share',
125 128
             enabled: true,
126
-            selectedThumbnail: selection
129
+            selectedThumbnail: selection,
130
+            url
127 131
         });
128 132
     };
129 133
 

+ 35
- 0
react/features/virtual-background/middleware.js 查看文件

@@ -0,0 +1,35 @@
1
+import { JitsiTrackEvents } from '../base/lib-jitsi-meet';
2
+import { MiddlewareRegistry } from '../base/redux';
3
+import { getLocalVideoTrack } from '../base/tracks';
4
+
5
+import { toggleBackgroundEffect } from './actions';
6
+
7
+/**
8
+ * Middleware which intercepts the desktop video type on
9
+ * virtual background. If the user stops the screen share
10
+ * then the default virtual background is set to 'none' option
11
+ *
12
+ * @param {Store} store - The redux store.
13
+ * @returns {Function}
14
+ */
15
+MiddlewareRegistry.register(store => next => action => {
16
+    const { dispatch, getState } = store;
17
+    const virtualSource = getState()['features/virtual-background'].virtualSource;
18
+    const currentLocalTrack = getLocalVideoTrack(getState()['features/base/tracks']);
19
+
20
+    if (virtualSource?.videoType === 'desktop') {
21
+        const noneOptions = {
22
+            enabled: false,
23
+            backgroundType: 'none',
24
+            selectedThumbnail: 'none',
25
+            backgroundEffectEnabled: false
26
+        };
27
+
28
+        virtualSource
29
+            && virtualSource.on(JitsiTrackEvents.LOCAL_TRACK_STOPPED, () => {
30
+                dispatch(toggleBackgroundEffect(noneOptions, currentLocalTrack.jitsiTrack));
31
+            });
32
+    }
33
+
34
+    return next(action);
35
+});

正在加载...
取消
保存