Skip to content

mxl-sink: Fix wrong write video/audio offset#430

Merged
vt-tv merged 2 commits intodmf-mxl:mainfrom
bisect-pt:fix/network-stream-write
Apr 1, 2026
Merged

mxl-sink: Fix wrong write video/audio offset#430
vt-tv merged 2 commits intodmf-mxl:mainfrom
bisect-pt:fix/network-stream-write

Conversation

@pedro-alves-ferreira
Copy link
Copy Markdown
Contributor

This PR changes the mxl-sink write algorithm so that writes to the circular buffer are delayed until just before their presentation time.
This avoids overflowing the buffer in case the video and audio streams are received at significantly different times, e.g. as occurs in MPEG TS.

@SimaoFonseca2 SimaoFonseca2 force-pushed the fix/network-stream-write branch from c63de18 to cd51cad Compare March 6, 2026 17:27
Copy link
Copy Markdown
Contributor

@mlefebvre1 mlefebvre1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this fix!

I've added some minor comments.

Unfortunately, I did not have time to test this new version yet, but I should have time soon.

trace!("Audio sleeping: {:#?}", Duration::from_nanos(pts - mxl_now));
let (lock, cvar) = &*engine.sleep_flag;

let shutdown_guard = lock.lock().map_err(|_| gst::FlowError::Error)?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just declare this one as mutable and delete the assignment below

Comment thread rust/gst-mxl-rs/src/mxlsink/imp.rs Outdated
.destroy()
.map_err(|_| gst::error_msg!(gst::CoreError::Failed, ["Failed to destroy flow"]))?
};
let audio = state.audio.take().ok_or(gst::error_msg!(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simplified by using if let Some(audio) = state.audio instead. Same applies to video.

pub index: u64,
}

const LATENCY_CUSHION: u64 = 5_000_000;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both video and audio define the same latency cushion shouldn't that be shared?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to keep the ability to tune the latency cushion for audio and video independently if needed. Also, having different latency cushions shouldn’t introduce A/V desync, since synchronization is still driven by the PTS.

Comment thread rust/gst-mxl-rs/src/mxlsink/state.rs Outdated
pub sleep_flag: Arc<(Mutex<bool>, Condvar)>,
}

#[derive(Debug, Clone)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clone could be removed, it's only used once, but not be needed. You probably don't want the channel to be cloned anyway.


let mut shutdown_guard = shutdown_guard;

while !*shutdown_guard && pts > mxl_now {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, I think this loop is useless. On first iteration you either return because of a shutdown notification or because of a timeout. So you basically never loop. (Also applies to video)

@pedro-alves-ferreira
Copy link
Copy Markdown
Contributor Author

Thank you for this fix!

I've added some minor comments.

Unfortunately, I did not have time to test this new version yet, but I should have time soon.

Thanks, @mlefebvre1! @SimaoPFonseca will get back to you as soon as he has some time.

Signed-off-by: Simao Fonseca <simao.fonseca@bisect.pt>
@SimaoFonseca2 SimaoFonseca2 force-pushed the fix/network-stream-write branch from cd51cad to b256f36 Compare March 26, 2026 16:42
@mlefebvre1 mlefebvre1 self-requested a review March 31, 2026 19:49
Copy link
Copy Markdown
Contributor

@mlefebvre1 mlefebvre1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done some spot checks and it worked well.

@pedro-alves-ferreira
Copy link
Copy Markdown
Contributor Author

I've done some spot checks and it worked well.

Thanks, @mlefebvre1!

@pedro-alves-ferreira pedro-alves-ferreira requested review from a team and pac-work April 1, 2026 07:50
Copy link
Copy Markdown
Contributor

@vt-tv vt-tv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vt-tv vt-tv added the backport/v1.0 This PR should be back ported to the release branch of version 1.0. label Apr 1, 2026
@vt-tv vt-tv merged commit 20b2432 into dmf-mxl:main Apr 1, 2026
11 checks passed
@backport-mxl-pull-requst
Copy link
Copy Markdown

Backport failed for release/v1.0, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin release/v1.0
git worktree add -d .worktree/backport/430/-/release/v1.0 origin/release/v1.0
cd .worktree/backport/430/-/release/v1.0
git switch --create backport/430/-/release/v1.0
git cherry-pick -x 20b243207d24c9f8358b401ffe664183f963ca55

@pedro-alves-ferreira
Copy link
Copy Markdown
Contributor Author

Backport failed for release/v1.0, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin release/v1.0
git worktree add -d .worktree/backport/430/-/release/v1.0 origin/release/v1.0
cd .worktree/backport/430/-/release/v1.0
git switch --create backport/430/-/release/v1.0
git cherry-pick -x 20b243207d24c9f8358b401ffe664183f963ca55

@SimaoFonseca2 Can you please check this? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/v1.0 This PR should be back ported to the release branch of version 1.0.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants