index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <div v-if="addrList.length > 0">
  3. <div class="vent-flex-row-wrap camera-box" >
  4. <div v-for="(item, index) in addrList" :key="index" class="player-box">
  5. <div class="player-name">{{ item.name }}</div>
  6. <div>
  7. <template v-if="item.addr.startsWith('rtsp://')">
  8. <video :id="`video${index}`" muted autoplay></video>
  9. <div class="click-box" @dblclick="goFullScreen(`video${index}`)"></div>
  10. </template>
  11. <template v-else>
  12. <div :id="'player'+index" ></div>
  13. </template>
  14. </div>
  15. </div>
  16. </div>
  17. <div class="pagination">
  18. <Pagination v-model:current="current" :total="total" show-less-items @change="onChange"/>
  19. </div>
  20. </div>
  21. <div class="camera-box" v-else>
  22. <Empty />
  23. </div>
  24. </template>
  25. <script lang="ts" setup>
  26. import {onMounted, onUnmounted, ref } from 'vue';
  27. import { Pagination, Empty } from 'ant-design-vue';
  28. import { list, cameraAddr } from './camera.api'
  29. import Player, {I18N} from 'xgplayer';
  30. import ZH from 'xgplayer/es/lang/zh-cn'
  31. import HlsPlugin from 'xgplayer-hls';
  32. import FlvPlugin from 'xgplayer-flv';
  33. import 'xgplayer/dist/index.min.css';
  34. I18N.use(ZH)
  35. const pageSize = 8
  36. const current = ref(1)
  37. const total = ref(0)
  38. const playerList = ref([])
  39. const webRtcServerList = <any[]>[]
  40. let addrList = ref<{ name: string, addr: string }[]>([])
  41. async function getVideoAddrs(){
  42. clearCamera();
  43. playerList.value = []
  44. const result = await list({ pageSize: pageSize, pageNo: current.value })
  45. if(result && result.records && result.records.length > 0){
  46. total.value = result['total']
  47. const cameraList = <{ name: string, addr: string }[]>[]
  48. const cameras = result.records
  49. // const camerasArr: [] = [
  50. // {
  51. // name: '1111',
  52. // devicekind: 'toHKRtsp',
  53. // },
  54. // {
  55. // name: '2222',
  56. // devicekind: 'toHKRtsp',
  57. // },
  58. // {
  59. // name: '3333',
  60. // devicekind: 'toHKRtsp',
  61. // },
  62. // {
  63. // name: '4444',
  64. // devicekind: 'toHKRtsp',
  65. // },
  66. // {
  67. // name: '5555',
  68. // devicekind: 'toHKRtsp',
  69. // },
  70. // {
  71. // name: '6666',
  72. // devicekind: 'toHKRtsp',
  73. // },
  74. // {
  75. // name: '7777',
  76. // devicekind: 'toHKRtsp',
  77. // },
  78. // {
  79. // name: '8888',
  80. // devicekind: 'toHKRtsp',
  81. // },
  82. // {
  83. // name: '9999',
  84. // devicekind: 'toHKRtsp',
  85. // },
  86. // {
  87. // name: 'aaaa',
  88. // devicekind: 'toHKRtsp',
  89. // },
  90. // {
  91. // name: 'bbbb',
  92. // devicekind: 'toHKRtsp',
  93. // },
  94. // {
  95. // name: 'cccc',
  96. // devicekind: 'toHKRtsp',
  97. // },
  98. // ];
  99. // const cameras = []
  100. // for (let index = (current.value - 1)*pageSize; index < current.value * pageSize && index < camerasArr.length ; index++) {
  101. // cameras.push(camerasArr[index]) ;
  102. // }
  103. for (let i = 0; i < cameras.length; i++) {
  104. const item = cameras[i];
  105. if (item['devicekind'] === 'toHKRtsp') {
  106. // 从海康平台接口获取视频流
  107. try {
  108. const data = await cameraAddr({ cameraCode: item['addr'] });
  109. if (data && data['url']) {
  110. cameraList.push({ name: item['name'], addr: data['url'] });
  111. }
  112. // cameraList.push({
  113. // name: item['name'],
  114. // // addr: 'http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8'
  115. // addr: 'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.mp4/.m3u8',
  116. // });
  117. } catch (error) {
  118. }
  119. } else {
  120. if(item['addr'].includes('0.0.0.0')){
  121. item['addr'] = item['addr'].replace('0.0.0.0', window.location.hostname)
  122. }
  123. cameraList.push({ name: item['name'], addr: item['addr'] });
  124. }
  125. }
  126. addrList.value = cameraList
  127. }
  128. }
  129. function onChange(page) {
  130. current.value = page;
  131. getVideoAddrs().then(() => {
  132. getVideo()
  133. })
  134. }
  135. function getVideo() {
  136. const ip = VUE_APP_URL.webRtcUrl;
  137. for(let i = 0; i < addrList.value.length; i++){
  138. const item = addrList.value[i]
  139. if(item.addr.startsWith('rtsp://')){
  140. const dom = document.getElementById('video' + i) as HTMLVideoElement
  141. dom.muted = true;
  142. dom.volume = 0
  143. const webRtcServer = new window['WebRtcStreamer'](dom, location.protocol + ip)
  144. webRtcServerList.push(webRtcServer)
  145. webRtcServer.connect(item.addr)
  146. }else{
  147. setNoRtspVideo('player'+i,item.addr )
  148. }
  149. }
  150. }
  151. function setNoRtspVideo(id,videoAddr) {
  152. const fileExtension = videoAddr.split('.').pop();
  153. // const plugins = fileExtension === 'flv' ? [ FlvPlugin ] : [ HlsPlugin ];
  154. if(fileExtension === 'flv'){
  155. const player = new Player({
  156. lang: 'zh',
  157. id: id,
  158. url: videoAddr,
  159. width: 421,
  160. height: 292,
  161. poster: '/src/assets/images/vent/noSinge.png',
  162. plugins: [ FlvPlugin ] ,
  163. fluid: true,
  164. autoplay: true,
  165. isLive: true,
  166. playsinline: false,
  167. screenShot: true,
  168. whitelist: [''],
  169. ignores: ['time'],
  170. closeVideoClick: true,
  171. customConfig: {
  172. isClickPlayBack: false
  173. },
  174. flv: {
  175. retryCount: 3, // 重试 3 次,默认值
  176. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  177. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  178. fetchOptions: {
  179. // 该参数会透传给 fetch,默认值为 undefined
  180. mode: 'cors'
  181. },
  182. targetLatency: 10, // 直播目标延迟,默认 10 秒
  183. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  184. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  185. maxJumpDistance: 10,
  186. }
  187. });
  188. playerList.value.push(player)
  189. }
  190. if(fileExtension === 'm3u8'){
  191. let player
  192. if (document.createElement('video').canPlayType('application/vnd.apple.mpegurl')) {
  193. // 原生支持 hls 播放
  194. player = new Player({
  195. lang: 'zh',
  196. id: id,
  197. url: videoAddr,
  198. width: 421,
  199. height: 292,
  200. isLive: true,
  201. autoplay: true,
  202. autoplayMuted: true,
  203. cors: true,
  204. poster: '/src/assets/images/vent/noSinge.png',
  205. hls: {
  206. retryCount: 3, // 重试 3 次,默认值
  207. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  208. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  209. fetchOptions: {
  210. // 该参数会透传给 fetch,默认值为 undefined
  211. mode: 'cors'
  212. },
  213. targetLatency: 10, // 直播目标延迟,默认 10 秒
  214. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  215. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  216. maxJumpDistance: 10,
  217. }
  218. })
  219. } else if (HlsPlugin.isSupported()) { // 第一步
  220. player = new Player({
  221. lang: 'zh',
  222. id: id,
  223. url: videoAddr,
  224. width: 421,
  225. height: 292,
  226. isLive: true,
  227. autoplay: true,
  228. autoplayMuted: true,
  229. plugins: [HlsPlugin], // 第二步
  230. poster: '/src/assets/images/vent/noSinge.png',
  231. hls: {
  232. retryCount: 3, // 重试 3 次,默认值
  233. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  234. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  235. fetchOptions: {
  236. // 该参数会透传给 fetch,默认值为 undefined
  237. mode: 'cors'
  238. },
  239. targetLatency: 10, // 直播目标延迟,默认 10 秒
  240. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  241. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  242. maxJumpDistance: 10,
  243. }
  244. })
  245. }
  246. playerList.value.push(player)
  247. }
  248. }
  249. function goFullScreen(domId) {
  250. const videoDom = document.getElementById(domId) as HTMLVideoElement
  251. if(videoDom.requestFullscreen){
  252. videoDom.requestFullscreen()
  253. videoDom.play()
  254. } else if(videoDom.mozRequestFullscreen){
  255. videoDom.mozRequestFullscreen()
  256. videoDom.play()
  257. } else if (videoDom.webkitRequestFullscreen) {
  258. videoDom.webkitRequestFullscreen()
  259. videoDom.play()
  260. } else if (videoDom.msRequestFullscreen) {
  261. videoDom.msRequestFullscreen()
  262. videoDom.play()
  263. }
  264. }
  265. function clearCamera() {
  266. const num = webRtcServerList.length
  267. for (let i = 0; i < num; i++) {
  268. webRtcServerList[i].disconnect()
  269. webRtcServerList[i] = null
  270. }
  271. for(let i = 0; i < playerList.value.length; i++){
  272. const player = playerList.value[i]
  273. if(player.destroy)player.destroy()
  274. }
  275. }
  276. onMounted(async() => {
  277. await getVideoAddrs()
  278. getVideo()
  279. })
  280. onUnmounted(() => {
  281. clearCamera()
  282. })
  283. </script>
  284. <style lang="less">
  285. .camera-box{
  286. height: 700px;
  287. overflow-y: auto;
  288. display: flex;
  289. justify-content: center;
  290. align-items: center;
  291. .player-box{
  292. width: 451px;
  293. height: 312px;
  294. padding: 10px;
  295. background: url('/@/assets/images/vent/camera_bg.png');
  296. background-size: cover;
  297. position: relative;
  298. margin: 10px;
  299. .player-name{
  300. font-size: 14px;
  301. position: absolute;
  302. top: 15px;
  303. right: 15px;
  304. color: #fff;
  305. background-color: hsla(0, 0%, 50%, .5);
  306. border-radius: 2px;
  307. padding: 1px 5px;
  308. max-width: 120px;
  309. overflow: hidden;
  310. white-space: nowrap;
  311. text-overflow: ellipsis;
  312. z-index: 999;
  313. }
  314. .click-box{
  315. position: absolute;
  316. width: 100%;
  317. height: 100%;
  318. top: 0;
  319. left: 0;
  320. }
  321. }
  322. }
  323. .pagination{
  324. width: 100%;
  325. height: 200px;
  326. display: flex;
  327. justify-content: center;
  328. align-items: center;
  329. }
  330. // :deep(video){
  331. // width: 100% !important;
  332. // height: 100% !important;
  333. // object-fit: cover !important;
  334. // }
  335. </style>