📄 jitter.cxx
字号:
/*
* jitter.cxx
*
* Jitter buffer support
*
* Open H323 Library
*
* Copyright (c) 1998-2000 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Open H323 Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions of this code were written with the assisance of funding from
* Vovida Networks, Inc. http://www.vovida.com.
*
* Contributor(s): ______________________________________.
*
* $Log: jitter.cxx,v $
* Revision 1.1 2006/06/26 03:03:54 joegenbaclor
* I have decided to include the latest development realease of OPAL tagged Deimos Devel 1 (June 8 2006) as inegrated classes to opensipstack to avoid future version conflicts due to the fast pace in OPAL development. This move is also aimed to reduce the size of projects using OPAL componets such as the soon to be relased OpenSIPPhone.
*
* Revision 2.11 2005/12/30 15:22:40 dsandras
* Synchronized with MIMAS OpenH323 version.
*
* Revision 2.10 2005/12/30 14:29:15 dsandras
* Removed the assumption that the jitter will contain a 8 kHz signal.
*
* Revision 2.9 2005/09/22 18:17:42 dsandras
* Fixed problems with jitter being restarted.
*
* Revision 2.8 2005/08/04 17:18:37 dsandras
* Reinitialize frame pointers when the jitter is restarted.
*
* Revision 2.7 2004/02/19 10:47:06 rjongbloed
* Merged OpenH323 version 1.13.1 changes.
*
* Revision 2.6 2003/01/07 04:39:53 robertj
* Updated to OpenH323 v1.11.2
*
* Revision 2.5 2002/11/10 11:33:20 robertj
* Updated to OpenH323 v1.10.3
*
* Revision 2.4 2002/09/04 06:01:49 robertj
* Updated to OpenH323 v1.9.6
*
* Revision 2.3 2002/04/15 08:52:20 robertj
* Added buffer reset on excess buffer overruns.
*
* Revision 2.2 2002/01/22 05:21:10 robertj
* Update from OpenH323, rev 1.27
*
* Revision 2.1 2001/10/05 00:22:14 robertj
* Updated to PWLib 1.2.0 and OpenH323 1.7.0
*
* Revision 2.0 2001/07/27 15:48:25 robertj
* Conversion of OpenH323 to Open Phone Abstraction Library (OPAL)
*
* Revision 1.35 2003/11/08 02:03:46 rjongbloed
* Removed warning on no logging build.
*
* Revision 1.34 2003/10/28 22:38:31 dereksmithies
* Rework of jitter buffer. Many thanks to Henry Harrison of Alice Street.
*
* Revision 1.33ACC1.0 6th October 2003 henryh
* Complete change to adaptive algorithm
*
* Revision 1.33 2002/11/26 03:00:06 robertj
* Added logging to help find logical channel thread stop failures.
*
* Revision 1.32 2002/11/05 04:06:48 robertj
* Better tracing
*
* Revision 1.31 2002/10/31 00:46:30 robertj
* Enhanced jitter buffer system so operates dynamically between minimum and
* maximum values. Altered API to assure app writers note the change!
*
* Revision 1.30 2002/10/30 05:56:12 craigs
* Added more bulletproofing thanks to Alex Kovatch
*
* Revision 1.29 2002/09/03 07:31:35 robertj
* Added buffer reset on excess buffer overruns.
*
* Revision 1.28 2002/08/05 10:03:47 robertj
* Cosmetic changes to normalise the usage of pragma interface/implementation.
*
* Revision 1.27 2002/01/17 07:01:28 robertj
* Fixed jitter buffer failing to deliver a talk burst shorted than the size of
* the buffer, this is particularly noticable with RFC2833.
*
* Revision 1.26 2001/09/11 00:21:23 robertj
* Fixed missing stack sizes in endpoint for cleaner thread and jitter thread.
*
* Revision 1.25 2001/09/10 08:18:11 robertj
* Added define to remove jitter buffer analyser, thanks Nick Hoath.
*
* Revision 1.24 2001/07/05 05:55:17 robertj
* Added thread name.
*
* Revision 1.23 2001/04/04 03:13:31 robertj
* Added PTRACE for when have packets too late.
*
* Revision 1.22 2001/02/09 05:13:56 craigs
* Added pragma implementation to (hopefully) reduce the executable image size
* under Linux
*
* Revision 1.21 2000/12/17 22:45:36 robertj
* Set media stream threads to highest unprivileged priority.
*
* Revision 1.20 2000/09/14 23:03:45 robertj
* Increased timeout on asserting because of driver lockup
*
* Revision 1.19 2000/09/05 22:21:02 robertj
* Fixed bug in jitter buffer list changes if get out of order packet within jitter time.
*
* Revision 1.18 2000/08/25 01:10:28 robertj
* Added assert if various thrads ever fail to terminate.
*
* Revision 1.17 2000/05/30 06:53:04 robertj
* Fixed bug where jitter buffer needs to be restarted, eg Cisco double use of session.
*
* Revision 1.16 2000/05/25 02:26:12 robertj
* Added ignore of marker bits on broken clients that sets it on every RTP packet.
*
* Revision 1.15 2000/05/25 00:35:37 robertj
* Fixed rare crashing bug in jitter buffer caused by our of order packet.
*
* Revision 1.14 2000/05/16 07:37:41 robertj
* Initialised preBuffering flag just in case sender does not set RTP marker bit.
*
* Revision 1.13 2000/05/08 14:05:58 robertj
* Increased log level of big jitter debug output to level 5.
*
* Revision 1.12 2000/05/05 02:54:15 robertj
* Fixed memory leak just introduced in jitter buffer.
*
* Revision 1.11 2000/05/04 11:52:35 robertj
* Added Packets Too Late statistics, requiring major rearrangement of jitter
* buffer code, not also changes semantics of codec Write() function slightly.
*
* Revision 1.10 2000/05/02 04:32:27 robertj
* Fixed copyright notice comment.
*
* Revision 1.9 2000/05/01 09:11:04 robertj
* Fixed removal of analysis code on No Trace version.
*
* Revision 1.8 2000/05/01 06:04:33 robertj
* More jitter buffer debugging.
*
* Revision 1.7 2000/04/10 18:55:46 robertj
* Changed RTP data receive tp be more forgiving, will process packet even if payload type is wrong.
*
* Revision 1.6 2000/03/31 20:10:43 robertj
* Fixed problem with insufficient jitter buffer frames being allocated.
* Fixed "center in frame" change in previous version.
*
* Revision 1.5 2000/03/23 03:08:52 robertj
* Changed jitter buffer so asumes output double buffering and centers output in time frames.
*
* Revision 1.4 2000/03/21 03:06:50 robertj
* Changes to make RTP TX of exact numbers of frames in some codecs.
*
* Revision 1.3 2000/03/20 20:51:47 robertj
* Fixed possible buffer overrun problem in RTP_DataFrames
*
* Revision 1.2 1999/12/29 01:18:07 craigs
* Fixed problem with codecs other than G.711 not working after reorganisation
*
* Revision 1.1 1999/12/23 23:02:36 robertj
* File reorganision for separating RTP from H.323 and creation of LID for VPB support.
*
*/
#include <ptlib.h>
#ifdef __GNUC__
#pragma implementation "jitter.h"
#endif
#include <rtp/jitter.h>
/*Number of consecutive attempts to add a packet to the jitter buffer while
it is full before the system clears the jitter buffer and starts over
again. */
#define MAX_BUFFER_OVERRUNS 20
/**How much time must elapse with lower jitter before jitter
buffer size is reduced */
#define DECREASE_JITTER_PERIOD 5000 // milliseconds
/* Percentage of current jitter buffer size that constitutes a "genuinely" smaller
jitter */
#define LOWER_JITTER_MAX_PCNT 80
/* Minimum number of packets that constitute a reliable sample for setting a lower
jitter buffer target */
#define DECREASE_JITTER_MIN_PACKETS 50
#if PTRACING && !defined(NO_ANALYSER)
class RTP_JitterBufferAnalyser : public PObject
{
PCLASSINFO(RTP_JitterBufferAnalyser, PObject);
public:
RTP_JitterBufferAnalyser();
void In(DWORD time, unsigned depth, const char * extra);
void Out(DWORD time, unsigned depth, const char * extra);
void PrintOn(ostream & strm) const;
struct Info {
Info() { }
DWORD time;
PTimeInterval tick;
int depth;
const char * extra;
} in[1000], out[1000];
PINDEX inPos, outPos;
};
#endif
#define new PNEW
/////////////////////////////////////////////////////////////////////////////
RTP_JitterBuffer::RTP_JitterBuffer(RTP_Session & sess,
unsigned minJitterDelay,
unsigned maxJitterDelay,
unsigned time,
PINDEX stackSize)
: PThread(stackSize, NoAutoDeleteThread, HighestPriority, "RTP Jitter:%x"),
session(sess)
{
// Jitter buffer is a queue of frames waiting for playback, a list of
// free frames, and a couple of place holders for the frame that is
// currently beeing read from the RTP transport or written to the codec.
oldestFrame = newestFrame = currentWriteFrame = NULL;
// Calculate the maximum amount of timestamp units for the jitter buffer
minJitterTime = minJitterDelay;
maxJitterTime = maxJitterDelay;
currentJitterTime = minJitterDelay;
targetJitterTime = currentJitterTime;
timeUnits = time;
// Calculate number of frames to allocate, we make the assumption that the
// smallest packet we can possibly get is 5ms long.
bufferSize = maxJitterTime/(5*timeUnits)+1;
// Nothing in the buffer so far
currentDepth = 0;
packetsTooLate = 0;
bufferOverruns = 0;
consecutiveBufferOverruns = 0;
maxConsecutiveMarkerBits = 10;
consecutiveMarkerBits = 0;
consecutiveEarlyPacketStartTime = 0;
doJitterReductionImmediately = FALSE;
doneFreeTrash = FALSE;
lastWriteTimestamp = 0;
lastWriteTick = 0;
jitterCalc = 0;
jitterCalcPacketCount = 0;
shuttingDown = FALSE;
preBuffering = TRUE;
doneFirstWrite = FALSE;
// Allocate the frames and put them all into the free list
freeFrames = new Entry;
freeFrames->next = freeFrames->prev = NULL;
for (PINDEX i = 0; i < bufferSize; i++) {
Entry * frame = new Entry;
frame->prev = NULL;
frame->next = freeFrames;
freeFrames->prev = frame;
freeFrames = frame;
}
PTRACE(2, "RTP\tJitter buffer created:"
" size=" << bufferSize <<
" delay=" << minJitterTime << '-' << maxJitterTime << '/' << currentJitterTime <<
" (" << (currentJitterTime/timeUnits) << "ms)"
" obj=" << this);
#if PTRACING && !defined(NO_ANALYSER)
analyser = new RTP_JitterBufferAnalyser;
#else
analyser = NULL;
#endif
// Start reading data from RTP session
Resume();
}
RTP_JitterBuffer::~RTP_JitterBuffer()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -