diff --git a/.gitignore b/.gitignore index 6591483..e18c4a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea goldgorilla +ice.servers.json \ No newline at end of file diff --git a/app.go b/app.go index 9d0e75c..664afde 100644 --- a/app.go +++ b/app.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/pion/webrtc/v3" "io" "net/http" "os" @@ -25,17 +26,28 @@ type App struct { func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, targetRoom string) { println("initializing ..") a.src = srcListenAddr + var iceServers []webrtc.ICEServer + iceconfjson, err := os.ReadFile("./ice.servers.json") + if err != nil { + println("[E] error reading ice.servers.json: " + err.Error()) + } else { + err = json.Unmarshal(iceconfjson, &iceServers) + if err != nil { + panic("[E] can't parse ice.servers.json: " + err.Error()) + } + } a.conf = &models.ConfigModel{ LogjamBaseUrl: logjamBaseUrl + "/auxiliary-node", TargetRoom: targetRoom, ServiceAddress: svcAddr, + ICEServers: iceServers, } roomRepo := repositories.NewRoomRepository(a.conf) a.router = &routers.Router{} respHelper := controllers.NewResponseHelper() roomCtrl := controllers.NewRoomController(respHelper, roomRepo, a.conf) - err := a.router.RegisterRoutes(roomCtrl) + err = a.router.RegisterRoutes(roomCtrl) panicIfErr(err) { diff --git a/ice.servers.sample.json b/ice.servers.sample.json new file mode 100644 index 0000000..0370a13 --- /dev/null +++ b/ice.servers.sample.json @@ -0,0 +1,7 @@ +[ + { + "urls": ["turn:turn.example.com:4499"], + "username": "user", + "credential": "1234" + } +] \ No newline at end of file diff --git a/models/config.go b/models/config.go index 8e043cf..fde1fde 100644 --- a/models/config.go +++ b/models/config.go @@ -1,7 +1,10 @@ package models +import "github.com/pion/webrtc/v3" + type ConfigModel struct { - ServiceAddress string `json:"serviceAddress"` - LogjamBaseUrl string `json:"logjamBaseUrl"` - TargetRoom string `json:"targetRoom"` + ServiceAddress string `json:"serviceAddress"` + LogjamBaseUrl string `json:"logjamBaseUrl"` + TargetRoom string `json:"targetRoom"` + ICEServers []webrtc.ICEServer `json:"iceServers"` } diff --git a/repositories/room.go b/repositories/room.go index 38c1208..4189fb5 100644 --- a/repositories/room.go +++ b/repositories/room.go @@ -109,10 +109,10 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish bool, i room := r.Rooms[roomId] r.Unlock() - room.Lock() - defer room.Unlock() - peerConn, err := webrtc.NewPeerConnection(webrtc.Configuration{}) + peerConn, err := webrtc.NewPeerConnection(webrtc.Configuration{ + ICEServers: r.conf.ICEServers, + }) if err != nil { return models.NewError("can't create peer connection", 500, models.MessageResponse{Message: err.Error()}) } @@ -130,6 +130,8 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish bool, i println("[PC] negotiating with peer", id) r.offerPeer(peerConn,roomId,id) })*/ + room.Lock() + defer room.Unlock() room.Peers[id] = &Peer{ ID: id, Conn: peerConn, @@ -175,8 +177,8 @@ func (r *RoomRepository) onPeerConnectionStateChange(roomId string, id uint64, n r.Unlock() return } - r.Unlock() room := r.Rooms[roomId] + r.Unlock() room.Lock() defer room.Unlock() @@ -329,20 +331,20 @@ func (r *RoomRepository) SetPeerAnswer(roomId string, id uint64, answer webrtc.S r.Unlock() return models.NewError("room doesn't exists", 403, map[string]any{"roomId": roomId}) } - r.Unlock() room := r.Rooms[roomId] + r.Unlock() room.Lock() - defer room.Unlock() - if !r.doesPeerExists(roomId, id) { + room.Unlock() return models.NewError("no such a peer with this id in this room", 403, map[string]any{"roomId": roomId, "peerId": id}) } - - err := room.Peers[id].Conn.SetRemoteDescription(answer) + peer := room.Peers[id] + room.Unlock() + err := peer.Conn.SetRemoteDescription(answer) if err != nil { return models.NewError(err.Error(), 500, models.MessageResponse{Message: err.Error()}) } - room.Peers[id].HandshakeLock.Unlock() + peer.HandshakeLock.Unlock() return nil } func (r *RoomRepository) SetPeerOffer(roomId string, id uint64, offer webrtc.SessionDescription) (sdpAnswer *webrtc.SessionDescription, err error) { @@ -408,15 +410,17 @@ func (r *RoomRepository) ClosePeer(roomId string, id uint64) error { r.Unlock() return models.NewError("room doesn't exists", 403, map[string]any{"roomId": roomId}) } - r.Unlock() room := r.Rooms[roomId] + r.Unlock() room.Lock() - defer room.Unlock() + peer := room.Peers[id] if !r.doesPeerExists(roomId, id) { + room.Unlock() return models.NewError("no such a peer with this id in this room", 403, map[string]any{"roomId": roomId, "peerId": id}) } - return room.Peers[id].Conn.Close() + room.Unlock() + return peer.Conn.Close() } func (r *RoomRepository) ResetRoom(roomId string) error {