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

Recording: Don't use stateful filters in ffmpeg video processing (2.5 backport) #16050

Merged

Conversation

kepstin
Copy link
Contributor

@kepstin kepstin commented Nov 22, 2022

Because the input videos for BigBlueButton recording processing switch resolution and aspect ratio, the filter chain gets re-initialized, and any state in the filters is lost. This causes problems with the following filters.

The end result of all of these issues is that a lot of duplicate frames were created with invalid timestamps, which eventually get discarded by ffmpeg. But a lot of time is wasted, causing recordings to sometimes take hours to process when they should be ready in minutes.

This is a backport of #16047 to BigBlueButton 2.5, see that PR for details on the problem and fix.
Fixes #16044

Because the input videos for BigBlueButton recording processing switch
resolution and aspect ratio, the filter chain gets re-initialized, and
any state in the filters is lost. This causes problems with the
following filters:

`color`: Timestamps restart from 0, rather than continuing at the point
where they left off.
`fps=start_time=12.345`: After reset, the fps filter thinks it's at the
start of the file again, so the next frame it sees gets duplicated
output for timestamps from the `start_time` until it catches back up.
`setpts=PTS-STARTPTS`: The 'STARTPTS' is re-read as the first pts the
filter sees after reinitialization, so timestamp of the next frame is
reset to 0. (In practise, this didn't cause any problems because the
duplicate frames created by the fps filter had the original start time.)

The end result of all of these issues is that a lot of duplicate frames
were created with invalid timestamps, which eventually get discarded
by ffmpeg. But a lot of time is wasted, causing recordings to sometimes
take hours to process when they should be ready in minutes.

The fixes are as follows:

* The `color` filters are used to generate the background and
  substitutes for missing videos. Move them out to separate filter
  chains by using the 'lavfi' input format, which lets you use a filter
  as if it was an input file.
* Rather than use the `fps` filter's `start_time` feature, use the
  `trim` filter to remove early frames.
* The actual start pts is already known by the script, so replace
  `setpts=PTS-STARTPTS` with `setpts=PTS-12.345/TB`, substituting in the
  absolute time.
@kepstin kepstin changed the title Recording: Don't use stateful filters in ffmpeg video processing Recording: Don't use stateful filters in ffmpeg video processing (2.5 backport) Nov 22, 2022
@MaximKhlobystov MaximKhlobystov added this to the Release 2.5 milestone Nov 24, 2022
Even with the filter changes made, there's still some cases where
filter chain hangs can result from filter reconfigurations. To solve the
issue completely, I have split out pre-processing video files to
separate ffmpeg processes, so that the filter chain for compositing will
not ever be reconfigured.

Each input video now has a separate ffmpeg process run for it which
does the scaling, padding, and video extending steps. To avoid issues
with disk space usage and extra cpu usage or quality loss, the output
from these separate processes is sent to the compositing ffmpeg process
as uncompressed video in a pipe. To simplify the setup, named pipes
(special files) are used rather than setting up pipes in the ruby code
programmatically.

The extra ffmpeg processes are configured to log to files, and when
complete their log output is copied to the recording processing log.
Processes are joined to ensure zombie processes are not created, and
the return codes of all the processes are checked so errors can be
detected.

Due to the overhead of transferring video through pipes, this might
be a bit slower than the 2.4 recording processing - but on the other
hand, some of the video decoding and scaling happens in parallel, so it
might balance out.
@kepstin kepstin marked this pull request as draft December 6, 2022 15:27
The tpad filter is problematic on the variable-framerate webcam files,
and the result can end up being hangs (or, at least, very slow
processing) in the compositing.

Move the tpad filter to the compositing process where it can run after
the fps filter has converted the video to constant framerate. It still
needs to run before the start trimming, so switch to using the trim
filter rather than the fps filter's start_pts feature.
@sonarcloud
Copy link

sonarcloud bot commented Dec 6, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@kepstin kepstin marked this pull request as ready for review December 7, 2022 15:56
@antobinary antobinary merged commit 9a39751 into bigbluebutton:v2.5.x-release Dec 8, 2022
@cod3r0k
Copy link

cod3r0k commented Dec 22, 2022

Hi, How can we enable generating mp4 to use FFmpeg? Is it available in the latest version of 2.5?

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

Successfully merging this pull request may close these issues.

None yet

4 participants