zhangzengfei
2022-08-10 907a3504a77873a8e762adc2f684222c8946adec
src/components/wasmPlayer/index.vue
@@ -1,12 +1,6 @@
<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
@@ -14,12 +8,7 @@
          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>
        <canvas ref="playCanvas" id="paly-canvas" width="960" height="540"></canvas>
      </div>
      <transition name="fade">
@@ -29,31 +18,16 @@
      </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>
        <!-- 声音 -->
@@ -64,25 +38,15 @@
        </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"
          style="margin: 9px 0 0 15px"
        ></i>
        <i class="el-icon-download jsmodern-video-download" @click="downLoad" style="margin: 9px 0 0 15px"></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">
@@ -109,68 +73,65 @@
/*
  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",
  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;
      return this.playerIndex - 1 >= 0
    },
    showNext() {
      return this.playerIndex + 1 < this.videoUrls.length;
    },
      return this.playerIndex + 1 < this.videoUrls.length
    }
  },
  data() {
    return {
@@ -182,7 +143,7 @@
        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,
@@ -202,101 +163,81 @@
      videoLoading: false,
      loadUrl: "",
      isFullScreen: false,
      fullScreenNotice: false,
    };
      fullScreenNotice: false
    }
  },
  watch: {
    rtspUrl: function (newVal, oldVal) {
    rtspUrl: function(newVal, oldVal) {
      if (newVal !== oldVal) {
        if (this.player) {
          this.player.stop();
          !!this.algoDataSocket && this.algoDataSocket.close();
          this.player.stop()
          !!this.algoDataSocket && this.algoDataSocket.close()
        }
        this.$nextTick(() => {
          this.playVideo();
        });
          this.playVideo()
        })
      }
    },
    }
  },
  mounted() {
    this.player = new Player();
    this.player.preload = this.preload;
    this.player.statusCallback = this.setPlayerStatus;
    this.player = new Player()
    this.player.preload = this.preload
    this.player.statusCallback = this.setPlayerStatus
    if (this.isStream) {
      this.playVideo();
      this.playVideo()
      this.$nextTick(() => {
        this.canvas = this.$refs.areaCanvas;
        this.ctx = this.canvas.getContext("2d");
        this.ctx.lineWidth = 1;
        this.initArea();
      });
        this.canvas = this.$refs.areaCanvas
        this.ctx = this.canvas.getContext("2d")
        this.ctx.lineWidth = 1
        this.initArea()
      })
    } else {
      // 录像URL处理, 可能存在多个录像地址
      if (this.videoUrl === "") {
        this.playerStatus = -1;
        return;
        this.playerStatus = -1
        return
      }
      this.videoUrls = this.videoUrl.split("||");
      this.videoUrls = this.videoUrl.split("||")
      if (this.autoPlay || this.preload) {
        this.playVideo();
        this.playVideo()
      }
    }
  },
  beforeDestroy() {
    this.player.stop();
    console.log("wasm paly beforeDestroy")
    this.player.stop()
  },
  methods: {
    // 回显cavas数据
    // 点击选中变色 将当前页面所有路径重绘判断当前鼠标的坐标在哪个图形内 如果不传坐标参数就是回显的方法
    clickSelect(e) {
      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.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.stroke();
        _this.canvas.style.cursor = "default";
      });
      _this.canvasData.rect.forEach(function (v, i) {
        _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.stroke();
        _this.canvas.style.cursor = "default";
      });
      _this.canvasData.arrow.forEach(function (v, i) {
        _this.ctx.strokeStyle = "yellow";
      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.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.stroke()
        _this.canvas.style.cursor = "default"
      })
      _this.canvasData.rect.forEach(function(v, i) {
        _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.stroke()
        _this.canvas.style.cursor = "default"
      })
      _this.canvasData.arrow.forEach(function(v, i) {
        _this.ctx.strokeStyle = "yellow"
        _this.drawArrow(
          _this.ctx,
          v.location[0].x / _this.showProportion,
@@ -306,43 +247,27 @@
          20,
          30,
          "yellow"
        );
        _this.canvas.style.cursor = "default";
      });
      _this.canvasData.polygon.forEach(function (v, i) {
        )
        _this.canvas.style.cursor = "default"
      })
      _this.canvasData.polygon.forEach(function(v, i) {
        if (v.location.length === 0) {
          return;
          return
        }
        _this.ctx.strokeStyle = "yellow";
        _this.ctx.beginPath();
        _this.ctx.moveTo(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportionY
        );
        _this.ctx.strokeStyle = "yellow"
        _this.ctx.beginPath()
        _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.ctx.closePath()
        _this.ctx.stroke()
        _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,只不过它是一个数组)
@@ -355,118 +280,118 @@
      // width = typeof width !== "undefined" ? width : 1;
      // color = typeof color !== "undefined" ? color : "yellow";
      // 计算各角度和对应的P2,P3坐标
      let angle = (Math.atan2(fromY - toY, fromX - toX) * 180) / Math.PI;
      let angle1 = ((angle + theta) * Math.PI) / 180;
      let angle2 = ((angle - theta) * Math.PI) / 180;
      let topX = headlen * Math.cos(angle1);
      let topY = headlen * Math.sin(angle1);
      let botX = headlen * Math.cos(angle2);
      let botY = headlen * Math.sin(angle2);
      let angle = (Math.atan2(fromY - toY, fromX - toX) * 180) / Math.PI
      let angle1 = ((angle + theta) * Math.PI) / 180
      let angle2 = ((angle - theta) * Math.PI) / 180
      let topX = headlen * Math.cos(angle1)
      let topY = headlen * Math.sin(angle1)
      let botX = headlen * Math.cos(angle2)
      let botY = headlen * Math.sin(angle2)
      ctx.save();
      ctx.beginPath();
      let arrowX = fromX - topX;
      let arrowY = fromY - topY;
      ctx.moveTo(arrowX, arrowY);
      ctx.moveTo(fromX, fromY);
      ctx.lineTo(toX, toY);
      arrowX = toX + topX;
      arrowY = toY + topY;
      ctx.moveTo(arrowX, arrowY);
      ctx.lineTo(toX, toY);
      arrowX = toX + botX;
      arrowY = toY + botY;
      ctx.lineTo(arrowX, arrowY);
      ctx.strokeStyle = color;
      ctx.lineWidth = width;
      ctx.stroke();
      ctx.restore();
      ctx.save()
      ctx.beginPath()
      let arrowX = fromX - topX
      let arrowY = fromY - topY
      ctx.moveTo(arrowX, arrowY)
      ctx.moveTo(fromX, fromY)
      ctx.lineTo(toX, toY)
      arrowX = toX + topX
      arrowY = toY + topY
      ctx.moveTo(arrowX, arrowY)
      ctx.lineTo(toX, toY)
      arrowX = toX + botX
      arrowY = toY + botY
      ctx.lineTo(arrowX, arrowY)
      ctx.strokeStyle = color
      ctx.lineWidth = width
      ctx.stroke()
      ctx.restore()
    },
    // 回显图形备注
    showRemarks(x, y, remarks) {
      this.ctx.moveTo(x, y - 10); // 因为放大之后是y-20,所以缩小版的为y-10
      this.ctx.fillStyle = "green"; // 设置填充颜色为绿色
      this.ctx.font = '10px "微软雅黑"'; // 设置字体
      this.ctx.textBaseline = "bottom"; // 设置字体底线对齐绘制基线
      this.ctx.textAlign = "left"; // 设置字体对齐的方式
      this.ctx.fillText(remarks, x, y - 10); // 填充文字
      this.ctx.moveTo(x, y - 10) // 因为放大之后是y-20,所以缩小版的为y-10
      this.ctx.fillStyle = "green" // 设置填充颜色为绿色
      this.ctx.font = '10px "微软雅黑"' // 设置字体
      this.ctx.textBaseline = "bottom" // 设置字体底线对齐绘制基线
      this.ctx.textAlign = "left" // 设置字体对齐的方式
      this.ctx.fillText(remarks, x, y - 10) // 填充文字
    },
    getCanvasData(data) {
      let polyon = { ...data };
      polyon.camera_id = this.Camera.cameraId;
      let polyon = { ...data }
      polyon.camera_id = this.Camera.cameraId
      savePolygon(polyon).then((rsp) => {
        this.Camera.getPolygon();
        this.Camera.getCameraTask();
      });
        this.Camera.getPolygon()
        this.Camera.getCameraTask()
      })
    },
    setWidthHeight() {
      this.canvasWidth = this.$refs.videoPlayer.offsetWidth;
      this.canvasHeight = this.$refs.videoPlayer.offsetHeight;
      console.log(this.canvasWidth, this.canvasHeight);
      this.canvasWidth = this.$refs.videoPlayer.offsetWidth
      this.canvasHeight = this.$refs.videoPlayer.offsetHeight
      console.log(this.canvasWidth, this.canvasHeight)
    },
    async initArea() {
      if (!this.showCanvas) {
        return;
        return
      }
      const res = await getAllPolygon({
        cameraId: this.TreeDataPool.selectedNode.id,
      });
      this.canvasData.line = res.data.line;
      this.canvasData.rect = res.data.rect;
      this.canvasData.arrow = res.data.arrow;
      this.canvasData.polygon = res.data.polygon;
      this.clickSelect(this.canvasData);
        cameraId: this.TreeDataPool.selectedNode.id
      })
      this.canvasData.line = res.data.line
      this.canvasData.rect = res.data.rect
      this.canvasData.arrow = res.data.arrow
      this.canvasData.polygon = res.data.polygon
      this.clickSelect(this.canvasData)
    },
    initAlgoDataWebScoket() {
      if (typeof WebSocket === "undefined") {
        console.log("error,您的浏览器不支持socket");
        console.log("error,您的浏览器不支持socket")
      } else {
        this.algoDataSocket = new WebSocket();
        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);
        };
          console.log(msg)
        }
      }
    },
    playVideo() {
      if (this.isStream && this.rtspUrl == "") {
        return;
        return
      }
      let payload = "";
      let url = "";
      let payload = ""
      let url = ""
      if (this.isStream) {
        if (this.cameraID == "") {
          this.cameraID = this.getUuid();
          this.cameraID = this.getUuid()
        }
        payload = JSON.stringify({
          cameraID: this.cameraID,
          rtspUrl: this.rtspUrl,
          isRunning: this.isRunning,
          isGb28181: this.isGb,
        });
          isGb28181: this.isGb
        })
        url = this.videoUrl;
        url = this.videoUrl
      } else {
        url = "/httpImage/" + this.videoUrls[this.playerIndex];
        url = "/httpImage/" + this.videoUrls[this.playerIndex]
      }
      this.videoLoading = true;
      this.videoLoading = true
      if (this.player.hPlayer == 0) {
        console.log(url);
        console.log(this.$refs.playCanvas);
        console.log(this.isStream);
        console.log(this.$refs.timeTrack);
        console.log(this.$refs.timeLabel);
        console.log(payload);
        console.log(url)
        console.log(this.$refs.playCanvas)
        console.log(this.isStream)
        console.log(this.$refs.timeTrack)
        console.log(this.$refs.timeLabel)
        console.log(payload)
        this.player.play(
          url,
          this.$refs.playCanvas,
@@ -475,47 +400,47 @@
          this.$refs.timeLabel,
          payload,
          () => {
            this.videoLoading = false;
            this.videoLoading = false
          }
        );
        )
      } else if (this.player.PlayOrPause == 0) {
        this.player.resume();
        this.videoLoading = false;
        this.player.resume()
        this.videoLoading = false
      } else {
        this.player.pause();
        this.videoLoading = false;
        this.player.pause()
        this.videoLoading = false
      }
      this.playerStatus = this.player.PlayOrPause;
      this.playerStatus = this.player.PlayOrPause
      this.loadUrl = url;
      this.loadUrl = url
    },
    setPlayerStatus(stat) {
      this.videoLoading = false;
      this.playerStatus = stat;
      this.videoLoading = false
      this.playerStatus = stat
    },
    stopVideo() {
      if (this.player.hPlayer == 0) return;
      if (this.player.hPlayer == 0) return
      this.player.stop();
      this.player.stop()
    },
    fullScreen() {
      if (!this.isFullScreen) {
        this.player.fullscreen();
        this.player.fullscreen()
      } else {
        this.player.exitfullscreen();
        this.player.exitfullscreen()
      }
      this.isFullScreen = !this.isFullScreen;
      this.isFullScreen = !this.isFullScreen
    },
    async downLoad() {
      if (!this.loadUrl) {
        this.$notify.info({
          title: "消息",
          message: "下载失败,无效的视频地址",
        });
        return;
          message: "下载失败,无效的视频地址"
        })
        return
      }
      if (this.isFullScreen) {
@@ -525,29 +450,29 @@
        p.appendChild(txt);
       p.classList.add("downloadNotice");
        screen.appendChild(p); */
        this.fullScreenNotice = true;
        this.fullScreenNotice = true
        setTimeout(() => {
          this.fullScreenNotice = false;
        }, 2000);
          this.fullScreenNotice = false
        }, 2000)
      } else {
        this.$notify.info({
          title: "消息",
          message: `资源下载中,请稍后...`,
        });
          message: `资源下载中,请稍后...`
        })
      }
      let url = "/httpImage/" + window.location.host + this.loadUrl;
      let url = "/httpImage/" + 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 responsePromise = await fetch(url);
      let blob = await responsePromise.blob();
      let objectURL = window.URL.createObjectURL(blob);
      let a = document.createElement("a");
      a.href = objectURL;
      a.download = name;
      a.click();
      a.remove();
      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")
      a.href = objectURL
      a.download = name
      a.click()
      a.remove()
      /*  let url = 'http:/'+this.loadUrl.substring(10)
    let link = document.createElement("a"); //创建a标签
    let name = url.substring(url.lastIndexOf("/") + 1) 
@@ -559,18 +484,18 @@
    },
    playPrev() {
      this.playerIndex--;
      this.player.stop();
      this.playVideo();
      this.playerIndex--
      this.player.stop()
      this.playVideo()
    },
    playNext() {
      this.playerIndex++;
      this.player.stop();
      this.playVideo();
    },
  },
};
      this.playerIndex++
      this.player.stop()
      this.playVideo()
    }
  }
}
</script>
<style lang="scss">