views:

126

answers:

1

We're currently using the Silverlight VideoSink to capture video from users' local webcams, kinda like so:

    protected override void OnSample(long sampleTime, long frameDuration, byte[] sampleData)
    {
        if (FrameShouldBeSubmitted())
        {
            byte[] resampledData = ResizeFrame(sampleData);
            mediaController.SetVideoFrame(resampledData);
        }
    }

Now, on most of the machines that we've tested, the video sample provided in the byte[] sampleData parameter is upside-down, i.e., if you try to take the RGBA data and turn it into, say, a WriteableBitmap, the bitmap will be upside-down. That's odd, but fairly easy to correct, of course -- you just have to reverse the array as you encode it.

The problem is that at least on some machines (e.g., the single Macintosh in our test environment), the video sample provided is no longer upside-down, but right-side up, and hence, flipping the image actually results in an image that's received upside-down on the far side.

I reported this to MS as a bug, but their (terse) response was that it was "As Designed". Further attempts at clarification have so far been ignored.

Now, I'll grant that it's kinda entertaining to imagine the discussions behind this design decision: "OK, just to make it interesting, let's play the video rightside up on a Mac, but let's turn it upside down for Windows!" "Great idea!" "Yeah, that'll keep those developers guessing!" But beyond that, I can't find this, umm, "feature" documented anywhere, nor can I find any documentation on how one is supposed to be able to tell that a given video sample is upside down or rightside up. Any thoughts on how to tell this?

EDIT 3/29/10 4:50 pm - I got a response from MS which said that the appropriate way to tell was through the Stride property on the VideoFormat object, i.e., if the stride value is negative, the image will be upside-down. However, my own testing indicates that unless I'm doing something wrong, this isn't the case. At least on my own machine, whether the stride value is zero or negative (the only options I see), the sampled image is still upside-down.

A: 

I was going to suggest looking at VideoFormat.Stride provided at VideoSink.OnFormatChange but then I noticed your edit. I went ahead and tested it at my dev machine, image is upside down and stride is negative as expected. Have you checked again recently?

Even though stride made perfect sense for native applications (using stride at pointer operations), I agree that current behavior is not what you expect from a modern API. However performance wise, it is better not to make changes on data received from native API.

Yet at this point, while we are talking about performance, why not provide samples in formats other than PixelFormatType.Format32bppArgb so that we can avoid color space conversion? BTW, there is a VideoCaptureDevice.DesiredFormat property which only works for resolution as there is no alternative to PixelFormatType.Format32bppArgb.

orca
I haven't tested this recently (Mac support has taken a backseat for the moment), but I will. What I remember seeing when I did test this was that I got an upside-down image whether stride was zero or negative. But I'll check again at some point. I agree also that the Silverlight webcam and microphone API's are pretty primitive. I'm running into a number of issues at the moment with problems resulting from Silverlight not correcting for sync issues with some audio cards (i.e., according to the system clock, they submit or request samples every 20.5 ms instead of every 20 ms). Uggh.
Ken Smith