From c291f5d139d4b65316e9fc6424878a49fc3ce485 Mon Sep 17 00:00:00 2001 From: qiushui Date: Tue, 28 Dec 2021 20:53:59 +0800 Subject: [PATCH] video --- public/antd_color.less | 2 +- public/index.html | 2 + public/video_play_plugins/webrtcplayer.js | 121 ++++++++++++++ src/views/realTimeMonitoring/VideoPlayer.vue | 2 +- .../realTimeMonitoring/VideoPlayerBak.vue | 157 ++++++++++++++++++ src/views/realTimeMonitoring/index.vue | 36 ++-- src/views/realTimeMonitoring/webrtcplayer.js | 122 ++++++++++++++ 7 files changed, 428 insertions(+), 14 deletions(-) create mode 100644 public/video_play_plugins/webrtcplayer.js create mode 100644 src/views/realTimeMonitoring/VideoPlayerBak.vue create mode 100644 src/views/realTimeMonitoring/webrtcplayer.js diff --git a/public/antd_color.less b/public/antd_color.less index b746b9c..bdf6854 100644 --- a/public/antd_color.less +++ b/public/antd_color.less @@ -1389,7 +1389,7 @@ tr.ant-table-expanded-row:hover {background: #fbfbfb;} .ant-tag-gold-inverse {color: #fff;background: #faad14;border-color: #faad14;} .ant-tag-cyan {color: #13c2c2;background: #e6fffb;border-color: #87e8de;} .ant-tag-cyan-inverse {color: #fff;background: #13c2c2;border-color: #13c2c2;} -.ant-tag-lime {color: #a0d911;background: #fcffe6;border-color: #eaff8f;} +.ant-tag-lime {color: #a0d911;background: color(~`colorPalette("@{text-color}", 1)`);border-color: #eaff8f;} .ant-tag-lime-inverse {color: #fff;background: #a0d911;border-color: #a0d911;} .ant-tag-green {color: #52c41a;background: #f6ffed;border-color: #b7eb8f;} .ant-tag-green-inverse {color: #fff;background: #52c41a;border-color: #52c41a;} diff --git a/public/index.html b/public/index.html index bb29b1d..b0c00e3 100644 --- a/public/index.html +++ b/public/index.html @@ -10,6 +10,8 @@ + + 垛机视觉系统 diff --git a/public/video_play_plugins/webrtcplayer.js b/public/video_play_plugins/webrtcplayer.js new file mode 100644 index 0000000..f5282c8 --- /dev/null +++ b/public/video_play_plugins/webrtcplayer.js @@ -0,0 +1,121 @@ +class WebRtcPlayer { + static server = '127.0.0.1:8083'; + webrtc = null; + video = null; + server = null; + codecLink = null; + rsdpLink = null; + stream = new MediaStream(); + uuid = null; + options={ + onStatusChange:null + }; + + constructor(id, uuid, options={}) { + this.server = WebRtcPlayer.server; + this.video = document.getElementById(id); + this.uuid = uuid; + Object.assign(this.options, options); + this.createLinks(); + this.play(); + } + + createLinks() { + this.codecLink = "//" + this.server + "/stream/codec/" + this.uuid + this.rsdpLink = "//" + this.server + "/stream/receiver/" + this.uuid + } + + play() { + this.webrtc = new RTCPeerConnection({ + iceServers: [{ + urls: ["stun:stun.l.google.com:19302"] + }] + }); + if(this.webrtc){ + + }else{ + console.log("no") + } + this.webrtc.onnegotiationneeded = this.handleNegotiationNeeded.bind(this); + this.webrtc.ontrack = this.onTrack.bind(this); + fetch(this.codecLink) + .then((response) => { + response.json().then((data) => { + data.forEach((item, i) => { + this.webrtc.addTransceiver(item.Type, { + 'direction': 'sendrecv' + }); + }); + }); + }) + .catch((error) => { + console.log(error); + }); + + this.webrtc.onconnectionstatechange = () => { + if(this.webrtc.connectionState == 'connected' || this.webrtc.connectionState == 'connecting'){ + + }else{ + console.log(this.webrtc.connectionState) + this.load(this.uuid); + } + + } + } + + async handleNegotiationNeeded() { + let offer = await this.webrtc.createOffer(); + await this.webrtc.setLocalDescription(offer); + let formData = new FormData(); + formData.append('suuid', this.uuid); + formData.append('data', btoa(this.webrtc.localDescription.sdp)); + fetch(this.rsdpLink, { + method: 'POST', + body: formData + }) + .then((response) => { + response.text().then((data) => { + this.webrtc.setRemoteDescription(new RTCSessionDescription({ + type: 'answer', + sdp: atob(data) + })) + }); + }) + .catch((err) => {}) + } + + onTrack(event) { + this.stream.addTrack(event.track); + this.video.srcObject = this.stream; + this.video.play(); + } + + load(uuid) { + this.destroy(); + this.uuid = uuid; + this.createLinks(); + this.play(); + } + + destroy() { + + this.webrtc.close(); + this.webrtc = null; + this.video.srcObject = null; + this.stream = new MediaStream(); + } + + getImageUrl() { + let canvas = document.createElement("canvas"); + canvas.width = this.video.videoWidth; + canvas.height = this.video.videoHeight; + canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height); + let dataURL = canvas.toDataURL(); + canvas.remove(); + return dataURL; + } + + static setServer(serv) { + this.server = serv; + } +} diff --git a/src/views/realTimeMonitoring/VideoPlayer.vue b/src/views/realTimeMonitoring/VideoPlayer.vue index cb3313b..ac04300 100644 --- a/src/views/realTimeMonitoring/VideoPlayer.vue +++ b/src/views/realTimeMonitoring/VideoPlayer.vue @@ -17,7 +17,7 @@ + + diff --git a/src/views/realTimeMonitoring/index.vue b/src/views/realTimeMonitoring/index.vue index d3fcc42..2d551cd 100644 --- a/src/views/realTimeMonitoring/index.vue +++ b/src/views/realTimeMonitoring/index.vue @@ -14,11 +14,12 @@ marginBottom: ((selectTab.videoStyleRow * selectTab.videoStyleColumn - index - 1) >= selectTab.videoStyleColumn) ? '20px': 0 }" > - + @@ -26,11 +27,11 @@ diff --git a/src/views/realTimeMonitoring/webrtcplayer.js b/src/views/realTimeMonitoring/webrtcplayer.js new file mode 100644 index 0000000..db142a6 --- /dev/null +++ b/src/views/realTimeMonitoring/webrtcplayer.js @@ -0,0 +1,122 @@ +class WebRtcPlayer { + static server = '127.0.0.1:8083'; + webrtc = null; + video = null; + server = null; + codecLink = null; + rsdpLink = null; + stream = new MediaStream(); + uuid = null; + options={ + onStatusChange:null + }; + + constructor(id, uuid, options={}) { + this.server = WebRtcPlayer.server; + this.video = document.getElementById(id); + this.uuid = uuid; + Object.assign(this.options, options); + this.createLinks(); + this.play(); + } + + createLinks() { + this.codecLink = "//" + this.server + "/stream/codec/" + this.uuid + this.rsdpLink = "//" + this.server + "/stream/receiver/" + this.uuid + } + + play() { + this.webrtc = new RTCPeerConnection({ + iceServers: [{ + urls: ["stun:stun.l.google.com:19302"] + }] + }); + if(this.webrtc){ + + }else{ + console.log("no") + } + this.webrtc.onnegotiationneeded = this.handleNegotiationNeeded.bind(this); + this.webrtc.ontrack = this.onTrack.bind(this); + fetch(this.codecLink) + .then((response) => { + response.json().then((data) => { + data.forEach((item, i) => { + this.webrtc.addTransceiver(item.Type, { + 'direction': 'sendrecv' + }); + }); + }); + }) + .catch((error) => { + console.log(error); + }); + + this.webrtc.onconnectionstatechange = () => { + if(this.webrtc.connectionState == 'connected' || this.webrtc.connectionState == 'connecting'){ + + }else{ + console.log(this.webrtc.connectionState) + this.load(this.uuid); + } + + } + } + + async handleNegotiationNeeded() { + let offer = await this.webrtc.createOffer(); + await this.webrtc.setLocalDescription(offer); + let formData = new FormData(); + formData.append('suuid', this.uuid); + formData.append('data', btoa(this.webrtc.localDescription.sdp)); + fetch(this.rsdpLink, { + method: 'POST', + body: formData + }) + .then((response) => { + response.text().then((data) => { + this.webrtc.setRemoteDescription(new RTCSessionDescription({ + type: 'answer', + sdp: atob(data) + })) + }); + }) + .catch((err) => {}) + } + + onTrack(event) { + this.stream.addTrack(event.track); + this.video.srcObject = this.stream; + this.video.play(); + } + + load(uuid) { + this.destroy(); + this.uuid = uuid; + this.createLinks(); + this.play(); + } + + destroy() { + + this.webrtc.close(); + this.webrtc = null; + this.video.srcObject = null; + this.stream = new MediaStream(); + } + + getImageUrl() { + let canvas = document.createElement("canvas"); + canvas.width = this.video.videoWidth; + canvas.height = this.video.videoHeight; + canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height); + let dataURL = canvas.toDataURL(); + canvas.remove(); + return dataURL; + } + + static setServer(serv) { + this.server = serv; + } +} +export default WebRtcPlayer;