- Home /
While loop stops
I use of the shelf code to display camera feed from a server (series of images). But after some time (sometimes few seconds, sometimes few minutes), camera feed freezes, and last two logs are isDone (True) and number i. The rest of the program (kinect tracking and oculus integration) works normally like nothing happened. Feed (server) still works since I can access it in a browser. Any idea why this happens?
#pragma strict
// Continuously get the latest webcam shot from the url provided (ffserver on the local network)
// and DXT compress them at runtime
var url = "http://192.168.1.50:8090/test.jpg";
var i : int = 0;
function Start () {
Debug.Log(i);
// Create a texture in DXT1 format
GetComponent.<Renderer>().material.mainTexture = new Texture2D(4, 4, TextureFormat.RGB24, false);
while(true) {
// Start a download of the given URL
var www = new WWW(url);
// wait until the download is done
yield www;
Debug.Log(www.isDone);
// assign the downloaded image to the main texture of the object
www.LoadImageIntoTexture(GetComponent.<Renderer>().material.mainTexture);
Debug.Log(i);
i++;
}
}
Honestly, I'm not quite sure what going on with your code. It LOO$$anonymous$$S like the new WWW(url)
command starts the download. If this is the case, I would think you want to call this BEFORE you start looping to wait for the download to finish, rather than inside the loop.
It's also not quite clear to me, when you "break;" or exit the while(true) loop. I'm not even sure why there is a loop at all, nothing except "i" (and your debuglog) seems to change with each iteration. I guess you DO create a new WWW each iteration, but I don't see why.
Server in question is ffserver on a machine local network. On it, there is just one file (image test.jpg) which is updated 30 times per second. This code should get the image from server and display it as a texture on a game object, and update it in every iteration of a while loop. So WWW(url) starts downloading, yield waits for download to finish LoadImageIntoTexture displays that image as a texture on an object in game. And it works, I see live camera feed on that object, but for some reason it stops (freezes) after some time.
Ah, I see now. Hmm, is it possible it's never co$$anonymous$$g back from the yield for some reason? One more debug.log between new WWW, and the yield statement, should confirm/eli$$anonymous$$ate that. Currently the Debug.Log(i) is the last item in your log, right?
oh, darn. sorry. Well, if you remove the Thread.Yield line the loop will be infinite, and wont release control back to unity and/or the OS. However, it WILL continue to output debug logs. Perhaps if you just remove the yield line, we will be able to get enough info out of the debug logs to deter$$anonymous$$e the problem.
Possible workaround: not sure if this would work for your situation: What if you put this class inside a $$anonymous$$onobehavior, and only downloaded ONE file each time UPDATE() is called? This way you no longer have any infinite loops you need to worry bout hogging the processor. Checking the time during your loop, and returning when a time limit is reached is also a good idea. $$anonymous$$ight want to take a look at http://docs.unity3d.com/ScriptReference/Time-realtimeSinceStartup.html for that.
The worst thing is that everything worked few days ago and I didn't change anything.
That is the worst! But it's also a clue your code may need to be more robust as far as error handling goes. If something external changes, code that used to work just fine, could be effected.
regarding error handling: I also just noticed this function in WWW (http://docs.unity3d.com/ScriptReference/WWW-responseHeaders.html) Perhaps these response headers will state if the stream is invalid, so you can delay and then try again? Really not sure about that tho. Displaying them all, each download, and seeing if the headers are any different when it fails; this is where I would start.
That's exactly what I did at the end. Put the GetUrl function in Update so it's being called every frame again. It works now, doesn't freeze, but in the log I get a lot of "recv failure: connection was reset" error messages, which means that there are some dropped/failed connections to the server. This solution will work for now, and I'll try to figure out that connection along the way. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220611235344im_/https://answers.unity.com/themes/thub/images/avi.jpg)