Skip to content

Commit b84ce38

Browse files
committed
applications/voipstream: Update deprecated FFmpeg API calls with version guards
Modernized FFmpeg API usage in the voipstream application to support both legacy and current FFmpeg versions while eliminating deprecation warnings on newer versions. Changes: - Added version guards for avcodec_close() calls (deprecated since FFmpeg 3.1/libavcodec 58). The function is now only called for older FFmpeg versions, as avcodec_free_context() automatically closes the codec in newer versions. - Updated sample format retrieval in VoipStreamSender to handle the deprecated 'sample_fmts' field (deprecated since FFmpeg 6.1). The code now uses the legacy field for FFmpeg < 6.1 and the new avcodec_get_supported_config() API for FFmpeg >= 6.1.
1 parent 70a78f7 commit b84ce38

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/inet/applications/voipstream/AudioOutFile.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,12 @@ bool AudioOutFile::close()
203203
av_write_trailer(oc);
204204

205205
// close each codec
206+
#if LIBAVCODEC_VERSION_MAJOR < 58
207+
// avcodec_close() is needed for FFmpeg < 3.1 (libavcodec < 58)
206208
if (audio_st)
207209
avcodec_close(codecCtx);
210+
#endif
211+
// Note: For FFmpeg >= 3.1, avcodec_free_context() automatically closes the codec
208212

209213
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
210214
// close the output file
@@ -225,4 +229,3 @@ AudioOutFile::~AudioOutFile()
225229
}
226230

227231
} // namespace inet
228-

src/inet/applications/voipstream/VoipStreamReceiver.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,11 @@ void VoipStreamReceiver::closeConnection()
227227
{
228228
if (!curConn.offline) {
229229
curConn.offline = true;
230+
#if LIBAVCODEC_VERSION_MAJOR < 58
231+
// avcodec_close() is needed for FFmpeg < 3.1 (libavcodec < 58)
230232
avcodec_close(curConn.decCtx);
233+
#endif
234+
// Note: For FFmpeg >= 3.1, avcodec_free_context() automatically closes the codec
231235
avcodec_free_context(&curConn.decCtx);
232236
curConn.outFile.close();
233237
emit(connStateSignal, -1L); // so that sum() yields the number of active sessions
@@ -284,4 +288,3 @@ void VoipStreamReceiver::finish()
284288
}
285289

286290
} // namespace inet
287-

src/inet/applications/voipstream/VoipStreamSender.cc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ VoipStreamSender::VoipStreamSender()
3737
VoipStreamSender::~VoipStreamSender()
3838
{
3939
if (pEncoderCtx) {
40+
#if LIBAVCODEC_VERSION_MAJOR < 58
41+
// avcodec_close() is needed for FFmpeg < 3.1 (libavcodec < 58)
4042
avcodec_close(pEncoderCtx);
43+
#endif
44+
// Note: For FFmpeg >= 3.1, avcodec_free_context() automatically closes the codec
4145
avcodec_free_context(&pEncoderCtx);
4246
}
4347
cancelAndDelete(timer);
@@ -179,9 +183,13 @@ void VoipStreamSender::finish()
179183
{
180184
outFile.close();
181185

186+
#if LIBAVCODEC_VERSION_MAJOR < 58
187+
// avcodec_close() is needed for FFmpeg < 3.1 (libavcodec < 58)
182188
if (pCodecCtx) {
183189
avcodec_close(pCodecCtx);
184190
}
191+
#endif
192+
// Note: For FFmpeg >= 3.1, avcodec_free_context() automatically closes the codec
185193

186194
if (pReSampleCtx) {
187195
swr_close(pReSampleCtx);
@@ -252,7 +260,22 @@ void VoipStreamSender::openSoundFile(const char *name)
252260
if (!pCodecEncoder)
253261
throw cRuntimeError("Codec '%s' not found!", codec);
254262

263+
// Set sample format - handle deprecated sample_fmts field in newer FFmpeg versions
264+
#if LIBAVCODEC_VERSION_MAJOR < 61
255265
pEncoderCtx->sample_fmt = pCodecEncoder->sample_fmts[0];
266+
#else /* LIBAVCODEC_VERSION_MAJOR < 61 */
267+
// For FFmpeg 6.1+, use the new API to get supported sample formats
268+
const void *sample_fmts_ptr = nullptr;
269+
int num_sample_fmts = 0;
270+
err = avcodec_get_supported_config(nullptr, pCodecEncoder, AV_CODEC_CONFIG_SAMPLE_FORMAT,
271+
0, &sample_fmts_ptr, &num_sample_fmts);
272+
if (err >= 0 && num_sample_fmts > 0 && sample_fmts_ptr != nullptr) {
273+
const enum AVSampleFormat *sample_fmts = (const enum AVSampleFormat *)sample_fmts_ptr;
274+
pEncoderCtx->sample_fmt = sample_fmts[0];
275+
} else {
276+
throw cRuntimeError("Codec '%s' does not support any sample format!", codec);
277+
}
278+
#endif /* LIBAVCODEC_VERSION_MAJOR < 61 */
256279

257280
if (avcodec_open2(pEncoderCtx, pCodecEncoder, nullptr) < 0)
258281
throw cRuntimeError("could not open %s encoding codec!", codec);
@@ -571,4 +594,3 @@ void VoipStreamSender::resampleFrame(const uint8_t **in_data, int in_nb_samples)
571594
}
572595

573596
} // namespace inet
574-

0 commit comments

Comments
 (0)