roadofcloud-playback-sdk
Version:
CloudHubPlaybackSdk
118 lines (117 loc) • 4.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Logger_1 = __importDefault(require("../Logger"));
const global_1 = __importDefault(require("../global"));
const utils_1 = __importDefault(require("../helper/utils"));
class UserController {
constructor(jsonString) {
this.streams = {
video: null,
file: null,
media: null,
screen: null
};
for (let key in jsonString) {
// @ts-ignore
this[key] = jsonString[key];
}
}
getStreamType(streamId) {
Logger_1.default.info('getStreamType', streamId, this.streams);
if (!streamId) {
return false;
}
for (let itemType in this.streams) {
if (this.streams[itemType] && utils_1.default.numToStr(this.streams[itemType].streamId) === utils_1.default.numToStr(streamId)) {
return itemType;
}
}
}
setStream(type, property, onSuccess) {
if (type) {
const { filename } = property;
this.streams[type] = Object.assign({ url: `${global_1.default.config.recordRootUrl}/${filename}` }, property);
if (utils_1.default.isFunction(onSuccess)) {
onSuccess && onSuccess();
}
}
}
getStream(type) {
if (!type) {
return false;
}
return this.streams[type];
}
delStream(streamId, onSuccess) {
console.error('delStream', streamId, this.streams);
if (!streamId) {
return false;
}
for (let itemType in this.streams) {
for (let itemStreamId in this.streams[itemType]) {
if (itemStreamId === utils_1.default.numToStr(streamId)) {
console.error('delStream', utils_1.default.deepCopy(this.streams[itemType][streamId]));
delete this.streams[itemType][streamId];
}
}
}
if (utils_1.default.isFunction(onSuccess)) {
onSuccess && onSuccess();
}
}
createVideo(type, view) {
Logger_1.default.info('createVideo', type, view);
if (!type || !view) {
return false;
}
type = utils_1.default.getMediaType(type);
const { url } = this.getStream(type);
let videoEle = document.getElementById(`${this.id}-${type}`);
if (!videoEle) {
videoEle = document.createElement('video');
videoEle.id = `${this.id}-${type}`;
videoEle.style.width = "100%";
videoEle.style.height = "100%";
}
videoEle.src = url;
videoEle.autoplay = true;
videoEle.controls = global_1.default.config.debug;
view.appendChild(videoEle);
// videoEle.addEventListener('ended', () => this.removeVideo(type))
}
//删除当前的 video 标签
removeVideo(type, onCallback) {
Logger_1.default.info('removeVideo', type);
type = utils_1.default.getMediaType(type);
let videoEle = document.getElementById(`${this.id}-${type}`);
if (videoEle) {
const parentDom = videoEle.parentElement;
parentDom.removeChild(videoEle);
}
onCallback && onCallback();
}
//找到当前用户下正在播放的视频 设置播放进度
setAllVideoTime(actionTs) {
for (let itemType in this.streams) {
for (let itemStreamId in this.streams[itemType]) {
const streamItem = this.streams[itemType][itemStreamId];
if (streamItem) {
this.setVideoTime(actionTs, streamItem);
}
}
}
}
//设置单个视频的播放进度
setVideoTime(actionTs, stream) {
const { type, publishTime } = stream;
const videoEle = document.getElementById(`${this.id}-${type}`);
const videoNextTime = (actionTs - publishTime) / 1000;
if (videoEle && videoNextTime >= 0) {
videoEle.currentTime = videoNextTime;
}
}
}
exports.default = UserController;