@@ -49,6 +49,7 @@ import (
4949 "github.com/stretchr/testify/require"
5050 "go.etcd.io/etcd/server/v3/embed"
5151
52+ "github.com/strukturag/nextcloud-spreed-signaling/api"
5253 "github.com/strukturag/nextcloud-spreed-signaling/log"
5354)
5455
@@ -198,7 +199,8 @@ func Test_sortConnectionsForCountryWithOverride(t *testing.T) {
198199type proxyServerClientHandler func (msg * ProxyClientMessage ) (* ProxyServerMessage , error )
199200
200201type testProxyServerPublisher struct {
201- id PublicSessionId
202+ id PublicSessionId
203+ bandwidth api.AtomicBandwidth
202204}
203205
204206type testProxyServerSubscriber struct {
@@ -298,6 +300,8 @@ func (c *testProxyServerClient) processRegularMessage(msg *ProxyClientMessage) (
298300 switch msg .Type {
299301 case "command" :
300302 handler = c .processCommandMessage
303+ case "payload" :
304+ handler = c .processPayloadMessage
301305 }
302306
303307 if handler == nil {
@@ -431,6 +435,21 @@ func (c *testProxyServerClient) processCommandMessage(msg *ProxyClientMessage) (
431435 }
432436 c .server .updateLoad (- 1 )
433437 }
438+ case "update-bandwidth" :
439+ pub := c .server .getPublisher (PublicSessionId (msg .Command .ClientId ))
440+ if pub == nil {
441+ response = msg .NewWrappedErrorServerMessage (fmt .Errorf ("publisher %s not found" , msg .Command .ClientId ))
442+ return response , nil
443+ }
444+
445+ pub .bandwidth .Store (msg .Command .Bandwidth )
446+ response = & ProxyServerMessage {
447+ Id : msg .Id ,
448+ Type : "command" ,
449+ Command : & CommandProxyServerMessage {
450+ Id : string (pub .id ),
451+ },
452+ }
434453 }
435454 if response == nil {
436455 response = msg .NewWrappedErrorServerMessage (fmt .Errorf ("command \" %s\" is not implemented" , msg .Command .Type ))
@@ -439,6 +458,36 @@ func (c *testProxyServerClient) processCommandMessage(msg *ProxyClientMessage) (
439458 return response , nil
440459}
441460
461+ func (c * testProxyServerClient ) processPayloadMessage (msg * ProxyClientMessage ) (* ProxyServerMessage , error ) {
462+ var response * ProxyServerMessage
463+ switch msg .Payload .Type {
464+ case "offer" :
465+ pub := c .server .getPublisher (PublicSessionId (msg .Payload .ClientId ))
466+ if pub == nil {
467+ response = msg .NewWrappedErrorServerMessage (fmt .Errorf ("no such publisher: %s" , msg .Payload .ClientId ))
468+ return response , nil
469+ }
470+
471+ assert .Equal (c .t , MockSdpOfferAudioAndVideo , msg .Payload .Payload ["sdp" ])
472+ response = & ProxyServerMessage {
473+ Id : msg .Id ,
474+ Type : "payload" ,
475+ Payload : & PayloadProxyServerMessage {
476+ ClientId : string (pub .id ),
477+ Type : "answer" ,
478+ Payload : api.StringMap {
479+ "type" : "answer" ,
480+ "sdp" : MockSdpAnswerAudioAndVideo ,
481+ },
482+ },
483+ }
484+ default :
485+ response = msg .NewWrappedErrorServerMessage (fmt .Errorf ("payload type \" %s\" is not implemented" , msg .Payload .Type ))
486+ }
487+
488+ return response , nil
489+ }
490+
442491func (c * testProxyServerClient ) close () {
443492 c .mu .Lock ()
444493 defer c .mu .Unlock ()
@@ -2598,3 +2647,112 @@ func Test_ProxyResumeFail(t *testing.T) {
25982647 assert .NotEqual (sessionId , connections [0 ].SessionId ())
25992648 }
26002649}
2650+
2651+ func Test_ProxySetBandwidth (t * testing.T ) {
2652+ t .Parallel ()
2653+ require := require .New (t )
2654+ assert := assert .New (t )
2655+ server := NewProxyServerForTest (t , "DE" )
2656+ mcu , _ := newMcuProxyForTestWithOptions (t , proxyTestOptions {
2657+ servers : []* TestProxyServerHandler {server },
2658+ }, 0 , nil )
2659+
2660+ hub , _ , _ , hubserver := CreateHubForTestWithConfig (t , func (s * httptest.Server ) (* goconf.ConfigFile , error ) {
2661+ config , err := getTestConfig (s )
2662+ if err != nil {
2663+ return nil , err
2664+ }
2665+
2666+ config .AddOption ("backend" , "maxstreambitrate" , "700000" )
2667+ config .AddOption ("backend" , "maxscreenbitrate" , "800000" )
2668+
2669+ config .AddOption ("backend" , "bitrateperroom" , "1000000" )
2670+ config .AddOption ("backend" , "minpublisherbitrate" , "10000" )
2671+ config .AddOption ("backend" , "maxpublisherbitrate" , "500000" )
2672+ return config , err
2673+ })
2674+ hub .SetMcu (mcu )
2675+
2676+ ctx , cancel := context .WithTimeout (context .Background (), testTimeout )
2677+ defer cancel ()
2678+
2679+ client , hello := NewTestClientWithHello (ctx , t , hubserver , hub , testDefaultUserId + "1" )
2680+
2681+ // Join room by id.
2682+ roomId := "test-room"
2683+ roomMsg := MustSucceed2 (t , client .JoinRoom , ctx , roomId )
2684+ require .Equal (roomId , roomMsg .Room .RoomId )
2685+ client .RunUntilJoined (ctx , hello .Hello )
2686+
2687+ require .NoError (client .SendMessage (MessageClientMessageRecipient {
2688+ Type : "session" ,
2689+ SessionId : hello .Hello .SessionId ,
2690+ }, MessageClientMessageData {
2691+ Type : "offer" ,
2692+ RoomType : "video" ,
2693+ Payload : api.StringMap {
2694+ "sdp" : MockSdpOfferAudioAndVideo ,
2695+ },
2696+ }))
2697+
2698+ client .RunUntilAnswer (ctx , MockSdpAnswerAudioAndVideo )
2699+
2700+ pub := mcu .getPublisherConnection (hello .Hello .SessionId , StreamTypeVideo )
2701+ require .NotNil (pub )
2702+
2703+ var publisherId string
2704+ var publisher * mcuProxyPublisher
2705+ pub .publishersLock .RLock ()
2706+ if assert .Len (pub .publishers , 1 ) {
2707+ for id , mcuPub := range pub .publishers {
2708+ publisherId = id
2709+ publisher = mcuPub
2710+ break
2711+ }
2712+ }
2713+ pub .publishersLock .RUnlock ()
2714+ require .NotEmpty (publisherId )
2715+ require .NotNil (publisher )
2716+
2717+ proxyclient := server .GetSingleClient ()
2718+ proxyclient .sendMessage (& ProxyServerMessage {
2719+ Type : "event" ,
2720+ Event : & EventProxyServerMessage {
2721+ Type : "update-load" ,
2722+ Load : 1 ,
2723+ ClientBandwidths : map [string ]EventProxyServerBandwidth {
2724+ publisherId : {
2725+ Sent : api .BandwidthFromBits (0 ),
2726+ Received : api .BandwidthFromBits (100000 ),
2727+ },
2728+ },
2729+ },
2730+ })
2731+
2732+ for publisher .Bandwidth () == nil {
2733+ if ! assert .NoError (ctx .Err ()) {
2734+ break
2735+ }
2736+
2737+ time .Sleep (time .Millisecond )
2738+ }
2739+ if bw := publisher .Bandwidth (); assert .NotNil (bw ) {
2740+ assert .EqualValues (0 , bw .Sent )
2741+ assert .EqualValues (100000 , bw .Received )
2742+ }
2743+
2744+ room := hub .getRoom (roomId )
2745+ require .NotNil (room )
2746+ room .updateBandwidth ().Wait ()
2747+
2748+ proxyPub := server .getPublisher (PublicSessionId (publisherId ))
2749+ require .NotNil (proxyPub )
2750+ for proxyPub .bandwidth .Load () == 0 {
2751+ if ! assert .NoError (ctx .Err ()) {
2752+ break
2753+ }
2754+
2755+ time .Sleep (time .Millisecond )
2756+ }
2757+ assert .EqualValues (500000 , proxyPub .bandwidth .Load ())
2758+ }
0 commit comments