-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
#2074 Fix dropped frames at the end of encoders that buffer more than one packet. #2307
Conversation
Thanks! Why wasn't the test failing 🤔 |
…l samples_in were sent.
I believe I've identified the root cause of the infinite loop issue. While investigating, I found that the assertions were never reached because no audio frames were being received. I confirmed this by using the FFmpeg CLI, which showed that the audio stream was empty. This seemed unusual, so I looked into the Upon inspection, I noticed an issue on line 1293. If the To address this, I updated the condition. Instead of breaking out of the loop whenever
Therefore, you should continue looping until if ((ret = swr_convert(samples_convert_ctx, plane_ptr2, outputCount, plane_ptr, inputCount)) < 0) {
throw new Exception("swr_convert() error " + ret + ": Cannot convert audio samples.");
} else if (ret == 0 && inputCount == 0) { // Added inputCount == 0 condition
break;
} With this change, I was able to remove the check for a null frame entirely within the encoding loop. As a result, Vorbis no longer triggered an infinite loop with that test. The infinite loop occurs when no valid frames were encoded before flushing. In such cases,
To handle cases when we flush before encoding, I think we have two options:
Either solution prevents the infinite loop, but option 2 could be a cleaner and more efficient approach. Let me know what you think! |
Looks good, thanks!
If you believe you have a good fix for this, by all means, please do! |
Add wrote samples check to avoid infinite loop when Vorbis attempts to flush encoder before writing any samples.
Here's what I came up with. Essentially, I set a flag to true whenever we write any samples. While we're encoding the flushed samples, if we haven't written anything yet, we exit the loop after the first iteration. Alternatively, we could just return early from the method if we prefer. During testing, I found that trying to write a very small number of samples could also trigger the infinite loop. It turns out the swr_context wasn't being flushed properly, so any samples left in the buffer at the end of the file were dropped. If the file was small enough, no samples were ever written, causing the infinite loop. To fix this, I changed the logic to prevent breaking out of the loop immediately when samples is null in the recordSamples method. Now, the samples_out array is flushed at the end of that method. |
This looks alright I think. Anything else you'd like to fix before we merge this though? |
Nope I think this is good for now! I may put in a different PR later for centralizing some of the flushing concepts, but that'll require a little more restructuring which I think is out of scope for this PR. |
Fix for #2074. Add a retry mechanism. The test that was modified was asserting incorrectly, since the expected amerge behavior is to add the channels to the file if both the input files are stereo.