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: 24784 $
00030  * $Author: rjongbloed $
00031  * $Date: 2010-10-08 01:15:30 -0500 (Fri, 08 Oct 2010) $
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 
00098     void Reset() { m_resetJitterBufferNow = true; }
00099 
00102     virtual PBoolean WriteData(
00103       const RTP_DataFrame & frame   
00104     );
00105 
00110     virtual PBoolean ReadData(
00111       RTP_DataFrame & frame   
00112     );
00113 
00116     DWORD GetJitterTime() const { return currentJitterTime; }
00117 
00120     unsigned GetTimeUnits() const { return timeUnits; }
00121     
00124     DWORD GetPacketsTooLate() const { return packetsTooLate; }
00125 
00128     DWORD GetBufferOverruns() const { return bufferOverruns; }
00129 
00132     DWORD GetMaxConsecutiveMarkerBits() const { return maxConsecutiveMarkerBits; }
00133 
00136     void SetMaxConsecutiveMarkerBits(DWORD max) { maxConsecutiveMarkerBits = max; }
00138 
00139   protected:
00140     class Entry : public RTP_DataFrame
00141     {
00142       public:
00143         Entry(PINDEX sz) : RTP_DataFrame(0, sz) { }
00144         PTimeInterval tick;
00145     };
00146     OpalJitterBuffer::Entry * GetAvailableEntry();
00147     void InternalWriteData(OpalJitterBuffer::Entry * availableEntry);
00148 
00149     DWORD         minJitterTime;
00150     DWORD         maxJitterTime;
00151     unsigned      timeUnits;
00152     PINDEX        bufferSize;
00153     DWORD         maxConsecutiveMarkerBits;
00154 
00155     DWORD         currentJitterTime;
00156     DWORD         packetsTooLate;
00157     unsigned      bufferOverruns;
00158     unsigned      consecutiveBufferOverruns;
00159     DWORD         consecutiveMarkerBits;
00160     bool          markerWarning;
00161     PTimeInterval consecutiveEarlyPacketStartTime;
00162     DWORD         lastWriteTimestamp;
00163     PTimeInterval lastWriteTick;
00164     DWORD         jitterCalc;
00165     DWORD         targetJitterTime;
00166     unsigned      jitterCalcPacketCount;
00167     bool          m_resetJitterBufferNow;
00168 
00169     struct FrameQueue : public PList<Entry>
00170     {
00171       FrameQueue() { DisallowDeleteObjects(); }
00172       ~FrameQueue() { AllowDeleteObjects(); }
00173     };
00174     FrameQueue freeFrames;
00175     FrameQueue jitterBuffer;
00176     Entry * GetNewest(bool pop);
00177     Entry * GetOldest(bool pop);
00178 
00179     Entry * currentFrame;    // storage of current frame
00180 
00181     PMutex bufferMutex;
00182     bool   preBuffering;
00183     bool   firstReadData;
00184 
00185     RTP_JitterBufferAnalyser * analyser;
00186 };
00187 
00188 
00192 class OpalJitterBufferThread : public OpalJitterBuffer
00193 {
00194     PCLASSINFO(OpalJitterBufferThread, OpalJitterBuffer);
00195  public:
00196     OpalJitterBufferThread(
00197       unsigned minJitterDelay, 
00198       unsigned maxJitterDelay, 
00199       unsigned timeUnits = 8,  
00200       PINDEX packetSize = 2048 
00201     );
00202     ~OpalJitterBufferThread();
00203 
00210     virtual PBoolean ReadData(
00211       RTP_DataFrame & frame   
00212     );
00213 
00218     virtual PBoolean OnReadPacket(
00219       RTP_DataFrame & frame   
00220     ) = 0;
00221 
00222   protected:
00223     PDECLARE_NOTIFIER(PThread, OpalJitterBufferThread, JitterThreadMain);
00224 
00226     void StartThread();
00227 
00229     void WaitForThreadTermination();
00230 
00231     PThread * m_jitterThread;
00232     bool      m_running;
00233 };
00234 
00235 
00237 
00240 class RTP_JitterBuffer : public OpalJitterBufferThread
00241 {
00242     PCLASSINFO(RTP_JitterBuffer, OpalJitterBufferThread);
00243  public:
00244     RTP_JitterBuffer(
00245       RTP_Session & session,   
00246       unsigned minJitterDelay, 
00247       unsigned maxJitterDelay, 
00248       unsigned timeUnits = 8,  
00249       PINDEX packetSize = 2048 
00250     );
00251     ~RTP_JitterBuffer();
00252 
00257     virtual PBoolean OnReadPacket(
00258       RTP_DataFrame & frame   
00259     );
00260 
00261  protected:
00263    RTP_Session & session;
00264 };
00265 
00266 #endif // OPAL_RTP_JITTER_H
00267 
00268 

Generated on Sun Nov 21 20:20:50 2010 for OPAL by  doxygen 1.4.7