title: Streaming a webcam from Silverlight 4 (Beta)
body: <p>The new webcam stuff in Silverlight 4 is darned cool.  By exposing it as a brush, it allows scenarios that are way beyond anything that Flash has.</p>
  
  <p>At the same time, accessing the webcam locally seems like it's only half the story.  Nobody buys a webcam so they can take pictures of themselves and make funny faces out of them.  They buy a webcam because they want other people to see the resulting video stream, i.e., they want to stream that video out to the Internet, a lay Skype or any of the dozens of other video chat sites/applications.  And so far, I haven't figured out how to do that with </p>
  
  <p>It turns out that it's pretty simple to get a hold of the raw (Format32bppArgb formatted) bytestream, as demonstrated <a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-camera-and-microphone-support.aspx" rel="noreferrer">here</a>.</p>
  
  <p>But unless we want to transmit that raw bytestream to a server (which would chew up way too much bandwidth), we need to encode that in some fashion.  And that's more complicated.  MS has implemented several codecs in Silverlight, but so far as I can tell, they're all focused on decoding a video stream, not encoding it in the first place.  And that's apart from the fact that I can't figure out how to get direct access to, say, the H.264 codec in the first place.</p>
  
  <p>There are a ton of open-source codecs (for instance, in the ffmpeg project <a href="http://www.ffmpeg.org/" rel="noreferrer">here</a>), but they're all written in C, and they don't look easy to port to C#.  Unless translating 10000+ lines of code that look like this is your idea of fun :-)</p>
  
  <pre><code>const int b_xy= h-&gt;mb2b_xy[left_xy[i]] + 3;
  const int b8_xy= h-&gt;mb2b8_xy[left_xy[i]] + 1;
  *(uint32_t*)h-&gt;mv_cache[list][cache_idx ]= *(uint32_t*)s-&gt;current_picture.motion_val[list][b_xy + h-&gt;b_stride*left_block[0+i*2]];
  *(uint32_t*)h-&gt;mv_cache[list][cache_idx+8]= *(uint32_t*)s-&gt;current_picture.motion_val[list][b_xy + h-&gt;b_stride*left_block[1+i*2]];
  h-&gt;ref_cache[list][cache_idx ]= s-&gt;current_picture.ref_index[list][b8_xy + h-&gt;b8_stride*(left_block[0+i*2]&gt;&gt;1)];
  h-&gt;ref_cache[list][cache_idx+8]= s-&gt;current_picture.ref_index[list][b8_xy + h-&gt;b8_stride*(left_block[1+i*2]&gt;&gt;1)];
  </code></pre>
  
  <p>The mooncodecs folder within the Mono project (<a href="http://anonsvn.mono-project.com/viewvc/trunk/mooncodecs/" rel="noreferrer">here</a>) has several audio codecs in C# (ADPCM and Ogg Vorbis), and one video codec (Dirac), but they all seem to implement just the decode portion of their respective formats, as do the java implementations from which they were ported.</p>
  
  <p>I found a C# codec for Ogg Theora (csTheora, <a href="http://www.wreckedgames.com/forum/index.php?topic=1053.0" rel="noreferrer">http://www.wreckedgames.com/forum/index.php?topic=1053.0</a>), but again, it's decode only, as is the jheora codec on which it's based.</p>
  
  <p>Of course, it would presumably be easier to port a codec from Java than from C or C++, but the only java video codecs that I found were decode-only (such as <a href="http://svn.wikimedia.org/viewvc/mediawiki/trunk/cortado/src/com/fluendo/jheora/" rel="noreferrer">jheora</a>, or jirac).</p>
  
  <p>So I'm kinda back at square one.  It looks like our options for hooking up a webcam (or microphone) through Silverlight to the Internet are:</p>
  
  <p>(1) Wait for Microsoft to provide some guidance on this;</p>
  
  <p>(2) Spend the brain cycles porting one of the C or C++ codecs over to Silverlight-compatible C#;</p>
  
  <p>(3) Send the raw, uncompressed bytestream up to a server (or perhaps compressed slightly with something like zlib), and then encode it server-side; or</p>
  
  <p>(4) Wait for someone smarter than me to figure this out and provide a solution.</p>
  
  <p>Does anybody else have any better guidance?  Have I missed something that's just blindingly obvious to everyone else?  (For instance, does Silverlight 4 somewhere have some classes I've missed that take care of this?)</p>