diff --git a/CMakeLists.txt b/CMakeLists.txt index ad789bd..0a851a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Export compile commands for IDE set(MOONLIGHT_BUILD_KIND "HOST" CACHE STRING "Internal Moonlight build mode") set_property(CACHE MOONLIGHT_BUILD_KIND PROPERTY STRINGS HOST XBOX) option(MOONLIGHT_BUILD_VIDEO_EXPERIMENTS "Build standalone GPU presentation and MPEG-2 sampling experiments" OFF) +option(MOONLIGHT_BUILD_AUDIO_EXPERIMENTS "Build the standalone Opus playback probe" OFF) option(MOONLIGHT_INPUT_DIAGNOSTICS "Display digital controller state while diagnosing stream input" OFF) if(MOONLIGHT_BUILD_KIND STREQUAL "XBOX") diff --git a/README.md b/README.md index 470749e..0f7cc5e 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,18 @@ framebuffer when available and performed better in xemu. **GPU** uses NV2A color with automatic software fallback. The setting is saved as `streaming.gpu_presentation` and both paths are available in the same build. +## Stream audio + +Choose **Settings → Audio → Audio Mode** before starting or resuming a stream. **Stereo** keeps +left and right separate and is the default. **Mono** mixes the stereo stream locally and plays the +same signal through both outputs. The selection is saved as `streaming.audio_mode` with a value +of `stereo` or `mono`. + +**Play Audio on Xbox** defaults to **On**. Turn it off to skip local audio decoding and playback. +**Play Audio on PC** defaults to **Off**. Turn it on to request playback on the host during the stream. +Saved choices take precedence. To check Xbox audio without a host, use the +[standalone audio probe](experiments/README.md#audio-playback-probe). + ## Streaming controls and statistics The default stream profile is **640×480 at 60 FPS and 3 Mbps**. Saved settings take precedence. @@ -263,11 +275,12 @@ For host and Xbox builds, CMake checks whether the compiler supports Clang's `un cmake -S . -B cmake-build-release -DBUILD_DOCS=OFF -DCMAKE_BUILD_TYPE=Release ``` -Video build settings: +Optional Xbox build settings: | CMake option | Default | Purpose | | ----------------------------------- | ------- | ---------------------------------------------------------------------------------------------- | -| `MOONLIGHT_BUILD_VIDEO_EXPERIMENTS` | `OFF` | Build the standalone probes described in [Video experiments](experiments/README.md). | +| `MOONLIGHT_BUILD_VIDEO_EXPERIMENTS` | `OFF` | Build the standalone [video probes](experiments/README.md). | +| `MOONLIGHT_BUILD_AUDIO_EXPERIMENTS` | `OFF` | Build the standalone [audio playback probe](experiments/README.md#audio-playback-probe). | | `MOONLIGHT_INPUT_DIAGNOSTICS` | `OFF` | Show controller state and event counters. Select Software presentation to see the debug strip. | Pass these options when configuring. Existing build directories retain cached build options until @@ -425,19 +438,18 @@ scripts\setup-xemu.cmd --skip-support-files - [x] Game/App Selection - [x] Host Details - [x] App Details - - [ ] Pause/Hotkey overlay + - [x] Pause/Hotkey overlay - Streaming - [x] Video - https://www.xbmc4xbox.org.uk/wiki/XBMC_Features_and_Supported_Formats#Xbox_supported_video_formats_and_resolutions - [ ] Audio - - [ ] Mono - - [ ] Stereo - - [ ] 5.1 Surround - - [ ] 7.1 Surround + - [x] Mono + - [x] Stereo + - [ ] Dolby Digital 5.1 output over optical S/PDIF - Input - [x] Gamepad Input - - [ ] Keyboard Input - - [ ] Mouse Input - - [ ] Mouse Emulation via Gamepad + - [x] Keyboard Input + - [x] Mouse Input + - [x] Mouse Emulation via Gamepad - Misc. - [x] Save config and pairing states - [x] Host pairing diff --git a/cmake/nxdk.cmake b/cmake/nxdk.cmake index 210e40b..e492c29 100644 --- a/cmake/nxdk.cmake +++ b/cmake/nxdk.cmake @@ -380,6 +380,7 @@ function(moonlight_add_xbox_build) -DMOONLIGHT_TOOLCHAIN_FILE:FILEPATH=${toolchain_file} -DMOONLIGHT_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} -DMOONLIGHT_BUILD_VIDEO_EXPERIMENTS:BOOL=${MOONLIGHT_BUILD_VIDEO_EXPERIMENTS} + -DMOONLIGHT_BUILD_AUDIO_EXPERIMENTS:BOOL=${MOONLIGHT_BUILD_AUDIO_EXPERIMENTS} -DMOONLIGHT_INPUT_DIAGNOSTICS:BOOL=${MOONLIGHT_INPUT_DIAGNOSTICS} -P "${driver_script}" ) diff --git a/cmake/run-child-build.cmake b/cmake/run-child-build.cmake index 73a1088..7b5871a 100644 --- a/cmake/run-child-build.cmake +++ b/cmake/run-child-build.cmake @@ -29,6 +29,7 @@ if(MOONLIGHT_COMMAND_MODE STREQUAL "configure") -B "${MOONLIGHT_BINARY_DIR}" -DMOONLIGHT_BUILD_KIND:STRING=XBOX "-DMOONLIGHT_BUILD_VIDEO_EXPERIMENTS:BOOL=${MOONLIGHT_BUILD_VIDEO_EXPERIMENTS}" + "-DMOONLIGHT_BUILD_AUDIO_EXPERIMENTS:BOOL=${MOONLIGHT_BUILD_AUDIO_EXPERIMENTS}" "-DMOONLIGHT_INPUT_DIAGNOSTICS:BOOL=${MOONLIGHT_INPUT_DIAGNOSTICS}" "-DMOONLIGHT_NXDK_DIR:PATH=${MOONLIGHT_NXDK_SOURCE_DIR}" "-DMOONLIGHT_NXDK_BUILD_DIR:PATH=${MOONLIGHT_NXDK_DIR}" diff --git a/cmake/sources.cmake b/cmake/sources.cmake index 00ded9a..89ca355 100644 --- a/cmake/sources.cmake +++ b/cmake/sources.cmake @@ -16,6 +16,7 @@ list(REMOVE_ITEM MOONLIGHT_SOURCES set(MOONLIGHT_TEST_EXCLUDED_SOURCES "${MOONLIGHT_SOURCE_ROOT}/src/input/shell_controller.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/main.cpp" + "${MOONLIGHT_SOURCE_ROOT}/src/streaming/audio_player.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/splash/splash_screen.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/streaming/ffmpeg_stream_backend.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/streaming/ffmpeg_video_frame.cpp" diff --git a/cmake/stage-lucide.cmake b/cmake/stage-lucide.cmake index 514bedc..e084979 100644 --- a/cmake/stage-lucide.cmake +++ b/cmake/stage-lucide.cmake @@ -30,6 +30,7 @@ foreach(icon IN ITEMS scroll-text settings share-2 + volume-2 ) set(_icon_source "${LUCIDE_SOURCE_DIR}/icons/${icon}.svg") if(NOT EXISTS "${_icon_source}") diff --git a/cmake/xbox-build.cmake b/cmake/xbox-build.cmake index 64d204b..35f162a 100644 --- a/cmake/xbox-build.cmake +++ b/cmake/xbox-build.cmake @@ -147,6 +147,37 @@ add_custom_target(xbe_iso ALL ) add_dependencies(xbe_iso cxbe_convert) +if(MOONLIGHT_BUILD_AUDIO_EXPERIMENTS) + add_executable(audio_playback_probe + "${CMAKE_SOURCE_DIR}/experiments/audio_playback_probe.cpp" + "${CMAKE_SOURCE_DIR}/src/logging/logger.cpp" + "${CMAKE_SOURCE_DIR}/src/streaming/audio_configuration.cpp" + "${CMAKE_SOURCE_DIR}/src/streaming/audio_player.cpp" + "${CMAKE_SOURCE_DIR}/src/_nxdk_compat/fenv_compat.cpp") + target_include_directories(audio_playback_probe PRIVATE + "${CMAKE_SOURCE_DIR}" + "${MOONLIGHT_FFMPEG_INCLUDE_DIR}") + target_compile_definitions(audio_playback_probe PRIVATE NXDK XBOX) + target_compile_options(audio_playback_probe PRIVATE -std=gnu++17) + target_link_options(audio_playback_probe PRIVATE /safeseh:no) + target_link_libraries(audio_playback_probe PRIVATE + ${MOONLIGHT_FFMPEG_LIBRARIES} + NXDK::NXDK NXDK::NXDK_CXX NXDK::SDL2 moonlight_nxdk_compat) + set(_audio_playback_probe_xbe_dir "${CMAKE_CURRENT_BINARY_DIR}/audio-playback-probe") + file(MAKE_DIRECTORY "${_audio_playback_probe_xbe_dir}") + add_custom_target(audio_playback_probe_iso ALL + COMMAND "${NXDK_DIR}/tools/cxbe/cxbe" + "-OUT:${_audio_playback_probe_xbe_dir}/default.xbe" + "-TITLE:Opus audio playback probe" + "$" + COMMAND "${NXDK_DIR}/tools/extract-xiso/build/extract-xiso" + -c "${_audio_playback_probe_xbe_dir}" "AudioPlaybackProbe.iso" + DEPENDS audio_playback_probe + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + COMMENT "Package the standalone Opus playback probe" + VERBATIM) +endif() + if(MOONLIGHT_BUILD_VIDEO_EXPERIMENTS) add_executable(pvideo_presentation_probe "${CMAKE_SOURCE_DIR}/experiments/pvideo_presentation_probe.cpp" diff --git a/docs/images/screenshots/04-settings.png b/docs/images/screenshots/04-settings.png index 8ae02cc..52c8f56 100644 Binary files a/docs/images/screenshots/04-settings.png and b/docs/images/screenshots/04-settings.png differ diff --git a/experiments/README.md b/experiments/README.md index a13c814..281853e 100644 --- a/experiments/README.md +++ b/experiments/README.md @@ -1,14 +1,14 @@ -# Video experiments +# Xbox experiments -These optional Xbox programs exercise video presentation and fractional sampling independently -of a streaming host. They use the production presenter and are disabled in the default build. +These optional Xbox programs exercise video presentation, fractional sampling and audio playback +independently of a streaming host. They share production code and are disabled in the default build. ## Build and run From the repository root, using the normal build prerequisites and toolchain: ```bash -cmake -S . -B cmake-build-experiments -DCMAKE_BUILD_TYPE=Release -DBUILD_DOCS=OFF -DMOONLIGHT_BUILD_VIDEO_EXPERIMENTS=ON +cmake -S . -B cmake-build-experiments -DCMAKE_BUILD_TYPE=Release -DBUILD_DOCS=OFF -DMOONLIGHT_BUILD_VIDEO_EXPERIMENTS=ON -DMOONLIGHT_BUILD_AUDIO_EXPERIMENTS=ON cmake --build cmake-build-experiments ``` @@ -19,6 +19,7 @@ The Xbox child build selects Unix Makefiles automatically. The generated files a | ----------------- | ---------------------------------------------------------- | -------------------------------------------------------------------- | | Presentation | `cmake-build-experiments/xbox/PvideoPresentationProbe.iso` | `cmake-build-experiments/xbox/pvideo-presentation-probe/default.xbe` | | MPEG-2 prediction | `cmake-build-experiments/xbox/Mpeg2MotionProbe.iso` | `cmake-build-experiments/xbox/mpeg2-motion-probe/default.xbe` | +| Audio playback | `cmake-build-experiments/xbox/AudioPlaybackProbe.iso` | `cmake-build-experiments/xbox/audio-playback-probe/default.xbe` | Launch an ISO with the repository's configured xemu installation, for example: @@ -27,7 +28,7 @@ Launch an ISO with the repository's configured xemu installation, for example: ``` From a Windows command prompt, use `scripts\run-xemu.cmd --iso` with the same ISO path. -The probes use GPU presentation directly. The streaming client selects its path in +The video probes use GPU presentation directly. The streaming client selects its path in **Settings → Display → Video Presentation**. ## Presentation probe @@ -50,3 +51,43 @@ This is a visual comparison of one prediction stage, not a compressed-video deco bit-exact reconstruction test. The overlay is composed during scanout and cannot be read back through the RGB framebuffer. Console testing is needed to assess image correctness and performance on hardware; emulator output alone cannot establish those properties. + +## Audio playback probe + +Launch `AudioPlaybackProbe.iso` with the same launcher. The probe runs two short tone tests through +the stream client's FFmpeg Opus decoder, mixer and SDL playback device. + +| Test | Encoded tone | Expected Xbox output | +| ------ | ------------ | -------------------- | +| Stereo | Front left | 500 Hz on the left | +| Mono | Front left | 500 Hz on both sides | + +Listen for the listed speaker in each test. **AUDIO PROBE COMPLETE** confirms every mode decoded and +queued the expected PCM byte count and the SDL queue drained. Audible output or a host audio capture +verifies the channel routing. The probe uses local Opus packets and can run with Sunshine closed. + +## 5.1 channel check + +Generate the host-side test files from the repository root with Python and FFmpeg on your path. +Choose an output directory under the current directory. Use a fresh directory for each run to +preserve any existing test files. + +```bash +python experiments/generate_surround_channel_check.py cmake-build-experiments/audio +``` + +The 5.1 WAV plays a one-second tone on each channel with half a second of silence between tones. +Play it through a 5.1-capable player and output to check speaker routing. + +| Time | Active channel | +| --------- | -------------- | +| 0.5–1.5 s | Front left | +| 2.0–3.0 s | Front right | +| 3.5–4.5 s | Front center | +| 5.0–6.0 s | LFE | +| 6.5–7.5 s | Surround left | +| 8.0–9.0 s | Surround right | + +The `.spdif` file carries the same sequence as IEC 61937 bursts containing Dolby Digital AC-3. +It provides test data for a future Xbox digital output backend. The xemu playback probe above +exercises the existing stereo PCM output. diff --git a/experiments/audio_playback_probe.cpp b/experiments/audio_playback_probe.cpp new file mode 100644 index 0000000..86abf7d --- /dev/null +++ b/experiments/audio_playback_probe.cpp @@ -0,0 +1,186 @@ +/** + * @file experiments/audio_playback_probe.cpp + * @brief Play isolated Opus channel tests through the production Xbox audio path. + */ + +// standard includes +#include +#include + +// lib includes +#include +#include +#include +#include + +// local includes +#include "src/logging/logger.h" +#include "src/streaming/audio_player.h" +#include "third-party/moonlight-common-c/src/Limelight.h" + +// The staged SDL library references Moonlight's USB hooks even when only audio is initialized. +/** @brief Satisfy the SDL joystick initializer's USB hook for this input-free probe. */ +extern "C" void moonlight_usb_input_initialize() { + // This probe initializes no input devices. +} + +/** @brief Satisfy the SDL event driver's USB hook for this input-free probe. */ +extern "C" void moonlight_usb_input_pump() { + // This probe has no input events to pump. +} + +namespace { + + constexpr int SAMPLE_RATE = 48000; + constexpr int PACKET_MS = 20; + constexpr int SAMPLES_PER_PACKET = SAMPLE_RATE * PACKET_MS / 1000; + constexpr int PLAYBACK_MS = 2000; + constexpr Uint32 EXPECTED_PCM_BYTES = SAMPLES_PER_PACKET * 2 * sizeof(std::int16_t); + + // Encoded with libopus in restricted-low-delay mode. The 500 Hz tone fills ten + // periods per packet, which allows a single steady-state packet to be repeated. + /** @brief 20 ms of Opus with a 500 Hz tone in the front left channel and silence elsewhere. */ + constexpr std::array STEREO_PACKET { + 0x9c, + 0xad, + 0x86, + 0x72, + 0x9f, + 0x5d, + 0xc3, + 0x85, + 0xbd, + 0x8e, + 0x96, + 0x0a, + 0xe5, + 0x21, + 0x42, + 0x41, + 0xd9, + 0x02, + 0x07, + 0xb9, + 0xc4, + 0x38, + 0xe3, + 0xcc, + 0x50, + 0xe7, + 0xad, + 0x77, + 0xd2, + 0xd6, + 0xe1, + 0xd7, + 0x85, + 0x7f, + 0xf2, + 0x66, + 0x98, + 0xf7, + 0x05, + 0xc9, + 0x7f, + 0x79, + 0x5e, + 0x41, + 0xfc, + 0xcd, + 0x01, + 0x3b, + 0x69, + 0x5d, + 0x56, + 0x66, + 0xd6, + 0x95, + 0x57, + 0xa7, + 0x76, + 0xa6, + 0x93, + 0xb5, + 0xe1, + }; + + /** + * @brief Give the audio device time to consume queued PCM. + * @param player Playback device under test. + * @param limit Maximum queued bytes before continuing. + * @return True if the queue reached the limit before the timeout. + */ + bool wait_for_queue(const streaming::AudioPlayer &player, Uint32 limit) { + const Uint32 deadline = SDL_GetTicks() + 2000; + while (player.pending_bytes() > limit && !SDL_TICKS_PASSED(SDL_GetTicks(), deadline)) { + SDL_Delay(10); + } + if (const auto pending = player.pending_bytes(); pending > limit) { + DbgPrint("AUDIO PROBE QUEUE TIMEOUT: %lu bytes remaining\n", static_cast(pending)); + return false; + } + return true; + } + + /** + * @brief Exercise the production decoder and wait for SDL to consume the queued PCM. + * @param player Reusable audio player. + * @param mode Audio mode under test. + * @param expected Description of the expected audible output. + * @return True if the decoder queued the expected amount of PCM and playback consumed it. + */ + bool play_test(streaming::AudioPlayer &player, streaming::AudioMode mode, const char *expected) { + const OPUS_MULTISTREAM_CONFIGURATION opus {SAMPLE_RATE, 2, 1, 1, SAMPLES_PER_PACKET, {0, 1}}; + + const char *label = streaming::audio_mode_label(mode); + debugPrint("%s: %s\n", label, expected); + DbgPrint("AUDIO PROBE START %s: %s\n", label, expected); + player.configure(mode, true); + if (player.initialize(AUDIO_CONFIGURATION_STEREO, &opus) != 0) { + return false; + } + player.start(); + for (int elapsed = 0; elapsed < PLAYBACK_MS; elapsed += PACKET_MS) { + // Pace the probe from the device queue so emulator clock differences cannot backlog it. + if (!wait_for_queue(player, 3 * EXPECTED_PCM_BYTES)) { + return false; + } + const auto queued = player.decode_and_queue(reinterpret_cast(STEREO_PACKET.data()), static_cast(STEREO_PACKET.size())); + if (queued != EXPECTED_PCM_BYTES) { + DbgPrint("AUDIO PROBE PACKET FAILED: queued %lu bytes\n", static_cast(queued)); + return false; + } + } + + if (!wait_for_queue(player, 0)) { + return false; + } + + // Allow the driver to play its last handoff before closing the device. + SDL_Delay(150); + player.cleanup(); + DbgPrint("AUDIO PROBE PASSED %s\n", label); + SDL_Delay(500); + return true; + } + +} // namespace + +/** @brief Run the stereo and mono playback tests and leave the result on screen. */ +int main() { + XVideoSetMode(640, 480, 32, REFRESH_DEFAULT); + logging::set_debugger_console_minimum_level(logging::LogLevel::warning); + debugPrint("OPUS AUDIO PLAYBACK PROBE\n"); + SDL_Delay(1000); + + using streaming::AudioMode; + streaming::AudioPlayer player; + const bool passed = play_test(player, AudioMode::stereo, "500 Hz on left") && play_test(player, AudioMode::mono, "500 Hz on both"); + player.cleanup(); + const char *result = passed ? "AUDIO PROBE COMPLETE" : "AUDIO PROBE FAILED"; + debugPrint("%s\n", result); + DbgPrint("%s\n", result); + for (;;) { + SDL_Delay(250); + } +} diff --git a/experiments/generate_surround_channel_check.py b/experiments/generate_surround_channel_check.py new file mode 100644 index 0000000..eca8579 --- /dev/null +++ b/experiments/generate_surround_channel_check.py @@ -0,0 +1,83 @@ +"""Generate a 5.1 channel check as PCM WAV and Dolby Digital S/PDIF bursts.""" + +import argparse +import math +import subprocess +import sys +from array import array +from pathlib import Path + +SAMPLE_RATE = 48_000 +TONE_FRAMES = SAMPLE_RATE +GAP_FRAMES = SAMPLE_RATE // 2 +FADE_FRAMES = SAMPLE_RATE // 100 +# FFmpeg's 5.1(side) order is FL, FR, FC, LFE, SL, SR. +CHANNEL_FREQUENCIES = (500, 500, 500, 80, 500, 500) +WAV_NAME = "surround-channel-check.wav" +SPDIF_NAME = "surround-channel-check.spdif" + + +def channel_check_pcm() -> bytes: + """Return interleaved signed 16-bit PCM with one active channel per slot.""" + channels = len(CHANNEL_FREQUENCIES) + slot_frames = TONE_FRAMES + GAP_FRAMES + total_frames = GAP_FRAMES + slot_frames * channels + samples = array("h", [0]) * (total_frames * channels) + for channel, frequency in enumerate(CHANNEL_FREQUENCIES): + tone = array( + "h", + ( + round( + 6_500 + * math.sin(math.tau * frequency * frame / SAMPLE_RATE) + * min(frame, TONE_FRAMES - 1 - frame, FADE_FRAMES) + / FADE_FRAMES + ) + for frame in range(TONE_FRAMES) + ), + ) + first_sample = (GAP_FRAMES + channel * slot_frames) * channels + channel + end_sample = first_sample + TONE_FRAMES * channels + samples[first_sample:end_sample:channels] = tone + + if sys.byteorder == "big": + samples.byteswap() + return samples.tobytes() + + +def main() -> None: + """Write both test files under the current directory using FFmpeg on PATH.""" + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("output_directory", type=Path, help="Output directory within the current directory") + args = parser.parse_args() + + base_directory = Path.cwd().resolve() + output_directory = args.output_directory.resolve() + if not output_directory.is_relative_to(base_directory): + parser.error("output directory must be within the current directory") + + output_directory.mkdir(parents=True, exist_ok=True) + wav = output_directory / WAV_NAME + spdif = output_directory / SPDIF_NAME + for output in (wav, spdif): + if output.exists() or output.is_symlink(): + parser.error(f"output already exists: {output}") + + subprocess.run( + [ + "ffmpeg", + "-hide_banner", "-loglevel", "error", "-nostdin", "-n", + "-f", "s16le", "-ar", str(SAMPLE_RATE), "-channel_layout", "5.1(side)", "-i", "pipe:0", + "-map", "0:a:0", "-c:a", "pcm_s16le", WAV_NAME, + "-map", "0:a:0", "-c:a", "ac3_fixed", "-b:a", "448k", "-f", "spdif", SPDIF_NAME, + ], + cwd=output_directory, + input=channel_check_pcm(), + check=True, + ) + print(wav) + print(spdif) + + +if __name__ == "__main__": + main() diff --git a/experiments/test_generate_surround_channel_check.py b/experiments/test_generate_surround_channel_check.py new file mode 100644 index 0000000..d076ccb --- /dev/null +++ b/experiments/test_generate_surround_channel_check.py @@ -0,0 +1,71 @@ +"""Regression tests for the surround fixture generator's command and output paths.""" + +import io +import subprocess +import sys +import tempfile +import unittest +from contextlib import chdir, redirect_stderr, redirect_stdout +from pathlib import Path +from unittest.mock import patch + +import generate_surround_channel_check as generator + + +class GenerateSurroundChannelCheckTest(unittest.TestCase): + """Check that the generator launches FFmpeg only with safe output paths.""" + + def test_runs_ffmpeg_with_fixed_names_inside_requested_directory(self): + """Pass the generated PCM to FFmpeg without passing CLI paths as arguments.""" + with tempfile.TemporaryDirectory() as temp, chdir(temp): + output = Path.cwd() / "fixtures with spaces" + with ( + patch.object(sys, "argv", ["generate_surround_channel_check.py", str(output)]), + patch.object(generator, "channel_check_pcm", return_value=b"pcm"), + patch.object(subprocess, "run") as run, + redirect_stdout(io.StringIO()), + ): + generator.main() + + self.assertTrue(output.is_dir()) + run.assert_called_once() + command = run.call_args.args[0] + self.assertEqual(command[0], "ffmpeg") + self.assertIn(generator.WAV_NAME, command) + self.assertIn(generator.SPDIF_NAME, command) + self.assertNotIn(str(output), command) + self.assertEqual(run.call_args.kwargs, {"cwd": output.resolve(), "input": b"pcm", "check": True}) + + def test_rejects_paths_outside_current_directory(self): + """Check that a parent traversal and an absolute sibling path both fail.""" + with tempfile.TemporaryDirectory() as temp: + parent = Path(temp) + working = parent / "working" + working.mkdir() + for output in ("../outside", str(parent / "outside")): + with self.subTest(output=output), chdir(working): + self.assert_cli_rejected(output, "output directory must be within the current directory") + self.assertFalse((parent / "outside").exists()) + + def test_rejects_existing_output(self): + """An existing surround fixture is preserved and FFmpeg never starts.""" + with tempfile.TemporaryDirectory() as temp, chdir(temp): + existing = Path(generator.SPDIF_NAME) + existing.write_bytes(b"keep") + self.assert_cli_rejected(".", "output already exists") + self.assertEqual(existing.read_bytes(), b"keep") + self.assertFalse(Path(generator.WAV_NAME).exists()) + + def assert_cli_rejected(self, output, message): + """Assert a path is rejected before starting any subprocess.""" + with ( + patch.object(sys, "argv", ["generate_surround_channel_check.py", output]), + patch.object(subprocess, "run") as run, + redirect_stderr(io.StringIO()) as stderr, + self.assertRaises(SystemExit) as exit_context, + ): + generator.main() + + self.assertEqual(exit_context.exception.code, 2) + self.assertIn(message, stderr.getvalue()) + run.assert_not_called() diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 61f01d6..1f0e7db 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -81,7 +81,7 @@ namespace { /** * @brief Map each independent settings category to its stable navigation identifier. * @param category Settings section to identify. - * @return Menu identifier for Logging, Display, Input, Storage or Reset. + * @return Stable identifier for a settings category. */ std::string settings_category_menu_id(app::SettingsCategory category) { switch (category) { @@ -89,6 +89,8 @@ namespace { return std::string(SETTINGS_CATEGORY_PREFIX) + "logging"; case app::SettingsCategory::display: return std::string(SETTINGS_CATEGORY_PREFIX) + "display"; + case app::SettingsCategory::audio: + return std::string(SETTINGS_CATEGORY_PREFIX) + "audio"; case app::SettingsCategory::input: return std::string(SETTINGS_CATEGORY_PREFIX) + "input"; case app::SettingsCategory::storage: @@ -109,6 +111,9 @@ namespace { if (itemId == settings_category_menu_id(app::SettingsCategory::display)) { return app::SettingsCategory::display; } + if (itemId == settings_category_menu_id(app::SettingsCategory::audio)) { + return app::SettingsCategory::audio; + } if (itemId == settings_category_menu_id(app::SettingsCategory::input)) { return app::SettingsCategory::input; } @@ -124,14 +129,16 @@ namespace { /** * @brief Describe the options owned by a settings category. * @param category Section whose scope should be explained. - * @return Category guidance, keeping file destinations separate from Display. + * @return Guidance for the selected settings category. */ const char *settings_category_description(app::SettingsCategory category) { switch (category) { case app::SettingsCategory::logging: return "Control the runtime log file, the in-app log viewer, and xemu debugger output verbosity."; case app::SettingsCategory::display: - return "Tune streaming video resolution, frame rate, bitrate, audio playback and performance statistics."; + return "Tune streaming video resolution, frame rate, bitrate and performance statistics."; + case app::SettingsCategory::audio: + return "Choose where stream audio plays and how the Xbox mixes it."; case app::SettingsCategory::input: return "Input options will live here when controller and navigation customization is added."; case app::SettingsCategory::storage: @@ -526,6 +533,7 @@ namespace { return { {settings_category_menu_id(app::SettingsCategory::logging), "Logging", settings_category_description(app::SettingsCategory::logging), true}, {settings_category_menu_id(app::SettingsCategory::display), "Display", settings_category_description(app::SettingsCategory::display), true}, + {settings_category_menu_id(app::SettingsCategory::audio), "Audio", settings_category_description(app::SettingsCategory::audio), true}, {settings_category_menu_id(app::SettingsCategory::input), "Input", settings_category_description(app::SettingsCategory::input), true}, {settings_category_menu_id(app::SettingsCategory::storage), "Storage", settings_category_description(app::SettingsCategory::storage), true}, {settings_category_menu_id(app::SettingsCategory::reset), "Reset", settings_category_description(app::SettingsCategory::reset), true}, @@ -560,7 +568,7 @@ namespace { } /** - * @brief Build the active settings options, including the screenshot destination picker. + * @brief Build the active settings options, including audio mode and screenshot destination. * @param state Current screen and settings values. * @return Selectable settings rows for the current category. */ @@ -602,22 +610,31 @@ namespace { "Choose how new streams display decoded video. GPU uses NV2A with automatic software fallback. Software can be faster in xemu.", true, }, + { + "toggle-show-performance-stats", + std::string("Performance Stats: ") + (state.settings.presentation.showPerformanceStats ? "On" : "Off"), + "Show performance statistics during playback and after the stream ends.", + true, + }, + }; + case app::SettingsCategory::audio: + return { { "toggle-play-audio-on-pc", - std::string("Play Audio on PC: ") + (state.settings.playAudioOnPc ? "On" : "Off"), + std::string("Play Audio on PC: ") + (state.settings.audio.playOnPc ? "On" : "Off"), "Toggle whether the host PC should continue local audio playback while also streaming audio to this Xbox client.", true, }, { "toggle-play-audio-on-xbox", - std::string("Play Audio on Xbox: ") + (state.settings.playAudioOnXbox ? "On" : "Off"), + std::string("Play Audio on Xbox: ") + (state.settings.audio.playOnXbox ? "On" : "Off"), "Toggle local audio playback on the Xbox. Disable this to skip Opus decode work when video latency matters more than sound.", true, }, { - "toggle-show-performance-stats", - std::string("Performance Stats: ") + (state.settings.presentation.showPerformanceStats ? "On" : "Off"), - "Show performance statistics during playback and after the stream ends.", + "cycle-audio-mode", + std::string("Audio Mode: ") + streaming::audio_mode_label(state.settings.audio.mode), + "Choose separate left and right channels or the same mono mix through both speakers.", true, }, }; @@ -1896,8 +1913,6 @@ namespace app { state.settings.logViewerPlacement = LogViewerPlacement::full; state.settings.loggingLevel = logging::LogLevel::none; state.settings.xemuConsoleLoggingLevel = logging::LogLevel::none; - state.settings.playAudioOnPc = false; - state.settings.playAudioOnXbox = true; state.settings.controls.screenshotDirectory.clear(); state.settings.controls.keyboardDockTop = false; state.settings.dirty = false; @@ -2475,7 +2490,7 @@ namespace app { } /** - * @brief Update settings inline or open the screenshot folder picker, emitting only required side effects. + * @brief Update settings, including audio mode, or open the screenshot folder picker. * * @param state Client state containing the settings menus. * @param detailUpdate Activated detail-menu update. @@ -2535,21 +2550,29 @@ namespace app { return; } if (detailUpdate.activatedItemId == "toggle-play-audio-on-pc") { - state.settings.playAudioOnPc = !state.settings.playAudioOnPc; + state.settings.audio.playOnPc = !state.settings.audio.playOnPc; state.settings.dirty = true; update->persistence.settingsChanged = true; - state.shell.statusMessage = std::string("Play audio on PC ") + (state.settings.playAudioOnPc ? "enabled" : "disabled"); + state.shell.statusMessage = std::string("Play audio on PC ") + (state.settings.audio.playOnPc ? "enabled" : "disabled"); rebuild_menu(state, "toggle-play-audio-on-pc"); return; } if (detailUpdate.activatedItemId == "toggle-play-audio-on-xbox") { - state.settings.playAudioOnXbox = !state.settings.playAudioOnXbox; + state.settings.audio.playOnXbox = !state.settings.audio.playOnXbox; state.settings.dirty = true; update->persistence.settingsChanged = true; - state.shell.statusMessage = std::string("Play audio on Xbox ") + (state.settings.playAudioOnXbox ? "enabled" : "disabled"); + state.shell.statusMessage = std::string("Play audio on Xbox ") + (state.settings.audio.playOnXbox ? "enabled" : "disabled"); rebuild_menu(state, "toggle-play-audio-on-xbox"); return; } + if (detailUpdate.activatedItemId == "cycle-audio-mode") { + state.settings.audio.mode = streaming::next_audio_mode(state.settings.audio.mode); + state.settings.dirty = true; + update->persistence.settingsChanged = true; + state.shell.statusMessage = std::string("Audio mode set to ") + streaming::audio_mode_label(state.settings.audio.mode); + rebuild_menu(state, "cycle-audio-mode"); + return; + } if (detailUpdate.activatedItemId == "screenshot-directory") { state.directoryPicker = {}; state.directoryPicker.path = state.settings.controls.screenshotDirectory.empty() ? startup::default_storage_path("screenshots") : state.settings.controls.screenshotDirectory; diff --git a/src/app/client_state.h b/src/app/client_state.h index ab91668..b0931ee 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -24,6 +24,7 @@ #include "src/input/ui_input.h" #include "src/logging/logger.h" #include "src/startup/saved_files.h" +#include "src/streaming/audio_configuration.h" #include "src/ui/menu_model.h" namespace app { @@ -91,7 +92,8 @@ namespace app { */ enum class SettingsCategory { logging, ///< Logging and diagnostics options. - display, ///< Display, stream video, audio and telemetry options. + display, ///< Display, stream video and telemetry options. + audio, ///< Playback destinations and audio mode. input, ///< Input and controller options. storage, ///< Screenshot destinations and other saved-file location options. reset, ///< Reset and cleanup actions. @@ -389,6 +391,13 @@ namespace app { bool showPerformanceStats = false; ///< Display live performance telemetry and include it in the stream summary. }; + /** @brief Preferences for host and Xbox audio playback during streaming. */ + struct AudioPlaybackPreferences { + bool playOnPc = false; ///< Keep audio playing on the host PC. + bool playOnXbox = true; ///< Decode and play streaming audio on the Xbox. + streaming::AudioMode mode = streaming::AudioMode::stereo; ///< Mix used for Xbox playback. + }; + /** * @brief State owned by the settings, log viewer, and saved-file workflows. */ @@ -407,8 +416,7 @@ namespace app { 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. - 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. + AudioPlaybackPreferences audio; ///< Host and Xbox playback preferences. LocalStreamPreferences controls; ///< Screenshot storage and keyboard placement preferences. bool dirty = false; ///< True when persisted TOML-backed settings changed and should be saved. std::vector savedFiles; ///< Saved-file catalog shown on the reset settings page. diff --git a/src/app/settings_storage.cpp b/src/app/settings_storage.cpp index a0c8b71..b63c78d 100644 --- a/src/app/settings_storage.cpp +++ b/src/app/settings_storage.cpp @@ -326,6 +326,11 @@ namespace { append_invalid_value_warning(warnings, filePath, keyPath, ""); } + /** + * @brief Serialize the saved settings, including the selected audio mode. + * @param settings Settings snapshot to serialize. + * @return TOML text for the settings file. + */ std::string format_settings_toml(const app::AppSettings &settings) { std::string content; content += "# Moonlight Xbox OG user settings\n"; @@ -352,6 +357,7 @@ namespace { 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"; + content += std::string("audio_mode = \"") + streaming::audio_mode_key(settings.audioMode) + "\"\n"; content += "# Show stream telemetry after streaming ends.\n"; std::ostringstream screenshotValue; screenshotValue << toml::value(settings.screenshotDirectory); @@ -392,7 +398,7 @@ namespace { } /** - * @brief Mark unknown streaming keys for cleanup on the next settings save. + * @brief Mark unknown streaming keys for cleanup, preserving recognized settings such as audio mode. * * @param streamingTable Parsed streaming settings table. * @param filePath Settings file path used in warnings. @@ -401,7 +407,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 == "gpu_presentation" || key == "play_audio_on_pc" || key == "play_audio_on_xbox" || key == "audio_mode" || key == "show_performance_stats" || key == "screenshot_directory" || key == "keyboard_dock_top") { continue; } @@ -505,6 +511,14 @@ namespace app { 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); + if (const auto audioModeNode = settingsTable["streaming"]["audio_mode"]; audioModeNode) { + const auto key = audioModeNode.value(); + if (const auto mode = key ? streaming::parse_audio_mode(*key) : std::nullopt; mode) { + result.settings.audioMode = *mode; + } else { + append_invalid_value_warning(&result.warnings, filePath, "streaming.audio_mode", key.value_or("")); + } + } load_boolean_setting(settingsTable["streaming"]["show_performance_stats"], filePath, "streaming.show_performance_stats", &result.settings.showPerformanceStats, &result.warnings); load_boolean_setting(settingsTable["streaming"]["keyboard_dock_top"], filePath, "streaming.keyboard_dock_top", &result.settings.keyboardDockTop, &result.warnings); if (const auto node = settingsTable["streaming"]["screenshot_directory"]; node) { diff --git a/src/app/settings_storage.h b/src/app/settings_storage.h index 152bbb8..f1b11dd 100644 --- a/src/app/settings_storage.h +++ b/src/app/settings_storage.h @@ -30,6 +30,7 @@ namespace app { std::string screenshotDirectory; ///< Screenshot folder; empty selects the title-scoped screenshots directory. bool keyboardDockTop = false; ///< Dock the stream keyboard to the top instead of the bottom. bool gpuPresentation = false; ///< Opt in to NV2A presentation with software fallback. + streaming::AudioMode audioMode = streaming::AudioMode::stereo; ///< Source channels and local playback mix. }; /** @@ -58,7 +59,7 @@ namespace app { std::string default_settings_path(); /** - * @brief Load saved preferences and derive a bitrate from the stream profile when no bitrate is saved. + * @brief Load saved preferences, defaulting to stereo and deriving the bitrate when absent. * * @param filePath Settings file to read. * @return Loaded settings plus any non-fatal warnings. diff --git a/src/main.cpp b/src/main.cpp index 47f0a02..335bc23 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,6 +38,11 @@ namespace { network::RuntimeNetworkStatus runtimeNetworkStatus; }; + /** + * @brief Restore saved preferences, including the requested audio mode, into the shell. + * @param state Shell state to update. + * @param settings Saved settings snapshot. + */ void apply_persisted_settings(app::ClientState &state, const app::AppSettings &settings) { state.settings.loggingLevel = settings.loggingLevel; state.settings.xemuConsoleLoggingLevel = settings.xemuConsoleLoggingLevel; @@ -47,9 +52,10 @@ namespace { state.settings.streamFramerate = settings.streamFramerate; state.settings.streamBitrateKbps = settings.streamBitrateKbps; state.settings.presentation.gpuPresentation = settings.gpuPresentation; - state.settings.playAudioOnPc = settings.playAudioOnPc; + state.settings.audio.playOnPc = settings.playAudioOnPc; state.settings.presentation.showPerformanceStats = settings.showPerformanceStats; - state.settings.playAudioOnXbox = settings.playAudioOnXbox; + state.settings.audio.playOnXbox = settings.playAudioOnXbox; + state.settings.audio.mode = settings.audioMode; state.settings.controls.screenshotDirectory = settings.screenshotDirectory; state.settings.controls.keyboardDockTop = settings.keyboardDockTop; state.settings.dirty = false; diff --git a/src/streaming/audio_configuration.cpp b/src/streaming/audio_configuration.cpp new file mode 100644 index 0000000..ea06733 --- /dev/null +++ b/src/streaming/audio_configuration.cpp @@ -0,0 +1,32 @@ +/** + * @file src/streaming/audio_configuration.cpp + * @brief Implements menu labels and persistence for the supported audio modes. + */ +// class header include +#include "src/streaming/audio_configuration.h" + +namespace streaming { + + const char *audio_mode_label(AudioMode mode) { + return mode == AudioMode::mono ? "Mono" : "Stereo"; + } + + const char *audio_mode_key(AudioMode mode) { + return mode == AudioMode::mono ? "mono" : "stereo"; + } + + std::optional parse_audio_mode(std::string_view key) { + if (key == audio_mode_key(AudioMode::mono)) { + return AudioMode::mono; + } + if (key == audio_mode_key(AudioMode::stereo)) { + return AudioMode::stereo; + } + return std::nullopt; + } + + AudioMode next_audio_mode(AudioMode mode) { + return mode == AudioMode::stereo ? AudioMode::mono : AudioMode::stereo; + } + +} // namespace streaming diff --git a/src/streaming/audio_configuration.h b/src/streaming/audio_configuration.h new file mode 100644 index 0000000..b258528 --- /dev/null +++ b/src/streaming/audio_configuration.h @@ -0,0 +1,47 @@ +/** + * @file src/streaming/audio_configuration.h + * @brief Defines the supported Xbox audio modes. + */ +#pragma once + +// standard includes +#include +#include + +namespace streaming { + + /** @brief Select the mix played through the Xbox stereo device. */ + enum class AudioMode { + stereo, ///< Play the stereo stream as received. + mono, ///< Mix stereo to mono and play it through both channels. + }; + + /** + * @brief Get the display label for an audio mode. + * @param mode Audio mode. + * @return Label shown in settings. + */ + const char *audio_mode_label(AudioMode mode); + + /** + * @brief Get the stable TOML value for an audio mode. + * @param mode Audio mode. + * @return Value written to the settings file. + */ + const char *audio_mode_key(AudioMode mode); + + /** + * @brief Parse a saved audio mode. + * @param key Value read from the settings file. + * @return Matching mode, or no value for an unknown key. + */ + std::optional parse_audio_mode(std::string_view key); + + /** + * @brief Advance to the next audio mode in settings. + * @param mode Current audio mode. + * @return Next audio mode, wrapping back to stereo. + */ + AudioMode next_audio_mode(AudioMode mode); + +} // namespace streaming diff --git a/src/streaming/audio_player.cpp b/src/streaming/audio_player.cpp new file mode 100644 index 0000000..b6cf271 --- /dev/null +++ b/src/streaming/audio_player.cpp @@ -0,0 +1,240 @@ +/** + * @file src/streaming/audio_player.cpp + * @brief Implements Opus decoding, mixing and queued SDL playback. + */ +// class header include +#include "src/streaming/audio_player.h" + +// standard includes +#include +#include + +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + +extern "C" { +#include +#include +#include +#include +#include +#include +} + +// local includes +#include "src/logging/logger.h" + +namespace { + + constexpr int OUTPUT_SAMPLE_RATE = 48000; + constexpr int OUTPUT_CHANNELS = 2; + constexpr Uint32 QUEUE_LIMIT_MS = 250; + + /** + * @brief Convert an FFmpeg error code to a log message. + * @param code Negative FFmpeg error code. + * @return Readable error message. + */ + std::string ffmpeg_error(int code) { + std::array buffer {}; + av_strerror(code, buffer.data(), buffer.size()); + return buffer.data(); + } + +} // namespace + +namespace streaming { + + AudioPlayer::~AudioPlayer() { + cleanup(); + } + + void AudioPlayer::configure(AudioMode mode, bool enabled) { + mode_ = mode; + enabled_ = enabled; + } + + bool AudioPlayer::enabled() const { + return enabled_; + } + + int AudioPlayer::initialize(int audioConfiguration, const _OPUS_MULTISTREAM_CONFIGURATION *opus) { + cleanup(); + if (!enabled_) { + return 0; + } + + if (audioConfiguration != AUDIO_CONFIGURATION_STEREO || opus == nullptr || opus->channelCount != OUTPUT_CHANNELS || opus->sampleRate != OUTPUT_SAMPLE_RATE || opus->streams != 1 || opus->coupledStreams != 1) { + logging::error("stream", "Invalid stereo Opus configuration"); + return -1; + } + + const AVCodec *decoder = avcodec_find_decoder(AV_CODEC_ID_OPUS); + if (decoder == nullptr) { + logging::error("stream", "FFmpeg did not provide an Opus decoder"); + return -1; + } + codec_ = avcodec_alloc_context3(decoder); + frame_ = av_frame_alloc(); + packet_ = av_packet_alloc(); + if (codec_ == nullptr || frame_ == nullptr || packet_ == nullptr) { + logging::error("stream", "Failed to allocate FFmpeg audio decoder resources"); + cleanup(); + return -1; + } + // OpusHead for 48 kHz stereo, mapping family 0, with no pre-skip or gain. + constexpr std::array header {'O', 'p', 'u', 's', 'H', 'e', 'a', 'd', 1, 2, 0, 0, 0x80, 0xBB, 0, 0, 0, 0, 0}; + codec_->extradata = static_cast(av_mallocz(header.size() + AV_INPUT_BUFFER_PADDING_SIZE)); + if (codec_->extradata == nullptr) { + logging::error("stream", "Failed to allocate Opus decoder extradata for FFmpeg"); + cleanup(); + return -1; + } + codec_->extradata_size = static_cast(header.size()); + std::memcpy(codec_->extradata, header.data(), header.size()); + if (const int result = avcodec_open2(codec_, decoder, nullptr); result < 0) { + logging::error("stream", "Failed to open the Opus decoder: " + ffmpeg_error(result)); + cleanup(); + return result; + } + + if ((SDL_WasInit(SDL_INIT_AUDIO) & SDL_INIT_AUDIO) == 0 && SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) { + logging::error("stream", std::string("SDL_InitSubSystem(SDL_INIT_AUDIO) failed: ") + SDL_GetError()); + cleanup(); + return -1; + } + // The nxdk SDL driver plays only 48 kHz signed 16-bit stereo PCM. + SDL_AudioSpec desired {}; + desired.freq = OUTPUT_SAMPLE_RATE; + desired.format = AUDIO_S16SYS; + desired.channels = OUTPUT_CHANNELS; + desired.samples = 1024; + device_ = SDL_OpenAudioDevice(nullptr, 0, &desired, &output_, 0); + if (device_ == 0) { + logging::error("stream", std::string("SDL_OpenAudioDevice failed: ") + SDL_GetError()); + cleanup(); + return -1; + } + return 0; + } + + void AudioPlayer::start() const { + if (device_ != 0) { + SDL_ClearQueuedAudio(device_); + SDL_PauseAudioDevice(device_, 0); + } + } + + void AudioPlayer::stop() const { + if (device_ != 0) { + SDL_PauseAudioDevice(device_, 1); + } + } + + void AudioPlayer::cleanup() { + if (device_ != 0) { + stop(); + SDL_CloseAudioDevice(device_); + device_ = 0; + } + av_packet_free(&packet_); + av_frame_free(&frame_); + avcodec_free_context(&codec_); + swr_free(&resampler_); + output_ = {}; + pcm_.clear(); + } + + Uint32 AudioPlayer::pending_bytes() const { + return device_ == 0 ? 0 : SDL_GetQueuedAudioSize(device_); + } + + std::string AudioPlayer::describe_playback() const { + if (!enabled_) { + return "audio decode disabled"; + } + return device_ == 0 ? "audio idle" : std::to_string(pending_bytes()) + " queued audio bytes"; + } + + bool AudioPlayer::initialize_resampler() { + AVChannelLayout stereo = AV_CHANNEL_LAYOUT_STEREO; + int result = swr_alloc_set_opts2(&resampler_, &stereo, AV_SAMPLE_FMT_S16, output_.freq, &frame_->ch_layout, static_cast(frame_->format), frame_->sample_rate, 0, nullptr); + if (result >= 0 && mode_ == AudioMode::mono) { + // Play the mean of left and right on both outputs of the stereo device. + constexpr std::array dualMono {0.5, 0.5, 0.5, 0.5}; + result = swr_set_matrix(resampler_, dualMono.data(), OUTPUT_CHANNELS); + } + if (result >= 0) { + result = swr_init(resampler_); + } + if (result < 0) { + logging::warn("stream", "Failed to initialize the streaming audio resampler: " + ffmpeg_error(result)); + swr_free(&resampler_); + return false; + } + return true; + } + + Uint32 AudioPlayer::queue_decoded_frame() { + if (resampler_ == nullptr && !initialize_resampler()) { + return 0; + } + + const int outputSamples = swr_get_out_samples(resampler_, frame_->nb_samples); + if (outputSamples <= 0) { + return 0; + } + constexpr Uint32 bytesPerFrame = OUTPUT_CHANNELS * sizeof(std::int16_t); + pcm_.resize(static_cast(outputSamples) * bytesPerFrame); + auto *output = pcm_.data(); + const int converted = swr_convert(resampler_, &output, outputSamples, const_cast(frame_->extended_data), frame_->nb_samples); + if (converted <= 0) { + if (converted < 0) { + logging::warn("stream", "Failed to convert decoded audio: " + ffmpeg_error(converted)); + } + return 0; + } + + const Uint32 bytes = static_cast(converted) * bytesPerFrame; + if (const Uint32 maxQueuedBytes = output_.freq * bytesPerFrame * QUEUE_LIMIT_MS / 1000U; pending_bytes() + bytes > maxQueuedBytes) { + SDL_ClearQueuedAudio(device_); + } + if (SDL_QueueAudio(device_, output, bytes) < 0) { + logging::warn("stream", std::string("Failed to queue decoded audio: ") + SDL_GetError()); + return 0; + } + return bytes; + } + + Uint32 AudioPlayer::decode_and_queue(const char *data, int length) { + if (device_ == 0 || data == nullptr || length <= 0) { + return 0; + } + if (const int result = av_new_packet(packet_, length); result < 0) { + logging::warn("stream", "Failed to allocate an Opus packet: " + ffmpeg_error(result)); + return 0; + } + std::memcpy(packet_->data, data, static_cast(length)); + const int sendResult = avcodec_send_packet(codec_, packet_); + av_packet_unref(packet_); + if (sendResult < 0 && sendResult != AVERROR(EAGAIN)) { + logging::warn("stream", "Failed to submit an Opus packet: " + ffmpeg_error(sendResult)); + return 0; + } + + Uint32 queued = 0; + while (true) { + const int result = avcodec_receive_frame(codec_, frame_); + if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) { + return queued; + } + if (result < 0) { + logging::warn("stream", "Failed to decode an Opus frame: " + ffmpeg_error(result)); + return queued; + } + queued += queue_decoded_frame(); + av_frame_unref(frame_); + } + } + +} // namespace streaming diff --git a/src/streaming/audio_player.h b/src/streaming/audio_player.h new file mode 100644 index 0000000..04e3543 --- /dev/null +++ b/src/streaming/audio_player.h @@ -0,0 +1,115 @@ +/** + * @file src/streaming/audio_player.h + * @brief Declares the FFmpeg Opus and SDL playback path shared by streaming and the audio probe. + */ +#pragma once + +// standard includes +#include + +// lib includes +#include + +// local includes +#include "src/streaming/aligned_media_buffer.h" +#include "src/streaming/audio_configuration.h" + +struct AVCodecContext; +struct AVFrame; +struct AVPacket; +struct SwrContext; +struct _OPUS_MULTISTREAM_CONFIGURATION; + +namespace streaming { + + /** + * @brief Decode one Moonlight audio stream and queue its local mix to SDL. + * + * The transport configures playback before starting and releases the decoder + * after joining its audio thread. + */ + class AudioPlayer { + public: + /** @brief Construct an idle player. */ + AudioPlayer() = default; + /** @brief Close the playback device and release decoder resources. */ + ~AudioPlayer(); + + AudioPlayer(const AudioPlayer &) = delete; + AudioPlayer &operator=(const AudioPlayer &) = delete; + + /** + * @brief Set playback preferences before initialization. + * @param mode How to mix the selected stream to the Xbox output. + * @param enabled Whether to decode and queue audio. + */ + void configure(AudioMode mode, bool enabled); + + /** + * @brief Check whether playback was enabled for this session. + * @return True if streaming audio should be decoded. + */ + bool enabled() const; + + /** + * @brief Prepare the decoder and open the stereo SDL device. + * @param audioConfiguration Moonlight stream layout. + * @param opus Negotiated codec parameters. + * @return Zero on success, including when playback is disabled. + */ + int initialize(int audioConfiguration, const _OPUS_MULTISTREAM_CONFIGURATION *opus); + + /** @brief Start SDL audio playback with an empty queue. */ + void start() const; + + /** @brief Pause playback. */ + void stop() const; + + /** @brief Release playback and codec resources, retaining the configured preferences. */ + void cleanup(); + + /** + * @brief Decode and queue a Moonlight Opus payload. + * @param data Opus payload, or null for a lost packet. + * @param length Payload size in bytes. + * @return Number of PCM bytes queued. + */ + Uint32 decode_and_queue(const char *data, int length); + + /** + * @brief Inspect the SDL queue for playback diagnostics. + * @return Pending PCM bytes, or zero if the device is closed. + */ + Uint32 pending_bytes() const; + + /** + * @brief Describe the configured audio path and its queue for session diagnostics. + * @return Text describing disabled, idle or active playback. + */ + std::string describe_playback() const; + + private: + /** + * @brief Create the SDL PCM converter for the decoder's fixed Opus format. + * @return True when ready to convert frames. + */ + bool initialize_resampler(); + + /** + * @brief Convert the current decoded frame and queue it with bounded latency. + * @return PCM bytes queued, or zero on failure. + */ + Uint32 queue_decoded_frame(); + + AudioMode mode_ = AudioMode::stereo; ///< Selected local mix. + bool enabled_ = false; ///< Whether the session plays audio. + AVCodecContext *codec_ = nullptr; ///< Open Opus decoder. + SwrContext *resampler_ = nullptr; ///< Stereo S16 format converter. + AVFrame *frame_ = nullptr; ///< Reusable decoded Opus frame. + AVPacket *packet_ = nullptr; ///< Reusable encoded Opus packet. + SDL_AudioDeviceID device_ = 0; ///< Open SDL device, or zero. + SDL_AudioSpec output_ {}; ///< SDL queue format. + AlignedMediaBuffer pcm_; ///< Aligned PCM mix buffer. + }; + +} // namespace streaming diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index 863323f..b9038bf 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -20,14 +20,10 @@ // lib includes extern "C" { #include -#include #include #include #include #include -#include -#include -#include #include } @@ -55,7 +51,6 @@ namespace { */ using MoonlightCallbackContext = void; - constexpr Uint32 STREAM_OVERLAY_AUDIO_QUEUE_LIMIT_MS = 250U; constexpr std::uint64_t STREAM_VIDEO_SUBMISSION_LOG_INTERVAL = 120; constexpr int STREAM_VIDEO_DROPPED_FRAME_STATUS = -2; constexpr std::uint64_t STREAM_VIDEO_DROP_LOG_INTERVAL = 30; @@ -184,16 +179,6 @@ namespace { }); } - /** - * @brief Return the number of bytes generated per second for the SDL audio format. - * - * @param audioSpec SDL audio format in use for playback. - * @return Number of bytes generated per second. - */ - Uint32 audio_bytes_per_second(const SDL_AudioSpec &audioSpec) { - return static_cast(audioSpec.freq) * static_cast(audioSpec.channels) * (SDL_AUDIO_BITSIZE(audioSpec.format) / 8U); - } - /** * @brief Compute a centered destination rectangle that preserves aspect ratio. * @@ -227,51 +212,6 @@ namespace { }; } - /** - * @brief Create FFmpeg Opus extradata for the stereo Moonlight stream. - * - * @param channelCount Negotiated channel count. - * @param sampleRate Negotiated sample rate. - * @param codecContext Audio codec context that will own the extradata. - * @return True when the extradata was allocated successfully. - */ - bool configure_opus_extradata(int channelCount, int sampleRate, AVCodecContext *codecContext) { - if (codecContext == nullptr || channelCount <= 0 || channelCount > 2) { - return false; - } - - std::array opusHead { - static_cast('O'), - static_cast('p'), - static_cast('u'), - static_cast('s'), - static_cast('H'), - static_cast('e'), - static_cast('a'), - static_cast('d'), - 1U, - static_cast(channelCount), - 0U, - 0U, - static_cast(sampleRate & 0xFF), - static_cast((sampleRate >> 8) & 0xFF), - static_cast((sampleRate >> 16) & 0xFF), - static_cast((sampleRate >> 24) & 0xFF), - 0U, - 0U, - 0U, - }; - - codecContext->extradata = static_cast(av_mallocz(opusHead.size() + AV_INPUT_BUFFER_PADDING_SIZE)); - if (codecContext->extradata == nullptr) { - return false; - } - - codecContext->extradata_size = static_cast(opusHead.size()); - std::memcpy(codecContext->extradata, opusHead.data(), opusHead.size()); - return true; - } - int on_video_setup(int videoFormat, int width, int height, int redrawRate, MoonlightCallbackContext *context, int drFlags) { // NOSONAR(cpp:S5008) moonlight-common-c requires a void* render context callback. auto *backend = static_cast(context); if (backend == nullptr) { @@ -301,38 +241,56 @@ namespace { } } - int on_audio_init(int audioConfiguration, const POPUS_MULTISTREAM_CONFIGURATION opusConfig, MoonlightCallbackContext *context, int arFlags) { // NOSONAR(cpp:S5008,cpp:S995) moonlight-common-c requires this callback signature. + /** + * @brief Prepare the active Opus decoder from the negotiated multistream configuration. + * @param audioConfiguration Negotiated Moonlight layout. + * @param opusConfig Negotiated Opus channels and mappings. + * @param context Stream backend supplied by Moonlight. + * @return Zero when playback can start or has been disabled. + */ + int on_audio_init(int audioConfiguration, const POPUS_MULTISTREAM_CONFIGURATION opusConfig, MoonlightCallbackContext *context, int) { // NOSONAR(cpp:S5008,cpp:S995) moonlight-common-c requires this callback signature. auto *backend = static_cast(context); if (backend == nullptr) { return -1; } - active_audio_backend() = backend; - return backend->initialize_audio_decoder(audioConfiguration, opusConfig, arFlags); + const int result = backend->audio_player().initialize(audioConfiguration, opusConfig); + if (result == 0) { + active_audio_backend() = backend; + } + return result; } + /** @brief Start the active SDL audio queue. */ void on_audio_start() { if (streaming::FfmpegStreamBackend *backend = active_audio_backend(); backend != nullptr) { - backend->start_audio_playback(); + backend->audio_player().start(); } } + /** @brief Pause the active SDL audio queue. */ void on_audio_stop() { if (streaming::FfmpegStreamBackend *backend = active_audio_backend(); backend != nullptr) { - backend->stop_audio_playback(); + backend->audio_player().stop(); } } + /** @brief Release the active audio player after the transport audio thread exits. */ void on_audio_cleanup() { if (streaming::FfmpegStreamBackend *backend = active_audio_backend(); backend != nullptr) { - backend->cleanup_audio_decoder(); + backend->audio_player().cleanup(); active_audio_backend() = nullptr; } } + /** + * @brief Decode and queue the next Moonlight Opus payload. + * @param sampleData Encoded payload, or null for a lost packet. + * @param sampleLength Payload length in bytes. + */ void on_audio_decode_and_play_sample(char *sampleData, int sampleLength) { // NOSONAR(cpp:S995) moonlight-common-c supplies mutable sample data by callback signature. if (streaming::FfmpegStreamBackend *backend = active_audio_backend(); backend != nullptr) { - backend->decode_and_play_audio_sample(sampleData, sampleLength); + backend->audio_player().decode_and_queue(sampleData, sampleLength); } } @@ -604,14 +562,14 @@ namespace streaming { audioCallbacks->cleanup = on_audio_cleanup; audioCallbacks->decodeAndPlaySample = on_audio_decode_and_play_sample; audioCallbacks->capabilities = CAPABILITY_SLOW_OPUS_DECODER; - if (!audioPlaybackEnabled_.load()) { + if (!audio_.enabled()) { audioCallbacks->capabilities |= CAPABILITY_DIRECT_SUBMIT; } } } - void FfmpegStreamBackend::set_audio_playback_enabled(bool enabled) { - audioPlaybackEnabled_.store(enabled); + AudioPlayer &FfmpegStreamBackend::audio_player() { + return audio_; } void FfmpegStreamBackend::set_gpu_presentation_enabled(bool enabled) { @@ -619,7 +577,7 @@ namespace streaming { } void FfmpegStreamBackend::shutdown() { - cleanup_audio_decoder(); + audio_.cleanup(); cleanup_video_decoder(); if (screenshot_.thread != nullptr) { SDL_WaitThread(screenshot_.thread, nullptr); @@ -700,13 +658,9 @@ namespace streaming { } std::string FfmpegStreamBackend::build_overlay_status_line() const { - std::string audioState = "audio decode disabled"; - if (audioPlaybackEnabled_.load()) { - audioState = audio_.deviceId != 0 ? std::to_string(SDL_GetQueuedAudioSize(audio_.deviceId)) + " queued audio bytes" : "audio idle"; - } return std::string("Video units: ") + std::to_string(video_.submittedDecodeUnitCount.load()) + " submitted / " + std::to_string(video_.decodedFrameCount.load()) + " decoded / " + std::to_string(video_.droppedDecodeUnitCount.load()) + " dropped | frame " + std::to_string(video_.lastDecodeFrameNumber.load()) + " queue " + std::to_string(video_.lastDecodeQueueUs.load() / 1000U) + - " ms / age " + std::to_string(video_.lastReceiveAgeUs.load() / 1000U) + " ms | " + audioState; + " ms / age " + std::to_string(video_.lastReceiveAgeUs.load() / 1000U) + " ms | " + audio_.describe_playback(); } bool FfmpegStreamBackend::render_latest_video_frame(SDL_Renderer *renderer, int screenWidth, int screenHeight, bool allowDirectFramebuffer) { @@ -1333,257 +1287,4 @@ namespace streaming { return DR_OK; } - int FfmpegStreamBackend::initialize_audio_decoder(int audioConfiguration, const OPUS_MULTISTREAM_CONFIGURATION *opusConfig, int arFlags) { - (void) arFlags; - - cleanup_audio_decoder(); - - if (!audioPlaybackEnabled_.load()) { - logging::info("stream", "Xbox audio playback is disabled; dropping streamed audio before Opus decode"); - return 0; - } - - if (opusConfig == nullptr) { - logging::error("stream", "Moonlight did not provide an Opus configuration for audio startup"); - return -1; - } - - const int channelCount = CHANNEL_COUNT_FROM_AUDIO_CONFIGURATION(audioConfiguration); - if (channelCount <= 0 || channelCount > 2) { - logging::error("stream", "The FFmpeg backend currently supports mono or stereo audio only"); - return -1; - } - - SDL_AudioSpec desiredSpec {}; - desiredSpec.freq = opusConfig->sampleRate > 0 ? opusConfig->sampleRate : 48000; - desiredSpec.format = AUDIO_S16SYS; - desiredSpec.channels = static_cast(channelCount); - desiredSpec.samples = 1024; - desiredSpec.callback = nullptr; - - if ((SDL_WasInit(SDL_INIT_AUDIO) & SDL_INIT_AUDIO) == 0 && SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) { - logging::error("stream", std::string("SDL_InitSubSystem(SDL_INIT_AUDIO) failed for streaming playback: ") + SDL_GetError()); - cleanup_audio_decoder(); - return -1; - } - - audio_.deviceId = SDL_OpenAudioDevice(nullptr, 0, &desiredSpec, &audio_.obtainedSpec, 0); - if (audio_.deviceId == 0) { - logging::error("stream", std::string("SDL_OpenAudioDevice failed for streaming playback: ") + SDL_GetError()); - cleanup_audio_decoder(); - return -1; - } - - const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_OPUS); - if (codec == nullptr) { - logging::error("stream", "FFmpeg did not provide an Opus decoder"); - cleanup_audio_decoder(); - return -1; - } - - audio_.codecContext = avcodec_alloc_context3(codec); - audio_.decodedFrame = av_frame_alloc(); - audio_.packet = av_packet_alloc(); - if (audio_.codecContext == nullptr || audio_.decodedFrame == nullptr || audio_.packet == nullptr) { - logging::error("stream", "Failed to allocate FFmpeg audio decoder resources"); - cleanup_audio_decoder(); - return -1; - } - - audio_.codecContext->sample_rate = desiredSpec.freq; - av_channel_layout_default(&audio_.codecContext->ch_layout, channelCount); - if (!configure_opus_extradata(channelCount, desiredSpec.freq, audio_.codecContext)) { - logging::error("stream", "Failed to allocate Opus decoder extradata for FFmpeg"); - cleanup_audio_decoder(); - return -1; - } - - if (const int openResult = avcodec_open2(audio_.codecContext, codec, nullptr); openResult < 0) { - logging::error("stream", std::string("avcodec_open2 failed for Opus: ") + describe_ffmpeg_error(openResult)); - cleanup_audio_decoder(); - return openResult; - } - - return 0; - } - - void FfmpegStreamBackend::start_audio_playback() { - if (audio_.deviceId != 0) { - SDL_ClearQueuedAudio(audio_.deviceId); - SDL_PauseAudioDevice(audio_.deviceId, 0); - audio_.deviceStarted.store(true); - } - } - - void FfmpegStreamBackend::stop_audio_playback() { - if (audio_.deviceId != 0) { - SDL_PauseAudioDevice(audio_.deviceId, 1); - audio_.deviceStarted.store(false); - } - } - - void FfmpegStreamBackend::cleanup_audio_decoder() { - stop_audio_playback(); - - if (audio_.deviceId != 0) { - SDL_ClearQueuedAudio(audio_.deviceId); - SDL_CloseAudioDevice(audio_.deviceId); - audio_.deviceId = 0; - } - - if (audio_.packet != nullptr) { - av_packet_free(&audio_.packet); - } - if (audio_.decodedFrame != nullptr) { - av_frame_free(&audio_.decodedFrame); - } - if (audio_.codecContext != nullptr) { - avcodec_free_context(&audio_.codecContext); - } - if (audio_.resampleContext != nullptr) { - swr_free(&audio_.resampleContext); - } - - audio_.obtainedSpec = SDL_AudioSpec {}; - audio_.outputBuffer.clear(); - audio_.resampleInputSampleRate = 0; - audio_.resampleInputSampleFormat = -1; - audio_.resampleInputChannelCount = 0; - audio_.queuedAudioBytes.store(0); - } - - bool FfmpegStreamBackend::ensure_audio_resampler() { - if (audio_.decodedFrame == nullptr) { - return false; - } - - const int inputSampleRate = audio_.decodedFrame->sample_rate; - const int inputSampleFormat = audio_.decodedFrame->format; - const int inputChannelCount = audio_.decodedFrame->ch_layout.nb_channels; - if (const bool needsReconfigure = audio_.resampleContext == nullptr || audio_.resampleInputSampleRate != inputSampleRate || audio_.resampleInputSampleFormat != inputSampleFormat || audio_.resampleInputChannelCount != inputChannelCount; !needsReconfigure) { - return true; - } - - if (audio_.resampleContext != nullptr) { - swr_free(&audio_.resampleContext); - } - - AVChannelLayout outputLayout {}; - av_channel_layout_default(&outputLayout, audio_.obtainedSpec.channels); - const int resampleConfigResult = swr_alloc_set_opts2( - &audio_.resampleContext, - &outputLayout, - AV_SAMPLE_FMT_S16, - audio_.obtainedSpec.freq, - &audio_.decodedFrame->ch_layout, - static_cast(audio_.decodedFrame->format), - audio_.decodedFrame->sample_rate, - 0, - nullptr - ); - av_channel_layout_uninit(&outputLayout); - if (resampleConfigResult < 0) { - logging::warn("stream", std::string("swr_alloc_set_opts2 failed for Opus: ") + describe_ffmpeg_error(resampleConfigResult)); - return false; - } - if (audio_.resampleContext == nullptr || swr_init(audio_.resampleContext) < 0) { - logging::warn("stream", "swr_init failed for the streaming audio resampler"); - return false; - } - - audio_.resampleInputSampleRate = inputSampleRate; - audio_.resampleInputSampleFormat = inputSampleFormat; - audio_.resampleInputChannelCount = inputChannelCount; - return true; - } - - void FfmpegStreamBackend::decode_and_play_audio_sample(const char *sampleData, int sampleLength) { - if (!audioPlaybackEnabled_.load()) { - return; - } - - if (audio_.codecContext == nullptr || audio_.packet == nullptr || audio_.decodedFrame == nullptr || audio_.deviceId == 0) { - return; - } - - if (sampleData == nullptr || sampleLength <= 0) { - return; - } - - if (const int packetResult = av_new_packet(audio_.packet, sampleLength); packetResult < 0) { - logging::warn("stream", std::string("av_new_packet failed for audio decode: ") + describe_ffmpeg_error(packetResult)); - return; - } - - std::memcpy(audio_.packet->data, sampleData, static_cast(sampleLength)); - audio_.packet->size = sampleLength; - - const int sendResult = avcodec_send_packet(audio_.codecContext, audio_.packet); - av_packet_unref(audio_.packet); - if (sendResult < 0 && sendResult != AVERROR(EAGAIN)) { - logging::warn("stream", std::string("avcodec_send_packet failed for Opus: ") + describe_ffmpeg_error(sendResult)); - return; - } - - while (true) { - const int receiveResult = avcodec_receive_frame(audio_.codecContext, audio_.decodedFrame); - if (receiveResult == AVERROR(EAGAIN) || receiveResult == AVERROR_EOF) { - break; - } - if (receiveResult < 0) { - logging::warn("stream", std::string("avcodec_receive_frame failed for Opus: ") + describe_ffmpeg_error(receiveResult)); - return; - } - - if (!ensure_audio_resampler()) { - av_frame_unref(audio_.decodedFrame); - return; - } - - const int outputSamples = av_rescale_rnd( - swr_get_delay(audio_.resampleContext, audio_.decodedFrame->sample_rate) + audio_.decodedFrame->nb_samples, - audio_.obtainedSpec.freq, - audio_.decodedFrame->sample_rate, - AV_ROUND_UP - ); - const int outputBufferSize = av_samples_get_buffer_size( - nullptr, - audio_.obtainedSpec.channels, - outputSamples, - AV_SAMPLE_FMT_S16, - 1 - ); - if (outputBufferSize <= 0) { - logging::warn("stream", "av_samples_get_buffer_size failed for decoded audio output"); - av_frame_unref(audio_.decodedFrame); - return; - } - - audio_.outputBuffer.resize(static_cast(outputBufferSize)); - std::array outputData {audio_.outputBuffer.data()}; - const int convertedSamples = swr_convert( - audio_.resampleContext, - outputData.data(), - outputSamples, - const_cast(audio_.decodedFrame->extended_data), - audio_.decodedFrame->nb_samples - ); - if (convertedSamples < 0) { - logging::warn("stream", std::string("swr_convert failed for Opus: ") + describe_ffmpeg_error(convertedSamples)); - av_frame_unref(audio_.decodedFrame); - return; - } - - const int convertedBytes = convertedSamples * audio_.obtainedSpec.channels * static_cast(sizeof(std::int16_t)); - if (const Uint32 maxQueuedBytes = (audio_bytes_per_second(audio_.obtainedSpec) * STREAM_OVERLAY_AUDIO_QUEUE_LIMIT_MS) / 1000U; SDL_GetQueuedAudioSize(audio_.deviceId) > maxQueuedBytes) { - SDL_ClearQueuedAudio(audio_.deviceId); - } - if (convertedBytes > 0 && SDL_QueueAudio(audio_.deviceId, audio_.outputBuffer.data(), static_cast(convertedBytes)) == 0) { - audio_.queuedAudioBytes.fetch_add(static_cast(convertedBytes)); - } - - av_frame_unref(audio_.decodedFrame); - } - } - } // namespace streaming diff --git a/src/streaming/ffmpeg_stream_backend.h b/src/streaming/ffmpeg_stream_backend.h index 3e5af60..f6087a5 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -20,6 +20,7 @@ // local includes #include "src/app/screenshot_library.h" #include "src/streaming/aligned_media_buffer.h" +#include "src/streaming/audio_player.h" #include "src/streaming/ffmpeg_video_frame.h" #include "src/streaming/pvideo_presenter.h" #include "src/streaming/stats_overlay.h" @@ -27,7 +28,6 @@ struct AVCodecContext; struct AVFrame; struct AVPacket; -struct SwrContext; struct SwsContext; namespace streaming { @@ -41,8 +41,8 @@ namespace streaming { * @brief Owns the FFmpeg decode and SDL presentation state for one stream. * * The backend exposes Moonlight-compatible callback tables for video and audio, - * decodes negotiated video and Opus stereo audio with FFmpeg, presents the most - * recent decoded frame through SDL, and queues decoded PCM samples to SDL audio. + * presents the latest decoded video frame and delegates Opus decode and playback + * to the audio player. */ class FfmpegStreamBackend { public: @@ -68,11 +68,10 @@ namespace streaming { void initialize_callbacks(DECODER_RENDERER_CALLBACKS *videoCallbacks, AUDIO_RENDERER_CALLBACKS *audioCallbacks) const; /** - * @brief Configure whether streamed audio should be decoded and played locally. - * - * @param enabled True to decode and play Opus audio on the Xbox. + * @brief Get the stream's Opus decoder and playback device. + * @return Audio player owned by this backend. */ - void set_audio_playback_enabled(bool enabled); + AudioPlayer &audio_player(); /** * @brief Select the preferred presentation path before starting the session. @@ -257,46 +256,6 @@ namespace streaming { */ void cleanup_video_decoder(); - /** - * @brief Initialize the FFmpeg Opus decoder and SDL playback device. - * - * @param audioConfiguration Negotiated Moonlight audio configuration. - * @param opusConfig Negotiated Opus multistream parameters. - * @param arFlags Moonlight audio renderer flags. - * @return Zero on success. - */ - int initialize_audio_decoder(int audioConfiguration, const OPUS_MULTISTREAM_CONFIGURATION *opusConfig, int arFlags); - - /** - * @brief Start SDL audio playback. - */ - void start_audio_playback(); - - /** - * @brief Stop SDL audio playback. - */ - void stop_audio_playback(); - - /** - * @brief Clean up all FFmpeg audio decode resources. - */ - void cleanup_audio_decoder(); - - /** - * @brief Ensure the audio resampler matches the current decoded frame. - * - * @return True when the resampler is ready for audio conversion. - */ - bool ensure_audio_resampler(); - - /** - * @brief Decode and queue one Moonlight Opus audio payload. - * - * @param sampleData Encoded Opus payload. - * @param sampleLength Encoded payload size in bytes. - */ - void decode_and_play_audio_sample(const char *sampleData, int sampleLength); - private: /** * @brief Present a frame while the public wrapper records submission time. @@ -464,26 +423,8 @@ namespace streaming { std::atomic lastDecodeFrameNumber = 0; }; - /** - * @brief Hold FFmpeg and SDL state used by the audio path. - */ - struct AudioState { - AVCodecContext *codecContext = nullptr; - SwrContext *resampleContext = nullptr; - AVFrame *decodedFrame = nullptr; - AVPacket *packet = nullptr; - SDL_AudioDeviceID deviceId = 0; - SDL_AudioSpec obtainedSpec {}; - AlignedMediaBuffer outputBuffer; ///< Aligned resampler output. - int resampleInputSampleRate = 0; - int resampleInputSampleFormat = -1; - int resampleInputChannelCount = 0; - std::atomic deviceStarted = false; - std::atomic queuedAudioBytes = 0; - }; - VideoState video_ {}; - AudioState audio_ {}; + AudioPlayer audio_; ///< Session Opus decoder and SDL playback device. ScreenshotTask screenshot_ {}; ///< Single capture slot, bounded to one additional YUV frame. PvideoPresenter presenter_; ///< Main-thread GPU presentation resources. VideoOverlay statisticsOverlay_; ///< Shared cached panel for all presentation paths. @@ -495,7 +436,6 @@ namespace streaming { VideoAccelerationReport acceleration_; ///< Session diagnostics survive decoder cleanup. bool gpuPresentationEnabled_ = false; ///< Session opt-in applied before decoder setup. bool overlayFailed_ = false; ///< Avoid repeating a known failing hardware path this session. - std::atomic audioPlaybackEnabled_ = false; }; } // namespace streaming diff --git a/src/streaming/session.cpp b/src/streaming/session.cpp index 14f713e..e28058a 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -1535,7 +1535,7 @@ namespace streaming { launchConfiguration.height = select_stream_height(streamParameters.videoMode); launchConfiguration.fps = streamParameters.fps; launchConfiguration.audioConfiguration = AUDIO_CONFIGURATION_STEREO; - launchConfiguration.playAudioOnPc = settings.playAudioOnPc; + launchConfiguration.playAudioOnPc = settings.audio.playOnPc; launchConfiguration.clientRefreshRateX100 = select_client_refresh_rate_x100(activeStreamVideoMode); launchConfiguration.remoteInputAesKeyHex = hex_encode(remoteInputKey.data(), remoteInputKey.size()); launchConfiguration.remoteInputAesIvHex = hex_encode(remoteInputIv.data(), remoteInputIv.size()); @@ -1566,9 +1566,9 @@ namespace streaming { "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) ); - resources.mediaBackend.set_audio_playback_enabled(settings.playAudioOnXbox); + resources.mediaBackend.audio_player().configure(settings.audio.mode, settings.audio.playOnXbox); resources.mediaBackend.set_gpu_presentation_enabled(settings.presentation.gpuPresentation); - logging::info("stream", std::string("Xbox audio playback ") + (settings.playAudioOnXbox ? "enabled" : "disabled for lower decode latency")); + logging::info("stream", std::string("Xbox audio mode: ") + (settings.audio.playOnXbox ? audio_mode_label(settings.audio.mode) : "Disabled")); logging::debug( "stream", std::string("Stream connection metadata | active=") + printable_log_value(startContext.serverInformation.address == nullptr ? std::string_view {} : std::string_view {startContext.serverInformation.address}) + diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 14496ae..1768553 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -2493,7 +2493,7 @@ namespace { } /** - * @brief Snapshot persisted shell and stream preferences, including screenshot storage and OSK placement. + * @brief Snapshot persisted shell and stream preferences, including playback mode. * @param state Current client state. * @return Settings snapshot ready for TOML serialization. */ @@ -2506,12 +2506,13 @@ namespace { state.settings.preferredVideoModeSet, state.settings.streamFramerate, state.settings.streamBitrateKbps, - state.settings.playAudioOnPc, + state.settings.audio.playOnPc, state.settings.presentation.showPerformanceStats, - state.settings.playAudioOnXbox, + state.settings.audio.playOnXbox, state.settings.controls.screenshotDirectory, state.settings.controls.keyboardDockTop, state.settings.presentation.gpuPresentation, + state.settings.audio.mode, }; } diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index 0c903a6..8b60ee0 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -7,6 +7,8 @@ // standard includes #include +#include +#include #include #include #include @@ -206,7 +208,7 @@ namespace { } /** - * @brief Name the selected settings section without conflating Display and Storage. + * @brief Name the selected settings section. * @param category Active settings category. * @return User-facing category name. */ @@ -216,6 +218,8 @@ namespace { return "Logging"; case app::SettingsCategory::display: return "Display"; + case app::SettingsCategory::audio: + return "Audio"; case app::SettingsCategory::input: return "Input"; case app::SettingsCategory::storage: @@ -384,7 +388,12 @@ namespace { return lines; } if (state.settings.selectedCategory == app::SettingsCategory::display) { - lines.emplace_back("Choose stream video, audio and performance statistics options."); + lines.emplace_back("Choose stream video and performance statistics options."); + return lines; + } + + if (state.settings.selectedCategory == app::SettingsCategory::audio) { + lines.emplace_back("Choose where stream audio plays and select stereo or mono for the Xbox."); return lines; } @@ -422,35 +431,24 @@ namespace { * @return Packaged Lucide SVG path, or an empty path for undecorated actions. */ std::string menu_icon(const std::string &id) { - if (id == "settings-category:logging") { - return "icons\\scroll-text.svg"; - } - if (id == "settings-category:display") { - return "icons\\monitor.svg"; - } - if (id == "settings-category:input") { - return "icons\\gamepad-2.svg"; - } - if (id == "settings-category:storage") { - return "icons\\save.svg"; - } - if (id == "settings-category:reset") { - return "icons\\rotate-ccw.svg"; - } - if (id == "edit-address") { - return "icons\\network.svg"; - } - if (id == "edit-port") { - return "icons\\hash.svg"; - } - if (id == "test-connection") { - return "icons\\activity.svg"; - } - if (id == "start-pairing") { - return "icons\\key-round.svg"; - } - if (id == "save-host") { - return "icons\\save.svg"; + using IconEntry = std::pair; + static constexpr std::array icons { + IconEntry {"settings-category:logging", "icons\\scroll-text.svg"}, + IconEntry {"settings-category:display", "icons\\monitor.svg"}, + IconEntry {"settings-category:audio", "icons\\volume-2.svg"}, + IconEntry {"settings-category:input", "icons\\gamepad-2.svg"}, + IconEntry {"settings-category:storage", "icons\\save.svg"}, + IconEntry {"settings-category:reset", "icons\\rotate-ccw.svg"}, + IconEntry {"edit-address", "icons\\network.svg"}, + IconEntry {"edit-port", "icons\\hash.svg"}, + IconEntry {"test-connection", "icons\\activity.svg"}, + IconEntry {"start-pairing", "icons\\key-round.svg"}, + IconEntry {"save-host", "icons\\save.svg"}, + }; + for (const auto &[action, path] : icons) { + if (id == action) { + return path; + } } return {}; } diff --git a/tests/unit/app/client_state_test.cpp b/tests/unit/app/client_state_test.cpp index 8105c4f..5fe5f2c 100644 --- a/tests/unit/app/client_state_test.cpp +++ b/tests/unit/app/client_state_test.cpp @@ -833,6 +833,7 @@ namespace { EXPECT_TRUE(state.hosts.active.appListStatusMessage.empty()); } + /** @brief Select a saved file in Reset and confirm the deletion request. */ TEST(ClientStateTest, SettingsCanRequestDeletionOfSavedFilesFromTheResetCategory) { app::ClientState state = app::create_initial_state(); app::handle_command(state, input::UiCommand::move_left); @@ -843,10 +844,7 @@ namespace { app::replace_saved_files(state, { {R"(E:\UDATA\12345678\moonlight.log)", "moonlight.log", 128U}, }); - app::handle_command(state, input::UiCommand::move_down); - app::handle_command(state, input::UiCommand::move_down); - app::handle_command(state, input::UiCommand::move_down); - app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_up); app::handle_command(state, input::UiCommand::activate); ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::reset); ASSERT_EQ(state.settings.focusArea, app::SettingsFocusArea::options); @@ -863,6 +861,7 @@ namespace { EXPECT_EQ(update.persistence.savedFileDeletePath, "E:\\UDATA\\12345678\\moonlight.log"); } + /** @brief Confirm a factory reset through the Reset category. */ TEST(ClientStateTest, SettingsFactoryResetUsesAConfirmationDialog) { app::ClientState state = app::create_initial_state(); app::handle_command(state, input::UiCommand::move_left); @@ -870,10 +869,7 @@ namespace { app::handle_command(state, input::UiCommand::activate); ASSERT_EQ(state.shell.activeScreen, app::ScreenId::settings); - app::handle_command(state, input::UiCommand::move_down); - app::handle_command(state, input::UiCommand::move_down); - app::handle_command(state, input::UiCommand::move_down); - app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_up); app::handle_command(state, input::UiCommand::activate); ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::reset); ASSERT_EQ(state.settings.focusArea, app::SettingsFocusArea::options); @@ -1471,18 +1467,20 @@ namespace { } /** - * @brief Other display controls leave a manually selected bitrate unchanged. + * @brief Display keeps the statistics toggle and the manually selected bitrate. */ TEST(ClientStateTest, DisplaySettingsPreserveManualBitrateWhenOtherOptionsChange) { 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"}; - for (const char *control : controls) { - ASSERT_TRUE(state.detailMenu.select_item_by_id(control)); - app::handle_command(state, input::UiCommand::activate); - EXPECT_EQ(state.settings.streamBitrateKbps, 1500); - } + EXPECT_FALSE(state.detailMenu.select_item_by_id("toggle-play-audio-on-pc")); + EXPECT_FALSE(state.detailMenu.select_item_by_id("toggle-play-audio-on-xbox")); + EXPECT_FALSE(state.detailMenu.select_item_by_id("cycle-audio-mode")); + ASSERT_TRUE(state.detailMenu.select_item_by_id("toggle-show-performance-stats")); + EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).persistence.settingsChanged); + EXPECT_TRUE(state.settings.presentation.showPerformanceStats); + EXPECT_EQ(state.settings.streamBitrateKbps, 1500); + EXPECT_EQ(state.detailMenu.selected_item()->label, "Performance Stats: On"); } /** @brief Switch from the software default to GPU and back while preserving bitrate and statistics. */ @@ -1509,44 +1507,80 @@ namespace { EXPECT_EQ(state.detailMenu.selected_item()->label, "Video Presentation: Software"); } - /** @brief Toggle audio and statistics independently while retaining the selected presentation path. */ - TEST(ClientStateTest, DisplaySettingsCanToggleXboxAudioAndPerformanceStats) { + /** + * @brief Navigate to Audio next to Display and focus its options. + * @param state Client state starting on the hosts screen. + */ + void open_audio_settings_options(app::ClientState &state) { + app::handle_command(state, input::UiCommand::move_left); + app::handle_command(state, input::UiCommand::move_left); + app::handle_command(state, input::UiCommand::activate); + ASSERT_EQ(state.shell.activeScreen, app::ScreenId::settings); + + app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_down); + EXPECT_EQ(app::handle_command(state, input::UiCommand::activate).navigation.activatedItemId, "settings-category:audio"); + ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::audio); + ASSERT_EQ(state.settings.focusArea, app::SettingsFocusArea::options); + } + + /** @brief Cycle every audio mode and persist the selection without leaving the row. */ + TEST(ClientStateTest, AudioSettingsCycleModesAndRetainFocus) { + using streaming::AudioMode; + auto state = app::create_initial_state(); + ASSERT_NO_FATAL_FAILURE(open_audio_settings_options(state)); + ASSERT_TRUE(state.detailMenu.select_item_by_id("cycle-audio-mode")); + ASSERT_EQ(state.detailMenu.selected_item()->label, "Audio Mode: Stereo"); + + constexpr std::array modes {AudioMode::mono, AudioMode::stereo}; + constexpr std::array labels {"Audio Mode: Mono", "Audio Mode: Stereo"}; + for (std::size_t i = 0; i < modes.size(); ++i) { + state.settings.dirty = false; + EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).persistence.settingsChanged); + EXPECT_TRUE(state.settings.dirty); + EXPECT_TRUE(state.settings.audio.playOnXbox); + EXPECT_EQ(state.settings.audio.mode, modes[i]); + ASSERT_NE(state.detailMenu.selected_item(), nullptr); + EXPECT_EQ(state.detailMenu.selected_item()->id, "cycle-audio-mode"); + EXPECT_EQ(state.detailMenu.selected_item()->label, labels[i]); + } + } + + /** @brief Show and independently toggle the default PC and Xbox playback choices. */ + TEST(ClientStateTest, AudioSettingsDefaultPcOffAndXboxOn) { app::ClientState state = app::create_initial_state(); - ASSERT_TRUE(state.settings.playAudioOnXbox); - ASSERT_FALSE(state.settings.presentation.showPerformanceStats); - state.settings.presentation.gpuPresentation = true; + state.settings.streamBitrateKbps = 1500; + ASSERT_NO_FATAL_FAILURE(open_audio_settings_options(state)); + ASSERT_EQ(state.detailMenu.items().size(), 3U); - ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); + ASSERT_NE(state.detailMenu.selected_item(), nullptr); + EXPECT_EQ(state.detailMenu.selected_item()->id, "toggle-play-audio-on-pc"); + EXPECT_EQ(state.detailMenu.selected_item()->label, "Play Audio on PC: Off"); + EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).persistence.settingsChanged); + EXPECT_TRUE(state.settings.audio.playOnPc); + EXPECT_TRUE(state.settings.audio.playOnXbox); + EXPECT_EQ(state.detailMenu.selected_item()->label, "Play Audio on PC: On"); ASSERT_TRUE(state.detailMenu.select_item_by_id("toggle-play-audio-on-xbox")); - app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(state.detailMenu.selected_item()->label, "Play Audio on Xbox: On"); + const auto update = app::handle_command(state, input::UiCommand::activate); EXPECT_EQ(update.navigation.activatedItemId, "toggle-play-audio-on-xbox"); EXPECT_TRUE(update.persistence.settingsChanged); - EXPECT_FALSE(state.settings.playAudioOnXbox); + EXPECT_TRUE(state.settings.audio.playOnPc); + EXPECT_FALSE(state.settings.audio.playOnXbox); + EXPECT_EQ(state.settings.streamBitrateKbps, 1500); EXPECT_EQ(state.shell.statusMessage, "Play audio on Xbox disabled"); ASSERT_NE(state.detailMenu.selected_item(), nullptr); EXPECT_EQ(state.detailMenu.selected_item()->label, "Play Audio on Xbox: Off"); - - ASSERT_TRUE(state.detailMenu.select_item_by_id("toggle-show-performance-stats")); - 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_EQ(state.shell.statusMessage, "Performance stats enabled"); - ASSERT_NE(state.detailMenu.selected_item(), nullptr); - EXPECT_EQ(state.detailMenu.selected_item()->label, "Performance Stats: On"); } + /** @brief Cancel the Reset confirmation without requesting any persistent changes. */ TEST(ClientStateTest, ConfirmationModalCanBeCancelledWithoutRequestingPersistenceChanges) { app::ClientState state = app::create_initial_state(); 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::move_down); - app::handle_command(state, input::UiCommand::move_down); - app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_up); app::handle_command(state, input::UiCommand::activate); ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::reset); diff --git a/tests/unit/app/directory_picker_test.cpp b/tests/unit/app/directory_picker_test.cpp index 2b8915b..5f256b2 100644 --- a/tests/unit/app/directory_picker_test.cpp +++ b/tests/unit/app/directory_picker_test.cpp @@ -31,6 +31,7 @@ namespace { app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::activate); state.detailMenu.select_item_by_id("screenshot-directory"); EXPECT_EQ(state.detailMenu.selected_item()->id, "screenshot-directory"); @@ -61,7 +62,7 @@ TEST(DirectoryPickerTest, StagesConfiguredAndDefaultLocationsUntilAccepted) { auto state = open_picker(configured); EXPECT_EQ(state.settings.selectedCategory, app::SettingsCategory::storage); EXPECT_EQ(state.menu.items()[1].label, "Display"); - EXPECT_EQ(state.menu.items()[3].label, "Storage"); + EXPECT_EQ(state.menu.items()[4].label, "Storage"); EXPECT_EQ(state.directoryPicker.path, configured.empty() ? startup::default_storage_path("screenshots") : configured); auto view = ui::build_shell_view_model(state, {}); EXPECT_EQ(view.modal.title, "Screenshot Folder"); @@ -240,13 +241,13 @@ TEST(DirectoryPickerTest, BuildsScrollableFoldersAndControllerOrKeyboardNameEntr } /** - * @brief Storage owns screenshot destinations while Display retains its original stream and presentation options. + * @brief Storage owns screenshot destinations independently of the expanded settings list. */ TEST(DirectoryPickerTest, KeepsStorageSeparateFromTheExistingDisplayCategory) { auto state = open_picker("F:\\Screenshots"); - ASSERT_EQ(state.menu.items().size(), 5); - EXPECT_EQ(state.menu.items()[3].id, "settings-category:storage"); - EXPECT_EQ(state.menu.items()[4].id, "settings-category:reset"); + ASSERT_EQ(state.menu.items().size(), 6U); + EXPECT_EQ(state.menu.items()[4].id, "settings-category:storage"); + EXPECT_EQ(state.menu.items()[5].id, "settings-category:reset"); app::handle_command(state, input::UiCommand::back); ASSERT_EQ(state.detailMenu.items().size(), 1); EXPECT_EQ(state.detailMenu.selected_item()->id, "screenshot-directory"); @@ -255,6 +256,7 @@ TEST(DirectoryPickerTest, KeepsStorageSeparateFromTheExistingDisplayCategory) { app::handle_command(state, input::UiCommand::back); app::handle_command(state, input::UiCommand::move_up); app::handle_command(state, input::UiCommand::move_up); + app::handle_command(state, input::UiCommand::move_up); 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"); diff --git a/tests/unit/app/settings_storage_test.cpp b/tests/unit/app/settings_storage_test.cpp index 8db2074..c424722 100644 --- a/tests/unit/app/settings_storage_test.cpp +++ b/tests/unit/app/settings_storage_test.cpp @@ -136,6 +136,42 @@ namespace { EXPECT_EQ(app::load_app_settings(settingsPath).settings.streamBitrateKbps, 10000); } + /** @brief Apply audio defaults to older files and preserve explicit mode and playback choices. */ + TEST_F(SettingsStorageTest, SavesAndLoadsAudioModes) { + using streaming::AudioMode; + write_text_file(settingsPath, "[streaming]\nfps = 30\n"); + const auto oldSettings = app::load_app_settings(settingsPath); + EXPECT_EQ(oldSettings.settings.audioMode, AudioMode::stereo); + EXPECT_FALSE(oldSettings.settings.playAudioOnPc); + EXPECT_TRUE(oldSettings.settings.playAudioOnXbox); + EXPECT_TRUE(oldSettings.warnings.empty()); + + for (const auto mode : {AudioMode::stereo, AudioMode::mono}) { + app::AppSettings settings; + settings.audioMode = mode; + settings.playAudioOnPc = true; + settings.playAudioOnXbox = false; + ASSERT_TRUE(app::save_app_settings(settings, settingsPath).success); + const auto reloaded = app::load_app_settings(settingsPath); + EXPECT_EQ(reloaded.settings.audioMode, mode); + EXPECT_TRUE(reloaded.settings.playAudioOnPc); + EXPECT_FALSE(reloaded.settings.playAudioOnXbox); + EXPECT_TRUE(reloaded.warnings.empty()); + EXPECT_FALSE(reloaded.cleanupRequired); + } + } + + /** @brief Warn about unknown or mistyped audio modes and use the stereo default. */ + TEST_F(SettingsStorageTest, InvalidAudioModesFallBackToStereo) { + for (const auto *value : {"'5.1'", "6"}) { + write_text_file(settingsPath, std::string("[streaming]\naudio_mode = ") + value + "\n"); + const auto invalid = app::load_app_settings(settingsPath); + EXPECT_EQ(invalid.settings.audioMode, streaming::AudioMode::stereo); + ASSERT_EQ(invalid.warnings.size(), 1U); + EXPECT_NE(invalid.warnings.front().find("streaming.audio_mode"), std::string::npos); + } + } + /** @brief Missing settings leave the default stream profile and produce no warnings. */ TEST_F(SettingsStorageTest, MissingFilesReturnDefaultsWithoutWarnings) { const app::LoadAppSettingsResult loadResult = app::load_app_settings(settingsPath); diff --git a/tests/unit/ui/shell_view_test.cpp b/tests/unit/ui/shell_view_test.cpp index e6ec2de..1302f44 100644 --- a/tests/unit/ui/shell_view_test.cpp +++ b/tests/unit/ui/shell_view_test.cpp @@ -151,7 +151,7 @@ namespace { } /** - * @brief All five settings categories, including separate Display and Storage sections, have packaged icons. + * @brief The Audio entry and the other settings categories have packaged icons. */ TEST(ShellViewTest, DecoratesSettingsCategoriesWithIcons) { app::ClientState state = app::create_initial_state(); @@ -159,13 +159,33 @@ namespace { app::handle_command(state, input::UiCommand::move_left); app::handle_command(state, input::UiCommand::activate); const auto view = ui::build_shell_view_model(state, {}); - ASSERT_EQ(view.content.menuRows.size(), 5U); + ASSERT_EQ(view.content.menuRows.size(), 6U); EXPECT_EQ(view.content.menuRows[0].iconAssetPath, "icons\\scroll-text.svg"); EXPECT_EQ(view.content.menuRows[1].iconAssetPath, "icons\\monitor.svg"); - EXPECT_EQ(view.content.menuRows[2].iconAssetPath, "icons\\gamepad-2.svg"); - EXPECT_EQ(view.content.menuRows[3].label, "Storage"); - EXPECT_EQ(view.content.menuRows[3].iconAssetPath, "icons\\save.svg"); - EXPECT_EQ(view.content.menuRows[4].iconAssetPath, "icons\\rotate-ccw.svg"); + EXPECT_EQ(view.content.menuRows[2].label, "Audio"); + EXPECT_EQ(view.content.menuRows[2].iconAssetPath, "icons\\volume-2.svg"); + EXPECT_EQ(view.content.menuRows[3].iconAssetPath, "icons\\gamepad-2.svg"); + EXPECT_EQ(view.content.menuRows[4].label, "Storage"); + EXPECT_EQ(view.content.menuRows[4].iconAssetPath, "icons\\save.svg"); + EXPECT_EQ(view.content.menuRows[5].iconAssetPath, "icons\\rotate-ccw.svg"); + } + + /** @brief Show playback help and the three Audio controls when the category is selected. */ + TEST(ShellViewTest, ShowsAudioDetailsOnTheSettingsScreen) { + auto state = app::create_initial_state(); + 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::move_down); + + const auto view = ui::build_shell_view_model(state, {}); + ASSERT_FALSE(view.content.bodyLines.empty()); + EXPECT_EQ(view.content.bodyLines.front(), "Category: Audio"); + ASSERT_EQ(view.content.detailMenuRows.size(), 3U); + EXPECT_EQ(view.content.detailMenuRows[0].label, "Play Audio on PC: Off"); + EXPECT_EQ(view.content.detailMenuRows[1].label, "Play Audio on Xbox: On"); + EXPECT_EQ(view.content.detailMenuRows[2].label, "Audio Mode: Stereo"); } /**