Loading...
Searching...
No Matches
ml::ScreenSource Class Reference

Grabs the desktop as pixels, using the OS's own capture API. More...

#include <Malena/Screen/ScreenSource.h>

Public Member Functions

 ScreenSource ()
 ScreenSource (const ScreenSource &)=delete
 ~ScreenSource ()
int displayIndex () const
std::optional< sf::Imagegrab ()
const std::string & lastError () const
ScreenSourceoperator= (const ScreenSource &)=delete
void setDisplayIndex (int index)
void setMaxDimension (unsigned int pixels)

Static Public Member Functions

static std::optional< sf::Imagedecode (const std::uint8_t *data, std::size_t size)
static std::optional< std::vector< std::uint8_t > > encodeJpeg (const sf::Image &frame)
static bool isSupported ()

Detailed Description

Grabs the desktop as pixels, using the OS's own capture API.

This is the capture half of screen sharing with no third-party media runtime: no GStreamer, nothing for the user to install. Each platform uses its native call (CGDisplayCreateImage on macOS, BitBlt on Windows, XGetImage on X11), and the result is handed back as a plain sf::Image so the rest of the pipeline is platform-agnostic.

Deliberately a pull API: the caller decides the frame rate by how often it calls grab(). A shared screen only needs a handful of frames per second, and pulling keeps the cost proportional to what is actually sent.

if (auto frame = src.grab())
if (auto jpeg = ml::ScreenSource::encodeJpeg(*frame))
send(*jpeg);
Grabs the desktop as pixels, using the OS's own capture API.
static std::optional< std::vector< std::uint8_t > > encodeJpeg(const sf::Image &frame)
std::optional< sf::Image > grab()

Definition at line 38 of file ScreenSource.h.

Constructor & Destructor Documentation

◆ ScreenSource() [1/2]

ml::ScreenSource::ScreenSource ( )

◆ ~ScreenSource()

ml::ScreenSource::~ScreenSource ( )

◆ ScreenSource() [2/2]

ml::ScreenSource::ScreenSource ( const ScreenSource & )
delete

Member Function Documentation

◆ decode()

std::optional< sf::Image > ml::ScreenSource::decode ( const std::uint8_t * data,
std::size_t size )
static

Decode a frame produced by encodeJpeg (or any format SFML reads).

◆ displayIndex()

int ml::ScreenSource::displayIndex ( ) const

◆ encodeJpeg()

std::optional< std::vector< std::uint8_t > > ml::ScreenSource::encodeJpeg ( const sf::Image & frame)
static

Encode to JPEG in memory. Uses SFML's own encoder, so it needs no extra dependency; quality is fixed by SFML. Returns nullopt on failure.

◆ grab()

std::optional< sf::Image > ml::ScreenSource::grab ( )

Grab one frame, or nullopt if capture failed / is unavailable here.

◆ isSupported()

bool ml::ScreenSource::isSupported ( )
static

True when this platform has a capture backend compiled in.

◆ lastError()

const std::string & ml::ScreenSource::lastError ( ) const

Human-readable reason capture is unavailable, or "" when it works.

◆ operator=()

ScreenSource & ml::ScreenSource::operator= ( const ScreenSource & )
delete

◆ setDisplayIndex()

void ml::ScreenSource::setDisplayIndex ( int index)

Which display to capture (0 = main). Out-of-range falls back to main.

◆ setMaxDimension()

void ml::ScreenSource::setMaxDimension ( unsigned int pixels)

Cap the long edge of captured frames, preserving aspect (0 = no scaling). A Retina desktop is enormous; downscaling before encoding is the single biggest win for both bandwidth and encode time.


The documentation for this class was generated from the following file: