UNPKG

roadofcloud-playback-sdk

Version:

CloudHubPlaybackSdk

144 lines (143 loc) 6.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ActionFactory_1 = __importDefault(require("../actions/ActionFactory")); const ActionEvent_1 = __importDefault(require("../ActionEvent")); const UserController_1 = __importDefault(require("./UserController")); const Logger_1 = __importDefault(require("../Logger")); const RoomController_1 = __importDefault(require("./RoomController")); const RemoteVideoStateChangedAction_1 = __importDefault(require("../actions/RemoteVideoStateChangedAction")); class PlaybackPlayerController { constructor(recordManager, options = {}) { if (!recordManager) { Logger_1.default.error('recordManager is required'); } this.recordManager = recordManager; this.currIndex = 0; //当前轮询的下标 this.time = null; //当前轮询的时间戳 this.actionTimer = null; //action定时器 this.options = this._defaultOptions(options); this.timeDifference = 0; //系统时间与 record 开始时间的差值 } _defaultOptions(options = {}) { let defaultOptions = { timeStep: 1000, speed: 1 }; for (let key in options) { defaultOptions[key] = options[key]; } return defaultOptions; } start() { this.setTimeDifference(0); //初始化 系统时间与 record 开始时间的差值 //开启定时器 this.actionTimer = setInterval(() => { const curTime = new Date().getTime(); //获取系统时间 this.time = curTime - this.timeDifference; //获取这一次轮询的时间戳 //返回这一次时间戳内的所有 action 事件 默认1s轮询一次 如果设置快进 就根据快进的进度 const { index, actions } = this.recordManager.getActions(this.currIndex, this.time); this.currIndex = index; // 循环所有的事件 actions.forEach((action) => { //派发前处理 this.beforeAction(action); const actionObject = ActionFactory_1.default.create(action); /************需要注意的是在派发前处理还是派发后处理************/ //todo 派发事件 ActionEvent_1.default.dispatch(actionObject); }); }, this.options.timeStep / this.options.speed); } setTimeDifference(time = 0) { Logger_1.default.info('setTimeDifference', time); //time为0的话 回放时间轴回到初始状态 if (time === 0) { this.currIndex = 0; } const date = new Date(); const startTime = this.recordManager.getStartTime(); //回放开始时间 const endTime = this.recordManager.getEndTime(); //回放结束时间 const nextTime = date.getTime() - (startTime + time); //当前时间与回放开始时间计算出来的差值 if (nextTime <= endTime) { //快进的时间在合理范围内就赋值 this.timeDifference = nextTime; Logger_1.default.info('setTimeDifference success', date.getTime() - this.timeDifference); } else { //快进时间不合理 赋值为初始时间 this.timeDifference = date.getTime() - startTime; Logger_1.default.warning('setTimeDifference fail', this.timeDifference); } } seekVideoPosition(actionTs) { const date = new Date(); const time = date.getTime() - this.timeDifference; //快进的时间戳如果大于当前action的时间戳 就把视频的进度切换至发送action的时间点(发布视频时记录了发布时间戳) if (actionTs <= time) { const users = RoomController_1.default.getUsers(); for (let i = 0; i < users.length; i++) { const user = users[i]; if (user) { user.setAllVideoTime(actionTs); } } } } beforeAction(action) { const { method, ts } = action; //存储加入的用户 if (method === 'join') { const { properties, userName: uid } = action; properties.id = uid; RoomController_1.default.setUser(new UserController_1.default(properties)); } // 删除离开的用户 if (method === 'LEAVE') { const { username: uid } = action; RoomController_1.default.delUser(uid); } //发布流时 存储当前用户的流 if (method === 'newPublisher') { const { userName: uid, streamId, options, filename, ts } = action; const { type } = options.attributes || {}; const user = RoomController_1.default.getUser(uid); if (!user) { return false; } const property = { streamId, filename, publishTime: ts, type }; user.setStream(type, property, () => { const actionObject = new RemoteVideoStateChangedAction_1.default(action, RoomController_1.default); ActionEvent_1.default.dispatch(actionObject); }); } //取消流时 删除当前用户的流 if (method === 'cancelPublisher') { const { userName: uid, streamId } = action; const user = RoomController_1.default.getUser(uid); if (!user) { return false; } // user.delStream(streamId, () => { // const actionObject = new RemoteVideoStateChangedAction(action, RoomController) // ActionEvent.dispatch(actionObject) // }) } // 判断是否结束 if (method === 'close') { Logger_1.default.info('回放已全部播放完成'); clearInterval(this.actionTimer); window.CloudHubPlayer.playFinished('Player is stop'); } //如果设置了快进 就更改当前正在播放视频的进度到相应位置 this.seekVideoPosition(ts); } } exports.default = PlaybackPlayerController;