Table of Contents

Publishing Point with Screen Capture Source

This use-case demonstrates how to capture the desktop screen or a specific application window, encode it as H.264 video, and serve it as a publishing point. The IScreenCaptureSource supports both full-screen and per-window capture modes.

Overview

The createScreenCaptureSource() method is an async void method that performs the following:

  1. Enumerates available displays
  2. Creates a screen capture source via SourceFactory
  3. Configures either full-screen or window capture mode
  4. Sets the desired H.264 encoding parameters
  5. Creates the publishing point

The method is called from a background task on supported platforms:

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
    || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
    Task.Run(() => this.createScreenCaptureSource());
}

Enumerating Displays

var display = VAST.Capture.DisplayHelper.EnumerateDisplays()[0];

DisplayHelper returns a list of available displays with their device IDs and screen coordinates. The sample uses the first display.

Creating the Source

VAST.Capture.IScreenCaptureSource source = VAST.Media.SourceFactory.CreateScreenCapture();

Full-Screen Capture

source.DeviceId = display.DeviceId;
source.Region = new VAST.Common.Rect
{
    Left = display.Location.Left,
    Top = display.Location.Top,
    Right = display.Location.Right,
    Bottom = display.Location.Bottom
};
source.ShowMouse = true;

Set the DeviceId to the target display and Region to the display's full area. ShowMouse controls whether the mouse cursor is included in the capture.

Window Capture

var window = VAST.Capture.DisplayHelper.FindWindow(display.DeviceId, "VLC")[0];
source.WindowHandle = window;
source.WindowCaptureMode = VAST.Capture.WindowCaptureMode.Resize;
source.ShowMouse = true;

To capture a specific window, use DisplayHelper.FindWindow to locate it by title. Set WindowHandle to the found window handle and WindowCaptureMode to control how the capture adapts when the window is resized.

IScreenCaptureSource Properties

Property Type Description
DeviceId string Display device ID (for full-screen capture)
Region Rect Screen region to capture (left, top, right, bottom)
WindowHandle IntPtr Window handle (for window capture)
WindowCaptureMode WindowCaptureMode How to handle window resizing
ShowMouse bool Include the mouse cursor in the capture

Configuring Encoding

int width = 1280;
int height = 720;
VAST.Common.Rational framerate = new VAST.Common.Rational(30);

VAST.Common.MediaType mt = new VAST.Common.MediaType
{
    ContentType = VAST.Common.ContentType.Video,
    CodecId = VAST.Common.Codec.H264,
    Bitrate = width * height * 3,
    Width = width,
    Height = height,
    Framerate = framerate,
};
mt.Metadata.Add("KeyframeInterval", "30");
mt.Metadata.Add("Profile", "66");

await source.SetDesiredOutputType(0, mt);

The captured screen content is scaled to 1280x720 and encoded as H.264 at 30 fps with Baseline profile. The Bitrate is calculated from the output dimensions. By default, the built-in framework is used for encoding. To use FFmpeg instead:

source.EncoderParameters = new VAST.Media.EncoderParameters
{
    PreferredMediaFramework = VAST.Common.MediaFramework.FFmpeg,
    AllowHardwareAcceleration = false,
};

Creating the Publishing Point

this.server.CreatePublishingPoint("screen", source);

The screen capture stream is video-only. It is available at rtsp://server/screen, rtmp://server/live/screen, http://server:8888/hls/screen, etc.

Error Handling

The method is wrapped in a try/catch block. If the capture library is not available on the system, the exception is caught and the publishing point is not created. The source is disposed in the finally block if the publishing point was not successfully created.

Platform Support

Screen capture is supported on Windows and Linux. The sample guards the call with a platform check before invoking createScreenCaptureSource().

See Also