Skip to content

Instantly share code, notes, and snippets.

@Sean-Der
Last active December 26, 2021 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sean-Der/2b5ce40d0070f1b580a4eb292260ae9d to your computer and use it in GitHub Desktop.
Save Sean-Der/2b5ce40d0070f1b580a4eb292260ae9d to your computer and use it in GitHub Desktop.
diff --git a/Live.go b/Live.go
index 0096cb7..0051ef4 100644
--- a/Live.go
+++ b/Live.go
@@ -8,6 +8,7 @@ import (
"runtime/debug"
"sync"
+ "github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
)
@@ -62,14 +63,27 @@ func SignalingServer(w http.ResponseWriter, r *http.Request) {
peer.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
defer ErrorHandler()
log.Println(`got track!`)
+
+ var lastTimestamp uint32
for {
- packet, _, e := track.ReadRTP()
- if e != nil {
- panic(e)
+ rtp, _, readErr := track.ReadRTP()
+ if readErr != nil {
+ panic(readErr)
}
- if e := whiteboard.WriteRTP(packet); e != nil {
- panic(e)
+
+ if writeErr := peer.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}}); writeErr != nil {
+ panic(writeErr)
+ }
+
+ oldTimestamp := rtp.Timestamp
+ if lastTimestamp == 0 {
+ rtp.Timestamp = 0
+ } else {
+ rtp.Timestamp -= lastTimestamp
}
+ lastTimestamp = oldTimestamp
+
+ packets <- rtp
}
})
if e := signaler.SendSignal(Signal{`offer-request`, `{}`}); e != nil {
diff --git a/Main.go b/Main.go
index 775aa7d..26d52ce 100644
--- a/Main.go
+++ b/Main.go
@@ -2,12 +2,15 @@
package main
import (
+ "errors"
+ "io"
"log"
"net/http"
"sync"
"github.com/gorilla/websocket"
"github.com/pion/interceptor"
+ "github.com/pion/rtp"
"github.com/pion/webrtc/v3"
)
@@ -22,6 +25,7 @@ var (
}
whiteboard *webrtc.TrackLocalStaticRTP
api *webrtc.API
+ packets chan *rtp.Packet
)
//Run setup and host webserver on https://localhost/
@@ -30,7 +34,7 @@ func main() {
http.Handle(`/`, http.FileServer(http.Dir(`frontend`)))
http.HandleFunc(`/connect`, SignalingServer)
log.Println(`Server Initialized`)
- log.Fatal(http.ListenAndServeTLS(`:443`, `server.crt`, `server.key`, nil))
+ log.Fatal(http.ListenAndServe(`:8080`, nil))
}
//register codecs to receive
@@ -64,6 +68,26 @@ func RTCSetup() {
`whiteboard`,
`whiteboard`,
)
+
+ packets = make(chan *rtp.Packet, 60)
+ go func() {
+ var currTimestamp uint32
+ for i := uint16(0); ; i++ {
+ packet := <-packets
+ currTimestamp += packet.Timestamp
+
+ packet.Timestamp = currTimestamp
+ packet.SequenceNumber = i
+
+ if err := whiteboard.WriteRTP(packet); err != nil {
+ if errors.Is(err, io.ErrClosedPipe) {
+ return
+ }
+
+ panic(err)
+ }
+ }
+ }()
}
//SignalingSocket is a thread safe WebSocket used only for establishing WebRTC connections
diff --git a/frontend/index.js b/frontend/index.js
index 85e572e..c9535cf 100644
--- a/frontend/index.js
+++ b/frontend/index.js
@@ -6,13 +6,13 @@ const broadcast = document.getElementById(`broadcast`),
// set up connections
function formatSignal(event, data) {
- return JSON.stringify({
- event: event,
+ return JSON.stringify({
+ event: event,
data: JSON.stringify(data)
});
}
-const ws = new WebSocket(`wss://${location.hostname}:${location.port}/connect`); //create a websocket for WebRTC signaling
+const ws = new WebSocket(`ws://${location.hostname}:${location.port}/connect`); //create a websocket for WebRTC signaling
ws.onopen = () => console.log(`Connected`);
ws.onclose = ws.onerror = ({reason}) => alert(`Disconnected ${reason}`);
@@ -91,7 +91,7 @@ function drawHandler(e) {
return;
let x = e.clientX - whiteboard.offsetLeft,
y = e.clientY - whiteboard.offsetTop,
- clientX = e.clientX,
+ clientX = e.clientX,
clientY = e.clientY;
if (e.touches && e.touches.length == 1) {
let touch = e.touches[0];
@@ -104,4 +104,4 @@ function drawHandler(e) {
whiteboard.brush.stroke();
whiteboard.brush.beginPath();
whiteboard.brush.moveTo(x, y);
-}
\ No newline at end of file
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment