@@ -47,6 +47,8 @@ import (
4747 "github.com/stretchr/testify/assert"
4848 "github.com/stretchr/testify/require"
4949 "go.etcd.io/etcd/server/v3/embed"
50+
51+ "github.com/strukturag/nextcloud-spreed-signaling/api"
5052)
5153
5254const (
@@ -190,7 +192,8 @@ func Test_sortConnectionsForCountryWithOverride(t *testing.T) {
190192type proxyServerClientHandler func (msg * ProxyClientMessage ) (* ProxyServerMessage , error )
191193
192194type testProxyServerPublisher struct {
193- id PublicSessionId
195+ id PublicSessionId
196+ bandwidth api.AtomicBandwidth
194197}
195198
196199type testProxyServerSubscriber struct {
@@ -290,6 +293,8 @@ func (c *testProxyServerClient) processRegularMessage(msg *ProxyClientMessage) (
290293 switch msg .Type {
291294 case "command" :
292295 handler = c .processCommandMessage
296+ case "payload" :
297+ handler = c .processPayloadMessage
293298 }
294299
295300 if handler == nil {
@@ -423,6 +428,21 @@ func (c *testProxyServerClient) processCommandMessage(msg *ProxyClientMessage) (
423428 }
424429 c .server .updateLoad (- 1 )
425430 }
431+ case "update-bandwidth" :
432+ pub := c .server .getPublisher (PublicSessionId (msg .Command .ClientId ))
433+ if pub == nil {
434+ response = msg .NewWrappedErrorServerMessage (fmt .Errorf ("publisher %s not found" , msg .Command .ClientId ))
435+ return response , nil
436+ }
437+
438+ pub .bandwidth .Store (msg .Command .Bandwidth )
439+ response = & ProxyServerMessage {
440+ Id : msg .Id ,
441+ Type : "command" ,
442+ Command : & CommandProxyServerMessage {
443+ Id : string (pub .id ),
444+ },
445+ }
426446 }
427447 if response == nil {
428448 response = msg .NewWrappedErrorServerMessage (fmt .Errorf ("command \" %s\" is not implemented" , msg .Command .Type ))
@@ -431,6 +451,36 @@ func (c *testProxyServerClient) processCommandMessage(msg *ProxyClientMessage) (
431451 return response , nil
432452}
433453
454+ func (c * testProxyServerClient ) processPayloadMessage (msg * ProxyClientMessage ) (* ProxyServerMessage , error ) {
455+ var response * ProxyServerMessage
456+ switch msg .Payload .Type {
457+ case "offer" :
458+ pub := c .server .getPublisher (PublicSessionId (msg .Payload .ClientId ))
459+ if pub == nil {
460+ response = msg .NewWrappedErrorServerMessage (fmt .Errorf ("no such publisher: %s" , msg .Payload .ClientId ))
461+ return response , nil
462+ }
463+
464+ assert .Equal (c .t , MockSdpOfferAudioAndVideo , msg .Payload .Payload ["sdp" ])
465+ response = & ProxyServerMessage {
466+ Id : msg .Id ,
467+ Type : "payload" ,
468+ Payload : & PayloadProxyServerMessage {
469+ ClientId : string (pub .id ),
470+ Type : "answer" ,
471+ Payload : api.StringMap {
472+ "type" : "answer" ,
473+ "sdp" : MockSdpAnswerAudioAndVideo ,
474+ },
475+ },
476+ }
477+ default :
478+ response = msg .NewWrappedErrorServerMessage (fmt .Errorf ("payload type \" %s\" is not implemented" , msg .Payload .Type ))
479+ }
480+
481+ return response , nil
482+ }
483+
434484func (c * testProxyServerClient ) close () {
435485 c .mu .Lock ()
436486 defer c .mu .Unlock ()
@@ -2588,3 +2638,112 @@ func Test_ProxyResumeFail(t *testing.T) {
25882638 assert .NotEqual (sessionId , connections [0 ].SessionId ())
25892639 }
25902640}
2641+
2642+ func Test_ProxySetBandwidth (t * testing.T ) {
2643+ t .Parallel ()
2644+ require := require .New (t )
2645+ assert := assert .New (t )
2646+ server := NewProxyServerForTest (t , "DE" )
2647+ mcu , _ := newMcuProxyForTestWithOptions (t , proxyTestOptions {
2648+ servers : []* TestProxyServerHandler {server },
2649+ }, 0 )
2650+
2651+ hub , _ , _ , hubserver := CreateHubForTestWithConfig (t , func (s * httptest.Server ) (* goconf.ConfigFile , error ) {
2652+ config , err := getTestConfig (s )
2653+ if err != nil {
2654+ return nil , err
2655+ }
2656+
2657+ config .AddOption ("backend" , "maxstreambitrate" , "700000" )
2658+ config .AddOption ("backend" , "maxscreenbitrate" , "800000" )
2659+
2660+ config .AddOption ("backend" , "bitrateperroom" , "1000000" )
2661+ config .AddOption ("backend" , "minpublisherbitrate" , "10000" )
2662+ config .AddOption ("backend" , "maxpublisherbitrate" , "500000" )
2663+ return config , err
2664+ })
2665+ hub .SetMcu (mcu )
2666+
2667+ ctx , cancel := context .WithTimeout (context .Background (), testTimeout )
2668+ defer cancel ()
2669+
2670+ client , hello := NewTestClientWithHello (ctx , t , hubserver , hub , testDefaultUserId + "1" )
2671+
2672+ // Join room by id.
2673+ roomId := "test-room"
2674+ roomMsg := MustSucceed2 (t , client .JoinRoom , ctx , roomId )
2675+ require .Equal (roomId , roomMsg .Room .RoomId )
2676+ client .RunUntilJoined (ctx , hello .Hello )
2677+
2678+ require .NoError (client .SendMessage (MessageClientMessageRecipient {
2679+ Type : "session" ,
2680+ SessionId : hello .Hello .SessionId ,
2681+ }, MessageClientMessageData {
2682+ Type : "offer" ,
2683+ RoomType : "video" ,
2684+ Payload : api.StringMap {
2685+ "sdp" : MockSdpOfferAudioAndVideo ,
2686+ },
2687+ }))
2688+
2689+ client .RunUntilAnswer (ctx , MockSdpAnswerAudioAndVideo )
2690+
2691+ pub := mcu .getPublisherConnection (hello .Hello .SessionId , StreamTypeVideo )
2692+ require .NotNil (pub )
2693+
2694+ var publisherId string
2695+ var publisher * mcuProxyPublisher
2696+ pub .publishersLock .RLock ()
2697+ if assert .Len (pub .publishers , 1 ) {
2698+ for id , mcuPub := range pub .publishers {
2699+ publisherId = id
2700+ publisher = mcuPub
2701+ break
2702+ }
2703+ }
2704+ pub .publishersLock .RUnlock ()
2705+ require .NotEmpty (publisherId )
2706+ require .NotNil (publisher )
2707+
2708+ proxyclient := server .GetSingleClient ()
2709+ proxyclient .sendMessage (& ProxyServerMessage {
2710+ Type : "event" ,
2711+ Event : & EventProxyServerMessage {
2712+ Type : "update-load" ,
2713+ Load : 1 ,
2714+ ClientBandwidths : map [string ]EventProxyServerBandwidth {
2715+ publisherId : {
2716+ Sent : api .BandwidthFromBits (0 ),
2717+ Received : api .BandwidthFromBits (100000 ),
2718+ },
2719+ },
2720+ },
2721+ })
2722+
2723+ for publisher .Bandwidth () == nil {
2724+ if ! assert .NoError (ctx .Err ()) {
2725+ break
2726+ }
2727+
2728+ time .Sleep (time .Millisecond )
2729+ }
2730+ if bw := publisher .Bandwidth (); assert .NotNil (bw ) {
2731+ assert .EqualValues (0 , bw .Sent )
2732+ assert .EqualValues (100000 , bw .Received )
2733+ }
2734+
2735+ room := hub .getRoom (roomId )
2736+ require .NotNil (room )
2737+ room .updateBandwidth ().Wait ()
2738+
2739+ proxyPub := server .getPublisher (PublicSessionId (publisherId ))
2740+ require .NotNil (proxyPub )
2741+ for proxyPub .bandwidth .Load () == 0 {
2742+ if ! assert .NoError (ctx .Err ()) {
2743+ break
2744+ }
2745+
2746+ time .Sleep (time .Millisecond )
2747+ }
2748+ assert .EqualValues (500000 , proxyPub .bandwidth .Load ())
2749+ }
0 commit comments