fix: swr_init failure dude to FFmpeg 7 channel layout changes#148
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the native audio resampling setup to accommodate FFmpeg 7.x channel layout API/option changes, aiming to prevent swr_init failures seen with some AAC streams.
Changes:
- Switches from using
ch_layout.u.mask(bitmask) to passing anAVChannelLayout. - Updates swresample option setting from
in_channel_layout/out_channel_layouttoin_chlayout/out_chlayoutviaav_opt_set_chlayout.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| resampleContext = swr_alloc(); | ||
| av_opt_set_int(resampleContext, "in_channel_layout", channelLayout, 0); | ||
| av_opt_set_int(resampleContext, "out_channel_layout", channelLayout, 0); | ||
| av_opt_set_chlayout(resampleContext, "in_chlayout", &channelLayout, 0); | ||
| av_opt_set_chlayout(resampleContext, "out_chlayout", &channelLayout, 0); | ||
| av_opt_set_int(resampleContext, "in_sample_rate", sampleRate, 0); | ||
| av_opt_set_int(resampleContext, "out_sample_rate", sampleRate, 0); |
There was a problem hiding this comment.
swr_alloc() and all av_opt_set_* calls return error codes (and swr_alloc() can return nullptr), but none of these are checked here. If in_chlayout/out_chlayout isn’t recognized by the linked FFmpeg build, the option set will silently fail and swr_init will still error (or you could dereference a null resampleContext). Please check return values, log a useful error, and ensure any partially-created SwrContext is freed on failure to avoid leaks/crashes.
Hello. This should fix swr_init on ffmpeg 7.x versions. Tested this on ffmpeg 7.1.1 with my own app, but haven't tested directly with nextlib.
Possibly #142 is the same issue