派生自 development/c++

zhangjixing
2019-01-05 641eab08f214327f107ee6e96f9209f24baedabb
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "VideoRecorder.h"
#include <unistd.h>
#include <QString>
 
 
VideoRecorder::VideoRecorder(const std::string &fullPath,const std::string &uuid, const std::string &strRtsp, const int mseconds,LDBTool* db)
    : m_pInFmtCtx(nullptr), m_pInVideoStream(nullptr), m_pOutFmtCtx(nullptr),
      m_pDict(nullptr), m_pOutVideo_stream(nullptr), m_bStatus(true),/*m_thd(recodeFunc,this),*/
      m_bStart(false), m_mseconds(mseconds), m_strfileName(""), m_uuid(uuid), m_bSave(false),
      m_fullPath(fullPath),db_c(db){
    m_CommonFFmpeg.init();
    for (int i = 0; i < 3; i++) {
        if (m_CommonFFmpeg.rtspInputOpen(strRtsp.c_str()) >= 0) {
            m_thd = new std::thread(recodeFunc, this);
            return;
        }
        usleep(20000);
    }
}
 
VideoRecorder::~VideoRecorder() {
 
    m_bSave = false;
    m_bStatus = false;
    m_thd->join();
    delete m_thd;
    m_thd = nullptr;
    m_CommonFFmpeg.rtspInputClose();
}
 
void VideoRecorder::thisRecodeFunc() {
    while (m_bStatus) {
        //        std::string strDate;
        //#todo get FilePath FileName
 
        ///#todo shengcheng  luzhi wenjian ming
 
 
 
        long lDate = getCurrentUs();
        bool bFirstKeyFrame = true;
        long int nFirstKeyPts = 0;
        long int nFirstKeyDts = 0;
 
        if (m_strfileName.empty() && !m_bSave) {
            GetFileName(m_strfileName);
 
            DBG("srtPath" << m_strfileName << "m_uuid" << m_uuid);
        } else{
            DBG("m_strfileName:" << m_strfileName);
            DBG("m_bSave:" << m_bSave);
        }
 
 
        m_CommonFFmpeg.fileOutOpen(m_strfileName.c_str(), m_CommonFFmpeg.getRtspStream());
        //#todo
        while (m_bStatus && ((getCurrentUs() - lDate) < m_mseconds * 1000)) {
            void *handle = m_CommonFFmpeg.rtspCreatePkg();
            if (m_CommonFFmpeg.rtspReadPkg(handle) < 0) {
                if(bFirstKeyFrame){
                    ERR("rtspReadPkg m_strfileName:" << m_strfileName.c_str() << "getRtspStream:"
                                                     << m_CommonFFmpeg.getRtspStream());
                    usleep(20000);//20ms
                    m_bSave = false;
                    m_strfileName.clear();
                    break;
                } else{
                    continue;
                }
 
            }
 
            if (bFirstKeyFrame) {
 
                if (m_CommonFFmpeg.isKeyFrame(handle)) {
                    DBG("I");
                    nFirstKeyPts = m_CommonFFmpeg.getPkgPts(handle);
                    nFirstKeyDts = m_CommonFFmpeg.getPkgDts(handle);
                    bFirstKeyFrame = false;
 
                    if (!m_bSave && db_c) {
                        if (db_c->insertRecordVedioPath(QString::fromStdString(m_uuid),
                                                       QString::fromStdString(m_strfileName))) {
                            m_bSave = true;
                        } else {
                            ERR("insertRecordVedioPath DB ERR m_uuid=" << m_uuid.c_str() << "m_strfileName="
                                                                       << m_strfileName.c_str());
                        }
                    }
                } else {
                    //                       DBG("slice");
                    m_CommonFFmpeg.rtspDestroyPkg(handle);
                    continue;
                    DBG("srtPath" << m_strfileName << "m_uuid" << m_uuid);
                }
            }
            m_CommonFFmpeg.conversion(handle, nFirstKeyPts, nFirstKeyDts,
                                      m_CommonFFmpeg.getRtspStream());
 
            m_CommonFFmpeg.fileWritePkg(handle);
 
            m_CommonFFmpeg.rtspDestroyPkg(handle);
        }
        m_CommonFFmpeg.fileOutClose();
 
    }
}
 
long VideoRecorder::getCurrentUs() {
    timeval time;
    gettimeofday(&time, nullptr);
    return time.tv_sec * 1000000 + time.tv_usec;
}
 
long VideoRecorder::getCurrentUs(std::string &strDateTime) {
    timeval time;
    gettimeofday(&time, nullptr);
    __time_t sec = time.tv_sec;
    char buf[128] = {0};
    strftime(buf, 64, "%Y%m%d_%H:%M:%S", localtime(&sec));
    char szDateTime[128] = {0};
    sprintf(szDateTime, "%s/%s:%ld.mp4", m_strfileName.c_str(), buf, time.tv_usec / 1000);
    strDateTime = szDateTime;
    return time.tv_sec * 1000000 + time.tv_usec;
}
 
void VideoRecorder::recodeFunc(void *arg) {
 
    VideoRecorder *thisPtr = (VideoRecorder *) arg;
    thisPtr->thisRecodeFunc();
}
 
void VideoRecorder::GetFileName(std::string &fileName) {
    //std::string tem="/home/basic/work_src/test_Rec/1";
//    timeval time;
//    gettimeofday(&time, nullptr);
    time_t t = time(nullptr);
 
    char buf[128] = {0};
    strftime(buf, 64, "%Y-%m-%d %H:%M:%S", localtime(&t));
    char szDateTime[256] = {0};
    sprintf(szDateTime, "%s%s.mp4", m_fullPath.c_str(), buf);
 
    fileName = szDateTime;
    //std::cout << "File path " << fileName.c_str() << "FUNC " << __FUNCTION__ << "  line  " << __LINE__ << std::endl;
}