DirectShow Custom Source Pin

549 views Asked by At

I am new to DirectShow. I, like many others, am trying to create a socket-based P2P streaming solution for a WPF-based card game. I want each player to be able to see each other via small video windows.

My questions are two-fold. The first is How do I lower the frame sample rate and resolution? I believe 320x200 x 15 to 20 fps should be fine. I am using the SampleGrabber callback to grab frame data and send it over the socket; which is actually working with no compression at 640x480 resolution.

My second question is, since each frame contains 921,600 bytes, this really bogs down and I get very slow rendering just across my local WiFi connected LAN. I added a simple MJPEG compression (wanting to switch to h.264 later) and I noticed the bytes drop to around 330-360k. Not a bad improvement.

On the receiving end Do I need to create a custom DirectShow Source Pin in order to serve up the bytes received from the socket so I can attach a decoder and render the bytes in a window?

I just wanted to ask this first since it seems like a lot of work to create a new COM object (haven't done that in about 15 years!), register it, and use/debug it.

Is there perhaps another way?

Also if that is the way to go, should I use a SampleGrabber on the receiving end and create a BitmapSource from the decompressed bytes, or should I allow DirectShow to create a child window? Thing is, I want to have more than one other player and I set an extra byte in the socket to tell what table position they are in. How do I render each position in turn?

1

There are 1 answers

0
NickV On

For those that are interested, here is how you set the resolution and add an encoder/compressor:

        // Create a graph builder
        int hr = captureGraphBuilder.SetFiltergraph(graphBuilder);

        // Find a capture device (WebCam) and attach it to the graph
        sourceFilter = FindCaptureDevice();
        hr = graphBuilder.AddFilter(sourceFilter, "Video Capture");

        // Get the source output Pin
        IPin sourcePin = DsFindPin.ByDirection((IBaseFilter)sourceFilter, PinDirection.Output, 0);
        IAMStreamConfig sc = (IAMStreamConfig)sourcePin;
        int count;
        int size;
        sc.GetNumberOfCapabilities(out count, out size);
        VideoInfoHeader v;
        AMMediaType media2 = null;
        IntPtr memPtr = Marshal.AllocCoTaskMem(size);
        for (int i = 0; i < count; ++i)
        {
            sc.GetStreamCaps(i, out media2, memPtr);

            v = (VideoInfoHeader)Marshal.PtrToStructure(media2.formatPtr, typeof(VideoInfoHeader));
            // Break when width is 160
            if (v.BmiHeader.Width == 160)
                break;
        }

        // Set the new media format t0 160 x 120
        hr = sc.SetFormat(media2);
        Marshal.FreeCoTaskMem(memPtr);
        DsUtils.FreeAMMediaType(media2);

        // Create a FramGrabber
        IBaseFilter grabberF = (IBaseFilter)new SampleGrabber();
        ISampleGrabber grabber = (ISampleGrabber)grabberF;

        // Set the media type
        var media = new AMMediaType
        {
            majorType = MediaType.Video,
            subType = MediaSubType.MJPG
            //subType = MediaSubType.RGB24
        };

        // The media sub type will be MJPG
        hr = grabber.SetMediaType(media);
        DsUtils.FreeAMMediaType(media);
        hr = grabber.SetCallback(this, 1);
        hr = graphBuilder.AddFilter(grabberF, "Sample Grabber");
        IPin grabberPin = DsFindPin.ByDirection(grabberF, PinDirection.Input, 0);

        // Get the MPEG compressor
        Guid iid = typeof(IBaseFilter).GUID;
        object compressor = null;
        foreach (DsDevice device in DsDevice.GetDevicesOfCat(FilterCategory.VideoCompressorCategory))//.MediaEncoderCategory))
        {
            if (device.Name == "MJPEG Compressor")
            {
                device.Mon.BindToObject(null, null, ref iid, out compressor);
                hr = graphBuilder.AddFilter((IBaseFilter)compressor, "Compressor");
                break;
            }
            string name = device.Name;
        }

        // This also works!
        //IBaseFilter enc = (IBaseFilter)new MJPGEnc();
        //graphBuilder.AddFilter(enc, "MJPEG Encoder");

        // Get the input and out pins of the compressor
        IBaseFilter enc = (IBaseFilter)compressor;
        IPin encPinIn = DsFindPin.ByDirection(enc, PinDirection.Input, 0);
        IPin encPinOut = DsFindPin.ByDirection(enc, PinDirection.Output, 0);

        // Attach the pins: source to input, output to grabber
        hr = graphBuilder.Connect(sourcePin, encPinIn);
        hr = graphBuilder.Connect(encPinOut, grabberPin);

        // Free the pin resources
        Marshal.ReleaseComObject(sourcePin);
        Marshal.ReleaseComObject(enc);
        Marshal.ReleaseComObject(encPinIn);
        Marshal.ReleaseComObject(encPinOut);
        Marshal.ReleaseComObject(grabberPin);

        // Create a render stream
        hr = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, null, grabberF);
        Marshal.ReleaseComObject(sourceFilter);
        Configure(grabber);