Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ Only the required SVGs and their license are packaged on the ISO.
No network connection or JavaScript runtime is needed to display the icons on the console.


## Video presentation
## Video codec selection

**Settings → Display → Video Codec** defaults to **Auto (Prefer MPEG-2)**. Moonlight tries MPEG-2
when the host advertises it and uses standard automatic negotiation if it is unavailable or the
MPEG-2 handshake fails. Choose **Auto** for standard negotiation, which prefers H.264, then MPEG-2,
then H.263+. You can also select H.264, MPEG-2, or H.263+ directly.
An explicit choice requests only that codec and fails negotiation when it is unavailable.
Sunshine must enable `mpeg2_mode = 2` or `h263p_mode = 2` to advertise the corresponding legacy codec.
These Sunshine protocol extensions are available to any client implementing their negotiation and decoding.

The preference is saved as `streaming.video_codec` in the settings file. The default value is
`"prefer_mpeg2"`. Moonlight also reads the older `streaming.video_decoder` key for compatibility.
`video_codec` takes precedence when both exist.

**Settings → Display → Video Presentation** defaults to **Software**, which writes directly to the
framebuffer when available and performed better in xemu. **GPU** uses NV2A color conversion and scaling
Expand Down
8 changes: 7 additions & 1 deletion cmake/moonlight-dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function(_moonlight_compute_ffmpeg_signature out_var nxdk_dir ffmpeg_source_dir)
"NXDK_DIR=${nxdk_dir}"
"NXDK_REVISION=${nxdk_revision}"
"COMPAT_SHA256=${compat_hashes}"
"FFMPEG_PROFILE=h264-opus-xbox"
"FFMPEG_PROFILE=h264-mpeg2-h263-opus-xbox"
"FFMPEG_TARGET_OS=none"
"FFMPEG_ARCH=x86"
"FFMPEG_CC_WRAPPER_SHA256=${ffmpeg_cc_wrapper_hash}"
Expand Down Expand Up @@ -221,7 +221,13 @@ function(_moonlight_get_ffmpeg_configure_args out_var)
--enable-swscale
--enable-swresample
--enable-parser=h264
--enable-parser=h263
--enable-parser=mpegvideo
--enable-decoder=h264
--enable-decoder=h263
--enable-decoder=h263i
--enable-decoder=h263p
--enable-decoder=mpeg2video
--enable-decoder=opus)

set(${out_var} "${ffmpeg_configure_args}" PARENT_SCOPE)
Expand Down
66 changes: 63 additions & 3 deletions src/app/client_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ namespace {
constexpr std::array<char, 10> ADD_HOST_PORT_KEYPAD_CHARACTERS {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
constexpr std::array<int, 6> STREAM_FRAMERATE_OPTIONS {15, 20, 24, 25, 30, 60};
constexpr std::array<int, 24> STREAM_BITRATE_OPTIONS {500, 750, 1000, 1500, 2000, 2500, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000, 20000};
constexpr std::array<app::VideoCodecSelection, 5> VIDEO_CODEC_OPTIONS {
app::VideoCodecSelection::preferMpeg2,
app::VideoCodecSelection::autoDetect,
app::VideoCodecSelection::h264,
app::VideoCodecSelection::mpeg2,
app::VideoCodecSelection::h263p,
};

/**
* @brief Describes the keypad characters available for the active add-host field.
Expand Down Expand Up @@ -168,6 +175,29 @@ namespace {
return std::to_string(videoMode.width) + "x" + std::to_string(videoMode.height);
}

/**
* @brief Return the settings label for one video codec preference.
*
* @param selection Codec preference to describe.
* @return User-facing codec preference label.
*/
const char *video_codec_selection_label(app::VideoCodecSelection selection) {
switch (selection) {
case app::VideoCodecSelection::preferMpeg2:
return "Auto (Prefer MPEG-2)";
case app::VideoCodecSelection::autoDetect:
return "Auto";
case app::VideoCodecSelection::h264:
return "H.264";
case app::VideoCodecSelection::mpeg2:
return "MPEG-2/H.262";
case app::VideoCodecSelection::h263p:
return "H.263+";
}

return "Auto";
}

/**
* @brief Return the selected stream-resolution index inside the detected mode list.
*
Expand Down Expand Up @@ -246,6 +276,22 @@ namespace {
state.settings.streamBitrateKbps = next == STREAM_BITRATE_OPTIONS.end() ? STREAM_BITRATE_OPTIONS.front() : *next;
}

/**
* @brief Advance the preferred video codec to the next supported option.
*
* @param state Current client state containing the preferred codec.
*/
void cycle_video_codec(app::ClientState &state) {
const auto current = std::find(VIDEO_CODEC_OPTIONS.begin(), VIDEO_CODEC_OPTIONS.end(), state.settings.video.codec);
if (current == VIDEO_CODEC_OPTIONS.end()) {
state.settings.video.codec = VIDEO_CODEC_OPTIONS.front();
return;
}

const std::size_t nextIndex = (static_cast<std::size_t>(std::distance(VIDEO_CODEC_OPTIONS.begin(), current)) + 1U) % VIDEO_CODEC_OPTIONS.size();
state.settings.video.codec = VIDEO_CODEC_OPTIONS[nextIndex];
}

std::string pairing_reset_endpoint_key(std::string_view address, uint16_t port) {
return app::normalize_ipv4_address(address) + ":" + std::to_string(app::effective_host_port(port));
}
Expand Down Expand Up @@ -596,9 +642,15 @@ namespace {
"Cycle through the preferred video bitrate. Lower bitrates reduce bandwidth use. Changing resolution or frame rate resets this value to a recommended default.",
true,
},
{
"cycle-video-codec",
std::string("Video Codec: ") + video_codec_selection_label(state.settings.video.codec),
"Prefer MPEG-2 for faster decoding, use standard automatic negotiation, or request a specific codec.",
true,
},
{
"toggle-gpu-presentation",
std::string("Video Presentation: ") + (state.settings.presentation.gpuPresentation ? "GPU" : "Software"),
std::string("Video Presentation: ") + (state.settings.video.gpuPresentation ? "GPU" : "Software"),
"Choose how new streams display decoded video. GPU uses NV2A with automatic software fallback. Software can be faster in xemu.",
true,
},
Expand All @@ -616,7 +668,7 @@ namespace {
},
{
"toggle-show-performance-stats",
std::string("Performance Stats: ") + (state.settings.presentation.showPerformanceStats ? "On" : "Off"),
std::string("Performance Stats: ") + (state.settings.video.showPerformanceStats ? "On" : "Off"),
"Show performance statistics during playback and after the stream ends.",
true,
},
Expand Down Expand Up @@ -2457,7 +2509,7 @@ namespace app {
* @return True when the activated row changed a presentation preference.
*/
bool handle_presentation_setting_activation(ClientState &state, const std::string &itemId, AppUpdate &update) {
auto &presentation = state.settings.presentation;
auto &presentation = state.settings.video;
if (itemId == "toggle-gpu-presentation") {
presentation.gpuPresentation = !presentation.gpuPresentation;
state.shell.statusMessage = std::string("Video presentation set to ") + (presentation.gpuPresentation ? "GPU" : "Software");
Expand Down Expand Up @@ -2531,6 +2583,14 @@ namespace app {
rebuild_menu(state, "cycle-stream-bitrate");
return;
}
if (detailUpdate.activatedItemId == "cycle-video-codec") {
cycle_video_codec(state);
state.settings.dirty = true;
update->persistence.settingsChanged = true;
state.shell.statusMessage = std::string("Video codec set to ") + video_codec_selection_label(state.settings.video.codec);
rebuild_menu(state, "cycle-video-codec");
return;
}
if (handle_presentation_setting_activation(state, detailUpdate.activatedItemId, *update)) {
return;
}
Expand Down
18 changes: 15 additions & 3 deletions src/app/client_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ namespace app {
right, ///< Dock the log viewer on the right side of the split layout.
};

/**
* @brief Video codec preference applied to new streaming sessions.
*/
enum class VideoCodecSelection {
preferMpeg2, ///< Prefer advertised MPEG-2, with normal negotiation available as a fallback.
autoDetect, ///< Negotiate supported codecs using the shared modern-first preference order.
h264, ///< Request only H.264 video.
mpeg2, ///< Request only MPEG-2/H.262 video.
h263p, ///< Request only H.263+ video.
};

inline constexpr int DEFAULT_STREAM_FRAMERATE = 60; ///< Stream frame rate at the default 640x480 resolution.
inline constexpr int DEFAULT_STREAM_BITRATE_KBPS = 3000; ///< Bitrate for the default 640x480, 60 FPS profile.

Expand Down Expand Up @@ -382,9 +393,10 @@ namespace app {
};

/**
* @brief Preferences for presenting decoded video and its on-screen statistics.
* @brief Preferences for decoding and presenting video in new streams.
*/
struct VideoPresentationPreferences {
struct StreamVideoPreferences {
VideoCodecSelection codec = VideoCodecSelection::preferMpeg2; ///< Preferred video codec for new streams.
bool gpuPresentation = false; ///< Opt in to NV2A presentation with software fallback on new streams.
bool showPerformanceStats = false; ///< Display live performance telemetry and include it in the stream summary.
};
Expand All @@ -406,7 +418,7 @@ namespace app {
bool preferredVideoModeSet = false; ///< True when preferredVideoMode contains a user-selected or default mode.
int streamFramerate = DEFAULT_STREAM_FRAMERATE; ///< Preferred stream frame rate in frames per second.
int streamBitrateKbps = DEFAULT_STREAM_BITRATE_KBPS; ///< Preferred stream bitrate in kilobits per second.
VideoPresentationPreferences presentation; ///< Presentation path and statistics overlay preferences.
StreamVideoPreferences video; ///< Codec, presentation path, and statistics overlay preferences.
bool playAudioOnPc = false; ///< True when the host PC should continue local audio playback during streaming.
bool playAudioOnXbox = true; ///< True when the Xbox should decode and play streamed audio locally.
LocalStreamPreferences controls; ///< Screenshot storage and keyboard placement preferences.
Expand Down
106 changes: 105 additions & 1 deletion src/app/settings_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ namespace {
return "full";
}

/**
* @brief Convert a codec preference to the persisted TOML token.
*
* @param selection Codec preference to serialize.
* @return Stable lowercase settings value.
*/
const char *video_codec_selection_text(app::VideoCodecSelection selection) {
switch (selection) {
case app::VideoCodecSelection::preferMpeg2:
return "prefer_mpeg2";
case app::VideoCodecSelection::autoDetect:
return "auto";
case app::VideoCodecSelection::h264:
return "h264";
case app::VideoCodecSelection::mpeg2:
return "mpeg2";
case app::VideoCodecSelection::h263p:
return "h263p";
}

return "auto";
}

bool try_parse_logging_level(std::string_view text, logging::LogLevel *level) {
const std::string normalized = ascii_lowercase(text);
if (normalized == "trace") {
Expand Down Expand Up @@ -196,6 +219,49 @@ namespace {
return false;
}

/**
* @brief Parse a codec preference from the persisted TOML token.
*
* @param text Settings value to parse.
* @param selection Receives the parsed codec preference on success.
* @return True when @p text is a supported codec preference.
*/
bool try_parse_video_codec_selection(std::string_view text, app::VideoCodecSelection *selection) {
const std::string normalized = ascii_lowercase(text);
if (normalized == "prefer_mpeg2") {
if (selection != nullptr) {
*selection = app::VideoCodecSelection::preferMpeg2;
}
return true;
}
if (normalized == "auto") {
if (selection != nullptr) {
*selection = app::VideoCodecSelection::autoDetect;
}
return true;
}
if (normalized == "h264" || normalized == "h.264") {
if (selection != nullptr) {
*selection = app::VideoCodecSelection::h264;
}
return true;
}
if (normalized == "mpeg2" || normalized == "mpeg-2" || normalized == "h262" || normalized == "h.262") {
if (selection != nullptr) {
*selection = app::VideoCodecSelection::mpeg2;
}
return true;
}
if (normalized == "h263p" || normalized == "h.263p" || normalized == "h263+" || normalized == "h.263+") {
if (selection != nullptr) {
*selection = app::VideoCodecSelection::h263p;
}
return true;
}

return false;
}

void append_invalid_value_warning(std::vector<std::string> *warnings, const std::string &filePath, std::string_view keyPath, std::string_view valueText) {
if (warnings == nullptr) {
return;
Expand Down Expand Up @@ -266,6 +332,36 @@ namespace {
append_invalid_value_warning(warnings, filePath, "ui.log_viewer_placement", "<non-string>");
}

/**
* @brief Load one codec preference setting when present.
*
* @param settingNode TOML node to parse.
* @param filePath Settings file path used in warnings.
* @param keyPath Canonical or legacy setting key reported in validation warnings.
* @param selection Receives the parsed codec preference on success.
* @param warnings Warning collection updated for invalid values.
*/
void load_video_codec_selection_setting(
toml::node_view<const toml::node> settingNode,
const std::string &filePath,
std::string_view keyPath,
app::VideoCodecSelection *selection,
std::vector<std::string> *warnings
) {
if (!settingNode) {
return;
}

if (const auto videoCodecText = settingNode.value<std::string>(); videoCodecText) {
if (!try_parse_video_codec_selection(*videoCodecText, selection)) {
append_invalid_value_warning(warnings, filePath, keyPath, *videoCodecText);
}
return;
}

append_invalid_value_warning(warnings, filePath, keyPath, "<non-string>");
}

/**
* @brief Load one integer settings value when present.
*
Expand Down Expand Up @@ -349,6 +445,8 @@ namespace {
content += "# Preferred streaming parameters.\n";
content += std::string("fps = ") + std::to_string(settings.streamFramerate) + "\n";
content += std::string("bitrate_kbps = ") + std::to_string(settings.streamBitrateKbps) + "\n";
content += "# Video codec preference. prefer_mpeg2 tries advertised MPEG-2 first. auto uses standard negotiation.\n";
content += std::string("video_codec = \"") + video_codec_selection_text(settings.videoCodec) + "\"\n";
content += std::string("gpu_presentation = ") + (settings.gpuPresentation ? "true" : "false") + "\n";
content += std::string("play_audio_on_pc = ") + (settings.playAudioOnPc ? "true" : "false") + "\n";
content += std::string("play_audio_on_xbox = ") + (settings.playAudioOnXbox ? "true" : "false") + "\n";
Expand Down Expand Up @@ -401,7 +499,7 @@ namespace {
void inspect_streaming_keys(const toml::table &streamingTable, const std::string &filePath, app::LoadAppSettingsResult *result) {
for (const auto &[rawKey, node] : streamingTable) {
const std::string key(rawKey.str());
if (key == "video_width" || key == "video_height" || key == "video_bpp" || key == "video_refresh" || key == "video_mode_selected" || key == "fps" || key == "bitrate_kbps" || key == "gpu_presentation" || key == "play_audio_on_pc" || key == "play_audio_on_xbox" || key == "show_performance_stats" || key == "screenshot_directory" || key == "keyboard_dock_top") {
if (key == "video_width" || key == "video_height" || key == "video_bpp" || key == "video_refresh" || key == "video_mode_selected" || key == "fps" || key == "bitrate_kbps" || key == "video_codec" || key == "video_decoder" || key == "gpu_presentation" || key == "play_audio_on_pc" || key == "play_audio_on_xbox" || key == "show_performance_stats" || key == "screenshot_directory" || key == "keyboard_dock_top") {
continue;
}

Expand Down Expand Up @@ -502,6 +600,12 @@ namespace app {
const VIDEO_MODE bitrateMode = result.settings.preferredVideoModeSet && preferredMode.width > 0 && preferredMode.height > 0 ? preferredMode : VIDEO_MODE {640, 480, 32, 60};
result.settings.streamBitrateKbps = default_stream_bitrate_kbps(bitrateMode.width, bitrateMode.height, result.settings.streamFramerate);
load_integer_setting(settingsTable["streaming"]["bitrate_kbps"], filePath, "streaming.bitrate_kbps", &result.settings.streamBitrateKbps, &result.warnings);
const auto codecNode = settingsTable["streaming"]["video_codec"];
if (codecNode) {
load_video_codec_selection_setting(codecNode, filePath, "streaming.video_codec", &result.settings.videoCodec, &result.warnings);
} else {
load_video_codec_selection_setting(settingsTable["streaming"]["video_decoder"], filePath, "streaming.video_decoder", &result.settings.videoCodec, &result.warnings);
}
load_boolean_setting(settingsTable["streaming"]["play_audio_on_pc"], filePath, "streaming.play_audio_on_pc", &result.settings.playAudioOnPc, &result.warnings);
load_boolean_setting(settingsTable["streaming"]["gpu_presentation"], filePath, "streaming.gpu_presentation", &result.settings.gpuPresentation, &result.warnings);
load_boolean_setting(settingsTable["streaming"]["play_audio_on_xbox"], filePath, "streaming.play_audio_on_xbox", &result.settings.playAudioOnXbox, &result.warnings);
Expand Down
1 change: 1 addition & 0 deletions src/app/settings_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace app {
bool preferredVideoModeSet = false; ///< True when preferredVideoMode contains a saved user preference.
int streamFramerate = DEFAULT_STREAM_FRAMERATE; ///< Preferred stream frame rate in frames per second.
int streamBitrateKbps = DEFAULT_STREAM_BITRATE_KBPS; ///< Preferred stream bitrate in kilobits per second.
app::VideoCodecSelection videoCodec = app::VideoCodecSelection::preferMpeg2; ///< Preferred video codec for new streams.
bool playAudioOnPc = false; ///< True when the host PC should continue local audio playback during streaming.
bool showPerformanceStats = false; ///< Display live performance telemetry and include it in the stream summary.
bool playAudioOnXbox = true; ///< True when the Xbox should decode and play streamed audio locally.
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ namespace {
state.settings.preferredVideoModeSet = settings.preferredVideoModeSet;
state.settings.streamFramerate = settings.streamFramerate;
state.settings.streamBitrateKbps = settings.streamBitrateKbps;
state.settings.presentation.gpuPresentation = settings.gpuPresentation;
state.settings.video.codec = settings.videoCodec;
state.settings.video.gpuPresentation = settings.gpuPresentation;
state.settings.playAudioOnPc = settings.playAudioOnPc;
state.settings.presentation.showPerformanceStats = settings.showPerformanceStats;
state.settings.video.showPerformanceStats = settings.showPerformanceStats;
state.settings.playAudioOnXbox = settings.playAudioOnXbox;
state.settings.controls.screenshotDirectory = settings.screenshotDirectory;
state.settings.controls.keyboardDockTop = settings.keyboardDockTop;
Expand Down
13 changes: 11 additions & 2 deletions src/startup/video_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ namespace startup {
return left.width == right.width && left.height == right.height;
}

/**
* @brief Return the pixel area used to compare stream resolutions.
* @param videoMode Stream mode to measure.
* @return Resolution area in pixels.
*/
int stream_video_mode_area(const VIDEO_MODE &videoMode) {
return videoMode.width * videoMode.height;
}

bool is_smaller_video_mode(const VIDEO_MODE &left, const VIDEO_MODE &right) {
const int leftArea = left.width * left.height;
if (const int rightArea = right.width * right.height; leftArea != rightArea) {
const int leftArea = stream_video_mode_area(left);
if (const int rightArea = stream_video_mode_area(right); leftArea != rightArea) {
return leftArea < rightArea;
}
return left.width < right.width;
Expand Down
Loading
Loading