<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dave Amenta .com &#187; IPCamera</title>
	<atom:link href="http://www.daveamenta.com/tag/ipcamera/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveamenta.com</link>
	<description>(dot (at dave daveamenta) com)</description>
	<lastBuildDate>Tue, 31 Jan 2012 17:22:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Reading Motion-JPEG camera streams</title>
		<link>http://www.daveamenta.com/2009-09/reading-motion-jpeg-camera-streams/</link>
		<comments>http://www.daveamenta.com/2009-09/reading-motion-jpeg-camera-streams/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 22:46:27 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IPCamera]]></category>

		<guid isPermaLink="false">http://www.daveamenta.com/?p=127</guid>
		<description><![CDATA[I&#8217;ve recently come across a Hawking HNC230G network camera, which shares internals with the Edimax IC-1500Wg. These cameras pump out a Motion-JPEG stream which is unlike many other &#8220;Motion-JPEG&#8221; IP cameras. Many of these devices will create video with a constantly updating jpeg file, served via HTTP. This is generally a better solution than what [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently come across a Hawking HNC230G network camera, which shares internals with the Edimax IC-1500Wg.  These cameras pump out a Motion-JPEG stream which is unlike many other &#8220;Motion-JPEG&#8221; IP cameras.</p>
<p>Many of these devices will create video with a constantly updating jpeg file, served via HTTP.  This is generally a better solution than what exists in my camera.  Reading these streams is simple in many languages.  The HNC230G took a little more probing and experimentation before I could read data without artifacts.</p>
<p><strong>Camera Services</strong></p>
<p>HTTP Administration website &#8211; Port 80<br />
Motion-JPEG stream &#8211; Port 4321</p>
<p>There does not appear to be any images on the HTTP administration website.  All video data passes through the service on 4321.  Video data is <em>not encrypted or secured in any way</em> &#8211; there is no authentication.</p>
<p><strong>Finished Protocol</strong></p>
<p>After a socket is connected to the service on 4321, it should send the single command which is supported: <em>0110\n</em>.  Sending 0110 followed by a linebreak will signal the device to send a JFIF frame.</p>
<p>The camera will send 4 bytes of data before the JFIF header, these bytes can be interpreted as follows:</p>
<ul>
<li>Byte 1:  High 8 bits of the frame size</li>
<li>Byte 2: Low 8 bits of the frame size</li>
<li>Byte 3: Active users on the camera</li>
<li>Byte 4: Unknown reserved byte</li>
</ul>
<p>The size of the frame is calculated only after the 4-byte header.  The process can be repeated once the frame has been received by the client.  The image data received will contain a JFIF (JPEG File Interchange Format) frame.</p>
<p>I&#8217;ve been able to achieve approximately 10fps at 640&#215;480 with this method.</p>
<p><strong>Limitations</strong></p>
<p>The key limitations with this method relate to the firmware design for this camera.</p>
<ul>
<li>Image size can only be set through the HTTP web service.</li>
<li>LED state can only be queried or set through the HTTP web service.</li>
<li>The device will sometimes disconnect the client or return invalid data.  This is solved by reconnecting the client.</li>
<li>As the number of users increase, the possible frames-per-second decreases.  Frame rates are 25-50% less when adding a second user.</li>
</ul>
<p><strong>Sample C# Code</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">       <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> GetFrame<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>conn<span style="color: #008000;">.</span><span style="color: #0000FF;">Connected</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                Connect<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>conn<span style="color: #008000;">.</span><span style="color: #0000FF;">Connected</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Cannot fetch frame from camera.  Not Connected.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            StreamWriter sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamWriter<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// request a frame</span>
            sw<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;0110&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            sw<span style="color: #008000;">.</span><span style="color: #0000FF;">Flush</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// get the high 8bits of the size</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> size <span style="color: #008000;">=</span> conn<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadByte</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;&amp;</span>lt<span style="color: #008000;">;</span> <span style="color: #FF0000;">8</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">// get the low 8bits of the size</span>
                size <span style="color: #008000;">+=</span> conn<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadByte</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// active users</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> j1 <span style="color: #008000;">=</span> conn<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadByte</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                ActiveUsers <span style="color: #008000;">=</span> j1<span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">// unknown byte</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> j2 <span style="color: #008000;">=</span> conn<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadByte</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>size <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> BinaryReader<span style="color: #008000;">&#40;</span>conn<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadBytes</span><span style="color: #008000;">&#40;</span>size<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.daveamenta.com/2009-09/reading-motion-jpeg-camera-streams/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

