使用 ffmpeg 打开 RTMP 文件时崩溃
Crash while opening a RTMP file using ffmpeg
我已经编写了使用 ffmpeg 播放视频的代码。
代码在我打开 AVI 文件时工作正常,但当我尝试打开 RTMP 源时出现错误。
在utils.c文件中utils.c文件中的以下函数
utils.c
int avcodec_parameters_to_context(AVCodecContext *codec,
const AVCodecParameters *par)
{
codec->codec_type = par->codec_type; // crash happens at this line.
}
**par 是 nullptr
这是我的代码
if (load_frame("rtmp://192.168.1.2/live/sumit", &file_width, &file_height, &myData)) {
std::cout << "file Loaded";
}
load_frame 函数的定义
AVFormatContext *av_format_ctx = avformat_alloc_context();
if (!av_format_ctx) {
std::cout << "could not create a format context\n";
return false;
}
if (avformat_open_input(&av_format_ctx, filename, NULL, NULL) < 0) {
std::cout << "Couldn't open video file\n";
return false;
}
AVCodecParameters* av_codec_params = nullptr;
AVCodec* av_codec = nullptr;
int video_stream_index = -1;
for (unsigned int i = 0; i < av_format_ctx->nb_streams; i++) {
auto stream = av_format_ctx->streams[i];
av_codec_params = av_format_ctx->streams[i]->codecpar;
av_codec = avcodec_find_decoder(av_codec_params->codec_id);
if (!av_codec) {
std::cout << "Couldn't find the codec\n";
continue;
}
if (av_codec_params->codec_type == AVMEDIA_TYPE_VIDEO)
{
video_stream_index = i;
std::cout << "Video stream found" << std::endl;
break;
}
if (video_stream_index < 0)
return false;
}
// set up codec context for the decoder
AVCodecContext* av_codec_ctx = avcodec_alloc_context3(av_codec);
if (!av_codec_ctx) {
std::cout << "Couldn't create AV context";
return false;
}
// this function invokes the error
if (avcodec_parameters_to_context(av_codec_ctx, av_codec_params) < 0) {
std::cout << "Couldn't initialize AVCodecContext";
return false;
}
////////////////// 编辑 ////////////////////////// ///////
我正在流式传输一个 mpeg 文件并且 av_format_ctx->nb_streams return 值为 0 ,为什么它找不到任何流。
我可以通过 vlc 中的流媒体选项在 vlc 上查看相同的文件。
将 avformat_open_input
视为 fopen
。它会打开一个 stream/file,但是你仍然没有关于 stream/file 内容的信息,只有一个句柄可以用来做进一步的操作。
如果要真正查看stream/file中的数据,必须先阅读headers以确定里面的内容。 avformat_find_stream_info
会做到这一点
我已经编写了使用 ffmpeg 播放视频的代码。
代码在我打开 AVI 文件时工作正常,但当我尝试打开 RTMP 源时出现错误。
在utils.c文件中utils.c文件中的以下函数
utils.c
int avcodec_parameters_to_context(AVCodecContext *codec,
const AVCodecParameters *par)
{
codec->codec_type = par->codec_type; // crash happens at this line.
}
**par 是 nullptr
这是我的代码
if (load_frame("rtmp://192.168.1.2/live/sumit", &file_width, &file_height, &myData)) {
std::cout << "file Loaded";
}
load_frame 函数的定义
AVFormatContext *av_format_ctx = avformat_alloc_context();
if (!av_format_ctx) {
std::cout << "could not create a format context\n";
return false;
}
if (avformat_open_input(&av_format_ctx, filename, NULL, NULL) < 0) {
std::cout << "Couldn't open video file\n";
return false;
}
AVCodecParameters* av_codec_params = nullptr;
AVCodec* av_codec = nullptr;
int video_stream_index = -1;
for (unsigned int i = 0; i < av_format_ctx->nb_streams; i++) {
auto stream = av_format_ctx->streams[i];
av_codec_params = av_format_ctx->streams[i]->codecpar;
av_codec = avcodec_find_decoder(av_codec_params->codec_id);
if (!av_codec) {
std::cout << "Couldn't find the codec\n";
continue;
}
if (av_codec_params->codec_type == AVMEDIA_TYPE_VIDEO)
{
video_stream_index = i;
std::cout << "Video stream found" << std::endl;
break;
}
if (video_stream_index < 0)
return false;
}
// set up codec context for the decoder
AVCodecContext* av_codec_ctx = avcodec_alloc_context3(av_codec);
if (!av_codec_ctx) {
std::cout << "Couldn't create AV context";
return false;
}
// this function invokes the error
if (avcodec_parameters_to_context(av_codec_ctx, av_codec_params) < 0) {
std::cout << "Couldn't initialize AVCodecContext";
return false;
}
////////////////// 编辑 ////////////////////////// ///////
我正在流式传输一个 mpeg 文件并且 av_format_ctx->nb_streams return 值为 0 ,为什么它找不到任何流。
我可以通过 vlc 中的流媒体选项在 vlc 上查看相同的文件。
将 avformat_open_input
视为 fopen
。它会打开一个 stream/file,但是你仍然没有关于 stream/file 内容的信息,只有一个句柄可以用来做进一步的操作。
如果要真正查看stream/file中的数据,必须先阅读headers以确定里面的内容。 avformat_find_stream_info
会做到这一点