@@ -11,6 +11,7 @@ function MonitorStream(monitorData) {
1111 this . height = monitorData . height ;
1212 this . RTSP2WebEnabled = monitorData . RTSP2WebEnabled ;
1313 this . RTSP2WebType = monitorData . RTSP2WebType ;
14+ this . webrtc = null ;
1415 this . mseStreamingStarted = false ;
1516 this . mseQueue = [ ] ;
1617 this . mseSourceBuffer = null ;
@@ -291,7 +292,7 @@ function MonitorStream(monitorData) {
291292 } else if ( this . RTSP2WebType == 'WebRTC' ) {
292293 const webrtcUrl = rtsp2webModUrl ;
293294 webrtcUrl . pathname = "/stream/" + this . id + "/channel/0/webrtc" ;
294- startRTSP2WebPlay ( videoEl , webrtcUrl . href ) ;
295+ startRTSP2WebPlay ( videoEl , webrtcUrl . href , this ) ;
295296 }
296297 this . statusCmdTimer = setInterval ( this . statusCmdQuery . bind ( this ) , statusRefreshTimeout ) ;
297298 this . started = true ;
@@ -344,6 +345,10 @@ function MonitorStream(monitorData) {
344345 this . statusCmdTimer = clearInterval ( this . statusCmdTimer ) ;
345346 this . streamCmdTimer = clearInterval ( this . streamCmdTimer ) ;
346347 this . started = false ;
348+ if ( this . webrtc ) {
349+ this . webrtc . close ( ) ;
350+ this . webrtc = null ;
351+ }
347352 } ;
348353
349354 this . kill = function ( ) {
@@ -976,35 +981,35 @@ const waitUntil = (condition) => {
976981 } ) ;
977982} ;
978983
979- function startRTSP2WebPlay ( videoEl , url ) {
980- const webrtc = new RTCPeerConnection ( {
984+ function startRTSP2WebPlay ( videoEl , url , stream ) {
985+ stream . webrtc = new RTCPeerConnection ( {
981986 iceServers : [ {
982987 urls : [ 'stun:stun.l.google.com:19302' ]
983988 } ] ,
984989 sdpSemantics : 'unified-plan'
985990 } ) ;
986- webrtc . ontrack = function ( event ) {
991+ stream . webrtc . ontrack = function ( event ) {
987992 console . log ( event . streams . length + ' track is delivered' ) ;
988993 videoEl . srcObject = event . streams [ 0 ] ;
989994 videoEl . play ( ) ;
990995 } ;
991- webrtc . addTransceiver ( 'video' , { direction : 'sendrecv' } ) ;
992- webrtc . onnegotiationneeded = async function handleNegotiationNeeded ( ) {
993- const offer = await webrtc . createOffer ( ) ;
996+ stream . webrtc . addTransceiver ( 'video' , { direction : 'sendrecv' } ) ;
997+ stream . webrtc . onnegotiationneeded = async function handleNegotiationNeeded ( ) {
998+ const offer = await stream . webrtc . createOffer ( ) ;
994999
995- await webrtc . setLocalDescription ( offer ) ;
1000+ await stream . webrtc . setLocalDescription ( offer ) ;
9961001
9971002 fetch ( url , {
9981003 method : 'POST' ,
999- body : new URLSearchParams ( { data : btoa ( webrtc . localDescription . sdp ) } )
1004+ body : new URLSearchParams ( { data : btoa ( stream . webrtc . localDescription . sdp ) } )
10001005 } )
10011006 . catch ( ( rejected ) => {
10021007 console . log ( rejected ) ;
10031008 } )
10041009 . then ( ( response ) => response . text ( ) )
10051010 . then ( ( data ) => {
10061011 try {
1007- webrtc . setRemoteDescription (
1012+ stream . webrtc . setRemoteDescription (
10081013 new RTCSessionDescription ( { type : 'answer' , sdp : atob ( data ) } )
10091014 ) ;
10101015 } catch ( e ) {
@@ -1013,14 +1018,16 @@ function startRTSP2WebPlay(videoEl, url) {
10131018 } ) ;
10141019 } ;
10151020
1016- const webrtcSendChannel = webrtc . createDataChannel ( 'rtsptowebSendChannel' ) ;
1021+ const webrtcSendChannel = stream . webrtc . createDataChannel ( 'rtsptowebSendChannel' ) ;
10171022 webrtcSendChannel . onopen = ( event ) => {
10181023 console . log ( `${ webrtcSendChannel . label } has opened` ) ;
10191024 webrtcSendChannel . send ( 'ping' ) ;
10201025 } ;
10211026 webrtcSendChannel . onclose = ( _event ) => {
10221027 console . log ( `${ webrtcSendChannel . label } has closed` ) ;
1023- startRTSP2WebPlay ( videoEl , url ) ;
1028+ if ( stream . started ) {
1029+ startRTSP2WebPlay ( videoEl , url , stream ) ;
1030+ }
10241031 } ;
10251032 webrtcSendChannel . onmessage = ( event ) => console . log ( event . data ) ;
10261033}
0 commit comments