Compare commits

..

No commits in common. "df92d0ec0a07ace6e0e5e417da60050c092577bb" and "0984f1b284bac56a55ffbf6f7838c135f01ab79a" have entirely different histories.

5 changed files with 17 additions and 44 deletions

1
.gitignore vendored
View File

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

14
app.go
View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/pion/webrtc/v3"
"io" "io"
"net/http" "net/http"
"os" "os"
@ -26,28 +25,17 @@ 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

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

View File

@ -1,10 +1,7 @@
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,8 +130,6 @@ 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,
@ -177,8 +175,8 @@ func (r *RoomRepository) onPeerConnectionStateChange(roomId string, id uint64, n
r.Unlock() r.Unlock()
return return
} }
room := r.Rooms[roomId]
r.Unlock() r.Unlock()
room := r.Rooms[roomId]
room.Lock() room.Lock()
defer room.Unlock() defer room.Unlock()
@ -331,20 +329,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})
} }
room := r.Rooms[roomId]
r.Unlock() r.Unlock()
room := r.Rooms[roomId]
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]
room.Unlock() err := room.Peers[id].Conn.SetRemoteDescription(answer)
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()})
} }
peer.HandshakeLock.Unlock() room.Peers[id].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) {
@ -410,17 +408,15 @@ 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})
} }
room := r.Rooms[roomId]
r.Unlock() r.Unlock()
room := r.Rooms[roomId]
room.Lock() room.Lock()
peer := room.Peers[id] 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})
} }
room.Unlock() return room.Peers[id].Conn.Close()
return peer.Conn.Close()
} }
func (r *RoomRepository) ResetRoom(roomId string) error { func (r *RoomRepository) ResetRoom(roomId string) error {