Blame view

3rdparty/jrtplib-3.11.2/src/rtpinternalsourcedata.cpp 7.46 KB
3d2ab595   Hu Chunming   支持gb28181
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  /*
  
    This file is a part of JRTPLIB
    Copyright (c) 1999-2017 Jori Liesenborgs
  
    Contact: jori.liesenborgs@gmail.com
  
    This library was developed at the Expertise Centre for Digital Media
    (http://www.edm.uhasselt.be), a research center of the Hasselt University
    (http://www.uhasselt.be). The library is based upon work done for 
    my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
  
    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:
  
    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.
  
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
  
  */
  
  #include "rtpinternalsourcedata.h"
  #include "rtppacket.h"
  #include <string.h>
  
  #include "rtpdebug.h"
  
  #define RTPINTERNALSOURCEDATA_MAXPROBATIONPACKETS		32
  
  namespace jrtplib
  {
  
  RTPInternalSourceData::RTPInternalSourceData(uint32_t ssrc,RTPSources::ProbationType probtype,RTPMemoryManager *mgr):RTPSourceData(ssrc,mgr)
  {
  	JRTPLIB_UNUSED(probtype); // possibly unused
  #ifdef RTP_SUPPORT_PROBATION
  	probationtype = probtype;
  #endif // RTP_SUPPORT_PROBATION
  }
  
  RTPInternalSourceData::~RTPInternalSourceData()
  {
  }
  
  // The following function should delete rtppack if necessary
  int RTPInternalSourceData::ProcessRTPPacket(RTPPacket *rtppack,const RTPTime &receivetime,bool *stored,RTPSources *sources)
  {
  	bool accept,onprobation,applyprobation;
  	double tsunit;
  	
  	*stored = false;
  	
  	if (timestampunit < 0) 
  		tsunit = INF_GetEstimatedTimestampUnit();
  	else
  		tsunit = timestampunit;
  
  #ifdef RTP_SUPPORT_PROBATION
  	if (validated) 				// If the source is our own process, we can already be validated. No 
  		applyprobation = false;		// probation should be applied in that case.
  	else
  	{
  		if (probationtype == RTPSources::NoProbation)
  			applyprobation = false;
  		else
  			applyprobation = true;
  	}
  #else
  	applyprobation = false;
  #endif // RTP_SUPPORT_PROBATION
  
  	stats.ProcessPacket(rtppack,receivetime,tsunit,ownssrc,&accept,applyprobation,&onprobation);
  
  #ifdef RTP_SUPPORT_PROBATION
  	switch (probationtype)
  	{
  		case RTPSources::ProbationStore:
  			if (!(onprobation || accept))
  				return 0;
  			if (accept)
  				validated = true;
  			break;
  		case RTPSources::ProbationDiscard:
  		case RTPSources::NoProbation:
  			if (!accept)
  				return 0;
  			validated = true;
  			break;
  		default:
  			return ERR_RTP_INTERNALSOURCEDATA_INVALIDPROBATIONTYPE;
  	}
  #else
  	if (!accept)
  		return 0;
  	validated = true;
  #endif // RTP_SUPPORT_PROBATION;
  	
  	if (validated && !ownssrc) // for own ssrc these variables depend on the outgoing packets, not on the incoming
  		issender = true;
  	
  	bool isonprobation = !validated;
  	bool ispackethandled = false;
  
  	sources->OnValidatedRTPPacket(this, rtppack, isonprobation, &ispackethandled);
  	if (ispackethandled) // Packet is already handled in the callback, no need to store it in the list
  	{
  		// Set 'stored' to true to avoid the packet being deallocated
  		*stored = true;
  		return 0;
  	}
  
  	// Now, we can place the packet in the queue
  	
  	if (packetlist.empty())
  	{
  		*stored = true;
  		packetlist.push_back(rtppack);
  		return 0;
  	}
  	
  	if (!validated) // still on probation
  	{
  		// Make sure that we don't buffer too much packets to avoid wasting memory
  		// on a bad source. Delete the packet in the queue with the lowest sequence
  		// number.
  		if (packetlist.size() == RTPINTERNALSOURCEDATA_MAXPROBATIONPACKETS)
  		{
  			RTPPacket *p = *(packetlist.begin());
  			packetlist.pop_front();
  			RTPDelete(p,GetMemoryManager());
  		}
  	}
  
  	// find the right position to insert the packet
  	
  	std::list<RTPPacket*>::iterator it,start;
  	bool done = false;
  	uint32_t newseqnr = rtppack->GetExtendedSequenceNumber();
  	
  	it = packetlist.end();
  	--it;
  	start = packetlist.begin();
  	
  	while (!done)
  	{
  		RTPPacket *p;
  		uint32_t seqnr;
  		
  		p = *it;
  		seqnr = p->GetExtendedSequenceNumber();
  		if (seqnr > newseqnr)
  		{
  			if (it != start)
  				--it;
  			else // we're at the start of the list
  			{
  				*stored = true;
  				done = true;
  				packetlist.push_front(rtppack);
  			}
  		}
  		else if (seqnr < newseqnr) // insert after this packet
  		{
  			++it;
  			packetlist.insert(it,rtppack);
  			done = true;
  			*stored = true;
  		}
  		else // they're equal !! Drop packet
  		{
  			done = true;
  		}
  	}
  
  	return 0;
  }
  
  int RTPInternalSourceData::ProcessSDESItem(uint8_t sdesid,const uint8_t *data,size_t itemlen,const RTPTime &receivetime,bool *cnamecollis)
  {
  	*cnamecollis = false;
  	
  	stats.SetLastMessageTime(receivetime);
  	
  	switch(sdesid)
  	{
  	case RTCP_SDES_ID_CNAME:
  		{
  			size_t curlen;
  			uint8_t *oldcname;
  			
  			// NOTE: we're going to make sure that the CNAME is only set once.
  			oldcname = SDESinf.GetCNAME(&curlen);
  			if (curlen == 0)
  			{
  				// if CNAME is set, the source is validated
  				SDESinf.SetCNAME(data,itemlen);
  				validated = true;
  			}
  			else // check if this CNAME is equal to the one that is already present
  			{
  				if (curlen != itemlen)
  					*cnamecollis = true;
  				else
  				{
  					if (memcmp(data,oldcname,itemlen) != 0)
  						*cnamecollis = true;
  				}
  			}
  		}
  		break;
  	case RTCP_SDES_ID_NAME:
  		{
  			size_t oldlen;
  
              		SDESinf.GetName(&oldlen);
  			if (oldlen == 0) // Name not set
  				return SDESinf.SetName(data,itemlen);
  		}
  		break;
  	case RTCP_SDES_ID_EMAIL:
  		{
  			size_t oldlen;
  
  			SDESinf.GetEMail(&oldlen);
  			if (oldlen == 0)
  				return SDESinf.SetEMail(data,itemlen);
  		}
  		break;
  	case RTCP_SDES_ID_PHONE:
  		return SDESinf.SetPhone(data,itemlen);
  	case RTCP_SDES_ID_LOCATION:
  		return SDESinf.SetLocation(data,itemlen);
  	case RTCP_SDES_ID_TOOL:
  		{
  			size_t oldlen;
  
  			SDESinf.GetTool(&oldlen);
  			if (oldlen == 0)
  				return SDESinf.SetTool(data,itemlen);
  		}
  		break;
  	case RTCP_SDES_ID_NOTE:
  		stats.SetLastNoteTime(receivetime);
  		return SDESinf.SetNote(data,itemlen);
  	}
  	return 0;
  }
  
  #ifdef RTP_SUPPORT_SDESPRIV
  
  int RTPInternalSourceData::ProcessPrivateSDESItem(const uint8_t *prefix,size_t prefixlen,const uint8_t *value,size_t valuelen,const RTPTime &receivetime)
  {
  	int status;
  	
  	stats.SetLastMessageTime(receivetime);
  	status = SDESinf.SetPrivateValue(prefix,prefixlen,value,valuelen);
  	if (status == ERR_RTP_SDES_MAXPRIVITEMS)
  		return 0; // don't stop processing just because the number of items is full
  	return status;
  }
  
  #endif // RTP_SUPPORT_SDESPRIV
  
  int RTPInternalSourceData::ProcessBYEPacket(const uint8_t *reason,size_t reasonlen,const RTPTime &receivetime)
  {
  	if (byereason)
  	{
  		RTPDeleteByteArray(byereason,GetMemoryManager());
  		byereason = 0;
  		byereasonlen = 0;
  	}
  
  	byetime = receivetime;
  	byereason = RTPNew(GetMemoryManager(),RTPMEM_TYPE_BUFFER_RTCPBYEREASON) uint8_t[reasonlen];
  	if (byereason == 0)
  		return ERR_RTP_OUTOFMEM;
  	memcpy(byereason,reason,reasonlen);
  	byereasonlen = reasonlen;
  	receivedbye = true;
  	stats.SetLastMessageTime(receivetime);
  	return 0;
  }
  
  } // end namespace