anxiao_web/pages/cmddispatch/js/popSosVideo.js

407 lines
15 KiB
JavaScript
Raw Normal View History

2024-07-24 09:22:32 +08:00
import { layuiObj } from "./index.js";
import { sosSdk } from "./sosVideoAndAudio.js";
var sosVideoIndex = null;
var currentSosVideo = null;
var sosAlarmVideo = {};
function popSosVideo(obj) {
currentSosVideo = JSON.parse(JSON.stringify(obj));
sosVideoIndex = layuiObj["layer"].open({
type: 1,
title: false, //不显示标题栏
closeBtn: false,
area: ['640px', '480px'],
shade: 0.4,
id: new Date().getTime(), //设定一个id防止重复弹出
btnAlign: 'c',
move: '.layui-move',
moveType: 1, //拖拽模式0或者1
content: `<div class="popWindows">
<div class="popInfoContent">
<div class="popInfoTitle">
<span class="layui-move">${currentSosVideo["pointAddress"]}</span>
<img src="./pages/cmddispatch/image/closeInfoWindow.png">
</div>
<div id="sosVideoContent">
<video id="sosVideo" autoplay="" controls muted=""></video>
<audio id='sosAudio' autoplay="" /> </audio>
<div class="sosVideoOpera">
<div class="sosVideoOperaTime">
<p>视频观看剩余时间<span class="sosVideoTime">0</span></p>
<p>
<span></span>
<span>正在录制</span>
</p>
</div>
<div class="sosVideoOperaTool">
<p class="sosVideoMonitoring" state="open">
<img src="./pages/cmddispatch/sosVideo/stopMonitor.png" alt="/">
<span>监听</span>
</p>
<p class="sosVideoTalk" state="open">
<img src="./pages/cmddispatch/sosVideo/stopTalk.png" alt="/">
<span>对讲</span>
</p>
<p class="sosVideoRecording" state="open">
<img src="./pages/cmddispatch/sosVideo/stopRecord.png" alt="/">
<span>录制</span>
</p>
</div>
</div>
</div>
</div>
</div>`,
success: function () {
sosVideo();
addVideoEvent();
}
});
$(".popInfoTitle>img").on('click', function () {
layuiObj["layer"].close(sosVideoIndex)
sosVideoIndex = null;
closeSosVideo();
stopAudioListen();
sosAudioStop();
})
}
function addVideoEvent() {
$(".sosVideoMonitoring").on('click', function () {
//如果对讲开着,需要去关闭对讲
if ("audioListenTaskCode" in currentSosVideo) {
$(".sosVideoTalk").attr("state","open");
$(".sosVideoTalk>span").css("color", "#fff");
$(".sosVideoTalk>img").attr("src","./pages/cmddispatch/sosVideo/stopTalk.png");
sosAudioStop();
}
var state = $(this).attr("state");
sosBtnOperaSlide(this);
if (state == "open") {
$(this).children("img").attr("src", "./pages/cmddispatch/sosVideo/startMonitor.png");
startAudioListen();
}else if(state == "close") {
$(this).children("img").attr("src", "./pages/cmddispatch/sosVideo/stopMonitor.png");
stopAudioListen();
}
})
$(".sosVideoTalk").on('click', function () {
//如果监听开着需要去关闭监听
if ("audioListenTaskCode" in currentSosVideo) {
$(".sosVideoMonitoring").attr("state","open");
$(".sosVideoMonitoring>span").css("color", "#fff");
$(".sosVideoMonitoring>img").attr("src","./pages/cmddispatch/sosVideo/stopMonitor.png");
stopAudioListen();
}
var state = $(this).attr("state");
sosBtnOperaSlide(this);
if (state == "open") {
$(this).children("img").attr("src", "./pages/cmddispatch/sosVideo/startTalk.png");
sosAudioStart();
} else if (state == "close") {
$(this).children("img").attr("src", "./pages/cmddispatch/sosVideo/stopTalk.png");
sosAudioStop();
}
})
$(".sosVideoRecording").on('click', function () {
var state = $(this).attr("state");
sosBtnOperaSlide(this);
if (state == "open") {
$(this).children("img").attr("src", "./pages/cmddispatch/sosVideo/startRecord.png");
$(".sosVideoOperaTime>p:last-child").css("display", "block");
startRecording();
} else if (state == "close") {
$(this).children("img").attr("src", "./pages/cmddispatch/sosVideo/stopRecord.png");
$(".sosVideoOperaTime>p:last-child").css("display", "none");
stopRecording();
}
})
}
function sosBtnOperaSlide(that){
var state = $(that).attr("state");
if (state == "open") {
$(that).attr("state", "close");
$(that).children("span").css("color", "#01E9FD");
} else if (state == "close") {
$(that).attr("state", "open");
$(that).children("span").css("color", "#fff");
}
}
//开始录像
var recordBlob = [];
async function startRecording() {
const stateCb = (videoDomId, state) => {
// 视频录制状态回调函数videoDomId-->启动时传入的video标签id state有'inprogress', 'failed', 'finished' 3种状态
console.log('videoDomId = %s, state = %s', videoDomId, state);
}
const dataCb = (videoDomId, blob) => {
// 视频录制数据回调函数该回调会1s调用一次把这1s录制的视频数据通过 blob参数传入回调blob为js的Blob对象类型
// 这里进行数据保存或者转发相关操作
console.log(videoDomId, blob);
recordBlob.push(blob)
}
sosSdk.videoRecordStart({
// 必选参数
VideoDomId: 'sosVideo',
StateCb: stateCb,
DataCb: dataCb,
}).then((rsp) => {
layuiObj.layer.msg("视频录制启动成功",
{
color:"#fff"
}
);
}).catch((e) => {
layuiObj.layer.msg("视频录制启动失败",
{
color:"#fff"
});
})
}
//结束录像
async function stopRecording() {
sosSdk.videoRecordStop({ VideoDomId: 'sosVideo' }).then(() => {
var tmpBlob = new Blob(recordBlob, { type: 'video/webm' });
// 创建 URL 对象
const url = URL.createObjectURL(tmpBlob);
// 创建链接
const link = document.createElement('a');
link.href = url;
link.download = "录像.mp4";
// 模拟点击链接进行下载
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}).catch((error) => {
console.error('停止视频录制失败', error);
})
}
//显示时间
var showTimeTimer=null;
function showVideoTime() {
var ExpireIn = Math.floor(currentSosVideo["task"]["ExpireIn"]);
$(".sosVideoTime").html(ExpireIn+"秒");
showTimeTimer = setInterval(() => {
if (ExpireIn <= 0) {
closeVideoClearTime();
}
ExpireIn--;
if (ExpireIn <= 0) {
ExpireIn = 0;
}
$(".sosVideoTime").html(ExpireIn+"秒");
}, 1000)
}
//关闭定时器
function closeVideoClearTime(){
if(showTimeTimer!=null){
clearInterval(showTimeTimer)
showTimeTimer=null;
}
}
//开始监听
async function startAudioListen() {
const disconnectedCallback = (SipNum) => {
// 开始监听之后,有设备或自己退出监听
console.warn(SipNum, '已退出监听', sosSdk && sosSdk.isMe(SipNum));
}
const stopCallback = () => {
// 开始监听之后,服务器结束了监听
console.warn('服务器结束监听');
}
sosSdk.audioListenStart({
// 必填参数
DeviceId: parseInt(currentSosVideo["deviceId"]),
AudioDomId: 'sosAudio',
// 非必填参数
Force: true,
OnDisconnected: disconnectedCallback,
OnStop: stopCallback,
}).then((ret) => {
currentSosVideo["audioListenTaskCode"] = ret.TaskCode
layuiObj.layer.msg("监听开启成功",
{
color:"#fff"
});
}).catch((e) => {
console.log('监听失败', e)
switch (e.code) {
case 2005:
layuiObj.layer.msg('监听失败设备不存在检查一下SipNum或DeviceId是否错误')
break;
case 3551:
layuiObj.layer.msg('监听失败,本端坐席忙(本端已在另外的一个会话任务中)')
break;
case 3552:
layuiObj.layer.msg('监听失败,设备忙(设备已在另外的一个会话任务中),被其他人坐席端对讲/监听了')
break;
default:
break;
}
})
}
//结束监听
async function stopAudioListen() {
if ("audioListenTaskCode" in currentSosVideo) {
const rsp = await sosSdk.audioListenStop({
TaskCode: currentSosVideo["TaskCode"]
});
delete currentSosVideo["audioListenTaskCode"]
//sosSdk.audioListenStop({
// TaskCode: currentSosVideo["audioListenTaskCode"]
//}).then(() => {
// delete currentSosVideo["audioListenTaskCode"]
// console.log('停止监听成功');
//}).catch((error) => {
// console.log('停止监听失败', error);
//})
}
}
//开始对讲
function sosAudioStart() {
if (sosSdk) {
let disconnectedCallback = (SipNum) => {
// 开始对讲之后,有设备或自己退出对讲
console.warn(SipNum, "已退出对讲", sosSdk && sosSdk.isMe(SipNum));
}
let stopCallback = () => {
// 开始对讲之后,服务器结束了对讲
console.warn("服务器结束对讲");
}
sosSdk.audioSpeakStart({
// 必填参数
DeviceId: parseInt(currentSosVideo["deviceId"]),
AudioDomId: "sosAudio", // 要保证document.getElementById("html_audio_remote")非null你的audio标签一定要存在
// 非必填参数
Force: true,
OnDisconnected: disconnectedCallback,
OnStop:stopCallback,
}).then((ret) => {
currentSosVideo["audioSpeakTaskCode"] = ret.TaskCode
layuiObj.layer.msg("对讲开启成功",
{icon: 1});
}).catch((e) => {
switch (e.code) {
case 2005:
layuiObj.layer.msg('对讲失败设备不存在检查一下SipNum或DeviceId是否错误',{icon: 2})
break;
case 3551:
layuiObj.layer.msg('对讲失败,本端坐席忙(本端已在另外的一个对讲任务中)',{icon: 2})
break;
case 3552:
layuiObj.layer.msg('对讲失败,设备忙(设备已在另外的一个对讲任务中),被其他人坐席端对讲了',{icon: 2})
break;
default:
break;
}
})
} else {
alert("对讲失败,请先启动SDK",{icon: 2})
}
}
// 结束对讲
async function sosAudioStop() {
if ("audioSpeakTaskCode" in currentSosVideo) {
if (sosSdk) {
const rsp = await sosSdk.audioSpeakStop({
TaskCode: currentSosVideo["TaskCode"]
});
delete currentSosVideo["audioSpeakTaskCode"]
//sosSdk.audioSpeakStop({
// TaskCode: currentSosVideo["audioSpeakTaskCode"]
//}).then(() => {
// delete currentSosVideo["audioSpeakTaskCode"]
// console.log("对讲停止成功");
//}).catch((error) => {
// console.log("对讲停止失败");
//})
}else{
console.log("对讲停止失败请先启动SDK")
}
}
}
//调用视频的
async function sosVideo() {
const stopCallback = () => {
// 开始监控之后,服务器结束了监控
console.log('监控结束');
}
try {
const task = await sosSdk.videoMonitorStart({
DeviceId: parseInt(currentSosVideo["deviceId"]),
VideoDomId: 'sosVideo',
OnStop: stopCallback,
});
if (!("task" in currentSosVideo)) {
currentSosVideo["task"] = task;
}
showVideoTime();
} catch (error) {
layer.msg("获取监控视频失败",{icon: 2});
}
}
//关闭视频
async function closeSosVideo() {
if("task" in currentSosVideo){
try {
const rsp = await sosSdk.videoMonitorStop({
TaskCode: currentSosVideo["task"]["TaskCode"]
});
delete currentSosVideo["task"];
closeVideoClearTime();
} catch (error) {
console.log('停止监控视频失败', error)
}
}
}
//调用告警的视频
async function openSosAlarmVideo(obj) {
console.log(obj);
var deviceId=obj["deviceId"];
const stopCallback = () => {
// 开始监控之后,服务器结束了监控
console.log('监控结束')
}
try {
const task = await sosSdk.videoMonitorStart({
DeviceId:parseInt(deviceId),
VideoDomId: `sosAlarmVideo${deviceId}`,
OnStop: stopCallback,
});
if (!(deviceId in sosAlarmVideo)) {
sosAlarmVideo[deviceId] = task.TaskCode
}
// console.log(currentSosVideo);
// console.log('获取监控视频成功 taskCode:', task.TaskCode)
} catch (error) {
console.log('获取监控视频失败', error)
}
}
//调用告警关闭视频
async function closeSosAlarmVideo(deviceId) {
try {
const rsp = await sosSdk.videoMonitorStop({
TaskCode: currentSosVideo["TaskCode"]
});
if (deviceId in sosAlarmVideo) {
delete sosAlarmVideo[deviceId]
}
console.log('停止监控视频成功')
} catch (error) {
console.log('停止监控视频失败', error)
}
}
export {
popSosVideo,
openSosAlarmVideo,
closeSosAlarmVideo
}