OPAL Version 3.10.2
jitter.h
Go to the documentation of this file.
00001 /*
00002  * jitter.h
00003  *
00004  * Jitter buffer support
00005  *
00006  * Open H323 Library
00007  *
00008  * Copyright (c) 1999-2001 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Open H323 Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions of this code were written with the assisance of funding from
00025  * Vovida Networks, Inc. http://www.vovida.com.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 25990 $
00030  * $Author: rjongbloed $
00031  * $Date: 2011-06-08 22:17:09 -0500 (Wed, 08 Jun 2011) $
00032  */
00033 
00034 #ifndef OPAL_RTP_JITTER_H
00035 #define OPAL_RTP_JITTER_H
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 #include <opal/buildopts.h>
00042 
00043 #include <rtp/rtp.h>
00044 
00045 
00046 class RTP_JitterBuffer;
00047 class RTP_JitterBufferAnalyser;
00048 
00049 
00051 
00055 class OpalJitterBuffer : public PSafeObject
00056 {
00057   PCLASSINFO(OpalJitterBuffer, PSafeObject);
00058 
00059   public:
00065     OpalJitterBuffer(
00066       unsigned minJitterDelay, 
00067       unsigned maxJitterDelay, 
00068       unsigned timeUnits = 8,  
00069       PINDEX packetSize = 2048 
00070     );
00071     
00074     virtual ~OpalJitterBuffer();
00076 
00080     void PrintOn(
00081       ostream & strm
00082     ) const;
00084 
00089     void SetDelay(
00090       unsigned minJitterDelay, 
00091       unsigned maxJitterDelay, 
00092       PINDEX packetSize = 2048 
00093     );
00094 
00097     void Reset();
00098 
00101     virtual PBoolean WriteData(
00102       const RTP_DataFrame & frame,   
00103       const PTimeInterval & tick = 0 
00104     );
00105 
00110     virtual PBoolean ReadData(
00111       RTP_DataFrame & frame,  
00112       const PTimeInterval & tick = 0 
00113     );
00114 
00117     DWORD GetCurrentJitterDelay() const { return m_currentJitterDelay; }
00118     
00121     DWORD GetMinJitterDelay() const { return m_minJitterDelay; }
00122     
00125     DWORD GetMaxJitterDelay() const { return m_maxJitterDelay; }
00126 
00129     unsigned GetTimeUnits() const { return m_timeUnits; }
00130     
00133     DWORD GetPacketsTooLate() const { return m_packetsTooLate; }
00134 
00137     DWORD GetBufferOverruns() const { return m_bufferOverruns; }
00138 
00141     DWORD GetMaxConsecutiveMarkerBits() const { return m_maxConsecutiveMarkerBits; }
00142 
00145     void SetMaxConsecutiveMarkerBits(DWORD max) { m_maxConsecutiveMarkerBits = max; }
00147 
00148   protected:
00149     DWORD CalculateRequiredTimestamp(DWORD playOutTimestamp) const;
00150     bool AdjustCurrentJitterDelay(int delta);
00151 
00152     unsigned m_timeUnits;
00153     PINDEX   m_packetSize;
00154     DWORD    m_minJitterDelay;      
00155     DWORD    m_maxJitterDelay;      
00156     int      m_jitterGrowTime;      
00157     DWORD    m_jitterShrinkPeriod;  
00158 
00159     int      m_jitterShrinkTime;    
00160     DWORD    m_silenceShrinkPeriod; 
00161     int      m_silenceShrinkTime;   
00162     DWORD    m_jitterDriftPeriod;
00163 
00164     int      m_currentJitterDelay;
00165     DWORD    m_packetsTooLate;
00166     DWORD    m_bufferOverruns;
00167     DWORD    m_consecutiveMarkerBits;
00168     DWORD    m_maxConsecutiveMarkerBits;
00169     DWORD    m_consecutiveLatePackets;
00170 
00171     DWORD    m_averageFrameTime;
00172     DWORD    m_lastTimestamp;
00173     DWORD    m_bufferFilledTime;
00174     DWORD    m_bufferLowTime;
00175     DWORD    m_bufferEmptiedTime;
00176     int      m_timestampDelta;
00177 
00178     enum {
00179       e_SynchronisationStart,
00180       e_SynchronisationFill,
00181       e_SynchronisationShrink,
00182       e_SynchronisationDone
00183     } m_synchronisationState;
00184 
00185     typedef std::map<DWORD, RTP_DataFrame> FrameMap;
00186     FrameMap m_frames;
00187     PMutex   m_bufferMutex;
00188 
00189     RTP_JitterBufferAnalyser * m_analyser;
00190 };
00191 
00192 
00196 class OpalJitterBufferThread : public OpalJitterBuffer
00197 {
00198     PCLASSINFO(OpalJitterBufferThread, OpalJitterBuffer);
00199  public:
00200     OpalJitterBufferThread(
00201       unsigned minJitterDelay, 
00202       unsigned maxJitterDelay, 
00203       unsigned timeUnits = 8,  
00204       PINDEX packetSize = 2048 
00205     );
00206     ~OpalJitterBufferThread();
00207 
00214     virtual PBoolean ReadData(
00215       RTP_DataFrame & frame   
00216     );
00217 
00222     virtual PBoolean OnReadPacket(
00223       RTP_DataFrame & frame   
00224     ) = 0;
00225 
00226   protected:
00227     PDECLARE_NOTIFIER(PThread, OpalJitterBufferThread, JitterThreadMain);
00228 
00230     void StartThread();
00231 
00233     void WaitForThreadTermination();
00234 
00235     PThread * m_jitterThread;
00236     bool      m_running;
00237 };
00238 
00239 
00241 
00244 class RTP_JitterBuffer : public OpalJitterBufferThread
00245 {
00246     PCLASSINFO(RTP_JitterBuffer, OpalJitterBufferThread);
00247  public:
00248     RTP_JitterBuffer(
00249       RTP_Session & session,   
00250       unsigned minJitterDelay, 
00251       unsigned maxJitterDelay, 
00252       unsigned timeUnits = 8,  
00253       PINDEX packetSize = 2048 
00254     );
00255     ~RTP_JitterBuffer();
00256 
00261     virtual PBoolean OnReadPacket(
00262       RTP_DataFrame & frame   
00263     );
00264 
00265  protected:
00267    RTP_Session & m_session;
00268 };
00269 
00270 #endif // OPAL_RTP_JITTER_H
00271 
00272