5
0
Fork 0

fix: Hardcode ICE servers

pull/6/head
Ambrose Chua 2019-07-27 16:37:23 +08:00
parent 3833e6cc26
commit 62c3a00c56
Signed by: ambrose
GPG Key ID: B34FBE029276BA5D
3 changed files with 242 additions and 250 deletions

@ -1 +1 @@
Subproject commit a36ddf9a8106e69a2ad1c4a42fc78ed8765fd23e Subproject commit 10e363e61b36d754351fc530f05b583152d62b04

View File

@ -1 +0,0 @@
stun:stun.l.google.com:19302

45
main.go
View File

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