I’m using a php script to serve files to JWPlayer in order to prevent leeches from getting the URL and using it in their iPhone and Roku apps. So I call videoplay.php?id=myfile . The output is a mp4 video stream. Files are typically 200-500MBytes.
The php script works correctly and with some tweaking it even performs much better than the initial version I had. But it is a resource hog. When I am streaming one file, I can’t open another window in my browser to any page on the same website…I can’t even display source of the current page. If I play exactly the same video files by giving the url directly to the player I have no resource hog problems and I can view source of the page while the video is playing, and play two videos from the same website simultaneously in the same browser (on two different pages).
Here is my code
// some code to query database and get the file url
//
$fh = fopen($filename, "rb") or die("Could not open movie\n");
while (!feof($fh) && connection_status() == 0)
{
print(fread($fh, 1024*8));
flush();
ob_flush();
usleep(10000);
}
fclose($fh);
I added the usleep to try to limit resource usage and it didn’t work, but videos seem to load faster. When I make usleep much longer (e.g. 1/10 sec) videos take much longer to load. If i vary the chunk size between 1024*4 and 1024*16 the 8kbyte seems to result in fastest load times for videos. 16kbyte uses even more resources and seems to max out my 25MBit connection. Videos won’t load at all from my local server (but remote videos on our cloud server do load). Reducing the chunk size to 1kbyte didn’t solve the resource problem.
What am I doing wrong?
It’s probably all because of you’re missing out the use of
session_write_close(). Add the function call to the start of the streaming. Session lock may be the reason why your scripts because the scripts are waiting for each other to finish before they continue.