index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <div class="vent-flex-row-wrap camera-box">
  3. <div v-for="(item, index) in addrList" :key="index" class="player-box">
  4. <div class="player-name">{{ item.name }}</div>
  5. <div>
  6. <template v-if="item.addr.startsWith('rtsp://')">
  7. <video :id="`video${index}`" muted autoplay></video>
  8. <div class="click-box" @dblclick="goFullScreen(`video${index}`)"></div>
  9. </template>
  10. <template v-else>
  11. <LivePlayer :videoUrl="item.addr" muted live loading/>
  12. </template>
  13. </div>
  14. </div>
  15. </div>
  16. </template>
  17. <script lang="ts" setup>
  18. import {onMounted, onUnmounted, ref } from 'vue';
  19. import { list, cameraAddr } from './camera.api'
  20. import LivePlayer from '@liveqing/liveplayer-v3';
  21. const webRtcServerList = <any[]>[]
  22. let addrList = ref<{ name: string, addr: string }[]>([])
  23. async function getVideoAddrs(){
  24. const result = await list({ pageSize: 10000 })
  25. if(result && result.records && result.records.length > 0){
  26. // addrList.value = result.records.map(item => ({ addr: item['addr'].includes('0.0.0.0') ? item['addr'].replace('0.0.0.0', window.location.hostname) :item['addr'] , name: item['name'] }))
  27. const cameraList = <{ name: string, addr: string }[]>[]
  28. result.records.forEach(async item => {
  29. if (item['devicekind'] == 'toHKRtsp') {
  30. // 从海康平台接口获取视频流
  31. const data = await cameraAddr({ cameraCode: item['addr'] });
  32. if (data && data['url']) {
  33. cameraList.push({ name: item['name'], addr: data['url'] });
  34. }
  35. } else {
  36. cameraList.push({ name: item['name'], addr: item['addr'] });
  37. }
  38. });
  39. addrList.value = cameraList
  40. }
  41. }
  42. function getVideo() {
  43. const ip = VUE_APP_URL.webRtcUrl;
  44. for(let i =0; i < addrList.value.length; i++){
  45. const item = addrList.value[i]
  46. if(item.addr.startsWith('rtsp://')){
  47. const dom = document.getElementById('video' + i) as HTMLVideoElement
  48. dom.muted = true;
  49. dom.volume = 0
  50. const webRtcServer = new window['WebRtcStreamer'](dom, location.protocol + ip)
  51. webRtcServerList.push(webRtcServer)
  52. webRtcServer.connect(item.addr)
  53. }
  54. }
  55. }
  56. function goFullScreen(domId) {
  57. const videoDom = document.getElementById(domId) as HTMLVideoElement
  58. if(videoDom.requestFullscreen){
  59. videoDom.requestFullscreen()
  60. videoDom.play()
  61. } else if(videoDom.mozRequestFullscreen){
  62. videoDom.mozRequestFullscreen()
  63. videoDom.play()
  64. } else if (videoDom.webkitRequestFullscreen) {
  65. videoDom.webkitRequestFullscreen()
  66. videoDom.play()
  67. } else if (videoDom.msRequestFullscreen) {
  68. videoDom.msRequestFullscreen()
  69. videoDom.play()
  70. }
  71. }
  72. onMounted(async() => {
  73. await getVideoAddrs()
  74. getVideo()
  75. })
  76. onUnmounted(() => {
  77. const num = webRtcServerList.length
  78. for (let i = 0; i < num; i++) {
  79. webRtcServerList[i].disconnect()
  80. webRtcServerList[i] = null
  81. }
  82. })
  83. </script>
  84. <style lang="less">
  85. .camera-box{
  86. overflow-y: auto;
  87. .player-box{
  88. width: 314px;
  89. height: 208px;
  90. padding: 10px;
  91. background: url('/@/assets/images/vent/camera_bg.png');
  92. background-size: cover;
  93. position: relative;
  94. margin: 10px;
  95. .player-name{
  96. font-size: 14px;
  97. position: absolute;
  98. top: 15px;
  99. right: 15px;
  100. color: #fff;
  101. background-color: hsla(0, 0%, 50%, .5);
  102. border-radius: 2px;
  103. padding: 1px 5px;
  104. max-width: 120px;
  105. overflow: hidden;
  106. white-space: nowrap;
  107. text-overflow: ellipsis;
  108. z-index: 999;
  109. }
  110. .click-box{
  111. position: absolute;
  112. width: 100%;
  113. height: 100%;
  114. top: 0;
  115. left: 0;
  116. }
  117. }
  118. }
  119. // :deep(video){
  120. // width: 100% !important;
  121. // height: 100% !important;
  122. // object-fit: cover !important;
  123. // }
  124. </style>