@@ -996,40 +996,71 @@ const waitUntil = (condition) => {
996996} ;
997997
998998function startRTSP2WebPlay ( videoEl , url , stream ) {
999+ const mediaStream = new MediaStream ( ) ;
1000+ videoEl . srcObject = mediaStream ;
9991001 stream . webrtc = new RTCPeerConnection ( {
10001002 iceServers : [ {
10011003 urls : [ 'stun:stun.l.google.com:19302' ]
10021004 } ] ,
10031005 sdpSemantics : 'unified-plan'
10041006 } ) ;
1005- stream . webrtc . ontrack = function ( event ) {
1006- console . log ( event . streams . length + ' track is delivered' ) ;
1007- videoEl . srcObject = event . streams [ 0 ] ;
1008- videoEl . play ( ) ;
1007+
1008+ /* It doesn't work yet
1009+ stream.webrtc.ondatachannel = function(event) {
1010+ console.log('onDataChannel trigger:', event.channel);
1011+ event.channel.onopen = () => console.log(`Data channel is open`);
1012+ event.channel.onmessage = (event) => console.log('Event data:', event.data);
10091013 };
1010- stream . webrtc . addTransceiver ( 'video' , { direction : 'sendrecv' } ) ;
1011- stream . webrtc . onnegotiationneeded = async function handleNegotiationNeeded ( ) {
1012- const offer = await stream . webrtc . createOffer ( ) ;
1014+ */
10131015
1016+ stream . webrtc . oniceconnectionstatechange = function ( event ) {
1017+ console . log ( 'iceServer changed state to: ' , '"' , event . currentTarget . connectionState , '"' ) ;
1018+ } ;
1019+ stream . webrtc . onnegotiationneeded = async function handleNegotiationNeeded ( ) {
1020+ const offer = await stream . webrtc . createOffer ( {
1021+ //iceRestart:true,
1022+ offerToReceiveAudio : true ,
1023+ offerToReceiveVideo : true
1024+ } ) ;
10141025 await stream . webrtc . setLocalDescription ( offer ) ;
1026+ $j . post ( url , {
1027+ data : btoa ( stream . webrtc . localDescription . sdp )
1028+ } , function ( data ) {
1029+ try {
1030+ stream . webrtc . setRemoteDescription ( new RTCSessionDescription ( {
1031+ type : 'answer' ,
1032+ sdp : atob ( data )
1033+ } ) ) ;
1034+ } catch ( e ) {
1035+ console . warn ( e ) ;
1036+ }
1037+ } ) ;
1038+ } ;
1039+ stream . webrtc . onsignalingstatechange = async function signalingstatechange ( ) {
1040+ switch ( stream . webrtc . signalingState ) {
1041+ case 'have-local-offer' :
1042+ break ;
1043+ case 'stable' :
1044+ /*
1045+ * There is no ongoing exchange of offer and answer underway.
1046+ * This may mean that the RTCPeerConnection object is new, in which case both the localDescription and remoteDescription are null;
1047+ * it may also mean that negotiation is complete and a connection has been established.
1048+ */
1049+ break ;
1050+ case 'closed' :
1051+ /*
1052+ * The RTCPeerConnection has been closed.
1053+ */
1054+ break ;
1055+ default :
1056+ console . log ( `unhandled signalingState is ${ stream . webrtc . signalingState } ` ) ;
1057+ break ;
1058+ }
1059+ } ;
10151060
1016- fetch ( url , {
1017- method : 'POST' ,
1018- body : new URLSearchParams ( { data : btoa ( stream . webrtc . localDescription . sdp ) } )
1019- } )
1020- . catch ( ( rejected ) => {
1021- console . log ( rejected ) ;
1022- } )
1023- . then ( ( response ) => response . text ( ) )
1024- . then ( ( data ) => {
1025- try {
1026- stream . webrtc . setRemoteDescription (
1027- new RTCSessionDescription ( { type : 'answer' , sdp : atob ( data ) } )
1028- ) ;
1029- } catch ( e ) {
1030- console . warn ( e ) ;
1031- }
1032- } ) ;
1061+ stream . webrtc . ontrack = function ontrack ( event ) {
1062+ console . log ( event . track . kind + ' track is delivered' ) ;
1063+ mediaStream . addTrack ( event . track ) ;
10331064 } ;
10341065
10351066 const webrtcSendChannel = stream . webrtc . createDataChannel ( 'rtsptowebSendChannel' ) ;
0 commit comments