Move callbacks out of messageRouter (#269)
* move callbacks out of messageRouter * OCD * more OCD * fix forwarder test * even more OCD * maximum OCD * package name collision, copy lock by value
This commit is contained in:
+11
-12
@@ -12,7 +12,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
livekit "github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/pion/rtp"
|
||||
@@ -20,8 +21,6 @@ import (
|
||||
"github.com/thoas/go-funk"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/protocol/logger"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
)
|
||||
|
||||
@@ -62,7 +61,7 @@ type RTCClient struct {
|
||||
OnConnected func()
|
||||
OnDataReceived func(data []byte, sid string)
|
||||
|
||||
// map of track Id and last packet
|
||||
// map of track ID and last packet
|
||||
lastPackets map[string]*rtp.Packet
|
||||
bytesReceived map[string]uint64
|
||||
}
|
||||
@@ -176,13 +175,13 @@ func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
||||
if ic == nil {
|
||||
return
|
||||
}
|
||||
c.SendIceCandidate(ic, livekit.SignalTarget_PUBLISHER)
|
||||
_ = c.SendIceCandidate(ic, livekit.SignalTarget_PUBLISHER)
|
||||
})
|
||||
c.subscriber.PeerConnection().OnICECandidate(func(ic *webrtc.ICECandidate) {
|
||||
if ic == nil {
|
||||
return
|
||||
}
|
||||
c.SendIceCandidate(ic, livekit.SignalTarget_SUBSCRIBER)
|
||||
_ = c.SendIceCandidate(ic, livekit.SignalTarget_SUBSCRIBER)
|
||||
})
|
||||
|
||||
c.subscriber.PeerConnection().OnTrack(func(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {
|
||||
@@ -281,10 +280,10 @@ func (c *RTCClient) Run() error {
|
||||
|
||||
logger.Infow("join accepted, awaiting offer", "participant", msg.Join.Participant.Identity)
|
||||
case *livekit.SignalResponse_Answer:
|
||||
//logger.Debugw("received server answer",
|
||||
// logger.Debugw("received server answer",
|
||||
// "participant", c.localParticipant.Identity,
|
||||
// "answer", msg.Answer.Sdp)
|
||||
c.handleAnswer(rtc.FromProtoSessionDescription(msg.Answer))
|
||||
_ = c.handleAnswer(rtc.FromProtoSessionDescription(msg.Answer))
|
||||
case *livekit.SignalResponse_Offer:
|
||||
logger.Infow("received server offer",
|
||||
"participant", c.localParticipant.Identity,
|
||||
@@ -361,7 +360,7 @@ func (c *RTCClient) ReadResponse() (*livekit.SignalResponse, error) {
|
||||
msg := &livekit.SignalResponse{}
|
||||
switch messageType {
|
||||
case websocket.PingMessage:
|
||||
c.conn.WriteMessage(websocket.PongMessage, nil)
|
||||
_ = c.conn.WriteMessage(websocket.PongMessage, nil)
|
||||
continue
|
||||
case websocket.BinaryMessage:
|
||||
// protobuf encoded
|
||||
@@ -399,7 +398,7 @@ func (c *RTCClient) Stop() {
|
||||
})
|
||||
c.connected.TrySet(false)
|
||||
c.iceConnected.TrySet(false)
|
||||
c.conn.Close()
|
||||
_ = c.conn.Close()
|
||||
c.publisher.Close()
|
||||
c.subscriber.Close()
|
||||
c.cancel()
|
||||
@@ -614,7 +613,7 @@ func (c *RTCClient) handleOffer(desc webrtc.SessionDescription) error {
|
||||
// send remote an answer
|
||||
logger.Infow("sending subscriber answer",
|
||||
"participant", c.localParticipant.Identity,
|
||||
//"sdp", answer,
|
||||
// "sdp", answer,
|
||||
)
|
||||
return c.SendRequest(&livekit.SignalRequest{
|
||||
Message: &livekit.SignalRequest_Answer{
|
||||
@@ -723,5 +722,5 @@ func (c *RTCClient) SendNacks(count int) {
|
||||
}
|
||||
c.lock.Unlock()
|
||||
|
||||
c.subscriber.PeerConnection().WriteRTCP(packets)
|
||||
_ = c.subscriber.PeerConnection().WriteRTCP(packets)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/livekit/protocol/auth"
|
||||
livekit "github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/twitchtv/twirp"
|
||||
@@ -28,9 +28,9 @@ const (
|
||||
testApiSecret = "apiSecret"
|
||||
testRoom = "mytestroom"
|
||||
defaultServerPort = 7880
|
||||
secondServerPort = 8880
|
||||
nodeId1 = "node-1"
|
||||
nodeId2 = "node-2"
|
||||
secondServerPort = 8880
|
||||
nodeID1 = "node-1"
|
||||
nodeID2 = "node-2"
|
||||
|
||||
syncDelay = 100 * time.Millisecond
|
||||
// if there are deadlocks, it's helpful to set a short test timeout (i.e. go test -timeout=30s)
|
||||
@@ -70,8 +70,8 @@ func setupSingleNodeTest(name string, roomName string) (*service.LivekitServer,
|
||||
|
||||
func setupMultiNodeTest(name string) (*service.LivekitServer, *service.LivekitServer, func()) {
|
||||
logger.Infow("----------------STARTING TEST----------------", "test", name)
|
||||
s1 := createMultiNodeServer(utils.NewGuid(nodeId1), defaultServerPort)
|
||||
s2 := createMultiNodeServer(utils.NewGuid(nodeId2), secondServerPort)
|
||||
s1 := createMultiNodeServer(utils.NewGuid(nodeID1), defaultServerPort)
|
||||
s2 := createMultiNodeServer(utils.NewGuid(nodeID2), secondServerPort)
|
||||
go s1.Start()
|
||||
go s2.Start()
|
||||
|
||||
@@ -145,7 +145,7 @@ func createSingleNodeServer() *service.LivekitServer {
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("could not create local node: %v", err))
|
||||
}
|
||||
currentNode.Id = utils.NewGuid(nodeId1)
|
||||
currentNode.Id = utils.NewGuid(nodeID1)
|
||||
|
||||
s, err := service.InitializeServer(conf, currentNode)
|
||||
if err != nil {
|
||||
@@ -156,7 +156,7 @@ func createSingleNodeServer() *service.LivekitServer {
|
||||
return s
|
||||
}
|
||||
|
||||
func createMultiNodeServer(nodeId string, port uint32) *service.LivekitServer {
|
||||
func createMultiNodeServer(nodeID string, port uint32) *service.LivekitServer {
|
||||
var err error
|
||||
conf, err := config.NewConfig("", nil)
|
||||
if err != nil {
|
||||
@@ -173,7 +173,7 @@ func createMultiNodeServer(nodeId string, port uint32) *service.LivekitServer {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
currentNode.Id = nodeId
|
||||
currentNode.Id = nodeID
|
||||
|
||||
// redis routing and store
|
||||
s, err := service.InitializeServer(conf, currentNode)
|
||||
|
||||
@@ -4,10 +4,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
livekit "github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/livekit/livekit-server/pkg/testutils"
|
||||
)
|
||||
|
||||
@@ -48,8 +48,8 @@ func TestMultiNodeRouting(t *testing.T) {
|
||||
}
|
||||
|
||||
tr1 := c2.SubscribedTracks()[c1.ID()][0]
|
||||
streamId, _ := rtc.UnpackStreamID(tr1.StreamID())
|
||||
require.Equal(t, c1.ID(), streamId)
|
||||
streamID, _ := rtc.UnpackStreamID(tr1.StreamID())
|
||||
require.Equal(t, c1.ID(), streamID)
|
||||
return true
|
||||
})
|
||||
}
|
||||
@@ -78,7 +78,7 @@ func TestMultinodePublishingUponJoining(t *testing.T) {
|
||||
_, _, finish := setupMultiNodeTest("TestMultinodePublishingUponJoining")
|
||||
defer finish()
|
||||
|
||||
scenarioPublishingUponJoining(t, defaultServerPort, secondServerPort)
|
||||
scenarioPublishingUponJoining(t)
|
||||
}
|
||||
|
||||
func TestMultinodeReceiveBeforePublish(t *testing.T) {
|
||||
|
||||
+2
-10
@@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
livekit "github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// a scenario with lots of clients connecting, publishing, and leaving at random periods
|
||||
func scenarioPublishingUponJoining(t *testing.T, ports ...int) {
|
||||
func scenarioPublishingUponJoining(t *testing.T) {
|
||||
c1 := createRTCClient("puj_1", defaultServerPort, nil)
|
||||
c2 := createRTCClient("puj_2", secondServerPort, &testclient.Options{AutoSubscribe: true})
|
||||
c3 := createRTCClient("puj_3", defaultServerPort, &testclient.Options{AutoSubscribe: true})
|
||||
@@ -142,14 +142,6 @@ func scenarioDataPublish(t *testing.T) {
|
||||
}
|
||||
|
||||
// websocket reconnects
|
||||
func scenarioWSReconnect(t *testing.T) {
|
||||
c1 := createRTCClient("wsr_1", defaultServerPort, nil)
|
||||
c2 := createRTCClient("wsr_2", defaultServerPort, nil)
|
||||
|
||||
waitUntilConnected(t, c1, c2)
|
||||
|
||||
// c1 publishes track, but disconnects websockets and reconnects
|
||||
}
|
||||
|
||||
func publishTracksForClients(t *testing.T, clients ...*testclient.RTCClient) []*testclient.TrackWriter {
|
||||
logger.Infow("publishing tracks for clients")
|
||||
|
||||
@@ -7,11 +7,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/auth"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/livekit/livekit-server/pkg/testutils"
|
||||
testclient "github.com/livekit/livekit-server/test/client"
|
||||
)
|
||||
@@ -74,7 +73,7 @@ func TestClientConnectDuplicate(t *testing.T) {
|
||||
return false
|
||||
}
|
||||
|
||||
//participant ID can be appended with '#..' . but should contain orig id as prefix
|
||||
// participant ID can be appended with '#..' . but should contain orig id as prefix
|
||||
tr1 := c2.SubscribedTracks()[c1.ID()][0]
|
||||
participantId1, _ := rtc.UnpackStreamID(tr1.StreamID())
|
||||
require.Equal(t, c1.ID(), participantId1)
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
livekit "github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/pion/turn/v2"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/service"
|
||||
)
|
||||
|
||||
func testTurnServer(t *testing.T) {
|
||||
conf, err := config.NewConfig("", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
conf.TURN.Enabled = true
|
||||
conf.Keys = map[string]string{testApiKey: testApiSecret}
|
||||
|
||||
currentNode, err := routing.NewLocalNode(conf)
|
||||
require.NoError(t, err)
|
||||
currentNode.Id = utils.NewGuid(nodeId1)
|
||||
|
||||
// local routing and store
|
||||
s, err := service.InitializeServer(conf, currentNode)
|
||||
require.NoError(t, err)
|
||||
go s.Start()
|
||||
waitForServerToStart(s)
|
||||
defer s.Stop(true)
|
||||
|
||||
time.Sleep(syncDelay)
|
||||
|
||||
// create a room
|
||||
rm := &livekit.Room{
|
||||
Sid: utils.NewGuid(utils.RoomPrefix),
|
||||
Name: "testroom",
|
||||
TurnPassword: utils.RandomSecret(),
|
||||
}
|
||||
// require.NoError(t, roomStore.CreateRoom(rm))
|
||||
|
||||
turnConf := &turn.ClientConfig{
|
||||
STUNServerAddr: fmt.Sprintf("localhost:%d", conf.TURN.TLSPort),
|
||||
TURNServerAddr: fmt.Sprintf("%s:%d", currentNode.Ip, conf.TURN.TLSPort),
|
||||
Username: rm.Name,
|
||||
Password: rm.TurnPassword,
|
||||
Realm: "livekit",
|
||||
}
|
||||
|
||||
t.Run("TURN works over TCP", func(t *testing.T) {
|
||||
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", conf.TURN.TLSPort))
|
||||
require.NoError(t, err)
|
||||
|
||||
tc := *turnConf
|
||||
tc.Conn = turn.NewSTUNConn(conn)
|
||||
c, err := turn.NewClient(&tc)
|
||||
require.NoError(t, err)
|
||||
defer c.Close()
|
||||
|
||||
require.NoError(t, c.Listen())
|
||||
|
||||
// Allocate a relay socket on the TURN server. On success, it
|
||||
// will return a net.PacketConn which represents the remote
|
||||
// socket.
|
||||
relayConn, err := c.Allocate()
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
require.NoError(t, relayConn.Close())
|
||||
}()
|
||||
})
|
||||
// UDP test doesn't pass
|
||||
//t.Run("TURN connects over UDP", func(t *testing.T) {
|
||||
// conn, err := net.ListenPacket("udp4", "0.0.0.0:0")
|
||||
// require.NoError(t, err)
|
||||
// defer func() {
|
||||
// require.NoError(t, conn.Close())
|
||||
// }()
|
||||
//
|
||||
// tc := *turnConf
|
||||
// tc.Conn = conn
|
||||
//
|
||||
// client, err := turn.NewClient(&tc)
|
||||
// require.NoError(t, err)
|
||||
// defer client.Close()
|
||||
//
|
||||
// // Start listening on the conn provided.
|
||||
// require.NoError(t, client.Listen())
|
||||
//
|
||||
// // Allocate a relay socket on the TURN server. On success, it
|
||||
// // will return a net.PacketConn which represents the remote
|
||||
// // socket.
|
||||
// relayConn, err := client.Allocate()
|
||||
// require.NoError(t, err)
|
||||
// defer func() {
|
||||
// require.NoError(t, relayConn.Close())
|
||||
// }()
|
||||
//})
|
||||
}
|
||||
+11
-11
@@ -9,7 +9,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/livekit/protocol/auth"
|
||||
livekit "github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/livekit/protocol/webhook"
|
||||
@@ -84,7 +84,7 @@ func TestWebhooks(t *testing.T) {
|
||||
require.Equal(t, testRoom, ts.GetEvent(webhook.EventRoomFinished).Room.Name)
|
||||
}
|
||||
|
||||
func setupServerWithWebhook() (server *service.LivekitServer, testServer *webookTestServer, finishFunc func(), err error) {
|
||||
func setupServerWithWebhook() (server *service.LivekitServer, testServer *webhookTestServer, finishFunc func(), err error) {
|
||||
conf, err := config.NewConfig("", nil)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("could not create config: %v", err))
|
||||
@@ -103,7 +103,7 @@ func setupServerWithWebhook() (server *service.LivekitServer, testServer *webook
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
currentNode.Id = utils.NewGuid(nodeId1)
|
||||
currentNode.Id = utils.NewGuid(nodeID1)
|
||||
|
||||
server, err = service.InitializeServer(conf, currentNode)
|
||||
if err != nil {
|
||||
@@ -125,15 +125,15 @@ func setupServerWithWebhook() (server *service.LivekitServer, testServer *webook
|
||||
return
|
||||
}
|
||||
|
||||
type webookTestServer struct {
|
||||
type webhookTestServer struct {
|
||||
server *http.Server
|
||||
events map[string]*livekit.WebhookEvent
|
||||
lock sync.Mutex
|
||||
provider auth.KeyProvider
|
||||
}
|
||||
|
||||
func newTestServer(addr string) *webookTestServer {
|
||||
s := &webookTestServer{
|
||||
func newTestServer(addr string) *webhookTestServer {
|
||||
s := &webhookTestServer{
|
||||
events: make(map[string]*livekit.WebhookEvent),
|
||||
provider: auth.NewFileBasedKeyProviderFromMap(map[string]string{testApiKey: testApiSecret}),
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func newTestServer(addr string) *webookTestServer {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *webookTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *webhookTestServer) ServeHTTP(_ http.ResponseWriter, r *http.Request) {
|
||||
data, err := webhook.Receive(r, s.provider)
|
||||
if err != nil {
|
||||
logger.Errorw("could not receive webhook", err)
|
||||
@@ -162,19 +162,19 @@ func (s *webookTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
func (s *webookTestServer) GetEvent(name string) *livekit.WebhookEvent {
|
||||
func (s *webhookTestServer) GetEvent(name string) *livekit.WebhookEvent {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
return s.events[name]
|
||||
}
|
||||
|
||||
func (s *webookTestServer) ClearEvents() {
|
||||
func (s *webhookTestServer) ClearEvents() {
|
||||
s.lock.Lock()
|
||||
s.events = make(map[string]*livekit.WebhookEvent)
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
func (s *webookTestServer) Start() error {
|
||||
func (s *webhookTestServer) Start() error {
|
||||
l, err := net.Listen("tcp", s.server.Addr)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -183,6 +183,6 @@ func (s *webookTestServer) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *webookTestServer) Stop() {
|
||||
func (s *webhookTestServer) Stop() {
|
||||
_ = s.server.Shutdown(context.Background())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user