Unity 5 (C#) live HTML MJPG-streamer viewer?

Hello community,

I have a webpage which shows MJPG-streamer images from a Raspberry Pi webcam which I want to display inside Unity using a texture. Here’s the “/stream” page html code:

<p id="streamwrap" class="xform-p">
    <img id="streamimage" class="xform" src="http://192.168.1.2:8080/?action=stream">
</p>

I can’t use standard ‘get’ WWW.texture with this because a stream of images can’t be pointed to with a “/stream/image.jpg”. I can only access the stream via a “/stream” or “/?action=stream” URI. I have tried the code below, but no luck:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GET_Stream : MonoBehaviour {

	public string url = "http://192.168.1.2:8080/?action=stream";

	IEnumerator Start() {
		WWW www = new WWW(url);
		yield return www;
		RawImage renderer = GetComponent<RawImage>();
		renderer.texture = www.texture;
	}

	void Update () {
		Start ();
	}
}

Another alternative to WWW I know is HttpWebRequest. It may contain something I could use (I’m currently researching this), because when I get the stream grab to work, I’ll make it work over SSL (using a similar code to this).

Then I began looking for Unity HTML viewers, but the best one I found was for Unity 3.4 - Awesomium. I did try to install it on Unity 5.4 64-bit (where I got DllNotFoundException) and 32-bit (where I got EntryPointNotFoundException). (I’m surprised that App Inventor has a WebViewer, but Unity doesn’t). I’m looking for free options. Any suggestions?

Awesomium error on Unity 5 32-bit:

EntryPointNotFoundException: awe_string_create_from_utf16
AwesomiumMono.StringHelper..ctor (System.String val)
AwesomiumMono.WebCore.Start ()
AwesomiumMono.WebCore.Initialize (AwesomiumMono.WebCoreConfig config, Boolean start)
WebTexture.Start () (at Assets/WebTexture.cs:32)

Thank you.

Using an HTML renderer just for displaying some images seems to be total overkill.

You basically have two options:

  • Use a TCPClient and send the request manually and parse the response manually. There might be a way with “HttpWebRequest” but it’s most likely more complicated.
  • Don’t use the stream at all. The mjpg streamer also allows you to request a single image. Instead of ?action=stream you can use ?action=snapshot which will return a single image. You just need to re-issue the request when you want a new image. Requesting a single snapshot should be possible with Unity’s WWW class.