2021-04-16 09:43:34 +00:00
|
|
|
import { batch } from 'react-redux';
|
|
|
|
|
2022-10-28 06:41:12 +00:00
|
|
|
import { IStore } from '../app/types';
|
2022-04-06 02:13:39 +00:00
|
|
|
import { CONFERENCE_JOIN_IN_PROGRESS, CONFERENCE_LEFT } from '../base/conference/actionTypes';
|
|
|
|
import { getCurrentConference } from '../base/conference/functions';
|
2022-10-28 06:41:12 +00:00
|
|
|
import { IJitsiConference } from '../base/conference/reducer';
|
|
|
|
import { MEDIA_TYPE } from '../base/media/constants';
|
|
|
|
import { PARTICIPANT_LEFT } from '../base/participants/actionTypes';
|
|
|
|
import { participantJoined, participantLeft, pinParticipant } from '../base/participants/actions';
|
|
|
|
import { getLocalParticipant, getParticipantById } from '../base/participants/functions';
|
2022-10-06 11:12:57 +00:00
|
|
|
import { FakeParticipant } from '../base/participants/types';
|
2022-10-28 06:41:12 +00:00
|
|
|
import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
|
2021-04-16 09:43:34 +00:00
|
|
|
|
2022-09-27 07:10:28 +00:00
|
|
|
import { RESET_SHARED_VIDEO_STATUS, SET_SHARED_VIDEO_STATUS } from './actionTypes';
|
2021-04-16 09:43:34 +00:00
|
|
|
import {
|
|
|
|
resetSharedVideoStatus,
|
|
|
|
setSharedVideoStatus
|
|
|
|
} from './actions.any';
|
2022-09-27 07:10:28 +00:00
|
|
|
import { PLAYBACK_STATUSES, SHARED_VIDEO, VIDEO_PLAYER_PARTICIPANT_NAME } from './constants';
|
2021-06-11 08:40:23 +00:00
|
|
|
import { isSharingStatus } from './functions';
|
2022-08-29 18:27:44 +00:00
|
|
|
import logger from './logger';
|
2021-04-16 09:43:34 +00:00
|
|
|
|
2022-08-23 12:17:18 +00:00
|
|
|
|
2021-04-16 09:43:34 +00:00
|
|
|
/**
|
|
|
|
* Middleware that captures actions related to video sharing and updates
|
|
|
|
* components not hooked into redux.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
const { dispatch, getState } = store;
|
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
switch (action.type) {
|
2022-04-06 02:13:39 +00:00
|
|
|
case CONFERENCE_JOIN_IN_PROGRESS: {
|
|
|
|
const { conference } = action;
|
|
|
|
const localParticipantId = getLocalParticipant(state)?.id;
|
|
|
|
|
|
|
|
conference.addCommandListener(SHARED_VIDEO,
|
2022-10-28 06:41:12 +00:00
|
|
|
({ value, attributes }: { attributes: {
|
|
|
|
from: string; muted: string; state: string; time: string; }; value: string; }) => {
|
2022-04-06 02:13:39 +00:00
|
|
|
|
|
|
|
const { from } = attributes;
|
|
|
|
const sharedVideoStatus = attributes.state;
|
|
|
|
|
|
|
|
if (isSharingStatus(sharedVideoStatus)) {
|
|
|
|
handleSharingVideoStatus(store, value, attributes, conference);
|
|
|
|
} else if (sharedVideoStatus === 'stop') {
|
2022-09-06 06:51:38 +00:00
|
|
|
const videoParticipant = getParticipantById(state, value);
|
|
|
|
|
|
|
|
dispatch(participantLeft(value, conference, {
|
2022-10-06 11:12:57 +00:00
|
|
|
fakeParticipant: videoParticipant?.fakeParticipant
|
2022-09-06 06:51:38 +00:00
|
|
|
}));
|
|
|
|
|
2022-04-06 02:13:39 +00:00
|
|
|
if (localParticipantId !== from) {
|
|
|
|
dispatch(resetSharedVideoStatus());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2021-04-16 09:43:34 +00:00
|
|
|
case CONFERENCE_LEFT:
|
|
|
|
dispatch(resetSharedVideoStatus());
|
|
|
|
break;
|
2022-04-06 02:13:39 +00:00
|
|
|
case PARTICIPANT_LEFT: {
|
|
|
|
const conference = getCurrentConference(state);
|
|
|
|
const { ownerId: stateOwnerId, videoUrl: statevideoUrl } = state['features/shared-video'];
|
|
|
|
|
2021-04-16 09:43:34 +00:00
|
|
|
if (action.participant.id === stateOwnerId) {
|
|
|
|
batch(() => {
|
|
|
|
dispatch(resetSharedVideoStatus());
|
2022-10-28 06:41:12 +00:00
|
|
|
dispatch(participantLeft(statevideoUrl ?? '', conference));
|
2021-04-16 09:43:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2022-04-06 02:13:39 +00:00
|
|
|
}
|
|
|
|
case SET_SHARED_VIDEO_STATUS: {
|
|
|
|
const conference = getCurrentConference(state);
|
|
|
|
const localParticipantId = getLocalParticipant(state)?.id;
|
|
|
|
const { videoUrl, status, ownerId, time, muted, volume } = action;
|
2022-08-23 12:17:18 +00:00
|
|
|
const operator = status === PLAYBACK_STATUSES.PLAYING ? 'is' : '';
|
|
|
|
|
|
|
|
logger.debug(`User with id: ${ownerId} ${operator} ${status} video sharing.`);
|
2022-08-29 18:27:44 +00:00
|
|
|
|
|
|
|
if (typeof APP !== 'undefined') {
|
|
|
|
APP.API.notifyAudioOrVideoSharingToggled(MEDIA_TYPE.VIDEO, status, ownerId);
|
|
|
|
}
|
2022-04-06 02:13:39 +00:00
|
|
|
|
2021-04-16 09:43:34 +00:00
|
|
|
if (localParticipantId === ownerId) {
|
|
|
|
sendShareVideoCommand({
|
|
|
|
conference,
|
|
|
|
localParticipantId,
|
|
|
|
muted,
|
|
|
|
status,
|
|
|
|
time,
|
|
|
|
id: videoUrl,
|
|
|
|
volume
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2022-04-06 02:13:39 +00:00
|
|
|
}
|
|
|
|
case RESET_SHARED_VIDEO_STATUS: {
|
|
|
|
const localParticipantId = getLocalParticipant(state)?.id;
|
|
|
|
const { ownerId: stateOwnerId, videoUrl: statevideoUrl } = state['features/shared-video'];
|
|
|
|
|
2022-08-29 18:27:44 +00:00
|
|
|
if (!stateOwnerId) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-23 12:17:18 +00:00
|
|
|
logger.debug(`User with id: ${stateOwnerId} stop video sharing.`);
|
2022-08-29 18:27:44 +00:00
|
|
|
|
|
|
|
if (typeof APP !== 'undefined') {
|
|
|
|
APP.API.notifyAudioOrVideoSharingToggled(MEDIA_TYPE.VIDEO, 'stop', stateOwnerId);
|
|
|
|
}
|
|
|
|
|
2021-04-16 09:43:34 +00:00
|
|
|
if (localParticipantId === stateOwnerId) {
|
2022-04-06 02:13:39 +00:00
|
|
|
const conference = getCurrentConference(state);
|
|
|
|
|
2021-04-16 09:43:34 +00:00
|
|
|
sendShareVideoCommand({
|
|
|
|
conference,
|
2022-10-28 06:41:12 +00:00
|
|
|
id: statevideoUrl ?? '',
|
2021-04-16 09:43:34 +00:00
|
|
|
localParticipantId,
|
|
|
|
muted: true,
|
|
|
|
status: 'stop',
|
|
|
|
time: 0,
|
|
|
|
volume: 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-04-06 02:13:39 +00:00
|
|
|
}
|
2021-04-16 09:43:34 +00:00
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the playing, pause and start statuses for the shared video.
|
|
|
|
* Dispatches participantJoined event and, if necessary, pins it.
|
|
|
|
* Sets the SharedVideoStatus if the event was triggered by the local user.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @param {string} videoUrl - The id of the video to the shared.
|
|
|
|
* @param {Object} attributes - The attributes received from the share video command.
|
|
|
|
* @param {JitsiConference} conference - The current conference.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-28 06:41:12 +00:00
|
|
|
function handleSharingVideoStatus(store: IStore, videoUrl: string,
|
|
|
|
{ state, time, from, muted }: { from: string; muted: string; state: string; time: string; },
|
|
|
|
conference: IJitsiConference) {
|
2021-04-16 09:43:34 +00:00
|
|
|
const { dispatch, getState } = store;
|
2022-10-28 06:41:12 +00:00
|
|
|
const localParticipantId = getLocalParticipant(getState())?.id;
|
|
|
|
const oldStatus = getState()['features/shared-video']?.status ?? '';
|
2021-04-16 09:43:34 +00:00
|
|
|
|
|
|
|
if (state === 'start' || ![ 'playing', 'pause', 'start' ].includes(oldStatus)) {
|
2021-06-11 08:40:23 +00:00
|
|
|
const youtubeId = videoUrl.match(/http/) ? false : videoUrl;
|
2021-04-16 09:43:34 +00:00
|
|
|
const avatarURL = youtubeId ? `https://img.youtube.com/vi/${youtubeId}/0.jpg` : '';
|
|
|
|
|
|
|
|
dispatch(participantJoined({
|
|
|
|
conference,
|
2022-10-06 11:12:57 +00:00
|
|
|
fakeParticipant: FakeParticipant.SharedVideo,
|
2021-04-16 09:43:34 +00:00
|
|
|
id: videoUrl,
|
|
|
|
avatarURL,
|
|
|
|
name: VIDEO_PLAYER_PARTICIPANT_NAME
|
|
|
|
}));
|
|
|
|
|
|
|
|
dispatch(pinParticipant(videoUrl));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localParticipantId !== from) {
|
|
|
|
dispatch(setSharedVideoStatus({
|
|
|
|
muted: muted === 'true',
|
|
|
|
ownerId: from,
|
|
|
|
status: state,
|
|
|
|
time: Number(time),
|
|
|
|
videoUrl
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable max-params */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends SHARED_VIDEO command.
|
|
|
|
*
|
|
|
|
* @param {string} id - The id of the video.
|
|
|
|
* @param {string} status - The status of the shared video.
|
|
|
|
* @param {JitsiConference} conference - The current conference.
|
|
|
|
* @param {string} localParticipantId - The id of the local participant.
|
|
|
|
* @param {string} time - The seek position of the video.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-28 06:41:12 +00:00
|
|
|
function sendShareVideoCommand({ id, status, conference, localParticipantId = '', time, muted, volume }: {
|
|
|
|
conference: IJitsiConference; id: string; localParticipantId?: string; muted: boolean;
|
|
|
|
status: string; time: number; volume: number;
|
|
|
|
}) {
|
2021-04-16 09:43:34 +00:00
|
|
|
conference.sendCommandOnce(SHARED_VIDEO, {
|
|
|
|
value: id,
|
|
|
|
attributes: {
|
|
|
|
from: localParticipantId,
|
|
|
|
muted,
|
|
|
|
state: status,
|
|
|
|
time,
|
|
|
|
volume
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|