Socket
Socket
Sign inDemoInstall

bonc-htmlplayer

Package Overview
Dependencies
Maintainers
5
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bonc-htmlplayer - npm Package Compare versions

Comparing version 5.0.2 to 5.0.3

4

package.json
{
"name": "bonc-htmlplayer",
"version": "5.0.2",
"version": "5.0.3",
"description": "东方国信的html播放器内核",

@@ -27,2 +27,2 @@ "main": "./dist/BoncH5Player.js",

}
}
}

@@ -1,6 +0,6 @@

import { extractNALubonc } from './utils'
import RemuxController from './controller/remux.js';
import BufferController from './controller/buffer.js';
import { NALU } from '../util/nalu.js';
import localRecorder from '../util/webmdownload/webmdownload'
import { extractNALubonc } from "./utils";
import RemuxController from "./controller/remux.js";
import BufferController from "./controller/buffer.js";
import { NALU } from "../util/nalu.js";
import localRecorder from "../util/webmdownload/webmdownload";
// import localRecorder from '../util/mp4download/mp4download'

@@ -12,615 +12,606 @@

class H264Mp4Player {
constructor(app) {
this.app = app;
let defaults = {
// node: videoid,
flushingTime: 1000,
clearBuffer: true,
onReady: null, // function called when MSE is ready to accept frames
fps: 25,
debug: true
};
this.options = defaults;
if (!this.options.fps) {
this.options.fps = 25;
}
this.frameDuration = (1000 / this.options.fps) | 0;
constructor(app) {
this.app = app;
let defaults = {
// node: videoid,
flushingTime: 1000,
clearBuffer: true,
onReady: null, // function called when MSE is ready to accept frames
fps: 25,
debug: true,
};
this.options = defaults;
if (!this.options.fps) {
this.options.fps = 25;
}
this.frameDuration = (1000 / this.options.fps) | 0;
this.nalsboncarr = [];
this.nalsboncarrReverseCachQueue = [];
this.nalsbonAudioarr = [];
this.extractNALubonc = extractNALubonc.bind(this);
this.remuxController = new RemuxController(true);
this.remuxController.on('videoPrepare', this.onVideoPrepared.bind(this));
this.remuxController.addTrack('both');
this.lastCleaningTime = Date.now();
this.keyframeCache = [];
this.frameCounter = 0;
this.sourceBuffers = {};
this.isMSESupported = !!window.MediaSource;
this.clearBufferValidate = true
if (!this.isMSESupported) {
throw 'Oops! Browser does not support media source extension.';
}
this.setupMSE();
//生成mp4buffer的回调
this.remuxController.on('buffer', this.onBuffer.bind(this));
//RemuxController ready事件的回调
this.remuxController.on('ready', this.createBuffer.bind(this));
this.spsnal = null;
this.ppsnal = null;
this.spspps = false;
this.cacheNum = 0;
this.recoder = new localRecorder(app)
this.nalsboncarr = [];
this.nalsboncarrReverseCachQueue = [];
this.nalsbonAudioarr = [];
this.extractNALubonc = extractNALubonc.bind(this);
this.remuxController = new RemuxController(true);
this.remuxController.on("videoPrepare", this.onVideoPrepared.bind(this));
this.remuxController.addTrack("both");
this.lastCleaningTime = Date.now();
this.keyframeCache = [];
this.frameCounter = 0;
this.sourceBuffers = {};
this.isMSESupported = !!window.MediaSource;
this.clearBufferValidate = true;
if (!this.isMSESupported) {
throw "Oops! Browser does not support media source extension.";
}
onVideoPrepared() {
this.app.dispatch('startSuccess')
this.setupMSE();
//生成mp4buffer的回调
this.remuxController.on("buffer", this.onBuffer.bind(this));
//RemuxController ready事件的回调
this.remuxController.on("ready", this.createBuffer.bind(this));
this.spsnal = null;
this.ppsnal = null;
this.spspps = false;
this.cacheNum = 0;
this.recoder = new localRecorder(app);
}
onVideoPrepared() {
this.app.dispatch("startSuccess");
}
/**
*
* @h264帧数据,带起始位和type头 {Uint8Array} videobuffer
*/
playBuffer(videobuffer) {
// 将videobuffer的起始位去掉存到 this.nalsboncarr
this.extractNALubonc(new Uint8Array(videobuffer));
this.PlayNals();
this.releaseBuffer();
this.clearBuffer();
}
playBfferReverse(videobuffer) {
this.extractNALubonc(new Uint8Array(videobuffer));
// 非关键帧返回 0
var cacheState = this.PlayNalsReverse();
// 只有收到关键帧的时候会 releaseBuffer clearBuffer
if (cacheState == 1) {
this.releaseBuffer();
this.clearBufferReverse();
}
/**
*
* @h264帧数据,带起始位和type头 {Uint8Array} videobuffer
*/
playBuffer(videobuffer) {
// 将videobuffer的起始位去掉存到 this.nalsboncarr
this.extractNALubonc(new Uint8Array(videobuffer));
this.PlayNals();
this.releaseBuffer();
this.clearBuffer();
}
clearReverseInterval() {
if (this.reversePlayInterval) {
clearInterval(this.reversePlayInterval);
}
playBfferReverse(videobuffer) {
}
this.extractNALubonc(new Uint8Array(videobuffer));
// 非关键帧返回 0
var cacheState = this.PlayNalsReverse();
// 只有收到关键帧的时候会 releaseBuffer clearBuffer
if (cacheState == 1) {
this.releaseBuffer();
this.clearBufferReverse();
}
// 开始录像
localRecord(name) {
this.recoder.startRecord(name);
}
// 结束录像
finishRecord() {
this.recoder.endRecord();
}
// 静音
muted() {
this.app.node.muted = !this.app.node.muted;
return this.app.node.muted;
}
setVolume(volume) {
this.app.node.volume = volume;
}
// 倒放的状态
playReverse(palyRate = 1) {
this.app.node.pause();
var fps = 25 * palyRate; // 每秒多少帧
if (this.reversePlayInterval) {
clearInterval(this.reversePlayInterval);
}
clearReverseInterval() {
if (this.reversePlayInterval) {
clearInterval(this.reversePlayInterval);
}
}
this.currentRange = this.bufferControllers["video"].rangeList.pop();
this.timeend = this.app.node.buffered.end(0);
this.app.node.currentTime = this.app.node.buffered.end(0);
this.reversePlayInterval = setInterval(() => {
if (this.app.node.currentTime < this.timeend - 2) {
// console.log('在边际')
this.app.node.currentTime = this.app.node.buffered.end(0);
this.timeend = this.app.node.buffered.end(0);
} else {
// 不在边际
this.app.node.currentTime += -(1 / fps);
// console.log('不在边际')
}
}, 1000 / fps);
console.log(this.reversePlayInterval);
}
/**
* RemuxController ready事件的回调
*/
createBuffer() {
if (
!this.mseReady ||
!this.remuxController ||
!this.remuxController.isReady() ||
this.bufferControllers
)
return;
this.bufferControllers = {};
// 开始录像
localRecord(name){
this.recoder.startRecord(name)
for (let type in this.remuxController.tracks) {
var sb = this.addSourceBuffer(type);
if (!sb) return;
this.bufferControllers[type] = new BufferController(
sb,
type,
this.app.node,
this.mediaSource
);
this.sourceBuffers[type] = sb;
this.bufferControllers[type].on("error", this.onBufferError.bind(this));
this.bufferControllers[type].on("restart", this.onRestart.bind(this));
}
// 结束录像
finishRecord(){
this.recoder.endRecord()
}
addSourceBuffer(type) {
if (!this.mediaSource || this.mediaSource.readyState !== "open") {
return false;
}
// 静音
muted(){
this.app.node.muted = !this.app.node.muted
return this.app.node.muted
var sb;
let track = this.remuxController.tracks[type];
if (type == "audio") {
if (this.remuxController.hasAudio == false) return;
let audioCodec = "audio/mp4; codecs=" + "mp4a.40." + 2;
sb = this.mediaSource.addSourceBuffer(audioCodec);
} else {
// debugger
sb = this.mediaSource.addSourceBuffer(
`${type}/mp4; codecs="${track.mp4track.codec}"`
);
}
setVolume(volume){
this.app.node.volume = volume
return sb;
}
onBuffer(data) {
if (data.isStart) {
this.startPayload = data.payload;
}
// 倒放的状态
playReverse(palyRate = 1) {
this.app.node.pause();
var fps = 25 * palyRate; // 每秒多少帧
if (this.reversePlayInterval) {
clearInterval(this.reversePlayInterval);
}
// console.log(this.app.node.buffered.end(0),this.app.node.buffered.start(0))
// debugger
this.currentRange = this.bufferControllers['video'].rangeList.pop()
// this.bufferControllers['video'].on('bufferAppended',()=>{
// console.log('append之后',this.app.node.buffered.end(0),this.app.node.buffered.start(0));
// })
this.timeend = this.app.node.buffered.end(0);
this.app.node.currentTime = this.app.node.buffered.end(0);
this.reversePlayInterval = setInterval(() => {
//console.log(this.app.node.buffered.end(0),this.app.node.buffered.start(0))
// 在边际
// if(this.app.node.currentTime<this.currentRange[0]){
// // if(false){
// this.currentRange = this.bufferControllers['video'].rangeList.pop()
// this.app.node.currentTime = this.currentRange[1];
// console.log('切换位置',this.app.node.currentTime);
// }else{
// // 不在边际
// console.log('不在边际,before',this.app.node.currentTime);
// this.app.node.currentTime += -(1/fps);
// console.log('不在边际,after',this.app.node.currentTime);
// }
// console.log(this.app.node.currentTime,this.currentRange)
if (this.app.node.currentTime < this.timeend - 2) {
// console.log('在边际')
this.app.node.currentTime = this.app.node.buffered.end(0);
this.timeend = this.app.node.buffered.end(0);
} else {
// 不在边际
this.app.node.currentTime += -(1 / fps);
// console.log('不在边际')
}
}, 1000 / fps);
console.log(this.reversePlayInterval);
if (this.bufferControllers && this.bufferControllers[data.type]) {
this.bufferControllers[data.type].feed(data.payload);
}
/**
* RemuxController ready事件的回调
*/
createBuffer() {
if (!this.mseReady || !this.remuxController || !this.remuxController.isReady() || this.bufferControllers) return;
this.bufferControllers = {};
}
for (let type in this.remuxController.tracks) {
// let track = this.remuxController.tracks[type];
// let sb;
// if (type == 'audio') {
// if (this.remuxController.hasAudio == false) return;
// let audioCodec = 'audio/mp4; codecs=' + 'mp4a.40.' + 2;
// sb = this.mediaSource.addSourceBuffer(audioCodec);
// } else {
// // debugger
// sb = this.mediaSource.addSourceBuffer(`${type}/mp4; codecs="${track.mp4track.codec}"`);
// }
// sb.mode = 'sequence'
var sb = this.addSourceBuffer(type)
if(!sb) return
this.bufferControllers[type] = new BufferController(sb, type, this.app.node, this.mediaSource);
this.sourceBuffers[type] = sb;
this.bufferControllers[type].on('error', this.onBufferError.bind(this));
this.bufferControllers[type].on('restart', this.onRestart.bind(this));
}
onBufferError(data) {
if (data.name == "QuotaExceeded") {
this.bufferControllers[data.type].initCleanup(this.app.node.currentTime);
return;
}
addSourceBuffer(type){
if (!this.mediaSource || this.mediaSource.readyState !== 'open') {
return false;
}
var sb
let track = this.remuxController.tracks[type];
if (type == 'audio') {
if (this.remuxController.hasAudio == false) return;
let audioCodec = 'audio/mp4; codecs=' + 'mp4a.40.' + 2;
sb = this.mediaSource.addSourceBuffer(audioCodec);
} else {
// debugger
sb = this.mediaSource.addSourceBuffer(`${type}/mp4; codecs="${track.mp4track.codec}"`);
}
return sb
if (
this.mediaSource.sourceBuffers.length > 0 &&
this.sourceBuffers[data.type]
) {
this.mediaSource.removeSourceBuffer(this.sourceBuffers[data.type]);
// 重新把buffer填上
var sb = this.addSourceBuffer(data.type);
this.bufferControllers[type] = new BufferController(
sb,
type,
this.app.node,
this.mediaSource
);
this.sourceBuffers[type] = sb;
this.bufferControllers[type].on("error", this.onBufferError.bind(this));
}
onBuffer(data) {
if(data.isStart){
this.startPayload = data.payload;
}
if (this.bufferControllers && this.bufferControllers[data.type]) {
this.bufferControllers[data.type].feed(data.payload);
}
if (this.mediaSource.sourceBuffers.length == 0) {
try {
this.mediaSource.endOfStream();
} catch (e) {
console.error("mediasource is not available to end");
}
}
}
onBufferError(data) {
if (data.name == 'QuotaExceeded') {
this.bufferControllers[data.type].initCleanup(this.app.node.currentTime);
return;
}
if (this.mediaSource.sourceBuffers.length > 0 && this.sourceBuffers[data.type]) {
this.mediaSource.removeSourceBuffer(this.sourceBuffers[data.type]);
// 重新把buffer填上
var sb = this.addSourceBuffer(data.type);
this.bufferControllers[type] = new BufferController(sb, type, this.app.node, this.mediaSource);
this.sourceBuffers[type] = sb;
this.bufferControllers[type].on('error', this.onBufferError.bind(this));
}
if (this.mediaSource.sourceBuffers.length == 0) {
try {
this.mediaSource.endOfStream();
} catch (e) {
console.error('mediasource is not available to end');
}
}
}
onRestart() {
this.app.dispatch("restart");
}
onRestart(){
this.app.dispatch('restart')
}
/**
* 停止播放
*/
destroy() {
if (this.mediaSource) {
try {
if (this.bufferControllers) {
let SBs = this.mediaSource.sourceBuffers;
if (SBs.length > 0 && SBs[0].updating === false && this.mediaSource.readyState === 'open') {
this.mediaSource.endOfStream();
this.mediaSource.removeSourceBuffer(SBs[0]);
}
}
} catch (e) {
console.error(`mediasource is not available to end ${e.message}`);
}
this.mediaSource = null;
}
if (this.remuxController) {
this.remuxController.destroy();
this.remuxController = null;
}
/**
* 停止播放
*/
destroy() {
if (this.mediaSource) {
try {
if (this.bufferControllers) {
for (let type in this.bufferControllers) {
this.bufferControllers[type].destroy();
}
this.bufferControllers = null;
let SBs = this.mediaSource.sourceBuffers;
if (
SBs.length > 0 &&
SBs[0].updating === false &&
this.mediaSource.readyState === "open"
) {
this.mediaSource.endOfStream();
this.mediaSource.removeSourceBuffer(SBs[0]);
}
}
this.node = false;
this.mseReady = false;
this.videoStarted = false;
} catch (e) {
console.error(`mediasource is not available to end ${e.message}`);
}
this.mediaSource = null;
}
if (this.remuxController) {
this.remuxController.destroy();
this.remuxController = null;
}
if (this.bufferControllers) {
for (let type in this.bufferControllers) {
this.bufferControllers[type].destroy();
}
this.bufferControllers = null;
}
this.node = false;
this.mseReady = false;
this.videoStarted = false;
}
/*-------------------------------内部方法-------------------------------------- */
/*-------------------------------内部方法-------------------------------------- */
setupMSE() {
if (!window.MediaSource) {
throw '当前浏览器不支持视频';
}
this.mediaSource = new MediaSource();
this.app.node.src = URL.createObjectURL(this.mediaSource);
this.mediaSource.addEventListener('sourceopen', this.onMSEOpen.bind(this));
this.mediaSource.addEventListener('sourceclose', this.onMSEClose.bind(this));
this.mediaSource.addEventListener('webkitsourceopen', this.onMSEOpen.bind(this));
this.mediaSource.addEventListener('webkitsourceclose', this.onMSEClose.bind(this));
setupMSE() {
if (!window.MediaSource) {
throw "当前浏览器不支持视频";
}
this.mediaSource = new MediaSource();
this.app.node.src = URL.createObjectURL(this.mediaSource);
this.mediaSource.addEventListener("sourceopen", this.onMSEOpen.bind(this));
this.mediaSource.addEventListener(
"sourceclose",
this.onMSEClose.bind(this)
);
this.mediaSource.addEventListener(
"webkitsourceopen",
this.onMSEOpen.bind(this)
);
this.mediaSource.addEventListener(
"webkitsourceclose",
this.onMSEClose.bind(this)
);
}
/* Events on MSE */
onMSEOpen() {
this.mseReady = true;
if (typeof this.options.onReady === 'function') {
this.options.onReady();
this.options.onReady = null;
}
this.app.dispatch('videoPrepared');
/* Events on MSE */
onMSEOpen() {
this.mseReady = true;
if (typeof this.options.onReady === "function") {
this.options.onReady();
this.options.onReady = null;
}
this.app.dispatch("videoPrepared");
}
onMSEClose() {
this.mseReady = false;
this.videoStarted = false;
}
PlayNals() {
var nalarr = [];
var nal = this.nalsboncarr.shift();
var nalType = nal[0] & 0x1f;
//debug.log('nalType==',nalType);
if (this.spspps === false) {
if (nalType == 7) {
this.spsnal = nal;
//debug.log('找到sps=');
return;
}
else if (nalType == 8) {
this.ppsnal = nal;
//debug.log('找到pps=');
return;
}
onMSEClose() {
this.mseReady = false;
this.videoStarted = false;
}
PlayNals() {
var nalarr = [];
var nal = this.nalsboncarr.shift();
var nalType = nal[0] & 0x1f;
//debug.log('nalType==',nalType);
if (this.spspps === false) {
if (nalType == 7) {
this.spsnal = nal;
//debug.log('找到sps=');
return;
} else if (nalType == 8) {
this.ppsnal = nal;
//debug.log('找到pps=');
return;
}
if (this.spsnal != null && this.ppsnal != null) {
nalarr.push(this.spsnal);
nalarr.push(this.ppsnal);
nalarr.push(nal);
this.spspps = true;
}
if (this.spsnal != null && this.ppsnal != null) {
nalarr.push(this.spsnal);
nalarr.push(this.ppsnal);
nalarr.push(nal);
this.spspps = true;
}
if (this.spspps == false) {
// console.log(nalType)
return;
}
}
else {
if (nalType != 7 && nalType != 8) {
nalarr.push(nal); // 过滤出不是pps和sps的帧
}
}
if (this.spspps == false) {
// console.log(nalType)
return;
}
} else {
if (nalType != 7 && nalType != 8) {
nalarr.push(nal); // 过滤出不是pps和sps的帧
}
}
let chunks = {
video: [],
audio: []
};
let chunks = {
video: [],
audio: [],
};
if (this.nalsbonAudioarr.length > 0) {
// debugger
let arr = [];
arr.push(this.nalsbonAudioarr.shift());
chunks.audio = arr;
this.remuxController.hasAudio = true;
}
if (this.nalsbonAudioarr.length > 0) {
// debugger
let arr = [];
arr.push(this.nalsbonAudioarr.shift());
chunks.audio = arr;
this.remuxController.hasAudio = true;
}
// console.log('nalarr',nalarr.length)
chunks.video = this.getVideoFrames(nalarr, 0);
if (chunks.video.length === 0) {
return;
}
this.remuxController.remux(chunks);
// console.log('nalarr',nalarr.length)
chunks.video = this.getVideoFrames(nalarr, 0);
if (chunks.video.length === 0) {
return;
}
PlayNalsReverse() {
this.remuxController.remux(chunks);
}
PlayNalsReverse() {
var nal = this.nalsboncarr.shift();
var nalType = nal[0] & 0x1f;
//debug.log('nalType==',nalType);
if (this.spspps === false) {
if (nalType == 7) {
this.spsnal = nal;
//debug.log('找到sps=');
return;
} else if (nalType == 8) {
this.ppsnal = nal;
//debug.log('找到pps=');
return;
}
var nal = this.nalsboncarr.shift();
var nalType = nal[0] & 0x1f;
//debug.log('nalType==',nalType);
if (this.spspps === false) {
if (nalType == 7) {
this.spsnal = nal;
//debug.log('找到sps=');
return;
}
else if (nalType == 8) {
this.ppsnal = nal;
//debug.log('找到pps=');
return;
}
if (this.spsnal != null && this.ppsnal != null) {
nalarr.push(this.spsnal);
nalarr.push(this.ppsnal);
nalarr.push(nal);
this.spspps = true;
}
if (this.spsnal != null && this.ppsnal != null) {
nalarr.push(this.spsnal);
nalarr.push(this.ppsnal);
nalarr.push(nal);
this.spspps = true;
}
if (this.spspps == false) {
console.log(nalType)
return;
}
if (this.spspps == false) {
console.log(nalType);
return;
}
} else {
if (nalType != 7 && nalType != 8) {
if (nalType == 1) {
// 非关键帧
this.nalsboncarrReverseCachQueue.unshift(nal);
return 0;
} else if (nalType == 5) {
// 关键帧
this.nalsboncarrReverseCachQueue.unshift(nal);
this.cacheNum++;
if (this.cacheNum > 1) {
this.cacheNum = 0;
return 1;
} else {
return 0;
}
}
else {
if (nalType != 7 && nalType != 8) {
if (nalType == 1) {
// 非关键帧
this.nalsboncarrReverseCachQueue.unshift(nal);
return 0
} else if (nalType == 5) {
// 关键帧
this.nalsboncarrReverseCachQueue.unshift(nal);
this.cacheNum++;
if (this.cacheNum > 1) {
this.cacheNum = 0;
return 1;
} else {
return 0;
}
}
}
}
}
}
let chunks = {
video: [],
audio: [],
};
let chunks = {
video: [],
audio: []
};
// console.log('nalarr',nalarr.length)
chunks.video = this.getVideoFrames(this.nalsboncarrReverseCachQueue, 0);
if (chunks.video.length === 0) {
return;
}
this.remuxController.remux(chunks);
this.nalsboncarrReverseCachQueue = [];
}
// console.log('nalarr',nalarr.length)
chunks.video = this.getVideoFrames(this.nalsboncarrReverseCachQueue, 0);
if (chunks.video.length === 0) {
return
}
this.remuxController.remux(chunks);
this.nalsboncarrReverseCachQueue = [];
/**
* 对每个bufferController执行doAppend()方法
*/
releaseBuffer() {
for (let type in this.bufferControllers) {
// console.log(type)
this.bufferControllers[type].doAppend();
}
}
/**
* 对每个bufferController执行doAppend()方法
*/
releaseBuffer() {
for (let type in this.bufferControllers) {
// console.log(type)
this.bufferControllers[type].doAppend();
}
clearBuffer() {
if (!this.clearBufferValidate) {
console.log(this.app.node.buffered.end(0) - this.app.node.currentTime);
return;
}
clearBuffer() {
if(!this.clearBufferValidate){
console.log(this.app.node.buffered.end(0) - this.app.node.currentTime)
return
// console.log('this.app.node.currentTime',this.app.node.currentTime, this.app.currentTime/1000-this.app.startTime)
if (this.app.node.buffered.length > 0) {
let bufferLength =
this.app.node.buffered.end(0) - this.app.node.currentTime;
if (bufferLength < 0) {
// console.log('this.app.node.buffered.end(0)',this.app.node.buffered.end(0))
// console.log('this.app.node.currentTime',this.app.node.currentTime)
// console.log('播超了')
this.app.node.pause();
} else if (bufferLength > 0 && bufferLength <= 3) {
let playPromise = this.app.node.play();
if (playPromise !== undefined) {
playPromise
.then(() => {
this.app.node.play();
})
.catch(() => {});
}
// console.log('this.app.node.currentTime',this.app.node.currentTime, this.app.currentTime/1000-this.app.startTime)
if (this.app.node.buffered.length > 0) {
let bufferLength = this.app.node.buffered.end(0) - this.app.node.currentTime;
if(bufferLength<0){
// console.log('this.app.node.buffered.end(0)',this.app.node.buffered.end(0))
// console.log('this.app.node.currentTime',this.app.node.currentTime)
// console.log('播超了')
this.app.node.pause();
}
else if (bufferLength > 0 && bufferLength <= 3) {
let playPromise = this.app.node.play()
if (playPromise !== undefined) {
playPromise.then(() => {
this.app.node.play()
}).catch(()=> {
})
}
// this.app.node.play();
this.app.node.playbackRate = 1;
}
else if (bufferLength > 2 && bufferLength <= 3) {
this.app.node.playbackRate = 1;
// this.app.node.play();
let playPromise = this.app.node.play()
if (playPromise !== undefined) {
playPromise.then(() => {
this.app.node.play()
}).catch(()=> {
})
}
}
else if (bufferLength > 3 && bufferLength <= 5) {
this.app.node.playbackRate = 1.25;
// this.app.node.play();
let playPromise = this.app.node.play()
if (playPromise !== undefined) {
playPromise.then(() => {
this.app.node.play()
}).catch(()=> {
})
}
// console.log('加速=1.25');
}
else if (bufferLength > 5 && bufferLength <= 7) {
this.app.node.playbackRate = 2;
// this.app.node.play();
let playPromise = this.app.node.play()
if (playPromise !== undefined) {
playPromise.then(() => {
this.app.node.play()
}).catch(()=> {
})
}
// console.log('加速=2');
}else if (bufferLength > 7 && bufferLength <= 9) {
this.app.node.playbackRate = 4;
// this.app.node.play();
let playPromise = this.app.node.play()
if (playPromise !== undefined) {
playPromise.then(() => {
this.app.node.play()
}).catch(()=> {
})
}
// console.log('加速=4',bufferLength);
}else if(bufferLength>9){
// console.log('bufferLength>9',bufferLength)
this.app.node.currentTime = this.app.node.buffered.end(0)-2
}
// else if (bufferLength > 9 && bufferLength <= 11) {
// this.app.node.playbackRate = 5;
// this.app.node.play();
// // console.log('加速=5',bufferLength);
// } else if (bufferLength > 11 && bufferLength <= 14) {
// this.app.node.playbackRate = 6;
// this.app.node.play();
// // console.log('加速=6',bufferLength);
// } else if (bufferLength > 14 && bufferLength <= 16) {
// this.app.node.playbackRate = 7;
// // console.log('加速=7',bufferLength);
// } else if (bufferLength > 16 && bufferLength <= 20) {
// this.app.node.playbackRate = 8;
// // console.log('加速=8',bufferLength);
// this.app.node.play();
// } else if (bufferLength > 20) {
// this.app.node.playbackRate = 15;
// // console.log('加速=16',bufferLength);
// this.app.node.play();
// }
// this.app.node.play();
this.app.node.playbackRate = 1;
} else if (bufferLength > 2 && bufferLength <= 3) {
this.app.node.playbackRate = 1;
// this.app.node.play();
let playPromise = this.app.node.play();
if (playPromise !== undefined) {
playPromise
.then(() => {
this.app.node.play();
})
.catch(() => {});
}
if (this.options.clearBuffer && (Date.now() - this.lastCleaningTime) > 3000) {
for (let type in this.bufferControllers) {
let cleanMaxLimit = this.getSafeBufferClearLimit(this.app.node.currentTime);
this.bufferControllers[type].initCleanup(cleanMaxLimit);
}
this.lastCleaningTime = Date.now();
} else if (bufferLength > 3 && bufferLength <= 5) {
this.app.node.playbackRate = 1.25;
// this.app.node.play();
let playPromise = this.app.node.play();
if (playPromise !== undefined) {
playPromise
.then(() => {
this.app.node.play();
})
.catch(() => {});
}
// console.log('加速=1.25');
} else if (bufferLength > 5 && bufferLength <= 7) {
this.app.node.playbackRate = 2;
// this.app.node.play();
let playPromise = this.app.node.play();
if (playPromise !== undefined) {
playPromise
.then(() => {
this.app.node.play();
})
.catch(() => {});
}
// console.log('加速=2');
} else if (bufferLength > 7 && bufferLength <= 9) {
this.app.node.playbackRate = 4;
// this.app.node.play();
let playPromise = this.app.node.play();
if (playPromise !== undefined) {
playPromise
.then(() => {
this.app.node.play();
})
.catch(() => {});
}
// console.log('加速=4',bufferLength);
} else if (bufferLength > 9) {
// console.log('bufferLength>9',bufferLength)
this.app.node.currentTime = this.app.node.buffered.end(0) - 2;
}
// else if (bufferLength > 9 && bufferLength <= 11) {
// this.app.node.playbackRate = 5;
// this.app.node.play();
// // console.log('加速=5',bufferLength);
// } else if (bufferLength > 11 && bufferLength <= 14) {
// this.app.node.playbackRate = 6;
// this.app.node.play();
// // console.log('加速=6',bufferLength);
// } else if (bufferLength > 14 && bufferLength <= 16) {
// this.app.node.playbackRate = 7;
// // console.log('加速=7',bufferLength);
// } else if (bufferLength > 16 && bufferLength <= 20) {
// this.app.node.playbackRate = 8;
// // console.log('加速=8',bufferLength);
// this.app.node.play();
// } else if (bufferLength > 20) {
// this.app.node.playbackRate = 15;
// // console.log('加速=16',bufferLength);
// this.app.node.play();
// }
}
// 倒放的时候清理过多的buffer
clearBufferReverse() {
// let bufferLength = - this.app.node.currentTime-this.app.node.buffered.start(0)
// console.log(bufferLength);
if (this.options.clearBuffer && (Date.now() - this.lastCleaningTime) > 3000) {
for (let type in this.bufferControllers) {
let cleanMaxLimit = this.getSafeBufferClearLimit(this.app.node.currentTime);
this.bufferControllers[type].initCleanup(cleanMaxLimit);
}
this.lastCleaningTime = Date.now();
}
if (this.options.clearBuffer && Date.now() - this.lastCleaningTime > 3000) {
for (let type in this.bufferControllers) {
let cleanMaxLimit = this.getSafeBufferClearLimit(
this.app.node.currentTime
);
this.bufferControllers[type].initCleanup(cleanMaxLimit);
}
this.lastCleaningTime = Date.now();
}
}
/**
*
* @param {*} nalus nal单元
* @param {*} duration
*/
getVideoFrames(nalus, duration) {
let nalu,
units = [],
samples = [],
naluObj,
sampleDuration,
adjustDuration = 0,
numberOfFrames = [];
// 倒放的时候清理过多的buffer
clearBufferReverse() {
// let bufferLength = - this.app.node.currentTime-this.app.node.buffered.start(0)
// console.log(bufferLength);
if (this.options.clearBuffer && Date.now() - this.lastCleaningTime > 3000) {
for (let type in this.bufferControllers) {
let cleanMaxLimit = this.getSafeBufferClearLimit(
this.app.node.currentTime
);
this.bufferControllers[type].initCleanup(cleanMaxLimit);
}
this.lastCleaningTime = Date.now();
}
}
for (nalu of nalus) {
naluObj = new NALU(nalu);
// debug.log('帧类型='+naluObj.type());
units.push(naluObj);
// NALU.IDR 5关键帧 NALU.NDR 1非关键帧
if (naluObj.type() === NALU.IDR || naluObj.type() === NALU.NDR) {
// console.log(naluObj.type())
samples.push({ units });
units = [];
if (this.options.clearBuffer) {
if (naluObj.type() === NALU.IDR) {
numberOfFrames.push(this.frameCounter);
}
this.frameCounter++;
}
}
}
/**
*
* @param {*} nalus nal单元
* @param {*} duration
*/
getVideoFrames(nalus, duration) {
let nalu,
units = [],
samples = [],
naluObj,
sampleDuration,
adjustDuration = 0,
numberOfFrames = [];
if (duration) {
sampleDuration = duration / samples.length | 0;
adjustDuration = (duration - (sampleDuration * samples.length));
} else {
sampleDuration = this.frameDuration;
}
samples.map((sample) => {
sample.duration = adjustDuration > 0 ? (sampleDuration + 1) : sampleDuration;
if (adjustDuration !== 0) {
adjustDuration--;
}
});
/* cache keyframe times if clearBuffer set true */
for (nalu of nalus) {
naluObj = new NALU(nalu);
// debug.log('帧类型='+naluObj.type());
units.push(naluObj);
// NALU.IDR 5关键帧 NALU.NDR 1非关键帧
if (naluObj.type() === NALU.IDR || naluObj.type() === NALU.NDR) {
// console.log(naluObj.type())
samples.push({ units });
units = [];
if (this.options.clearBuffer) {
numberOfFrames = numberOfFrames.map((total) => {
return (total * sampleDuration) / 1000;
});
this.keyframeCache = this.keyframeCache.concat(numberOfFrames);
if (naluObj.type() === NALU.IDR) {
numberOfFrames.push(this.frameCounter);
}
this.frameCounter++;
}
return samples;
}
}
getSafeBufferClearLimit(offset) {
let maxLimit = (this.options.mode === 'audio' && offset) || 0,
adjacentOffset;
if (duration) {
sampleDuration = (duration / samples.length) | 0;
adjustDuration = duration - sampleDuration * samples.length;
} else {
sampleDuration = this.frameDuration;
}
samples.map((sample) => {
sample.duration =
adjustDuration > 0 ? sampleDuration + 1 : sampleDuration;
if (adjustDuration !== 0) {
adjustDuration--;
}
});
for (let i = 0; i < this.keyframeCache.length; i++) {
if (this.keyframeCache[i] >= offset) {
break;
}
adjacentOffset = this.keyframeCache[i];
}
/* cache keyframe times if clearBuffer set true */
if (this.options.clearBuffer) {
numberOfFrames = numberOfFrames.map((total) => {
return (total * sampleDuration) / 1000;
});
this.keyframeCache = this.keyframeCache.concat(numberOfFrames);
}
return samples;
}
if (adjacentOffset) {
this.keyframeCache = this.keyframeCache.filter(keyframePoint => {
if (keyframePoint < adjacentOffset) {
maxLimit = keyframePoint;
}
return keyframePoint >= adjacentOffset;
});
}
getSafeBufferClearLimit(offset) {
let maxLimit = (this.options.mode === "audio" && offset) || 0,
adjacentOffset;
return maxLimit;
for (let i = 0; i < this.keyframeCache.length; i++) {
if (this.keyframeCache[i] >= offset) {
break;
}
adjacentOffset = this.keyframeCache[i];
}
if (adjacentOffset) {
this.keyframeCache = this.keyframeCache.filter((keyframePoint) => {
if (keyframePoint < adjacentOffset) {
maxLimit = keyframePoint;
}
return keyframePoint >= adjacentOffset;
});
}
return maxLimit;
}
}
export default H264Mp4Player
export default H264Mp4Player;

@@ -61,15 +61,3 @@ // import './libffmpeg_264_265.js';

setTimeout(() => {
// console.log("进setTimeout");
var _this = this;
// this.videoCallback = Module.addFunction(function (
// addr_y,
// addr_u,
// addr_v,
// stride_y,
// stride_u,
// stride_v,
// width,
// height,
// pts,
// id)
this.videoCallback = Module.addFunction(function (

@@ -91,3 +79,2 @@ nDataType,

};
//console.log(`videoCallback:width${width},长度:height${height}`)

@@ -110,2 +97,3 @@ // 处理音频

//nAudioCodecID 值区分音频编码类型:预留字段,暂未使用
console.log("调用c接口");
this.modulestart = Module._init_decoder(173, 0, this.videoCallback);

@@ -166,43 +154,2 @@ }, 2000);

}
// else if (naltype !== 1 && naltype != 19)
// {
// // 如果没有初始化 并且不是关键帧或非关键帧
// if (!this.naltypeObj[naltype])
// {
// this.naltypeObj[naltype] = true;
// try {
// var typedArray = new Uint8Array(videoBuffer);
// var size = typedArray.length;
// var cacheBuffer = Module._malloc(size);
// Module.HEAPU8.set(typedArray, cacheBuffer);
// console.log("_decodeData没有初始化的Size值:" + size);
// //var ret = Module._decodeData(cacheBuffer, size, this.pts++, 1);
// var ret = Module._decode_buffer(cacheBuffer, size);
// //console.log("_decodeData没有初始化解码后返回值:" + ret);
// } catch (e) {
// console.log(e);
// }
// if (cacheBuffer != null) {
// Module._free(cacheBuffer);
// cacheBuffer = null;
// }
// }
// if (Object.keys(this.naltypeObj).length == 4)
// {
// this.initialized = true;
// }
// }
// console.log(nalu)
// debugger
// var size = typedArray.length;
// var cacheBuffer = Module._malloc(size);
// Module.HEAPU8.set(typedArray, cacheBuffer);
// // console.time('decode')
// if(size < CHUNK_SIZE) {
// console.log('Flush frame data')
// Module._flushDecoder();
// Module._closeDecoder();
// }
}

@@ -214,4 +161,2 @@

var height = obj.height;
//console.log(`displayVideoFrame:width${width},长度:height${height}`);
var yLength = width * height;

@@ -241,4 +186,2 @@ var uvLength = (width / 2) * (height / 2);

this.isStart = false;
// this.modulestart
// Module._uninit_decoder();
}

@@ -245,0 +188,0 @@

@@ -180,2 +180,3 @@ import Event from "./util/event";

searchRecord(xmljson) {
console.log("searchRecord:", xmljson);
if (Array.isArray(xmljson)) {

@@ -634,20 +635,37 @@ this.configs.multiServer = xmljson.length;

playForward() {
var playxml = xmlPlayRecord;
var playxmlBody = BLoadXML(playxml);
playxmlBody.getElementsByTagName("Speed")[0].firstChild.nodeValue =
this.speed;
var playtime = this.currentTime;
playxmlBody.getElementsByTagName("StartTime")[0].firstChild.nodeValue =
playtime;
if (this._player) {
//wuyage 解决无录像时正放报错问题
var playxml = xmlPlayRecord;
var playxmlBody = BLoadXML(playxml);
playxmlBody.getElementsByTagName("Speed")[0].firstChild.nodeValue =
this.speed;
var playtime = this.currentTime;
playxmlBody.getElementsByTagName("StartTime")[0].firstChild.nodeValue =
playtime;
playxmlBody.getElementsByTagName("EndTime")[0].firstChild.nodeValue =
this.recordEndTime / 1000;
playxmlBody.getElementsByTagName("ChannelID")[0].firstChild.nodeValue =
this.channelId;
this.webSocketController.playControl(Xml2String(playxmlBody));
playxmlBody.getElementsByTagName("EndTime")[0].firstChild.nodeValue =
this.recordEndTime / 1000;
playxmlBody.getElementsByTagName("ChannelID")[0].firstChild.nodeValue =
this.channelId;
playxmlBody.getElementsByTagName("DeviceType")[0].firstChild.nodeValue =
this.DeviceType;
playxmlBody.getElementsByTagName("RecorderType")[0].firstChild.nodeValue =
this.RecorderType;
playxmlBody.getElementsByTagName("GBSServerIP")[0].firstChild.nodeValue =
this.GBSServerIP;
playxmlBody.getElementsByTagName(
"GBSServerPort"
)[0].firstChild.nodeValue = this.GBSServerPort;
playxmlBody.getElementsByTagName("MDSServerIP")[0].firstChild.nodeValue =
this.MDSServerIP;
playxmlBody.getElementsByTagName(
"MDSServerPort"
)[0].firstChild.nodeValue = this.MDSServerPort;
this.webSocketController.playControl(Xml2String(playxmlBody));
if (this._player.reversePlayInterval) {
this._player.clearReverseInterval();
this.reverse = false;
this.node.play();
if (this._player.reversePlayInterval) {
this._player.clearReverseInterval();
this.reverse = false;
this.node.play();
}
}

@@ -660,15 +678,18 @@ }

reversePlay() {
this.reverse = true;
var playxml = xmlPlayReverse;
var playxmlBody = BLoadXML(playxml);
playxmlBody.getElementsByTagName("Speed")[0].firstChild.nodeValue =
this.speed * 1;
// playxmlBody.getElementsByTagName('EndTime')[0].firstChild.nodeValue = this.currentTime;
playxmlBody.getElementsByTagName("StartTime")[0].firstChild.nodeValue =
this.recordInfo.recordStartTime;
// playxmlBody.getElementsByTagName('CmdType')[0].firstChild.nodeValue = 'ReversePlay';
// playxmlBody.getElementsByTagName('ChannelID')[0].firstChild.nodeValue = this.channelId;
this.webSocketController.playControl(Xml2String(playxmlBody));
// 向播放器发出倒放的指令
this._player.playReverse();
if (this._player) {
//wuyage 解决无录像时正放报错问题
this.reverse = true;
var playxml = xmlPlayReverse;
var playxmlBody = BLoadXML(playxml);
playxmlBody.getElementsByTagName("Speed")[0].firstChild.nodeValue =
this.speed * 1;
// playxmlBody.getElementsByTagName('EndTime')[0].firstChild.nodeValue = this.currentTime;
playxmlBody.getElementsByTagName("StartTime")[0].firstChild.nodeValue =
this.recordInfo.recordStartTime;
// playxmlBody.getElementsByTagName('CmdType')[0].firstChild.nodeValue = 'ReversePlay';
// playxmlBody.getElementsByTagName('ChannelID')[0].firstChild.nodeValue = this.channelId;
this.webSocketController.playControl(Xml2String(playxmlBody));
// 向播放器发出倒放的指令
this._player.playReverse();
}
}

@@ -684,2 +705,3 @@

playxmlBody.getElementsByTagName("StartTime")[0].firstChild.nodeValue = 0;
console.log("逐帧播放:", Xml2String(playxmlBody));
this.webSocketController.playControl(Xml2String(playxmlBody));

@@ -686,0 +708,0 @@ }

//对于传进来的configs中的各个值进行校验
export function validateConfigs(configs) {
const { videoId } = configs;
console.log("videoId:");
console.log(videoId)
if (!videoId || typeof videoId !== 'string') {
throw new Error("videoId配置出错");
} else {
this.node = document.getElementById(videoId);
const { videoId } = configs;
console.log("videoId:", videoId);
if (!videoId || typeof videoId !== "string") {
throw new Error("videoId配置出错");
} else {
this.node = document.getElementById(videoId);
window.videoNode = this.node;
if(!this.node){
throw new Error('没有找到对应的video标签');
}
this.node.muted = true
this.node.muted = false
window.videoNode = this.node;
if (!this.node) {
throw new Error("没有找到对应的video标签");
}
this.node.muted = true;
this.node.muted = false;
}
}
export function validateChannelInfo(info) {
return true;
return true;
}

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc