Expose Participant.IsPublisher to differentiate pubs from subs (#643)

This commit is contained in:
David Zhao
2022-04-21 22:15:01 -07:00
committed by GitHub
parent 8bb46637bb
commit 57e2321a18
9 changed files with 56 additions and 23 deletions
+2 -3
View File
@@ -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
+2 -2
View File
@@ -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=
+20 -9
View File
@@ -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)
}
-1
View File
@@ -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
+5
View File
@@ -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()
+12 -1
View File
@@ -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)
+3 -1
View File
@@ -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 == "" {
+6 -4
View File
@@ -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()
+6 -2
View File
@@ -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) {