派生自 development/c++

pansen
2019-03-15 d883ec2891210b7e9361557b11e435c892528901
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "RtspCaptureElement.h"
#include <basic/debug/Debug.h>
#include <opencv2/opencv.hpp>
#include <basic/timer_counter/Clocktimer.h>
#include <basic/pipe_element/ffmpeg/cap_ffmpeg_impl.hpp>
#include <thread>
#include <basic/util/app/AppPreference.hpp>
#include <QString>
#include <QDateTime>
RtspCaptureElement::RtspCaptureElement(const std::string &path, const std::string& camId,int fps, int reopenTime, int gpuIndex):
    TimerElement(10),m_path(path),m_gpuIndex(gpuIndex),
    m_reopenTime(reopenTime),m_camId(camId){
    m_cutPath= appPref.getStringData("user.loop.absolute.path");
 
    assert(!m_cutPath.empty());
    m_capture = new CvCapture_FFMPEG(m_camId);
}
 
//定时执行,将
void RtspCaptureElement::timerFunc()
{
    u_char *data = nullptr;
    int width = 0;
    int height = 0;
    int step = 0;
    int cn = 0;
    DBG("GRABFrame "<<m_camId);
    bool ret = m_capture->grabFrame();
    if (!ret) {
        if (m_reopenTime < 0) {
            stop();
            INFO("grabFrame faild, element stopping");
            return;
        } else {
            usleep(m_reopenTime * 100);
            INFO("grabFrame faild, try reopen video: " << m_path);
            openVideo();
            ret = m_capture->grabFrame();
            if (!ret)return;
        }
    }
 
    m_picCount++;
    //几张选一张放入Redis
    if (m_picCount % m_nPicsPickOne != 0) {
        return;
    } else {
        m_picCount.store(0);
    }
 
    {
        ClockTimer timer("FrameToImage");
        m_capture->retrieveFrame(0, &data, &step, &width, &height, &cn);
 
        cv::Mat img(height, width, CV_8UC3, data, step);
        cv::Mat copyMat;
        img.copyTo(copyMat);
        std::string imageName = m_capture->GetImageName();
    }
    fireConnectors();
}
 
std::string RtspCaptureElement::MakeDir(const std::string &timeStamp)
{
    std::string t_FilePath = m_cutPath;
 
    if (t_FilePath.back() != '/') {
        t_FilePath.push_back('/');
    }
    char buf[24];
    QDateTime dt = QDateTime::fromString(QString::fromStdString(timeStamp), "yyyy-MM-dd hh:mm:ss:zzz");
 
    std::string t_strTime=dt.toString("yyyyMMddhh").toStdString();
    // DBG("t_strTime="<<t_strTime);
    t_FilePath.append(m_camId + "/" + t_strTime.substr(0, 6)+ "/" +t_strTime.substr(6, 2) + "/");
    //YYYYMMDDHH
    t_FilePath.append(t_strTime.substr(0,10)+ "/");
    std::string t_cmd = "mkdir -p '";
    t_cmd.append(t_FilePath + "'");
    //#get path mkdir path
    system(t_cmd.c_str());
 
    return t_FilePath;
}
void RtspCaptureElement::SaveVideo(const std::string &strImageName)
{
    std::string strTimeStamp= AppUtil::getTimeUSecString();
    std::string strPath=MakeDir(strTimeStamp);
    m_capture->SaveVideoByImageName(strPath,strImageName);
}
 
 
void RtspCaptureElement::openVideo()
{
    if(m_gpuIndex>=0){
        setenv("CUDA_VISIBLE_DEVICES", std::to_string(m_gpuIndex).c_str(),0);
    }
    bool bResult = m_capture->open(m_path.c_str(),m_gpuIndex>=0);
    if(bResult)
    {
        INFO("GPUIndex: "<<m_gpuIndex<<" VideoPath:"<<m_path<<" Succeed");
    } else{
        ERR("GPUIndex: "<<m_gpuIndex<<" VideoPath:"<<m_path<<" Failed");
    }
}
 
//线程启动之前调用,
void RtspCaptureElement::threadInitial()
{
    openVideo();
}
 
//线程结束的时候调用,关闭ffmpeg流
void RtspCaptureElement::threadClosing()
{
    m_capture->close();
    delete m_capture;
    m_capture = nullptr;
}
 
//设置保存视频的最小和最大时长
void RtspCaptureElement::SetVideoMinMaxSeconds(const int minSeconds, const int maxSeconds)
{
    INFO("VideoMinSeconds: "<<minSeconds<<" VideoMaxSeconds: "<<maxSeconds);
    m_capture->SetMinMaxVideoSeconds(minSeconds,maxSeconds);
}