bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/video-provider/service.js

1141 lines
36 KiB
JavaScript
Raw Normal View History

import { Tracker } from 'meteor/tracker';
2019-11-28 21:13:06 +08:00
import { Session } from 'meteor/session';
import Settings from '/imports/ui/services/settings';
import Auth from '/imports/ui/services/auth';
2019-11-28 21:13:06 +08:00
import Meetings from '/imports/api/meetings';
import Users from '/imports/api/users';
import VideoStreams from '/imports/api/video-streams';
2018-04-09 22:39:27 +08:00
import UserListService from '/imports/ui/components/user-list/service';
import { meetingIsBreakout } from '/imports/ui/components/app/service';
2019-11-28 21:13:06 +08:00
import { makeCall } from '/imports/ui/services/api';
import { notify } from '/imports/ui/services/notification';
2021-04-01 01:13:36 +08:00
import deviceInfo from '/imports/utils/deviceInfo';
import browserInfo from '/imports/utils/browserInfo';
2019-12-19 01:44:56 +08:00
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';
2019-11-28 21:13:06 +08:00
import logger from '/imports/startup/client/logger';
2023-08-10 00:26:42 +08:00
import { debounce } from '/imports/utils/debounce';
import { partition } from '/imports/utils/array-utils';
import {
getSortingMethod,
sortVideoStreams,
} from '/imports/ui/components/video-provider/stream-sorting';
import getFromMeetingSettings from '/imports/ui/services/meeting-settings';
2019-11-28 21:13:06 +08:00
const CAMERA_PROFILES = window.meetingClientSettings.public.kurento.cameraProfiles;
const MULTIPLE_CAMERAS = window.meetingClientSettings.public.app.enableMultipleCameras;
2017-09-01 23:26:57 +08:00
const SFU_URL = window.meetingClientSettings.public.kurento.wsUrl;
const ROLE_MODERATOR = window.meetingClientSettings.public.user.role_moderator;
const ROLE_VIEWER = window.meetingClientSettings.public.user.role_viewer;
const MIRROR_WEBCAM = window.meetingClientSettings.public.app.mirrorOwnWebcam;
const PIN_WEBCAM = window.meetingClientSettings.public.kurento.enableVideoPin;
const {
thresholds: CAMERA_QUALITY_THRESHOLDS = [],
applyConstraints: CAMERA_QUALITY_THR_CONSTRAINTS = false,
debounceTime: CAMERA_QUALITY_THR_DEBOUNCE = 2500,
} = window.meetingClientSettings.public.kurento.cameraQualityThresholds;
const {
paginationToggleEnabled: PAGINATION_TOGGLE_ENABLED,
pageChangeDebounceTime: PAGE_CHANGE_DEBOUNCE_TIME,
desktopPageSizes: DESKTOP_PAGE_SIZES,
mobilePageSizes: MOBILE_PAGE_SIZES,
2023-08-14 20:46:05 +08:00
desktopGridSizes: DESKTOP_GRID_SIZES,
mobileGridSizes: MOBILE_GRID_SIZES,
} = window.meetingClientSettings.public.kurento.pagination;
const PAGINATION_THRESHOLDS_CONF = window.meetingClientSettings.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,
} = window.meetingClientSettings.public.kurento.cameraSortingModes;
const DEFAULT_VIDEO_MEDIA_SERVER = window.meetingClientSettings.public.kurento.videoMediaServer;
2019-11-28 21:13:06 +08:00
const FILTER_VIDEO_STATS = [
'outbound-rtp',
'inbound-rtp',
];
2019-11-28 21:13:06 +08:00
const TOKEN = '_';
class VideoService {
constructor() {
this.defineProperties({
2019-11-28 21:13:06 +08:00
isConnecting: false,
isConnected: false,
currentVideoPageIndex: 0,
numberOfPages: 0,
pageSize: 0,
});
2020-03-27 05:15:41 +08:00
this.userParameterProfile = null;
2021-04-01 01:13:36 +08:00
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 = {};
}
2017-09-20 11:12:10 +08:00
defineProperties(obj) {
Object.keys(obj).forEach((key) => {
const privateKey = `_${key}`;
this[privateKey] = {
value: obj[key],
tracker: new Tracker.Dependency(),
};
2017-09-20 11:12:10 +08:00
Object.defineProperty(this, key, {
set: (value) => {
this[privateKey].value = value;
this[privateKey].tracker.changed();
},
get: () => {
this[privateKey].tracker.depend();
return this[privateKey].value;
},
});
});
}
2017-09-01 23:26:57 +08:00
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);
});
}
}
2019-11-28 21:13:06 +08:00
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());
}
2024-01-27 00:21:58 +08:00
exitVideo(sendUserUnshareWebcam) {
2019-11-28 21:13:06 +08:00
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();
2024-01-27 00:21:58 +08:00
streams.forEach(s => sendUserUnshareWebcam(s.stream));
2019-11-28 21:13:06 +08:00
this.exitedVideo();
}
}
exitedVideo() {
2019-11-28 21:13:06 +08:00
this.isConnecting = false;
this.deviceId = null;
this.isConnected = false;
}
2024-01-27 00:21:58 +08:00
stopVideo(cameraId, sendUserUnshareWebcam) {
2019-11-28 21:13:06 +08:00
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) {
2024-01-27 00:21:58 +08:00
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.
2019-11-28 21:13:06 +08:00
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();
2019-11-28 21:13:06 +08:00
}
}
getSharedDevices() {
const devices = VideoStreams.find(
{
meetingId: Auth.meetingID,
userId: Auth.userID,
}, { fields: { deviceId: 1 } },
).fetch().map(vs => vs.deviceId);
return devices;
}
2019-11-28 21:13:06 +08:00
sendUserShareWebcam(cameraId) {
makeCall('userShareWebcam', cameraId);
}
2019-11-28 21:13:06 +08:00
sendUserUnshareWebcam(cameraId) {
makeCall('userUnshareWebcam', cameraId);
}
2019-11-28 21:13:06 +08:00
getAuthenticatedURL() {
return Auth.authenticateURL(SFU_URL);
}
2018-04-10 02:28:54 +08:00
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);
}
2023-08-09 00:05:54 +08:00
getGridSize () {
let size;
const myRole = this.getMyRole();
2023-08-14 20:46:05 +08:00
const gridSizes = !this.isMobile ? DESKTOP_GRID_SIZES : MOBILE_GRID_SIZES;
2023-08-09 00:05:54 +08:00
switch (myRole) {
case ROLE_MODERATOR:
2023-08-14 20:46:05 +08:00
size = gridSizes.moderator;
2023-08-09 00:05:54 +08:00
break;
case ROLE_VIEWER:
default:
2023-08-14 20:46:05 +08:00
size = gridSizes.viewer
2023-08-09 00:05:54 +08:00
}
return 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 } });
2024-02-23 23:01:53 +08:00
return user?.pin || false;
}
2023-07-26 00:20:45 +08:00
isGridEnabled() {
return Session.get('isGridEnabled');
}
2019-11-28 21:13:06 +08:00
getVideoStreams() {
const pageSize = this.getMyPageSize();
const isPaginationDisabled = !this.isPaginationEnabled() || pageSize === 0;
const { neededDataTypes } = isPaginationDisabled
? getSortingMethod(DEFAULT_SORTING)
: getSortingMethod(PAGINATION_SORTING);
2023-07-26 00:20:45 +08:00
const isGridEnabled = this.isGridEnabled();
let gridUsers = [];
let users = [];
if (isGridEnabled) {
users = Users.find(
{ meetingId: Auth.meetingID },
{ fields: { loggedOut: 1, left: 1, ...neededDataTypes} },
).fetch();
}
2020-03-27 02:17:59 +08:00
let streams = VideoStreams.find(
2019-11-28 21:13:06 +08:00
{ meetingId: Auth.meetingID },
{ fields: neededDataTypes },
2019-11-28 21:13:06 +08:00
).fetch();
// Data savings enabled will only show local streams
const { viewParticipantsWebcams } = Settings.dataSaving;
if (!viewParticipantsWebcams) streams = this.filterLocalOnly(streams);
2020-03-27 02:17:59 +08:00
const connectingStream = this.getConnectingStream(streams);
2019-11-28 21:13:06 +08:00
if (connectingStream) streams.push(connectingStream);
// Pagination is either explicitly 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) {
if (isGridEnabled) {
const streamUsers = streams.map((stream) => stream.userId);
2024-03-12 21:24:37 +08:00
gridUsers = users.filter(
(user) => !user.loggedOut && !user.left && !streamUsers.includes(user.userId)
).map((user) => ({
isGridItem: true,
...user,
2024-03-12 21:24:37 +08:00
}));
}
return {
streams: sortVideoStreams(streams, DEFAULT_SORTING),
gridUsers,
totalNumberOfStreams: streams.length
};
}
const paginatedStreams = this.getVideoPage(streams, pageSize);
2023-08-09 00:05:54 +08:00
if (isGridEnabled) {
const streamUsers = paginatedStreams.map((stream) => stream.userId);
gridUsers = users.filter(
(user) => !user.loggedOut && !user.left && !streamUsers.includes(user.userId)
).map((user) => ({
isGridItem: true,
...user,
2024-03-12 21:24:37 +08:00
}));
2023-08-09 00:05:54 +08:00
}
return { streams: paginatedStreams, gridUsers, totalNumberOfStreams: streams.length };
2019-11-28 21:13:06 +08:00
}
2024-02-23 23:01:53 +08:00
fetchVideoStreams() {
const pageSize = this.getMyPageSize();
const isPaginationDisabled = !this.isPaginationEnabled() || pageSize === 0;
let streams = [...VideoStreams.find(
{ meetingId: Auth.meetingID },
).fetch()];
const { viewParticipantsWebcams } = Settings.dataSaving;
if (!viewParticipantsWebcams) streams = this.filterLocalOnly(streams);
if (!isPaginationDisabled) {
return this.getVideoPage(streams, pageSize);
}
const connectingStream = this.getConnectingStream(streams);
if (connectingStream) {
streams.push(connectingStream);
}
return streams;
}
getGridUsers(users, streams) {
const pageSize = this.getMyPageSize();
const isPaginationDisabled = !this.isPaginationEnabled() || pageSize === 0;
const isGridEnabled = this.isGridEnabled();
let gridUsers = [];
if (isPaginationDisabled) {
if (isGridEnabled) {
const streamUsers = streams.map((stream) => stream.userId);
gridUsers = users.filter(
(user) => !user.loggedOut && !user.left && !streamUsers.includes(user.userId),
).map((user) => ({
isGridItem: true,
...user,
}));
}
return gridUsers;
}
const paginatedStreams = this.getVideoPage(streams, pageSize);
if (isGridEnabled) {
const streamUsers = paginatedStreams.map((stream) => stream.userId);
gridUsers = users.filter(
(user) => !user.loggedOut && !user.left && !streamUsers.includes(user.userId),
).map((user) => ({
isGridItem: true,
...user,
}));
}
return gridUsers;
}
stopConnectingStream() {
this.deviceId = null;
this.isConnecting = false;
}
2019-11-28 21:13:06 +08:00
getConnectingStream(streams) {
let connectingStream;
2018-04-10 02:28:54 +08:00
2019-11-28 21:13:06 +08:00
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();
2019-11-28 21:13:06 +08:00
}
} else {
logger.error({
logCode: 'video_provider_missing_deviceid',
}, 'Could not retrieve a valid deviceId');
}
}
2018-04-10 02:28:54 +08:00
2019-11-28 21:13:06 +08:00
return connectingStream;
2018-04-10 02:28:54 +08:00
}
2019-11-28 21:13:06 +08:00
buildStreamName(userId, deviceId) {
return `${userId}${TOKEN}${deviceId}`;
2018-03-22 22:14:54 +08:00
}
2019-11-28 21:13:06 +08:00
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;
}
2020-03-27 02:17:59 +08:00
filterModeratorOnly(streams) {
const amIViewer = this.getMyRole() === ROLE_VIEWER;
2020-03-27 02:17:59 +08:00
if (amIViewer) {
const moderators = Users.find(
{
role: ROLE_MODERATOR,
},
2020-03-27 05:15:41 +08:00
{ fields: { userId: 1 } },
2020-03-27 02:17:59 +08:00
).fetch().map(user => user.userId);
return streams.reduce((result, stream) => {
const { userId } = stream;
const isModerator = moderators.includes(userId);
const isMe = Auth.userID === userId;
2020-03-27 02:17:59 +08:00
if (isModerator || isMe) result.push(stream);
return result;
}, []);
}
2020-03-27 05:15:41 +08:00
return streams;
2020-03-27 02:17:59 +08:00
}
filterLocalOnly(streams) {
return streams.filter(stream => stream.userId === Auth.userID);
}
2019-11-28 21:13:06 +08:00
disableCam() {
const m = Meetings.findOne({ meetingId: Auth.meetingID },
{ fields: { 'lockSettings.disableCam': 1 } });
return m.lockSettings ? m.lockSettings.disableCam : false;
2018-03-22 22:14:54 +08:00
}
2020-03-27 02:17:59 +08:00
webcamsOnlyForModerator() {
2021-07-08 01:00:47 +08:00
const meeting = Meetings.findOne({ meetingId: Auth.meetingID },
{ fields: { 'usersPolicies.webcamsOnlyForModerator': 1 } });
2021-07-08 01:00:47 +08:00
const user = Users.findOne({ userId: Auth.userID }, { fields: { locked: 1, role: 1 } });
if (meeting?.usersPolicies && user?.role !== ROLE_MODERATOR && user?.locked) {
return meeting.usersPolicies.webcamsOnlyForModerator;
2021-07-08 01:00:47 +08:00
}
return false;
2020-03-27 02:17:59 +08:00
}
hasCapReached() {
const meeting = Meetings.findOne(
{ meetingId: Auth.meetingID },
{
fields: {
meetingCameraCap: 1,
'usersPolicies.userCameraCap': 1,
},
},
);
// If the meeting prop data is unreachable, force a safe return
if (
meeting?.usersPolicies === undefined
|| !meeting?.meetingCameraCap === undefined
) return true;
const { meetingCameraCap } = meeting;
const { userCameraCap } = meeting.usersPolicies;
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;
}
2019-11-28 21:13:06 +08:00
getInfo() {
const m = Meetings.findOne({ meetingId: Auth.meetingID },
{ fields: { 'voiceSettings.voiceConf': 1 } });
const voiceBridge = m.voiceSettings ? m.voiceSettings.voiceConf : null;
2019-11-28 21:13:06 +08:00
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;
2020-06-16 04:51:46 +08:00
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
2023-12-05 00:26:26 +08:00
isVideoPinEnabledForCurrentUser(isModerator) {
const isBreakout = meetingIsBreakout();
const isPinEnabled = this.isPinEnabled();
return !!(isModerator
&& isPinEnabled
&& !isBreakout);
}
getMyStreamId(deviceId) {
2019-12-19 01:44:56 +08:00
const videoStream = VideoStreams.findOne(
{
meetingId: Auth.meetingID,
userId: Auth.userID,
deviceId,
}, { fields: { stream: 1 } },
2019-12-19 01:44:56 +08:00
);
return videoStream ? videoStream.stream : null;
}
2019-11-28 21:13:06 +08:00
isUserLocked() {
return !!Users.findOne({
userId: Auth.userID,
locked: true,
role: { $ne: ROLE_MODERATOR },
}, { fields: {} }) && this.disableCam();
}
2024-01-27 00:21:58 +08:00
lockUser(sendUserUnshareWebcam) {
2019-11-28 21:13:06 +08:00
if (this.isConnected) {
2024-01-27 00:21:58 +08:00
this.exitVideo(sendUserUnshareWebcam);
2019-11-28 21:13:06 +08:00
}
}
2019-11-28 21:13:06 +08:00
isLocalStream(cameraId) {
2023-07-26 00:20:45 +08:00
return cameraId?.startsWith(Auth.userID);
2019-11-28 21:13:06 +08:00
}
playStart(cameraId) {
if (this.isLocalStream(cameraId)) {
this.sendUserShareWebcam(cameraId);
this.joinedVideo();
}
}
getCameraProfile() {
const profileId = BBBStorage.getItem('WebcamProfileId') || '';
2019-11-28 21:13:06 +08:00
const cameraProfile = CAMERA_PROFILES.find(profile => profile.id === profileId)
|| CAMERA_PROFILES.find(profile => profile.default)
|| CAMERA_PROFILES[0];
const deviceId = BBBStorage.getItem('WebcamDeviceId');
2019-11-28 21:13:06 +08:00
if (deviceId) {
cameraProfile.constraints = cameraProfile.constraints || {};
cameraProfile.constraints.deviceId = { exact: deviceId };
}
return cameraProfile;
}
addCandidateToPeer(peer, candidate, cameraId) {
peer.addIceCandidate(candidate).catch((error) => {
2019-11-28 21:13:06 +08:00
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}`);
}
});
}
2019-11-28 21:13:06 +08:00
processInboundIceQueue(peer, cameraId) {
while (peer.inboundIceQueue.length) {
const candidate = peer.inboundIceQueue.shift();
this.addCandidateToPeer(peer, candidate, cameraId);
}
}
2024-01-27 00:21:58 +08:00
onBeforeUnload(sendUserUnshareWebcam) {
this.exitVideo(sendUserUnshareWebcam);
2019-11-28 21:13:06 +08:00
}
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;
}
2017-09-01 23:26:57 +08:00
2019-11-28 21:13:06 +08:00
getRole(isLocal) {
return isLocal ? 'share' : 'viewer';
}
2019-12-19 01:44:56 +08:00
getUserParameterProfile() {
2020-03-27 05:15:41 +08:00
if (this.userParameterProfile === null) {
this.userParameterProfile = getFromUserSettings(
'bbb_preferred_camera_profile',
2022-10-03 19:10:08 +08:00
(CAMERA_PROFILES.find(i => i.default) || {}).id || null,
2020-03-27 05:15:41 +08:00
);
}
2019-12-19 01:44:56 +08:00
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;
2019-12-19 01:44:56 +08:00
}
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 encoder 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();
2017-09-01 23:26:57 +08:00
export default {
storeDeviceIds: () => videoService.storeDeviceIds(),
2024-01-27 00:21:58 +08:00
exitVideo: (sendUserUnshareWebcam) => videoService.exitVideo(sendUserUnshareWebcam),
joinVideo: deviceId => videoService.joinVideo(deviceId),
2024-01-27 00:21:58 +08:00
stopVideo: (cameraId, sendUserUnshareWebcam) => videoService.stopVideo(
cameraId,
sendUserUnshareWebcam,
),
2019-11-28 21:13:06 +08:00
getVideoStreams: () => videoService.getVideoStreams(),
getInfo: () => videoService.getInfo(),
getMyStreamId: deviceId => videoService.getMyStreamId(deviceId),
2019-11-28 21:13:06 +08:00
isUserLocked: () => videoService.isUserLocked(),
2024-01-27 00:21:58 +08:00
lockUser: (sendUserUnshareWebcam) => videoService.lockUser(sendUserUnshareWebcam),
2019-11-28 21:13:06 +08:00
getAuthenticatedURL: () => videoService.getAuthenticatedURL(),
isLocalStream: cameraId => videoService.isLocalStream(cameraId),
hasVideoStream: () => videoService.hasVideoStream(),
getStatus: () => videoService.getStatus(),
disableReason: () => videoService.disableReason(),
2019-11-28 21:13:06 +08:00
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(),
2019-12-19 01:44:56 +08:00
getUserParameterProfile: () => videoService.getUserParameterProfile(),
isMultipleCamerasEnabled: () => videoService.isMultipleCamerasEnabled(),
mirrorOwnWebcam: userId => videoService.mirrorOwnWebcam(userId),
hasCapReached: () => videoService.hasCapReached(),
2024-01-27 00:21:58 +08:00
onBeforeUnload: (sendUserUnshareWebcam) => videoService.onBeforeUnload(sendUserUnshareWebcam),
2019-11-28 21:13:06 +08:00
notify: message => notify(message, 'error', 'video'),
updateNumberOfDevices: devices => videoService.updateNumberOfDevices(devices),
2023-03-01 21:39:04 +08:00
applyCameraProfile: debounce(
2023-08-10 00:26:42 +08:00
videoService.applyCameraProfile.bind(videoService),
CAMERA_QUALITY_THR_DEBOUNCE,
{ leading: false, trailing: true },
),
getThreshold: (numberOfPublishers) => videoService.getThreshold(numberOfPublishers),
isPaginationEnabled: () => videoService.isPaginationEnabled(),
getNumberOfPages: () => videoService.getNumberOfPages(),
getCurrentVideoPageIndex: () => videoService.getCurrentVideoPageIndex(),
getPreviousVideoPage: () => videoService.getPreviousVideoPage(),
getNextVideoPage: () => videoService.getNextVideoPage(),
getPageChangeDebounceTime: () => { return PAGE_CHANGE_DEBOUNCE_TIME },
getUsersIdFromVideoStreams: () => videoService.getUsersIdFromVideoStreams(),
shouldRenderPaginationToggle: () => videoService.shouldRenderPaginationToggle(),
getVideoPinByUser: (userId) => videoService.getVideoPinByUser(userId),
isVideoPinEnabledForCurrentUser: (user) => videoService.isVideoPinEnabledForCurrentUser(user),
isPinEnabled: () => videoService.isPinEnabled(),
getPreloadedStream: () => videoService.getPreloadedStream(),
getStats: () => videoService.getStats(),
updatePeerDictionaryReference: (newRef) => videoService.updatePeerDictionaryReference(newRef),
2024-01-26 21:29:52 +08:00
joinedVideo: () => videoService.joinedVideo(),
2024-02-23 23:01:53 +08:00
fetchVideoStreams: () => videoService.fetchVideoStreams(),
getGridUsers: (users = [], streams = []) => videoService.getGridUsers(users, streams),
2024-03-12 21:24:37 +08:00
webcamsOnlyForModerators: () => videoService.webcamsOnlyForModerator(),
2017-09-01 23:26:57 +08:00
};