synchronizer

package
v2.12.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2025 License: Apache-2.0 Imports: 16 Imported by: 3

Documentation

Index

Constants

View Source
const (
	DefaultMaxTsDiff                    = time.Minute
	DefaultMaxDriftAdjustment           = 5 * time.Millisecond
	DefaultDriftAdjustmentWindowPercent = 0.0 // 0 -> disables throttling
	DefaultOldPacketThreshold           = 500 * time.Millisecond
)

Variables

View Source
var (
	ErrPacketOutOfOrder = errors.New("packet out-of-order")
	ErrPacketTooOld     = errors.New("packet too old")
)
View Source
var OWDEstimatorParamsDefault = OWDEstimatorParams{

	PropagationDelayFallFactor: 0.9,
	PropagationDelayRiseFactor: 0.1,

	PropagationDelaySpikeAdaptationFactor: 0.5,

	PropagationDelayDeltaThresholdMin:                10 * time.Millisecond,
	PropagationDelayDeltaThresholdMaxFactor:          2,
	PropagationDelayDeltaHighResetNumReports:         2,
	PropagationDelayDeltaHighResetWait:               10 * time.Second,
	PropagationDelayDeltaLongTermAdaptationThreshold: 50 * time.Millisecond,
}

Functions

This section is empty.

Types

type OWDEstimator added in v2.12.0

type OWDEstimator struct {
	// contains filtered or unexported fields
}

func NewOWDEstimator added in v2.12.0

func NewOWDEstimator(params OWDEstimatorParams) *OWDEstimator

func (*OWDEstimator) EstimatedPropagationDelay added in v2.12.0

func (o *OWDEstimator) EstimatedPropagationDelay() int64

func (*OWDEstimator) InitialAdjustment added in v2.12.0

func (o *OWDEstimator) InitialAdjustment(adjustmentNs int64) int64

func (*OWDEstimator) MarshalLogObject added in v2.12.0

func (o *OWDEstimator) MarshalLogObject(e zapcore.ObjectEncoder) error

func (*OWDEstimator) Update added in v2.12.0

func (o *OWDEstimator) Update(senderClockTimeNs int64, receiverClockTimeNs int64) (int64, bool)

type OWDEstimatorParams added in v2.12.0

type OWDEstimatorParams struct {
	PropagationDelayFallFactor float64
	PropagationDelayRiseFactor float64

	PropagationDelaySpikeAdaptationFactor float64

	PropagationDelayDeltaThresholdMin                time.Duration
	PropagationDelayDeltaThresholdMaxFactor          int64
	PropagationDelayDeltaHighResetNumReports         int
	PropagationDelayDeltaHighResetWait               time.Duration
	PropagationDelayDeltaLongTermAdaptationThreshold time.Duration
}

type Synchronizer

type Synchronizer struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

a single Synchronizer is shared between all audio and video writers

func NewSynchronizer

func NewSynchronizer(onStarted func()) *Synchronizer

func NewSynchronizerWithOptions added in v2.10.0

func NewSynchronizerWithOptions(opts ...SynchronizerOption) *Synchronizer

func (*Synchronizer) AddTrack

func (s *Synchronizer) AddTrack(track TrackRemote, identity string) *TrackSynchronizer

func (*Synchronizer) End

func (s *Synchronizer) End()

func (*Synchronizer) GetEndedAt

func (s *Synchronizer) GetEndedAt() int64

func (*Synchronizer) GetStartedAt

func (s *Synchronizer) GetStartedAt() int64

func (*Synchronizer) OnRTCP

func (s *Synchronizer) OnRTCP(packet rtcp.Packet)

OnRTCP syncs a/v using sender reports

func (*Synchronizer) RemoveTrack

func (s *Synchronizer) RemoveTrack(trackID string)

func (*Synchronizer) SetMediaRunningTime added in v2.12.0

func (s *Synchronizer) SetMediaRunningTime(mediaRunningTime func() (time.Duration, bool))

SetMediaRunningTime updates the external media running time provider after the synchronizer has been created. Passing a nil provider clears the configuration.

type SynchronizerConfig added in v2.10.0

type SynchronizerConfig struct {
	MaxTsDiff                         time.Duration
	MaxDriftAdjustment                time.Duration
	DriftAdjustmentWindowPercent      float64
	AudioPTSAdjustmentDisabled        bool
	PreJitterBufferReceiveTimeEnabled bool
	RTCPSenderReportRebaseEnabled     bool
	OldPacketThreshold                time.Duration
	EnableStartGate                   bool

	OnStarted func()

	MediaRunningTime         func() (time.Duration, bool)
	MaxMediaRunningTimeDelay time.Duration
}

SynchronizerConfig holds configuration for the Synchronizer

type SynchronizerOption added in v2.10.0

type SynchronizerOption func(*SynchronizerConfig)

func WithAudioPTSAdjustmentDisabled added in v2.11.3

func WithAudioPTSAdjustmentDisabled() SynchronizerOption

WithAudioPTSAdjustmentDisabled - disables auto PTS adjustments after sender reports Use case: when media processing pipeline needs stable - monotonically increasing PTS sequence - small adjustments coming from RTCP sender reports could cause gaps in the audio Media processing pipeline could opt out of auto PTS adjustments and handle the gap by e.g modifying tempo to compensate instead

func WithDriftAdjustmentWindowPercent added in v2.12.0

func WithDriftAdjustmentWindowPercent(driftAdjustmentWindowPercent float64) SynchronizerOption

WithDriftAdjustmentWinddowPercent controls throttling of how often drift adjustments are applied Throttles PTS adjustment to a limited amount in a time window. This setting determines how long a certain amount of adjustment throttles the next adjustment.

For example, if a 1ms adjustment is appied at 1%, it means that 1ms is 1% of ajustment window, so the adjustment window is 100ms and next adjustment will not be applied till that time elapses

With the settings of 5ms adjustment at 5%, a mamximum adjustment of 5ms per 100ms

func WithMaxDriftAdjustment added in v2.12.0

func WithMaxDriftAdjustment(maxDriftAdjustment time.Duration) SynchronizerOption

WithMaxDriftAdjustment sets the maximum drift adjustment applied at a time

func WithMaxTsDiff added in v2.10.0

func WithMaxTsDiff(maxTsDiff time.Duration) SynchronizerOption

WithMaxTsDiff sets the maximum acceptable difference between RTP packets In case exceeded the synchronizer will adjust the PTS offset to keep the audio and video in sync

func WithMediaRunningTime added in v2.12.0

func WithMediaRunningTime(mediaRunningTime func() (time.Duration, bool), maxMediaRunningTimeDelay time.Duration) SynchronizerOption

WithMediaRunningTime sets the callback to be called to get the media running time maxMediaRunningTimeDelay is the maximum allowed latency a packet can be behind the media running time

func WithOldPacketThreshold added in v2.12.0

func WithOldPacketThreshold(oldPacketThreshold time.Duration) SynchronizerOption

WithOldPacketThreshold sets the threshold at which a packet is considered old

func WithOnStarted added in v2.10.0

func WithOnStarted(onStarted func()) SynchronizerOption

WithOnStarted sets the callback to be called when the synchronizer starts

func WithPreJitterBufferReceiveTimeEnabled added in v2.12.0

func WithPreJitterBufferReceiveTimeEnabled() SynchronizerOption

WithPreJitterBufferReceiveTimeEnabled - enables use of packet arrival time before it is added to the jitter buffer

func WithRTCPSenderReportRebaseEnabled added in v2.12.0

func WithRTCPSenderReportRebaseEnabled() SynchronizerOption

WithRTCPSenderReportRebaseEnabled - enables rebasing RTCP Sender Report to local clock

func WithStartGate added in v2.12.0

func WithStartGate() SynchronizerOption

WithStartGate enabled will buffer incoming packets until pacing stabilizes before initializing tracks

type TrackRemote

type TrackRemote interface {
	ID() string
	Codec() webrtc.RTPCodecParameters
	Kind() webrtc.RTPCodecType
	SSRC() webrtc.SSRC
}

type TrackSynchronizer

type TrackSynchronizer struct {
	sync.Mutex

	*rtputil.RTPConverter
	// contains filtered or unexported fields
}

func (*TrackSynchronizer) Close added in v2.12.0

func (t *TrackSynchronizer) Close()

func (*TrackSynchronizer) GetPTS

GetPTS will adjust PTS offsets if necessary Packets are expected to be in order

func (*TrackSynchronizer) Initialize

func (t *TrackSynchronizer) Initialize(pkt *rtp.Packet)

Initialize should be called as soon as the first packet is received

func (*TrackSynchronizer) MarshalLogObject added in v2.12.0

func (t *TrackSynchronizer) MarshalLogObject(e zapcore.ObjectEncoder) error

func (*TrackSynchronizer) OnSenderReport added in v2.8.2

func (t *TrackSynchronizer) OnSenderReport(f func(drift time.Duration))

func (*TrackSynchronizer) PrimeForStart added in v2.12.0

func (t *TrackSynchronizer) PrimeForStart(pkt jitter.ExtPacket) ([]jitter.ExtPacket, int, bool)

PrimeForStart buffers incoming packets until pacing stabilizes, initializing the track synchronizer automatically once a suitable sequence has been observed. It returns the packets that should be forwarded, the number of packets dropped while waiting, and a boolean indicating whether the track is ready to process samples. After the gate finishes, there is no need to call the API again.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL