Compare commits

..

2 Commits

5 changed files with 44 additions and 17 deletions

1
.gitignore vendored
View File

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

14
app.go
View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/pion/webrtc/v3"
"io" "io"
"net/http" "net/http"
"os" "os"
@ -25,17 +26,28 @@ type App struct {
func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, targetRoom string) { func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, targetRoom string) {
println("initializing ..") println("initializing ..")
a.src = srcListenAddr 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{ a.conf = &models.ConfigModel{
LogjamBaseUrl: logjamBaseUrl + "/auxiliary-node", LogjamBaseUrl: logjamBaseUrl + "/auxiliary-node",
TargetRoom: targetRoom, TargetRoom: targetRoom,
ServiceAddress: svcAddr, ServiceAddress: svcAddr,
ICEServers: iceServers,
} }
roomRepo := repositories.NewRoomRepository(a.conf) roomRepo := repositories.NewRoomRepository(a.conf)
a.router = &routers.Router{} a.router = &routers.Router{}
respHelper := controllers.NewResponseHelper() respHelper := controllers.NewResponseHelper()
roomCtrl := controllers.NewRoomController(respHelper, roomRepo, a.conf) roomCtrl := controllers.NewRoomController(respHelper, roomRepo, a.conf)
err := a.router.RegisterRoutes(roomCtrl) err = a.router.RegisterRoutes(roomCtrl)
panicIfErr(err) 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 package models
import "github.com/pion/webrtc/v3"
type ConfigModel struct { type ConfigModel struct {
ServiceAddress string `json:"serviceAddress"` ServiceAddress string `json:"serviceAddress"`
LogjamBaseUrl string `json:"logjamBaseUrl"` LogjamBaseUrl string `json:"logjamBaseUrl"`
TargetRoom string `json:"targetRoom"` 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] room := r.Rooms[roomId]
r.Unlock() 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 { if err != nil {
return models.NewError("can't create peer connection", 500, models.MessageResponse{Message: err.Error()}) 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) println("[PC] negotiating with peer", id)
r.offerPeer(peerConn,roomId,id) r.offerPeer(peerConn,roomId,id)
})*/ })*/
room.Lock()
defer room.Unlock()
room.Peers[id] = &Peer{ room.Peers[id] = &Peer{
ID: id, ID: id,
Conn: peerConn, Conn: peerConn,
@ -175,8 +177,8 @@ func (r *RoomRepository) onPeerConnectionStateChange(roomId string, id uint64, n
r.Unlock() r.Unlock()
return return
} }
r.Unlock()
room := r.Rooms[roomId] room := r.Rooms[roomId]
r.Unlock()
room.Lock() room.Lock()
defer room.Unlock() defer room.Unlock()
@ -329,20 +331,20 @@ func (r *RoomRepository) SetPeerAnswer(roomId string, id uint64, answer webrtc.S
r.Unlock() r.Unlock()
return models.NewError("room doesn't exists", 403, map[string]any{"roomId": roomId}) return models.NewError("room doesn't exists", 403, map[string]any{"roomId": roomId})
} }
r.Unlock()
room := r.Rooms[roomId] room := r.Rooms[roomId]
r.Unlock()
room.Lock() room.Lock()
defer room.Unlock()
if !r.doesPeerExists(roomId, 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 models.NewError("no such a peer with this id in this room", 403, map[string]any{"roomId": roomId, "peerId": id})
} }
peer := room.Peers[id]
err := room.Peers[id].Conn.SetRemoteDescription(answer) room.Unlock()
err := peer.Conn.SetRemoteDescription(answer)
if err != nil { if err != nil {
return models.NewError(err.Error(), 500, models.MessageResponse{Message: err.Error()}) return models.NewError(err.Error(), 500, models.MessageResponse{Message: err.Error()})
} }
room.Peers[id].HandshakeLock.Unlock() peer.HandshakeLock.Unlock()
return nil return nil
} }
func (r *RoomRepository) SetPeerOffer(roomId string, id uint64, offer webrtc.SessionDescription) (sdpAnswer *webrtc.SessionDescription, err error) { 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() r.Unlock()
return models.NewError("room doesn't exists", 403, map[string]any{"roomId": roomId}) return models.NewError("room doesn't exists", 403, map[string]any{"roomId": roomId})
} }
r.Unlock()
room := r.Rooms[roomId] room := r.Rooms[roomId]
r.Unlock()
room.Lock() room.Lock()
defer room.Unlock() peer := room.Peers[id]
if !r.doesPeerExists(roomId, 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 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 { func (r *RoomRepository) ResetRoom(roomId string) error {