Move probe observer to pacer (#3214)
* Probe ID pass * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * clean up * typo * populate desired bytes * correct num probes calculation * debug log * remove unused constant * log channel observer * debug * clear isInProbe flag on end * clear probe flag on reset * re-arrange
This commit is contained in:
@@ -29,12 +29,15 @@ type Base struct {
|
||||
logger logger.Logger
|
||||
|
||||
bwe bwe.BWE
|
||||
|
||||
*ProbeObserver
|
||||
}
|
||||
|
||||
func NewBase(logger logger.Logger, bwe bwe.BWE) *Base {
|
||||
return &Base{
|
||||
logger: logger,
|
||||
bwe: bwe,
|
||||
logger: logger,
|
||||
bwe: bwe,
|
||||
ProbeObserver: NewProbeObserver(logger),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,10 +87,11 @@ func (b *Base) patchRTPHeaderExtensions(p *Packet) error {
|
||||
}
|
||||
}
|
||||
|
||||
packetSize := p.HeaderSize + len(p.Payload)
|
||||
if p.TransportWideExtID != 0 && b.bwe != nil {
|
||||
twccSN := b.bwe.RecordPacketSendAndGetSequenceNumber(
|
||||
sendingAt.UnixMicro(),
|
||||
p.Header.MarshalSize()+len(p.Payload),
|
||||
packetSize,
|
||||
p.IsRTX,
|
||||
)
|
||||
twccExt := rtp.TransportCCExtension{
|
||||
@@ -103,6 +107,7 @@ func (b *Base) patchRTPHeaderExtensions(p *Packet) error {
|
||||
}
|
||||
}
|
||||
|
||||
b.ProbeObserver.RecordPacket(packetSize, p.IsRTX, p.ProbeClusterId, p.IsProbe)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -18,14 +18,18 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
|
||||
"github.com/pion/rtp"
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
type Packet struct {
|
||||
Header *rtp.Header
|
||||
HeaderSize int
|
||||
Payload []byte
|
||||
IsRTX bool
|
||||
ProbeClusterId ccutils.ProbeClusterId
|
||||
IsProbe bool
|
||||
AbsSendTimeExtID uint8
|
||||
TransportWideExtID uint8
|
||||
WriteStream webrtc.TrackLocalWriter
|
||||
@@ -39,6 +43,14 @@ type Pacer interface {
|
||||
|
||||
SetInterval(interval time.Duration)
|
||||
SetBitrate(bitrate int)
|
||||
|
||||
SetPacerProbeObserverListener(listener PacerProbeObserverListener)
|
||||
StartProbeCluster(probeClusterId ccutils.ProbeClusterId, desiredBytes int)
|
||||
EndProbeCluster(probeClusterId ccutils.ProbeClusterId) ccutils.ProbeClusterInfo
|
||||
}
|
||||
|
||||
type PacerProbeObserverListener interface {
|
||||
OnPacerProbeObserverClusterComplete(probeClusterId ccutils.ProbeClusterId)
|
||||
}
|
||||
|
||||
// ------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
// Copyright 2023 LiveKit, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pacer
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils/mono"
|
||||
)
|
||||
|
||||
type ProbeObserver struct {
|
||||
logger logger.Logger
|
||||
|
||||
listener PacerProbeObserverListener
|
||||
|
||||
isInProbe atomic.Bool
|
||||
|
||||
lock sync.Mutex
|
||||
clusterStartTime int64
|
||||
activeProbeClusterId ccutils.ProbeClusterId
|
||||
desiredProbeClusterBytes int
|
||||
bytesNonProbePrimary int
|
||||
bytesNonProbeRTX int
|
||||
bytesProbe int
|
||||
isActiveClusterDone bool
|
||||
}
|
||||
|
||||
func NewProbeObserver(logger logger.Logger) *ProbeObserver {
|
||||
return &ProbeObserver{
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (po *ProbeObserver) SetPacerProbeObserverListener(listener PacerProbeObserverListener) {
|
||||
po.listener = listener
|
||||
}
|
||||
|
||||
func (po *ProbeObserver) StartProbeCluster(probeClusterId ccutils.ProbeClusterId, desiredBytes int) {
|
||||
if po.isInProbe.Load() {
|
||||
po.logger.Warnw(
|
||||
"ignoring start of a new probe cluster when already active", nil,
|
||||
"probeClusterId", probeClusterId,
|
||||
"desiredBytes", desiredBytes,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
po.lock.Lock()
|
||||
defer po.lock.Unlock()
|
||||
|
||||
po.clusterStartTime = mono.UnixNano()
|
||||
po.activeProbeClusterId = probeClusterId
|
||||
po.desiredProbeClusterBytes = desiredBytes
|
||||
po.bytesNonProbePrimary = 0
|
||||
po.bytesNonProbeRTX = 0
|
||||
po.bytesProbe = 0
|
||||
po.isActiveClusterDone = false
|
||||
|
||||
po.isInProbe.Store(true)
|
||||
}
|
||||
|
||||
func (po *ProbeObserver) EndProbeCluster(probeClusterId ccutils.ProbeClusterId) ccutils.ProbeClusterInfo {
|
||||
if !po.isInProbe.Load() {
|
||||
// probe not active
|
||||
if probeClusterId != ccutils.ProbeClusterIdInvalid {
|
||||
po.logger.Debugw(
|
||||
"ignoring end of a probe cluster when not active",
|
||||
"probeClusterId", probeClusterId,
|
||||
)
|
||||
}
|
||||
return ccutils.ProbeClusterInfoInvalid
|
||||
}
|
||||
|
||||
po.lock.Lock()
|
||||
defer po.lock.Unlock()
|
||||
|
||||
if po.activeProbeClusterId != probeClusterId {
|
||||
// probe cluster id not active
|
||||
po.logger.Warnw(
|
||||
"ignoring end of a probe cluster of a non-active one", nil,
|
||||
"probeClusterId", probeClusterId,
|
||||
"active", po.activeProbeClusterId,
|
||||
)
|
||||
return ccutils.ProbeClusterInfoInvalid
|
||||
}
|
||||
|
||||
clusterInfo := ccutils.ProbeClusterInfo{
|
||||
ProbeClusterId: po.activeProbeClusterId,
|
||||
DesiredBytes: po.desiredProbeClusterBytes,
|
||||
StartTime: po.clusterStartTime,
|
||||
EndTime: mono.UnixNano(),
|
||||
BytesProbe: po.bytesProbe,
|
||||
BytesNonProbePrimary: po.bytesNonProbePrimary,
|
||||
BytesNonProbeRTX: po.bytesNonProbeRTX,
|
||||
}
|
||||
|
||||
po.activeProbeClusterId = ccutils.ProbeClusterIdInvalid
|
||||
po.isInProbe.Store(false)
|
||||
|
||||
return clusterInfo
|
||||
}
|
||||
|
||||
func (po *ProbeObserver) RecordPacket(size int, isRTX bool, probeClusterId ccutils.ProbeClusterId, isProbe bool) {
|
||||
if !po.isInProbe.Load() {
|
||||
return
|
||||
}
|
||||
|
||||
po.lock.Lock()
|
||||
if probeClusterId != po.activeProbeClusterId || po.isActiveClusterDone {
|
||||
po.lock.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
if isProbe {
|
||||
po.bytesProbe += size
|
||||
} else {
|
||||
if isRTX {
|
||||
po.bytesNonProbeRTX += size
|
||||
} else {
|
||||
po.bytesNonProbePrimary += size
|
||||
}
|
||||
}
|
||||
|
||||
notify := false
|
||||
var clusterId ccutils.ProbeClusterId
|
||||
if !po.isActiveClusterDone && po.bytesProbe+po.bytesNonProbePrimary+po.bytesNonProbeRTX >= po.desiredProbeClusterBytes {
|
||||
po.isActiveClusterDone = true
|
||||
|
||||
notify = true
|
||||
clusterId = po.activeProbeClusterId
|
||||
}
|
||||
po.lock.Unlock()
|
||||
|
||||
if notify && po.listener != nil {
|
||||
po.listener.OnPacerProbeObserverClusterComplete(clusterId)
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------
|
||||
Reference in New Issue
Block a user