zhangzengfei
2022-07-20 4a800a8fc83c6bd1f86a8e847b079a51a7532c09
src/components/wasmPlayer/index.vue
@@ -1,85 +1,52 @@
<template>
  <div class="video-player">
    <canvas
      v-show="showArea"
      id="area-canvas"
      ref="areaCanvas"
      width="960"
      height="540"
    ></canvas>
    <canvas v-show="showArea" id="area-canvas" ref="areaCanvas" width="960" height="540"></canvas>
    <div class="container" id="videoPlayer">
      <div class="canvasDiv">
        <div
          v-loading="videoLoading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          style="position: unset !important"
        ></div>
          <canvas
          ref="playCanvas"
          id="paly-canvas"
          width="960"
          height="540"
        ></canvas>
        ></div>
        <canvas ref="playCanvas" id="paly-canvas" width="960" height="540"></canvas>
      </div>
       <transition name="fade">
       <div class="popuptext" id="myPopup" v-if="fullScreenNotice">资源下载中,请稍后...</div>
       </transition>
      <transition name="fade">
        <div class="popuptext" id="myPopup" v-if="fullScreenNotice">
          资源下载中,请稍后...
        </div>
      </transition>
      <!-- 控制条 -->
      <section
        class="jsmodern-video-panel"
        :style="{ display: isStream ? 'none' : 'block' }"
      >
      <section class="jsmodern-video-panel" :style="{ display: isStream ? 'none' : 'block' }">
        <!-- 播放/暂停 -->
        <b
          :class="
            playerStatus == 0 ? 'jsmodern-video-play' : 'jsmodern-video-pause'
          "
          @click="playVideo"
        ></b>
        <b :class="playerStatus == 0 ? 'jsmodern-video-play' : 'jsmodern-video-pause'" @click="playVideo"></b>
        <!-- 时间 -->
        <span class="jsmodern-video-start" ref="timeLabel"
          >00:00:00/00:00:00</span
        >
        <span class="jsmodern-video-start" ref="timeLabel">00:00:00/00:00:00</span>
        <!-- 进度条 -->
        <div>
          <input
            class="jsmodern-video-linebox"
            ref="timeTrack"
            type="range"
            value="0"
          />
          <input class="jsmodern-video-linebox" ref="timeTrack" type="range" value="0" />
        </div>
        <!-- 声音 -->
        <b class="jsmodern-video-volume"></b>
        <!-- <b class="jsmodern-video-volume"></b> -->
        <!-- <div class="jsmodern-video-volumebox">
          <div class="jsmodern-video-volumeline"></div>
          <b class="jsmodern-video-volumedot"></b>
        </div> -->
        <!-- 全屏 -->
        <b
          class="jsmodern-video-fullin"
          @click="fullScreen"
          :disable="false"
        ></b>
        <b class="jsmodern-video-fullin" @click="fullScreen" :disable="false"></b>
        <!-- 下载 -->
        <i
         class="el-icon-download jsmodern-video-download"
         @click="downLoad"
        ></i>
        <i class="el-icon-download jsmodern-video-download" @click="downLoad" v-show="!isStream"></i>
      </section>
      <!-- 大播放按钮 -->
      <div v-show="!isStream">
        <span class="video-btn" v-show="playerStatus == 0" @click="playVideo"
          ><img src="./wasm/img/bo1.png"
        /></span>
        <span class="video-btn" v-show="playerStatus == 0" @click="playVideo"><img src="./wasm/img/bo1.png"/></span>
        <!-- 上一个 -->
        <span class="video-prve" v-show="showPrev">
@@ -93,7 +60,7 @@
        <!-- 播放失败 -->
        <span class="video-error" v-show="playerStatus == -1">
          <i class="el-icon-warning-outline" style="font-size:40px"></i>
          <i class="el-icon-warning-outline" style="font-size: 40px"></i>
          <br />
          视频加载失败 无效的视频地址
        </span>
@@ -106,80 +73,77 @@
/*
  2021.09.22 添加多个录像地址的处理, 与录像模块约定, 用 || 分割多个视频地址, 前端处理播放.
*/
import { Player } from './wasm/player'
import VideoRuleData from '@/Pool/VideoRuleData'
import { getAllPolygon } from '@/api/polygon'
import { Player } from "./wasm/player"
import VideoRuleData from "@/Pool/VideoRuleData"
import { getAllPolygon } from "@/api/polygon"
export default {
  name: 'CameraPlayer',
  name: "CameraPlayer",
  props: {
    videoUrl: {
      type: String,
      // default: "ws://192.168.1.182:10101/ws"
      default: `${location.protocol === 'https' ? 'wss' : 'ws'}://${
        location.host
      }/ws`,
      default: `${location.protocol === "https" ? "wss" : "ws"}://${location.host}/ws`
    },
    cameraName: {
      type: String,
      default: '',
      default: ""
    },
    cameraID: {
      type: String,
      default: 'C4668FD0-3CAE-C31F-C21E-28B7001243C4',
      default: "C4668FD0-3CAE-C31F-C21E-28B7001243C4"
    },
    rtspUrl: {
      type: String,
      default:
        'rtsp://admin:a1234567@192.168.1.201:554/h264/ch1/main/av_stream',
      default: "rtsp://admin:a1234567@192.168.1.201:554/h264/ch1/main/av_stream"
    },
    isRunning: {
      type: Boolean,
      default: false,
      default: false
    },
    isGb: {
      type: Boolean,
      default: false,
      default: false
    },
    showArea: {
      type: Boolean,
      default: false,
      default: false
    },
    isStream: {
      type: Boolean,
      default: true,
      default: true
    },
    autoPlay: {
      type: Boolean,
      default: false,
      default: false
    },
    preload: {
      type: Boolean,
      default: true,
    },
      default: true
    }
  },
  computed: {
    poster() {
      return '/images/player/player_poster.gif?t=' + Math.random()
      return "/images/player/player_poster.gif?t=" + Math.random()
    },
    showPrev() {
      return this.playerIndex - 1 >= 0
    },
    showNext() {
      return this.playerIndex + 1 < this.videoUrls.length
    },
    }
  },
  data() {
    return {
      player: null,
      playerId: 0,
      Camera: new VideoRuleData(),
      Camera: new VideoRuleData(this.cameraID, this.isGb),
      showCanvas: true,
      canvasData: {
        line: [],
        rect: [], // {id:'uuid', name: '矩形1', location: [{ x: 20, y: 30 }, { x: 20, y: 60 }, { x: 100, y: 60 }, { x: 100, y: 30 }] }
        arrow: [],
        polygon: [],
        polygon: []
      },
      //showProportion: 3.2,
      //showProportionY: 3.58,
@@ -197,9 +161,9 @@
      playerIndex: 0,
      isEmptyUrl: false,
      videoLoading: false,
      loadUrl:'',
      isFullScreen:false,
      fullScreenNotice:false
      loadUrl: "",
      isFullScreen: false,
      fullScreenNotice: false
    }
  },
  watch: {
@@ -214,6 +178,17 @@
        })
      }
    },
    videoUrl: function(newVal, oldVal) {
      if (newVal !== oldVal) {
        if (this.player) {
          this.player.stop()
        }
        this.$nextTick(() => {
          this.player.preloadFlag = 0
          this.playVideo()
        })
      }
    }
  },
  mounted() {
    this.player = new Player()
@@ -224,18 +199,18 @@
      this.playVideo()
      this.$nextTick(() => {
        this.canvas = this.$refs.areaCanvas
        this.ctx = this.canvas.getContext('2d')
        this.ctx = this.canvas.getContext("2d")
        this.ctx.lineWidth = 1
        this.initArea()
      })
    } else {
      // 录像URL处理, 可能存在多个录像地址
      if (this.videoUrl === '') {
      if (this.videoUrl === "") {
        this.playerStatus = -1
        return
      }
      this.videoUrls = this.videoUrl.split('||')
      this.videoUrls = this.videoUrl.split("||")
      if (this.autoPlay || this.preload) {
        this.playVideo()
@@ -252,47 +227,26 @@
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
      let _this = this // 集合中遍历需要将this转存一下使用
      _this.canvasData.line.forEach(function(v, i) {
        _this.ctx.strokeStyle = 'yellow'
        _this.ctx.strokeStyle = "yellow"
        _this.ctx.beginPath()
        _this.ctx.moveTo(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion
        )
        _this.ctx.lineTo(
          v.location[1].x / _this.showProportion,
          v.location[1].y / _this.showProportion
        )
        _this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion)
        _this.ctx.lineTo(v.location[1].x / _this.showProportion, v.location[1].y / _this.showProportion)
        _this.ctx.stroke()
        _this.canvas.style.cursor = 'default'
        _this.canvas.style.cursor = "default"
      })
      _this.canvasData.rect.forEach(function(v, i) {
        _this.ctx.strokeStyle = 'yellow'
        _this.ctx.strokeStyle = "yellow"
        _this.ctx.beginPath()
        _this.ctx.moveTo(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion
        )
        _this.ctx.lineTo(
          v.location[1].x / _this.showProportion,
          v.location[1].y / _this.showProportion
        )
        _this.ctx.lineTo(
          v.location[2].x / _this.showProportion,
          v.location[2].y / _this.showProportion
        )
        _this.ctx.lineTo(
          v.location[3].x / _this.showProportion,
          v.location[3].y / _this.showProportion
        )
        _this.ctx.lineTo(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion
        )
        _this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion)
        _this.ctx.lineTo(v.location[1].x / _this.showProportion, v.location[1].y / _this.showProportion)
        _this.ctx.lineTo(v.location[2].x / _this.showProportion, v.location[2].y / _this.showProportion)
        _this.ctx.lineTo(v.location[3].x / _this.showProportion, v.location[3].y / _this.showProportion)
        _this.ctx.lineTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion)
        _this.ctx.stroke()
        _this.canvas.style.cursor = 'default'
        _this.canvas.style.cursor = "default"
      })
      _this.canvasData.arrow.forEach(function(v, i) {
        _this.ctx.strokeStyle = 'yellow'
        _this.ctx.strokeStyle = "yellow"
        _this.drawArrow(
          _this.ctx,
          v.location[0].x / _this.showProportion,
@@ -301,44 +255,29 @@
          v.location[1].y / _this.showProportion,
          20,
          30,
          'yellow'
          "yellow"
        )
        _this.canvas.style.cursor = 'default'
        _this.canvas.style.cursor = "default"
      })
      _this.canvasData.polygon.forEach(function(v, i) {
        if (v.location.length === 0) {
        if (v.location && v.location.length === 0) {
          return
        }
        _this.ctx.strokeStyle = 'yellow'
        _this.ctx.strokeStyle = "yellow"
        _this.ctx.beginPath()
        _this.ctx.moveTo(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportionY
        )
        _this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportionY)
        for (let i = 1; i < v.location.length; i++) {
          _this.ctx.lineTo(
            v.location[i].x / _this.showProportion,
            v.location[i].y / _this.showProportionY
          )
          _this.ctx.lineTo(v.location[i].x / _this.showProportion, v.location[i].y / _this.showProportionY)
        }
        _this.ctx.closePath()
        _this.ctx.stroke()
        _this.canvas.style.cursor = 'default'
        _this.canvas.style.cursor = "default"
      })
    },
    // 箭头绘制函数
    drawArrow(
      ctx,
      fromX,
      fromY,
      toX,
      toY,
      theta = 30,
      headlen = 10,
      width = 1,
      color = 'yellow'
    ) {
    drawArrow(ctx, fromX, fromY, toX, toY, theta = 30, headlen = 10, width = 1, color = "yellow") {
      // ctx:Canvas绘图环境
      // fromX, fromY:起点坐标(也可以换成p1,只不过它是一个数组)
      // toX, toY:终点坐标 (也可以换成p2,只不过它是一个数组)
@@ -382,10 +321,10 @@
    // 回显图形备注
    showRemarks(x, y, remarks) {
      this.ctx.moveTo(x, y - 10) // 因为放大之后是y-20,所以缩小版的为y-10
      this.ctx.fillStyle = 'green' // 设置填充颜色为绿色
      this.ctx.fillStyle = "green" // 设置填充颜色为绿色
      this.ctx.font = '10px "微软雅黑"' // 设置字体
      this.ctx.textBaseline = 'bottom' // 设置字体底线对齐绘制基线
      this.ctx.textAlign = 'left' // 设置字体对齐的方式
      this.ctx.textBaseline = "bottom" // 设置字体底线对齐绘制基线
      this.ctx.textAlign = "left" // 设置字体对齐的方式
      this.ctx.fillText(remarks, x, y - 10) // 填充文字
    },
    getCanvasData(data) {
@@ -399,7 +338,6 @@
    setWidthHeight() {
      this.canvasWidth = this.$refs.videoPlayer.offsetWidth
      this.canvasHeight = this.$refs.videoPlayer.offsetHeight
      console.log(this.canvasWidth, this.canvasHeight)
    },
    async initArea() {
      if (!this.showCanvas) {
@@ -407,7 +345,7 @@
      }
      const res = await getAllPolygon({
        cameraId: this.TreeDataPool.selectedNode.id,
        cameraId: this.TreeDataPool.selectedNode.id
      })
      this.canvasData.line = res.data.line
      this.canvasData.rect = res.data.rect
@@ -416,15 +354,15 @@
      this.clickSelect(this.canvasData)
    },
    initAlgoDataWebScoket() {
      if (typeof WebSocket === 'undefined') {
        console.log('error,您的浏览器不支持socket')
      if (typeof WebSocket === "undefined") {
        console.log("error,您的浏览器不支持socket")
      } else {
        this.algoDataSocket = new WebSocket()
        this.algoDataSocket.onopen = () => {
          console.log('socket连接成功')
          console.log("socket连接成功")
        }
        this.algoDataSocket.onerror = () => {
          console.log('连接错误')
          console.log("连接错误")
        }
        this.algoDataSocket.onmessage = (msg) => {
          console.log(msg)
@@ -432,14 +370,14 @@
      }
    },
    playVideo() {
      if (this.isStream && this.rtspUrl == '') {
      if (this.isStream && this.rtspUrl == "") {
        return
      }
      let payload = ''
      let url = ''
      let payload = ""
      let url = ""
      if (this.isStream) {
        if (this.cameraID == '') {
        if (this.cameraID == "") {
          this.cameraID = this.getUuid()
        }
@@ -447,12 +385,12 @@
          cameraID: this.cameraID,
          rtspUrl: this.rtspUrl,
          isRunning: this.isRunning,
          isGb28181: this.isGb,
          isGb28181: this.isGb
        })
        url = this.videoUrl
      } else {
        url = '/httpImage/' + this.videoUrls[this.playerIndex]
        url = "/httpImage/" + this.videoUrls[this.playerIndex]
      }
      this.videoLoading = true
@@ -482,7 +420,8 @@
    },
    setPlayerStatus(stat) {
      this.videoLoading = false
      this.playerStatus = stat
      // 控制加载后的播放按钮状态,实时流为自动播放,视频为preload
      this.playerStatus = this.isStream ? 1 : 0
    },
    stopVideo() {
      if (this.player.hPlayer == 0) return
@@ -491,53 +430,54 @@
    },
    fullScreen() {
      if(!this.isFullScreen){
       this.player.fullscreen()
      }
      else{
      this.player.exitfullscreen()
      if (!this.isFullScreen) {
        this.player.fullscreen()
      } else {
        this.player.exitfullscreen()
      }
      this.isFullScreen = !this.isFullScreen
    },
    async downLoad() {
      if (!this.loadUrl) {
      this.$notify.info({
          title: '消息',
          message:'下载失败,无效的视频地址'
        });
        this.$notify.info({
          title: "消息",
          message: "下载失败,无效的视频地址"
        })
        return
      }
      if (this.isFullScreen) {
       /* const screen = this.player.webglPlayer.canvas.parentNode
        /* const screen = this.player.webglPlayer.canvas.parentNode
       var p = document.createElement("p");
        var txt = document.createTextNode("资源下载中,请稍后...");
        p.appendChild(txt);
       p.classList.add("downloadNotice");
        screen.appendChild(p); */
       this.fullScreenNotice = true
         setTimeout(()=>{this.fullScreenNotice = false},2000);}
       else {
         this.$notify.info({
          title: '消息',
          message:`资源下载中,请稍后...`
        });
       }
        this.fullScreenNotice = true
        setTimeout(() => {
          this.fullScreenNotice = false
        }, 2000)
      } else {
        this.$notify.info({
          title: "消息",
          message: `资源下载中,请稍后...`
        })
      }
      let url = 'http://'+window.location.host+this.loadUrl
      let url = "http://" + window.location.host + this.loadUrl
     // let url = "http://localhost:8080/httpImage/192.168.20.189:6700/283,2f49bf283ad7?collection=2021-09-28-DSVAD010120190703-video"
      let name = url.substring(url.lastIndexOf("/") + 1)
      // let url = "http://localhost:8080/httpImage/192.168.20.189:6700/283,2f49bf283ad7?collection=2021-09-28-DSVAD010120190703-video"
      let name = url.substring(url.lastIndexOf("/") + 1)
      let responsePromise = await fetch(url)
      let blob = await responsePromise.blob()
      let objectURL = window.URL.createObjectURL(blob)
      let a = document.createElement('a')
      let a = document.createElement("a")
      a.href = objectURL
      a.download = name
      a.click()
      a.remove()
   /*  let url = 'http:/'+this.loadUrl.substring(10)
      /*  let url = 'http:/'+this.loadUrl.substring(10)
    let link = document.createElement("a"); //创建a标签
    let name = url.substring(url.lastIndexOf("/") + 1) 
    link.style.display = "none"; //使其隐藏
@@ -545,7 +485,6 @@
    link.setAttribute("download", name); //设置下载属性 以及文件名
    document.body.appendChild(link); //a标签插至页面中
    link.click(); //强制触发a标签事件 */
    },
    playPrev() {
@@ -558,8 +497,8 @@
      this.playerIndex++
      this.player.stop()
      this.playVideo()
    },
  },
    }
  }
}
</script>
@@ -574,7 +513,9 @@
  width: 100%;
  height: 100%;
}
#area-canvas {
  z-index: 1;
}
video {
  object-fit: fill;
  width: 100%;
@@ -616,9 +557,9 @@
    background: rgba(255, 255, 255, 0.25);
    border-radius: 3px;
    width: 460px;
    width: -webkit-calc(100% - 260px);
    width: -moz-calc(100% - 260px);
    width: calc(100% - 260px);
    width: -webkit-calc(100% - 215px);
    width: -moz-calc(100% - 215px);
    width: calc(100% - 215px);
    float: left;
    margin: 19px 0 0 65px;
    position: relative;
@@ -770,7 +711,6 @@
    margin-top: 7px;
    margin-left: -12px;
  }
}
.video-player:hover {
@@ -824,34 +764,34 @@
  background-color: pink;
}
 .popuptext {
    /* display:none; */
    height: 50px;
    line-height: 50px;
    font-size: 20px;
    background-color: #555;
    opacity: .8;
    color: #fff;
    text-align: center;
    border-radius: 25px;
    position: relative;
    top: 30px;
    z-index: 1;
.popuptext {
  /* display:none; */
  height: 50px;
  line-height: 50px;
  font-size: 20px;
  background-color: #555;
  opacity: 0.8;
  color: #fff;
  text-align: center;
  border-radius: 25px;
  position: relative;
  top: 30px;
  z-index: 1;
}
    .fade-enter{
         opacity: 0;
      }
      .fade-enter-active{
         transition: opacity .5s;
      }
      .fade-leave-to{
         opacity: 0;
      }
      .fade-leave-active{
         transition: opacity 3s;
    }
 .canvasDiv .el-loading-mask {
   z-index: 1000 !important;
 }
.fade-enter {
  opacity: 0;
}
.fade-enter-active {
  transition: opacity 0.5s;
}
.fade-leave-to {
  opacity: 0;
}
.fade-leave-active {
  transition: opacity 3s;
}
.canvasDiv .el-loading-mask {
  z-index: 1000 !important;
}
</style>