Jelajahi Sumber

feat(core): update

gemercheung 3 tahun lalu
induk
melakukan
ecf145a3cb
2 mengubah file dengan 54 tambahan dan 24 penghapusan
  1. 19 12
      webrtc/srs.js
  2. 35 12
      webrtc/srsRtc.js

+ 19 - 12
webrtc/srs.js

@@ -12,33 +12,40 @@ console.log('sdk',sdk)
 console.log('sdk',connection)
 
 connection.socket.emit('pushVideo',{
-    videoList:['0-1-0'],
-    videoId: 'mini'
+    // videoList:['0/0_1_0'],
+    videoList:['sample-5s','output'],
+    videoId: 'mini1'
 })
 setTimeout(()=>{
     console.log('第二个')
+
     connection.socket.emit('pushVideo',{
-        videoList:['0-1-0'],
-        videoId: 'mini'
+        // videoList:['0/0_1_0'],
+        videoList:['output'],
+        videoId: 'mini1'
     })
-},5000)
+
+},10000)
 
 
 
 setTimeout(()=>{
     console.log('第三个')
     connection.socket.emit('pushVideo',{
-        videoList:['0-1-0'],
-        videoId: 'mini'
+        videoList:["sample-5s"],
+        videoId: 'mini1'
     })
-},12000)
+},20000)
 
 window.connection =connection
-connection.socket.on('getVideo',(videoId)=>{
-    console.log('getVideo',videoId)
+connection.socket.on('getVideo',async(videoId)=>{
+
     if(videoId){
-        const steamUrl ='webrtc://demo-kms.4dage.com/live/' + videoId
-        getVideo(steamUrl)
+        const steamUrl ='webrtc://demo-kms.4dage.com/live/' + videoId;
+        
+        console.log('getVideo',videoId);
+        sdk.close();
+        await getVideo(steamUrl)
     }
 })
 

+ 35 - 12
webrtc/srsRtc.js

@@ -1,27 +1,52 @@
 
 export class SrsRTC {
     constructor(url){
-        this.pc = new RTCPeerConnection(null);
+       
         this.stream = new MediaStream();
         this.url = url;
-        this.pc.ontrack =  (event) => {
-            // https://webrtc.org/getting-started/remote-streams
-            console.log('event',event.track)
-            if(this.stream){
-                this.stream.addTrack(event.track);
-            }
-     
-       
-        };
+        this.offer = null;
+        this.pc  =null;
+        // this.pc = new RTCPeerConnection(null);
+        // this.pc.ontrack =  (event) => {
+        //         // https://webrtc.org/getting-started/remote-streams
+        //     console.warn('event',event.track)
+        //     if(this.stream){
+        //         this.stream.addTrack(event.track);
+        //     }
+        // };
 
     }
+    close(){
+        this.pc && this.pc.close();
+        this.pc = null;
+    }
  
     async getVideo(clipUrl){
+
+        if(!this.pc){
+            this.pc = new RTCPeerConnection(null);  
+            this.pc.ontrack =  (event) => {
+                // https://webrtc.org/getting-started/remote-streams
+                console.log('event',event.track)
+                if(this.stream){
+                    // this.stream.removeTrack(event.track));
+                    const videoTrack = this.stream.getVideoTracks().find(i=>i.kind ==='video')
+                    console.log('videoTrack',videoTrack)
+                    if(videoTrack){
+                        this.stream.removeTrack(videoTrack);
+                    }
+                    // if(this.stream.removeTrack())
+                    this.stream.addTrack(event.track);
+                }
+            };
+        }
+     
         this.pc.addTransceiver("video", {direction: "recvonly"});
 
         const serverApi ='https://demo-kms.4dage.com:443/rtc/v1/play/';
         const tid = Number(parseInt(new Date().getTime()*Math.random()*100)).toString(16).slice(0, 7);
         var offer = await this.pc.createOffer();
+        this.offer  = offer;
         await this.pc.setLocalDescription(offer);
         const session = await new Promise(function(resolve, reject) {
             // @see https://github.com/rtcdn/rtcdn-draft
@@ -32,9 +57,7 @@ export class SrsRTC {
                 clientip: null, 
                 sdp: offer.sdp
             };
-
             console.log("Generated offer: ", data);
-
             $.ajax({
                 type: "POST", url: serverApi, data: JSON.stringify(data),
                 contentType:'application/json', dataType: 'json'