2. Raw Hardware Access
The Windows Phone SDK includes a couple of
key classes that support low-level access to the camera and microphone
hardware. Although it is much easier to use the PhotoCamera
class, if you need to capture video and/or audio and manipulate it in real time, these APIs are the best tool for the job.
The starting point to the raw camera API is the CaptureSource
class. The CaptureSource
class enables you to have access to video input on the phone. Like the PhotoCamera
class, you can use the CaptureSource
class as the source for a VideoBrush
. This way, you can show the raw input from the camera in your application:
CaptureSource _src = new CaptureSource();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Show the preview
previewBrush.SetSource(_src);
}
To enable the video, you need to enable the CaptureSource
by using the Start
method:
private void camButton_Click(object sender, RoutedEventArgs e)
{
_src.Start();
}
At this point, you have only the camera showing
up in your own application. To deal with live input from the camera and
microphone, you have to create special classes called sinks. To retrieve video you need a VideoSink
class; for audio you need an AudioSink
class. These are abstract classes from which you must derive in order to retrieve real-time data from the hardware. They have a small number of abstract methods (that you have to override). A skeleton VideoSink
derived class would look like this:
public class MyVideoSink : VideoSink
{
protected override void OnSample(long
sampleTimeInHundredNanoseconds,
long frameDurationInHundredNanoseconds,
byte[] sampleData)
{
// Encode or Save the stream
}
protected override void OnCaptureStarted()
{
// Handle Startup of Capture
}
protected override void OnCaptureStopped()
{
// Cleanup After Capture
}
VideoFormat _format = null;
protected override void OnFormatChange(VideoFormat videoFormat)
{
// Store the Video Format
_format = videoFormat;
}
}
Here are the abstract methods you must override.
• OnCaptureStarted: This is where you can do any initialization necessary to prepare for data capture.
• OnCaptureStopped: This is where you can clean up or save after a capture is complete.
• OnFormatChange: This is where you would store the current format to determine how to consume the capture samples.
• OnSample:
This is where most of the real work is accomplished in the sink. This
is called periodically with a set of samples from the hardware.
After you have a sink, you can specify the CaptureSource
of the sink before you start capturing:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Show the preview
previewBrush.SetSource(_src);
// Capture the video
_sink.CaptureSource = _src;
}
When you specify the CaptureSource,
your sink will be notified because the CaptureSource
is manipulated by the user. In this way, you can have more than one audio and video sink per CaptureSource
.
Because the raw camera API originated from Silverlight, it enables you to specify the camera on the device you want. The CaptureDeviceConfiguration
class allows you to enumerate the different audio and video capture
devices. In this way, you can specify the capture device for the CaptureSource
to use, like so:
ICollection<VideoCaptureDevice> devices =
CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();
// Pick the first device
_src.VideoCaptureDevice = devices.First();
The API supports multiple devices so that, in
the future, if there are multiple video devices (for example, a
front-facing camera), your code will be able to pick which device to
use. By default, the CaptureSource
uses the “primary
device,” which is usually the standard camera, so working with this API
isn’t necessary with the current crop of phones.