zhangzengfei
2022-06-17 c8a38d5fd8b1a92d4d14a9785f21b1e0223e2460
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<template>
  <div style="width:100%; height: 100%;">
    <camera-player
      :cameraID="query.cameraId"
      :rtspUrl="query.rtspUrl"
      :isGb="query.gb28181 === '1'"
      v-if="!enableWasm"
    ></camera-player>
    <wasm-player
      :cameraID="query.cameraId"
      :rtspUrl="query.rtspUrl"
      :isGb="query.gb28181 === '1'"
      :videoUrl="query.videoUrl"
      :isStream="isStream"
      v-else
    ></wasm-player>
  </div>
</template>
 
<script>
// 接口使用
// 参数 cameraId 摄像机id, rtspUrl 摄像机rtsp地址, gb28181 是否是国标摄像机(1 或 0), enableWasm 使用wasm 播放器, isStream 是否播放实时流, videoUrl 视频文件地址
// http://192.168.20.191:7003/view/cameraPlayer/index.html?cameraId=e7e6157a-5929-4e78-b390-e365141169c8&rtspUrl=rtsp://admin:a1234567@192.168.5.51:554/h264/ch1/main/av_stream
 
import CameraPlayer from "../components/player"
import WasmPlayer from "@/components/wasmPlayer"
 
export default {
  name: "BasicCameraPlayer",
  components: {
    CameraPlayer,
    WasmPlayer
  },
  data() {
    return {
      query: {
        cameraId: "",
        rtspUrl: "",
        gb28181: "0",
        videoUrl: `${location.protocol === "https" ? "wss" : "ws"}://${location.host}/ws`
      },
      enableWasm: false,
      isStream: true
    }
  },
  mounted() {
    this.urlParse()
  },
  methods: {
    urlParse() {
      let url = window.location.search
      let obj = {}
      let reg = /[?&][^?&]+=[^?&]+/g
      let arr = url.match(reg)
      if (arr) {
        arr.forEach((item) => {
          let temArr = item.substring(1).split("=")
          let key = decodeURIComponent(temArr[0])
          let value = decodeURIComponent(temArr[1])
          obj[key] = value
          if (key == "rtspUrl") {
            obj[key] = this.rtspParse(value)
          }
 
          if (key == "enableWasm" && value == "1") {
            this.enableWasm = true
          }
          if (key == "isStream" && value == "0") {
            this.isStream = false
          }
        })
      }
      this.query = Object.assign({}, this.query, obj)
      console.log("cameraPlayer:", this.query, "enableWasm:", this.enableWasm, "isStream:", this.isStream)
    },
    // 对传入的rtsp密码进行urlEncode处理
    rtspParse(input) {
      // 无效的rtsp地址
      if (!input.length || input.indexOf("rtsp://") != 0) {
        return
      }
 
      let userinfoSplitPos = input.indexOf(":", 7)
      let userinfoEndPos = input.lastIndexOf("@")
 
      // 未包含登录信息的rtsp地址, 直接返回原url
      if (userinfoSplitPos == -1 || userinfoEndPos == -1 || userinfoEndPos < userinfoSplitPos) {
        return input
      }
 
      let usrPassword = input.slice(userinfoSplitPos + 1, userinfoEndPos)
      if (usrPassword.indexOf("@") != -1) {
        return input
      }
 
      let encodePassword = encodeURIComponent(usrPassword)
 
      return input.replace(usrPassword, encodePassword)
    }
  }
}
</script>