Explorar el Código

Merge branch 'master' of http://192.168.0.115:3000/zhouenguang/PrimordialUniverse

zhouenguang hace 3 años
padre
commit
c87d61eb8d
Se han modificado 3 ficheros con 155 adiciones y 36 borrados
  1. 61 34
      webrtc/srs.js
  2. 83 0
      webrtc/srsRtc copy.js
  3. 11 2
      webrtc/srsRtc.js

+ 61 - 34
webrtc/srs.js

@@ -3,49 +3,82 @@ import {SrsRTC } from './SrsRTC.js';
 import { Connection } from './connection.js';
 const sdk = new SrsRTC();
 const testConfig ={
-    userId: '11111',
-    roomId: '111',
+    sceneCode:"testApp",
+    userId: 'testUser',
+    roomId: '8888',
 }
 const connection =new Connection(testConfig);
 
 console.log('sdk',sdk)
 console.log('sdk',connection)
 
+// console.log('第一个')
 connection.socket.emit('pushVideo',{
-    // videoList:['0/0_1_0'],
     videoList:['sample-5s','output'],
-    videoId: 'mini1'
+    sessionId:'1',
+    sceneCode:testConfig.sceneCode,
+    roomId:testConfig.roomId,
+    userId:testConfig.userId,
 })
-setTimeout(()=>{
-    console.log('第二个')
-
-    connection.socket.emit('pushVideo',{
-        // videoList:['0/0_1_0'],
-        videoList:['output'],
-        videoId: 'mini1'
-    })
-
-},10000)
+// setTimeout(()=>{
+//     console.log('第二个')
 
+//     connection.socket.emit('pushVideo',{
+//         videoList:['output'],
+//         sessionId:'',
+//         sceneCode:testConfig.sceneCode,
+//         roomId:testConfig.roomId,
+//         userId:testConfig.userId,
+//     })
 
+// },10000)
 
 setTimeout(()=>{
-    console.log('第三个')
-    connection.socket.emit('pushVideo',{
-        videoList:["sample-5s"],
-        videoId: 'mini1'
+    console.log('第二个')
+    connection.socket.emit('rotate',{
+        videoPath:'sample-5s',
+        sessionId:'2',
+        sceneCode:testConfig.sceneCode,
+        roomId:testConfig.roomId,
+        userId:testConfig.userId,
+        sangle: '0',
+        eangle:'900',
+        reverses:true,
     })
-},20000)
-
-window.connection =connection
-connection.socket.on('getVideo',async(videoId)=>{
-
-    if(videoId){
-        const steamUrl ='webrtc://demo-kms.4dage.com/live/' + videoId;
-        
-        console.log('getVideo',videoId);
+},12000)
+
+// setTimeout(()=>{
+//     console.log('正转')
+//     connection.socket.emit('rotate',{
+//         videoList:['output'],
+//         sessionId:'3',
+//         sceneCode:testConfig.sceneCode,
+//         roomId:testConfig.roomId,
+//         userId:testConfig.userId,
+//     })
+// },14000)
+
+
+
+// setTimeout(()=>{
+//     console.log('第三个')
+//     connection.socket.emit('pushVideo',{
+//         videoList:['sample-5s'],
+//         sessionId:'4',
+//         sceneCode:testConfig.sceneCode,
+//         roomId:testConfig.roomId,
+//         userId:testConfig.userId,
+//     })
+// },20000)
+
+window.connection =connection;
+
+connection.socket.on('getVideo',async(streamUrl)=>{
+    if(streamUrl){
+        const url ='webrtc://demo-kms.4dage.com/' + streamUrl;
+        console.log('getVideo',streamUrl);
         sdk.close();
-        await getVideo(steamUrl)
+        await getVideo(url)
     }
 })
 
@@ -62,11 +95,5 @@ const getVideo =(videoUrl)=>{
 
 
 
-// sdk.pc.ondatachannel = function(ev) {
-//     console.log('Data channel is created!');
-//     ev.channel.onopen = function() {
-//       console.log('Data channel is open and ready to be used.');
-//     };
-//   };
 
 

+ 83 - 0
webrtc/srsRtc copy.js

@@ -0,0 +1,83 @@
+
+export class SrsRTC {
+    constructor(url){
+       
+        this.stream = new MediaStream();
+        this.url = url;
+        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
+            var data = {
+                api: serverApi,
+                tid: tid,
+                streamurl: clipUrl,
+                clientip: null, 
+                sdp: offer.sdp
+            };
+            console.log("Generated offer: ", data);
+            $.ajax({
+                type: "POST", url: serverApi, data: JSON.stringify(data),
+                contentType:'application/json', dataType: 'json'
+            }).done(function(data) {
+                console.log("Got answer: ", data);
+                if (data.code) {
+                    reject(data); return;
+                }
+
+                resolve(data);
+            }).fail(function(reason){
+                reject(reason);
+            });
+        });
+        await this.pc.setRemoteDescription(
+            new RTCSessionDescription({type: 'answer', sdp: session.sdp})
+        );
+        
+    
+        return session;
+        
+    }
+}

+ 11 - 2
webrtc/srsRtc.js

@@ -17,6 +17,10 @@ export class SrsRTC {
 
     }
     close(){
+        var remoteVideo = document.querySelector("#testVideoFeed");
+        if(remoteVideo.srcObject){
+            remoteVideo.srcObject.getTracks().forEach(track => track.stop()); 
+        }
         this.pc && this.pc.close();
         this.pc = null;
     }
@@ -27,17 +31,22 @@ export class SrsRTC {
             this.pc = new RTCPeerConnection(null);  
             this.pc.ontrack =  (event) => {
                 // https://webrtc.org/getting-started/remote-streams
-                console.log('event',event.track)
+                console.log('event',event)
                 if(this.stream){
                     // this.stream.removeTrack(event.track));
                     const videoTrack = this.stream.getVideoTracks().find(i=>i.kind ==='video')
-                    console.log('videoTrack',videoTrack)
+                    console.log('videoTrack',videoTrack)
                     if(videoTrack){
                         this.stream.removeTrack(videoTrack);
                     }
                     // if(this.stream.removeTrack())
                     this.stream.addTrack(event.track);
                 }
+               const  mediaRecorder = new MediaRecorder(event.streams[0]);
+               console.log('mediaRecorder',mediaRecorder)
+            //    mediaRecorder.
+
+
             };
         }