#desktop #capture #dxgi #directx #duplication

win_desktop_duplication

Simple and efficient DXGI desktop duplication api

7 releases

Uses new Rust 2021

new 0.10.6 Aug 13, 2022
0.10.5 Aug 13, 2022
0.10.4 Jul 30, 2022
0.9.0 Jul 20, 2022
0.8.2 Jul 20, 2022

#28 in Windows APIs

Download history 90/week @ 2022-07-17 87/week @ 2022-07-24 89/week @ 2022-07-31 43/week @ 2022-08-07

311 downloads per month
Used in dxfilter

MIT/Apache

66KB
1K SLoC

Windows Desktop Duplication

docs.rs Crates.io Crates.io

This is meant to provide a low latency, low level access to desktop frames for use in applications like Game streaming (e.g., Google Stadia, Microsoft XCloud).

Crate provides convenient wrapper for acquiring gpu textures from Windows desktop duplication api. The crate includes some convenient features that the source api does not provide

Async Example

Although this example shows using TextureReader, for best performance, you want to use the texture directly to encode via one of the hardware based encoders like nvenc or quick-sync.

use win_desktop_duplication::*;
use win_desktop_duplication::{tex_reader::*, devices::*};

#[tokio::main(flavor = "current_thread")]
async fn main() {
    // this is required to be able to use desktop duplication api
    set_process_dpi_awareness();
    co_init();


    // select gpu and output you want to use.
    let adapter = AdapterFactory::new().get_adapter_by_idx(0).unwrap();
    let output = adapter.get_display_by_idx(0).unwrap();


    // get output duplication api
    let mut dupl = DesktopDuplicationApi::new(adapter, output).unwrap();

    // Optional: get TextureReader to read GPU textures into CPU.
    let (device, ctx) = dupl.get_device_and_ctx();
    let mut texture_reader = TextureReader::new(device, ctx);


    // create a vector to hold picture data;
    let mut pic_data: Vec<u8> = vec![0; 0];
    loop {
        // this api send one frame per vsync. the frame also has cursor pre drawn
        let tex = dupl.acquire_next_vsync_frame().await;
        if let Ok(tex) = tex {
            texture_reader.get_data(&mut pic_data, &tex);
            // use pic_data as necessary
        }
    }
}

Sync Example

Although this example shows using TextureReader, for best performance, you want to use the texture directly to encode via one of the hardware based encoders like nvenc or quick-sync.

use win_desktop_duplication::*;
use win_desktop_duplication::{tex_reader::*, devices::*};

fn main() {
    // this is required to be able to use desktop duplication api
    set_process_dpi_awareness();
    co_init();

    // select gpu and output you want to use.
    let adapter = AdapterFactory::new().get_adapter_by_idx(0).unwrap();
    let output = adapter.get_display_by_idx(0).unwrap();

    // get output duplication api
    let mut dupl = DesktopDuplicationApi::new(adapter, output.clone()).unwrap();

    // Optional: get TextureReader to read GPU textures into CPU.
    let (device, ctx) = dupl.get_device_and_ctx();
    let mut texture_reader = TextureReader::new(device, ctx);


    // create a vector to hold picture data;
    let mut pic_data: Vec<u8> = vec![0; 0];
    loop {
        // this api send one frame per vsync. the frame also has cursor pre drawn
        output.wait_for_vsync().unwrap();
        let tex = dupl.acquire_next_frame_now();

        if let Ok(tex) = tex {
            texture_reader.get_data(&mut pic_data, &tex);
            // use pic_data as necessary
        }
    }
}

Features

  • VSync when providing frames
  • Auto draw cursor onto the frame
  • Handle desktop switch automatically
  • Convenient functions to copy pixel data in cpu memory
  • Scale and color conversion (checkout dxfilter-rs).

Dependencies

~200MB
~3.5M SLoC