cameraModal.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <div class="camera-modal">
  3. <div v-for="(item, index) in addrList" :key="index" class="player-box">
  4. <div class="player-name">{{ item.name }}</div>
  5. <div style="padding-top: 3px">
  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. <div :id="'player' + index"></div>
  12. </template>
  13. </div>
  14. </div>
  15. </div>
  16. </template>
  17. <script setup lang="ts">
  18. import { onMounted, onUnmounted, ref, reactive, computed } from 'vue';
  19. import { useRouter } from 'vue-router';
  20. import Player, { I18N } from 'xgplayer';
  21. import ZH from 'xgplayer/es/lang/zh-cn';
  22. import HlsPlugin from 'xgplayer-hls';
  23. import FlvPlugin from 'xgplayer-flv';
  24. import 'xgplayer/dist/index.min.css';
  25. import { cameraAddr } from '../airdoor.api';
  26. let props = defineProps({
  27. cameraData: {
  28. type: Object,
  29. default: () => {
  30. return {}
  31. }
  32. }
  33. })
  34. const playerList = ref([]);
  35. let addrList = ref<{ name: string; addr: string; cameraRate: number; devicekind: string }[]>([]);
  36. const webRtcServerList = <any[]>[];
  37. async function getVideoAddrs() {
  38. console.log(props.cameraData, 'camera---')
  39. clearCamera();
  40. playerList.value = [];
  41. const cameraList = <{ name: string; addr: string; cameraRate: number; devicekind: string }[]>[];
  42. const cameras = props.cameraData.cameras;
  43. for (let i = 0; i < cameras.length; i++) {
  44. const item = cameras[i];
  45. if (item['devicekind'] === 'toHKRtsp' || item['devicekind'] === 'toHKHLs' || item['devicekind'] === 'HLL' || item['devicekind'] === 'YZG_URL') {
  46. // 从海康平台接口获取视频流
  47. const videoType = item['devicekind'] === 'toHKRtsp' ? 'rtsp' : '';
  48. const devicekindType = item['devicekind'] === 'YZG_URL' ? 'YZG_URL' : ''
  49. try {
  50. const data = await cameraAddr({ devicekind: devicekindType, cameraCode: item['addr'], videoType });
  51. if (data && data['url']) {
  52. cameraList.push({ name: item['name'], addr: data['url'], cameraRate: item['cameraRate'], devicekind: item['devicekind'] });
  53. }
  54. } catch (error) { }
  55. } else {
  56. if (item['addr'].includes('0.0.0.0')) {
  57. item['addr'] = item['addr'].replace('0.0.0.0', window.location.hostname);
  58. }
  59. cameraList.push({ name: item['name'], addr: item['addr'], cameraRate: item['cameraRate'], devicekind: item['devicekind'] });
  60. }
  61. }
  62. addrList.value = cameraList;
  63. console.log(addrList.value, ' addrList.value-------------');
  64. }
  65. function getVideo() {
  66. const ip = VUE_APP_URL.webRtcUrl;
  67. for (let i = 0; i < addrList.value.length; i++) {
  68. const item = addrList.value[i];
  69. if (item.addr.startsWith('rtsp://')) {
  70. const dom = document.getElementById('video' + i) as HTMLVideoElement;
  71. dom.muted = true;
  72. dom.volume = 0;
  73. const webRtcServer = new window['WebRtcStreamer'](dom, location.protocol + ip);
  74. webRtcServerList.push(webRtcServer);
  75. webRtcServer.connect(item.addr);
  76. } else {
  77. setNoRtspVideo('player' + i, item.addr, item.cameraRate, item.devicekind);
  78. }
  79. }
  80. }
  81. function clearCamera() {
  82. const num = webRtcServerList.length;
  83. for (let i = 0; i < num; i++) {
  84. if (webRtcServerList[i]) {
  85. webRtcServerList[i].disconnect();
  86. webRtcServerList[i] = null;
  87. }
  88. }
  89. for (let i = 0; i < playerList.value.length; i++) {
  90. const player = playerList.value[i];
  91. if (player.destroy) player.destroy();
  92. }
  93. playerList.value = [];
  94. }
  95. function setNoRtspVideo(id, videoAddr, cameraRate, devicekind) {
  96. const fileExtension = videoAddr.split('.').pop();
  97. if (fileExtension === 'flv' || devicekind == 'flv') {
  98. const player = new Player({
  99. lang: 'zh',
  100. id: id,
  101. url: videoAddr,
  102. width: 589,
  103. height: 330,
  104. poster: '/src/assets/images/vent/noSinge.png',
  105. plugins: [FlvPlugin],
  106. fluid: true,
  107. autoplay: true,
  108. isLive: true,
  109. playsinline: true,
  110. screenShot: true,
  111. whitelist: [''],
  112. ignores: ['time', 'progress', 'play', 'i18n', 'volume', 'fullscreen', 'screenShot', 'playbackRate'],
  113. closeVideoClick: true,
  114. customConfig: {
  115. isClickPlayBack: false,
  116. },
  117. defaultPlaybackRate: cameraRate || 1,
  118. controls: false,
  119. flv: {
  120. retryCount: 3, // 重试 3 次,默认值
  121. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  122. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  123. fetchOptions: {
  124. // 该参数会透传给 fetch,默认值为 undefined
  125. mode: 'cors',
  126. },
  127. targetLatency: 10, // 直播目标延迟,默认 10 秒
  128. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  129. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  130. maxJumpDistance: 10,
  131. },
  132. });
  133. playerList.value.push(player);
  134. }
  135. if (fileExtension === 'm3u8' || devicekind == 'm3u8') {
  136. let player;
  137. if (document.createElement('video').canPlayType('application/vnd.apple.mpegurl')) {
  138. // 原生支持 hls 播放
  139. player = new Player({
  140. lang: 'zh',
  141. id: id,
  142. url: videoAddr,
  143. width: 376,
  144. height: 210,
  145. isLive: true,
  146. autoplay: true,
  147. autoplayMuted: true,
  148. cors: true,
  149. ignores: ['time', 'progress', 'play', 'i18n', 'volume', 'fullscreen', 'screenShot', 'playbackRate'],
  150. poster: '/src/assets/images/vent/noSinge.png',
  151. defaultPlaybackRate: cameraRate || 1,
  152. controls: false,
  153. hls: {
  154. retryCount: 3, // 重试 3 次,默认值
  155. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  156. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  157. fetchOptions: {
  158. // 该参数会透传给 fetch,默认值为 undefined
  159. mode: 'cors',
  160. },
  161. targetLatency: 10, // 直播目标延迟,默认 10 秒
  162. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  163. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  164. maxJumpDistance: 10,
  165. },
  166. });
  167. } else if (HlsPlugin.isSupported()) {
  168. // 第一步
  169. player = new Player({
  170. lang: 'zh',
  171. id: id,
  172. url: videoAddr,
  173. width: 376,
  174. height: 210,
  175. isLive: true,
  176. autoplay: true,
  177. autoplayMuted: true,
  178. plugins: [HlsPlugin], // 第二步
  179. poster: '/src/assets/images/vent/noSinge.png',
  180. ignores: ['time', 'progress', 'play', 'i18n', 'volume', 'fullscreen', 'screenShot', 'playbackRate'],
  181. defaultPlaybackRate: cameraRate || 1,
  182. controls: false,
  183. hls: {
  184. retryCount: 3, // 重试 3 次,默认值
  185. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  186. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  187. fetchOptions: {
  188. // 该参数会透传给 fetch,默认值为 undefined
  189. mode: 'cors',
  190. },
  191. targetLatency: 10, // 直播目标延迟,默认 10 秒
  192. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  193. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  194. maxJumpDistance: 10,
  195. },
  196. });
  197. }
  198. playerList.value.push(player);
  199. }
  200. }
  201. function goFullScreen(domId) {
  202. const videoDom = document.getElementById(domId) as HTMLVideoElement;
  203. if (videoDom.requestFullscreen) {
  204. videoDom.requestFullscreen();
  205. videoDom.play();
  206. } else if (videoDom.mozRequestFullscreen) {
  207. videoDom.mozRequestFullscreen();
  208. videoDom.play();
  209. } else if (videoDom.webkitRequestFullscreen) {
  210. videoDom.webkitRequestFullscreen();
  211. videoDom.play();
  212. } else if (videoDom.msRequestFullscreen) {
  213. videoDom.msRequestFullscreen();
  214. videoDom.play();
  215. }
  216. }
  217. onMounted(async () => {
  218. await getVideoAddrs();
  219. getVideo();
  220. });
  221. onUnmounted(() => {
  222. clearCamera();
  223. });
  224. </script>
  225. <style lang="less" scoped>
  226. @import '/@/design/theme.less';
  227. @{theme-deepblue} {
  228. .camera-modal {
  229. --image-camera_bg: url('/@/assets/images/themify/deepblue/vent/camera_bg.png');
  230. }
  231. }
  232. .camera-modal {
  233. --image-camera_bg: url('/@/assets/images/vent/camera_bg.png');
  234. display: flex;
  235. justify-content: space-between;
  236. flex-wrap: wrap;
  237. .player-box {
  238. width: 400px;
  239. height: 235px;
  240. padding: 10px 12px;
  241. background: var(--image-camera_bg);
  242. background-size: 100% 100%;
  243. position: relative;
  244. margin: 10px;
  245. .player-name {
  246. font-size: 14px;
  247. position: absolute;
  248. top: 12px;
  249. right: 12px;
  250. color: #fff;
  251. background-color: hsla(0, 0%, 50%, 0.5);
  252. border-radius: 2px;
  253. padding: 1px 5px;
  254. max-width: 120px;
  255. overflow: hidden;
  256. white-space: nowrap;
  257. text-overflow: ellipsis;
  258. z-index: 999;
  259. }
  260. .click-box {
  261. position: absolute;
  262. width: 100%;
  263. height: 100%;
  264. top: 0;
  265. left: 0;
  266. }
  267. }
  268. }
  269. </style>