播放h265流解決方案
文中代碼:https://github.com/moxiefxj/video_vue
1. 視頻WEB插件
-
視頻WEB插件,下載的壓縮包中含有播放監(jiān)控所需要的插件九串、開(kāi)發(fā)的demo等。
- 在項(xiàng)目中具體的實(shí)現(xiàn)(以vue組件實(shí)現(xiàn)為例)
<template>
<!--視頻窗口展示-->
<div :id="id"></div>
</template>
<script>
export default {
name: "hkVideo",
data() {
return {
// 視頻播放
initCount: 0, // 初始化重試次數(shù),3次失敗則報(bào)錯(cuò)
oWebControl: null, // 頁(yè)面控制對(duì)象
pubKey: "",
cameraIp: "xxx.xxx.xx.xx", // 綜合安防管理平臺(tái)IP地址
};
},
props: ["code", "id"], // 接收父頁(yè)面?zhèn)鱽?lái)的攝像頭 code
methods: {
// 創(chuàng)建播放實(shí)例
initPlugin() {
// 初始化播放插件
const _this = this;
_this.oWebControl = new WebControl({
szPluginContainer: _this.id, // 指定容器id
iServicePortStart: 15900, // 指定起止端口號(hào)循集,建議使用該值
iServicePortEnd: 15909,
szClassId: "23BF3B0A-2C56-4D97-9C03-0CB103AA8F11", // 用于IE10使用ActiveX的clsid
cbConnectSuccess: function () {
// 創(chuàng)建WebControl實(shí)例成功
console.log("創(chuàng)建WebControl實(shí)例成功");
_this.oWebControl
.JS_StartService("window", {
// WebControl實(shí)例創(chuàng)建成功后需要啟動(dòng)服務(wù)
dllPath: "./VideoPluginConnect.dll", // 值"./VideoPluginConnect.dll"寫(xiě)死
})
.then(
function () {
// 啟動(dòng)插件服務(wù)成功
console.log("啟動(dòng)插件服務(wù)成功");
_this.oWebControl.JS_SetWindowControlCallback({
// 設(shè)置消息回調(diào)
cbIntegrationCallBack: _this.cbIntegrationCallBack,
});
let divWidth = document.getElementById(_this.id).scrollWidth;
let divHeight = document.getElementById(_this.id).scrollHeight;
_this.oWebControl
.JS_CreateWnd(_this.id, divWidth, divHeight)
.then(function () {
//JS_CreateWnd創(chuàng)建視頻播放窗口掸哑,寬高可設(shè)定
_this.init(); // 創(chuàng)建播放實(shí)例成功后初始化
});
},
function () {
// 啟動(dòng)插件服務(wù)失敗
}
);
},
cbConnectError: function () {
// 創(chuàng)建WebControl實(shí)例失敗
_this.oWebControl = null;
console.log("warn,插件未啟動(dòng),正在嘗試啟動(dòng)筋栋,請(qǐng)稍候...");
WebControl.JS_WakeUp("VideoWebPlugin://"); // 程序未啟動(dòng)時(shí)執(zhí)行error函數(shù)炊汤,采用wakeup來(lái)啟動(dòng)程序
initCount++;
if (initCount < 3) {
setTimeout(function () {
_this.initPlugin();
}, 3000);
} else {
console.log("error, 插件啟動(dòng)失敗,請(qǐng)檢查插件是否安裝!");
}
},
cbConnectClose: function (bNormalClose) {
// 異常斷開(kāi):bNormalClose = false
// JS_Disconnect正常斷開(kāi):bNormalClose = true
if (!bNormalClose) {
console.log("error, 視屏鏈接異常中斷弊攘!");
}
_this.oWebControl = null;
},
});
},
//初始化, 主要改這里
init() {
const _this = this;
this.getPubKey(function () {
// 請(qǐng)自行修改以下變量值
let appkey = "xxxxxxx"; //綜合安防管理平臺(tái)提供的appkey抢腐,必填
let secret = _this.setEncrypt("xxxxxxxxxx"); //綜合安防管理平臺(tái)提供的secret,必填
let ip = _this.cameraIp; //綜合安防管理平臺(tái)IP地址襟交,必填
let playMode = 0; //初始播放模式:0-預(yù)覽迈倍,1-回放
let port = 8443; //綜合安防管理平臺(tái)端口,若啟用HTTPS協(xié)議捣域,默認(rèn)443
let snapDir = "D:\\SnapDir"; //抓圖存儲(chǔ)路徑
let videoDir = "D:\\VideoDir"; //緊急錄像或錄像剪輯存儲(chǔ)路徑
let layout = "1x1"; //playMode指定模式的布局
let enableHTTPS = 1; //是否啟用HTTPS協(xié)議與綜合安防管理平臺(tái)交互啼染,這里總是填1
let encryptedFields = "secret"; //加密字段,默認(rèn)加密領(lǐng)域?yàn)閟ecret
let showToolbar = 0; //是否顯示工具欄焕梅,0-不顯示迹鹅,非0-顯示
let showSmart = 1; //是否顯示智能信息(如配置移動(dòng)偵測(cè)后畫(huà)面上的線框),0-不顯示贞言,非0-顯示
let buttonIDs = ""; //自定義工具條按鈕
// 請(qǐng)自行修改以上變量值
_this.oWebControl
.JS_RequestInterface({
funcName: "init",
argument: JSON.stringify({
appkey: appkey, //API網(wǎng)關(guān)提供的appkey
secret: secret, //API網(wǎng)關(guān)提供的secret
ip: ip, //API網(wǎng)關(guān)IP地址
playMode: playMode, //播放模式(決定顯示預(yù)覽還是回放界面)
port: port, //端口
snapDir: snapDir, //抓圖存儲(chǔ)路徑
videoDir: videoDir, //緊急錄像或錄像剪輯存儲(chǔ)路徑
layout: layout, //布局
enableHTTPS: enableHTTPS, //是否啟用HTTPS協(xié)議
encryptedFields: encryptedFields, //加密字段
showToolbar: showToolbar, //是否顯示工具欄
showSmart: showSmart, //是否顯示智能信息
buttonIDs: buttonIDs, //自定義工具條按鈕
}),
})
.then((oData) => {
let divWidth = document.getElementById(_this.id).scrollWidth;
let divHeight = document.getElementById(_this.id).scrollHeight;
_this.oWebControl.JS_Resize(divWidth, divHeight); // 初始化后resize一次斜棚,規(guī)避firefox下首次顯示窗口后插件窗口未與DIV窗口重合問(wèn)題
_this.startPreview();
});
});
},
//獲取公鑰, 不用改
getPubKey(callback) {
this.oWebControl
.JS_RequestInterface({
funcName: "getRSAPubKey",
argument: JSON.stringify({
keyLength: 1024,
}),
})
.then((oData) => {
if (oData.responseMsg.data) {
this.pubKey = oData.responseMsg.data;
callback();
}
});
},
//RSA加密, 不用改
setEncrypt(value) {
let encrypt = new JSEncrypt();
encrypt.setPublicKey(this.pubKey);
return encrypt.encrypt(value);
},
//視頻預(yù)覽功能, 就設(shè)置 cameraIndexCode 就行了
startPreview() {
let _this = this;
let cameraIndexCode = _this.code; //獲取輸入的監(jiān)控點(diǎn)編號(hào)值,必填
let streamMode = 0; //主子碼流標(biāo)識(shí):0-主碼流该窗,1-子碼流
let transMode = 1; //傳輸協(xié)議:0-UDP弟蚀,1-TCP
let gpuMode = 0; //是否啟用GPU硬解,0-不啟用酗失,1-啟用
let wndId = -1; //播放窗口序號(hào)(在2x2以上布局下可指定播放窗口)
cameraIndexCode = cameraIndexCode.replace(/(^\s*)/g, "");
cameraIndexCode = cameraIndexCode.replace(/(\s*$)/g, "");
_this.oWebControl.JS_RequestInterface({
funcName: "startPreview",
argument: JSON.stringify({
cameraIndexCode: cameraIndexCode, //監(jiān)控點(diǎn)編號(hào)
streamMode: streamMode, //主子碼流標(biāo)識(shí)
transMode: transMode, //傳輸協(xié)議
gpuMode: gpuMode, //是否開(kāi)啟GPU硬解
wndId: wndId, //可指定播放窗口
}),
});
},
//停止全部預(yù)覽
stopAlldwPreview() {
this.oWebControl.JS_RequestInterface({
funcName: "stopAllPreview",
});
},
destroyedView() {
if (this.oWebControl != null) {
this.stopAlldwPreview();
this.oWebControl.JS_HideWnd(); // 先讓窗口隱藏义钉,規(guī)避可能的插件窗口滯后于瀏覽器消失問(wèn)題
this.oWebControl.JS_Disconnect().then(
function () {
// 斷開(kāi)與插件服務(wù)連接成功
},
function () {
// 斷開(kāi)與插件服務(wù)連接失敗
}
);
}
},
},
mounted() {
// 有攝像頭 code, 才加載, 另外因?yàn)槲颐看沃伙@示一個(gè), 所以顯示之前要把之前顯示的攝像頭銷毀, 所以加了 this.oWebControl == null 的判斷.
if (this.cameraIndexCode && this.oWebControl == null) {
this.initPlugin();
} else {
// 如果 code 不為空, 則先銷毀現(xiàn)有攝像頭, 再去加載新的攝像頭
this.destroyedView();
setTimeout(this.initPlugin(), 1000);
}
},
destroyed() {
this.destroyedView();
},
};
</script>
2. h5player播放
-
H5視頻播放器開(kāi)發(fā)包,開(kāi)發(fā)包中含有demo和開(kāi)發(fā)文檔
- 需要先將開(kāi)發(fā)包中的js導(dǎo)入到項(xiàng)目根目錄中
- 相關(guān)組件代碼示例:
<template>
<div :id="id"></div>
</template>
<script>
export default {
data() {
return {
oPlugin: null,
iWind: 0,
curIndex: 0,
};
},
props: ["id", "code"],
mounted() {
this.init();
},
methods: {
init() {
this.oPlugin = new JSPlugin({
szId: this.id,
szBasePath: "./",
oStyle: {
border: "#343434",
borderSelect: "#FFCC00",
background: "#000",
},
openDebug: true,
});
this.initPlugin();
},
initPlugin() {
this.oPlugin.JS_SetWindowControlCallback({
windowEventSelect: function (iWndIndex) {
//插件選中窗口回調(diào)
this.iWind = iWndIndex;
console.log(iWndIndex);
},
pluginErrorHandler: function (iWndIndex, iErrorCode, oError) {
//插件錯(cuò)誤回調(diào)
console.error(
`window-${iWndIndex}, errorCode: ${iErrorCode}`,
oError
);
},
windowEventOver: function (iWndIndex) {
//鼠標(biāo)移過(guò)回調(diào)
//console.log(iWndIndex);
},
windowEventOut: function (iWndIndex) {
//鼠標(biāo)移出回調(diào)
//console.log(iWndIndex);
},
windowEventUp: function (iWndIndex) {
//鼠標(biāo)mouseup事件回調(diào)
//console.log(iWndIndex);
},
windowFullCcreenChange: function (bFull) {
//全屏切換回調(diào)
console.log(bFull);
},
firstFrameDisplay: function (iWndIndex, iWidth, iHeight) {
//首幀顯示回調(diào)
console.log(iWndIndex, iWidth, iHeight);
},
performanceLack: function () {
//性能不足回調(diào)
console.log("ddiwduw");
},
});
this.oPlugin
.JS_SetOptions({
// bSupportSound: false //是否支持音頻,默認(rèn)支持
// bSupporDoubleClickFull: false //是否雙擊窗口全屏级零,默認(rèn)支持
// bOnlySupportMSE: true //只支持MSE
// bOnlySupportJSDecoder: true //只支持JSDecoder
})
.then(() => {
// 這里需要請(qǐng)求后端地址 最好為ws流.
this.realplay();
});
},
realplay() {
let url = "ws://xxxxxxxxxxxxxxx";
this.oPlugin
.JS_Play(
url,
{
playURL: url,
mode: 1,
},
this.curIndex
)
.then(
function () {
console.log("realplay success");
},
function () {
console.log("realplay failed");
}
);
},
resizePlay(index) {
this.oPlugin.JS_Resize(500, 300).then(
() => {
console.info("JS_Resize success");
// do you want...
},
(err) => {
console.info("JS_Resize failed");
// do you want...
}
);
},
},
};
</script>