Expose Participant.IsPublisher to differentiate pubs from subs (#643)
This commit is contained in:
+20
-9
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 == "" {
|
||||
|
||||
Reference in New Issue
Block a user