From 57e2321a185445a9cad17d2429c41552e4d91b62 Mon Sep 17 00:00:00 2001 From: David Zhao <320582+davidzhao@users.noreply.github.com> Date: Thu, 21 Apr 2022 22:15:01 -0700 Subject: [PATCH] Expose Participant.IsPublisher to differentiate pubs from subs (#643) --- go.mod | 5 ++--- go.sum | 4 ++-- pkg/rtc/participant.go | 29 +++++++++++++++++++--------- pkg/rtc/participant_internal_test.go | 1 - pkg/rtc/transport.go | 5 +++++ pkg/rtc/transport_test.go | 13 ++++++++++++- pkg/testutils/timeout.go | 4 +++- test/client/client.go | 10 ++++++---- test/scenarios.go | 8 ++++++-- 9 files changed, 56 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index e81bfb68..efcaf6a8 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/google/wire v0.5.0 github.com/gorilla/websocket v1.4.2 github.com/hashicorp/golang-lru v0.5.4 - github.com/livekit/protocol v0.13.1 + github.com/livekit/protocol v0.13.2-0.20220421193517-fa9efaff8ca5 github.com/mackerelio/go-osstat v0.2.1 github.com/magefile/mage v1.11.0 github.com/maxbrunsfeld/counterfeiter/v6 v6.3.0 @@ -45,8 +45,6 @@ require ( gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) -require github.com/go-logr/zapr v1.2.3 // indirect - require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.1 // indirect @@ -56,6 +54,7 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/eapache/channels v1.1.0 // indirect github.com/eapache/queue v1.1.0 // indirect + github.com/go-logr/zapr v1.2.3 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/subcommands v1.2.0 // indirect github.com/google/uuid v1.3.0 // indirect diff --git a/go.sum b/go.sum index 53a19a45..8f94d8a8 100644 --- a/go.sum +++ b/go.sum @@ -131,8 +131,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lithammer/shortuuid/v3 v3.0.6 h1:pr15YQyvhiSX/qPxncFtqk+v4xLEpOZObbsY/mKrcvA= github.com/lithammer/shortuuid/v3 v3.0.6/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts= -github.com/livekit/protocol v0.13.1 h1:n7NDJmnaG9susPhKHPqwxLDwDYxl9L47+AgifPvT14s= -github.com/livekit/protocol v0.13.1/go.mod h1:BLtSeVmn2rLP37xjzw7gHgaAmkWl3L/L9bPvgSbaOfo= +github.com/livekit/protocol v0.13.2-0.20220421193517-fa9efaff8ca5 h1:cizw9GNLEQosDETGl69H/0dDGyIyDd8I6nLqjlXd/Ic= +github.com/livekit/protocol v0.13.2-0.20220421193517-fa9efaff8ca5/go.mod h1:BLtSeVmn2rLP37xjzw7gHgaAmkWl3L/L9bPvgSbaOfo= github.com/mackerelio/go-osstat v0.2.1 h1:5AeAcBEutEErAOlDz6WCkEvm6AKYgHTUQrfwm5RbeQc= github.com/mackerelio/go-osstat v0.2.1/go.mod h1:UzRL8dMCCTqG5WdRtsxbuljMpZt9PCAGXqxPst5QtaY= github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls= diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index e55883d7..004a4fa7 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -79,6 +79,7 @@ type ParticipantImpl struct { resSinkValid atomic.Bool subscriberAsPrimary bool grants atomic.Value // *auth.ClaimGrants + isPublisher atomic.Bool // reliable and unreliable data channels reliableDC *webrtc.DataChannel @@ -231,7 +232,6 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) { p.publisher.pc.OnDataChannel(p.onDataChannel) p.subscriber.OnOffer(p.onOffer) - p.subscriber.OnStreamStateChange(p.onStreamStateChange) p.setupUpTrackManager() @@ -381,6 +381,8 @@ func (p *ParticipantImpl) SetPermission(permission *livekit.ParticipantPermissio } } } + // update isPublisher attribute + p.isPublisher.Store(video.GetCanPublish() && p.publisher.IsEstablished()) if p.onParticipantUpdate != nil { p.onParticipantUpdate(p) @@ -394,14 +396,15 @@ func (p *ParticipantImpl) SetPermission(permission *livekit.ParticipantPermissio func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo { grants := p.ClaimGrants() info := &livekit.ParticipantInfo{ - Sid: string(p.params.SID), - Identity: string(p.params.Identity), - Name: string(p.params.Name), - State: p.State(), - JoinedAt: p.ConnectedAt().Unix(), - Version: p.version.Inc(), - Permission: grants.Video.ToPermission(), - Region: p.params.Region, + Sid: string(p.params.SID), + Identity: string(p.params.Identity), + Name: string(p.params.Name), + State: p.State(), + JoinedAt: p.ConnectedAt().Unix(), + Version: p.version.Inc(), + Permission: grants.Video.ToPermission(), + Region: p.params.Region, + IsPublisher: p.isPublisher.Load(), } info.Tracks = p.UpTrackManager.ToProto() if p.params.Grants != nil { @@ -491,6 +494,14 @@ func (p *ParticipantImpl) HandleOffer(sdp webrtc.SessionDescription) (answer web return } + if p.isPublisher.Load() != p.CanPublish() { + p.isPublisher.Store(p.CanPublish()) + // trigger update as well if participant is already fully connected + if p.State() == livekit.ParticipantInfo_ACTIVE && p.onParticipantUpdate != nil { + p.onParticipantUpdate(p) + } + } + if p.State() == livekit.ParticipantInfo_JOINING { p.updateState(livekit.ParticipantInfo_JOINED) } diff --git a/pkg/rtc/participant_internal_test.go b/pkg/rtc/participant_internal_test.go index 808fb8bd..08d521bc 100644 --- a/pkg/rtc/participant_internal_test.go +++ b/pkg/rtc/participant_internal_test.go @@ -221,7 +221,6 @@ func TestMuteSetting(t *testing.T) { } func TestConnectionQuality(t *testing.T) { - // loss based score is currently a publisher method. videoScore := func(loss float32, numPublishing, numRegistered uint32) float32 { var reducedQuality bool diff --git a/pkg/rtc/transport.go b/pkg/rtc/transport.go index 0d2de281..48db5b0b 100644 --- a/pkg/rtc/transport.go +++ b/pkg/rtc/transport.go @@ -241,6 +241,11 @@ func (t *PCTransport) PeerConnection() *webrtc.PeerConnection { return t.pc } +// IsEstablished returns true if the PeerConnection has been established +func (t *PCTransport) IsEstablished() bool { + return t.pc.ConnectionState() != webrtc.PeerConnectionStateNew +} + func (t *PCTransport) Close() { if t.streamAllocator != nil { t.streamAllocator.Stop() diff --git a/pkg/rtc/transport_test.go b/pkg/rtc/transport_test.go index 2ad3ecbb..9b45ef9b 100644 --- a/pkg/rtc/transport_test.go +++ b/pkg/rtc/transport_test.go @@ -88,6 +88,8 @@ func TestNegotiationTiming(t *testing.T) { require.NoError(t, err) transportB, err := NewPCTransport(params) require.NoError(t, err) + require.False(t, transportA.IsEstablished()) + require.False(t, transportB.IsEstablished()) handleICEExchange(t, transportA, transportB) offer := atomic.Value{} @@ -116,7 +118,16 @@ func TestNegotiationTiming(t *testing.T) { require.NoError(t, err) require.NoError(t, transportB.pc.SetLocalDescription(answer)) require.NoError(t, transportA.SetRemoteDescription(answer)) - time.Sleep(5 * time.Millisecond) + + testutils.WithTimeout(t, func() string { + if !transportA.IsEstablished() { + return "transportA is not established" + } + if !transportB.IsEstablished() { + return "transportB is not established" + } + return "" + }) // it should still be negotiating again require.Equal(t, negotiationStateClient, transportA.negotiationState) diff --git a/pkg/testutils/timeout.go b/pkg/testutils/timeout.go index 7f22a5df..ab641471 100644 --- a/pkg/testutils/timeout.go +++ b/pkg/testutils/timeout.go @@ -4,6 +4,8 @@ import ( "context" "testing" "time" + + "github.com/stretchr/testify/require" ) var ( @@ -17,7 +19,7 @@ func WithTimeout(t *testing.T, f func() string) { for { select { case <-ctx.Done(): - t.Fatal("timed out: " + lastErr) + require.Empty(t, lastErr) case <-time.After(10 * time.Millisecond): lastErr = f() if lastErr == "" { diff --git a/test/client/client.go b/test/client/client.go index 88421ff2..f62365dd 100644 --- a/test/client/client.go +++ b/test/client/client.go @@ -314,10 +314,12 @@ func (c *RTCClient) Run() error { case *livekit.SignalResponse_Update: c.lock.Lock() for _, p := range msg.Update.Participants { - if p.State != livekit.ParticipantInfo_DISCONNECTED { - c.remoteParticipants[livekit.ParticipantID(p.Sid)] = p - } else { - delete(c.remoteParticipants, livekit.ParticipantID(p.Sid)) + if livekit.ParticipantID(p.Sid) != c.id { + if p.State != livekit.ParticipantInfo_DISCONNECTED { + c.remoteParticipants[livekit.ParticipantID(p.Sid)] = p + } else { + delete(c.remoteParticipants, livekit.ParticipantID(p.Sid)) + } } } c.lock.Unlock() diff --git a/test/scenarios.go b/test/scenarios.go index d02de119..c15a980e 100644 --- a/test/scenarios.go +++ b/test/scenarios.go @@ -116,8 +116,12 @@ func scenarioReceiveBeforePublish(t *testing.T) { // now leave, and ensure that it's immediate c2.Stop() - time.Sleep(testutils.ConnectTimeout) - require.Empty(t, c1.RemoteParticipants()) + testutils.WithTimeout(t, func() string { + if len(c1.RemoteParticipants()) > 0 { + return fmt.Sprintf("expected no remote participants, actual: %v", c1.RemoteParticipants()) + } + return "" + }) } func scenarioDataPublish(t *testing.T) {