Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have developed a software that sends media using the RTP protocol using C++, the packets is sent every 20ms, and the files are read from discs. My problem is timing the send inteval, I have not found a good way to wait exactly 20 ms without overusing CPU resources.
We all now that Sleep(20) could return any time after 20ms and can not be used. The way I do it now is by polling a timer, and that works perfectly, but a hate the solution cause it is using to much cpu and it is ugly.
Does anyone has a suggestion?

Thanks in advance.
Posted
Updated 19-Apr-12 4:11am
v2
Comments
Resmi Anna 19-Apr-12 7:55am    
You can use the function WaiteForSingleObject(), if you can fire some event from the place where you finish sending the media
Mikael Segerstein 19-Apr-12 8:53am    
Thanks for your suggesion. It will still not work, sending the next packet to soon will flood the receiving buffer on the receiving part, I need a short delay. The receiving part is usually a softphone or asterix switch.

You may use a WaitableTimer. See CreateWaitableTimer()[^] at MSDN and the example link [^].

If this is still too inaccurate you may tweak the timer resolution using the undocumented function NtSetTimerResolution() (use GetProcAddress() to get the function address from the Ntdll). See http://www.decuslib.com/decus/vmslt97a/ntstuff/timer.txt[^].

See also the Timers Tutorial[^].
 
Share this answer
 
Comments
Mikael Segerstein 19-Apr-12 10:29am    
Thanks for the links, I'll have a look at them.
Thank you for your effort.
Mikael Segerstein 19-Apr-12 17:52pm    
The solution was also found in one of the links you sent so thank you for pointing them out.
You should take a look at Windows Multimedia Timers, thus the function in the group:

timeSetEvent
timeKillEvent

timeGetDevCaps


I personally haven't worked with them, but MS promises that they are way better for the precise timing required in multi-media applications.
 
Share this answer
 
Comments
Mikael Segerstein 19-Apr-12 17:49pm    
I have been doing some test with your suggestion and I belive I can make this work. It is not totally accurate but it will be a big improvment.
Thanks.
Windows is not a "real time operating system". There are no guarantees on timing of any event.

Even if you sat in a loop reading the cycle counter to determine exactly when 20ms has passed, your process may still be preempted by some obscure Windows process that may take *SECONDS* to complete and return to you.

You can't get that accurate with Windows.

Now granted that you did not specify the operating system and "sleep()" does exist on many other operating systems (although you did seem to evaluate a solution based on WaitForSingleObject(), a Windows call). But most generat purpose operating systems still do not provide a complete "real time kernel" effect that would give you the precise timing you are looking for.
 
Share this answer
 
Comments
Mikael Segerstein 19-Apr-12 10:19am    
Your assumption that it is Windows is correct, I have updated the tags, thanks for pointing it out. The solution I have now is working surprisingly well but since it it using way to much cpu, I can not have as many simultaneous thread as I would like.
JackDingler 19-Apr-12 11:47am    
Are you putting any Sleep calls in between the timer calls?

If Sleep(20) is too inaccurate, how about Sleep(5)?
Mikael Segerstein 19-Apr-12 17:43pm    
No, I don't. If I do, the media transfer is slowed down.
Can't use Sleep(), not even with lower values like you suggest. It can still wake up after 50 ms or so.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900