feat: configurable ice servers list

pull/1/head
Benyamin Azarkhazin 2023-08-24 17:10:18 +03:30
parent e71790d80d
commit db9432d277
Signed by: benyamin
GPG Key ID: 3AE44F5623C70269
5 changed files with 44 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea
goldgorilla
ice.servers.json

14
app.go
View File

@ -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)
{

View File

@ -0,0 +1,7 @@
[
{
"urls": ["turn:turn.example.com:4499"],
"username": "user",
"credential": "1234"
}
]

View File

@ -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"`
ICEServers []webrtc.ICEServer `json:"iceServers"`
}

View File

@ -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 {