Files
screen_cast/include/screencast/network/signaling.h
T
fegger 30538fba73 perf(codec): upgrade x264 preset and downscale to the receiver display
Two quality improvements:

Preset: ultrafast -> veryfast. Unlocks Main profile with CABAC
entropy coding, hexagonal motion search, 3 reference frames, and
adaptive quantization — typically 30-40% better quality at the same
bitrate. The desktop handles the extra encoding cost trivially
(150+ fps at 1080p).

Downscaling: the receiver now advertises its display resolution in
the signaling answer (display_width/display_height, 0 = unknown).
When the capture exceeds the display (e.g. 2256x1504 source on a
1920x1080 receiver), the sender scales down preserving aspect ratio
before encoding — the same sws_scale pass that already converts the
pixel format also handles the resolution change, so there is no
extra step. This concentrates the entire bitrate into pixels the
display actually shows (~2.7x more bits per visible pixel at
4000 kbps when going from 2256x1504 to 1620x1080).

The renderer caches the display size during window creation
(native monitor resolution in fullscreen/KMSDRM; window size
otherwise). The receiver includes it in every signaling answer; the
sender pipeline computes aspect-preserving, even-rounded scaled
dimensions when the display is smaller than the capture.

meson test 5/5 in both configurations, valgrind clean.
2026-09-09 11:02:42 +02:00

69 lines
2.2 KiB
C++

#pragma once
#include "screencast/network/error.h"
#include "screencast/network/transport.h"
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <variant>
namespace sc {
// JSON-based control messages exchanged before or during a session. The wire
// format is one JSON object per newline-terminated TCP line; a WebSocket
// transport can replace TCP in a later phase without changing these types.
struct SessionOffer {
std::string session_id;
std::string codec_name;
int width = 0; // informational; the stream carries its own SPS/PPS
int height = 0;
int frame_rate_num = 30;
int frame_rate_den = 1;
Endpoint rtp_endpoint; // unused by the receiver; kept for symmetry
};
struct SessionAnswer {
std::string session_id;
// The address may be empty: the sender then targets the address of its
// signaling connection and the port carried here.
Endpoint rtp_endpoint;
// The receiver's display resolution (0 = unknown). The sender may
// downscale to this to avoid encoding pixels the display cannot show.
int display_width = 0;
int display_height = 0;
};
// Picture Loss Indication: the receiver asks the sender for a keyframe
// after discarding a damaged frame.
struct SessionPli {
std::string session_id;
};
using SignalingMessage = std::variant<SessionOffer, SessionAnswer, SessionPli>;
class SignalingChannel {
public:
using MessageCallback = std::function<void(const SignalingMessage&)>;
virtual ~SignalingChannel() = default;
// Client channels connect to a receiver's signaling server.
virtual bool connect(const Endpoint& server) = 0;
virtual void send(const SignalingMessage& message) = 0;
virtual void on_message(MessageCallback callback) = 0;
virtual void disconnect() = 0;
};
class SignalingFactory {
public:
// A client channel that connects to a receiver's signaling server.
static NetworkResult<std::unique_ptr<SignalingChannel>> create_client();
// A server channel listening on the given port; it keeps the most
// recent connection as its active peer.
static NetworkResult<std::unique_ptr<SignalingChannel>> create_server(uint16_t port);
};
} // namespace sc