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: 27264 $
00030  * $Author: rjongbloed $
00031  * $Date: 2012-03-21 19:25:50 -0500 (Wed, 21 Mar 2012) $
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_lastSyncSource;
00174     DWORD    m_bufferFilledTime;
00175     DWORD    m_bufferLowTime;
00176     DWORD    m_bufferEmptiedTime;
00177     int      m_timestampDelta;
00178 
00179     enum {
00180       e_SynchronisationStart,
00181       e_SynchronisationFill,
00182       e_SynchronisationShrink,
00183       e_SynchronisationDone
00184     } m_synchronisationState;
00185 
00186     typedef std::map<DWORD, RTP_DataFrame> FrameMap;
00187     FrameMap m_frames;
00188     PMutex   m_bufferMutex;
00189 
00190     RTP_JitterBufferAnalyser * m_analyser;
00191 };
00192 
00193 
00197 class OpalJitterBufferThread : public OpalJitterBuffer
00198 {
00199     PCLASSINFO(OpalJitterBufferThread, OpalJitterBuffer);
00200  public:
00201     OpalJitterBufferThread(
00202       unsigned minJitterDelay, 
00203       unsigned maxJitterDelay, 
00204       unsigned timeUnits = 8,  
00205       PINDEX packetSize = 2048 
00206     );
00207     ~OpalJitterBufferThread();
00208 
00215     virtual PBoolean ReadData(
00216       RTP_DataFrame & frame   
00217     );
00218 
00223     virtual PBoolean OnReadPacket(
00224       RTP_DataFrame & frame   
00225     ) = 0;
00226 
00227   protected:
00228     PDECLARE_NOTIFIER(PThread, OpalJitterBufferThread, JitterThreadMain);
00229 
00231     void StartThread();
00232 
00234     void WaitForThreadTermination();
00235 
00236     PThread * m_jitterThread;
00237     bool      m_running;
00238 };
00239 
00240 
00242 
00245 class RTP_JitterBuffer : public OpalJitterBufferThread
00246 {
00247     PCLASSINFO(RTP_JitterBuffer, OpalJitterBufferThread);
00248  public:
00249     RTP_JitterBuffer(
00250       RTP_Session & session,   
00251       unsigned minJitterDelay, 
00252       unsigned maxJitterDelay, 
00253       unsigned timeUnits = 8,  
00254       PINDEX packetSize = 2048 
00255     );
00256     ~RTP_JitterBuffer();
00257 
00262     virtual PBoolean OnReadPacket(
00263       RTP_DataFrame & frame   
00264     );
00265 
00266  protected:
00268    RTP_Session & m_session;
00269 };
00270 
00271 #endif // OPAL_RTP_JITTER_H
00272 
00273 

Generated on 5 Jun 2012 for OPAL by  doxygen 1.6.1