If you're working with FFmpeg's C API and encounter deprecated functions like avcodec_flush_buffers, you should look for the updated best practices and functions that replace such deprecated ones. As of recent versions, avcodec_flush_buffers() may still be available, but if it's flagged as deprecated in your version of FFmpeg, here are some general steps for how you might handle buffer flushing and stream seeking:
General Approach for Seeking and Buffer Management
-
Ensure Decoder Properly Resets: When you seek, resetting or reinitializing the decoder state can be important to avoid using stale decoding data.
-
Use Updated APIs: If avcodec_flush_buffers() is deprecated, consult the FFmpeg documentation or source for alternatives. Often, new or updated APIs replace old functionality.
-
Re-open Streams and Reset States: Sometimes you might need to re-open streams or even re-initialize contexts to properly handle seeking in a way consistent with new versions of libraries.
Example Code with Updated Practices
Below is an improved version of our function, showing the concept of managing codec contexts and properly handling seeking while checking for newer library practices:
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/avutil.h>
#include <libavutil/time.h>
// Simulate receiving a new seek time
double receive_new_seek_time() {
// Implement mechanism to receive new seek time (e.g., socket or IPC)
// Temporary example returning -1 indicating no new seek requested
return -1.0;
}
void stream_to_rtsp(const char *input_filename, const char *output_url) {
avformat_network_init();
AVFormatContext *input_format_ctx = NULL;
AVFormatContext *output_format_ctx = NULL;
AVCodecContext *input_codec_ctx = NULL;
AVCodecContext *new_codec_ctx = NULL; // for reinitializing the stream
AVStream *input_video_stream = NULL;
int video_stream_index = -1;
if (avformat_open_input(&input_format_ctx, input_filename, NULL, NULL) < 0) {
fprintf(stderr, "Could not open input file '%s'\n", input_filename);
return;
}
if (avformat_find_stream_info(input_format_ctx, NULL) < 0) {
fprintf(stderr, "Failed to retrieve input stream information\n");
return;
}
for (unsigned int i = 0; i < input_format_ctx->nb_streams; i++) {
if (input_format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_index = i;
input_video_stream = input_format_ctx->streams[i];
break;
}
}
if (video_stream_index == -1) {
fprintf(stderr, "Could not find video stream in the input file\n");
return;
}
if (avformat_alloc_output_context2(&output_format_ctx, NULL, "rtsp", output_url) < 0) {
fprintf(stderr, "Could not create output context\n");
return;
}
AVStream *output_stream = avformat_new_stream(output_format_ctx, NULL);
if (!output_stream) {
fprintf(stderr, "Failed allocating output stream\n");
return;
}
input_codec_ctx = avcodec_alloc_context3(NULL);
if (!input_codec_ctx) {
fprintf(stderr, "Could not allocate codec context\n");
return;
}
avcodec_parameters_to_context(input_codec_ctx, input_video_stream->codecpar);
if (avcodec_parameters_from_context(output_stream->codecpar, input_codec_ctx) < 0) {
fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
return;
}
if (!(output_format_ctx->oformat->flags & AVFMT_NOFILE)) {
if (avio_open(&output_format_ctx->pb, output_url, AVIO_FLAG_WRITE) < 0) {
fprintf(stderr, "Could not open output URL '%s'\n", output_url);
return;
}
}
if (avformat_write_header(output_format_ctx, NULL) < 0) {
fprintf(stderr, "Error occurred when writing header to output stream\n");
return;
}
AVPacket pkt;
while (1) {
double new_seek_time = receive_new_seek_time();
if (new_seek_time >= 0) {
int64_t seek_target = new_seek_time * AV_TIME_BASE;
if (av_seek_frame(input_format_ctx, video_stream_index, seek_target, AVSEEK_FLAG_BACKWARD) < 0) {
fprintf(stderr, "Error seeking to %f seconds\n", new_seek_time);
continue;
}
// If avcodec_flush_buffers is deprecated, consider the following:
avcodec_free_context(&input_codec_ctx); // Free old context
input_codec_ctx = avcodec_alloc_context3(NULL); // Allocate new context
if (!input_codec_ctx) {
fprintf(stderr, "Could not reallocate codec context\n");
return;
}
avcodec_parameters_to_context(input_codec_ctx, input_video_stream->codecpar);
}
while (av_read_frame(input_format_ctx, &pkt) >= 0) {
if (pkt.stream_index == video_stream_index) {
pkt.pts = av_rescale_q_rnd(pkt.pts, input_codec_ctx->time_base, output_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
pkt.dts = av_rescale_q_rnd(pkt.dts, input_codec_ctx->time_base, output_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
pkt.duration = av_rescale_q(pkt.duration, input_codec_ctx->time_base, output_stream->time_base);
pkt.pos = -1;
if (av_interleaved_write_frame(output_format_ctx, &pkt) < 0) {
fprintf(stderr, "Error muxing packet\n");
break;
}
}
av_packet_unref(&pkt);
}
av_usleep(1000000);
}
av_write_trailer(output_format_ctx);
avcodec_free_context(&input_codec_ctx);
avformat_close_input(&input_format_ctx);
if (output_format_ctx && !(output_format_ctx->oformat->flags & AVFMT_NOFILE)) {
avio_closep(&output_format_ctx->pb);
}
avformat_free_context(output_format_ctx);
avformat_network_deinit();
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "Usage: %s <input_file> <rtsp_url>\n", argv[0]);
return 1;
}
const char *input_filename = argv[1];
const char *output_url = argv[2];
stream_to_rtsp(input_filename, output_url);
return 0;
}
Key Changes:
-
avcodec_free_context: Instead of using avcodec_flush_buffers to flush the codec state, I reallocate the codec context. This effectively resets the decoder, eliminating any remnants of the previous state.
-
av_seek_frame: The seeking operation remains the same, performing a seek with AVSEEK_FLAG_BACKWARD to a specific frame at the request time.
-
Resource Management: It's crucial to manage memory correctly, free old contexts before allocating new ones, and handle all resources properly to avoid memory leaks.
-
Communication Placeholder: The receive_new_seek_time function is still a stub. In a practical scenario, replace this with actual inter-process communication (IPC) logic or another mechanism to receive target seek times dynamically.
This method of resetting by re-allocating is a practical workaround if direct buffer flushing is unavailable or deprecated, assuming re-initialization costs are acceptable for your use case.