-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
service.js
executable file
·1028 lines (866 loc) · 32.5 KB
/
service.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Tracker } from 'meteor/tracker';
import { Session } from 'meteor/session';
import Settings from '/imports/ui/services/settings';
import Auth from '/imports/ui/services/auth';
import Meetings from '/imports/api/meetings';
import Users from '/imports/api/users';
import VideoStreams from '/imports/api/video-streams';
import UserListService from '/imports/ui/components/user-list/service';
import { meetingIsBreakout } from '/imports/ui/components/app/service';
import { makeCall } from '/imports/ui/services/api';
import { notify } from '/imports/ui/services/notification';
import deviceInfo from '/imports/utils/deviceInfo';
import browserInfo from '/imports/utils/browserInfo';
import getFromUserSettings from '/imports/ui/services/users-settings';
import VideoPreviewService from '../video-preview/service';
import Storage from '/imports/ui/services/storage/session';
import BBBStorage from '/imports/ui/services/storage';
import logger from '/imports/startup/client/logger';
import _ from 'lodash';
import {
getSortingMethod,
sortVideoStreams,
} from '/imports/ui/components/video-provider/stream-sorting';
import getFromMeetingSettings from '/imports/ui/services/meeting-settings';
const CAMERA_PROFILES = Meteor.settings.public.kurento.cameraProfiles;
const MULTIPLE_CAMERAS = Meteor.settings.public.app.enableMultipleCameras;
const SFU_URL = Meteor.settings.public.kurento.wsUrl;
const ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
const ROLE_VIEWER = Meteor.settings.public.user.role_viewer;
const MIRROR_WEBCAM = Meteor.settings.public.app.mirrorOwnWebcam;
const PIN_WEBCAM = Meteor.settings.public.kurento.enableVideoPin;
const {
thresholds: CAMERA_QUALITY_THRESHOLDS = [],
applyConstraints: CAMERA_QUALITY_THR_CONSTRAINTS = false,
debounceTime: CAMERA_QUALITY_THR_DEBOUNCE = 2500,
} = Meteor.settings.public.kurento.cameraQualityThresholds;
const {
paginationToggleEnabled: PAGINATION_TOGGLE_ENABLED,
pageChangeDebounceTime: PAGE_CHANGE_DEBOUNCE_TIME,
desktopPageSizes: DESKTOP_PAGE_SIZES,
mobilePageSizes: MOBILE_PAGE_SIZES,
} = Meteor.settings.public.kurento.pagination;
const PAGINATION_THRESHOLDS_CONF = Meteor.settings.public.kurento.paginationThresholds;
const PAGINATION_THRESHOLDS = PAGINATION_THRESHOLDS_CONF.thresholds.sort((t1, t2) => t1.users - t2.users);
const PAGINATION_THRESHOLDS_ENABLED = PAGINATION_THRESHOLDS_CONF.enabled;
const {
paginationSorting: PAGINATION_SORTING,
defaultSorting: DEFAULT_SORTING,
} = Meteor.settings.public.kurento.cameraSortingModes;
const DEFAULT_VIDEO_MEDIA_SERVER = Meteor.settings.public.kurento.videoMediaServer;
const FILTER_VIDEO_STATS = [
'outbound-rtp',
'inbound-rtp',
];
const TOKEN = '_';
class VideoService {
constructor() {
this.defineProperties({
isConnecting: false,
isConnected: false,
currentVideoPageIndex: 0,
numberOfPages: 0,
pageSize: 0,
});
this.userParameterProfile = null;
this.isMobile = deviceInfo.isMobile;
this.isSafari = browserInfo.isSafari;
this.numberOfDevices = 0;
this.record = null;
this.hackRecordViewer = null;
// If the page isn't served over HTTPS there won't be mediaDevices
if (navigator.mediaDevices) {
this.updateNumberOfDevices = this.updateNumberOfDevices.bind(this);
// Safari doesn't support ondevicechange
if (!this.isSafari) {
navigator.mediaDevices.ondevicechange = event => this.updateNumberOfDevices();
}
this.updateNumberOfDevices();
}
// FIXME this is abhorrent. Remove when peer lifecycle is properly decoupled
// from the React component's lifecycle. Any attempt at a half-baked
// decoupling will most probably generate problems - prlanzarin Dec 16 2021
this.webRtcPeersRef = {};
}
defineProperties(obj) {
Object.keys(obj).forEach((key) => {
const privateKey = `_${key}`;
this[privateKey] = {
value: obj[key],
tracker: new Tracker.Dependency(),
};
Object.defineProperty(this, key, {
set: (value) => {
this[privateKey].value = value;
this[privateKey].tracker.changed();
},
get: () => {
this[privateKey].tracker.depend();
return this[privateKey].value;
},
});
});
}
fetchNumberOfDevices(devices) {
const deviceIds = [];
devices.forEach(d => {
const validDeviceId = d.deviceId !== '' && !deviceIds.includes(d.deviceId)
if (d.kind === 'videoinput' && validDeviceId) {
deviceIds.push(d.deviceId);
}
});
return deviceIds.length;
}
updateNumberOfDevices(devices = null) {
if (devices) {
this.numberOfDevices = this.fetchNumberOfDevices(devices);
} else {
navigator.mediaDevices.enumerateDevices().then(devices => {
this.numberOfDevices = this.fetchNumberOfDevices(devices);
});
}
}
joinVideo(deviceId) {
this.deviceId = deviceId;
this.isConnecting = true;
Storage.setItem('isFirstJoin', false);
}
joinedVideo() {
this.isConnected = true;
}
storeDeviceIds() {
const streams = VideoStreams.find(
{
meetingId: Auth.meetingID,
userId: Auth.userID,
}, { fields: { deviceId: 1 } },
).fetch();
let deviceIds = [];
streams.forEach(s => {
deviceIds.push(s.deviceId);
}
);
Session.set('deviceIds', deviceIds.join());
}
exitVideo() {
if (this.isConnected) {
logger.info({
logCode: 'video_provider_unsharewebcam',
}, `Sending unshare all ${Auth.userID} webcams notification to meteor`);
const streams = VideoStreams.find(
{
meetingId: Auth.meetingID,
userId: Auth.userID,
}, { fields: { stream: 1 } },
).fetch();
streams.forEach(s => this.sendUserUnshareWebcam(s.stream));
this.exitedVideo();
}
}
exitedVideo() {
this.isConnecting = false;
this.deviceId = null;
this.isConnected = false;
}
stopVideo(cameraId) {
const streams = VideoStreams.find(
{
meetingId: Auth.meetingID,
userId: Auth.userID,
}, { fields: { stream: 1 } },
).fetch();
const hasTargetStream = streams.some(s => s.stream === cameraId);
const hasOtherStream = streams.some(s => s.stream !== cameraId);
// Check if the target (cameraId) stream exists in the remote collection.
// If it does, means it was successfully shared. So do the full stop procedure.
if (hasTargetStream) {
this.sendUserUnshareWebcam(cameraId);
}
if (!hasOtherStream) {
// There's no other remote stream, meaning (OR)
// a) This was effectively the last webcam being unshared
// b) This was a connecting stream timing out (not effectively shared)
// For both cases, we clean everything up.
this.exitedVideo();
} else {
// It was not the last webcam the user had successfully shared,
// nor was cameraId present in the server collection.
// Hence it's a connecting stream (not effectively shared) which timed out
this.stopConnectingStream();
}
}
getSharedDevices() {
const devices = VideoStreams.find(
{
meetingId: Auth.meetingID,
userId: Auth.userID,
}, { fields: { deviceId: 1 } },
).fetch().map(vs => vs.deviceId);
return devices;
}
sendUserShareWebcam(cameraId) {
makeCall('userShareWebcam', cameraId);
}
sendUserUnshareWebcam(cameraId) {
makeCall('userUnshareWebcam', cameraId);
}
getAuthenticatedURL() {
return Auth.authenticateURL(SFU_URL);
}
shouldRenderPaginationToggle() {
// Only enable toggle if configured to do so and if we have a page size properly setup
return PAGINATION_TOGGLE_ENABLED && (this.getMyPageSize() > 0);
}
isPaginationEnabled () {
return Settings.application.paginationEnabled && (this.getMyPageSize() > 0);
}
setNumberOfPages (numberOfPublishers, numberOfSubscribers, pageSize) {
// Page size 0 means no pagination, return itself
if (pageSize === 0) return 0;
// Page size refers only to the number of subscribers. Publishers are always
// shown, hence not accounted for
const nofPages = Math.ceil(numberOfSubscribers / pageSize);
if (nofPages !== this.numberOfPages) {
this.numberOfPages = nofPages;
// Check if we have to page back on the current video page index due to a
// page ceasing to exist
if (nofPages === 0) {
this.currentVideoPageIndex = 0;
} else if ((this.currentVideoPageIndex + 1) > this.numberOfPages) {
this.getPreviousVideoPage();
}
}
return this.numberOfPages;
}
getNumberOfPages () {
return this.numberOfPages;
}
setCurrentVideoPageIndex (newVideoPageIndex) {
if (this.currentVideoPageIndex !== newVideoPageIndex) {
this.currentVideoPageIndex = newVideoPageIndex;
}
}
getCurrentVideoPageIndex () {
return this.currentVideoPageIndex;
}
calculateNextPage () {
if (this.numberOfPages === 0) {
return 0;
}
return ((this.currentVideoPageIndex + 1) % this.numberOfPages + this.numberOfPages) % this.numberOfPages;
}
calculatePreviousPage () {
if (this.numberOfPages === 0) {
return 0;
}
return ((this.currentVideoPageIndex - 1) % this.numberOfPages + this.numberOfPages) % this.numberOfPages;
}
getNextVideoPage() {
const nextPage = this.calculateNextPage();
this.setCurrentVideoPageIndex(nextPage);
return this.currentVideoPageIndex;
}
getPreviousVideoPage() {
const previousPage = this.calculatePreviousPage();
this.setCurrentVideoPageIndex(previousPage);
return this.currentVideoPageIndex;
}
getPageSizeDictionary () {
// Dynamic page sizes are disabled. Fetch the stock page sizes.
if (!PAGINATION_THRESHOLDS_ENABLED || PAGINATION_THRESHOLDS.length <= 0) {
return !this.isMobile ? DESKTOP_PAGE_SIZES : MOBILE_PAGE_SIZES;
}
// Dynamic page sizes are enabled. Get the user count, isolate the
// matching threshold entry, return the val.
let targetThreshold;
const userCount = UserListService.getUserCount();
const processThreshold = (threshold = {
desktopPageSizes: DESKTOP_PAGE_SIZES,
mobilePageSizes: MOBILE_PAGE_SIZES
}) => {
// We don't demand that all page sizes should be set in pagination profiles.
// That saves us some space because don't necessarily need to scale mobile
// endpoints.
// If eg mobile isn't set, then return the default value.
if (!this.isMobile) {
return threshold.desktopPageSizes || DESKTOP_PAGE_SIZES;
} else {
return threshold.mobilePageSizes || MOBILE_PAGE_SIZES;
}
};
// Short-circuit: no threshold yet, return stock values (processThreshold has a default arg)
if (userCount < PAGINATION_THRESHOLDS[0].users) return processThreshold();
// Reverse search for the threshold where our participant count is directly equal or great
// The PAGINATION_THRESHOLDS config is sorted when imported.
for (let mapIndex = PAGINATION_THRESHOLDS.length - 1; mapIndex >= 0; --mapIndex) {
targetThreshold = PAGINATION_THRESHOLDS[mapIndex];
if (targetThreshold.users <= userCount) {
return processThreshold(targetThreshold);
}
}
}
setPageSize (size) {
if (this.pageSize !== size) {
this.pageSize = size;
}
return this.pageSize;
}
getMyPageSize () {
let size;
const myRole = this.getMyRole();
const pageSizes = this.getPageSizeDictionary();
switch (myRole) {
case ROLE_MODERATOR:
size = pageSizes.moderator;
break;
case ROLE_VIEWER:
default:
size = pageSizes.viewer
}
return this.setPageSize(size);
}
getVideoPage (streams, pageSize) {
// Publishers are taken into account for the page size calculations. They
// also appear on every page. Same for pinned user.
const [filtered, others] = _.partition(streams, (vs) => Auth.userID === vs.userId || vs.pin);
// Separate pin from local cameras
const [pin, mine] = _.partition(filtered, (vs) => vs.pin);
// Recalculate total number of pages
this.setNumberOfPages(filtered.length, others.length, pageSize);
const chunkIndex = this.currentVideoPageIndex * pageSize;
// This is an extra check because pagination is globally in effect (hard
// limited page sizes, toggles on), but we might still only have one page.
// Use the default sorting method if that's the case.
const sortingMethod = (this.numberOfPages > 1) ? PAGINATION_SORTING : DEFAULT_SORTING;
const paginatedStreams = sortVideoStreams(others, sortingMethod)
.slice(chunkIndex, (chunkIndex + pageSize)) || [];
if (getSortingMethod(sortingMethod).localFirst) {
return [...pin, ...mine, ...paginatedStreams];
}
return [...pin, ...paginatedStreams, ...mine];
}
getUsersIdFromVideoStreams() {
const usersId = VideoStreams.find(
{ meetingId: Auth.meetingID },
{ fields: { userId: 1 } },
).fetch().map(user => user.userId);
return usersId;
}
getVideoPinByUser(userId) {
const user = Users.findOne({ userId }, { fields: { pin: 1 } });
return user.pin;
}
toggleVideoPin(userId, userIsPinned) {
makeCall('changePin', userId, !userIsPinned);
}
getVideoStreams() {
const pageSize = this.getMyPageSize();
const isPaginationDisabled = !this.isPaginationEnabled() || pageSize === 0;
const { neededDataTypes } = isPaginationDisabled
? getSortingMethod(DEFAULT_SORTING)
: getSortingMethod(PAGINATION_SORTING);
let streams = VideoStreams.find(
{ meetingId: Auth.meetingID },
{ fields: neededDataTypes },
).fetch();
// Data savings enabled will only show local streams
const { viewParticipantsWebcams } = Settings.dataSaving;
if (!viewParticipantsWebcams) streams = this.filterLocalOnly(streams);
const moderatorOnly = this.webcamsOnlyForModerator();
if (moderatorOnly) streams = this.filterModeratorOnly(streams);
const connectingStream = this.getConnectingStream(streams);
if (connectingStream) streams.push(connectingStream);
// Pagination is either explictly disabled or pagination is set to 0 (which
// is equivalent to disabling it), so return the mapped streams as they are
// which produces the original non paginated behaviour
if (isPaginationDisabled) {
return {
streams: sortVideoStreams(streams, DEFAULT_SORTING),
totalNumberOfStreams: streams.length
};
}
const paginatedStreams = this.getVideoPage(streams, pageSize);
return { streams: paginatedStreams, totalNumberOfStreams: streams.length };
}
stopConnectingStream () {
this.deviceId = null;
this.isConnecting = false;
}
getConnectingStream(streams) {
let connectingStream;
if (this.isConnecting) {
if (this.deviceId) {
const stream = this.buildStreamName(Auth.userID, this.deviceId);
if (!this.hasStream(streams, stream) && !this.isUserLocked()) {
connectingStream = {
stream,
userId: Auth.userID,
name: Auth.fullname,
};
} else {
// Connecting stream is already stored at database
this.stopConnectingStream();
}
} else {
logger.error({
logCode: 'video_provider_missing_deviceid',
}, 'Could not retrieve a valid deviceId');
}
}
return connectingStream;
}
buildStreamName(userId, deviceId) {
return `${userId}${TOKEN}${deviceId}`;
}
hasVideoStream() {
const videoStreams = VideoStreams.findOne({ userId: Auth.userID },
{ fields: {} });
return !!videoStreams;
}
hasStream(streams, stream) {
return streams.find(s => s.stream === stream);
}
getMediaServerAdapter() {
return getFromMeetingSettings('media-server-video', DEFAULT_VIDEO_MEDIA_SERVER);
}
getMyRole () {
return Users.findOne({ userId: Auth.userID },
{ fields: { role: 1 } })?.role;
}
getRecord() {
if (this.record === null) {
this.record = getFromUserSettings('bbb_record_video', true);
}
// TODO: Remove this
// This is a hack to handle a missing piece at the backend of a particular deploy.
// If, at the time the video is shared, the user has a viewer role and
// meta_hack-record-viewer-video is 'false' this user won't have this video
// stream recorded.
if (this.hackRecordViewer === null) {
const value = getFromMeetingSettings('hack-record-viewer-video', null);
this.hackRecordViewer = value ? value.toLowerCase() === 'true' : true;
}
const hackRecord = this.getMyRole() === ROLE_MODERATOR || this.hackRecordViewer;
return this.record && hackRecord;
}
filterModeratorOnly(streams) {
const amIViewer = this.getMyRole() === ROLE_VIEWER;
if (amIViewer) {
const moderators = Users.find(
{
meetingId: Auth.meetingID,
role: ROLE_MODERATOR,
},
{ fields: { userId: 1 } },
).fetch().map(user => user.userId);
return streams.reduce((result, stream) => {
const { userId } = stream;
const isModerator = moderators.includes(userId);
const isMe = Auth.userID === userId;
if (isModerator || isMe) result.push(stream);
return result;
}, []);
}
return streams;
}
filterLocalOnly(streams) {
return streams.filter(stream => stream.userId === Auth.userID);
}
disableCam() {
const m = Meetings.findOne({ meetingId: Auth.meetingID },
{ fields: { 'lockSettingsProps.disableCam': 1 } });
return m.lockSettingsProps ? m.lockSettingsProps.disableCam : false;
}
webcamsOnlyForModerator() {
const meeting = Meetings.findOne({ meetingId: Auth.meetingID },
{ fields: { 'usersProp.webcamsOnlyForModerator': 1 } });
const user = Users.findOne({ userId: Auth.userID }, { fields: { locked: 1, role: 1 } });
if (meeting?.usersProp && user?.role !== ROLE_MODERATOR && user?.locked) {
return meeting.usersProp.webcamsOnlyForModerator;
}
return false;
}
hasCapReached() {
const meeting = Meetings.findOne(
{ meetingId: Auth.meetingID },
{
fields: {
'meetingProp.meetingCameraCap': 1,
'usersProp.userCameraCap': 1,
},
},
);
// If the meeting prop data is unreachable, force a safe return
if (!meeting?.usersProp || !meeting?.meetingProp) return true;
const { meetingCameraCap } = meeting.meetingProp;
const { userCameraCap } = meeting.usersProp;
const meetingCap = meetingCameraCap !== 0 && this.getVideoStreamsCount() >= meetingCameraCap;
const userCap = userCameraCap !== 0 && this.getLocalVideoStreamsCount() >= userCameraCap;
return meetingCap || userCap;
}
getVideoStreamsCount() {
const streams = VideoStreams.find({}).count();
return streams;
}
getLocalVideoStreamsCount() {
const localStreams = VideoStreams.find(
{ userId: Auth.userID }
).count();
return localStreams;
}
getInfo() {
const m = Meetings.findOne({ meetingId: Auth.meetingID },
{ fields: { 'voiceProp.voiceConf': 1 } });
const voiceBridge = m.voiceProp ? m.voiceProp.voiceConf : null;
return {
userId: Auth.userID,
userName: Auth.fullname,
meetingId: Auth.meetingID,
sessionToken: Auth.sessionToken,
voiceBridge,
};
}
mirrorOwnWebcam(userId = null) {
// only true if setting defined and video ids match
const isOwnWebcam = userId ? Auth.userID === userId : true;
const isEnabledMirroring = getFromUserSettings('bbb_mirror_own_webcam', MIRROR_WEBCAM);
return isOwnWebcam && isEnabledMirroring;
}
isPinEnabled() {
return PIN_WEBCAM;
}
// In user-list it is necessary to check if the user is sharing his webcam
isVideoPinEnabledForCurrentUser() {
const currentUser = Users.findOne({ userId: Auth.userID },
{ fields: { role: 1 } });
const isModerator = currentUser?.role === 'MODERATOR';
const isBreakout = meetingIsBreakout();
const isPinEnabled = this.isPinEnabled();
return !!(isModerator
&& isPinEnabled
&& !isBreakout);
}
getMyStreamId(deviceId) {
const videoStream = VideoStreams.findOne(
{
meetingId: Auth.meetingID,
userId: Auth.userID,
deviceId,
}, { fields: { stream: 1 } },
);
return videoStream ? videoStream.stream : null;
}
isUserLocked() {
return !!Users.findOne({
meetingId: Auth.meetingID,
userId: Auth.userID,
locked: true,
role: { $ne: ROLE_MODERATOR },
}, { fields: {} }) && this.disableCam();
}
lockUser() {
if (this.isConnected) {
this.exitVideo();
}
}
isLocalStream(cameraId) {
return cameraId.startsWith(Auth.userID);
}
playStart(cameraId) {
if (this.isLocalStream(cameraId)) {
this.sendUserShareWebcam(cameraId);
this.joinedVideo();
}
}
getCameraProfile() {
const profileId = BBBStorage.getItem('WebcamProfileId') || '';
const cameraProfile = CAMERA_PROFILES.find(profile => profile.id === profileId)
|| CAMERA_PROFILES.find(profile => profile.default)
|| CAMERA_PROFILES[0];
const deviceId = BBBStorage.getItem('WebcamDeviceId');
if (deviceId) {
cameraProfile.constraints = cameraProfile.constraints || {};
cameraProfile.constraints.deviceId = { exact: deviceId };
}
return cameraProfile;
}
addCandidateToPeer(peer, candidate, cameraId) {
peer.addIceCandidate(candidate).catch((error) => {
if (error) {
// Just log the error. We can't be sure if a candidate failure on add is
// fatal or not, so that's why we have a timeout set up for negotiations
// and listeners for ICE state transitioning to failures, so we won't
// act on it here
logger.error({
logCode: 'video_provider_addicecandidate_error',
extraInfo: {
cameraId,
error,
},
}, `Adding ICE candidate failed for ${cameraId} due to ${error.message}`);
}
});
}
processInboundIceQueue(peer, cameraId) {
while (peer.inboundIceQueue.length) {
const candidate = peer.inboundIceQueue.shift();
this.addCandidateToPeer(peer, candidate, cameraId);
}
}
onBeforeUnload() {
this.exitVideo();
}
getStatus() {
if (this.isConnecting) return 'videoConnecting';
if (this.isConnected) return 'connected';
return 'disconnected';
}
disableReason() {
const locks = {
videoLocked: this.isUserLocked(),
camCapReached: this.hasCapReached() && !this.hasVideoStream(),
meteorDisconnected: !Meteor.status().connected
};
const locksKeys = Object.keys(locks);
const disableReason = locksKeys.filter( i => locks[i]).shift();
return disableReason ? disableReason : false;
}
getRole(isLocal) {
return isLocal ? 'share' : 'viewer';
}
getUserParameterProfile() {
if (this.userParameterProfile === null) {
this.userParameterProfile = getFromUserSettings(
'bbb_preferred_camera_profile',
(CAMERA_PROFILES.find(i => i.default) || {}).id || null,
);
}
return this.userParameterProfile;
}
isMultipleCamerasEnabled() {
// Multiple cameras shouldn't be enabled with video preview skipping
// Mobile shouldn't be able to share more than one camera at the same time
// Safari needs to implement devicechange event for safe device control
return MULTIPLE_CAMERAS
&& !VideoPreviewService.getSkipVideoPreview()
&& !this.isMobile
&& !this.isSafari
&& this.numberOfDevices > 1;
}
isProfileBetter (newProfileId, originalProfileId) {
return CAMERA_PROFILES.findIndex(({ id }) => id === newProfileId)
> CAMERA_PROFILES.findIndex(({ id }) => id === originalProfileId);
}
applyBitrate (peer, bitrate) {
const peerConnection = peer.peerConnection;
if ('RTCRtpSender' in window
&& 'setParameters' in window.RTCRtpSender.prototype
&& 'getParameters' in window.RTCRtpSender.prototype) {
peerConnection.getSenders().forEach(sender => {
const { track } = sender;
if (track && track.kind === 'video') {
const parameters = sender.getParameters();
const normalizedBitrate = bitrate * 1000;
// The encoder parameters might not be up yet; if that's the case,
// add a filler object so we can alter the parameters anyways
if (parameters.encodings == null || parameters.encodings.length === 0) {
parameters.encodings = [{}];
}
// Only reset bitrate if it changed in some way to avoid enconder fluctuations
if (parameters.encodings[0].maxBitrate !== normalizedBitrate) {
parameters.encodings[0].maxBitrate = normalizedBitrate;
sender.setParameters(parameters)
.then(() => {
logger.info({
logCode: 'video_provider_bitratechange',
extraInfo: { bitrate },
}, `Bitrate changed: ${bitrate}`);
})
.catch(error => {
logger.warn({
logCode: 'video_provider_bitratechange_failed',
extraInfo: { bitrate, errorMessage: error.message, errorCode: error.code },
}, `Bitrate change failed.`);
});
}
}
})
}
}
// Some browsers (mainly iOS Safari) garble the stream if a constraint is
// reconfigured without propagating previous height/width info
reapplyResolutionIfNeeded (track, constraints) {
if (typeof track.getSettings !== 'function') {
return constraints;
}
const trackSettings = track.getSettings();
if (trackSettings.width && trackSettings.height) {
return {
...constraints,
width: trackSettings.width,
height: trackSettings.height,
};
}
return constraints;
}
applyCameraProfile (peer, profileId) {
const profile = CAMERA_PROFILES.find((targetProfile) => targetProfile.id === profileId);
// When this should be skipped:
// 1 - Badly defined profile
// 2 - Badly defined peer (ie {})
// 3 - The target profile is already applied
// 4 - The targetr profile is better than the original profile
if (!profile
|| peer == null
|| peer.peerConnection == null
|| peer.currentProfileId === profileId
|| this.isProfileBetter(profileId, peer.originalProfileId)) {
return;
}
const { bitrate, constraints } = profile;
if (bitrate) this.applyBitrate(peer, bitrate);
if (CAMERA_QUALITY_THR_CONSTRAINTS
&& constraints
&& typeof constraints === 'object'
) {
peer.peerConnection.getSenders().forEach((sender) => {
const { track } = sender;
if (track && track.kind === 'video' && typeof track.applyConstraints === 'function') {
const normalizedVideoConstraints = this.reapplyResolutionIfNeeded(track, constraints);
track.applyConstraints(normalizedVideoConstraints)
.catch((error) => {
logger.warn({
logCode: 'video_provider_constraintchange_failed',
extraInfo: { errorName: error.name, errorCode: error.code },
}, 'Error applying camera profile');
});
}
});
}
logger.info({
logCode: 'video_provider_profile_applied',
extraInfo: { profileId },
}, `New camera profile applied: ${profileId}`);
peer.currentProfileId = profileId;
}
getThreshold (numberOfPublishers) {
let targetThreshold = { threshold: 0, profile: 'original' };
let finalThreshold = { threshold: 0, profile: 'original' };
for(let mapIndex = 0; mapIndex < CAMERA_QUALITY_THRESHOLDS.length; mapIndex++) {
targetThreshold = CAMERA_QUALITY_THRESHOLDS[mapIndex];
if (targetThreshold.threshold <= numberOfPublishers) {
finalThreshold = targetThreshold;
}
}
return finalThreshold;
}
getPreloadedStream () {
if (this.deviceId == null) return;
return VideoPreviewService.getStream(this.deviceId);
}
/**
* Get all active video peers.
* @returns An Object containing the reference for all active peers peers
*/
getActivePeers() {
const videoData = this.getVideoStreams();
if (!videoData) return null;
const { streams: activeVideoStreams } = videoData;
if (!activeVideoStreams) return null;
const activePeers = {};
activeVideoStreams.forEach((stream) => {
if (this.webRtcPeersRef[stream.stream]) {
activePeers[stream.stream] = this.webRtcPeersRef[stream.stream].peerConnection;
}
});
return activePeers;
}
/**
* Get stats about all active video peer.
* We filter the status based on FILTER_VIDEO_STATS constant.
*
* For more information see:
* https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/getStats
* and
* https://developer.mozilla.org/en-US/docs/Web/API/RTCStatsReport
* @returns An Object containing the information about each active peer.
* The returned object follows the format:
* {
* peerId: RTCStatsReport
* }
*/
async getStats() {
const peers = this.getActivePeers();
if (!peers) return null;
const stats = {};
await Promise.all(
Object.keys(peers).map(async (peerId) => {
const peerStats = await peers[peerId].getStats();
const videoStats = {};
peerStats.forEach((stat) => {
if (FILTER_VIDEO_STATS.includes(stat.type)) {
videoStats[stat.type] = stat;
}
});
stats[peerId] = videoStats;
})
);
return stats;
}
updatePeerDictionaryReference(newRef) {
this.webRtcPeersRef = newRef;
}
}
const videoService = new VideoService();
export default {
storeDeviceIds: () => videoService.storeDeviceIds(),
exitVideo: () => videoService.exitVideo(),
joinVideo: deviceId => videoService.joinVideo(deviceId),
stopVideo: cameraId => videoService.stopVideo(cameraId),
getVideoStreams: () => videoService.getVideoStreams(),
getInfo: () => videoService.getInfo(),
getMyStreamId: deviceId => videoService.getMyStreamId(deviceId),
isUserLocked: () => videoService.isUserLocked(),
lockUser: () => videoService.lockUser(),
getAuthenticatedURL: () => videoService.getAuthenticatedURL(),
isLocalStream: cameraId => videoService.isLocalStream(cameraId),
hasVideoStream: () => videoService.hasVideoStream(),
getStatus: () => videoService.getStatus(),
disableReason: () => videoService.disableReason(),
playStart: cameraId => videoService.playStart(cameraId),
getCameraProfile: () => videoService.getCameraProfile(),
addCandidateToPeer: (peer, candidate, cameraId) => videoService.addCandidateToPeer(peer, candidate, cameraId),
processInboundIceQueue: (peer, cameraId) => videoService.processInboundIceQueue(peer, cameraId),
getRole: isLocal => videoService.getRole(isLocal),
getMediaServerAdapter: () => videoService.getMediaServerAdapter(),
getRecord: () => videoService.getRecord(),
getSharedDevices: () => videoService.getSharedDevices(),
getUserParameterProfile: () => videoService.getUserParameterProfile(),