forked from xufuji456/FFmpegAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacketQueue.h
More file actions
75 lines (60 loc) · 1.54 KB
/
PacketQueue.h
File metadata and controls
75 lines (60 loc) · 1.54 KB
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
#ifndef PACKET_QUEUE_H
#define PACKET_QUEUE_H
#include <queue>
#include <thread>
template<typename T>
class PacketQueue {
typedef void (*ReleaseCallback)(T &);
private:
std::mutex m_mutex;
std::condition_variable m_cond;
std::queue<T> m_queue;
bool m_running;
ReleaseCallback releaseCallback;
public:
void push(T new_value) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_running) {
m_queue.push(new_value);
m_cond.notify_one();
}
}
int pop(T &value) {
int ret = 0;
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_running) {
return ret;
}
if (!m_queue.empty()) {
value = m_queue.front();
m_queue.pop();
ret = 1;
}
return ret;
}
void clear() {
std::lock_guard<std::mutex> lock(m_mutex);
int size = m_queue.size();
for (int i = 0; i < size; ++i) {
T value = m_queue.front();
releaseCallback(value);
m_queue.pop();
}
}
void setRunning(bool run) {
std::lock_guard<std::mutex> lock(m_mutex);
m_running = run;
}
bool empty() {
std::lock_guard<std::mutex> lock(m_mutex);
return m_queue.empty();
}
int size() {
std::lock_guard<std::mutex> lock(m_mutex);
return static_cast<int>(m_queue.size());
}
void setReleaseCallback(ReleaseCallback callback) {
releaseCallback = callback;
}
};
#endif // PACKET_QUEUE_H