-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMediaQueue.cpp
139 lines (120 loc) · 2.29 KB
/
MediaQueue.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
#include "stdafx.h"
#include "MediaQueue.h"
#include "trace.h"
/**
The queue of video frames.
Initalize the queue with the specified size
\param queueSize The size of the video queue
*/
CMediaQueue::CMediaQueue(int queueSize)
{
count = 0;
size=queueSize;
head = tail = NULL;
ptr = pstatic = (MediaQueue *)malloc(sizeof(MediaQueue)*queueSize);
ZeroMemory(pstatic, sizeof(MediaQueue)*queueSize);
head = ptr;
readpos = writepos = head;
for (int i = 1; i < queueSize; i++)
{
ptr->next = pstatic+i;
ptr = ptr->next;
}
tail = ptr;
tail->next = head;
ptr = head;
hFrameListLock = CreateMutex(NULL,FALSE,NULL);
hRecvEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
/**
Cleanup the queuw
*/
CMediaQueue::~CMediaQueue()
{
CloseHandle(hRecvEvent);
CloseHandle(hFrameListLock);
//empty the queue
while (count >= 0)
{
//remove a frame so we can add one
count--;
if (readpos!=NULL && readpos->frame != NULL)
{
free(readpos->frame);
readpos->frame = NULL;
}
readpos = readpos->next;
}
free(pstatic);
}
/**
Add a new video frame to the queue
*/
void CMediaQueue::put(FrameInfo* frame)
{
if(ptr == NULL)
return;
WaitForSingleObject(hFrameListLock,INFINITE);
if (count >= size)
{
//remove a frame so we can add one
count = size-1;
if (readpos->frame)
{
free(readpos->frame);
readpos->frame = NULL;
}
readpos = readpos->next;
}
//add the frame
writepos->frame = frame;
writepos = writepos->next;
count++;
if (count <=1)
{
SetEvent(hRecvEvent);
}
ReleaseMutex(hFrameListLock);
}
/**
Remove a video frame from the queue
*/
FrameInfo* CMediaQueue::get()
{
FrameInfo* frame = NULL;
if (count < 1)
{
TRACE_WARN("No frames in queue, waiting");
WaitForSingleObject(hRecvEvent, 500);
}
ResetEvent(hRecvEvent);
WaitForSingleObject(hFrameListLock,INFINITE);
if(count > 0)
{
frame = readpos->frame;
readpos->frame = NULL;
readpos = readpos->next;
count--;
}else{
TRACE_ERROR("No frames in queue");
}
ReleaseMutex(hFrameListLock);
return(frame);
}
/**
Empty the queue
*/
void CMediaQueue::reset()
{
WaitForSingleObject(hFrameListLock,INFINITE);
ptr = readpos;
while (ptr->frame)
{
free(ptr->frame);
ptr->frame = NULL;
ptr = ptr->next;
}
writepos = readpos;
count = 0;
ReleaseMutex(hFrameListLock);
}