diff options
author | dakkar <dakkar@thenautilus.net> | 2020-07-25 11:31:23 +0000 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2020-07-25 11:56:19 +0000 |
commit | c6ab41dc8c9816a2a457f61b5d90d2436dfbb471 (patch) | |
tree | c34b53e7d2dd8f151ad2b638b194a816b427c24d | |
parent | maybe new deps? mah (diff) | |
download | rtwatch-c6ab41dc8c9816a2a457f61b5d90d2436dfbb471.tar.gz rtwatch-c6ab41dc8c9816a2a457f61b5d90d2436dfbb471.tar.bz2 rtwatch-c6ab41dc8c9816a2a457f61b5d90d2436dfbb471.zip |
changes from another guy
https://github.com/ahamlinman/rtwatch/commit/eb91a43eeccc5959ea981dbc6eef10978d5179fb
-rw-r--r-- | gst/gst.go | 24 | ||||
-rw-r--r-- | index.html | 45 | ||||
-rw-r--r-- | main.go | 63 |
3 files changed, 83 insertions, 49 deletions
@@ -31,9 +31,31 @@ type Pipeline struct { var pipeline = &Pipeline{} var pipelinesLock sync.Mutex +var pipelineFmtStr = ` +filesrc location="%s" +! decodebin name=demux + +demux. +! queue +! videoconvert +! videoscale +! video/x-raw,width=800,height=600 +! x264enc bframes=0 speed-preset=veryfast key-int-max=60 +! video/x-h264,stream-format=byte-stream +! appsink name=video + +demux. +! queue +! audioconvert +! audioresample +! audio/x-raw,rate=48000 +! opusenc bitrate=128000 +! appsink name=audio +` + // CreatePipeline creates a GStreamer Pipeline func CreatePipeline(containerPath string, audioTrack, videoTrack *webrtc.Track) *Pipeline { - pipelineStr := fmt.Sprintf("filesrc location=\"%s\" ! decodebin name=demux ! queue ! x264enc bframes=0 speed-preset=veryfast key-int-max=60 ! video/x-h264,stream-format=byte-stream ! appsink name=video demux. ! queue ! audioconvert ! audioresample ! opusenc ! appsink name=audio", containerPath) + pipelineStr := fmt.Sprintf(pipelineFmtStr, containerPath) pipelineStrUnsafe := C.CString(pipelineStr) defer C.free(unsafe.Pointer(pipelineStrUnsafe)) @@ -15,8 +15,10 @@ </div> <script> - let conn = new WebSocket('wss://' + window.location.host + window.location.pathname + '/ws/') + let pc = new RTCPeerConnection() + pc.addTransceiver('video', {direction: 'recvonly'}); + pc.addTransceiver('audio', {direction: 'recvonly'}); window.seekClick = () => { conn.send(JSON.stringify({event: 'seek', data: document.getElementById('seekTime').value})) @@ -30,26 +32,19 @@ pc.ontrack = function (event) { if (event.track.kind === 'audio') { - //return + return } var el = document.getElementById('video1') - el.srcObject = event.streams[0] - el.autoplay = true - el.controls = true + el.srcObject = event.streams[0]; + el.autoplay = true; + el.controls = true; } - conn.onopen = () => { - navigator.mediaDevices.getUserMedia({video: true, audio: true}).then(g => { - for (const track of g.getTracks()) { - pc.addTrack(track,g); - } + let conn = new WebSocket('wss://' + window.location.host + window.location.pathname + '/ws/') + window.conn = conn - pc.createOffer({offerToReceiveVideo: true, offerToReceiveAudio: true}).then(offer => { - return pc.setLocalDescription(offer); - }).then(() => { - conn.send(JSON.stringify({event: 'offer', data: JSON.stringify(pc.localDescription)})) - }) - }).catch(e => { console.log(`failed getUserMedia: ${e}`) }); + conn.onopen = () => { + console.log('Connection opened') } conn.onclose = evt => { @@ -63,15 +58,21 @@ } switch (msg.event) { - case 'answer': - answer = JSON.parse(msg.data) - if (!answer) { - return console.log('failed to parse answer') + case 'offer': + offer = JSON.parse(msg.data) + if (!offer) { + return console.log('failed to parse offer') } - pc.setRemoteDescription(answer) + console.log('Received offer', offer); + (async () => { + pc.setRemoteDescription(offer); + const answer = await pc.createAnswer(); + await pc.setLocalDescription(answer); + console.log('Sending answer', answer); + conn.send(JSON.stringify({event: 'answer', data: JSON.stringify(answer)})); + })(); } } - window.conn = conn </script> </body> </html> @@ -45,6 +45,7 @@ func main() { panic("-container-path must be specified") } + log.Println("Initializing WebRTC PeerConnection") pc, err := webrtc.NewPeerConnection(webrtc.Configuration{ ICEServers: []webrtc.ICEServer{ { @@ -56,16 +57,18 @@ func main() { log.Fatal(err) } - videoTrack, err = pc.NewTrack(webrtc.DefaultPayloadTypeH264, 5000, "synced-video", "synced-video") + log.Println("Initializing WebRTC tracks") + videoTrack, err = pc.NewTrack(webrtc.DefaultPayloadTypeH264, 5000, "sync", "sync") if err != nil { log.Fatal(err) } - audioTrack, err = pc.NewTrack(webrtc.DefaultPayloadTypeOpus, 5001, "synced-video", "synced-video") + audioTrack, err = pc.NewTrack(webrtc.DefaultPayloadTypeOpus, 5001, "sync", "sync") if err != nil { log.Fatal(err) } + log.Println("Creating and starting pipeline") pipeline = gst.CreatePipeline(containerPath, audioTrack, videoTrack) pipeline.Start() @@ -87,33 +90,13 @@ func handleWebsocketMessage(pc *webrtc.PeerConnection, ws *websocket.Conn, messa log.Print(err) } pipeline.SeekToTime(i) - case "offer": - offer := webrtc.SessionDescription{} - if err := json.Unmarshal([]byte(message.Data), &offer); err != nil { + case "answer": + answer := webrtc.SessionDescription{} + if err := json.Unmarshal([]byte(message.Data), &answer); err != nil { return err } - if err := pc.SetRemoteDescription(offer); err != nil { - return err - } - - answer, err := pc.CreateAnswer(nil) - if err != nil { - return err - } - if err := pc.SetLocalDescription(answer); err != nil { - return err - } - - answerString, err := json.Marshal(answer) - if err != nil { - return err - } - - if err = ws.WriteJSON(&websocketMessage{ - Event: "answer", - Data: string(answerString), - }); err != nil { + if err := pc.SetRemoteDescription(answer); err != nil { return err } } @@ -133,6 +116,8 @@ func serveWs(w http.ResponseWriter, r *http.Request) { return } + defer ws.Close() + peerConnection, err := webrtc.NewPeerConnection(peerConnectionConfig) if err != nil { log.Print(err) @@ -151,6 +136,32 @@ func serveWs(w http.ResponseWriter, r *http.Request) { } }() + sdp, err := peerConnection.CreateOffer(nil) + if err != nil { + log.Println(err) + return + } + + if err := peerConnection.SetLocalDescription(sdp); err != nil { + log.Println(err) + return + } + + sdpData, err := json.Marshal(sdp) + if err != nil { + log.Print(err) + return + } + + offerMsg := &websocketMessage{ + Event: "offer", + Data: string(sdpData), + } + if err := ws.WriteJSON(offerMsg); err != nil { + log.Print(err) + return + } + message := &websocketMessage{} for { _, msg, err := ws.ReadMessage() |