migrate emitExternalVideoEvent action

This commit is contained in:
Ramón Souza 2024-01-19 11:46:11 -03:00
parent 2ffd9ce7e9
commit 600fe50b73
6 changed files with 60 additions and 107 deletions

View File

@ -1,8 +1,6 @@
import { Meteor } from 'meteor/meteor';
import stopWatchingExternalVideo from './methods/stopWatchingExternalVideo';
import emitExternalVideoEvent from './methods/emitExternalVideoEvent';
Meteor.methods({
stopWatchingExternalVideo,
emitExternalVideoEvent,
});

View File

@ -1,40 +0,0 @@
import { check } from 'meteor/check';
import Logger from '/imports/startup/server/logger';
import RedisPubSub from '/imports/startup/server/redis';
import { extractCredentials } from '/imports/api/common/server/helpers';
export default function emitExternalVideoEvent(options) {
const REDIS_CONFIG = Meteor.settings.private.redis;
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
const EVENT_NAME = 'UpdateExternalVideoPubMsg';
try {
const { meetingId, requesterUserId } = extractCredentials(this.userId);
check(meetingId, String);
check(requesterUserId, String);
const { status, playerStatus } = options;
check(status, String);
check(playerStatus, {
rate: Match.Maybe(Number),
time: Match.Maybe(Number),
state: Match.Maybe(Number),
});
const state = playerStatus.state || 0;
const payload = {
status,
rate: playerStatus.rate || 0,
time: playerStatus.time || 0,
state,
};
Logger.debug(`User id=${requesterUserId} sending ${EVENT_NAME} event:${state} for meeting ${meetingId}`);
RedisPubSub.publishUserMessage(CHANNEL, EVENT_NAME, meetingId, requesterUserId, payload);
} catch (err) {
Logger.error(`Exception while invoking method emitExternalVideoEvent ${err.stack}`);
}
}

View File

@ -2,7 +2,7 @@ import React, { useEffect, useMemo, useRef } from 'react';
import ReactPlayer from 'react-player';
import { defineMessages, useIntl } from 'react-intl';
import audioManager from '/imports/ui/services/audio-manager';
import { useReactiveVar } from '@apollo/client';
import { useReactiveVar, useMutation } from '@apollo/client';
import useCurrentUser from '/imports/ui/core/hooks/useCurrentUser';
import { OnProgressProps } from 'react-player/base';
@ -24,8 +24,8 @@ import { uniqueId } from '/imports/utils/string-utils';
import useTimeSync from '/imports/ui/core/local-states/useTimeSync';
import ExternalVideoPlayerToolbar from './toolbar/component';
import deviceInfo from '/imports/utils/deviceInfo';
import { sendMessage } from './service';
import { ACTIONS } from '../../layout/enums';
import { EXTERNAL_VIDEO_UPDATE } from '../mutations';
import PeerTube from '../custom-players/peertube';
import { ArcPlayer } from '../custom-players/arc-player';
@ -160,6 +160,47 @@ const ExternalVideoPlayer: React.FC<ExternalVideoPlayerProps> = ({
const playerRef = useRef<ReactPlayer>();
const playerParentRef = useRef<HTMLDivElement| null>(null);
const timeoutRef = useRef<ReturnType<typeof setTimeout>>();
const [updateExternalVideo] = useMutation(EXTERNAL_VIDEO_UPDATE);
let lastMessage: {
event: string;
rate: number;
time: number;
state?: string;
} = { event: '', rate: 0, time: 0 };
const sendMessage = (event: string, data: { rate: number; time: number; state?: string}) => {
// don't re-send repeated update messages
if (
lastMessage.event === event
&& lastMessage.time === data.time
) {
return;
}
// don't register to redis a viewer joined message
if (event === 'viewerJoined') {
return;
}
lastMessage = { ...data, event };
// Use an integer for playing state
// 0: stopped 1: playing
// We might use more states in the future
const state = data.state ? 1 : 0;
updateExternalVideo({
variables: {
status: event,
rate: data?.rate,
time: data?.time,
state,
},
});
};
useEffect(() => {
timeoutRef.current = setTimeout(() => {
setAutoPlayBlocked(true);

View File

@ -1,36 +0,0 @@
import { makeCall } from '/imports/ui/services/api';
let lastMessage: {
event: string;
rate: number;
time: number;
state?: string;
} = { event: '', rate: 0, time: 0 };
export const sendMessage = (event: string, data: { rate: number; time: number; state?: string}) => {
// don't re-send repeated update messages
if (
lastMessage.event === event
&& lastMessage.time === data.time
) {
return;
}
// don't register to redis a viewer joined message
if (event === 'viewerJoined') {
return;
}
lastMessage = { ...data, event };
// Use an integer for playing state
// 0: stopped 1: playing
// We might use more states in the future
const state = data.state ? 1 : 0;
makeCall('emitExternalVideoEvent', { status: event, playerStatus: { ...data, state } });
};
export default {
sendMessage,
};

View File

@ -8,4 +8,20 @@ export const EXTERNAL_VIDEO_START = gql`
}
`;
export default { EXTERNAL_VIDEO_START };
export const EXTERNAL_VIDEO_UPDATE = gql`
mutation ExternalVideoUpdate(
$status: String!
$rate: Float!,
$time: Float!,
$state: Float!,
) {
externalVideoUpdate(
status: $status,
rate: $rate,
time: $time,
state: $state,
)
}
`;
export default { EXTERNAL_VIDEO_START, EXTERNAL_VIDEO_UPDATE };

View File

@ -22,31 +22,6 @@ const stopWatching = () => {
makeCall('stopWatchingExternalVideo');
};
let lastMessage = null;
const sendMessage = (event, data) => {
// don't re-send repeated update messages
if (lastMessage && lastMessage.event === event
&& event === 'playerUpdate' && lastMessage.time === data.time) {
return;
}
// don't register to redis a viewer joined message
if (event === 'viewerJoined') {
return;
}
lastMessage = { ...data, event };
// Use an integer for playing state
// 0: stopped 1: playing
// We might use more states in the future
data.state = data.state ? 1 : 0;
makeCall('emitExternalVideoEvent', { status: event, playerStatus: data });
};
const onMessage = (message, func) => {
const streamer = getStreamer(Auth.meetingID);
streamer.on(message, func);
@ -65,7 +40,6 @@ const getPlayingState = (state) => {
};
export {
sendMessage,
onMessage,
removeAllListeners,
isUrlValid,