Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]Audio Video not in sync. Sync logic in egress code not working #847

Open
sridhard opened this issue Jan 25, 2025 · 0 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@sridhard
Copy link

Describe the bug
Randomly the audio/video in mp4 files recorded by egress in participant egress is out-of-sync.
This issue happens when the user state audio and video at different times

I have analysed the a/v sync logic in egress code.
Below functions are used for synchronizing audio/video (Code snippets from server-sdk-go)

func (p *participantSynchronizer) onSenderReport(pkt *rtcp.SenderReport) {
	p.Lock()
	defer p.Unlock()

	if p.ntpStart.IsZero() {
		p.senderReports[pkt.SSRC] = pkt
		if len(p.senderReports) == len(p.tracks) {
			p.synchronizeTracks()
		}
		return
	}

	if t := p.tracks[pkt.SSRC]; t != nil {
		t.onSenderReport(pkt, p.ntpStart)
	}
}
func (p *participantSynchronizer) synchronizeTracks() {
	// get estimated ntp start times for all tracks
	estimatedStartTimes := make(map[uint32]time.Time)

	// we will sync all tracks to the earliest
	var earliestStart time.Time
	for ssrc, pkt := range p.senderReports {
		t := p.tracks[ssrc]
		pts := t.getSenderReportPTS(pkt)
		ntpStart := mediatransportutil.NtpTime(pkt.NTPTime).Time().Add(-pts)
		if earliestStart.IsZero() || ntpStart.Before(earliestStart) {
			earliestStart = ntpStart
		}
		estimatedStartTimes[ssrc] = ntpStart
	}
	p.ntpStart = earliestStart

	// update pts delay so all ntp start times will match the earliest
	for ssrc, startedAt := range estimatedStartTimes {
		t := p.tracks[ssrc]
		if diff := startedAt.Sub(earliestStart); diff != 0 {
			t.Lock()
			t.ptsOffset += diff
			t.Unlock()
		}
	}
}
func (t *TrackSynchronizer) onSenderReport(pkt *rtcp.SenderReport, ntpStart time.Time) {
	t.Lock()
	defer t.Unlock()

	// we receive every sender report twice
	if pkt.RTPTime == t.lastSR {
		return
	}

	pts := t.getSenderReportPTSLocked(pkt)
	calculatedNTPStart := mediatransportutil.NtpTime(pkt.NTPTime).Time().Add(-pts)
	drift := calculatedNTPStart.Sub(ntpStart)

	t.stats.updateDrift(drift)
	if drift > maxDrift {
		drift = maxDrift
	} else if drift < -maxDrift {
		drift = -maxDrift
	}
	t.ptsOffset += drift
	t.lastSR = pkt.RTPTime
}

This code sync audio/video using the ntp time stanp in sender recort of the tracks.
But egress is not receiving sender report for audio track from livekit-server.
Hence audio and video are not synchronized with ntp timestamp.

I have added logs in these functions and verified that no sender report is received for audio track.
Sender report is received for video track only.

I have verified participant egress and track egress. In both for audio track sender report is not received.

Is my understanding correct about the code or some other logic is used for a/v sync.

Egress Version
Latest master code

Additional context
NA

Logs
Post any relevant logs from the egress service here.

@sridhard sridhard added the bug Something isn't working label Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants