From 12879769af38a00425309e292b2c167afc6612c1 Mon Sep 17 00:00:00 2001
From: pansen <pansen626@sina.com>
Date: 星期四, 04 四月 2019 11:27:38 +0800
Subject: [PATCH] 增加点播功能
---
QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp | 125 +++++++++++++++++++++++++----------------
1 files changed, 76 insertions(+), 49 deletions(-)
diff --git a/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp b/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
index 9e8fd7e..af7f598 100644
--- a/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
+++ b/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
@@ -32,7 +32,8 @@
dst.data, dst.linesize);
sws_freeContext(convert_ctx);
- DBG("m.size is " << m.size());
+// DBG("m.size is " << m.size());
+// LOG_IF();
return m;
}
@@ -44,7 +45,9 @@
}
BASICGB28181::FFmpegDecoderJPG::~FFmpegDecoderJPG() {
+ //娓呯┖闃熷垪
while (m_rtpQueue.count_queue()) {
+ //#todo delete frameBuffInfo*
m_rtpQueue.popNotWait();
}
@@ -74,10 +77,9 @@
}
#ifdef TestCode
- DBG(" m_rtpQueue.push before ");
+ DBG(" m_rtpQueue.push befores ");
#endif
- m_rtpQueue.
- push(info);
+ m_rtpQueue.push(info);
#ifdef TestCode
DBG(" m_rtpQueue.push after ");
#endif
@@ -95,10 +97,11 @@
do {
// DBG(" m_rtpQueue.pop before ");
+ //浠庣紦瀛樹腑鑾峰彇buffinfo
frameBuffInfo *buffinfo = fFmpegDecoderJPG->m_rtpQueue.pop();
// DBG(" m_rtpQueue.pop after ");
diff = len - buffinfo->buffLen;
-// printf("bufsize is :%ld,len is :%ld, datalen:%d \n", bufsize, len, buffinfo->buffLen);
+
//甯ч暱澶т簬bufsize
if (diff < 0) {
// DBG("/甯ч暱澶т簬bufsize" << diff);
@@ -129,7 +132,7 @@
delete[] buffinfo->buff;
delete buffinfo;
} while (diff > 0);
- //#todo 瑙﹀彂淇″彿
+
// DBG("emitSigal(\"read_dataOk\") begin");
// gSignalLock.emitSigal("read_dataOk");
fFmpegDecoderJPG->m_readData = true;
@@ -142,20 +145,23 @@
* @param p_this
*/
void BASICGB28181::FFmpegDecoderJPG::BareFlowDecoderThd(FFmpegDecoderJPG *p_this) {
- DBG(p_this->m_camIdx << " BareFlowDecoderThd ok ... gpuIdx is " << p_this->m_gpuIdx);
- while(!p_this->m_running) {
+ DBG(p_this->m_camIdx << " BareFlowDecoderThd ok ... gpuIdx is " << p_this->m_gpuIdx);
+ DBG("p_thisADDR:" << p_this << "p_this->m_running" << p_this->m_running);
+ while (!p_this->m_running) {
p_this->m_running = true;
// av_register_all();
// avformat_network_init();
p_this->ic = avformat_alloc_context();
+ //鍒涘缓ffmpeg浣跨敤鐨勫唴瀛樼┖闂�
p_this->iobuffer = (unsigned char *) av_malloc(p_this->m_buf_size);
if (!p_this->iobuffer) {
ERR("av_malloc: err======" << p_this->m_camIdx);
p_this->m_running = false;
continue;
}
+// 浣跨敤鍥炶皟鍑芥暟鍜屽唴瀛樺潡鍒涘缓 AVIOContext
p_this->avio = avio_alloc_context(p_this->iobuffer, p_this->m_buf_size, 0, p_this, p_this->read_data, NULL,
NULL);
if (!p_this->avio) {
@@ -167,20 +173,20 @@
int err = av_probe_input_buffer(p_this->ic->pb, &p_this->ic->iformat, nullptr, nullptr, 0, p_this->m_buf_size);
if (err) {
- ERR("av_probe_input_buffer: err======" << err << p_this->m_camIdx);
+ ERR("av_probe_input_buffer: err======" << err << p_this->m_camIdx);
p_this->m_running = false;
continue;
}
err = avformat_open_input(&p_this->ic, "", NULL, NULL);
if (err) {
- ERR("avformat_open_input: err======" << err << p_this->m_camIdx);
+ ERR("avformat_open_input: err======" << err << p_this->m_camIdx);
p_this->m_running = false;
continue;
}
// int err2 = avformat_find_stream_info(ic, nullptr);
err = avformat_find_stream_info(p_this->ic, NULL);
if (err) {
- ERR("avformat_find_stream_info: err======" << err << p_this->m_camIdx);
+ ERR("avformat_find_stream_info: err======" << err << p_this->m_camIdx);
// avformat_close_input(&p_this->ic);
// DBG("avformat_close_input(&p_this->ic);");
p_this->m_running = false;
@@ -193,12 +199,18 @@
break;
}
}
+ if(vi == -1)//鏃犺棰戝寘
+ {
+ ERR("no video packet!!");
+ p_this->m_running = false;
+ continue;
+ }
p_this->stream = p_this->ic->streams[vi];
p_this->video_st = p_this->stream;
p_this->ctx = avcodec_alloc_context3(nullptr);
err = avcodec_parameters_to_context(p_this->ctx, p_this->stream->codecpar);
if (err) {
- ERR("avcodec_parameters_to_context: err======" << err << p_this->m_camIdx);
+ ERR("avcodec_parameters_to_context: err======" << err << p_this->m_camIdx);
// avformat_close_input(&p_this->ic);
// DBG("avformat_close_input(&p_this->ic);");
p_this->m_running = false;
@@ -218,14 +230,14 @@
p_this->codec = p_this->codec_cuvid;
} else {
// return false;
- ERR("codec_cuvid is NULL" << p_this->m_camIdx);
+ ERR("codec_cuvid is NULL" << p_this->m_camIdx);
}
}
}
}
err = avcodec_open2(p_this->ctx, p_this->codec, nullptr);
if (err) {
- ERR("avcodec_open2: err======" << err << p_this->m_camIdx);
+ ERR("avcodec_open2: err======" << err << p_this->m_camIdx);
// avformat_close_input(&p_this->ic);
// DBG("avformat_close_input(&p_this->ic);");
// avcodec_free_context(&p_this->ctx);
@@ -237,9 +249,9 @@
av_init_packet(&p_this->pkt);
p_this->frame = av_frame_alloc();
- unsigned int usleepTime = (1000 / p_this->m_fps) - 12;
- usleepTime *= 1000;
- DBG("===open ok, camid: " << p_this->m_camIdx );
+// unsigned int usleepTime = (1000 / p_this->m_fps) - 12;
+// usleepTime *= 1000;
+ DBG("===open ok, camid: " << p_this->m_camIdx);
while (p_this->m_running) {
#ifdef TestCode
ClockTimer Test("while time");
@@ -255,7 +267,7 @@
int err6 = avcodec_send_packet(p_this->ctx, &p_this->pkt);
int err7 = avcodec_receive_frame(p_this->ctx, p_this->frame);
if ((err7 == AVERROR(EAGAIN)) || (err5 < 0) || (err6 < 0)) {
- ERR(" err======: err7" << err7 << " err5: " << err5 << " err6: " << err6 << p_this->m_camIdx);
+ ERR(" err======: err7" << err7 << " err5: " << err5 << " err6: " << err6 << p_this->m_camIdx);
usleep(40000);
continue;
}
@@ -266,39 +278,52 @@
// BASICGB28181::avframe_to_cvmat(frame).copyTo(p_this->m_image);
p_this->m_image = std::move(BASICGB28181::avframe_to_cvmat(p_this->frame));
-
-#ifdef TestCode
- {
- // TestCode 娴嬭瘯浠g爜淇濆瓨鍥剧墖鍒版湰鍦�
- ClockTimer cl("TestCode");
- std::string strNewTime2 = AppUtil::getTimeUSecString();
- cv::putText(p_this->m_image, strNewTime2, cv::Point(408, 540), cv::HersheyFonts::FONT_HERSHEY_PLAIN,
- 5, cv::Scalar(255, 255, 0), 2);
- std::thread test([&](cv::Mat img, std::string strThing) {
- try {
- std::string strNewTime = "tmpDec/";
- strNewTime.append(p_this->m_camIdx + "_").append(strThing).append(".jpg");
- // cv::imwrite(strNewTime, p_this->m_image);
- } catch (std::exception ex) {
- ERR(ex.what());
- }
- }, p_this->m_image, strNewTime2);
- test.detach();
+// 灏唅甯т繚瀛樹负蹇収
+ if (p_this->m_SnapshotNotSaveRet && (p_this->pkt.flags & AV_PKT_FLAG_KEY)) {
+ try {
+ std::string strNewName = "./";
+ strNewName.append(p_this->m_camIdx).append(".jpg");
+ cv::imwrite(strNewName, p_this->m_image);
+ p_this->m_SnapshotNotSaveRet = false;
+ } catch (std::exception ex) {
+ ERR(ex.what());
+ }
}
+
+
+#ifdef TestCode
+ {
+ // TestCode 娴嬭瘯浠g爜淇濆瓨鍥剧墖鍒版湰鍦�
+ ClockTimer cl("TestCode");
+ std::string strNewTime2 = AppUtil::getTimeUSecString();
+ cv::putText(p_this->m_image, strNewTime2, cv::Point(408, 540), cv::HersheyFonts::FONT_HERSHEY_PLAIN,
+ 5, cv::Scalar(255, 255, 0), 2);
+ std::thread test([&](cv::Mat img, std::string strThing) {
+ try {
+ std::string strNewTime = "tmpDec/";
+ strNewTime.append(p_this->m_camIdx + "_").append(strThing).append(".jpg");
+ // cv::imwrite(strNewTime, p_this->m_image);
+ } catch (std::exception ex) {
+ ERR(ex.what());
+ }
+ }, p_this->m_image, strNewTime2);
+ test.detach();
+ }
#endif
- //#todo send to other thd
+ //#todo send to other thd
#ifdef TestCode
- DBG("emitSigal(\"DecoderImageOK\") begin");
+ DBG("emitSigal(\"DecoderImageOK\") begin");
#endif
- //瑙﹀彂淇″彿
+ //瑙﹀彂涓婂眰淇″彿
gSignalLock.emitSigal(p_this->m_camIdx + "DecoderImageOK");
//#ifdef TestCode
// DBG("emitSigal(\"DecoderImageOK\") after");
//#endif
- DBG("emitSigal(\"DecoderImageOK\") after");
- DBG("p_this->m_camIdx is " << p_this->m_camIdx << " queue size is " << p_this->m_rtpQueue.count_queue());
+// DBG("emitSigal(\"DecoderImageOK\") after");
+
+// DBG("p_this->m_camIdx is " << p_this->m_camIdx << " queue size is " << p_this->m_rtpQueue.count_queue());
#ifdef TestCode
{
@@ -335,6 +360,7 @@
}
m_camIdx = camIdx;
DBG("BareFlowDecoderThd camIdx : " << camIdx);
+// 鍚姩瑙g爜绾跨▼
std::thread t_BareFlowDecoder(BareFlowDecoderThd, this);
t_BareFlowDecoder.detach();
);
@@ -427,7 +453,7 @@
delete iter->m_packet.data;
iter = m_packetsVec.erase(iter);
while (!(iter->m_packet.flags & AV_PKT_FLAG_KEY)) {
- INFO("DropFrame: " << iter->m_frameId);
+// INFO("DropFrame: " << iter->m_frameId);
delete iter->m_packet.data;
iter = m_packetsVec.erase(iter);
}
@@ -438,7 +464,7 @@
bool BASICGB28181::FFmpegDecoderJPG::SaveVideo(std::string path, int64_t lastFrameId) {
std::lock_guard<std::mutex> lock(g_mutex);
- INFO("SaveVideo: " << path << "m_packetsVec.size : " <<m_packetsVec.size());
+ INFO("SaveVideo: " << path << "m_packetsVec.size : " << m_packetsVec.size());
if (!m_packetsVec.empty()) {
startWrite(path.c_str());
int64_t firstKeyFramePts = m_packetsVec[0].m_packet.pts;
@@ -447,7 +473,8 @@
for (const auto &item:m_packetsVec) {
if (item.m_frameId < lastFrameId) {
DBG("item.m_frameId < lastFrameId " << item.m_frameId << " " << lastFrameId);
- conversion(const_cast<AVPacket *> (&item.m_packet), firstKeyFramePts, firstKeyFrameDts, video_st, frame_index);
+ conversion(const_cast<AVPacket *> (&item.m_packet), firstKeyFramePts, firstKeyFrameDts, video_st,
+ frame_index);
frame_index++;
av_write_frame(m_pOutFmtCtx, &item.m_packet);
} else {
@@ -593,13 +620,13 @@
DBG("frame_index==%d\n" << frame_index);
//Write PTS
- AVRational time_base1=inStream->time_base;
+ AVRational time_base1 = inStream->time_base;
//Duration between 2 frames (us)
- int64_t calc_duration=(double)AV_TIME_BASE/av_q2d(inStream->r_frame_rate);
+ int64_t calc_duration = (double) AV_TIME_BASE / av_q2d(inStream->r_frame_rate);
//Parameters
- pkg->pts=(double)(frame_index*calc_duration)/(double)(av_q2d(time_base1)*AV_TIME_BASE);
- pkg->dts=pkg->pts;
- pkg->duration=(double)calc_duration/(double)(av_q2d(time_base1)*AV_TIME_BASE);
+ pkg->pts = (double) (frame_index * calc_duration) / (double) (av_q2d(time_base1) * AV_TIME_BASE);
+ pkg->dts = pkg->pts;
+ pkg->duration = (double) calc_duration / (double) (av_q2d(time_base1) * AV_TIME_BASE);
// DBG("pts:" << pkg->pts);
// DBG("dts:" << pkg->dts);
--
Gitblit v1.8.0