diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index ebe59cc5..c1f785d0 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -3248,11 +3248,11 @@ func (p *ParticipantImpl) mediaTrackReceived( mt = p.addMediaTrack(signalCid, ti) newTrack = true - // if the addTrackRequest is sent before participant active then it means the client tries to publish - // before fully connected, in this case we only record the time when the participant is active since + // if the addTrackRequest is sent before publisher peer connection is established, then it means the client tries to publish + // before fully connected, in this case we only record the time when publisher peer connection is established since // we want this metric to represent the time cost by publishing. - if activeAt := p.lastActiveAt.Load(); activeAt != nil && createdAt.Before(*activeAt) { - createdAt = *activeAt + if connectedAt := p.TransportManager.PublisherFirstConnectedAt(); !connectedAt.IsZero() && createdAt.Before(connectedAt) { + createdAt = connectedAt } pubTime = time.Since(createdAt) p.dirty.Store(true) diff --git a/pkg/rtc/transport.go b/pkg/rtc/transport.go index 0848ae13..be22bbc4 100644 --- a/pkg/rtc/transport.go +++ b/pkg/rtc/transport.go @@ -1432,6 +1432,13 @@ func (t *PCTransport) HasEverConnected() bool { return !t.firstConnectedAt.IsZero() } +func (t *PCTransport) FirstConnectedAt() time.Time { + t.lock.RLock() + defer t.lock.RUnlock() + + return t.firstConnectedAt +} + func (t *PCTransport) GetICEConnectionInfo() *types.ICEConnectionInfo { return t.connectionDetails.GetInfo() } diff --git a/pkg/rtc/transportmanager.go b/pkg/rtc/transportmanager.go index 528cd72c..886217d7 100644 --- a/pkg/rtc/transportmanager.go +++ b/pkg/rtc/transportmanager.go @@ -227,6 +227,10 @@ func (t *TransportManager) HasPublisherEverConnected() bool { return t.publisher.HasEverConnected() } +func (t *TransportManager) PublisherFirstConnectedAt() time.Time { + return t.publisher.FirstConnectedAt() +} + func (t *TransportManager) IsPublisherEstablished() bool { return t.publisher.IsEstablished() } @@ -267,6 +271,14 @@ func (t *TransportManager) HasSubscriberEverConnected() bool { } } +func (t *TransportManager) SubscriberFirstConnectedAt() time.Time { + if t.params.UseOneShotSignallingMode || t.params.UseSinglePeerConnection { + return t.publisher.FirstConnectedAt() + } else { + return t.subscriber.FirstConnectedAt() + } +} + func (t *TransportManager) AddTrackLocal( trackLocal webrtc.TrackLocal, params types.AddTrackParams,