Suggestions

close search

Screen sharing — Windows

You can publish a stream that uses a video view of your screen (instead of a camera) as the source. A client connected to the session can subscribe to the stream (and view it), just as they would subscribe to a stream that uses a camera as the source.

This topic includes the following sections:

Publishing a stream with a screen-sharing source

To use the device's screen, instead of a camera, as the video source, you will need to implement a custom video capturer.

Here is a simple example that defines a custom video capturer to implement screen sharing (using the screen instead of a camera as the video source):

public class ScreenSharingCapturer : IVideoCapturer
{
    System.Threading.Timer timer;
    IVideoFrameConsumer frameConsumer;
    const int WIDTH = 640;
    const int HEIGHT = 480;
    const int FPS = 30;        

    public void Init(IVideoFrameConsumer frameConsumer)
    {
        this.frameConsumer = frameConsumer;
    }

    public void Start()
    {
        timer = new System.Threading.Timer((Object stateInfo) =>
        {
            using (Bitmap bitmap = new Bitmap(WIDTH, HEIGHT, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                using (Graphics graphics = Graphics.FromImage(bitmap as Image))
                {
                    graphics.CopyFromScreen(0, 0, 0, 0, new Size(WIDTH, HEIGHT), CopyPixelOperation.SourceCopy);
                }
                using (var frame = OpenTok.VideoFrame.CreateYuv420pFrameFromBitmap(bitmap))
                {
                    frameConsumer.Consume(frame);
                }
            }

        }, null, 0, 1000 / FPS);
    }

    public void Stop()
    {
        if (timer != null)
        {
            using (var timerDisposed = new ManualResetEvent(false))
            {
                timer.Dispose(timerDisposed);
                timerDisposed.WaitOne();
            }
        }
        timer = null;
    }

    public void Destroy()
    {
    }

    public VideoCaptureSettings GetCaptureSettings()
    {
        VideoCaptureSettings settings = new VideoCaptureSettings();
        settings.Width = WIDTH;
        settings.Height = HEIGHT;
        settings.Fps = FPS;
        settings.MirrorOnLocalRender = false;
        settings.PixelFormat = PixelFormat.FormatYuv420p;

        return settings;
    }
}

Set the VideoSourceType property of the Publisher object to OpenTok.VideoSourceType.Screen. This flags the published stream as having a screen-sharing video source (instead of a camera).

Determining the video type ("screen" or "camera") for a stream

The Stream object contains a VideoSourceType property. This can be set to one of the following values, defined in the OpenTok.VideoSourceType enum:

Subscribing to screen-sharing streams

You can subscribe to a stream that uses a screen-sharing video source in the same way that you subscribe to a stream that uses a camera as the source. See Subscribing to streams.