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
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
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
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.
47 #pragma implementation "delaychan.h"
51 #include <ptclib/delaychan.h>
53 /////////////////////////////////////////////////////////
55 PAdaptiveDelay::PAdaptiveDelay()
60 void PAdaptiveDelay::Restart()
65 BOOL
PAdaptiveDelay::Delay(int frameTime
)
69 targetTime
= PTime(); // targetTime is the time we want to delay to
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();
81 #if defined(P_LINUX) || defined(P_MACOSX)
82 usleep(sleep_time
* 1000);
84 PThread::Current()->Sleep(sleep_time
);
87 return sleep_time
<= -frameTime
;
90 /////////////////////////////////////////////////////////
92 PDelayChannel::PDelayChannel(Mode m
,
101 maximumSlip
= -PTimeInterval(max
);
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();
129 PTimeInterval delay
= nextTick
- thisTick
;
130 if (delay
> maximumSlip
)
131 PTRACE(6, "Delay\t" << delay
);
133 PTRACE(6, "Delay\t" << delay
<< " ignored, too large");
139 nextTick
+= count
*frameDelay
/frameSize
;
141 nextTick
+= frameDelay
;
143 if (delay
> minimumDelay
)
144 PThread::Current()->Sleep(delay
);
148 // End of File ///////////////////////////////////////////////////////////////