nsds
nsds

Reputation: 981

C# - Screen capture source filter - Direct Show

I can play a live video by selecting a web camera as input using direct show. Below is the code I used for it. Now instead of selecting video input device, I want to capture the screen as video. How can I define screen as primary filter? Please help to suggest

   private void buttonPlayVideoDevice_Click(object sender, EventArgs e)
    {
        if (comboBoxDevices.SelectedIndex < 0)
            return;
        StopPlaying();
        Device device = (Device)comboBoxDevices.Items[comboBoxDevices.SelectedIndex];
        if (m_player.OpenCamera(device, m_vcam_filter))
        {
            buttonStopVideoDevice.Enabled = true;
            m_player.Run();
        }
      }

  public bool OpenCamera(Device i_device, IBaseFilter i_vcam_filter)
    {
        // Stop and release interfaces
        Cleanup();
        bool succeeded = true;
        IPin pin_out = null;
        IPin pin_in = null;
        int hr = 0;

        // Create an instance of FilterGraph interface
        m_graph_builder = (IGraphBuilder)new FilterGraph();

        // Add camera source filter to our graph.
        IBaseFilter filter_source = i_device.CreateDevice();
        if (0 != (hr = m_graph_builder.AddFilter(filter_source, "Source Filter")))
        {
            succeeded = false;
            goto exit;
        }

        // Add VCam Render filter to graph, the VCam Render will pass video frames to VCam
        if (0 != (hr = m_graph_builder.AddFilter(i_vcam_filter, "VCam Renderer Filter")))
        {
            succeeded = false;
            goto exit;
        }

        pin_out = DsFindPin.ByDirection(filter_source, PinDirection.Output, 0);
        pin_in = DsFindPin.ByDirection(i_vcam_filter, PinDirection.Input, 0);
        if (pin_out == null || pin_in == null)
        {
            succeeded = false;
            goto exit;
        }

        if (0 != (hr = m_graph_builder.Connect(pin_out, pin_in)))
        {
            succeeded = false;
            goto exit;
        }

        m_control = (IMediaControl)m_graph_builder;      
        exit:
          if (filter_source != null)                                                                                      Marshal.ReleaseComObject(filter_source);
        if (pin_out != null) Marshal.ReleaseComObject(pin_out);
        if (pin_in != null) Marshal.ReleaseComObject(pin_in);
        return succeeded;            
    }

Upvotes: 0

Views: 1209

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69734

DirectShow does not come with a stock capability to capture screen as a video feed.

For reasons that are beyond the scope of this question DirectShow screen capture solutions, most of them at least, will exhibit constrained performance.

A typical solution is to take a look at Windows SDK 7.x samples and pull PushSourceDesktop filter from there. It is a sample project that implements a video source filter producing frames captured from desktop. The filter can be inserted into DirectShow filter graph and then used similarly to web camera filter. PushSourceDesktop has a mention on MSDN, and you will also find a few discussion threads here and on MSDN Forums.

Upvotes: 2

Related Questions