-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathRtpQueue.cpp
More file actions
185 lines (169 loc) · 3.98 KB
/
RtpQueue.cpp
File metadata and controls
185 lines (169 loc) · 3.98 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
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
#include "RtpQueue.h"
#include <iostream>
#include <string.h>
using namespace std;
/*
* Implements a simple RTP packet queue
*/
RtpQueueItem::RtpQueueItem() {
used = false;
size = 0;
seqNr = 0;
}
RtpQueue::RtpQueue() {
for (int n=0; n < kRtpQueueSize; n++) {
items[n] = new RtpQueueItem();
}
head = -1;
tail = 0;
nItems = 0;
sizeOfLastFrame = 0;
bytesInQueue_ = 0;
sizeOfQueue_ = 0;
sizeOfNextRtp_ = -1;
}
bool RtpQueue::push(void *rtpPacket, int size, unsigned short seqNr, bool isMark, float ts) {
int ix = head+1;
if (ix == kRtpQueueSize) ix = 0;
if (items[ix]->used) {
/*
* RTP queue is full, do a drop tail i.e ignore new RTP packets
*/
return (false);
}
head = ix;
items[head]->seqNr = seqNr;
items[head]->size = size;
items[head]->ts = ts;
items[head]->isMark = isMark;
items[head]->used = true;
bytesInQueue_ += size;
sizeOfQueue_ += 1;
#ifndef IGNORE_PACKET
items[head]->packet = rtpPacket;
#endif
computeSizeOfNextRtp();
return (true);
}
bool RtpQueue::pop(void **rtpPacket, int& size, unsigned short& seqNr, bool &isMark)
{
if (items[tail]->used == false) {
*rtpPacket = NULL;
sizeOfNextRtp_ = -1;
return false;
} else {
size = items[tail]->size;
#ifndef IGNORE_PACKET
*rtpPacket = items[tail]->packet;
#endif
seqNr = items[tail]->seqNr;
isMark = items[tail]->isMark;
items[tail]->used = false;
bytesInQueue_ -= size;
sizeOfQueue_ -= 1;
tail++; if (tail == kRtpQueueSize) tail = 0;
computeSizeOfNextRtp();
return true;
}
}
#ifdef GSCREAM
void RtpQueue::push(GstBuffer *buffer, int size, unsigned short seqNr, float ts) {
int ix = head+1;
if (ix == kRtpQueueSize) ix = 0;
if (items[ix]->used) {
/*
* RTP queue is full, do a drop tail i.e ignore new RTP packets
*/
return;
}
head = ix;
items[head]->seqNr = seqNr;
items[head]->size = size;
items[head]->ts = ts;
items[head]->used = true;
items[head]->buffer = buffer;
bytesInQueue_ += size;
sizeOfQueue_ += 1;
computeSizeOfNextRtp();
}
GstBuffer* RtpQueue::pop(unsigned short& seqNr) {
if (items[tail]->used == false) {
sizeOfNextRtp_ = -1;
return 0;
} else {
GstBuffer *buffer = items[tail]->buffer;
seqNr = items[tail]->seqNr;
items[tail]->used = false;
bytesInQueue_ -= items[tail]->size;
sizeOfQueue_ -= 1;
tail++; if (tail == kRtpQueueSize) tail = 0;
computeSizeOfNextRtp();
return buffer;
}
}
#endif
void RtpQueue::computeSizeOfNextRtp() {
if (!items[tail]->used) {
sizeOfNextRtp_ = - 1;
} else {
sizeOfNextRtp_ = items[tail]->size;
}
}
int RtpQueue::sizeOfNextRtp() {
return sizeOfNextRtp_;
}
int RtpQueue::seqNrOfNextRtp() {
if (!items[tail]->used) {
return -1;
} else {
return items[tail]->seqNr;
}
}
int RtpQueue::seqNrOfLastRtp() {
if (!items[head]->used) {
return -1;
} else {
return items[head]->seqNr;
}
}
int RtpQueue::bytesInQueue() {
return bytesInQueue_;
}
int RtpQueue::sizeOfQueue() {
return sizeOfQueue_;
}
float RtpQueue::getDelay(float currTs) {
if (items[tail]->used == false) {
return 0;
} else {
return currTs-items[tail]->ts;
}
}
bool RtpQueue::sendPacket(void **rtpPacket, int& size, unsigned short& seqNr) {
if (sizeOfQueue() > 0) {
bool isMark;
pop(rtpPacket, size, seqNr, isMark);
return true;
}
return false;
}
#ifndef IGNORE_PACKET
extern void packet_free(void *buf);
#endif
int RtpQueue::clear() {
uint16_t seqNr;
int freed = 0;
int size;
void *buf;
while (sizeOfQueue() > 0) {
bool isMark;
pop(&buf, size, seqNr, isMark);
if (buf != NULL) {
freed++;
}
#ifndef IGNORE_PACKET
packet_free(buf);
#endif
}
return (freed);
}