Skip to content

Latency and misc fixes#3401

Merged
j0sh merged 3 commits intomasterfrom
ja/latency-misc
Feb 24, 2025
Merged

Latency and misc fixes#3401
j0sh merged 3 commits intomasterfrom
ja/latency-misc

Conversation

@j0sh
Copy link
Copy Markdown
Collaborator

@j0sh j0sh commented Feb 20, 2025

  • Reduce segmenter ffmpeg's analyzeduration to 2s. This avoids waiting for audio during RTMP pull at the risk of missing late tracks, especially video with a long GOP.

  • Send current segment immediately when the publisher is ready to receive data, rather than waiting for the next segment. This reduces time-to-first-frame at the risk of higher perceived latency if playback happens to start at the current segment (which we are effectively buffering, instead of staying on the leading edge by waiting for the next segment)

  • Rearrange retry sleeps in the segmenter: if ffmpeg needs to retry, it will immediately check for stream existence and exit if necessary. If the stream still exists, only then will it sleep for 5 seconds. This avoids a 5 second delay in signaling EOF.

  • Add some comments around how retries are still kinda broken anyway

  • Fix a couple of missing body closes in mediamtx HTTP request handling

  • Log ffmpeg output on rtmp pull errors

@github-actions github-actions bot added the go Pull requests that update Go code label Feb 20, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 20, 2025

Codecov Report

Attention: Patch coverage is 0% with 19 lines in your changes missing coverage. Please review.

Project coverage is 32.13857%. Comparing base (0f9be6a) to head (3687f1f).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
media/rtmp2segment.go 0.00000% 8 Missing ⚠️
media/segment_reader.go 0.00000% 8 Missing ⚠️
media/mediamtx.go 0.00000% 2 Missing ⚠️
server/ai_live_video.go 0.00000% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@                 Coverage Diff                 @@
##              master       #3401         +/-   ##
===================================================
- Coverage   32.15038%   32.13857%   -0.01181%     
===================================================
  Files            147         147                 
  Lines          40830       40845         +15     
===================================================
  Hits           13127       13127                 
- Misses         26928       26943         +15     
  Partials         775         775                 
Files with missing lines Coverage Δ
server/ai_live_video.go 0.00000% <0.00000%> (ø)
media/mediamtx.go 0.00000% <0.00000%> (ø)
media/rtmp2segment.go 3.38983% <0.00000%> (-0.08843%) ⬇️
media/segment_reader.go 0.00000% <0.00000%> (ø)

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0f9be6a...3687f1f. Read the comment docs.

Files with missing lines Coverage Δ
server/ai_live_video.go 0.00000% <0.00000%> (ø)
media/mediamtx.go 0.00000% <0.00000%> (ø)
media/rtmp2segment.go 3.38983% <0.00000%> (-0.08843%) ⬇️
media/segment_reader.go 0.00000% <0.00000%> (ø)

... and 2 files with indirect coverage changes

* Reduce segmenter ffmpeg's analyzeduration to 2s. This avoids waiting for
  audio during RTMP pull at the risk of missing late tracks, especially video
  with a long GOP.

* Send current segment immediately when the publisher is ready to receive data,
  rather than waiting for the next segment. This reduces time-to-first-frame at
  the risk of higher *perceived* latency if playback happens to start at the
  current segment (which we are effectively buffering, instead of staying on the
  leading edge by waiting for the next segment)

* Rearrange retry sleeps in the segmenter: if ffmpeg needs to retry, it will
  immediately check for stream existence and exit if necessary. If the stream
  still exists, only then will it sleep for 5 seconds. This avoids a 5 second
  delay in signaling EOF.

* Add some comments around how retries are still kinda broken anyway

* Fix a couple of missing body closes in mediamtx HTTP request handling

* Log ffmpeg output on rtmp pull errors
@leszko
Copy link
Copy Markdown
Contributor

leszko commented Feb 20, 2025

Looks good from my end. Thanks for the fixes Josh!

@j0sh
Copy link
Copy Markdown
Collaborator Author

j0sh commented Feb 24, 2025

Also added a 2.5s analyzeduration to the output side of ffmpeg. That should trim a couple seconds off the output delay - it's not much but something for now.

@j0sh j0sh merged commit 7f48092 into master Feb 24, 2025
18 checks passed
@j0sh j0sh deleted the ja/latency-misc branch February 24, 2025 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants