fix: Hardcode ICE servers
parent
3833e6cc26
commit
62c3a00c56
|
@ -1 +1 @@
|
|||
Subproject commit a36ddf9a8106e69a2ad1c4a42fc78ed8765fd23e
|
||||
Subproject commit 10e363e61b36d754351fc530f05b583152d62b04
|
|
@ -1 +0,0 @@
|
|||
stun:stun.l.google.com:19302
|
45
main.go
45
main.go
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -13,12 +12,12 @@ import (
|
|||
|
||||
. "webrtc/backend-protobuf/go"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/pion/webrtc/v2"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/nats-io/go-nats"
|
||||
"github.com/pion/webrtc/v2"
|
||||
)
|
||||
|
||||
// Peer config
|
||||
|
@ -32,9 +31,9 @@ var upgrader websocket.Upgrader
|
|||
var mediaEngine webrtc.MediaEngine
|
||||
var webrtcApi *webrtc.API
|
||||
|
||||
var userTracks map[string] map[string] *webrtc.Track // userid + clientid
|
||||
var conversationUsers map[string] []string
|
||||
var userConversation map[string] string
|
||||
var userTracks map[string]map[string]*webrtc.Track // userid + clientid
|
||||
var conversationUsers map[string][]string
|
||||
var userConversation map[string]string
|
||||
|
||||
var natsConn *nats.Conn
|
||||
|
||||
|
@ -55,25 +54,18 @@ func main() {
|
|||
webrtcApi = webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine))
|
||||
|
||||
// Read ICE servers
|
||||
fileBytes, err := ioutil.ReadFile("iceservers.txt")
|
||||
if err != nil {
|
||||
log.Fatal("error opening ice servers file")
|
||||
}
|
||||
fileString := string(fileBytes)
|
||||
servers := strings.Split(fileString, `\n`)
|
||||
|
||||
peerConnectionConfig = webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
{
|
||||
URLs: servers,
|
||||
URLs: []string{"stun:stun.l.google.com:19302"},
|
||||
},
|
||||
},
|
||||
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
|
||||
}
|
||||
|
||||
userTracks = make(map[string] map[string] *webrtc.Track)
|
||||
conversationUsers = make(map[string] []string)
|
||||
userConversation = make(map[string] string)
|
||||
userTracks = make(map[string]map[string]*webrtc.Track)
|
||||
conversationUsers = make(map[string][]string)
|
||||
userConversation = make(map[string]string)
|
||||
|
||||
// NATs client
|
||||
natsConn, err := nats.Connect(natsHost)
|
||||
|
@ -97,6 +89,7 @@ type RawClient struct {
|
|||
UserId string `json:"userid"`
|
||||
ClientId string `json:"clientid"`
|
||||
}
|
||||
|
||||
func GetAuth(next httprouter.Handle) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
ua := r.Header.Get("X-User-Claim")
|
||||
|
@ -155,7 +148,7 @@ func NewConnection(w http.ResponseWriter, r *http.Request, p httprouter.Params)
|
|||
_, msg, err := c.ReadMessage()
|
||||
iceCandidate := strings.Join(strings.Split(string(msg), "::")[1:], "::")
|
||||
if err == nil {
|
||||
clientReceiver.AddICECandidate(webrtc.ICECandidateInit {
|
||||
clientReceiver.AddICECandidate(webrtc.ICECandidateInit{
|
||||
Candidate: iceCandidate,
|
||||
})
|
||||
}
|
||||
|
@ -164,7 +157,7 @@ func NewConnection(w http.ResponseWriter, r *http.Request, p httprouter.Params)
|
|||
|
||||
// Handle ICE candidates
|
||||
clientReceiver.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||
_ = c.WriteMessage(websocket.TextMessage, []byte("ice::" + candidate.String()))
|
||||
_ = c.WriteMessage(websocket.TextMessage, []byte("ice::"+candidate.String()))
|
||||
})
|
||||
|
||||
_, err = clientReceiver.AddTransceiver(webrtc.RTPCodecTypeAudio)
|
||||
|
@ -202,7 +195,7 @@ func NewConnection(w http.ResponseWriter, r *http.Request, p httprouter.Params)
|
|||
}
|
||||
|
||||
start := time.Now().Unix()
|
||||
bite := Bite {
|
||||
bite := Bite{
|
||||
Start: uint64(start),
|
||||
Key: conversationId,
|
||||
Data: rtpBuf[:i],
|
||||
|
@ -214,7 +207,7 @@ func NewConnection(w http.ResponseWriter, r *http.Request, p httprouter.Params)
|
|||
natsConn.Publish("bite", biteOut)
|
||||
}
|
||||
|
||||
store := Store {
|
||||
store := Store{
|
||||
Type: "bite",
|
||||
Bite: &bite,
|
||||
}
|
||||
|
@ -241,7 +234,7 @@ func NewConnection(w http.ResponseWriter, r *http.Request, p httprouter.Params)
|
|||
|
||||
// Do signalling things
|
||||
err = clientReceiver.SetRemoteDescription(
|
||||
webrtc.SessionDescription {
|
||||
webrtc.SessionDescription{
|
||||
SDP: offerSDP,
|
||||
Type: webrtc.SDPTypeOffer,
|
||||
})
|
||||
|
@ -265,7 +258,7 @@ func NewConnection(w http.ResponseWriter, r *http.Request, p httprouter.Params)
|
|||
return
|
||||
}
|
||||
|
||||
err = c.WriteMessage(mt, []byte("answer::" + answer.SDP))
|
||||
err = c.WriteMessage(mt, []byte("answer::"+answer.SDP))
|
||||
if err != nil {
|
||||
log.Printf("%s", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -298,10 +291,10 @@ func JoinConversation(w http.ResponseWriter, r *http.Request, p httprouter.Param
|
|||
for i, u := range users {
|
||||
if u == user.UserId {
|
||||
lastIndex = i
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
users[lastIndex] = users[len(users) - 1]
|
||||
users[lastIndex] = users[len(users)-1]
|
||||
conversationUsers[oldConversation] = users[:len(users)-1]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue