Handle time stamp increment across mute. (#1705)

* Handle time stamp increment across mute.

Two cases handled
1. Starting on mute could inject blank frame/padding packets.
These time stamps are randomly generated. So, when the publisher
unmutes, the time stamp was jumping ahead by only 1. Make it so
that they jump ahead by elapsed time since starting the blank frames/
padding packets.
2. When generating blank frames at the end of a down track, if
the track was muted at that time, the blank frame time stamps
could have been off (i. e. would have been pointing to time
after the last forwarded frame). Here also use current time
to adjust time stamp. Maybe, this could help in some cases where
we are seeing unflushed video buffer?

* remove unnecessary check

* address feedback and also maintain first synthesized time stamp
This commit is contained in:
Raja Subramanian
2023-05-10 18:31:49 +05:30
committed by GitHub
parent c254e5713f
commit b61fad339f
4 changed files with 121 additions and 93 deletions
+21 -27
View File
@@ -2,7 +2,6 @@ package sfu
import (
"fmt"
"math/rand"
"github.com/livekit/protocol/logger"
@@ -42,19 +41,17 @@ type SnTs struct {
// ----------------------------------------------------------------------
type RTPMungerState struct {
Started bool
LastSN uint16
LastTS uint32
LastSN uint16
LastTS uint32
}
func (r RTPMungerState) String() string {
return fmt.Sprintf("RTPMungerState{started: %v, lastSN: %d, lastTS: %d)", r.Started, r.LastSN, r.LastTS)
return fmt.Sprintf("RTPMungerState{lastSN: %d, lastTS: %d)", r.LastSN, r.LastTS)
}
// ----------------------------------------------------------------------
type RTPMungerParams struct {
started bool
highestIncomingSN uint16
lastSN uint16
snOffset uint16
@@ -86,7 +83,6 @@ func NewRTPMunger(logger logger.Logger) *RTPMunger {
func (r *RTPMunger) GetParams() RTPMungerParams {
return RTPMungerParams{
started: r.started,
highestIncomingSN: r.highestIncomingSN,
lastSN: r.lastSN,
snOffset: r.snOffset,
@@ -99,14 +95,12 @@ func (r *RTPMunger) GetParams() RTPMungerParams {
func (r *RTPMunger) GetLast() RTPMungerState {
return RTPMungerState{
Started: r.started,
LastSN: r.lastSN,
LastTS: r.lastTS,
LastSN: r.lastSN,
LastTS: r.lastTS,
}
}
func (r *RTPMunger) SeedLast(state RTPMungerState) {
r.started = state.Started
r.lastSN = state.LastSN
r.lastTS = state.LastTS
}
@@ -114,14 +108,8 @@ func (r *RTPMunger) SeedLast(state RTPMungerState) {
func (r *RTPMunger) SetLastSnTs(extPkt *buffer.ExtPacket) {
r.highestIncomingSN = extPkt.Packet.SequenceNumber - 1
r.highestIncomingTS = extPkt.Packet.Timestamp
if !r.started {
r.lastSN = extPkt.Packet.SequenceNumber
r.lastTS = extPkt.Packet.Timestamp
} else {
r.snOffset = extPkt.Packet.SequenceNumber - r.lastSN - 1
r.tsOffset = extPkt.Packet.Timestamp - r.lastTS - 1
}
r.started = true
r.lastSN = extPkt.Packet.SequenceNumber
r.lastTS = extPkt.Packet.Timestamp
}
func (r *RTPMunger) UpdateSnTsOffsets(extPkt *buffer.ExtPacket, snAdjust uint16, tsAdjust uint32) {
@@ -270,7 +258,8 @@ func (r *RTPMunger) FilterRTX(nacks []uint16) []uint16 {
return filtered
}
func (r *RTPMunger) UpdateAndGetPaddingSnTs(num int, clockRate uint32, frameRate uint32, forceMarker bool) ([]SnTs, error) {
func (r *RTPMunger) UpdateAndGetPaddingSnTs(num int, clockRate uint32, frameRate uint32, forceMarker bool, rtpTimestamp uint32) ([]SnTs, error) {
useLastTSForFirst := false
tsOffset := 0
if !r.lastMarker {
if !forceMarker {
@@ -278,20 +267,25 @@ func (r *RTPMunger) UpdateAndGetPaddingSnTs(num int, clockRate uint32, frameRate
}
// if forcing frame end, use timestamp of latest received frame for the first one
useLastTSForFirst = true
tsOffset = 1
}
if !r.started {
r.lastSN = uint16(rand.Intn(1<<14)) + uint16(1<<15) // a random number in third quartile of sequence number space
r.lastTS = uint32(rand.Intn(1<<30)) + uint32(1<<31) // a random number in third quartile of time stamp space
r.started = true
}
lastTS := r.lastTS
vals := make([]SnTs, num)
for i := 0; i < num; i++ {
vals[i].sequenceNumber = r.lastSN + uint16(i) + 1
if frameRate != 0 {
vals[i].timestamp = r.lastTS + uint32(i+1-tsOffset)*(clockRate/frameRate)
if useLastTSForFirst && i == 0 {
vals[i].timestamp = r.lastTS
} else {
ts := rtpTimestamp + ((uint32(i+1-tsOffset)*clockRate)+frameRate-1)/frameRate
if (ts-lastTS) == 0 || (ts-lastTS) > (1<<31) {
ts = lastTS + 1
lastTS = ts
}
vals[i].timestamp = ts
}
} else {
vals[i].timestamp = r.lastTS
}