forked from ZhaoliangGuo/pxScreenLiveStreaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpxCommonDef.cpp
161 lines (130 loc) · 4 KB
/
pxCommonDef.cpp
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
#include "pxCommonDef.h"
#include <time.h>
#include <stdio.h>
// YUV
CPxBufferPool g_oYUVBufferPool; // YUV内存池
CPxQueueBuffer g_oYUVQueueBuffer; // 存储采集的YUV数据
// PCM
CPxBufferPool g_oPCMBufferPool; // PCM内存池
CPxQueueBuffer g_oPCMQueueBuffer; // 存储采集的PCM数据
// H.264/AAC
CPxBufferPool g_oCodedBufferPool; // 存储编码后数据的内存池(H.264/AAC)
CPxQueueBuffer g_oCodedQueueBuffer; // 存储编码后数据的队列
// used to make sure that static variables in gettimeofday() aren't initialized simultaneously by multiple threads
#define _WIN32_WCE (1)
static LONG initializeLock_gettimeofday = 0;
int gettimeofday(struct timeval* tp, int* /*tz*/)
{
static LARGE_INTEGER tickFrequency, epochOffset;
static bool isInitialized = false;
LARGE_INTEGER tickNow;
#if !defined(_WIN32_WCE)
QueryPerformanceCounter(&tickNow);
#else
tickNow.QuadPart = GetTickCount();
#endif
if (!isInitialized)
{
if(1 == InterlockedIncrement(&initializeLock_gettimeofday))
{
#if !defined(_WIN32_WCE)
// For our first call, use "ftime()", so that we get a time with a proper epoch.
// For subsequent calls, use "QueryPerformanceCount()", because it's more fine-grain.
struct timeb tb;
ftime(&tb);
tp->tv_sec = tb.time;
tp->tv_usec = 1000*tb.millitm;
// Also get our counter frequency:
QueryPerformanceFrequency(&tickFrequency);
#else
/* FILETIME of Jan 1 1970 00:00:00. */
const LONGLONG epoch = 116444736000000000LL;
FILETIME fileTime;
LARGE_INTEGER time;
GetSystemTimeAsFileTime(&fileTime);
time.HighPart = fileTime.dwHighDateTime;
time.LowPart = fileTime.dwLowDateTime;
// convert to from 100ns time to unix timestamp in seconds, 1000*1000*10
tp->tv_sec = (long)((time.QuadPart - epoch) / 10000000L);
/*
GetSystemTimeAsFileTime has just a seconds resolution,
thats why wince-version of gettimeofday is not 100% accurate, usec accuracy would be calculated like this:
// convert 100 nanoseconds to usec
tp->tv_usec= (long)((time.QuadPart - epoch)%10000000L) / 10L;
*/
tp->tv_usec = 0;
// resolution of GetTickCounter() is always milliseconds
tickFrequency.QuadPart = 1000;
#endif
// compute an offset to add to subsequent counter times, so we get a proper epoch:
epochOffset.QuadPart = tp->tv_sec * tickFrequency.QuadPart
+ (tp->tv_usec * tickFrequency.QuadPart) / 1000000L
- tickNow.QuadPart;
// next caller can use ticks for time calculation
isInitialized = true;
return 0;
}
else
{
InterlockedDecrement(&initializeLock_gettimeofday);
// wait until first caller has initialized static values
while(!isInitialized)
{
Sleep(1);
}
}
}
// adjust our tick count so that we get a proper epoch:
tickNow.QuadPart += epochOffset.QuadPart;
tp->tv_sec = (long)(tickNow.QuadPart / tickFrequency.QuadPart);
tp->tv_usec = (long)(((tickNow.QuadPart % tickFrequency.QuadPart) * 1000000L) / tickFrequency.QuadPart);
return 0;
}
UINT64 GetCurrentTimestamp()
{
timeval tvTimestamp;
gettimeofday(&tvTimestamp, NULL);
UINT64 ui64TimeStamp = ((UINT64)tvTimestamp.tv_sec) * 1000 + tvTimestamp.tv_usec / 1000;
return ui64TimeStamp;
}
bool g_IsKeyFrame(const uint8_t *in_kui8Data, const int in_knSize)
{
assert(in_knSize > 5);
int nNALType = 0;
bool bKeyFrame = false;
if (AV_RB32(in_kui8Data) == 0x00000001)
{
nNALType = in_kui8Data[4] & 0x1F;
}
else if (AV_RB24(in_kui8Data) == 0x000001)
{
nNALType = in_kui8Data[3] & 0x1F;
}
if ((5 == nNALType) || (7 == nNALType) || (8 == nNALType))
{
bKeyFrame = true;
}
else
{
bKeyFrame = false;
}
return bKeyFrame;
}
bool g_WriteFile(const char *in_kpszFileName, const uint8_t *in_kui8Data, const int in_knSize, char *in_kszMode)
{
assert(NULL != in_kpszFileName);
assert(NULL != in_kui8Data);
assert(in_knSize > 0);
FILE *fpFile = fopen(in_kpszFileName, in_kszMode);
if (NULL == fpFile)
{
return false;
}
fwrite(in_kui8Data, 1, in_knSize, fpFile);
if (fpFile)
{
fclose(fpFile);
fpFile = NULL;
}
return true;
}