Added IsSupportingRTP function to simplify detecting when STUN supports RTP
[pwlib.git] / src / ptclib / delaychan.cxx
bloba44ac3cb9a665bb366e4f07e363f025c6940a491
1 /*
2 * delaychan.cxx
4 * Class for controlling the timing of data passing through it.
6 * Portable Windows Library
8 * Copyright (c) 2001 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
26 * $Log$
27 * Revision 1.5 2003/02/20 08:43:44 rogerh
28 * On Mac OS X, the thread sleep() (which uses select) is not as fine grained
29 * as usleep. So use usleep(). Tested by Shawn.
31 * Revision 1.4 2002/02/26 00:42:13 robertj
32 * Fixed MSVC warning.
34 * Revision 1.3 2002/02/25 11:05:02 rogerh
35 * New Delay code which solves the accumulated error problem. Based on ideas
36 * by Tomasz Motylewski <T.Motylewski@bfad.de>, Roger and Craig.
38 * Revision 1.2 2002/01/15 03:56:03 craigs
39 * Added PAdaptiveDelay class
41 * Revision 1.1 2001/07/10 03:07:07 robertj
42 * Added queue channel and delay channel classes to ptclib.
46 #ifdef __GNUC__
47 #pragma implementation "delaychan.h"
48 #endif
50 #include <ptlib.h>
51 #include <ptclib/delaychan.h>
53 /////////////////////////////////////////////////////////
55 PAdaptiveDelay::PAdaptiveDelay()
57 firstTime = TRUE;
60 void PAdaptiveDelay::Restart()
62 firstTime = TRUE;
65 BOOL PAdaptiveDelay::Delay(int frameTime)
67 if (firstTime) {
68 firstTime = FALSE;
69 targetTime = PTime(); // targetTime is the time we want to delay to
70 return TRUE;
73 // Set the new target
74 targetTime += frameTime;
76 // Calculate the sleep time so we delay until the target time
77 PTimeInterval delay = targetTime - PTime();
78 int sleep_time = (int)delay.GetMilliSeconds();
80 if (sleep_time > 0)
81 #if defined(P_LINUX) || defined(P_MACOSX)
82 usleep(sleep_time * 1000);
83 #else
84 PThread::Current()->Sleep(sleep_time);
85 #endif
87 return sleep_time <= -frameTime;
90 /////////////////////////////////////////////////////////
92 PDelayChannel::PDelayChannel(Mode m,
93 unsigned delay,
94 PINDEX size,
95 unsigned max,
96 unsigned min)
98 mode = m;
99 frameDelay = delay;
100 frameSize = size;
101 maximumSlip = -PTimeInterval(max);
102 minimumDelay = min;
106 BOOL PDelayChannel::Read(void * buf, PINDEX count)
108 if (mode != DelayWritesOnly)
109 Wait(count, nextReadTick);
110 return PIndirectChannel::Read(buf, count);
114 BOOL PDelayChannel::Write(const void * buf, PINDEX count)
116 if (mode != DelayReadsOnly)
117 Wait(count, nextWriteTick);
118 return PIndirectChannel::Write(buf, count);
122 void PDelayChannel::Wait(PINDEX count, PTimeInterval & nextTick)
124 PTimeInterval thisTick = PTimer::Tick();
126 if (nextTick == 0)
127 nextTick = thisTick;
129 PTimeInterval delay = nextTick - thisTick;
130 if (delay > maximumSlip)
131 PTRACE(6, "Delay\t" << delay);
132 else {
133 PTRACE(6, "Delay\t" << delay << " ignored, too large");
134 nextTick = thisTick;
135 delay = 0;
138 if (frameSize > 0)
139 nextTick += count*frameDelay/frameSize;
140 else
141 nextTick += frameDelay;
143 if (delay > minimumDelay)
144 PThread::Current()->Sleep(delay);
148 // End of File ///////////////////////////////////////////////////////////////