xbmc/xbmc — pull request #5681 by popcornmix (2014-11-09 14:09:51+00:00)
Title: [curl] Avoid busy spinning when socket timeout is too low

This is an RFC as I don't know this code well, but my investigation may provoke a better fix.
See: http://forum.kodi.tv/showthread.php?tid=201797&pid=1771808#pid1771808

Summary: when streaming audio through http the cpu usage starts low and creeps higher and higher.

For first minute, the CPU usage of FileCache thread is a few percent.
As time goes by the cpu usage for this thread increases until it hits about 60% and we start stuttering.

Disabling the filecache (buffermode=3) avoids the high cpu issue.

I've tracked down where the cpu is occurring.
CCurlFile::CReadState::FillBuffer is called with want=1.
After a while, g_curlInterface.multi_timeout returns smaller and smaller timeouts,
until it it just returning 0 and 1 for timeout. This results in busy spinning in this function.
The small timeouts mean small amounts of data are read and so FillBuffer gets called more often.

My fix just ensures the timeout is never less than 200ms.
Now stream plays for a long time without stuttering and FileCache thread remains at about 1% CPU.