diff --git a/README.md b/README.md index 470749e..173c73b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmake/moonlight-dependencies.cmake b/cmake/moonlight-dependencies.cmake index 98f1c45..7d80ed2 100644 --- a/cmake/moonlight-dependencies.cmake +++ b/cmake/moonlight-dependencies.cmake @@ -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}" @@ -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) diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 61f01d6..75ac75a 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -34,6 +34,13 @@ namespace { constexpr std::array ADD_HOST_PORT_KEYPAD_CHARACTERS {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; constexpr std::array STREAM_FRAMERATE_OPTIONS {15, 20, 24, 25, 30, 60}; constexpr std::array 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 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. @@ -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. * @@ -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::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)); } @@ -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, }, @@ -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, }, @@ -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"); @@ -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; } diff --git a/src/app/client_state.h b/src/app/client_state.h index ab91668..2477576 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -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. @@ -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. }; @@ -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. diff --git a/src/app/settings_storage.cpp b/src/app/settings_storage.cpp index a0c8b71..5ee74c6 100644 --- a/src/app/settings_storage.cpp +++ b/src/app/settings_storage.cpp @@ -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") { @@ -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 *warnings, const std::string &filePath, std::string_view keyPath, std::string_view valueText) { if (warnings == nullptr) { return; @@ -266,6 +332,36 @@ namespace { append_invalid_value_warning(warnings, filePath, "ui.log_viewer_placement", ""); } + /** + * @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 settingNode, + const std::string &filePath, + std::string_view keyPath, + app::VideoCodecSelection *selection, + std::vector *warnings + ) { + if (!settingNode) { + return; + } + + if (const auto videoCodecText = settingNode.value(); 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, ""); + } + /** * @brief Load one integer settings value when present. * @@ -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"; @@ -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; } @@ -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); diff --git a/src/app/settings_storage.h b/src/app/settings_storage.h index 152bbb8..45190dd 100644 --- a/src/app/settings_storage.h +++ b/src/app/settings_storage.h @@ -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. diff --git a/src/main.cpp b/src/main.cpp index 47f0a02..735a115 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; diff --git a/src/startup/video_mode.cpp b/src/startup/video_mode.cpp index 7d0548b..504db5e 100644 --- a/src/startup/video_mode.cpp +++ b/src/startup/video_mode.cpp @@ -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; diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index 863323f..12067bc 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -61,6 +61,14 @@ namespace { constexpr std::uint64_t STREAM_VIDEO_DROP_LOG_INTERVAL = 30; constexpr Uint32 STREAM_VIDEO_DECODE_YIELD_MILLISECONDS = 1U; + /** + * @brief FFmpeg codec metadata selected from one negotiated Moonlight format. + */ + struct VideoCodecSelection { + AVCodecID codecId = AV_CODEC_ID_NONE; ///< FFmpeg codec identifier. + const char *displayName = "video"; ///< Human-readable codec name for logs. + }; + /** * @brief Return the backend currently receiving video callbacks. * @@ -184,6 +192,37 @@ namespace { }); } + /** + * @brief Resolve the FFmpeg decoder to use for one negotiated video format. + * + * @param videoFormat Moonlight negotiated video format mask. + * @param selection Output codec selection populated on success. + * @return True when the video format is supported by this backend. + */ + bool select_video_codec(int videoFormat, VideoCodecSelection *selection) { + if (selection == nullptr) { + return false; + } + + if ((videoFormat & VIDEO_FORMAT_MASK_MPEG2) != 0) { + selection->codecId = AV_CODEC_ID_MPEG2VIDEO; + selection->displayName = "MPEG-2/H.262"; + return true; + } + if ((videoFormat & VIDEO_FORMAT_H263P) != 0) { + selection->codecId = AV_CODEC_ID_H263P; + selection->displayName = "H.263+"; + return true; + } + if ((videoFormat & VIDEO_FORMAT_MASK_H264) != 0) { + selection->codecId = AV_CODEC_ID_H264; + selection->displayName = "H.264"; + return true; + } + + return false; + } + /** * @brief Return the number of bytes generated per second for the SDL audio format. * @@ -858,18 +897,19 @@ namespace streaming { acceleration_.cpuFeatures = (cpuFlags & AV_CPU_FLAG_SSE) != 0 ? "MMX/SSE" : ((cpuFlags & AV_CPU_FLAG_MMX) != 0 ? "MMX" : "Scalar"); acceleration_.threaded = std::string(avcodec_configuration()).find("--enable-w32threads") != std::string::npos; - if ((videoFormat & VIDEO_FORMAT_MASK_H264) == 0) { - logging::error("stream", "The FFmpeg backend currently supports only H.264 video streams"); + VideoCodecSelection codecSelection; + if (!select_video_codec(videoFormat, &codecSelection)) { + logging::error("stream", "The FFmpeg backend supports H.264, MPEG-2/H.262, and H.263+ video streams"); return -1; } - const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264); + const AVCodec *codec = avcodec_find_decoder(codecSelection.codecId); if (codec == nullptr) { - logging::error("stream", "FFmpeg did not provide an H.264 decoder"); + logging::error("stream", std::string("FFmpeg did not provide a ") + codecSelection.displayName + " decoder"); return -1; } - video_.codecName = "H.264"; + video_.codecName = codecSelection.displayName; video_.codecContext = avcodec_alloc_context3(codec); video_.decodedFrame = av_frame_alloc(); video_.convertedFrame = av_frame_alloc(); diff --git a/src/streaming/session.cpp b/src/streaming/session.cpp index 14f713e..65c7695 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -42,6 +42,7 @@ #include "src/streaming/ffmpeg_stream_backend.h" #include "src/streaming/stats_overlay.h" #include "src/streaming/stream_controls.h" +#include "src/streaming/video_codec_selection.h" #ifdef NXDK /** @@ -192,9 +193,11 @@ namespace { }; struct ResolvedStreamParameters { - VIDEO_MODE videoMode {}; + VIDEO_MODE presentationVideoMode {}; ///< Xbox presentation mode selected by user settings. + VIDEO_MODE videoMode {}; ///< Stream mode requested from the host. int fps = app::DEFAULT_STREAM_FRAMERATE; int bitrateKbps = app::DEFAULT_STREAM_BITRATE_KBPS; + app::VideoCodecSelection videoCodec = app::VideoCodecSelection::preferMpeg2; ///< Decoder preference resolved against host capabilities after launch. int packetSize = DEFAULT_PACKET_SIZE; int streamingRemotely = STREAM_CFG_AUTO; }; @@ -782,6 +785,29 @@ namespace { return std::clamp(settings.streamBitrateKbps, MIN_STREAM_BITRATE_KBPS, MAX_STREAM_BITRATE_KBPS); } + /** + * @brief Describe a codec preference for logs and waiting screens. + * + * @param selection Codec preference selected in settings. + * @return Human-readable codec preference. + */ + const char *describe_video_codec_selection(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 Resolve the effective stream parameters for the next session. * @@ -791,13 +817,24 @@ namespace { */ ResolvedStreamParameters resolve_stream_parameters(const VIDEO_MODE &fallbackVideoMode, const app::SettingsState &settings) { ResolvedStreamParameters resolved {}; - resolved.videoMode = select_effective_stream_video_mode(fallbackVideoMode, settings); + resolved.presentationVideoMode = select_effective_stream_video_mode(fallbackVideoMode, settings); + resolved.videoMode = resolved.presentationVideoMode; resolved.fps = select_stream_fps(settings); resolved.bitrateKbps = select_stream_bitrate_kbps(settings); + resolved.videoCodec = settings.video.codec; return resolved; } + /** + * @brief Prepare connection settings and resolve the initial codec choice against the host's capabilities. + * @param launchResult Host capabilities and endpoint returned by launch or resume. + * @param remoteInputKey Encryption key for controller and keyboard traffic. + * @param remoteInputIv Initialization vector for remote input encryption. + * @param outputVideoMode Current Xbox scanout mode. + * @param streamParameters Requested video mode, frame rate, bitrate, and codec. + * @param context Stream startup context receiving the resolved settings. + */ void configure_stream_start_context( const network::StreamLaunchResult &launchResult, const std::array &remoteInputKey, @@ -837,7 +874,7 @@ namespace { context->streamConfiguration.packetSize = streamParameters.packetSize; context->streamConfiguration.streamingRemotely = streamParameters.streamingRemotely; context->streamConfiguration.audioConfiguration = AUDIO_CONFIGURATION_STEREO; - context->streamConfiguration.supportedVideoFormats = VIDEO_FORMAT_H264; + context->streamConfiguration.supportedVideoFormats = streaming::supported_video_formats(streamParameters.videoCodec, launchResult.serverCodecModeSupport); context->streamConfiguration.clientRefreshRateX100 = select_client_refresh_rate_x100(outputVideoMode); context->streamConfiguration.colorSpace = COLORSPACE_REC_601; context->streamConfiguration.colorRange = COLOR_RANGE_LIMITED; @@ -1388,18 +1425,42 @@ namespace { return false; } - bool should_retry_stream_without_rtsp_session_url(const StreamStartContext &startContext, const StreamConnectionState &connectionState) { - return !connectionState.stopRequested.load() && !connectionState.connectionStarted.load() && connectionState.failedStage.load() == STAGE_RTSP_HANDSHAKE && !startContext.rtspSessionUrl.empty(); + /** + * @brief Check whether negotiation ended in RTSP before playback and without a local cancellation. + * @param connectionState Completed connection attempt. + * @return True when a retry is permissible. + */ + bool can_retry_rtsp_handshake(const StreamConnectionState &connectionState) { + return !connectionState.stopRequested.load() && !connectionState.connectionStarted.load() && connectionState.failedStage.load() == STAGE_RTSP_HANDSHAKE; } - bool run_stream_start_with_rtsp_fallback(const StreamStartAttemptContext &attempt, bool *rtspFallbackAttempted) { + /** + * @brief Start transport with one retry per applicable RTSP fallback. + * + * A failed preferred MPEG-2 handshake can retry with standard codec negotiation. + * + * @param attempt Stream startup state and UI resources. + * @param videoCodec User's requested codec selection. + * @param rtspFallbackAttempted Receives whether default RTSP discovery was attempted. + * @return True when the transport thread ran, including connection failures reported in the state. + */ + bool run_stream_start_with_rtsp_fallback(const StreamStartAttemptContext &attempt, app::VideoCodecSelection videoCodec, bool *rtspFallbackAttempted) { if (rtspFallbackAttempted != nullptr) { *rtspFallbackAttempted = false; } if (!run_stream_start_attempt(attempt)) { return false; } - if (!should_retry_stream_without_rtsp_session_url(attempt.startContext, attempt.connectionState)) { + if (auto &videoFormats = attempt.startContext.streamConfiguration.supportedVideoFormats; videoCodec == app::VideoCodecSelection::preferMpeg2 && videoFormats == VIDEO_FORMAT_MPEG2 && can_retry_rtsp_handshake(attempt.connectionState)) { + logging::warn("stream", "MPEG-2 handshake failed; retrying with standard codec negotiation"); + attempt.resources.mediaBackend.shutdown(); + videoFormats = streaming::supported_video_formats(app::VideoCodecSelection::autoDetect, attempt.startContext.serverInformation.serverCodecModeSupport); + reset_connection_state(&attempt.connectionState); + if (!run_stream_start_attempt(attempt)) { + return false; + } + } + if (!can_retry_rtsp_handshake(attempt.connectionState) || attempt.startContext.rtspSessionUrl.empty()) { return true; } @@ -1500,12 +1561,12 @@ namespace streaming { *returnController = nullptr; } const ResolvedStreamParameters streamParameters = resolve_stream_parameters(videoMode, settings); - const VIDEO_MODE streamPresentationVideoMode = select_stream_presentation_video_mode(videoMode, streamParameters.videoMode); + const VIDEO_MODE streamPresentationVideoMode = select_stream_presentation_video_mode(videoMode, streamParameters.presentationVideoMode); ScopedStreamVideoMode scopedStreamVideoMode(window, videoMode, streamPresentationVideoMode); const VIDEO_MODE &activeStreamVideoMode = scopedStreamVideoMode.active_video_mode(); StreamUiResources resources {}; - resources.statisticsEnabled = settings.presentation.showPerformanceStats; + resources.statisticsEnabled = settings.video.showPerformanceStats; if (std::string initializationError; !initialize_stream_ui_resources(window, activeStreamVideoMode, preferredController, &resources, &initializationError)) { assign_status_message(statusMessage, initializationError); logging::error("stream", initializationError); @@ -1564,10 +1625,10 @@ namespace streaming { ); logging::info( "stream", - std::string("Requested stream configuration | resolution=") + std::to_string(startContext.streamConfiguration.width) + "x" + std::to_string(startContext.streamConfiguration.height) + " | fps=" + std::to_string(startContext.streamConfiguration.fps) + " | bitrate=" + std::to_string(startContext.streamConfiguration.bitrate) + " kbps" + " | packetSize=" + std::to_string(startContext.streamConfiguration.packetSize) + " | networkProfile=" + describe_streaming_remotely_mode(startContext.streamConfiguration.streamingRemotely) + " | clientRefreshX100=" + std::to_string(startContext.streamConfiguration.clientRefreshRateX100) + std::string("Requested stream configuration | resolution=") + std::to_string(startContext.streamConfiguration.width) + "x" + std::to_string(startContext.streamConfiguration.height) + " | fps=" + std::to_string(startContext.streamConfiguration.fps) + " | bitrate=" + std::to_string(startContext.streamConfiguration.bitrate) + " kbps | decoder=" + describe_video_codec_selection(settings.video.codec) + " | packetSize=" + std::to_string(startContext.streamConfiguration.packetSize) + " | networkProfile=" + describe_streaming_remotely_mode(startContext.streamConfiguration.streamingRemotely) + " | clientRefreshX100=" + std::to_string(startContext.streamConfiguration.clientRefreshRateX100) ); resources.mediaBackend.set_audio_playback_enabled(settings.playAudioOnXbox); - resources.mediaBackend.set_gpu_presentation_enabled(settings.presentation.gpuPresentation); + resources.mediaBackend.set_gpu_presentation_enabled(settings.video.gpuPresentation); logging::info("stream", std::string("Xbox audio playback ") + (settings.playAudioOnXbox ? "enabled" : "disabled for lower decode latency")); logging::debug( "stream", @@ -1587,7 +1648,7 @@ namespace streaming { startContext.connectionCallbacks.logMessage = on_log_message; bool rtspFallbackAttempted = false; - if (const StreamStartAttemptContext startAttempt {host, app, startContext, connectionState, resources, statusMessage}; !run_stream_start_with_rtsp_fallback(startAttempt, &rtspFallbackAttempted)) { + if (const StreamStartAttemptContext startAttempt {host, app, startContext, connectionState, resources, statusMessage}; !run_stream_start_with_rtsp_fallback(startAttempt, settings.video.codec, &rtspFallbackAttempted)) { return false; } diff --git a/src/streaming/session.h b/src/streaming/session.h index ea56600..4a81bfd 100644 --- a/src/streaming/session.h +++ b/src/streaming/session.h @@ -32,7 +32,7 @@ namespace streaming { * * @param window Shared SDL window reused from the shell. * @param videoMode Active Xbox video mode. - * @param settings Active stream preferences; receives OSK placement changes for persistence on return to the shell. + * @param settings Active stream preferences, including codec selection; receives OSK placement changes for persistence on return to the shell. * @param host Selected paired host. * @param app Selected host app. * @param clientIdentity Paired client identity used for authenticated launch requests. diff --git a/src/streaming/video_codec_selection.cpp b/src/streaming/video_codec_selection.cpp new file mode 100644 index 0000000..f7fd191 --- /dev/null +++ b/src/streaming/video_codec_selection.cpp @@ -0,0 +1,26 @@ +/** + * @file src/streaming/video_codec_selection.cpp + * @brief Maps client codec preferences to shared decoder capability bits. + */ +#include "src/streaming/video_codec_selection.h" + +#include "third-party/moonlight-common-c/src/Limelight.h" + +namespace streaming { + int supported_video_formats(app::VideoCodecSelection selection, int serverCodecModeSupport) { + constexpr int ALL_VIDEO_FORMATS = VIDEO_FORMAT_H264 | VIDEO_FORMAT_MPEG2 | VIDEO_FORMAT_H263P; + switch (selection) { + case app::VideoCodecSelection::preferMpeg2: + return (serverCodecModeSupport & SCM_MPEG2) != 0 ? VIDEO_FORMAT_MPEG2 : ALL_VIDEO_FORMATS; + case app::VideoCodecSelection::h264: + return VIDEO_FORMAT_H264; + case app::VideoCodecSelection::mpeg2: + return VIDEO_FORMAT_MPEG2; + case app::VideoCodecSelection::h263p: + return VIDEO_FORMAT_H263P; + case app::VideoCodecSelection::autoDetect: + default: + return ALL_VIDEO_FORMATS; + } + } +} // namespace streaming diff --git a/src/streaming/video_codec_selection.h b/src/streaming/video_codec_selection.h new file mode 100644 index 0000000..3312d4c --- /dev/null +++ b/src/streaming/video_codec_selection.h @@ -0,0 +1,18 @@ +/** + * @file src/streaming/video_codec_selection.h + * @brief Declares the codec capabilities associated with client preferences. + */ +#pragma once + +#include "src/app/client_state.h" + +namespace streaming { + /** + * @brief Select the decoder capability mask for the initial negotiation attempt. + * + * @param selection Codec preference selected by the user. + * @param serverCodecModeSupport Codec capability mask advertised by the host. + * @return Moonlight video format mask used for negotiation. + */ + int supported_video_formats(app::VideoCodecSelection selection, int serverCodecModeSupport); +} // namespace streaming diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 14496ae..9b0f0ab 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -2506,12 +2506,13 @@ namespace { state.settings.preferredVideoModeSet, state.settings.streamFramerate, state.settings.streamBitrateKbps, + state.settings.video.codec, state.settings.playAudioOnPc, - state.settings.presentation.showPerformanceStats, + state.settings.video.showPerformanceStats, state.settings.playAudioOnXbox, state.settings.controls.screenshotDirectory, state.settings.controls.keyboardDockTop, - state.settings.presentation.gpuPresentation, + state.settings.video.gpuPresentation, }; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 541f231..a6f8019 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -82,6 +82,9 @@ target_link_options(${PROJECT_NAME} add_dependencies(${PROJECT_NAME} gtest gtest_main gmock gmock_main moonlight-common-c) +target_sources(${PROJECT_NAME} PRIVATE + "${CMAKE_SOURCE_DIR}/third-party/moonlight-common-c/src/VideoFormats.c") + include(GoogleTest) set(TEST_RUNTIME_PROPERTIES) if(WIN32 OR MINGW OR CMAKE_HOST_WIN32) diff --git a/tests/unit/app/client_state_test.cpp b/tests/unit/app/client_state_test.cpp index 8105c4f..787b3e3 100644 --- a/tests/unit/app/client_state_test.cpp +++ b/tests/unit/app/client_state_test.cpp @@ -185,7 +185,8 @@ namespace { EXPECT_EQ(state.settings.loggingLevel, logging::LogLevel::none); EXPECT_EQ(state.settings.streamFramerate, 60); EXPECT_EQ(state.settings.streamBitrateKbps, 3000); - EXPECT_FALSE(state.settings.presentation.gpuPresentation); + EXPECT_EQ(state.settings.video.codec, app::VideoCodecSelection::preferMpeg2); + EXPECT_FALSE(state.settings.video.gpuPresentation); } TEST(ClientStateTest, ReplacesHostsFromPersistenceWithoutMarkingThemDirty) { @@ -1477,7 +1478,7 @@ namespace { app::ClientState state = app::create_initial_state(); state.settings.streamBitrateKbps = 1500; ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); - constexpr std::array controls {"toggle-play-audio-on-pc", "toggle-play-audio-on-xbox", "toggle-show-performance-stats"}; + constexpr std::array controls {"toggle-play-audio-on-pc", "toggle-play-audio-on-xbox", "toggle-show-performance-stats", "cycle-video-codec"}; for (const char *control : controls) { ASSERT_TRUE(state.detailMenu.select_item_by_id(control)); app::handle_command(state, input::UiCommand::activate); @@ -1485,26 +1486,64 @@ namespace { } } - /** @brief Switch from the software default to GPU and back while preserving bitrate and statistics. */ + /** + * @brief Cycle codec choices independently of the bitrate, presenter, and statistics. + */ + TEST(ClientStateTest, DisplaySettingsCanForceVideoCodec) { + app::ClientState state = app::create_initial_state(); + state.settings.streamBitrateKbps = 4500; + state.settings.video.gpuPresentation = true; + state.settings.video.showPerformanceStats = true; + ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); + ASSERT_TRUE(state.detailMenu.select_item_by_id("cycle-video-codec")); + const std::array choices { + app::VideoCodecSelection::autoDetect, + app::VideoCodecSelection::h264, + app::VideoCodecSelection::mpeg2, + app::VideoCodecSelection::h263p, + app::VideoCodecSelection::preferMpeg2, + }; + const std::array labels {"Auto", "H.264", "MPEG-2/H.262", "H.263+", "Auto (Prefer MPEG-2)"}; + EXPECT_EQ(state.detailMenu.selected_item()->label, "Video Codec: Auto (Prefer MPEG-2)"); + for (std::size_t index = 0; index < choices.size(); ++index) { + const app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(update.navigation.activatedItemId, "cycle-video-codec"); + EXPECT_TRUE(update.persistence.settingsChanged); + EXPECT_EQ(state.settings.video.codec, choices[index]); + EXPECT_EQ(state.settings.streamBitrateKbps, 4500); + EXPECT_TRUE(state.settings.video.gpuPresentation); + EXPECT_TRUE(state.settings.video.showPerformanceStats); + EXPECT_EQ(state.shell.statusMessage, std::string("Video codec set to ") + labels[index]); + ASSERT_NE(state.detailMenu.selected_item(), nullptr); + EXPECT_EQ(state.detailMenu.selected_item()->label, std::string("Video Codec: ") + labels[index]); + } + state.settings.video.codec = static_cast(-1); + app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(state.settings.video.codec, app::VideoCodecSelection::preferMpeg2); + } + + /** @brief Switch from the software default to GPU and back while preserving bitrate, codec, and statistics. */ TEST(ClientStateTest, DisplaySettingsCanSelectGpuPresentation) { auto state = app::create_initial_state(); ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); ASSERT_TRUE(state.detailMenu.select_item_by_id("toggle-gpu-presentation")); - ASSERT_FALSE(state.settings.presentation.gpuPresentation); + ASSERT_FALSE(state.settings.video.gpuPresentation); state.settings.streamBitrateKbps = 4500; - state.settings.presentation.showPerformanceStats = true; + state.settings.video.codec = app::VideoCodecSelection::mpeg2; + state.settings.video.showPerformanceStats = true; EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).persistence.settingsChanged); - EXPECT_TRUE(state.settings.presentation.gpuPresentation); + EXPECT_TRUE(state.settings.video.gpuPresentation); EXPECT_TRUE(state.settings.dirty); ASSERT_NE(state.detailMenu.selected_item(), nullptr); EXPECT_EQ(state.detailMenu.selected_item()->label, "Video Presentation: GPU"); EXPECT_EQ(state.settings.streamBitrateKbps, 4500); - EXPECT_TRUE(state.settings.presentation.showPerformanceStats); + EXPECT_EQ(state.settings.video.codec, app::VideoCodecSelection::mpeg2); + EXPECT_TRUE(state.settings.video.showPerformanceStats); EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).persistence.settingsChanged); - EXPECT_FALSE(state.settings.presentation.gpuPresentation); - EXPECT_TRUE(state.settings.presentation.showPerformanceStats); + EXPECT_FALSE(state.settings.video.gpuPresentation); + EXPECT_TRUE(state.settings.video.showPerformanceStats); ASSERT_NE(state.detailMenu.selected_item(), nullptr); EXPECT_EQ(state.detailMenu.selected_item()->label, "Video Presentation: Software"); } @@ -1513,8 +1552,8 @@ namespace { TEST(ClientStateTest, DisplaySettingsCanToggleXboxAudioAndPerformanceStats) { app::ClientState state = app::create_initial_state(); ASSERT_TRUE(state.settings.playAudioOnXbox); - ASSERT_FALSE(state.settings.presentation.showPerformanceStats); - state.settings.presentation.gpuPresentation = true; + ASSERT_FALSE(state.settings.video.showPerformanceStats); + state.settings.video.gpuPresentation = true; ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); @@ -1531,8 +1570,8 @@ namespace { update = app::handle_command(state, input::UiCommand::activate); EXPECT_EQ(update.navigation.activatedItemId, "toggle-show-performance-stats"); EXPECT_TRUE(update.persistence.settingsChanged); - EXPECT_TRUE(state.settings.presentation.showPerformanceStats); - EXPECT_TRUE(state.settings.presentation.gpuPresentation); + EXPECT_TRUE(state.settings.video.showPerformanceStats); + EXPECT_TRUE(state.settings.video.gpuPresentation); EXPECT_EQ(state.shell.statusMessage, "Performance stats enabled"); ASSERT_NE(state.detailMenu.selected_item(), nullptr); EXPECT_EQ(state.detailMenu.selected_item()->label, "Performance Stats: On"); diff --git a/tests/unit/app/directory_picker_test.cpp b/tests/unit/app/directory_picker_test.cpp index 2b8915b..60e1eec 100644 --- a/tests/unit/app/directory_picker_test.cpp +++ b/tests/unit/app/directory_picker_test.cpp @@ -240,7 +240,7 @@ TEST(DirectoryPickerTest, BuildsScrollableFoldersAndControllerOrKeyboardNameEntr } /** - * @brief Storage owns screenshot destinations while Display retains its original stream and presentation options. + * @brief Storage owns screenshot destinations while Display retains stream options, including codec selection. */ TEST(DirectoryPickerTest, KeepsStorageSeparateFromTheExistingDisplayCategory) { auto state = open_picker("F:\\Screenshots"); @@ -258,6 +258,7 @@ TEST(DirectoryPickerTest, KeepsStorageSeparateFromTheExistingDisplayCategory) { app::handle_command(state, input::UiCommand::activate); EXPECT_EQ(state.settings.selectedCategory, app::SettingsCategory::display); EXPECT_EQ(state.detailMenu.selected_item()->id, "cycle-stream-video-mode"); + EXPECT_TRUE(state.detailMenu.select_item_by_id("cycle-video-codec")); EXPECT_TRUE(state.detailMenu.select_item_by_id("toggle-gpu-presentation")); EXPECT_FALSE(state.detailMenu.select_item_by_id("screenshot-directory")); EXPECT_EQ(state.settings.controls.screenshotDirectory, "F:\\Screenshots"); diff --git a/tests/unit/app/settings_storage_test.cpp b/tests/unit/app/settings_storage_test.cpp index 8db2074..5fb08eb 100644 --- a/tests/unit/app/settings_storage_test.cpp +++ b/tests/unit/app/settings_storage_test.cpp @@ -42,6 +42,26 @@ namespace { } }; + /** + * @brief Codec migration preserves canonical precedence, screenshot storage and keyboard placement. + */ + TEST_F(SettingsStorageTest, LoadsCanonicalAndLegacyCodecKeysWithoutLosingPreferences) { + write_text_file(settingsPath, "[streaming]\nvideo_decoder = \"mpeg2\"\n"); + EXPECT_EQ(app::load_app_settings(settingsPath).settings.videoCodec, app::VideoCodecSelection::mpeg2); + write_text_file(settingsPath, "[streaming]\nvideo_codec = \"h263p\"\nvideo_decoder = \"h264\"\nscreenshot_directory = 'E:\\Shots'\nkeyboard_dock_top = true\n"); + const auto loaded = app::load_app_settings(settingsPath); + EXPECT_EQ(loaded.settings.videoCodec, app::VideoCodecSelection::h263p); + EXPECT_EQ(loaded.settings.screenshotDirectory, R"(E:\Shots)"); + EXPECT_TRUE(loaded.settings.keyboardDockTop); + EXPECT_TRUE(loaded.warnings.empty()); + ASSERT_TRUE(app::save_app_settings(loaded.settings, settingsPath).success); + const auto reloaded = app::load_app_settings(settingsPath); + EXPECT_EQ(reloaded.settings.videoCodec, app::VideoCodecSelection::h263p); + EXPECT_EQ(reloaded.settings.screenshotDirectory, R"(E:\Shots)"); + EXPECT_TRUE(reloaded.settings.keyboardDockTop); + EXPECT_TRUE(reloaded.warnings.empty()); + } + TEST_F(SettingsStorageTest, SavesAndLoadsTomlSettings) { const app::AppSettings savedSettings { logging::LogLevel::debug, @@ -51,6 +71,7 @@ namespace { true, 24, 2500, + app::VideoCodecSelection::mpeg2, true, true, true, @@ -71,6 +92,7 @@ namespace { EXPECT_EQ(loadResult.settings.preferredVideoMode.refresh, 60); EXPECT_EQ(loadResult.settings.streamFramerate, 24); EXPECT_EQ(loadResult.settings.streamBitrateKbps, 2500); + EXPECT_EQ(loadResult.settings.videoCodec, app::VideoCodecSelection::mpeg2); EXPECT_TRUE(loadResult.settings.playAudioOnPc); EXPECT_TRUE(loadResult.settings.showPerformanceStats); EXPECT_TRUE(loadResult.settings.playAudioOnXbox); @@ -86,6 +108,7 @@ namespace { true, 15, 500, + app::VideoCodecSelection::h264, false, false, false, @@ -104,6 +127,7 @@ namespace { EXPECT_EQ(loadResult.settings.preferredVideoMode.refresh, 60); EXPECT_EQ(loadResult.settings.streamFramerate, 15); EXPECT_EQ(loadResult.settings.streamBitrateKbps, 500); + EXPECT_EQ(loadResult.settings.videoCodec, app::VideoCodecSelection::h264); EXPECT_FALSE(loadResult.settings.playAudioOnXbox); } @@ -130,6 +154,17 @@ namespace { EXPECT_NE(invalid.warnings.front().find("streaming.gpu_presentation"), std::string::npos); } + /** @brief The new default codec preference round-trips while a saved Auto preference remains valid. */ + TEST_F(SettingsStorageTest, SavesMpeg2PreferenceAndPreservesStandardAuto) { + app::AppSettings settings; + ASSERT_TRUE(app::save_app_settings(settings, settingsPath).success); + const auto preferred = app::load_app_settings(settingsPath); + EXPECT_EQ(preferred.settings.videoCodec, app::VideoCodecSelection::preferMpeg2); + EXPECT_TRUE(preferred.warnings.empty()); + write_text_file(settingsPath, "[streaming]\nvideo_codec = 'auto'\n"); + EXPECT_EQ(app::load_app_settings(settingsPath).settings.videoCodec, app::VideoCodecSelection::autoDetect); + } + /** @brief A partial settings file derives bitrate from the saved resolution and active frame rate. */ TEST_F(SettingsStorageTest, DerivesMissingBitrateFromSavedStreamMode) { write_text_file(settingsPath, "[streaming]\nvideo_width = 1280\nvideo_height = 720\nvideo_mode_selected = true\n"); @@ -149,6 +184,7 @@ namespace { EXPECT_FALSE(loadResult.settings.preferredVideoModeSet); EXPECT_EQ(loadResult.settings.streamFramerate, 60); EXPECT_EQ(loadResult.settings.streamBitrateKbps, 3000); + EXPECT_EQ(loadResult.settings.videoCodec, app::VideoCodecSelection::preferMpeg2); EXPECT_FALSE(loadResult.settings.gpuPresentation); EXPECT_FALSE(loadResult.settings.playAudioOnPc); EXPECT_FALSE(loadResult.settings.showPerformanceStats); @@ -169,6 +205,7 @@ namespace { "[streaming]\n" "video_width = \"wide\"\n" "fps = \"fast\"\n" + "video_decoder = \"vp9\"\n" "play_audio_on_pc = \"sometimes\"\n" "play_audio_on_xbox = \"sometimes\"\n" ); @@ -182,6 +219,7 @@ namespace { EXPECT_EQ(loadResult.settings.logViewerPlacement, app::LogViewerPlacement::full); EXPECT_FALSE(loadResult.settings.preferredVideoModeSet); EXPECT_EQ(loadResult.settings.streamFramerate, 60); + EXPECT_EQ(loadResult.settings.videoCodec, app::VideoCodecSelection::preferMpeg2); EXPECT_FALSE(loadResult.settings.playAudioOnPc); EXPECT_TRUE(loadResult.settings.playAudioOnXbox); } diff --git a/tests/unit/streaming/video_codec_selection_test.cpp b/tests/unit/streaming/video_codec_selection_test.cpp new file mode 100644 index 0000000..0b37a07 --- /dev/null +++ b/tests/unit/streaming/video_codec_selection_test.cpp @@ -0,0 +1,38 @@ +/** + * @file tests/unit/streaming/video_codec_selection_test.cpp + * @brief Checks automatic capabilities and strict codec requests. + */ +// test header include +#include "src/streaming/video_codec_selection.h" + +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + +#include + +/** + * @brief Automatic mode advertises every decoder implemented by this client. + */ +TEST(VideoCodecSelectionTest, AutomaticModeAdvertisesSupportedCodecs) { + const int formats = VIDEO_FORMAT_H264 | VIDEO_FORMAT_MPEG2 | VIDEO_FORMAT_H263P; + EXPECT_EQ(streaming::supported_video_formats(app::VideoCodecSelection::autoDetect, SCM_H264 | SCM_MPEG2), formats); + EXPECT_EQ(streaming::supported_video_formats(static_cast(-1), 0), formats); +} + +/** @brief The default tries MPEG-2 on advertising hosts and preserves negotiation with other hosts. */ +TEST(VideoCodecSelectionTest, Mpeg2PreferenceRequiresHostSupport) { + const auto preference = app::VideoCodecSelection::preferMpeg2; + const int formats = VIDEO_FORMAT_H264 | VIDEO_FORMAT_MPEG2 | VIDEO_FORMAT_H263P; + EXPECT_EQ(streaming::supported_video_formats(preference, SCM_H264 | SCM_MPEG2 | SCM_H263P), VIDEO_FORMAT_MPEG2); + EXPECT_EQ(streaming::supported_video_formats(preference, SCM_H264 | SCM_H263P), formats); + EXPECT_EQ(streaming::supported_video_formats(preference, 0), formats); +} + +/** + * @brief Explicit choices cannot negotiate a different codec as a fallback. + */ +TEST(VideoCodecSelectionTest, ExplicitChoicesAdvertiseOnlyTheRequestedCodec) { + EXPECT_EQ(streaming::supported_video_formats(app::VideoCodecSelection::h264, SCM_MPEG2), VIDEO_FORMAT_H264); + EXPECT_EQ(streaming::supported_video_formats(app::VideoCodecSelection::mpeg2, SCM_H264), VIDEO_FORMAT_MPEG2); + EXPECT_EQ(streaming::supported_video_formats(app::VideoCodecSelection::h263p, SCM_H264), VIDEO_FORMAT_H263P); +} diff --git a/tests/unit/streaming/video_formats_test.cpp b/tests/unit/streaming/video_formats_test.cpp new file mode 100644 index 0000000..68c5454 --- /dev/null +++ b/tests/unit/streaming/video_formats_test.cpp @@ -0,0 +1,108 @@ +/** + * @file tests/unit/streaming/video_formats_test.cpp + * @brief Regression coverage for shared codec negotiation and wire identifiers. + */ +// test header include +extern "C" { +#include "third-party/moonlight-common-c/src/VideoFormats.h" +} + +// standard includes +#include +#include + +// lib includes +#include + +namespace { + constexpr char ALL_CODECS[] = "AV1/90000\nsprop-parameter-sets=AAAAAU\nMPV/90000\nH263-1998/90000\n"; ///< Host SDP advertising every codec. + constexpr int ALL_FORMATS = VIDEO_FORMAT_MASK_AV1 | VIDEO_FORMAT_MASK_H265 | VIDEO_FORMAT_MASK_H264 | VIDEO_FORMAT_MPEG2 | VIDEO_FORMAT_H263P; ///< All known decoder formats. + constexpr int ALL_MODES = SCM_MASK_AV1 | SCM_MASK_HEVC | SCM_MASK_H264 | SCM_MPEG2 | SCM_H263P; ///< All known host codec capabilities. +} // namespace + +/** + * @brief Prefer each modern codec profile before either legacy codec. + */ +TEST(VideoFormatNegotiation, PreservesModernCodecPriorityWhenLegacyCodecsAreAvailable) { + const std::array order = {VIDEO_FORMAT_AV1_HIGH10_444, VIDEO_FORMAT_AV1_MAIN10, VIDEO_FORMAT_AV1_HIGH8_444, VIDEO_FORMAT_AV1_MAIN8, VIDEO_FORMAT_H265_REXT10_444, VIDEO_FORMAT_H265_MAIN10, VIDEO_FORMAT_H265_REXT8_444, VIDEO_FORMAT_H265, VIDEO_FORMAT_H264_HIGH8_444, VIDEO_FORMAT_H264, VIDEO_FORMAT_MPEG2, VIDEO_FORMAT_H263P}; + int supported = ALL_FORMATS; + for (int expected : order) { + EXPECT_EQ(chooseVideoFormat(supported, ALL_MODES, ALL_CODECS), expected); + supported &= ~expected; + } + EXPECT_EQ(chooseVideoFormat(supported, ALL_MODES, ALL_CODECS), 0); +} + +/** + * @brief Select only mutually supported profiles from the client's strict request mask. + */ +TEST(VideoFormatNegotiation, ExplicitRequestsNeverFallBackToAnUnsupportedCodec) { + for (int format : {VIDEO_FORMAT_H264, VIDEO_FORMAT_H265, VIDEO_FORMAT_AV1_MAIN8, VIDEO_FORMAT_MPEG2, VIDEO_FORMAT_H263P}) { + EXPECT_EQ(chooseVideoFormat(format, ALL_MODES, ALL_CODECS), format); + } + EXPECT_EQ(chooseVideoFormat(VIDEO_FORMAT_MPEG2, SCM_H264, ALL_CODECS), 0); + EXPECT_EQ(chooseVideoFormat(VIDEO_FORMAT_H263P, SCM_H264, ALL_CODECS), 0); + EXPECT_EQ(chooseVideoFormat(VIDEO_FORMAT_AV1_MAIN10, SCM_AV1_MAIN8, ALL_CODECS), 0); + EXPECT_EQ(chooseVideoFormat(VIDEO_FORMAT_H265_MAIN10, SCM_HEVC, ALL_CODECS), 0); + EXPECT_EQ(chooseVideoFormat(VIDEO_FORMAT_H264_HIGH8_444, SCM_H264, ALL_CODECS), 0); +} + +/** + * @brief Require matching host capabilities and SDP markers for legacy codecs. + */ +TEST(VideoFormatNegotiation, RequiresHostMetadataAndSdpForLegacyCodecs) { + const std::array aliases = { + std::pair {VIDEO_FORMAT_MPEG2, "MPV/90000"}, + std::pair {VIDEO_FORMAT_MPEG2, "MPEG2/90000"}, + std::pair {VIDEO_FORMAT_MPEG2, "MPEG-2/90000"}, + std::pair {VIDEO_FORMAT_H263P, "H263-1998/90000"}, + std::pair {VIDEO_FORMAT_H263P, "H263-2000/90000"}, + }; + for (const auto &[format, sdp] : aliases) { + EXPECT_EQ(chooseVideoFormat(format, ALL_MODES, sdp), format); + EXPECT_EQ(chooseVideoFormat(format, 0, sdp), 0); + EXPECT_EQ(chooseVideoFormat(format, ALL_MODES, ""), 0); + } + EXPECT_EQ(chooseVideoFormat(ALL_FORMATS, ALL_MODES, nullptr), 0); +} + +/** + * @brief Preserve H.264 for old hosts while respecting explicit legacy-only advertisements. + */ +TEST(VideoFormatNegotiation, SupportsLegacyHostsAndLegacyOnlyHosts) { + EXPECT_EQ(chooseVideoFormat(ALL_FORMATS, 0, ""), VIDEO_FORMAT_H264); + EXPECT_EQ(chooseVideoFormat(ALL_FORMATS, 0, "sprop-parameter-sets=AAAAAU"), VIDEO_FORMAT_H265); + EXPECT_EQ(chooseVideoFormat(ALL_FORMATS, 0, ALL_CODECS), VIDEO_FORMAT_AV1_MAIN8); + EXPECT_EQ(chooseVideoFormat(VIDEO_FORMAT_H265_MAIN10, 0, ALL_CODECS), 0); + EXPECT_EQ(chooseVideoFormat(VIDEO_FORMAT_AV1_MAIN10, 0, ALL_CODECS), 0); + EXPECT_EQ(chooseVideoFormat(ALL_FORMATS, SCM_MPEG2, ALL_CODECS), VIDEO_FORMAT_MPEG2); + EXPECT_EQ(chooseVideoFormat(ALL_FORMATS, SCM_H263P, ALL_CODECS), VIDEO_FORMAT_H263P); + EXPECT_EQ(chooseVideoFormat(ALL_FORMATS, SCM_HEVC, ""), 0); + EXPECT_EQ(chooseVideoFormat(ALL_FORMATS, SCM_AV1_MAIN8, ""), 0); +} + +/** + * @brief Map every known codec profile and reject combinations and reserved bits. + */ +TEST(VideoFormatNegotiation, EncodesOnlyValidSingleNegotiatedFormats) { + const std::array mappings = { + std::pair {VIDEO_FORMAT_H264, 0}, + std::pair {VIDEO_FORMAT_H264_HIGH8_444, 0}, + std::pair {VIDEO_FORMAT_H265, 1}, + std::pair {VIDEO_FORMAT_H265_MAIN10, 1}, + std::pair {VIDEO_FORMAT_H265_REXT8_444, 1}, + std::pair {VIDEO_FORMAT_H265_REXT10_444, 1}, + std::pair {VIDEO_FORMAT_AV1_MAIN8, 2}, + std::pair {VIDEO_FORMAT_AV1_MAIN10, 2}, + std::pair {VIDEO_FORMAT_AV1_HIGH8_444, 2}, + std::pair {VIDEO_FORMAT_AV1_HIGH10_444, 2}, + std::pair {VIDEO_FORMAT_MPEG2, 3}, + std::pair {VIDEO_FORMAT_H263P, 4}, + }; + for (const auto &[format, wire] : mappings) { + EXPECT_EQ(getVideoBitStreamFormat(format), wire); + } + for (int invalid : {0, -1, 0x2, 0x8, 0x40000000, VIDEO_FORMAT_H264 | VIDEO_FORMAT_MPEG2}) { + EXPECT_EQ(getVideoBitStreamFormat(invalid), -1); + } +} diff --git a/tests/unit/ui/shell_view_test.cpp b/tests/unit/ui/shell_view_test.cpp index e6ec2de..e2cf338 100644 --- a/tests/unit/ui/shell_view_test.cpp +++ b/tests/unit/ui/shell_view_test.cpp @@ -376,6 +376,34 @@ namespace { EXPECT_EQ(viewModel.content.selectedMenuRowDescription, "Delete only this saved file from disk while leaving the rest of the Moonlight data intact."); } + /** + * @brief Display settings show a fixed codec choice and explain the available negotiation modes. + */ + TEST(ShellViewTest, ShowsSelectedCodecInDisplaySettings) { + auto state = app::create_initial_state(); + state.settings.video.codec = app::VideoCodecSelection::mpeg2; + app::handle_command(state, input::UiCommand::move_left); + app::handle_command(state, input::UiCommand::move_left); + app::handle_command(state, input::UiCommand::activate); + app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::activate); + ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::display); + ASSERT_TRUE(state.detailMenu.select_item_by_id("cycle-video-codec")); + + const auto view = ui::build_shell_view_model(state, {}); + EXPECT_EQ(view.content.selectedMenuRowLabel, "Video Codec: MPEG-2/H.262"); + EXPECT_EQ(view.content.selectedMenuRowDescription, "Prefer MPEG-2 for faster decoding, use standard automatic negotiation, or request a specific codec."); + bool found = false; + for (const auto &row : view.content.detailMenuRows) { + if (row.id == "cycle-video-codec") { + EXPECT_EQ(row.label, "Video Codec: MPEG-2/H.262"); + EXPECT_TRUE(row.selected); + found = true; + } + } + EXPECT_TRUE(found); + } + TEST(ShellViewTest, ShowsSettingDescriptionsForFocusedOptions) { app::ClientState state = app::create_initial_state(); app::handle_command(state, input::UiCommand::move_left); diff --git a/third-party/moonlight-common-c b/third-party/moonlight-common-c index 94c65fd..ed6b2da 160000 --- a/third-party/moonlight-common-c +++ b/third-party/moonlight-common-c @@ -1 +1 @@ -Subproject commit 94c65fd364d17b223f00a8763c9a5d94a66cb8dd +Subproject commit ed6b2da643e5c16c315c4e4a93f0ac601c5cb32c