2021-05-27 03:09:25 +08:00
|
|
|
import { ExternalVideoMeetings } from '/imports/api/meetings';
|
2018-11-30 01:24:02 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2020-02-27 05:55:56 +08:00
|
|
|
|
2019-10-23 04:15:53 +08:00
|
|
|
import { getStreamer } from '/imports/api/external-videos';
|
2018-11-30 01:24:02 +08:00
|
|
|
import { makeCall } from '/imports/ui/services/api';
|
|
|
|
|
2019-07-16 04:12:07 +08:00
|
|
|
import ReactPlayer from 'react-player';
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2020-07-01 03:20:24 +08:00
|
|
|
import Panopto from './custom-players/panopto';
|
|
|
|
|
|
|
|
const isUrlValid = (url) => {
|
2021-02-10 02:24:21 +08:00
|
|
|
return /^https.*$/.test(url) && (ReactPlayer.canPlay(url) || Panopto.canPlay(url));
|
2020-07-01 03:20:24 +08:00
|
|
|
}
|
2019-01-16 04:44:41 +08:00
|
|
|
|
|
|
|
const startWatching = (url) => {
|
2020-07-01 03:20:24 +08:00
|
|
|
let externalVideoUrl = url;
|
|
|
|
|
|
|
|
if (Panopto.canPlay(url)) {
|
|
|
|
externalVideoUrl = Panopto.getSocialUrl(url);
|
|
|
|
}
|
|
|
|
|
2021-05-28 07:50:43 +08:00
|
|
|
makeCall('startWatchingExternalVideo', externalVideoUrl);
|
2019-01-16 04:44:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const stopWatching = () => {
|
|
|
|
makeCall('stopWatchingExternalVideo');
|
|
|
|
};
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2020-08-28 18:58:55 +08:00
|
|
|
let lastMessage = null;
|
|
|
|
|
2018-11-30 01:24:02 +08:00
|
|
|
const sendMessage = (event, data) => {
|
2020-02-27 05:55:56 +08:00
|
|
|
|
2020-08-28 18:58:55 +08:00
|
|
|
// don't re-send repeated update messages
|
2019-05-23 04:12:13 +08:00
|
|
|
if (lastMessage && lastMessage.event === event
|
|
|
|
&& event === 'playerUpdate' && lastMessage.time === data.time) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-28 18:58:55 +08:00
|
|
|
|
2019-05-23 04:12:13 +08:00
|
|
|
// don't register to redis a viewer joined message
|
2020-08-28 18:58:55 +08:00
|
|
|
if (event === 'viewerJoined') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastMessage = { ...data, event };
|
|
|
|
|
2021-06-03 02:07:03 +08:00
|
|
|
// Use an integer for playing state
|
|
|
|
// 0: stopped 1: playing
|
|
|
|
// We might use more states in the future
|
|
|
|
data.state = data.state ? 1 : 0;
|
|
|
|
|
2020-08-28 18:58:55 +08:00
|
|
|
makeCall('emitExternalVideoEvent', { status: event, playerStatus: data });
|
2018-11-30 01:24:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const onMessage = (message, func) => {
|
2019-10-23 04:15:53 +08:00
|
|
|
const streamer = getStreamer(Auth.meetingID);
|
|
|
|
streamer.on(message, func);
|
2018-11-30 01:24:02 +08:00
|
|
|
};
|
|
|
|
|
2019-09-11 03:35:58 +08:00
|
|
|
const removeAllListeners = (eventType) => {
|
2019-10-23 04:15:53 +08:00
|
|
|
const streamer = getStreamer(Auth.meetingID);
|
|
|
|
streamer.removeAllListeners(eventType);
|
2019-09-11 03:35:58 +08:00
|
|
|
};
|
|
|
|
|
2019-07-13 04:08:55 +08:00
|
|
|
const getVideoUrl = () => {
|
2019-01-21 13:49:01 +08:00
|
|
|
const meetingId = Auth.meetingID;
|
2021-05-27 03:09:25 +08:00
|
|
|
const externalVideo = ExternalVideoMeetings
|
|
|
|
.findOne({ meetingId }, { fields: { externalVideoUrl: 1 } });
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2021-05-27 03:09:25 +08:00
|
|
|
return externalVideo && externalVideo.externalVideoUrl;
|
2019-01-16 04:44:41 +08:00
|
|
|
};
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2021-06-03 02:07:03 +08:00
|
|
|
// Convert state (Number) to playing (Boolean)
|
|
|
|
const getPlayingState = (state) => {
|
|
|
|
if (state === 1) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-11-30 01:24:02 +08:00
|
|
|
export {
|
|
|
|
sendMessage,
|
|
|
|
onMessage,
|
2019-09-11 03:35:58 +08:00
|
|
|
removeAllListeners,
|
2019-07-13 04:08:55 +08:00
|
|
|
getVideoUrl,
|
2019-01-16 04:44:41 +08:00
|
|
|
isUrlValid,
|
2018-11-30 01:24:02 +08:00
|
|
|
startWatching,
|
|
|
|
stopWatching,
|
2021-06-03 02:07:03 +08:00
|
|
|
getPlayingState,
|
2018-11-30 01:24:02 +08:00
|
|
|
};
|