文章目錄
- RTSPStreamPlayer.cpp
- RTSPStreamPlayer.h
- main.cpp
- 編譯
- 運行
在ffmpeg_rtsp原有的rtsp拉流項目基礎上加入了udp連接rtsp,日志模塊,opencv實施預覽等功能。
RTSPStreamPlayer.cpp
#include "RTSPStreamPlayer.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <experimental/filesystem>
#include <iomanip>
#include <sstream>namespace fs = std::experimental::filesystem;RTSPStreamPlayer::RTSPStreamPlayer(const std::string& rtsp_url, int target_width, int target_height): rtsp_url_(rtsp_url), target_width_(target_width), target_height_(target_height),is_running_(false), is_initialized_(false), format_context_(nullptr),codec_context_(nullptr), frame_(nullptr), frame_rgb_(nullptr),sws_context_(nullptr), buffer_(nullptr), video_stream_index_(-1),start_time_(-1), end_time_(-1), execution_duration_sec_(-1),start_time_set_(false), reconnect_attempts_(0), max_reconnect_attempts_(5),buffer_size_(10 * 1024 * 1024), // 默認10MB緩沖區timeout_(5000000), // 默認5秒超時frame_count_(0), current_fps_(0.0),is_recording_(false), output_format_context_(nullptr),output_codec_context_(nullptr), output_video_stream_(nullptr), start_pts_(0)
{}RTSPStreamPlayer::~RTSPStreamPlayer() {stopRecording();stop();// 釋放FFmpeg資源if (buffer_) {av_free(buffer_);}if (frame_rgb_) {av_frame_free(&frame_rgb_);}if (frame_) {av_frame_free(&frame_);}if (codec_context_) {avcodec_free_context(&codec_context_);}if (format_context_) {avformat_close_input(&format_context_);}if (sws_context_) {sws_freeContext(sws_context_);}
}void RTSPStreamPlayer::setBufferSize(int buffer_size) {buffer_size_ = buffer_size;
}void RTSPStreamPlayer::setTimeout(int timeout) {timeout_ = timeout;
}bool RTSPStreamPlayer::init(bool use_tcp) {// 釋放可能存在的資源if (format_context_) {avformat_close_input(&format_context_);format_context_ = nullptr;}// 初始化FFmpegav_register_all();avformat_network_init();std::cout << "嘗試初始化RTSP流: " << rtsp_url_ << std::endl;std::cout << "使用" << (use_tcp ? "TCP" : "UDP") << "傳輸協議" << std::endl;// 打開RTSP流format_context_ = avformat_alloc_context();AVDictionary *options = nullptr;// 設置傳輸協議if (use_tcp) {av_dict_set(&options, "rtsp_transport", "tcp", 0);}// 設置超時時間char timeout_str[20];snprintf(timeout_str, sizeof(timeout_str), "%d", timeout_);av_dict_set(&options, "stimeout", timeout_str, 0);// 設置緩沖區大小char buffer_size_str[20];snprintf(buffer_size_str, sizeof(buffer_size_str), "%d", buffer_size_);av_dict_set(&options, "buffer_size", buffer_size_str, 0);// 禁用多播av_dict_set(&options, "use_multicast", "0", 0);// 打開輸入流int ret = avformat_open_input(&format_context_, rtsp_url_.c_str(), nullptr, &options);if (ret != 0) {char errbuf[AV_ERROR_MAX_STRING_SIZE];av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);std::cerr << "無法打開RTSP流: " << errbuf << std::endl;av_dict_free(&options);return false;}av_dict_free(&options);// 獲取流信息ret = avformat_find_stream_info(format_context_, nullptr);if (ret < 0) {char errbuf[AV_ERROR_MAX_STRING_SIZE];av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);std::cerr << "無法獲取流信息: " << errbuf << std::endl;return false;}// 打印流信息(調試用)av_dump_format(format_context_, 0, rtsp_url_.c_str(), 0);// 查找視頻流video_stream_index_ = -1;for (unsigned int i = 0; i < format_context_->nb_streams; i++) {if (format_context_->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {video_stream_index_ = i;// 計算并打印幀率AVStream* video_stream = format_context_->streams[i];if (video_stream->r_frame_rate.den && video_stream->r_frame_rate.num) {double fps = av_q2d(video_stream->r_frame_rate);std::cout << "視頻幀率: " << fps << " FPS" << std::endl;} else if (video_stream->avg_frame_rate.den && video_stream->avg_frame_rate.num) {double fps = av_q2d(video_stream->avg_frame_rate);std::cout << "視頻平均幀率: " << fps << " FPS" << std::endl;}break;}}if (video_stream_index_ == -1) {std::cerr << "未找到視頻流" << std::endl;return false;}// 獲取解碼器參數AVCodecParameters* codec_params = format_context_->streams[video_stream_index_]->codecpar;std::cout << "找到視頻流,編碼格式: " << codec_params->codec_id << std::endl;// 獲取解碼器const AVCodec* codec = avcodec_find_decoder(codec_params->codec_id);if (!codec) {std::cerr << "無法找到解碼器" << std::endl;return false;}// 初始化解碼器上下文if (codec_context_) {avcodec_free_context(&codec_context_);}codec_context_ = avcodec_alloc_context3(codec);if (avcodec_parameters_to_context(codec_context_, codec_params) < 0) {std::cerr << "無法初始化解碼器上下文" << std::endl;return false;}// 解碼器選項AVDictionary *decoder_opts = nullptr;av_dict_set(&decoder_opts, "threads", "auto", 0); // 自動多線程av_dict_set(&decoder_opts, "low_delay", "1", 0); // 低延遲模式av_dict_set(&decoder_opts, "error_concealment", "1", 0); // 錯誤隱藏// 打開解碼器ret = avcodec_open2(codec_context_, codec, &decoder_opts);av_dict_free(&decoder_opts);if (ret < 0) {char errbuf[AV_ERROR_MAX_STRING_SIZE];av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);std::cerr << "無法打開解碼器: " << errbuf << std::endl;return false;}// 初始化幀if (frame_) av_frame_free(&frame_);frame_ = av_frame_alloc();if (frame_rgb_) av_frame_free(&frame_rgb_);frame_rgb_ = av_frame_alloc();// 分配RGB幀緩沖區int num_bytes = av_image_get_buffer_size(AV_PIX_FMT_BGR24, target_width_, target_height_, 1);if (buffer_) av_free(buffer_);buffer_ = (uint8_t*)av_malloc(num_bytes * sizeof(uint8_t));av_image_fill_arrays(frame_rgb_->data, frame_rgb_->linesize, buffer_, AV_PIX_FMT_BGR24, target_width_, target_height_, 1);// 初始化圖像轉換上下文if (sws_context_) sws_freeContext(sws_context_);sws_context_ = sws_getContext(codec_context_->width, codec_context_->height, codec_context_->pix_fmt,target_width_, target_height_, AV_PIX_FMT_BGR24,SWS_BICUBIC, nullptr, nullptr, nullptr);if (!sws_context_) {std::cerr << "無法初始化圖像轉換上下文" << std::endl;return false;}is_initialized_ = true;reconnect_attempts_ = 0;std::cout << "RTSP流初始化成功" << std::endl;return true;
}bool RTSPStreamPlayer::reconnect() {if (reconnect_attempts_ >= max_reconnect_attempts_) {std::cerr << "已達到最大重連次數(" << max_reconnect_attempts_ << "),停止嘗試" << std::endl;return false;}reconnect_attempts_++;std::cout << "嘗試重連(" << reconnect_attempts_ << "/" << max_reconnect_attempts_ << ")..." << std::endl;// 釋放當前資源if (codec_context_) {avcodec_free_context(&codec_context_);codec_context_ = nullptr;}if (format_context_) {avformat_close_input(&format_context_);format_context_ = nullptr;}// 等待一段時間再重連std::this_thread::sleep_for(std::chrono::seconds(2));// 嘗試重新初始化return init(true);
}void RTSPStreamPlayer::start() {if (!is_initialized_) {std::cerr << "請先初始化RTSP流" << std::endl;return;}is_running_ = true;std::thread processing_thread(&RTSPStreamPlayer::processStream, this);processing_thread.detach();// 顯示窗口cv::namedWindow("RTSP Stream", cv::WINDOW_AUTOSIZE);// 顯示循環while (is_running_) {cv::Mat frame;{std::lock_guard<std::mutex> lock(frame_mutex_);if (!current_frame_.empty()) {frame = current_frame_.clone();}}if (!frame.empty()) {cv::imshow("RTSP Stream", frame);}// 按鍵控制char c = (char)cv::waitKey(1);if (c == 27) { // ESC鍵退出stop();break;} else if (c == 'r' || c == 'R') { // R鍵開始/停止錄制if (isRecording()) {stopRecording();} else {startRecording();}}}cv::destroyWindow("RTSP Stream");
}void RTSPStreamPlayer::stop() {is_running_ = false;
}void RTSPStreamPlayer::setTimeRange(int64_t start_sec, int64_t duration_sec) {std::lock_guard<std::mutex> lock(frame_mutex_);if (start_sec == -1) {start_time_ = av_gettime();} else {start_time_ = av_gettime() + start_sec * AV_TIME_BASE;}if (duration_sec != -1) {end_time_ = start_time_ + duration_sec * AV_TIME_BASE;execution_duration_sec_ = duration_sec;} else {end_time_ = -1;execution_duration_sec_ = -1;}start_time_set_ = true;
}bool RTSPStreamPlayer::saveCurrentFrame(const std::string& filename, int quality) {std::lock_guard<std::mutex> lock(frame_mutex_);if (current_frame_.empty()) {return false;}std::vector<int> compression_params = {cv::IMWRITE_JPEG_QUALITY, quality};return cv::imwrite(filename, current_frame_, compression_params);
}cv::Mat RTSPStreamPlayer::getCurrentFrame() {std::lock_guard<std::mutex> lock(frame_mutex_);return current_frame_.clone();
}void RTSPStreamPlayer::processStream() {AVPacket packet;bool done = false;last_fps_calc_time_ = std::chrono::steady_clock::now();while (is_running_ && !done) {int ret = av_read_frame(format_context_, &packet);if (ret < 0) {char errbuf[AV_ERROR_MAX_STRING_SIZE];av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);std::cerr << "讀取幀失敗: " << errbuf << std::endl;// 嘗試重連if (!reconnect()) {done = true;}continue;}// 重置重連計數器reconnect_attempts_ = 0;if (packet.stream_index == video_stream_index_) {// 發送數據包到解碼器ret = avcodec_send_packet(codec_context_, &packet);if (ret != 0) {char errbuf[AV_ERROR_MAX_STRING_SIZE];av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);std::cerr << "發送數據包到解碼器失敗: " << errbuf << std::endl;av_packet_unref(&packet);// 刷新解碼器avcodec_flush_buffers(codec_context_);continue;}// 接收解碼后的幀while (avcodec_receive_frame(codec_context_, frame_) == 0) {// 計算時間戳int64_t pts = (frame_->pts != AV_NOPTS_VALUE) ?frame_->pts * av_q2d(format_context_->streams[video_stream_index_]->time_base) * AV_TIME_BASE :(frame_->pkt_dts != AV_NOPTS_VALUE ?frame_->pkt_dts * av_q2d(format_context_->streams[video_stream_index_]->time_base) * AV_TIME_BASE :(av_gettime() - start_time_));// 設置初始時間if (!start_time_set_) {start_time_ = av_gettime();if (execution_duration_sec_ != -1) {end_time_ = start_time_ + execution_duration_sec_ * AV_TIME_BASE;}start_time_set_ = true;}// 檢查是否在時間范圍內if (isWithinTimeRange(pts)) {// 轉換為RGB格式sws_scale(sws_context_, frame_->data, frame_->linesize, 0, codec_context_->height, frame_rgb_->data, frame_rgb_->linesize);// 幀率統計frame_count_++;auto now = std::chrono::steady_clock::now();auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - last_fps_calc_time_).count();// 每1秒計算一次幀率if (elapsed >= 1) {current_fps_ = frame_count_ / (double)elapsed;std::cout << "實際幀率: " << std::fixed << std::setprecision(2) << current_fps_ << " FPS" << std::endl;// 重置計數器frame_count_ = 0;last_fps_calc_time_ = now;}// 轉換為OpenCV的Matcv::Mat frame(target_height_, target_width_, CV_8UC3, frame_rgb_->data[0], frame_rgb_->linesize[0]);// 更新當前幀{std::lock_guard<std::mutex> lock(frame_mutex_);current_frame_ = frame.clone();}// 錄制幀(如果正在錄制)if (is_recording_) {AVFrame* frame_av = av_frame_alloc();frame_av->format = AV_PIX_FMT_BGR24;frame_av->width = target_width_;frame_av->height = target_height_;frame_av->data[0] = frame_rgb_->data[0];frame_av->linesize[0] = frame_rgb_->linesize[0];encodeAndWriteFrame(frame_av);av_frame_free(&frame_av);}} else if (end_time_ != -1 && pts > end_time_) {done = true;break;}}}av_packet_unref(&packet);}is_running_ = false;
}bool RTSPStreamPlayer::isWithinTimeRange(int64_t pts) {if (end_time_ == -1) {return true; // 沒有結束時間限制}return (pts >= start_time_ && pts <= end_time_);
}// 初始化視頻編碼器
bool RTSPStreamPlayer::initVideoEncoder() {// 創建輸出格式上下文avformat_alloc_output_context2(&output_format_context_, nullptr, "mp4", output_filename_.c_str());if (!output_format_context_) {std::cerr << "無法創建輸出格式上下文" << std::endl;return false;}// 查找輸出編碼器const AVCodec* output_codec = avcodec_find_encoder(AV_CODEC_ID_H264);if (!output_codec) {std::cerr << "找不到H264編碼器" << std::endl;return false;}// 創建視頻流output_video_stream_ = avformat_new_stream(output_format_context_, output_codec);if (!output_video_stream_) {std::cerr << "無法創建視頻流" << std::endl;return false;}// 初始化解碼器上下文output_codec_context_ = avcodec_alloc_context3(output_codec);if (!output_codec_context_) {std::cerr << "無法創建編碼器上下文" << std::endl;return false;}// 設置編碼器參數output_codec_context_->codec_id = output_codec->id;output_codec_context_->codec_type = AVMEDIA_TYPE_VIDEO;output_codec_context_->width = target_width_;output_codec_context_->height = target_height_;output_codec_context_->time_base = {1, 25}; // 25fpsoutput_codec_context_->framerate = {25, 1};output_codec_context_->pix_fmt = AV_PIX_FMT_YUV420P;// H264特定設置if (output_codec_context_->codec_id == AV_CODEC_ID_H264) {av_opt_set(output_codec_context_->priv_data, "preset", "ultrafast", 0);av_opt_set(output_codec_context_->priv_data, "tune", "zerolatency", 0);}// 設置比特率output_codec_context_->bit_rate = 4000000; // 4Mbps// 打開編碼器if (avcodec_open2(output_codec_context_, output_codec, nullptr) < 0) {std::cerr << "無法打開編碼器" << std::endl;return false;}// 將編碼器參數復制到流if (avcodec_parameters_from_context(output_video_stream_->codecpar, output_codec_context_) < 0) {std::cerr << "無法復制編碼器參數到流" << std::endl;return false;}// 打開輸出文件if (!(output_format_context_->oformat->flags & AVFMT_NOFILE)) {if (avio_open(&output_format_context_->pb, output_filename_.c_str(), AVIO_FLAG_WRITE) < 0) {std::cerr << "無法打開輸出文件: " << output_filename_ << std::endl;return false;}}// 寫入文件頭if (avformat_write_header(output_format_context_, nullptr) < 0) {std::cerr << "無法寫入文件頭" << std::endl;return false;}start_pts_ = 0;return true;
}// 編碼并寫入幀
bool RTSPStreamPlayer::encodeAndWriteFrame(AVFrame* frame) {if (!is_recording_ || !output_codec_context_ || !frame)return false;// 轉換為YUV420P格式AVFrame* yuv_frame = av_frame_alloc();yuv_frame->format = AV_PIX_FMT_YUV420P;yuv_frame->width = target_width_;yuv_frame->height = target_height_;if (av_frame_get_buffer(yuv_frame, 0) < 0) {std::cerr << "無法分配幀緩沖區" << std::endl;av_frame_free(&yuv_frame);return false;}SwsContext* sws_ctx = sws_getContext(target_width_, target_height_, AV_PIX_FMT_BGR24,target_width_, target_height_, AV_PIX_FMT_YUV420P,SWS_BICUBIC, nullptr, nullptr, nullptr);if (!sws_ctx) {std::cerr << "無法創建SWS上下文" << std::endl;av_frame_free(&yuv_frame);return false;}// 轉換顏色空間uint8_t* in_data[1] = {frame->data[0]};int in_linesize[1] = {frame->linesize[0]};sws_scale(sws_ctx, in_data, in_linesize, 0, target_height_,yuv_frame->data, yuv_frame->linesize);sws_freeContext(sws_ctx);// 設置時間戳yuv_frame->pts = start_pts_++;// 發送幀到編碼器if (avcodec_send_frame(output_codec_context_, yuv_frame) < 0) {std::cerr << "發送幀到編碼器失敗" << std::endl;av_frame_free(&yuv_frame);return false;}// 接收編碼后的數據包AVPacket pkt;av_init_packet(&pkt);pkt.data = nullptr;pkt.size = 0;bool success = false;while (avcodec_receive_packet(output_codec_context_, &pkt) == 0) {// 調整時間戳pkt.stream_index = output_video_stream_->index;av_packet_rescale_ts(&pkt, output_codec_context_->time_base, output_video_stream_->time_base);// 寫入數據包if (av_interleaved_write_frame(output_format_context_, &pkt) < 0) {std::cerr << "寫入數據包失敗" << std::endl;break;}success = true;}av_packet_unref(&pkt);av_frame_free(&yuv_frame);return success;
}// 開始錄制
bool RTSPStreamPlayer::startRecording() {std::lock_guard<std::mutex> lock(recording_mutex_);if (is_recording_) {std::cout << "已經在錄制中" << std::endl;return true;}// 獲取當前時間字符串auto now = std::chrono::system_clock::now();std::time_t now_time = std::chrono::system_clock::to_time_t(now);std::tm now_tm;
#ifdef _WIN32localtime_s(&now_tm, &now_time);
#elselocaltime_r(&now_time, &now_tm);
#endifstd::stringstream ss;ss << std::put_time(&now_tm, "%Y-%m-%d-%H-%M-%S");output_filename_ = "rtsp_" + ss.str() + ".mp4";// 初始化編碼器if (!initVideoEncoder()) {std::cerr << "初始化編碼器失敗" << std::endl;return false;}is_recording_ = true;std::cout << "開始錄制視頻到: " << output_filename_ << std::endl;return true;
}// 停止錄制
void RTSPStreamPlayer::stopRecording() {std::lock_guard<std::mutex> lock(recording_mutex_);if (!is_recording_) return;// 刷新編碼器if (output_codec_context_) {avcodec_send_frame(output_codec_context_, nullptr);AVPacket pkt;av_init_packet(&pkt);pkt.data = nullptr;pkt.size = 0;while (avcodec_receive_packet(output_codec_context_, &pkt) == 0) {pkt.stream_index = output_video_stream_->index;av_packet_rescale_ts(&pkt, output_codec_context_->time_base, output_video_stream_->time_base);av_interleaved_write_frame(output_format_context_, &pkt);av_packet_unref(&pkt);}}// 寫入文件尾if (output_format_context_) {av_write_trailer(output_format_context_);}// 釋放資源if (output_format_context_ && !(output_format_context_->oformat->flags & AVFMT_NOFILE)) {avio_closep(&output_format_context_->pb);}if (output_codec_context_) {avcodec_free_context(&output_codec_context_);output_codec_context_ = nullptr;}if (output_format_context_) {avformat_free_context(output_format_context_);output_format_context_ = nullptr;}output_video_stream_ = nullptr;is_recording_ = false;std::cout << "停止錄制,視頻已保存到: " << output_filename_ << std::endl;
}// 檢查是否正在錄制
bool RTSPStreamPlayer::isRecording() const {std::lock_guard<std::mutex> lock(recording_mutex_);return is_recording_;
}
RTSPStreamPlayer.h
#ifndef RTSP_STREAM_PLAYER_H
#define RTSP_STREAM_PLAYER_H#include <string>
#include <mutex>
#include <opencv2/opencv.hpp>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/time.h>
#include <libavutil/opt.h> // 用于av_opt_set
}class RTSPStreamPlayer {
public:/*** @brief 構造函數* @param rtsp_url RTSP流地址* @param target_width 目標寬度* @param target_height 目標高度*/RTSPStreamPlayer(const std::string& rtsp_url, int target_width = 1280, int target_height = 720);/*** @brief 析構函數*/~RTSPStreamPlayer();/*** @brief 初始化RTSP流* @param use_tcp 是否使用TCP傳輸* @return 成功返回true,失敗返回false*/bool init(bool use_tcp = true);/*** @brief 開始播放RTSP流*/void start();/*** @brief 停止播放*/void stop();/*** @brief 設置播放時間范圍* @param start_sec 開始時間(秒),-1表示立即開始* @param duration_sec 持續時間(秒),-1表示無限期*/void setTimeRange(int64_t start_sec = -1, int64_t duration_sec = -1);/*** @brief 保存當前幀為JPEG* @param filename 文件名* @param quality 質量(0-100)* @return 成功返回true*/bool saveCurrentFrame(const std::string& filename, int quality = 95);/*** @brief 獲取當前幀* @return 當前幀的拷貝*/cv::Mat getCurrentFrame();/*** @brief 設置緩沖區大小* @param buffer_size 緩沖區大小(字節)*/void setBufferSize(int buffer_size);/*** @brief 設置超時時間* @param timeout 超時時間(微秒)*/void setTimeout(int timeout);/*** @brief 開始錄制視頻* @return 成功返回true*/bool startRecording();/*** @brief 停止錄制視頻*/void stopRecording();/*** @brief 檢查是否正在錄制* @return 正在錄制返回true*/bool isRecording() const;/*** @brief 獲取幀互斥鎖*/std::mutex& getFrameMutex() { return frame_mutex_; }private:std::string rtsp_url_;int target_width_;int target_height_;bool is_running_;bool is_initialized_;int buffer_size_; // 緩沖區大小int timeout_; // 超時時間(微秒)// 幀率統計int frame_count_; // 幀計數器std::chrono::steady_clock::time_point last_fps_calc_time_; // 上次計算幀率的時間double current_fps_; // 當前幀率// FFmpeg相關變量AVFormatContext* format_context_;AVCodecContext* codec_context_;AVFrame* frame_;AVFrame* frame_rgb_;SwsContext* sws_context_;uint8_t* buffer_;int video_stream_index_;// 時間控制int64_t start_time_;int64_t end_time_;int64_t execution_duration_sec_;bool start_time_set_;// 當前幀和互斥鎖cv::Mat current_frame_;std::mutex frame_mutex_;// 重連控制int reconnect_attempts_;const int max_reconnect_attempts_;// 錄制相關變量bool is_recording_;std::string output_filename_;AVFormatContext* output_format_context_;AVCodecContext* output_codec_context_;AVStream* output_video_stream_;int64_t start_pts_;mutable std::mutex recording_mutex_;/*** @brief 處理視頻流的線程函數*/void processStream();/*** @brief 判斷時間戳是否在指定范圍內*/bool isWithinTimeRange(int64_t pts);/*** @brief 嘗試重新連接RTSP流*/bool reconnect();/*** @brief 初始化視頻編碼器*/bool initVideoEncoder();/*** @brief 將幀編碼并寫入文件*/bool encodeAndWriteFrame(AVFrame* frame);
};#endif // RTSP_STREAM_PLAYER_H
main.cpp
#include "RTSPStreamPlayer.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <sstream>// 自定義流緩沖區,同時輸出到終端和文件
class TeeBuf : public std::streambuf {
public:TeeBuf(std::streambuf* sb1, std::streambuf* sb2) : sb1_(sb1), sb2_(sb2) {}
private:int overflow(int c) override {if (c == EOF) {return !EOF;} else {return sb1_->sputc(c) == EOF || sb2_->sputc(c) == EOF ? EOF : c;}}int sync() override {return sb1_->pubsync() == 0 && sb2_->pubsync() == 0 ? 0 : -1;}
private:std::streambuf* sb1_;std::streambuf* sb2_;
};// 獲取當前時間字符串
std::string getCurrentTimeString() {auto now = std::chrono::system_clock::now();std::time_t now_time = std::chrono::system_clock::to_time_t(now);std::tm now_tm;
#ifdef _WIN32localtime_s(&now_tm, &now_time);
#elselocaltime_r(&now_time, &now_tm);
#endifstd::stringstream ss;ss << std::put_time(&now_tm, "%Y-%m-%d-%H-%M-%S");return ss.str();
}// 重定向輸出到日志文件
void redirectOutputToLog() {std::string timeStr = getCurrentTimeString();std::string logFileName = "rtsp_log_" + timeStr + ".txt";static std::ofstream logFile(logFileName, std::ios::app);static TeeBuf teeBuf(std::cout.rdbuf(), logFile.rdbuf());static std::ostream tee(&teeBuf);std::cout.rdbuf(tee.rdbuf());std::cerr.rdbuf(tee.rdbuf());
}int main(int argc, char* argv[]) {// 重定向輸出到日志文件redirectOutputToLog();if (argc < 2) {std::cerr << "用法: " << argv[0] << " <RTSP_URL>" << std::endl;return -1;}// 創建RTSP播放器實例RTSPStreamPlayer player(argv[1], 1280, 720);// 可以根據需要調整參數// player.setBufferSize(5 * 1024 * 1024); 設置5MB緩沖區// player.setTimeout(10000000); 設置10秒超時// 嘗試初始化,先嘗試TCP,如果失敗則嘗試UDPbool init_success = player.init(true);if (!init_success) {std::cout << "TCP初始化失敗,嘗試UDP傳輸..." << std::endl;init_success = player.init(false);}if (!init_success) {std::cerr << "初始化RTSP播放器失敗" << std::endl;return -1;}// 開始播放std::cout << "開始播放RTSP流: " << argv[1] << std::endl;std::cout << "按ESC鍵退出" << std::endl;std::cout << "按R鍵開始/停止錄制視頻" << std::endl;player.start();return 0;
}
編譯
g++ main.cpp RTSPStreamPlayer.cpp -o rtsp_player `pkg-config --cflags --libs opencv4` -lavformat -lavcodec -lswscale -lavutil -lpthread
運行
./rtsp_player rtsp://10.130.209.12:8554/v
10.130.209.12是本機ip地址,可以用vlc退流進行測試:
按R開啟錄制,實測拉流速度要比 vlc 快一秒。