基于qt,msvc2017-64bits,ffmpeg.opengl的播放器
zhangmeng
2021-03-03 4a6d9312cc1c9d62d66c4def71246d9faae29edb
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "LogWriter.h"
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
#include "types.h"
 
#ifdef WIN32
#include <direct.h>
#include <io.h>                      //C (Windows)    access
#define R_OK 0
#else
#include <unistd.h>                  //C (Linux)      access
#endif
 
#if defined(WIN32)
    #include <WinSock2.h>
    #include <Windows.h>
static DWORD WINAPI thread_Func(LPVOID pM)
#else
    #include <sys/time.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <unistd.h>
static void *thread_Func(void *pM)
#endif
{
    LogWriter *pointer = (LogWriter*)pM;
    pointer->run();
 
    return 0;
}
 
#define TMPBUFFERLEN (1024 * 1024 * 3)
 
LogWriter::LogWriter()
{
    mCondition = new Cond;
 
    mTmpBuffer = new char[TMPBUFFERLEN];
 
#if defined(WIN32)
     HANDLE handle = CreateThread(NULL, 0, thread_Func, this, 0, NULL);
#else
    pthread_t thread1;
    pthread_create(&thread1,NULL,thread_Func,this);
#endif
}
 
LogWriter::~LogWriter()
{
    if (mTmpBuffer != NULL)
    {
        delete mTmpBuffer;
        mTmpBuffer = NULL;
    }
 
    if (mCondition != NULL)
    {
        delete mCondition;
        mCondition = NULL;
    }
}
 
void LogWriter::addLogNode(const LogInfoNode &node)
{
    mCondition->Lock();
    mLogNodeList.push_back(node);
    mCondition->Signal();
    mCondition->Unlock();
}
 
void LogWriter::writeLog(int cameraId, const std::string &str)
{
 
    LogInfoNode node;
    node.cameraId = cameraId;
    node.mCreateTime =  getTimeStamp_MilliSecond();
 
#if defined(WIN32)
    SYSTEMTIME sys;
    GetLocalTime( &sys );
 
    memset(mTmpBuffer, 0x0, TMPBUFFERLEN);
 
    sprintf(mTmpBuffer,"[%d-%02d-%02d %02d:%02d:%02d.%03d] %s\n",
            sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, str.c_str());
 
    node.logStr = mTmpBuffer;
 
#else
    struct timeval    tv;
    struct timezone tz;
 
    struct tm         *p;
 
    gettimeofday(&tv, &tz);
    p = localtime(&tv.tv_sec);
 
 
    memset(mTmpBuffer, 0x0, TMPBUFFERLEN);
 
//    memset(node.time,0x0,32);
//    sprintf(node.time,"%d-%02d-%02d %02d:%02d:%02d.%03d",1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
 
    sprintf(mTmpBuffer,"[%d-%02d-%02d %02d:%02d:%02d.%03d] %s\n",
            1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec, str.c_str());
 
    node.logStr = mTmpBuffer;
 
#endif
 
    addLogNode(node);
 
//    if (cameraId == WRITE_LOG_ID_MAIN)
//    {
//        fprintf(stderr, "******:");
//    }
 
//    if (cameraId == WRITE_LOG_ID_MAIN)
    {
 
#if defined(WIN32)
        fprintf(stderr,"[%d-%02d-%02d %02d:%02d:%02d.%03d] %s\n",
                sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, str.c_str());
 
#else
        fprintf(stderr,"[%d-%02d-%02d %02d:%02d:%02d.%03d] %s",
                1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec, str.c_str());
#endif
    }
 
}
 
void LogWriter::run()
{
 
    while(1)
    {
        mCondition->Lock();
 
        if (mLogNodeList.empty())
        {
            mCondition->Wait();
        }
 
        bool isNeedWriteToFile = false;
        if (mLogNodeList.size() >= 10)//ÈÕÖ¾Îļþ³¬¹ý10Ìõ ÔòдÈëÎļþ
        {
            isNeedWriteToFile = true;
        }
        else
        {
            uint64_t startTime = mLogNodeList.front().mCreateTime;
            uint64_t currentTime = getTimeStamp_MilliSecond();
 
            if ((currentTime - startTime) > (10000)) //ÈÕÖ¾Êý¾Ý×î³Ù10ÃëдÈëÎļþ
            {
                isNeedWriteToFile = true;
            }
        }
 
        if(isNeedWriteToFile)
        {
            std::list<LogInfoNode> LogNodeList = mLogNodeList;
            mLogNodeList.clear();
 
            mCondition->Unlock();
 
            while(!LogNodeList.empty())
            {
                LogInfoNode node = LogNodeList.front();
                LogNodeList.pop_front();
 
#ifdef WIN32
                char logDirName[20] = {0};
                sprintf(logDirName,"log\\%d",node.cameraId);
                ///Èç¹ûlogĿ¼²»´æÔÚ Ôò´´½¨
                if (access(logDirName, R_OK)!=0)
                {
//                        _mkdir(logDirName);
                    char cmd[32] = {0};
                    sprintf(cmd,"mkdir %s",logDirName);
                    system(cmd);
                }
#else
                char logDirName[20] = {0};
                sprintf(logDirName,"log/%d",node.cameraId);
                ///Èç¹ûlogĿ¼²»´æÔÚ Ôò´´½¨
                if (access(logDirName,R_OK)!=0)
                {
                    char cmd[32] = {0};
                    sprintf(cmd,"mkdir %s -p",logDirName);
                    system(cmd);
                }
#endif
 
                ///Ò»¸öÎļþ×î¶à5M ³¬¹ý5MÔò´´½¨ÏÂÒ»¸öÎļþ
                int index = 0;
                char fileName[36];
                while(1)
                {
                    memset(fileName,0x0,36);
                    sprintf(fileName,"log/%d/logfile_%d",node.cameraId,index++);
                    if (access(fileName, R_OK)==0)
                    {
                        FILE * fp = fopen(fileName, "r");
                        fseek(fp, 0L, SEEK_END);
                        int size = ftell(fp);
                        fclose(fp);
                        if (size < 5*1024*1024) //СÓÚ5MÔò¿ÉÒÔд
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
 
                FILE * fp = fopen(fileName, "at+");
                if (fp == NULL)
                {
                    fprintf(stderr,"дÈÕ־ʧ°Ü£¬ÇëÈ·±£ÄãÓÐ×ã¹»µÄȨÏÞд!\n");
                }
                else
                {
                    fwrite(node.logStr.c_str(),1,node.logStr.size(),fp);
                    fclose(fp);
                }
            }
        }
        else
        {
            mCondition->Unlock();
            mSleep(5000);
            continue;
        }
    }
}