基于ffmpeg进行RTSP推流 c++
时间: 2024-01-29 20:41:08 浏览: 295
推流是将视频流通过网络传输到远程服务器或者设备的过程,而RTSP(Real Time Streaming Protocol)是一种流媒体传输协议,可以用于实现视频直播和点播等功能。基于FFmpeg进行RTSP推流可以使用FFmpeg的库函数来实现,下面是一个简单的C++代码示例:
```C++
#include <iostream>
#include <string>
#include <unistd.h>
#include <errno.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
#define RTSP_URL "rtsp://192.168.1.1/live"
int main(int argc, char *argv[]) {
av_register_all();
avcodec_register_all();
AVFormatContext *fmt_ctx = nullptr;
AVCodecContext *codec_ctx = nullptr;
AVCodec *codec = nullptr;
AVFrame *frame = nullptr;
AVPacket pkt = {};
// 打开输入文件并读取流信息
int ret = avformat_open_input(&fmt_ctx, "input.mp4", nullptr, nullptr);
if (ret < 0) {
std::cerr << "avformat_open_input error: " << av_err2str(ret) << std::endl;
return 1;
}
// 检索流信息
ret = avformat_find_stream_info(fmt_ctx, nullptr);
if (ret < 0) {
std::cerr << "avformat_find_stream_info error: " << av_err2str(ret) << std::endl;
return 1;
}
// 找到视频流
int video_stream_index = -1;
for (int i = 0; i < fmt_ctx->nb_streams; i++) {
if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_index = i;
break;
}
}
if (video_stream_index == -1) {
std::cerr << "Cannot find video stream" << std::endl;
return 1;
}
// 打开视频解码器
codec = avcodec_find_decoder(fmt_ctx->streams[video_stream_index]->codecpar->codec_id);
if (!codec) {
std::cerr << "Cannot find decoder for video stream" << std::endl;
return 1;
}
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) {
std::cerr << "Failed to allocate codec context" << std::endl;
return 1;
}
ret = avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[video_stream_index]->codecpar);
if (ret < 0) {
std::cerr << "Failed to copy codec parameters to codec context" << std::endl;
return 1;
}
ret = avcodec_open2(codec_ctx, codec, nullptr);
if (ret < 0) {
std::cerr << "Failed to open codec" << std::endl;
return 1;
}
// 分配AVFrame并初始化
frame = av_frame_alloc();
if (!frame) {
std::cerr << "Failed to allocate frame" << std::endl;
return 1;
}
// 初始化pkt
av_init_packet(&pkt);
pkt.data = nullptr;
pkt.size = 0;
// 打开输出
AVFormatContext *out_fmt_ctx = nullptr;
ret = avformat_alloc_output_context2(&out_fmt_ctx, nullptr, "rtsp", RTSP_URL);
if (ret < 0) {
std::cerr << "Failed to allocate output context" << std::endl;
return 1;
}
AVStream *out_stream = avformat_new_stream(out_fmt_ctx, nullptr);
if (!out_stream) {
std::cerr << "Failed to create new stream" << std::endl;
return 1;
}
AVCodecParameters *out_codecpar = out_stream->codecpar;
avcodec_parameters_copy(out_codecpar, fmt_ctx->streams[video_stream_index]->codecpar);
out_codecpar->codec_tag = 0;
ret = avio_open(&out_fmt_ctx->pb, RTSP_URL, AVIO_FLAG_WRITE);
if (ret < 0) {
std::cerr << "Failed to open output" << std::endl;
return 1;
}
// 写文件头
ret = avformat_write_header(out_fmt_ctx, nullptr);
if (ret < 0) {
std::cerr << "Failed to write header" << std::endl;
return 1;
}
// 读取并解码数据
while (av_read_frame(fmt_ctx, &pkt) >= 0) {
if (pkt.stream_index != video_stream_index) {
av_packet_unref(&pkt);
continue;
}
// 解码
ret = avcodec_send_packet(codec_ctx, &pkt);
if (ret < 0) {
std::cerr << "avcodec_send_packet error: " << av_err2str(ret) << std::endl;
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(codec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
std::cerr << "avcodec_receive_frame error: " << av_err2str(ret) << std::endl;
goto end;
}
// 转换像素格式
SwsContext *sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt,
codec_ctx->width, codec_ctx->height, AV_PIX_FMT_YUV420P,
SWS_BILINEAR, nullptr, nullptr, nullptr);
if (!sws_ctx) {
std::cerr << "Failed to create SwsContext" << std::endl;
goto end;
}
AVFrame *yuv_frame = av_frame_alloc();
if (!yuv_frame) {
std::cerr << "Failed to allocate yuv_frame" << std::endl;
goto end;
}
int dst_bufsize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, codec_ctx->width, codec_ctx->height, 1);
uint8_t *dst_data[4] = {nullptr};
int dst_linesize[4] = {0};
ret = av_image_alloc(dst_data, dst_linesize, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_YUV420P, 1);
if (ret < 0) {
std::cerr << "Failed to allocate image buffer" << std::endl;
av_frame_free(&yuv_frame);
goto end;
}
sws_scale(sws_ctx, frame->data, frame->linesize, 0, codec_ctx->height, yuv_frame->data, yuv_frame->linesize);
yuv_frame->width = codec_ctx->width;
yuv_frame->height = codec_ctx->height;
yuv_frame->format = AV_PIX_FMT_YUV420P;
// 编码
AVPacket out_pkt = {};
av_init_packet(&out_pkt);
out_pkt.data = nullptr;
out_pkt.size = 0;
ret = avcodec_send_frame(codec_ctx, yuv_frame);
if (ret < 0) {
std::cerr << "avcodec_send_frame error: " << av_err2str(ret) << std::endl;
av_packet_unref(&out_pkt);
goto end;
}
while (ret >= 0) {
ret = avcodec_receive_packet(codec_ctx, &out_pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
std::cerr << "avcodec_receive_packet error: " << av_err2str(ret) << std::endl;
av_packet_unref(&out_pkt);
goto end;
}
// 写入输出
out_pkt.stream_index = out_stream->index;
av_packet_rescale_ts(&out_pkt, codec_ctx->time_base, out_stream->time_base);
ret = av_interleaved_write_frame(out_fmt_ctx, &out_pkt);
if (ret < 0) {
std::cerr << "av_interleaved_write_frame error: " << av_err2str(ret) << std::endl;
av_packet_unref(&out_pkt);
goto end;
}
av_packet_unref(&out_pkt);
}
av_frame_free(&yuv_frame);
}
av_packet_unref(&pkt);
}
// 写文件尾
ret = av_write_trailer(out_fmt_ctx);
if (ret < 0) {
std::cerr << "Failed to write trailer" << std::endl;
}
end:
if (codec_ctx) {
avcodec_free_context(&codec_ctx);
}
if (fmt_ctx) {
avformat_close_input(&fmt_ctx);
}
if (frame) {
av_frame_free(&frame);
}
if (out_fmt_ctx) {
avio_closep(&out_fmt_ctx->pb);
avformat_free_context(out_fmt_ctx);
}
return 0;
}
```
这个代码示例将从本地文件`input.mp4`中读取视频流,然后将视频流转换为YUV420P像素格式,接着使用AVCodec对YUV420P像素进行压缩编码,并将编码后的数据通过RTSP协议推送到远程服务器。需要注意的是,这个代码示例中使用的RTSP URL是`rtsp://192.168.1.1/live`,如果你要使用这个示例代码,请先将RTSP URL替换为你要推流的URL。另外,这个示例代码仅供参考,实际使用中还需要根据具体情况进行修改和优化。
阅读全文