There is a lot of buzz due to the new capabilities of Silverlight 4, one of them are webcam and microphone capturing.
Here are two great examples that you can use as a starting point: Accessing Web Camera and Microphone by Tim Heuer and Silverlight 4 Augmented Reality Proof Of Concept by René Schulte.
The problem is that in the current API for video and audio capturing, there is support only for RAW data, and that means 32bpp ARGB video and PCM audio samples. Also you have nothing in the server side to handle streaming from client to server, which prevents you from develop any real scenario of video/audio communication tool.
However, this is only the current Beta stage and I’m pretty sure Microsoft will push hard for complement this new features in the incoming versions.
Meanwhile, I handle to port (from JSpeex) the Speex audio codec completely to C# and modified the Tim Heuer’s sample to encode the audio. I uploaded the source code to CodePlex at CSpeex – http://cspeex.codeplex.com/. There is a new button that allows you to save the recorded audio. The audio is encoded in real time to a memory Ogg stream and when you press “Save Audio” button, this stream is decoded and saved as a WAV in your computer.
Here it is the code snippet for the encoding part.
protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)
{
for (int i = 0; i < sampleData.Length; )
{
int len = Math.Min(sampleData.Length - i, temp.Length - tempOffset);
Buffer.BlockCopy(sampleData, i, temp, tempOffset, len);
if (len < temp.Length - tempOffset)
{
tempOffset += len;
}
else
{
tempOffset = 0;
speexEncoder.processData(temp, 0, temp.Length);
int encsize = speexEncoder.getProcessedData(temp, 0);
if (encsize > 0 && (memFile.InnerStream.Position + encsize < ((MemoryStream)memFile.InnerStream).Capacity))
{
writer.writePacket(temp, 0, encsize);
}
}
i += len;
}
}
Remember, there is a long path to a real chat application, since you need to handle how to upload the encoded stream to the server and do the actual communication between two parties.













11 Comments
Very nice! I found the JSpeex implementation a couple days ago, and was getting ready to try this port myself, when I found yours. Very handy.
Great stuff and keep up the good work!
I just subscribed to your blog feed.
Thank you! for the comments.
By the way, we just got this working with a round-trip from the client to a home-grown media server, decoded the audio in C++ with the latest Speex codec, mixed it with other decoded streams, re-encoded it all, sent it back to the clients, and played it back, all in real-time. Many thanks for putting this out there: this was the big missing piece.
Glad to hear your good results.
I cannot get this working for the life of me. Alden, could you send me an e-mail so I can talk to you?
Hi Guys, this is wonderful work. since we are working on a multiplayer game for Facebook that can use this, I am offering you my development time for the networking part of this in either UDP or TCP and the server side to receive and retransmit to the subscriber clients in you guys also help on the video part. Please let me know and I can help on the networking part pretty quickly…
Hi @Nader Rahimizad
Thanks for your offer and interest. As you can see, this is only a proof of concept. My bet is that Microsoft will address this shortcoming in the final release with an integrated solution for audio and video. If you need to release something before that, please, feel free to take the codeplex code and play with it. Regards.
this is cool, thanks for sharing the code
good control,
But i cound’nt manage to get the encoded stream to save it to server thru WCF, it’s just save to file disk, and also i can’t play it back when i capture the audio, can you give code snipt or an example?
Thanks
are you planning on applying the patch that Ken Smith has uploaded to codeplex? Any idea on when you will post an updated download file?
Also, do you know where i can find more information on how to acoomplish what Ken has done in “mixed it with other decoded streams, re-encoded it all” any sample code out there? I need to blend 4 player streams and rebroadcast to all the players. Thanks in advance for your guidance
One Trackback
[...] This post was mentioned on Twitter by René Schulte, Alden Torres. Alden Torres said: @timheuer Thanks for your Camera and Microphone example, I added Speex audio codec http://bit.ly/6Y0ncF [...]