123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <template>
- <div v-if="addrList.length > 0">
- <div class="vent-flex-row-wrap camera-box" >
- <div v-for="(item, index) in addrList" :key="index" class="player-box">
- <div class="player-name">{{ item.name }}</div>
- <div>
- <template v-if="item.addr.startsWith('rtsp://')">
- <video :id="`video${index}`" muted autoplay></video>
- <div class="click-box" @dblclick="goFullScreen(`video${index}`)"></div>
- </template>
- <template v-else>
- <div :id="'player'+index" ></div>
- </template>
- </div>
- </div>
- </div>
- <div class="pagination">
- <Pagination v-model:current="current" :total="total" show-less-items @change="onChange"/>
- </div>
- </div>
- <div class="camera-box" v-else>
- <Empty />
- </div>
-
- </template>
- <script lang="ts" setup>
- import {onMounted, onUnmounted, ref } from 'vue';
- import { Pagination, Empty } from 'ant-design-vue';
- import { list, cameraAddr } from './camera.api'
- import Player from 'xgplayer';
- import HlsPlugin from 'xgplayer-hls';
- import FlvPlugin from 'xgplayer-flv';
- import 'xgplayer/dist/index.min.css';
- const pageSize = 8
- const current = ref(1)
- const total = ref(0)
- const playerList = ref([])
- const webRtcServerList = <any[]>[]
- let addrList = ref<{ name: string, addr: string }[]>([])
- async function getVideoAddrs(){
- clearCamera();
- playerList.value = []
- const result = await list({ pageSize: pageSize, pageNo: current.value })
- if(result && result.records && result.records.length > 0){
- total.value = result['total']
- const cameraList = <{ name: string, addr: string }[]>[]
- const cameras = result.records
- // const camerasArr: [] = [
- // {
- // name: '1111',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: '2222',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: '3333',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: '4444',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: '5555',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: '6666',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: '7777',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: '8888',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: '9999',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: 'aaaa',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: 'bbbb',
- // devicekind: 'toHKRtsp',
- // },
- // {
- // name: 'cccc',
- // devicekind: 'toHKRtsp',
- // },
- // ];
- // const cameras = []
- // for (let index = (current.value - 1)*pageSize; index < current.value * pageSize && index < camerasArr.length ; index++) {
- // cameras.push(camerasArr[index]) ;
- // }
- for (let i = 0; i < cameras.length; i++) {
- const item = cameras[i];
-
- if (item['devicekind'] === 'toHKRtsp') {
- // 从海康平台接口获取视频流
- const data = await cameraAddr({ cameraCode: item['addr'] });
- if (data && data['url']) {
- cameraList.push({ name: item['name'], addr: data['url'] });
- }
- // cameraList.push({
- // name: item['name'],
- // // addr: 'http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8'
- // addr: 'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.mp4/.m3u8',
- // });
- } else {
- if(item['addr'].includes('0.0.0.0')){
- item['addr'].replace('0.0.0.0', window.location.hostname)
- }
- cameraList.push({ name: item['name'], addr: item['addr'] });
- }
- }
- addrList.value = cameraList
- }
- }
- function onChange(page) {
- current.value = page;
- getVideoAddrs().then(() => {
- getVideo()
- })
- }
- function getVideo() {
- const ip = VUE_APP_URL.webRtcUrl;
- for(let i = 0; i < addrList.value.length; i++){
- const item = addrList.value[i]
- if(item.addr.startsWith('rtsp://')){
- const dom = document.getElementById('video' + i) as HTMLVideoElement
- dom.muted = true;
- dom.volume = 0
- const webRtcServer = new window['WebRtcStreamer'](dom, location.protocol + ip)
- webRtcServerList.push(webRtcServer)
- webRtcServer.connect(item.addr)
- }else{
- setNoRtspVideo('player'+i,item.addr )
- }
- }
- }
- function setNoRtspVideo(id,videoAddr) {
- const fileExtension = videoAddr.split('.').pop();
- // const plugins = fileExtension === 'flv' ? [ FlvPlugin ] : [ HlsPlugin ];
- if(fileExtension === 'flv'){
- const player = new Player({
- id: id,
- url: videoAddr,
- width: 421,
- height: 292,
- poster: '/src/assets/images/vent/noSinge.png',
- plugins: [ FlvPlugin ] ,
- fluid: true,
- autoplay: true,
- isLive: true,
- playsinline: false,
- screenShot: true,
- whitelist: [''],
- ignores: ['time'],
- closeVideoClick: true,
- customConfig: {
- isClickPlayBack: false
- },
- flv: {
- retryCount: 3, // 重试 3 次,默认值
- retryDelay: 1000, // 每次重试间隔 1 秒,默认值
- loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
- fetchOptions: {
- // 该参数会透传给 fetch,默认值为 undefined
- mode: 'cors'
- },
- targetLatency: 10, // 直播目标延迟,默认 10 秒
- maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
- disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
- maxJumpDistance: 10,
- }
- });
- playerList.value.push(player)
- }
- if(fileExtension === 'm3u8'){
- let player
- if (document.createElement('video').canPlayType('application/vnd.apple.mpegurl')) {
- // 原生支持 hls 播放
- player = new Player({
- id: id,
- url: videoAddr,
- width: 421,
- height: 292,
- isLive: true,
- autoplay: true,
- autoplayMuted: true,
- cors: true,
- poster: '/src/assets/images/vent/noSinge.png',
- hls: {
- retryCount: 3, // 重试 3 次,默认值
- retryDelay: 1000, // 每次重试间隔 1 秒,默认值
- loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
- fetchOptions: {
- // 该参数会透传给 fetch,默认值为 undefined
- mode: 'cors'
- },
- targetLatency: 10, // 直播目标延迟,默认 10 秒
- maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
- disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
- maxJumpDistance: 10,
- }
-
- })
- } else if (HlsPlugin.isSupported()) { // 第一步
- player = new Player({
- id: id,
- url: videoAddr,
- width: 421,
- height: 292,
- isLive: true,
- autoplay: true,
- autoplayMuted: true,
- plugins: [HlsPlugin], // 第二步
- poster: '/src/assets/images/vent/noSinge.png',
- hls: {
- retryCount: 3, // 重试 3 次,默认值
- retryDelay: 1000, // 每次重试间隔 1 秒,默认值
- loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
- fetchOptions: {
- // 该参数会透传给 fetch,默认值为 undefined
- mode: 'cors'
- },
- targetLatency: 10, // 直播目标延迟,默认 10 秒
- maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
- disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
- maxJumpDistance: 10,
- }
- })
- }
- playerList.value.push(player)
- }
- }
-
- function goFullScreen(domId) {
- const videoDom = document.getElementById(domId) as HTMLVideoElement
- if(videoDom.requestFullscreen){
- videoDom.requestFullscreen()
- videoDom.play()
- } else if(videoDom.mozRequestFullscreen){
- videoDom.mozRequestFullscreen()
- videoDom.play()
- } else if (videoDom.webkitRequestFullscreen) {
- videoDom.webkitRequestFullscreen()
- videoDom.play()
- } else if (videoDom.msRequestFullscreen) {
- videoDom.msRequestFullscreen()
- videoDom.play()
- }
- }
- function clearCamera() {
- const num = webRtcServerList.length
- for (let i = 0; i < num; i++) {
- webRtcServerList[i].disconnect()
- webRtcServerList[i] = null
- }
- for(let i = 0; i < playerList.value.length; i++){
- const player = playerList.value[i]
- if(player.destroy)player.destroy()
- }
- }
- onMounted(async() => {
- await getVideoAddrs()
- getVideo()
- })
- onUnmounted(() => {
- clearCamera()
- })
- </script>
- <style lang="less">
-
- .camera-box{
- height: 700px;
- overflow-y: auto;
- display: flex;
- justify-content: center;
- align-items: center;
- .player-box{
- width: 451px;
- height: 312px;
- padding: 10px;
- background: url('/@/assets/images/vent/camera_bg.png');
- background-size: cover;
- position: relative;
- margin: 10px;
-
- .player-name{
- font-size: 14px;
- position: absolute;
- top: 15px;
- right: 15px;
- color: #fff;
- background-color: hsla(0, 0%, 50%, .5);
- border-radius: 2px;
- padding: 1px 5px;
- max-width: 120px;
- overflow: hidden;
- white-space: nowrap;
- text-overflow: ellipsis;
- z-index: 999;
- }
- .click-box{
- position: absolute;
- width: 100%;
- height: 100%;
- top: 0;
- left: 0;
- }
- }
- }
- .pagination{
- width: 100%;
- height: 200px;
- display: flex;
- justify-content: center;
- align-items: center;
- }
- // :deep(video){
- // width: 100% !important;
- // height: 100% !important;
- // object-fit: cover !important;
- // }
- </style>
|