From ba5e19ff88dab41ac3ffc084a8c27c1cfa230cc3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:07:40 -0800 Subject: [PATCH 1/5] MgbaLink: connection and video source for mGBA (lab slice 1) MgbaLink_Connection speaks MgbaLink v1 to bridge.lua over a ClientSocket (HELLO, FRAME, KEYS with LATE, BYE on close). VideoSource_MgbaLink polls FRAME, composes the 240x160 picture nearest-neighbour into GAME_BOX of a 1920x1080 frame and stamps it from the frame number. Developer mode lists it as a video source. Unit tests: framing, replies, LATE, frame clock, compose geometry, and a session against a fake bridge. --- .../Sources/VideoSource_MgbaLink.cpp | 223 ++++++++++ .../Sources/VideoSource_MgbaLink.h | 134 ++++++ .../Sources/VideoSource_MgbaLink_Tests.cpp | 130 ++++++ .../Sources/VideoSource_MgbaLink_Tests.h | 20 + .../UI/VideoSourceSelectorWidget.cpp | 4 + .../VideoPipeline/VideoSourceDescriptor.h | 1 + .../VideoPipeline/VideoSourceOption.cpp | 11 + .../ComputerPrograms_UnitTestRunner.cpp | 4 + .../MgbaLink/MgbaLink_Connection.cpp | 185 ++++++++ .../MgbaLink/MgbaLink_Connection.h | 100 +++++ .../MgbaLink/MgbaLink_Protocol.cpp | 156 +++++++ .../Controllers/MgbaLink/MgbaLink_Protocol.h | 123 ++++++ .../Controllers/MgbaLink/MgbaLink_Tests.cpp | 412 ++++++++++++++++++ .../Controllers/MgbaLink/MgbaLink_Tests.h | 24 + SerialPrograms/cmake/SourceFiles.cmake | 10 + 15 files changed, 1537 insertions(+) create mode 100644 SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp create mode 100644 SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h create mode 100644 SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp create mode 100644 SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h create mode 100644 SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp create mode 100644 SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h create mode 100644 SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp create mode 100644 SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h create mode 100644 SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp create mode 100644 SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp new file mode 100644 index 0000000000..5ea950d000 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp @@ -0,0 +1,223 @@ +/* Video Source (mGBA over MgbaLink) + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/ImageTypes/ImageRGB32_Qt.h" +#include "CommonFramework/Tools/GlobalThreadPools.h" +#include "VideoSource_MgbaLink.h" + +namespace PokemonAutomation{ + +using namespace MgbaLink; + + +namespace{ + +// Re-anchor the frame clock when a frame maps further than this from the +// time it was read (mGBA paused, or running free without GRANT pacing). +constexpr std::chrono::milliseconds MAX_CLOCK_DRIFT(500); + +constexpr std::chrono::milliseconds RECONNECT_DELAY(2000); + +// One GBA frame, rounded down: polling faster only repeats a frame number. +constexpr std::chrono::milliseconds POLL_PERIOD(16); + +} + + + +ImageRGB32 compose_mgba_frame(const ImageViewRGB32& gba_frame, const MgbaComposeGeometry& geometry){ + ImageRGB32 canvas(geometry.canvas_width, geometry.canvas_height); + canvas.fill(0xff000000); + const size_t src_width = gba_frame.width(); + const size_t src_height = gba_frame.height(); + if (src_width == 0 || src_height == 0){ + return canvas; + } + std::vector src_x(geometry.box_width); + for (size_t x = 0; x < geometry.box_width; x++){ + src_x[x] = (2 * x + 1) * src_width / (2 * geometry.box_width); + } + for (size_t y = 0; y < geometry.box_height; y++){ + const size_t sy = (2 * y + 1) * src_height / (2 * geometry.box_height); + for (size_t x = 0; x < geometry.box_width; x++){ + canvas.pixel(geometry.box_x + x, geometry.box_y + y) = gba_frame.pixel(src_x[x], sy); + } + } + return canvas; +} + + +std::optional> parse_mgba_address(std::string_view address){ + const size_t colon = address.rfind(':'); + if (colon == std::string_view::npos || colon == 0){ + return std::nullopt; + } + std::string_view port_text = address.substr(colon + 1); + uint16_t port = 0; + const char* end = port_text.data() + port_text.size(); + std::from_chars_result result = std::from_chars(port_text.data(), end, port); + if (port_text.empty() || result.ec != std::errc() || result.ptr != end || port == 0){ + return std::nullopt; + } + return std::make_pair(std::string(address.substr(0, colon)), port); +} + + + +std::string VideoSourceDescriptor_MgbaLink::address() const{ + ReadSpinLock lg(m_lock, PA_CURRENT_FUNCTION); + return m_address; +} +bool VideoSourceDescriptor_MgbaLink::operator==(const VideoSourceDescriptor& x) const{ + if (typeid(*this) != typeid(x)){ + return false; + } + return address() == static_cast(x).address(); +} +std::string VideoSourceDescriptor_MgbaLink::display_name() const{ + return "mGBA (" + address() + ")"; +} +JsonValue VideoSourceDescriptor_MgbaLink::to_json() const{ + return address(); +} +void VideoSourceDescriptor_MgbaLink::load_json(const JsonValue& json){ + const std::string* address = json.to_string(); + if (address != nullptr && parse_mgba_address(*address)){ + WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); + m_address = *address; + } +} +std::unique_ptr VideoSourceDescriptor_MgbaLink::make_VideoSource( + Logger& logger, + Resolution resolution, + VideoFormat format, + FramesPerSecond fps +) const{ + return std::make_unique(logger, address()); +} + + + +VideoSource_MgbaLink::VideoSource_MgbaLink(Logger& logger, std::string address) + : VideoSource(logger, false) + , m_logger(logger) + , m_address(std::move(address)) +{ + m_formats[{1920, 1080}][VideoFormat::OTHER] = {60}; + m_poller = GlobalThreadPools::unlimited_realtime().dispatch_now_blocking( + [this]{ poll_loop(); } + ); +} +VideoSource_MgbaLink::~VideoSource_MgbaLink(){ + { + std::lock_guard lg(m_lock); + m_stopping = true; + } + m_cv.notify_all(); + m_poller.wait_and_ignore_exceptions(); +} + + +VideoSnapshot VideoSource_MgbaLink::snapshot_latest_blocking(){ + std::lock_guard lg(m_lock); + return m_last; +} +VideoSnapshot VideoSource_MgbaLink::snapshot_recent_nonblocking(WallClock min_time){ + std::lock_guard lg(m_lock); + return m_last; +} + + +bool VideoSource_MgbaLink::wait_or_stop(std::chrono::milliseconds duration){ + std::unique_lock lg(m_lock); + return m_cv.wait_for(lg, duration, [this]{ return m_stopping; }); +} + +void VideoSource_MgbaLink::poll_loop(){ + std::optional> address = parse_mgba_address(m_address); + if (!address){ + m_logger.log("MgbaLink: invalid address \"" + m_address + "\"; expected host:port.", COLOR_RED); + return; + } + do{ + try{ + MgbaLink_Connection connection( + m_logger, GlobalThreadPools::unlimited_realtime(), address->first, address->second + ); + connection.open(); + poll_connected(connection); + }catch (Exception& e){ + m_logger.log(e.message() + " Retrying in 2 s.", COLOR_ORANGE); + } + }while (!wait_or_stop(RECONNECT_DELAY)); +} + +void VideoSource_MgbaLink::poll_connected(MgbaLink_Connection& connection){ + std::optional clock; + uint64_t last_frame = 0; + do{ + FrameReply reply = connection.frame(); + WallClock now = current_time(); + if (clock && reply.frame == last_frame){ + continue; + } + if (!clock){ + clock.emplace(now, reply.frame); + }else if (clock->resync(now, reply.frame, MAX_CLOCK_DRIFT)){ + m_logger.log("MgbaLink: frame clock re-anchored at frame " + std::to_string(reply.frame) + "."); + } + last_frame = reply.frame; + + // Read the PNG before the next FRAME overwrites it. + ImageRGB32 gba_frame(reply.path); + VideoSnapshot snapshot(compose_mgba_frame(gba_frame), clock->timestamp(reply.frame)); + std::lock_guard lg(m_lock); + m_last = std::move(snapshot); + }while (!wait_or_stop(POLL_PERIOD)); +} + + + +class VideoWidget_MgbaLink : public QWidget{ +public: + VideoWidget_MgbaLink(QWidget* parent, VideoSource_MgbaLink& source) + : QWidget(parent) + , m_source(source) + { + QTimer* timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, [this]{ update(); }); + timer->start(33); + } + +private: + virtual void paintEvent(QPaintEvent* event) override{ + QWidget::paintEvent(event); + VideoSnapshot snapshot = m_source.snapshot_latest_blocking(); + if (!snapshot.frame || snapshot.frame->width() == 0){ + return; + } + QPainter painter(this); + painter.drawImage(QRect(0, 0, width(), height()), to_QImage_ref(*snapshot.frame)); + } + +private: + VideoSource_MgbaLink& m_source; +}; + + +QWidget* VideoSource_MgbaLink::make_display_QtWidget(QWidget* parent){ + return new VideoWidget_MgbaLink(parent, *this); +} + + + +} diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h new file mode 100644 index 0000000000..ceedd8e4df --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h @@ -0,0 +1,134 @@ +/* Video Source (mGBA over MgbaLink) + * + * From: https://github.com/PokemonAutomation/ + * + * Shows the picture of an mGBA instance running "bridge.lua" (MgbaLink v1). + * Each new emulated frame is composed into a 1920x1080 frame at FRLG's + * GAME_BOX with nearest-neighbour scaling, so detectors see console + * geometry, and is stamped with the time its frame number maps to. + * Developer mode only (fork lab tooling). + * + */ + +#ifndef PokemonAutomation_VideoPipeline_VideoSource_MgbaLink_H +#define PokemonAutomation_VideoPipeline_VideoSource_MgbaLink_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/AsyncTask.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/Concurrency/SpinLock.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "Controllers/MgbaLink/MgbaLink_Connection.h" +#include "CommonFramework/VideoPipeline/VideoSourceDescriptor.h" +#include "CommonFramework/VideoPipeline/VideoSource.h" + +namespace PokemonAutomation{ + + +// Where the GBA picture goes: GAME_BOX {0.09375, 0.00462963, 0.8125, +// 0.962963} of a 1920x1080 frame, i.e. x = 180..1740, y = 5..1045 (6.5x). +struct MgbaComposeGeometry{ + size_t canvas_width = 1920; + size_t canvas_height = 1080; + size_t box_x = 180; + size_t box_y = 5; + size_t box_width = 1560; + size_t box_height = 1040; +}; + +// Draws "gba_frame" into a black canvas at the box, nearest-neighbour with +// pixel-centre sampling (as Pillow's NEAREST, which the geometry gate used): +// canvas pixel x takes source column floor((2 (x - box_x) + 1) w / (2 box_width)). +ImageRGB32 compose_mgba_frame( + const ImageViewRGB32& gba_frame, + const MgbaComposeGeometry& geometry = MgbaComposeGeometry() +); + + +// "host:port" -> (host, port). nullopt if the port is missing or invalid. +std::optional> parse_mgba_address(std::string_view address); + + + +class VideoSourceDescriptor_MgbaLink : public VideoSourceDescriptor{ +public: + static constexpr const char* DEFAULT_ADDRESS = "127.0.0.1:47801"; + + VideoSourceDescriptor_MgbaLink() + : VideoSourceDescriptor(VideoSourceType::MgbaLink) + , m_address(DEFAULT_ADDRESS) + {} + + std::string address() const; + + virtual bool operator==(const VideoSourceDescriptor& x) const override; + virtual std::string display_name() const override; + + virtual JsonValue to_json() const override; + virtual void load_json(const JsonValue& json) override; + + virtual std::unique_ptr make_VideoSource( + Logger& logger, + Resolution resolution, + VideoFormat format, + FramesPerSecond fps + ) const override; + +private: + mutable SpinLock m_lock; + std::string m_address; +}; + + + +class VideoSource_MgbaLink : public VideoSource{ +public: + VideoSource_MgbaLink(Logger& logger, std::string address); + ~VideoSource_MgbaLink(); + + virtual Resolution current_resolution() const override{ + return Resolution(1920, 1080); + } + virtual VideoFormat current_format() const override{ + return VideoFormat::OTHER; + } + virtual FramesPerSecond current_fps() const override{ + return 60; + } + virtual const VideoFormatSet& supported_formats() const override{ + return m_formats; + } + + virtual VideoSnapshot snapshot_latest_blocking() override; + virtual VideoSnapshot snapshot_recent_nonblocking(WallClock min_time) override; + + virtual QWidget* make_display_QtWidget(QWidget* parent) override; + +private: + void poll_loop(); + void poll_connected(MgbaLink::MgbaLink_Connection& connection); + bool wait_or_stop(std::chrono::milliseconds duration); + +private: + Logger& m_logger; + const std::string m_address; + VideoFormatSet m_formats; + + Mutex m_lock; + ConditionVariable m_cv; + bool m_stopping = false; + VideoSnapshot m_last; + + AsyncTask m_poller; +}; + + + +} +#endif diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp new file mode 100644 index 0000000000..c2e051526d --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp @@ -0,0 +1,130 @@ +/* Video Source (mGBA over MgbaLink) Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "VideoSource_MgbaLink.h" +#include "VideoSource_MgbaLink_Tests.h" + +namespace PokemonAutomation{ + +namespace{ + + +// A 240x160 frame whose pixels name their own coordinates. +ImageRGB32 make_gba_frame(){ + ImageRGB32 frame(240, 160); + for (size_t y = 0; y < 160; y++){ + for (size_t x = 0; x < 240; x++){ + frame.pixel(x, y) = 0xff000000 | (uint32_t)(x << 8) | (uint32_t)y; + } + } + return frame; +} + + +UnitTestResult test_compose_geometry(){ + struct Point{ + size_t x; + size_t y; + }; + struct Case{ + std::string name; + Point canvas; + std::optional expected_source; // nullopt: black border. + }; + const Case CASES[] = { + {"box top-left", {180, 5}, Point{0, 0}}, + {"box bottom-right", {1739, 1044}, Point{239, 159}}, + {"left of the box", {179, 500}, std::nullopt}, + {"right of the box", {1740, 500}, std::nullopt}, + {"above the box", {960, 4}, std::nullopt}, + {"below the box", {960, 1045}, std::nullopt}, + // 6.5x: source columns alternate 6 and 7 canvas pixels. + {"last pixel of column 0", {185, 5}, Point{0, 0}}, + {"first pixel of column 1", {186, 5}, Point{1, 0}}, + {"last pixel of column 1", {192, 5}, Point{1, 0}}, + {"first pixel of column 2", {193, 5}, Point{2, 0}}, + {"first pixel of row 1", {180, 11}, Point{0, 1}}, + {"centre", {960, 525}, Point{120, 80}}, + }; + ImageRGB32 canvas = compose_mgba_frame(make_gba_frame()); + if (canvas.width() != 1920 || canvas.height() != 1080){ + return std::format("canvas is {}x{}, expected 1920x1080", canvas.width(), canvas.height()); + } + for (const Case& c : CASES){ + uint32_t expected = c.expected_source + ? 0xff000000 | (uint32_t)(c.expected_source->x << 8) | (uint32_t)c.expected_source->y + : 0xff000000; + uint32_t actual = canvas.pixel(c.canvas.x, c.canvas.y); + if (actual != expected){ + return std::format("{}: pixel {:08x}, expected {:08x}", c.name, actual, expected); + } + } + return true; +} + + +UnitTestResult test_parse_address(){ + struct Expectation{ + std::string host; + uint16_t port; + }; + struct Case{ + std::string name; + std::string input; + std::optional expectation; + }; + const Case CASES[] = { + {"default bridge", "127.0.0.1:47801", Expectation{"127.0.0.1", 47801}}, + {"host name", "localhost:47802", Expectation{"localhost", 47802}}, + {"no port", "127.0.0.1", std::nullopt}, + {"empty port", "127.0.0.1:", std::nullopt}, + {"port 0", "127.0.0.1:0", std::nullopt}, + {"port too large", "127.0.0.1:70000", std::nullopt}, + {"no host", ":47801", std::nullopt}, + }; + for (const Case& c : CASES){ + std::optional> address = parse_mgba_address(c.input); + if (address.has_value() != c.expectation.has_value()){ + return c.name + ": parsed=" + (address ? "true" : "false"); + } + if (address && (address->first != c.expectation->host || address->second != c.expectation->port)){ + return c.name + ": wrong host or port."; + } + } + return true; +} + + + +class Test_VideoSource_MgbaLink : public UnitTest{ +public: + Test_VideoSource_MgbaLink(std::string name, std::function body) + : UnitTest("CommonFramework::VideoSource_MgbaLink - " + std::move(name)) + , m_body(std::move(body)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_body(); + } + +private: + std::function m_body; +}; + +} + + +void add_tests_VideoSource_MgbaLink(UnitTestDatabase& database){ + database.add("compose geometry", test_compose_geometry); + database.add("address", test_parse_address); +} + + + +} diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h new file mode 100644 index 0000000000..f65856b5fe --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h @@ -0,0 +1,20 @@ +/* Video Source (mGBA over MgbaLink) Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_VideoPipeline_VideoSource_MgbaLink_Tests_H +#define PokemonAutomation_VideoPipeline_VideoSource_MgbaLink_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ + + +// Compose geometry (nearest-neighbour into GAME_BOX) and address parsing. +void add_tests_VideoSource_MgbaLink(UnitTestDatabase& database); + + +} +#endif diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/UI/VideoSourceSelectorWidget.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/UI/VideoSourceSelectorWidget.cpp index 10957abbd6..7d27106a9b 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/UI/VideoSourceSelectorWidget.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/UI/VideoSourceSelectorWidget.cpp @@ -9,6 +9,7 @@ #include #include #include "Common/Qt/NoWheelComboBox.h" +#include "CommonFramework/StaticGlobals.h" #include "CommonFramework/GlobalSettingsPanel.h" #include "CommonFramework/VideoPipeline/VideoPipelineOptions.h" #include "CommonFramework/Panels/ConsoleSettingsStretch.h" @@ -138,6 +139,9 @@ void VideoSourceSelectorWidget::update_source_list(){ m_session.save(option); m_sources.emplace_back(option.get_descriptor_from_cache(VideoSourceType::None)); m_sources.emplace_back(option.get_descriptor_from_cache(VideoSourceType::StillImage)); + if (STATIC_GLOBALS.DEVELOPER_MODE){ + m_sources.emplace_back(option.get_descriptor_from_cache(VideoSourceType::MgbaLink)); + } } // Now add all the cameras. diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceDescriptor.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceDescriptor.h index 24db4ea985..ab874a6df1 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceDescriptor.h +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceDescriptor.h @@ -23,6 +23,7 @@ enum class VideoSourceType{ StillImage, VideoPlayback, Camera, + MgbaLink, // Developer mode only (fork lab tooling). }; diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp index 6c11a5daca..c7db545eaa 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp @@ -12,6 +12,7 @@ #include "Sources/VideoSource_Null.h" #include "Sources/VideoSource_StillImage.h" #include "Sources/VideoSource_Camera.h" +#include "Sources/VideoSource_MgbaLink.h" #include "VideoSourceOption.h" @@ -27,6 +28,7 @@ const EnumStringMap VIDEO_TYPE_STRINGS{ {VideoSourceType::StillImage, "Still Image"}, {VideoSourceType::VideoPlayback, "Video Playback"}, {VideoSourceType::Camera, "Camera"}, + {VideoSourceType::MgbaLink, "mGBA Link"}, }; @@ -57,6 +59,9 @@ std::shared_ptr VideoSourceOption::get_descriptor_from_ca case VideoSourceType::Camera: descriptor.reset(new VideoSourceDescriptor_Camera()); break; + case VideoSourceType::MgbaLink: + descriptor.reset(new VideoSourceDescriptor_MgbaLink()); + break; default:; descriptor.reset(new VideoSourceDescriptor_Null()); } @@ -136,6 +141,12 @@ void VideoSourceOption::load_json(const JsonValue& json){ x->load_json(*params); m_descriptor_cache[VideoSourceType::Camera] = std::move(x); } + params = obj->get_value(VIDEO_TYPE_STRINGS.get_string(VideoSourceType::MgbaLink)); + if (params != nullptr){ + auto x = std::make_unique(); + x->load_json(*params); + m_descriptor_cache[VideoSourceType::MgbaLink] = std::move(x); + } auto iter = m_descriptor_cache.find(VIDEO_TYPE_STRINGS.get_enum(*type, VideoSourceType::None)); if (iter == m_descriptor_cache.end()){ diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 0bf2672c6d..af07f73363 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -23,6 +23,8 @@ #include "CommonFramework/ProgramStats/StatsTracking.h" #include "CommonFramework/Tools/GlobalThreadPools.h" #include "CommonFramework/Windows/LabChannel_Tests.h" +#include "CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h" +#include "Controllers/MgbaLink/MgbaLink_Tests.h" #include "CommonTools/VisualDetectors/BlackBorderDetector.h" #include "CommonTools/OCR/OCR_Tests.h" @@ -69,6 +71,8 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_SinkPipeline(ret); add_tests_AudioFileLoader(ret); add_tests_LabChannel(ret); + MgbaLink::add_tests(ret, GlobalThreadPools::unlimited_normal()); + add_tests_VideoSource_MgbaLink(ret); OCR::add_tests(ret); Kernels::add_tests(ret); Pokemon::add_tests_AutoStorySelect(ret); diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp new file mode 100644 index 0000000000..8a945a5985 --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp @@ -0,0 +1,185 @@ +/* MgbaLink Connection + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "MgbaLink_Connection.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + + +MgbaLink_Connection::MgbaLink_Connection( + Logger& logger, + ThreadPool& thread_pool, + std::string host, + uint16_t port, + std::chrono::milliseconds timeout +) + : m_logger(logger) + , m_host(std::move(host)) + , m_port(port) + , m_timeout(timeout) + , m_socket(thread_pool) +{ + m_socket.add_listener(*this); +} +MgbaLink_Connection::~MgbaLink_Connection(){ + close(); + m_socket.remove_listener(*this); +} + + +HelloReply MgbaLink_Connection::open(){ + connect_socket(); + std::string reply = command("HELLO 1"); + std::optional hello = parse_hello_reply(reply); + if (!hello){ + fail("Unexpected HELLO reply: " + reply); + } + m_logger.log( + "MgbaLink: connected to " + m_host + ":" + std::to_string(m_port) + + ", game " + hello->game_code + ", " + hello->bridge_version + ", mode " + hello->mode + "." + ); + return *hello; +} + +void MgbaLink_Connection::connect_socket(){ + m_socket.connect(m_host, m_port); + std::unique_lock lg(m_lock); + if (!m_cv.wait_for(lg, m_timeout, [this]{ return m_connect_error.has_value(); })){ + lg.unlock(); + fail("Timed out connecting to " + m_host + ":" + std::to_string(m_port) + "."); + } + if (!m_connect_error->empty()){ + std::string error = *m_connect_error; + lg.unlock(); + fail("Unable to connect to " + m_host + ":" + std::to_string(m_port) + ": " + error); + } +} + +void MgbaLink_Connection::close() noexcept{ + if (m_socket.state() == AbstractClientSocket::State::CONNECTED){ + try{ + command("BYE"); + }catch (...){} + } + m_socket.close(); +} + + +std::string MgbaLink_Connection::command(std::string_view line){ + std::lock_guard command_lg(m_command_lock); + std::string_view name = line.substr(0, line.find(' ')); + // A line the bridge sent unasked ("ERR busy" right after connecting) + // is still read as this command's reply, even after the bridge closed. + std::string data(line); + data += '\n'; + bool sent = m_socket.send(data.data(), data.size(), m_timeout) == data.size(); + { + std::lock_guard lg(m_lock); + if (!sent && m_replies.empty()){ + fail("Unable to send " + std::string(name) + "."); + } + } + return wait_for_reply(name); +} + +std::string MgbaLink_Connection::wait_for_reply(std::string_view command_name){ + std::unique_lock lg(m_lock); + bool ready = m_cv.wait_for(lg, m_timeout, [this]{ + return !m_replies.empty() || m_disconnect_error.has_value(); + }); + if (m_replies.empty()){ + std::string reason = ready ? "connection lost: " + *m_disconnect_error : std::string("timed out"); + if (!ready){ + // A late reply would pair with the next command: drop the link. + m_disconnect_error = "no reply to " + std::string(command_name); + } + lg.unlock(); + m_socket.close(); + fail("No reply to " + std::string(command_name) + " (" + reason + ")."); + } + std::string reply = std::move(m_replies.front()); + m_replies.pop_front(); + lg.unlock(); + if (reply.starts_with("ERR")){ + fail(std::string(command_name) + " -> " + reply); + } + return reply; +} + +FrameReply MgbaLink_Connection::frame(){ + std::string reply = command("FRAME"); + std::optional frame = parse_frame_reply(reply); + if (!frame){ + fail("Unexpected FRAME reply: " + reply); + } + return std::move(*frame); +} + +uint64_t MgbaLink_Connection::keys(uint64_t frame, uint16_t mask){ + std::string reply = command("KEYS " + std::to_string(frame) + " " + std::to_string(mask)); + std::optional late = parse_keys_reply(reply); + if (!late){ + fail("Unexpected KEYS reply: " + reply); + } + if (*late > 0){ + m_logger.log( + "MgbaLink: KEYS for frame " + std::to_string(frame) + " was " + + std::to_string(*late) + " frame(s) late.", + COLOR_ORANGE + ); + } + return *late; +} + +void MgbaLink_Connection::fail(std::string message){ + throw ConnectionException(&m_logger, "MgbaLink: " + std::move(message)); +} + + + +void MgbaLink_Connection::on_connect_finished(const std::string& error_message){ + std::lock_guard lg(m_lock); + m_connect_error = error_message; + m_disconnect_error.reset(); + m_splitter = LineSplitter(); + m_cv.notify_all(); +} + +void MgbaLink_Connection::on_receive_data(const void* data, size_t bytes){ + bool overflowed; + { + std::lock_guard lg(m_lock); + for (std::string& line : m_splitter.push(std::string_view((const char*)data, bytes))){ + m_replies.emplace_back(std::move(line)); + } + overflowed = m_splitter.overflowed(); + if (overflowed){ + m_disconnect_error = "reply line longer than " + std::to_string(MAX_LINE_BYTES) + " bytes"; + } + m_cv.notify_all(); + } + if (overflowed){ + m_socket.close(); + } +} + +void MgbaLink_Connection::on_disconnect(const std::string& error_message){ + std::lock_guard lg(m_lock); + if (!m_disconnect_error){ + m_disconnect_error = error_message.empty() ? "closed by the bridge" : error_message; + } + m_cv.notify_all(); +} + + + +} +} diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h new file mode 100644 index 0000000000..e1c06e3fd1 --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h @@ -0,0 +1,100 @@ +/* MgbaLink Connection + * + * From: https://github.com/PokemonAutomation/ + * + * One TCP connection to one "bridge.lua" running inside mGBA (MgbaLink v1, + * lab/mgba/PROTOCOL.md on the fork's agents branch). Every command is one + * line and gets exactly one reply line; commands from several threads are + * serialized. The bridge holds one client at a time, so the destructor + * always says BYE, which also releases every key and grant it holds. + * + * Qt-free. The socket's receive thread comes from the thread pool passed in. + * + */ + +#ifndef PokemonAutomation_Controllers_MgbaLink_Connection_H +#define PokemonAutomation_Controllers_MgbaLink_Connection_H + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/Concurrency/ThreadPool.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/Sockets/ClientSocket.h" +#include "MgbaLink_Protocol.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +class MgbaLink_Connection final : private AbstractClientSocket::Listener{ +public: + MgbaLink_Connection( + Logger& logger, + ThreadPool& thread_pool, + std::string host, + uint16_t port, + std::chrono::milliseconds timeout = std::chrono::seconds(5) + ); + ~MgbaLink_Connection(); + + // Connects and says "HELLO 1". Throws ConnectionException if the bridge + // cannot be reached, refuses the client ("ERR busy") or replies with + // anything but a v1 HELLO. + HelloReply open(); + + // Sends "BYE" (best effort) and closes. Safe to call more than once. + void close() noexcept; + + // Sends one command line and returns its reply line. Throws + // ConnectionException on a timeout, a lost connection or an "ERR" reply. + std::string command(std::string_view line); + + // "FRAME": the bridge writes the last finished frame as a PNG. + FrameReply frame(); + + // "KEYS ": hold "mask" from "frame" on. Returns how many + // frames late the command arrived (0 = on time). + uint64_t keys(uint64_t frame, uint16_t mask); + +private: + virtual void on_connect_finished(const std::string& error_message) override; + virtual void on_receive_data(const void* data, size_t bytes) override; + virtual void on_disconnect(const std::string& error_message) override; + + void connect_socket(); + std::string wait_for_reply(std::string_view command_name); + [[noreturn]] void fail(std::string message); + +private: + Logger& m_logger; + const std::string m_host; + const uint16_t m_port; + const std::chrono::milliseconds m_timeout; + + // Held for a whole command: one request in flight at a time. + Mutex m_command_lock; + + // Guards everything below; the socket's receive thread writes it. + Mutex m_lock; + ConditionVariable m_cv; + std::optional m_connect_error; + std::optional m_disconnect_error; + LineSplitter m_splitter; + std::deque m_replies; + + // Last member: destroyed first, which joins its receive thread before + // the state above goes away. + ClientSocket m_socket; +}; + + + +} +} +#endif diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp new file mode 100644 index 0000000000..eb24e47661 --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp @@ -0,0 +1,156 @@ +/* MgbaLink Protocol + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "MgbaLink_Protocol.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +namespace{ + +// Splits on single spaces, at most "max_parts" parts; the last part keeps +// the rest of the line (a FRAME path may contain spaces). +std::vector split_spaces(std::string_view text, size_t max_parts){ + std::vector parts; + while (parts.size() + 1 < max_parts){ + size_t space = text.find(' '); + if (space == std::string_view::npos){ + break; + } + parts.emplace_back(text.substr(0, space)); + text.remove_prefix(space + 1); + } + parts.emplace_back(text); + return parts; +} + +// Digits only: no sign, no spaces, no empty string. +std::optional parse_unsigned(std::string_view text){ + uint64_t value = 0; + if (text.empty()){ + return std::nullopt; + } + const char* end = text.data() + text.size(); + std::from_chars_result result = std::from_chars(text.data(), end, value); + if (result.ec != std::errc() || result.ptr != end){ + return std::nullopt; + } + return value; +} + +} + + + +std::optional parse_hello_reply(std::string_view reply){ + std::vector parts = split_spaces(reply, 6); + if (parts.size() != 5 || parts[0] != "OK" || parts[1] != "1"){ + return std::nullopt; + } + for (std::string_view part : parts){ + if (part.empty()){ + return std::nullopt; + } + } + return HelloReply{std::string(parts[2]), std::string(parts[3]), std::string(parts[4])}; +} + +std::optional parse_ok_frame_reply(std::string_view reply){ + std::vector parts = split_spaces(reply, 3); + if (parts.size() != 2 || parts[0] != "OK"){ + return std::nullopt; + } + return parse_unsigned(parts[1]); +} + +std::optional parse_keys_reply(std::string_view reply){ + if (reply == "OK"){ + return 0; + } + std::vector parts = split_spaces(reply, 3); + if (parts.size() != 2 || parts[0] != "LATE"){ + return std::nullopt; + } + return parse_unsigned(parts[1]); +} + +std::optional parse_frame_reply(std::string_view reply){ + std::vector parts = split_spaces(reply, 5); + if (parts.size() != 5 || parts[0] != "FRAME" || parts[4].empty()){ + return std::nullopt; + } + std::optional frame = parse_unsigned(parts[1]); + std::optional width = parse_unsigned(parts[2]); + std::optional height = parse_unsigned(parts[3]); + if (!frame || !width || !height){ + return std::nullopt; + } + return FrameReply{*frame, (size_t)*width, (size_t)*height, std::string(parts[4])}; +} + + + +std::vector LineSplitter::push(std::string_view data){ + std::vector lines; + if (m_overflowed){ + return lines; + } + while (!data.empty()){ + size_t newline = data.find('\n'); + std::string_view piece = data.substr(0, newline); + if (m_buffer.size() + piece.size() > m_max_line + 1){ + // "+ 1" leaves room for the CR that is dropped below. + m_buffer.clear(); + m_overflowed = true; + return lines; + } + m_buffer.append(piece); + if (newline == std::string_view::npos){ + break; + } + if (!m_buffer.empty() && m_buffer.back() == '\r'){ + m_buffer.pop_back(); + } + if (m_buffer.size() > m_max_line){ + m_buffer.clear(); + m_overflowed = true; + return lines; + } + lines.emplace_back(std::move(m_buffer)); + m_buffer.clear(); + data.remove_prefix(newline + 1); + } + return lines; +} + + + +WallClock FrameClock::timestamp(uint64_t frame) const{ + double frames = frame >= m_f0 + ? (double)(frame - m_f0) + : -(double)(m_f0 - frame); + double microseconds = std::round(frames * 1000000.0 / GBA_FRAMES_PER_SECOND); + return m_t0 + std::chrono::duration_cast( + std::chrono::microseconds((int64_t)microseconds) + ); +} + + +bool FrameClock::resync(WallClock now, uint64_t frame, WallDuration max_drift){ + WallDuration drift = timestamp(frame) - now; + if (drift <= max_drift && -drift <= max_drift){ + return false; + } + m_t0 = now; + m_f0 = frame; + return true; +} + +} +} diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h new file mode 100644 index 0000000000..2365b7ce41 --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h @@ -0,0 +1,123 @@ +/* MgbaLink Protocol + * + * From: https://github.com/PokemonAutomation/ + * + * Reply parsing, line framing and the frame clock of MgbaLink v1, the text + * protocol spoken by "bridge.lua" inside mGBA (lab/mgba/PROTOCOL.md on the + * fork's agents branch). Pure functions; the socket lives in + * MgbaLink_Connection. + * + */ + +#ifndef PokemonAutomation_Controllers_MgbaLink_Protocol_H +#define PokemonAutomation_Controllers_MgbaLink_Protocol_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/Time.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +// GBA frame rate: 16.78 MHz / 280,896 cycles per frame. +constexpr double GBA_FRAMES_PER_SECOND = 59.7275; + +// The bridge drops a client whose command line is longer than this; replies +// are bounded the same way (a FRAME path is the longest reply). +constexpr size_t MAX_LINE_BYTES = 4096; + + +struct HelloReply{ + std::string game_code; // "BPRE" for FireRed US. + std::string bridge_version; // "bridge-0.1" + std::string mode; // "paced" or "free" +}; + +struct FrameReply{ + uint64_t frame; + size_t width; + size_t height; + std::string path; // PNG written by the bridge; may contain spaces. +}; + + +// Each parser returns nullopt when the reply does not have the expected +// shape (including any "ERR ..." reply). + +// "OK 1 " +std::optional parse_hello_reply(std::string_view reply); + +// "OK " (GRANT, SAVE, LOAD) +std::optional parse_ok_frame_reply(std::string_view reply); + +// KEYS: "OK" -> 0, "LATE " -> n (frames late). +std::optional parse_keys_reply(std::string_view reply); + +// "FRAME " +std::optional parse_frame_reply(std::string_view reply); + + + +// Splits a byte stream into lines: LF ends a line, a CR right before it is +// dropped. A line longer than "max_line" bytes sets "overflowed()" and +// drops everything buffered so far; the caller should then disconnect. +class LineSplitter{ +public: + explicit LineSplitter(size_t max_line = MAX_LINE_BYTES) + : m_max_line(max_line) + {} + + // Appends "data" and returns the lines it completed, in order. + std::vector push(std::string_view data); + + bool overflowed() const{ + return m_overflowed; + } + +private: + size_t m_max_line; + std::string m_buffer; + bool m_overflowed = false; +}; + + + +// Maps emulated frame numbers to app time. Anchored at a known pair +// (t0, F0): frame F happened at t0 + (F - F0) * 1000 / 59.7275 ms, so a +// frame's timestamp comes from its number, not from when the app read it. +class FrameClock{ +public: + FrameClock(WallClock t0, uint64_t f0) + : m_t0(t0) + , m_f0(f0) + {} + + WallClock timestamp(uint64_t frame) const; + + // While mGBA runs free (no GRANT pacing yet), its clock drifts against + // the app's and stops while it is paused. If "frame" maps more than + // "max_drift" away from "now", re-anchors at (now, frame) and returns + // true. + bool resync(WallClock now, uint64_t frame, WallDuration max_drift); + + WallClock anchor_time() const{ + return m_t0; + } + uint64_t anchor_frame() const{ + return m_f0; + } + +private: + WallClock m_t0; + uint64_t m_f0; +}; + + + +} +} +#endif diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp new file mode 100644 index 0000000000..b81073bf4b --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp @@ -0,0 +1,412 @@ +/* MgbaLink Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#ifdef _WIN32 +#include "Common/Cpp/Sockets/ServerSocket_WinSocket.h" +#else +#include "Common/Cpp/Sockets/ServerSocket_POSIX.h" +#endif +#include "MgbaLink_Connection.h" +#include "MgbaLink_Protocol.h" +#include "MgbaLink_Tests.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + +namespace{ + +#ifdef _WIN32 +using FakeServerSocket = ServerSocket_WinSocket; +#else +using FakeServerSocket = ServerSocket_POSIX; +#endif + + +UnitTestResult test_line_splitter(){ + struct Input{ + std::vector chunks; + size_t max_line; + }; + struct Expectation{ + std::vector lines; + bool overflowed; + }; + struct Case{ + std::string name; + Input input; + Expectation expectation; + }; + const Case CASES[] = { + {"one line", {{"OK\n"}, 16}, {{"OK"}, false}}, + {"CR before LF dropped", {{"OK 5\r\n"}, 16}, {{"OK 5"}, false}}, + {"line split across chunks", {{"FRAME 12", "0 240 160 /a b.png\nOK"}, 64}, {{"FRAME 120 240 160 /a b.png"}, false}}, + {"two lines in one chunk", {{"OK\nLATE 3\n"}, 16}, {{"OK", "LATE 3"}, false}}, + {"empty line", {{"\n"}, 16}, {{""}, false}}, + {"exactly max", {{std::string(8, 'a') + "\r\n"}, 8}, {{std::string(8, 'a')}, false}}, + {"over max without LF", {{std::string(10, 'a')}, 8}, {{}, true}}, + {"over max across chunks", {{"aaaaa", "aaaaa\n"}, 8}, {{}, true}}, + }; + for (const Case& c : CASES){ + LineSplitter splitter(c.input.max_line); + std::vector lines; + for (const std::string& chunk : c.input.chunks){ + for (std::string& line : splitter.push(chunk)){ + lines.emplace_back(std::move(line)); + } + } + if (lines != c.expectation.lines || splitter.overflowed() != c.expectation.overflowed){ + return std::format("{}: got {} line(s), overflowed={}", c.name, lines.size(), splitter.overflowed()); + } + } + return true; +} + + +UnitTestResult test_parse_hello(){ + struct Case{ + std::string name; + std::string input; + std::optional expected_code; + }; + const Case CASES[] = { + {"v1 reply", "OK 1 BPRE bridge-0.1 paced", "BPRE"}, + {"wrong version", "OK 2 BPRE bridge-0.1 paced", std::nullopt}, + {"missing mode", "OK 1 BPRE bridge-0.1", std::nullopt}, + {"extra field", "OK 1 BPRE bridge-0.1 paced x", std::nullopt}, + {"error", "ERR busy", std::nullopt}, + }; + for (const Case& c : CASES){ + std::optional hello = parse_hello_reply(c.input); + std::optional code; + if (hello){ + code = hello->game_code; + } + if (code != c.expected_code){ + return c.name + ": unexpected result."; + } + } + return true; +} + + +UnitTestResult test_parse_frame(){ + struct Expectation{ + uint64_t frame; + size_t width; + size_t height; + std::string path; + }; + struct Case{ + std::string name; + std::string input; + std::optional expectation; + }; + const Case CASES[] = { + {"0.10.5 reply", "FRAME 1234 240 160 /tmp/mgba-bridge-47801.png", Expectation{1234, 240, 160, "/tmp/mgba-bridge-47801.png"}}, + {"path with spaces", "FRAME 7 240 160 /Users/x/My Folder/f.png", Expectation{7, 240, 160, "/Users/x/My Folder/f.png"}}, + {"no path", "FRAME 7 240 160", std::nullopt}, + {"negative frame", "FRAME -7 240 160 /f.png", std::nullopt}, + {"error", "ERR no-game", std::nullopt}, + }; + for (const Case& c : CASES){ + std::optional reply = parse_frame_reply(c.input); + if (reply.has_value() != c.expectation.has_value()){ + return c.name + ": parsed=" + (reply ? "true" : "false"); + } + if (reply && ( + reply->frame != c.expectation->frame || reply->width != c.expectation->width || + reply->height != c.expectation->height || reply->path != c.expectation->path + )){ + return c.name + ": wrong fields."; + } + } + return true; +} + + +UnitTestResult test_parse_keys_and_ok_frame(){ + struct Case{ + std::string name; + std::function(std::string_view)> parser; + std::string input; + std::optional expected; + }; + const Case CASES[] = { + {"KEYS on time", parse_keys_reply, "OK", 0}, + {"KEYS late", parse_keys_reply, "LATE 3", 3}, + {"KEYS late without count", parse_keys_reply, "LATE", std::nullopt}, + {"KEYS error", parse_keys_reply, "ERR usage KEYS ", std::nullopt}, + {"GRANT reply", parse_ok_frame_reply, "OK 4521", 4521}, + {"bare OK", parse_ok_frame_reply, "OK", std::nullopt}, + {"OK with text", parse_ok_frame_reply, "OK BYE", std::nullopt}, + }; + for (const Case& c : CASES){ + if (c.parser(c.input) != c.expected){ + return c.name + ": unexpected result."; + } + } + return true; +} + + +UnitTestResult test_frame_clock(){ + using std::chrono::microseconds; + const WallClock t0 = WallClock() + std::chrono::hours(1000); + struct Case{ + std::string name; + uint64_t frame; + microseconds expected_offset; + }; + // Anchored at frame 1000; one frame = 16742.7 us. + const Case CASES[] = { + {"anchor frame", 1000, microseconds(0)}, + {"next frame", 1001, microseconds(16743)}, + {"one second of frames", 1060, microseconds(1004562)}, + {"before the anchor (after a LOAD)", 940, microseconds(-1004562)}, + }; + FrameClock clock(t0, 1000); + for (const Case& c : CASES){ + WallDuration offset = clock.timestamp(c.frame) - t0; + if (offset != std::chrono::duration_cast(c.expected_offset)){ + return std::format( + "{}: offset {} us", + c.name, std::chrono::duration_cast(offset).count() + ); + } + } + if (clock.resync(t0 + std::chrono::milliseconds(1100), 1060, std::chrono::milliseconds(500))){ + return "resync: 95 ms of drift should keep the anchor."; + } + if (!clock.resync(t0 + std::chrono::seconds(10), 1060, std::chrono::milliseconds(500)) || + clock.timestamp(1060) != t0 + std::chrono::seconds(10) + ){ + return "resync: 9 s of drift (mGBA paused) should re-anchor at the read time."; + } + return true; +} + + + +// A bridge stand-in: answers each command line from a script, or refuses +// the client with "ERR busy" as a bridge that already has one. +class FakeBridge : public AbstractServerSocket::Listener{ +public: + FakeBridge(ThreadPool& thread_pool, bool busy) + : m_server(thread_pool) + , m_busy(busy) + { + m_server.add_listener(*this); + } + ~FakeBridge(){ + m_server.remove_listener(*this); + } + + // Listens on a random port in 40000..59999; nullopt if 8 tries fail. + std::optional start(); + + std::vector commands(){ + std::lock_guard lg(m_lock); + return m_commands; + } + + // Waits up to 5 s for the client to send "count" commands. + bool wait_for_commands(size_t count){ + std::unique_lock lg(m_lock); + return m_cv.wait_for(lg, std::chrono::seconds(5), [&]{ return m_commands.size() >= count; }); + } + +private: + virtual void on_listen_finished(const std::string& error_message) override{ + std::lock_guard lg(m_lock); + m_listen_error = error_message; + m_cv.notify_all(); + } + virtual void on_connect() override{ + if (m_busy){ + // Closed once the client's first line is read (no reset that + // could drop this line before the client reads it). + send("ERR busy\n"); + } + } + virtual void on_receive_data(const void* data, size_t bytes) override; + + void send(std::string_view text){ + m_server.send(text.data(), text.size()); + } + + static std::string reply_for(std::string_view line); + +private: + FakeServerSocket m_server; + const bool m_busy; + LineSplitter m_splitter; + Mutex m_lock; + ConditionVariable m_cv; + std::optional m_listen_error; + std::vector m_commands; +}; + +std::optional FakeBridge::start(){ + std::mt19937 rng{std::random_device{}()}; + for (int attempt = 0; attempt < 8; attempt++){ + uint16_t port = (uint16_t)(40000 + rng() % 20000); + { + std::lock_guard lg(m_lock); + m_listen_error.reset(); + } + m_server.start("127.0.0.1", port); + std::unique_lock lg(m_lock); + m_cv.wait_for(lg, std::chrono::seconds(10), [this]{ return m_listen_error.has_value(); }); + if (m_listen_error && m_listen_error->empty()){ + return port; + } + } + return std::nullopt; +} + +void FakeBridge::on_receive_data(const void* data, size_t bytes){ + if (m_busy){ + m_server.close_connection(); + return; + } + std::vector lines = m_splitter.push(std::string_view((const char*)data, bytes)); + for (const std::string& line : lines){ + { + std::lock_guard lg(m_lock); + m_commands.emplace_back(line); + m_cv.notify_all(); + } + std::string reply = reply_for(line); + // Two writes, so the client has to reassemble the line. + send(std::string_view(reply).substr(0, 3)); + send(std::string_view(reply).substr(3)); + if (line == "BYE"){ + m_server.close_connection(); + } + } +} + +std::string FakeBridge::reply_for(std::string_view line){ + // The emulator is about to run frame 120. + if (line == "HELLO 1"){ + return "OK 1 BPRE bridge-0.1 paced\r\n"; + } + if (line == "FRAME"){ + return "FRAME 120 240 160 /tmp/mgba bridge.png\n"; + } + if (line == "KEYS 100 1"){ + return "LATE 20\n"; + } + if (line == "KEYS 130 1"){ + return "OK\n"; + } + if (line == "BYE"){ + return "OK BYE\n"; + } + return "ERR unknown " + std::string(line.substr(0, line.find(' '))) + "\n"; +} + + + +UnitTestResult test_session(Logger& logger, ThreadPool& thread_pool){ + FakeBridge bridge(thread_pool, false); + std::optional port = bridge.start(); + if (!port){ + return UnitTestResult(UnitTestResult::SKIPPED, "No free port for the fake bridge."); + } + { + MgbaLink_Connection connection(logger, thread_pool, "127.0.0.1", *port); + HelloReply hello = connection.open(); + if (hello.game_code != "BPRE" || hello.mode != "paced"){ + return "HELLO: wrong game code or mode."; + } + FrameReply frame = connection.frame(); + if (frame.frame != 120 || frame.path != "/tmp/mgba bridge.png"){ + return "FRAME: wrong frame number or path."; + } + if (connection.keys(100, 1) != 20 || connection.keys(130, 1) != 0){ + return "KEYS: expected LATE 20, then on time."; + } + try{ + connection.command("FLY"); + return "An ERR reply should throw."; + }catch (ConnectionException&){} + } + const std::vector expected{"HELLO 1", "FRAME", "KEYS 100 1", "KEYS 130 1", "FLY", "BYE"}; + if (!bridge.wait_for_commands(expected.size()) || bridge.commands() != expected){ + return std::format("The bridge saw {} command(s); the session should end with BYE.", bridge.commands().size()); + } + return true; +} + +UnitTestResult test_busy_bridge(Logger& logger, ThreadPool& thread_pool){ + FakeBridge bridge(thread_pool, true); + std::optional port = bridge.start(); + if (!port){ + return UnitTestResult(UnitTestResult::SKIPPED, "No free port for the fake bridge."); + } + MgbaLink_Connection connection(logger, thread_pool, "127.0.0.1", *port); + try{ + connection.open(); + }catch (ConnectionException& e){ + if (e.message().find("ERR busy") == std::string::npos){ + return "Expected the bridge's ERR busy in the error, got: " + e.message(); + } + return true; + } + return "open() should fail when the bridge already has a client."; +} + + + +class Test_MgbaLink : public UnitTest{ +public: + using Body = std::function; + + Test_MgbaLink(std::string name, ThreadPool& thread_pool, Body body) + : UnitTest("Controllers::MgbaLink - " + std::move(name)) + , m_thread_pool(thread_pool) + , m_body(std::move(body)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_body(logger, m_thread_pool); + } + +private: + ThreadPool& m_thread_pool; + Body m_body; +}; + +Test_MgbaLink::Body pure(UnitTestResult (*body)()){ + return [body](Logger&, ThreadPool&){ return body(); }; +} + +} + + +void add_tests(UnitTestDatabase& database, ThreadPool& thread_pool){ + database.add("line framing", thread_pool, pure(test_line_splitter)); + database.add("HELLO reply", thread_pool, pure(test_parse_hello)); + database.add("FRAME reply", thread_pool, pure(test_parse_frame)); + database.add("KEYS and OK replies", thread_pool, pure(test_parse_keys_and_ok_frame)); + database.add("frame clock", thread_pool, pure(test_frame_clock)); + database.add("fake bridge session", thread_pool, test_session); + database.add("fake bridge busy", thread_pool, test_busy_bridge); +} + + + +} +} diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h new file mode 100644 index 0000000000..8f85efb465 --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h @@ -0,0 +1,24 @@ +/* MgbaLink Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Controllers_MgbaLink_Tests_H +#define PokemonAutomation_Controllers_MgbaLink_Tests_H + +#include "Common/Cpp/Concurrency/ThreadPool.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +// Reply parsing, line framing, the frame clock, and a connection against a +// fake bridge on 127.0.0.1 (sockets run on "thread_pool"). +void add_tests(UnitTestDatabase& database, ThreadPool& thread_pool); + + +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 66bbc02b7b..1116c05fbc 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -280,6 +280,12 @@ file(GLOB CORE_LIBRARY_SOURCES Source/Controllers/Joystick.cpp Source/Controllers/Joystick.h Source/Controllers/JoystickTools.h + Source/Controllers/MgbaLink/MgbaLink_Connection.cpp + Source/Controllers/MgbaLink/MgbaLink_Connection.h + Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp + Source/Controllers/MgbaLink/MgbaLink_Protocol.h + Source/Controllers/MgbaLink/MgbaLink_Tests.cpp + Source/Controllers/MgbaLink/MgbaLink_Tests.h Source/Controllers/NullController.cpp Source/Controllers/NullController.h Source/Controllers/PABotBase2/PABotBase2_CommandQueueManager.cpp @@ -787,6 +793,10 @@ file(GLOB LIBRARY_SOURCES Source/CommonFramework/VideoPipeline/VideoSourceOption.h Source/CommonFramework/VideoPipeline/Sources/VideoSource_Camera.cpp Source/CommonFramework/VideoPipeline/Sources/VideoSource_Camera.h + Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp + Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h + Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp + Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h Source/CommonFramework/VideoPipeline/Sources/VideoSource_Null.cpp Source/CommonFramework/VideoPipeline/Sources/VideoSource_Null.h Source/CommonFramework/VideoPipeline/Sources/VideoSource_StillImage.cpp From 753eff46f91daa8fe17f980dd50b72f71f365e2d Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:10:47 -0800 Subject: [PATCH 2/5] MgbaLink: code health (case tables, namespace comments, header docs) --- .../Sources/VideoSource_MgbaLink.cpp | 9 +- .../Sources/VideoSource_MgbaLink.h | 6 +- .../Sources/VideoSource_MgbaLink_Tests.cpp | 169 +++--- .../Sources/VideoSource_MgbaLink_Tests.h | 2 +- .../VideoPipeline/VideoSourceOption.cpp | 49 +- .../ComputerPrograms_UnitTestRunner.cpp | 2 +- .../MgbaLink/MgbaLink_Connection.cpp | 8 +- .../MgbaLink/MgbaLink_Connection.h | 5 +- .../MgbaLink/MgbaLink_Protocol.cpp | 11 +- .../Controllers/MgbaLink/MgbaLink_Protocol.h | 21 +- .../Controllers/MgbaLink/MgbaLink_Tests.cpp | 496 ++++++++++-------- .../Controllers/MgbaLink/MgbaLink_Tests.h | 9 +- 12 files changed, 441 insertions(+), 346 deletions(-) diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp index 5ea950d000..c310e3ff98 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp @@ -30,7 +30,7 @@ constexpr std::chrono::milliseconds RECONNECT_DELAY(2000); // One GBA frame, rounded down: polling faster only repeats a frame number. constexpr std::chrono::milliseconds POLL_PERIOD(16); -} +} // namespace @@ -63,9 +63,8 @@ std::optional> parse_mgba_address(std::string_v } std::string_view port_text = address.substr(colon + 1); uint16_t port = 0; - const char* end = port_text.data() + port_text.size(); - std::from_chars_result result = std::from_chars(port_text.data(), end, port); - if (port_text.empty() || result.ec != std::errc() || result.ptr != end || port == 0){ + std::from_chars_result result = std::from_chars(port_text.data(), port_text.data() + port_text.size(), port); + if (port_text.empty() || result.ec != std::errc() || result.ptr != port_text.data() + port_text.size() || port == 0){ return std::nullopt; } return std::make_pair(std::string(address.substr(0, colon)), port); @@ -220,4 +219,4 @@ QWidget* VideoSource_MgbaLink::make_display_QtWidget(QWidget* parent){ -} +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h index ceedd8e4df..0aec4a9a60 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h @@ -56,9 +56,10 @@ std::optional> parse_mgba_address(std::string_v +// Selects an mGBA bridge by "host:port" (saved in the profile). class VideoSourceDescriptor_MgbaLink : public VideoSourceDescriptor{ public: - static constexpr const char* DEFAULT_ADDRESS = "127.0.0.1:47801"; + static constexpr std::string_view DEFAULT_ADDRESS = "127.0.0.1:47801"; VideoSourceDescriptor_MgbaLink() : VideoSourceDescriptor(VideoSourceType::MgbaLink) @@ -87,6 +88,7 @@ class VideoSourceDescriptor_MgbaLink : public VideoSourceDescriptor{ +// Polls FRAME on its own connection and keeps the latest composed frame. class VideoSource_MgbaLink : public VideoSource{ public: VideoSource_MgbaLink(Logger& logger, std::string address); @@ -130,5 +132,5 @@ class VideoSource_MgbaLink : public VideoSource{ -} +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp index c2e051526d..61ea0e952b 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp @@ -5,7 +5,6 @@ */ #include -#include #include #include "Common/Cpp/TestRunners/UnitTest.h" #include "VideoSource_MgbaLink.h" @@ -27,104 +26,118 @@ ImageRGB32 make_gba_frame(){ return frame; } +struct Point{ + size_t x; + size_t y; +}; -UnitTestResult test_compose_geometry(){ - struct Point{ - size_t x; - size_t y; - }; - struct Case{ - std::string name; - Point canvas; - std::optional expected_source; // nullopt: black border. - }; - const Case CASES[] = { - {"box top-left", {180, 5}, Point{0, 0}}, - {"box bottom-right", {1739, 1044}, Point{239, 159}}, - {"left of the box", {179, 500}, std::nullopt}, - {"right of the box", {1740, 500}, std::nullopt}, - {"above the box", {960, 4}, std::nullopt}, - {"below the box", {960, 1045}, std::nullopt}, - // 6.5x: source columns alternate 6 and 7 canvas pixels. - {"last pixel of column 0", {185, 5}, Point{0, 0}}, - {"first pixel of column 1", {186, 5}, Point{1, 0}}, - {"last pixel of column 1", {192, 5}, Point{1, 0}}, - {"first pixel of column 2", {193, 5}, Point{2, 0}}, - {"first pixel of row 1", {180, 11}, Point{0, 1}}, - {"centre", {960, 525}, Point{120, 80}}, - }; - ImageRGB32 canvas = compose_mgba_frame(make_gba_frame()); - if (canvas.width() != 1920 || canvas.height() != 1080){ - return std::format("canvas is {}x{}, expected 1920x1080", canvas.width(), canvas.height()); - } - for (const Case& c : CASES){ - uint32_t expected = c.expected_source - ? 0xff000000 | (uint32_t)(c.expected_source->x << 8) | (uint32_t)c.expected_source->y - : 0xff000000; - uint32_t actual = canvas.pixel(c.canvas.x, c.canvas.y); - if (actual != expected){ - return std::format("{}: pixel {:08x}, expected {:08x}", c.name, actual, expected); - } - } - return true; -} +// Which GBA pixel lands on a canvas pixel of the composed 1920x1080 frame. +struct ComposeInput{ + Point canvas; +}; +struct ComposeExpectation{ + std::optional gba_pixel; // nullopt: black border. +}; +struct ComposeCase{ + std::string name; + ComposeInput input; + ComposeExpectation expectation; +}; -UnitTestResult test_parse_address(){ - struct Expectation{ - std::string host; - uint16_t port; - }; - struct Case{ - std::string name; - std::string input; - std::optional expectation; - }; - const Case CASES[] = { - {"default bridge", "127.0.0.1:47801", Expectation{"127.0.0.1", 47801}}, - {"host name", "localhost:47802", Expectation{"localhost", 47802}}, - {"no port", "127.0.0.1", std::nullopt}, - {"empty port", "127.0.0.1:", std::nullopt}, - {"port 0", "127.0.0.1:0", std::nullopt}, - {"port too large", "127.0.0.1:70000", std::nullopt}, - {"no host", ":47801", std::nullopt}, - }; - for (const Case& c : CASES){ - std::optional> address = parse_mgba_address(c.input); - if (address.has_value() != c.expectation.has_value()){ - return c.name + ": parsed=" + (address ? "true" : "false"); +class Test_Compose : public UnitTest{ +public: + Test_Compose(ComposeCase test_case) + : UnitTest("CommonFramework::VideoSource_MgbaLink - compose - " + test_case.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ImageRGB32 canvas = compose_mgba_frame(make_gba_frame()); + if (canvas.width() != 1920 || canvas.height() != 1080){ + return std::format("canvas is {}x{}, expected 1920x1080", canvas.width(), canvas.height()); } - if (address && (address->first != c.expectation->host || address->second != c.expectation->port)){ - return c.name + ": wrong host or port."; + const std::optional& gba = m_case.expectation.gba_pixel; + uint32_t expected = gba ? 0xff000000 | (uint32_t)(gba->x << 8) | (uint32_t)gba->y : 0xff000000; + uint32_t actual = canvas.pixel(m_case.input.canvas.x, m_case.input.canvas.y); + if (actual != expected){ + return std::format("pixel {:08x}, expected {:08x}", actual, expected); } + return true; } - return true; -} +private: + ComposeCase m_case; +}; -class Test_VideoSource_MgbaLink : public UnitTest{ +// "host:port" parsing for the descriptor. +struct AddressInput{ + std::string address; +}; +struct AddressExpectation{ + std::optional> host_and_port; +}; +struct AddressCase{ + std::string name; + AddressInput input; + AddressExpectation expectation; +}; + +class Test_Address : public UnitTest{ public: - Test_VideoSource_MgbaLink(std::string name, std::function body) - : UnitTest("CommonFramework::VideoSource_MgbaLink - " + std::move(name)) - , m_body(std::move(body)) + Test_Address(AddressCase test_case) + : UnitTest("CommonFramework::VideoSource_MgbaLink - address - " + test_case.name) + , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - return m_body(); + if (parse_mgba_address(m_case.input.address) != m_case.expectation.host_and_port){ + return std::format("unexpected result for <{}>", m_case.input.address); + } + return true; } private: - std::function m_body; + AddressCase m_case; }; -} +} // namespace void add_tests_VideoSource_MgbaLink(UnitTestDatabase& database){ - database.add("compose geometry", test_compose_geometry); - database.add("address", test_parse_address); + // GAME_BOX at 1080p is x = 180..1739, y = 5..1044; 6.5x, so source + // columns alternate 6 and 7 canvas pixels. + const ComposeCase COMPOSE_CASES[] = { + {.name = "box top-left", .input = {{180, 5}}, .expectation = {Point{0, 0}}}, + {.name = "box bottom-right", .input = {{1739, 1044}}, .expectation = {Point{239, 159}}}, + {.name = "left of the box", .input = {{179, 500}}, .expectation = {std::nullopt}}, + {.name = "right of the box", .input = {{1740, 500}}, .expectation = {std::nullopt}}, + {.name = "above the box", .input = {{960, 4}}, .expectation = {std::nullopt}}, + {.name = "below the box", .input = {{960, 1045}}, .expectation = {std::nullopt}}, + {.name = "last pixel of column 0", .input = {{185, 5}}, .expectation = {Point{0, 0}}}, + {.name = "first pixel of column 1", .input = {{186, 5}}, .expectation = {Point{1, 0}}}, + {.name = "last pixel of column 1", .input = {{192, 5}}, .expectation = {Point{1, 0}}}, + {.name = "first pixel of column 2", .input = {{193, 5}}, .expectation = {Point{2, 0}}}, + {.name = "first pixel of row 1", .input = {{180, 11}}, .expectation = {Point{0, 1}}}, + {.name = "centre", .input = {{960, 525}}, .expectation = {Point{120, 80}}}, + }; + for (const ComposeCase& test_case : COMPOSE_CASES){ + database.add(test_case); + } + + const AddressCase ADDRESS_CASES[] = { + {.name = "default bridge", .input = {"127.0.0.1:47801"}, .expectation = {std::make_pair(std::string("127.0.0.1"), uint16_t(47801))}}, + {.name = "host name", .input = {"localhost:47802"}, .expectation = {std::make_pair(std::string("localhost"), uint16_t(47802))}}, + {.name = "no port", .input = {"127.0.0.1"}, .expectation = {std::nullopt}}, + {.name = "empty port", .input = {"127.0.0.1:"}, .expectation = {std::nullopt}}, + {.name = "port 0", .input = {"127.0.0.1:0"}, .expectation = {std::nullopt}}, + {.name = "port too large", .input = {"127.0.0.1:70000"}, .expectation = {std::nullopt}}, + {.name = "no host", .input = {":47801"}, .expectation = {std::nullopt}}, + }; + for (const AddressCase& test_case : ADDRESS_CASES){ + database.add(test_case); + } } -} +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h index f65856b5fe..18ceaa14e6 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h @@ -16,5 +16,5 @@ namespace PokemonAutomation{ void add_tests_VideoSource_MgbaLink(UnitTestDatabase& database); -} +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp index c7db545eaa..3be13fe948 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp @@ -32,6 +32,27 @@ const EnumStringMap VIDEO_TYPE_STRINGS{ }; +namespace{ + +// Loads the saved parameters of one source type into the cache, if present. +template +void load_cached_descriptor( + std::map>& cache, + const JsonObject& obj, + VideoSourceType type +){ + const JsonValue* params = obj.get_value(VIDEO_TYPE_STRINGS.get_string(type)); + if (params == nullptr){ + return; + } + auto descriptor = std::make_unique(); + descriptor->load_json(*params); + cache[type] = std::move(descriptor); +} + +} // namespace + + VideoSourceOption::VideoSourceOption() : m_resolution(1920, 1080) , m_format(VideoFormat::OTHER) @@ -60,7 +81,7 @@ std::shared_ptr VideoSourceOption::get_descriptor_from_ca descriptor.reset(new VideoSourceDescriptor_Camera()); break; case VideoSourceType::MgbaLink: - descriptor.reset(new VideoSourceDescriptor_MgbaLink()); + descriptor = std::make_shared(); break; default:; descriptor.reset(new VideoSourceDescriptor_Null()); @@ -124,29 +145,9 @@ void VideoSourceOption::load_json(const JsonValue& json){ } #endif - const JsonValue* params; - params = obj->get_value(VIDEO_TYPE_STRINGS.get_string(VideoSourceType::StillImage)); - if (params != nullptr){ - auto x = std::make_unique(); - x->load_json(*params); - m_descriptor_cache[VideoSourceType::StillImage] = std::move(x); - } - params = obj->get_value(VIDEO_TYPE_STRINGS.get_string(VideoSourceType::VideoPlayback)); - if (params != nullptr){ -// m_descriptor_cache[VideoSourceType::VideoPlayback] = - } - params = obj->get_value(VIDEO_TYPE_STRINGS.get_string(VideoSourceType::Camera)); - if (params != nullptr){ - auto x = std::make_unique(); - x->load_json(*params); - m_descriptor_cache[VideoSourceType::Camera] = std::move(x); - } - params = obj->get_value(VIDEO_TYPE_STRINGS.get_string(VideoSourceType::MgbaLink)); - if (params != nullptr){ - auto x = std::make_unique(); - x->load_json(*params); - m_descriptor_cache[VideoSourceType::MgbaLink] = std::move(x); - } + load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::StillImage); + load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::Camera); + load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::MgbaLink); auto iter = m_descriptor_cache.find(VIDEO_TYPE_STRINGS.get_enum(*type, VideoSourceType::None)); if (iter == m_descriptor_cache.end()){ diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index af07f73363..6b1a61c9b9 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -71,7 +71,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_SinkPipeline(ret); add_tests_AudioFileLoader(ret); add_tests_LabChannel(ret); - MgbaLink::add_tests(ret, GlobalThreadPools::unlimited_normal()); + MgbaLink::add_tests(ret); add_tests_VideoSource_MgbaLink(ret); OCR::add_tests(ret); Kernels::add_tests(ret); diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp index 8a945a5985..7a58f2eaa0 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp @@ -157,12 +157,12 @@ void MgbaLink_Connection::on_receive_data(const void* data, size_t bytes){ bool overflowed; { std::lock_guard lg(m_lock); - for (std::string& line : m_splitter.push(std::string_view((const char*)data, bytes))){ + for (std::string& line : m_splitter.push(std::string_view(static_cast(data), bytes))){ m_replies.emplace_back(std::move(line)); } overflowed = m_splitter.overflowed(); if (overflowed){ - m_disconnect_error = "reply line longer than " + std::to_string(MAX_LINE_BYTES) + " bytes"; + m_disconnect_error = "reply line longer than " + std::to_string(LineSplitter::MAX_LINE_BYTES) + " bytes"; } m_cv.notify_all(); } @@ -181,5 +181,5 @@ void MgbaLink_Connection::on_disconnect(const std::string& error_message){ -} -} +} // namespace MgbaLink +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h index e1c06e3fd1..ca74611c98 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h @@ -32,6 +32,7 @@ namespace PokemonAutomation{ namespace MgbaLink{ +// A synchronous MgbaLink v1 client: open(), then one command at a time. class MgbaLink_Connection final : private AbstractClientSocket::Listener{ public: MgbaLink_Connection( @@ -95,6 +96,6 @@ class MgbaLink_Connection final : private AbstractClientSocket::Listener{ -} -} +} // namespace MgbaLink +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp index eb24e47661..c521a254ca 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp @@ -36,15 +36,14 @@ std::optional parse_unsigned(std::string_view text){ if (text.empty()){ return std::nullopt; } - const char* end = text.data() + text.size(); - std::from_chars_result result = std::from_chars(text.data(), end, value); - if (result.ec != std::errc() || result.ptr != end){ + std::from_chars_result result = std::from_chars(text.data(), text.data() + text.size(), value); + if (result.ec != std::errc() || result.ptr != text.data() + text.size()){ return std::nullopt; } return value; } -} +} // namespace @@ -152,5 +151,5 @@ bool FrameClock::resync(WallClock now, uint64_t frame, WallDuration max_drift){ return true; } -} -} +} // namespace MgbaLink +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h index 2365b7ce41..dbbe5a03d4 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h @@ -23,20 +23,14 @@ namespace PokemonAutomation{ namespace MgbaLink{ -// GBA frame rate: 16.78 MHz / 280,896 cycles per frame. -constexpr double GBA_FRAMES_PER_SECOND = 59.7275; - -// The bridge drops a client whose command line is longer than this; replies -// are bounded the same way (a FRAME path is the longest reply). -constexpr size_t MAX_LINE_BYTES = 4096; - - +// The fields of a "HELLO 1" reply. struct HelloReply{ std::string game_code; // "BPRE" for FireRed US. std::string bridge_version; // "bridge-0.1" std::string mode; // "paced" or "free" }; +// The fields of a "FRAME" reply (0.10.5: the frame is a PNG on disk). struct FrameReply{ uint64_t frame; size_t width; @@ -67,6 +61,10 @@ std::optional parse_frame_reply(std::string_view reply); // drops everything buffered so far; the caller should then disconnect. class LineSplitter{ public: + // The bridge drops a client whose command line is longer than this; + // replies are bounded the same way (a FRAME path is the longest reply). + static constexpr size_t MAX_LINE_BYTES = 4096; + explicit LineSplitter(size_t max_line = MAX_LINE_BYTES) : m_max_line(max_line) {} @@ -91,6 +89,9 @@ class LineSplitter{ // frame's timestamp comes from its number, not from when the app read it. class FrameClock{ public: + // GBA frame rate: 16.78 MHz / 280,896 cycles per frame. + static constexpr double GBA_FRAMES_PER_SECOND = 59.7275; + FrameClock(WallClock t0, uint64_t f0) : m_t0(t0) , m_f0(f0) @@ -118,6 +119,6 @@ class FrameClock{ -} -} +} // namespace MgbaLink +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp index b81073bf4b..7f88a9af55 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp @@ -13,6 +13,7 @@ #include "Common/Cpp/Concurrency/ConditionVariable.h" #include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/Tools/GlobalThreadPools.h" #ifdef _WIN32 #include "Common/Cpp/Sockets/ServerSocket_WinSocket.h" #else @@ -34,168 +35,187 @@ using FakeServerSocket = ServerSocket_POSIX; #endif -UnitTestResult test_line_splitter(){ - struct Input{ - std::vector chunks; - size_t max_line; - }; - struct Expectation{ - std::vector lines; - bool overflowed; - }; - struct Case{ - std::string name; - Input input; - Expectation expectation; - }; - const Case CASES[] = { - {"one line", {{"OK\n"}, 16}, {{"OK"}, false}}, - {"CR before LF dropped", {{"OK 5\r\n"}, 16}, {{"OK 5"}, false}}, - {"line split across chunks", {{"FRAME 12", "0 240 160 /a b.png\nOK"}, 64}, {{"FRAME 120 240 160 /a b.png"}, false}}, - {"two lines in one chunk", {{"OK\nLATE 3\n"}, 16}, {{"OK", "LATE 3"}, false}}, - {"empty line", {{"\n"}, 16}, {{""}, false}}, - {"exactly max", {{std::string(8, 'a') + "\r\n"}, 8}, {{std::string(8, 'a')}, false}}, - {"over max without LF", {{std::string(10, 'a')}, 8}, {{}, true}}, - {"over max across chunks", {{"aaaaa", "aaaaa\n"}, 8}, {{}, true}}, - }; - for (const Case& c : CASES){ - LineSplitter splitter(c.input.max_line); +// Line framing over a byte stream. +struct LineFramingInput{ + std::vector chunks; + size_t max_line; +}; +struct LineFramingExpectation{ + std::vector lines; + bool overflowed; +}; +struct LineFramingCase{ + std::string name; + LineFramingInput input; + LineFramingExpectation expectation; +}; + +class Test_LineFraming : public UnitTest{ +public: + Test_LineFraming(LineFramingCase test_case) + : UnitTest("Controllers::MgbaLink - line framing - " + test_case.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + LineSplitter splitter(m_case.input.max_line); std::vector lines; - for (const std::string& chunk : c.input.chunks){ + for (const std::string& chunk : m_case.input.chunks){ for (std::string& line : splitter.push(chunk)){ lines.emplace_back(std::move(line)); } } - if (lines != c.expectation.lines || splitter.overflowed() != c.expectation.overflowed){ - return std::format("{}: got {} line(s), overflowed={}", c.name, lines.size(), splitter.overflowed()); + if (lines != m_case.expectation.lines || splitter.overflowed() != m_case.expectation.overflowed){ + return std::format("got {} line(s), overflowed={}", lines.size(), splitter.overflowed()); } + return true; } - return true; -} + +private: + LineFramingCase m_case; +}; -UnitTestResult test_parse_hello(){ - struct Case{ - std::string name; - std::string input; - std::optional expected_code; - }; - const Case CASES[] = { - {"v1 reply", "OK 1 BPRE bridge-0.1 paced", "BPRE"}, - {"wrong version", "OK 2 BPRE bridge-0.1 paced", std::nullopt}, - {"missing mode", "OK 1 BPRE bridge-0.1", std::nullopt}, - {"extra field", "OK 1 BPRE bridge-0.1 paced x", std::nullopt}, - {"error", "ERR busy", std::nullopt}, - }; - for (const Case& c : CASES){ - std::optional hello = parse_hello_reply(c.input); - std::optional code; - if (hello){ - code = hello->game_code; + +// Reply parsing. Each parser's result is printed as one string, so one table +// covers all reply kinds; nullopt means the reply was rejected. +enum class ReplyKind{ + HELLO, + OK_FRAME, + KEYS, + FRAME, +}; +struct ReplyInput{ + ReplyKind kind; + std::string reply; +}; +struct ReplyExpectation{ + std::optional parsed; +}; +struct ReplyCase{ + std::string name; + ReplyInput input; + ReplyExpectation expectation; +}; + +std::optional describe_reply(const ReplyInput& input){ + switch (input.kind){ + case ReplyKind::HELLO:{ + std::optional hello = parse_hello_reply(input.reply); + if (!hello){ + return std::nullopt; } - if (code != c.expected_code){ - return c.name + ": unexpected result."; + return std::format("{}|{}|{}", hello->game_code, hello->bridge_version, hello->mode); + } + case ReplyKind::OK_FRAME:{ + std::optional frame = parse_ok_frame_reply(input.reply); + return frame ? std::optional(std::to_string(*frame)) : std::nullopt; + } + case ReplyKind::KEYS:{ + std::optional late = parse_keys_reply(input.reply); + return late ? std::optional(std::to_string(*late)) : std::nullopt; + } + case ReplyKind::FRAME:{ + std::optional frame = parse_frame_reply(input.reply); + if (!frame){ + return std::nullopt; } + return std::format("{}|{}|{}|{}", frame->frame, frame->width, frame->height, frame->path); } - return true; + } + return std::nullopt; } - -UnitTestResult test_parse_frame(){ - struct Expectation{ - uint64_t frame; - size_t width; - size_t height; - std::string path; - }; - struct Case{ - std::string name; - std::string input; - std::optional expectation; - }; - const Case CASES[] = { - {"0.10.5 reply", "FRAME 1234 240 160 /tmp/mgba-bridge-47801.png", Expectation{1234, 240, 160, "/tmp/mgba-bridge-47801.png"}}, - {"path with spaces", "FRAME 7 240 160 /Users/x/My Folder/f.png", Expectation{7, 240, 160, "/Users/x/My Folder/f.png"}}, - {"no path", "FRAME 7 240 160", std::nullopt}, - {"negative frame", "FRAME -7 240 160 /f.png", std::nullopt}, - {"error", "ERR no-game", std::nullopt}, - }; - for (const Case& c : CASES){ - std::optional reply = parse_frame_reply(c.input); - if (reply.has_value() != c.expectation.has_value()){ - return c.name + ": parsed=" + (reply ? "true" : "false"); - } - if (reply && ( - reply->frame != c.expectation->frame || reply->width != c.expectation->width || - reply->height != c.expectation->height || reply->path != c.expectation->path - )){ - return c.name + ": wrong fields."; +class Test_Reply : public UnitTest{ +public: + Test_Reply(ReplyCase test_case) + : UnitTest("Controllers::MgbaLink - reply - " + test_case.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::optional parsed = describe_reply(m_case.input); + if (parsed != m_case.expectation.parsed){ + return std::format("parsed as <{}>", parsed.value_or("(rejected)")); } + return true; } - return true; -} +private: + ReplyCase m_case; +}; -UnitTestResult test_parse_keys_and_ok_frame(){ - struct Case{ - std::string name; - std::function(std::string_view)> parser; - std::string input; - std::optional expected; - }; - const Case CASES[] = { - {"KEYS on time", parse_keys_reply, "OK", 0}, - {"KEYS late", parse_keys_reply, "LATE 3", 3}, - {"KEYS late without count", parse_keys_reply, "LATE", std::nullopt}, - {"KEYS error", parse_keys_reply, "ERR usage KEYS ", std::nullopt}, - {"GRANT reply", parse_ok_frame_reply, "OK 4521", 4521}, - {"bare OK", parse_ok_frame_reply, "OK", std::nullopt}, - {"OK with text", parse_ok_frame_reply, "OK BYE", std::nullopt}, - }; - for (const Case& c : CASES){ - if (c.parser(c.input) != c.expected){ - return c.name + ": unexpected result."; + + +// Frame numbers to timestamps, anchored at (t0, frame 1000). +struct FrameClockInput{ + uint64_t frame; +}; +struct FrameClockExpectation{ + std::chrono::microseconds offset_from_anchor; +}; +struct FrameClockCase{ + std::string name; + FrameClockInput input; + FrameClockExpectation expectation; +}; + +const WallClock CLOCK_T0 = WallClock() + std::chrono::hours(1000); + +class Test_FrameClock : public UnitTest{ +public: + Test_FrameClock(FrameClockCase test_case) + : UnitTest("Controllers::MgbaLink - frame clock - " + test_case.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + FrameClock clock(CLOCK_T0, 1000); + WallDuration offset = clock.timestamp(m_case.input.frame) - CLOCK_T0; + if (offset != std::chrono::duration_cast(m_case.expectation.offset_from_anchor)){ + return std::format("offset {} us", std::chrono::duration_cast(offset).count()); } + return true; } - return true; -} +private: + FrameClockCase m_case; +}; -UnitTestResult test_frame_clock(){ - using std::chrono::microseconds; - const WallClock t0 = WallClock() + std::chrono::hours(1000); - struct Case{ - std::string name; - uint64_t frame; - microseconds expected_offset; - }; - // Anchored at frame 1000; one frame = 16742.7 us. - const Case CASES[] = { - {"anchor frame", 1000, microseconds(0)}, - {"next frame", 1001, microseconds(16743)}, - {"one second of frames", 1060, microseconds(1004562)}, - {"before the anchor (after a LOAD)", 940, microseconds(-1004562)}, - }; - FrameClock clock(t0, 1000); - for (const Case& c : CASES){ - WallDuration offset = clock.timestamp(c.frame) - t0; - if (offset != std::chrono::duration_cast(c.expected_offset)){ - return std::format( - "{}: offset {} us", - c.name, std::chrono::duration_cast(offset).count() - ); + + +// Re-anchoring when frame 1060 (1004.6 ms after the anchor) is read late. +struct ResyncInput{ + std::chrono::milliseconds read_after_anchor; +}; +struct ResyncExpectation{ + bool resynced; + std::chrono::milliseconds frame_1060_after_anchor; +}; +struct ResyncCase{ + std::string name; + ResyncInput input; + ResyncExpectation expectation; +}; + +class Test_Resync : public UnitTest{ +public: + Test_Resync(ResyncCase test_case) + : UnitTest("Controllers::MgbaLink - clock resync - " + test_case.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + FrameClock clock(CLOCK_T0, 1000); + bool resynced = clock.resync(CLOCK_T0 + m_case.input.read_after_anchor, 1060, std::chrono::milliseconds(500)); + WallDuration offset = clock.timestamp(1060) - CLOCK_T0; + if (resynced != m_case.expectation.resynced || + std::chrono::duration_cast(offset) != m_case.expectation.frame_1060_after_anchor + ){ + return std::format("resynced={}", resynced); } + return true; } - if (clock.resync(t0 + std::chrono::milliseconds(1100), 1060, std::chrono::milliseconds(500))){ - return "resync: 95 ms of drift should keep the anchor."; - } - if (!clock.resync(t0 + std::chrono::seconds(10), 1060, std::chrono::milliseconds(500)) || - clock.timestamp(1060) != t0 + std::chrono::seconds(10) - ){ - return "resync: 9 s of drift (mGBA paused) should re-anchor at the read time."; - } - return true; -} + +private: + ResyncCase m_case; +}; @@ -278,10 +298,14 @@ std::optional FakeBridge::start(){ void FakeBridge::on_receive_data(const void* data, size_t bytes){ if (m_busy){ + // Record what the client sent, then refuse it. + std::lock_guard lg(m_lock); + m_commands.emplace_back("(refused)"); + m_cv.notify_all(); m_server.close_connection(); return; } - std::vector lines = m_splitter.push(std::string_view((const char*)data, bytes)); + std::vector lines = m_splitter.push(std::string_view(static_cast(data), bytes)); for (const std::string& line : lines){ { std::lock_guard lg(m_lock); @@ -320,93 +344,149 @@ std::string FakeBridge::reply_for(std::string_view line){ -UnitTestResult test_session(Logger& logger, ThreadPool& thread_pool){ - FakeBridge bridge(thread_pool, false); - std::optional port = bridge.start(); - if (!port){ - return UnitTestResult(UnitTestResult::SKIPPED, "No free port for the fake bridge."); - } - { - MgbaLink_Connection connection(logger, thread_pool, "127.0.0.1", *port); - HelloReply hello = connection.open(); - if (hello.game_code != "BPRE" || hello.mode != "paced"){ - return "HELLO: wrong game code or mode."; - } - FrameReply frame = connection.frame(); - if (frame.frame != 120 || frame.path != "/tmp/mgba bridge.png"){ - return "FRAME: wrong frame number or path."; - } - if (connection.keys(100, 1) != 20 || connection.keys(130, 1) != 0){ - return "KEYS: expected LATE 20, then on time."; - } - try{ - connection.command("FLY"); - return "An ERR reply should throw."; - }catch (ConnectionException&){} - } - const std::vector expected{"HELLO 1", "FRAME", "KEYS 100 1", "KEYS 130 1", "FLY", "BYE"}; - if (!bridge.wait_for_commands(expected.size()) || bridge.commands() != expected){ - return std::format("The bridge saw {} command(s); the session should end with BYE.", bridge.commands().size()); - } - return true; -} +// A whole session against the fake bridge: open, FRAME, KEYS late and on +// time, an ERR reply, then close (BYE). +struct SessionInput{ + bool bridge_busy; +}; +struct SessionExpectation{ + std::vector results; + std::vector bridge_saw; +}; +struct SessionCase{ + std::string name; + SessionInput input; + SessionExpectation expectation; +}; -UnitTestResult test_busy_bridge(Logger& logger, ThreadPool& thread_pool){ - FakeBridge bridge(thread_pool, true); - std::optional port = bridge.start(); - if (!port){ - return UnitTestResult(UnitTestResult::SKIPPED, "No free port for the fake bridge."); - } - MgbaLink_Connection connection(logger, thread_pool, "127.0.0.1", *port); +std::vector run_session(Logger& logger, ThreadPool& thread_pool, uint16_t port){ + std::vector results; + MgbaLink_Connection connection(logger, thread_pool, "127.0.0.1", port); try{ - connection.open(); + HelloReply hello = connection.open(); + results.emplace_back(std::format("hello {} {}", hello.game_code, hello.mode)); + FrameReply frame = connection.frame(); + results.emplace_back(std::format("frame {} {}", frame.frame, frame.path)); + results.emplace_back(std::format("late {}", connection.keys(100, 1))); + results.emplace_back(std::format("late {}", connection.keys(130, 1))); + connection.command("FLY"); + results.emplace_back("FLY accepted"); }catch (ConnectionException& e){ - if (e.message().find("ERR busy") == std::string::npos){ - return "Expected the bridge's ERR busy in the error, got: " + e.message(); - } - return true; + bool busy = e.message().find("ERR busy") != std::string::npos; + results.emplace_back(busy ? "failed: ERR busy" : "failed: " + e.message()); } - return "open() should fail when the bridge already has a client."; + return results; } - - -class Test_MgbaLink : public UnitTest{ +class Test_Session : public UnitTest{ public: - using Body = std::function; - - Test_MgbaLink(std::string name, ThreadPool& thread_pool, Body body) - : UnitTest("Controllers::MgbaLink - " + std::move(name)) - , m_thread_pool(thread_pool) - , m_body(std::move(body)) + Test_Session(SessionCase test_case) + : UnitTest("Controllers::MgbaLink - fake bridge - " + test_case.name) + , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - return m_body(logger, m_thread_pool); + ThreadPool& thread_pool = GlobalThreadPools::unlimited_normal(); + FakeBridge bridge(thread_pool, m_case.input.bridge_busy); + std::optional port = bridge.start(); + if (!port){ + return UnitTestResult(UnitTestResult::SKIPPED, "No free port for the fake bridge."); + } + std::vector results = run_session(logger, thread_pool, *port); + if (results != m_case.expectation.results){ + return std::format("result {}: <{}>", results.size(), results.empty() ? "" : results.back()); + } + bridge.wait_for_commands(m_case.expectation.bridge_saw.size()); + if (bridge.commands() != m_case.expectation.bridge_saw){ + return std::format("the bridge saw {} command(s)", bridge.commands().size()); + } + return true; } private: - ThreadPool& m_thread_pool; - Body m_body; + SessionCase m_case; }; -Test_MgbaLink::Body pure(UnitTestResult (*body)()){ - return [body](Logger&, ThreadPool&){ return body(); }; -} +} // namespace -} +void add_tests(UnitTestDatabase& database){ + const LineFramingCase LINE_FRAMING_CASES[] = { + {.name = "one line", .input = {{"OK\n"}, 16}, .expectation = {{"OK"}, false}}, + {.name = "CR before LF dropped", .input = {{"OK 5\r\n"}, 16}, .expectation = {{"OK 5"}, false}}, + {.name = "line split across chunks", .input = {{"FRAME 12", "0 240 160 /a b.png\nOK"}, 64}, .expectation = {{"FRAME 120 240 160 /a b.png"}, false}}, + {.name = "two lines in one chunk", .input = {{"OK\nLATE 3\n"}, 16}, .expectation = {{"OK", "LATE 3"}, false}}, + {.name = "empty line", .input = {{"\n"}, 16}, .expectation = {{""}, false}}, + {.name = "exactly max", .input = {{"aaaaaaaa\r\n"}, 8}, .expectation = {{"aaaaaaaa"}, false}}, + {.name = "over max without LF", .input = {{"aaaaaaaaaa"}, 8}, .expectation = {{}, true}}, + {.name = "over max across chunks", .input = {{"aaaaa", "aaaaa\n"}, 8}, .expectation = {{}, true}}, + }; + for (const LineFramingCase& test_case : LINE_FRAMING_CASES){ + database.add(test_case); + } -void add_tests(UnitTestDatabase& database, ThreadPool& thread_pool){ - database.add("line framing", thread_pool, pure(test_line_splitter)); - database.add("HELLO reply", thread_pool, pure(test_parse_hello)); - database.add("FRAME reply", thread_pool, pure(test_parse_frame)); - database.add("KEYS and OK replies", thread_pool, pure(test_parse_keys_and_ok_frame)); - database.add("frame clock", thread_pool, pure(test_frame_clock)); - database.add("fake bridge session", thread_pool, test_session); - database.add("fake bridge busy", thread_pool, test_busy_bridge); -} + const ReplyCase REPLY_CASES[] = { + {.name = "HELLO v1", .input = {ReplyKind::HELLO, "OK 1 BPRE bridge-0.1 paced"}, .expectation = {"BPRE|bridge-0.1|paced"}}, + {.name = "HELLO wrong version", .input = {ReplyKind::HELLO, "OK 2 BPRE bridge-0.1 paced"}, .expectation = {std::nullopt}}, + {.name = "HELLO missing mode", .input = {ReplyKind::HELLO, "OK 1 BPRE bridge-0.1"}, .expectation = {std::nullopt}}, + {.name = "HELLO extra field", .input = {ReplyKind::HELLO, "OK 1 BPRE bridge-0.1 paced x"}, .expectation = {std::nullopt}}, + {.name = "HELLO busy", .input = {ReplyKind::HELLO, "ERR busy"}, .expectation = {std::nullopt}}, + {.name = "GRANT frame", .input = {ReplyKind::OK_FRAME, "OK 4521"}, .expectation = {"4521"}}, + {.name = "bare OK", .input = {ReplyKind::OK_FRAME, "OK"}, .expectation = {std::nullopt}}, + {.name = "BYE is not a frame", .input = {ReplyKind::OK_FRAME, "OK BYE"}, .expectation = {std::nullopt}}, + {.name = "KEYS on time", .input = {ReplyKind::KEYS, "OK"}, .expectation = {"0"}}, + {.name = "KEYS late", .input = {ReplyKind::KEYS, "LATE 3"}, .expectation = {"3"}}, + {.name = "KEYS late without count", .input = {ReplyKind::KEYS, "LATE"}, .expectation = {std::nullopt}}, + {.name = "KEYS usage error", .input = {ReplyKind::KEYS, "ERR usage KEYS "}, .expectation = {std::nullopt}}, + {.name = "FRAME on 0.10.5", .input = {ReplyKind::FRAME, "FRAME 1234 240 160 /tmp/mgba-bridge-47801.png"}, .expectation = {"1234|240|160|/tmp/mgba-bridge-47801.png"}}, + {.name = "FRAME path with spaces", .input = {ReplyKind::FRAME, "FRAME 7 240 160 /Users/x/My Folder/f.png"}, .expectation = {"7|240|160|/Users/x/My Folder/f.png"}}, + {.name = "FRAME without path", .input = {ReplyKind::FRAME, "FRAME 7 240 160"}, .expectation = {std::nullopt}}, + {.name = "FRAME negative number", .input = {ReplyKind::FRAME, "FRAME -7 240 160 /f.png"}, .expectation = {std::nullopt}}, + {.name = "FRAME no game", .input = {ReplyKind::FRAME, "ERR no-game"}, .expectation = {std::nullopt}}, + }; + for (const ReplyCase& test_case : REPLY_CASES){ + database.add(test_case); + } + // One frame = 1000 / 59.7275 ms = 16742.7 us. + const FrameClockCase FRAME_CLOCK_CASES[] = { + {.name = "anchor frame", .input = {1000}, .expectation = {std::chrono::microseconds(0)}}, + {.name = "next frame", .input = {1001}, .expectation = {std::chrono::microseconds(16743)}}, + {.name = "60 frames", .input = {1060}, .expectation = {std::chrono::microseconds(1004562)}}, + {.name = "before the anchor (after a LOAD)", .input = {940}, .expectation = {std::chrono::microseconds(-1004562)}}, + }; + for (const FrameClockCase& test_case : FRAME_CLOCK_CASES){ + database.add(test_case); + } + const ResyncCase RESYNC_CASES[] = { + {.name = "small drift keeps the anchor", .input = {std::chrono::milliseconds(1100)}, .expectation = {false, std::chrono::milliseconds(1004)}}, + {.name = "mGBA paused re-anchors", .input = {std::chrono::milliseconds(10000)}, .expectation = {true, std::chrono::milliseconds(10000)}}, + }; + for (const ResyncCase& test_case : RESYNC_CASES){ + database.add(test_case); + } + const SessionCase SESSION_CASES[] = { + { + .name = "session", + .input = {false}, + .expectation = { + {"hello BPRE paced", "frame 120 /tmp/mgba bridge.png", "late 20", "late 0", "failed: MgbaLink: FLY -> ERR unknown FLY"}, + {"HELLO 1", "FRAME", "KEYS 100 1", "KEYS 130 1", "FLY", "BYE"}, + }, + }, + { + .name = "busy", + .input = {true}, + .expectation = {{"failed: ERR busy"}, {"(refused)"}}, + }, + }; + for (const SessionCase& test_case : SESSION_CASES){ + database.add(test_case); + } } -} + + + +} // namespace MgbaLink +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h index 8f85efb465..94ce429843 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h @@ -7,7 +7,6 @@ #ifndef PokemonAutomation_Controllers_MgbaLink_Tests_H #define PokemonAutomation_Controllers_MgbaLink_Tests_H -#include "Common/Cpp/Concurrency/ThreadPool.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" namespace PokemonAutomation{ @@ -15,10 +14,10 @@ namespace MgbaLink{ // Reply parsing, line framing, the frame clock, and a connection against a -// fake bridge on 127.0.0.1 (sockets run on "thread_pool"). -void add_tests(UnitTestDatabase& database, ThreadPool& thread_pool); +// fake bridge on 127.0.0.1. +void add_tests(UnitTestDatabase& database); -} -} +} // namespace MgbaLink +} // namespace PokemonAutomation #endif From 06d50ddd135b75555b38d7ec858f4bf40b258788 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:11:10 -0800 Subject: [PATCH 3/5] MgbaLink tests: split registration by table --- .../Source/Controllers/MgbaLink/MgbaLink_Tests.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp index 7f88a9af55..a1e5913953 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp @@ -406,10 +406,8 @@ class Test_Session : public UnitTest{ SessionCase m_case; }; -} // namespace - -void add_tests(UnitTestDatabase& database){ +void add_framing_and_reply_tests(UnitTestDatabase& database){ const LineFramingCase LINE_FRAMING_CASES[] = { {.name = "one line", .input = {{"OK\n"}, 16}, .expectation = {{"OK"}, false}}, {.name = "CR before LF dropped", .input = {{"OK 5\r\n"}, 16}, .expectation = {{"OK 5"}, false}}, @@ -446,7 +444,9 @@ void add_tests(UnitTestDatabase& database){ for (const ReplyCase& test_case : REPLY_CASES){ database.add(test_case); } +} +void add_clock_and_session_tests(UnitTestDatabase& database){ // One frame = 1000 / 59.7275 ms = 16742.7 us. const FrameClockCase FRAME_CLOCK_CASES[] = { {.name = "anchor frame", .input = {1000}, .expectation = {std::chrono::microseconds(0)}}, @@ -486,6 +486,14 @@ void add_tests(UnitTestDatabase& database){ } } +} // namespace + + +void add_tests(UnitTestDatabase& database){ + add_framing_and_reply_tests(database); + add_clock_and_session_tests(database); +} + } // namespace MgbaLink From bd6f9f708897d63c065dbe5fcd4a308c4b9ea658 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:56:51 -0800 Subject: [PATCH 4/5] MgbaLink: review fixes (clock, dead bridge, abort, single-use open) - FrameClock::stamp(): never stamps a frame later than its read time (re-anchors when mGBA runs ahead), keeps stamps monotonic; only lag re-anchors are logged. Table cases for running ahead and a frame going back. - VideoSource_MgbaLink: snapshot_recent_nonblocking() honours min_time; the last frame is dropped when the connection ends; each new frame goes through report_source_frame() and the widget reports rendered frames; the no-video watchdog is no longer opted out. - Connection::abort(): wakes open() or a pending command at once and skips BYE; the source's destructor aborts the active connection so it never waits out a 5 s timeout. open() is single-use (InternalProgramError). - Lows: FrameReply defaults, PNG size checked against FRAME, std::format, test messages print expected vs read, repeated retry errors logged once, saved mGBA selection ignored outside developer mode, box-in-canvas check, loopback hosts only, MgbaLink_Connection -> MgbaLink::Connection. --- .../Sources/VideoSource_MgbaLink.cpp | 150 ++++++++++-- .../Sources/VideoSource_MgbaLink.h | 26 ++- .../Sources/VideoSource_MgbaLink_Tests.cpp | 67 +++++- .../VideoPipeline/VideoSourceOption.cpp | 5 +- .../MgbaLink/MgbaLink_Connection.cpp | 128 ++++++---- .../MgbaLink/MgbaLink_Connection.h | 23 +- .../MgbaLink/MgbaLink_Protocol.cpp | 21 +- .../Controllers/MgbaLink/MgbaLink_Protocol.h | 28 ++- .../Controllers/MgbaLink/MgbaLink_Tests.cpp | 218 +++++++++++++----- 9 files changed, 514 insertions(+), 152 deletions(-) diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp index c310e3ff98..c0f3066d3e 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp @@ -5,13 +5,17 @@ */ #include +#include +#include #include #include +#include #include #include "Common/Cpp/Color.h" #include "Common/Cpp/Exceptions.h" #include "CommonFramework/ImageTypes/ImageRGB32_Qt.h" #include "CommonFramework/Tools/GlobalThreadPools.h" +#include "CommonFramework/VideoPipeline/Backends/VideoFrameQt.h" #include "VideoSource_MgbaLink.h" namespace PokemonAutomation{ @@ -21,20 +25,62 @@ using namespace MgbaLink; namespace{ -// Re-anchor the frame clock when a frame maps further than this from the -// time it was read (mGBA paused, or running free without GRANT pacing). -constexpr std::chrono::milliseconds MAX_CLOCK_DRIFT(500); +// Re-anchor the frame clock when a frame maps further than this before +// the time it was read (mGBA paused, or slower than real time). +constexpr std::chrono::milliseconds MAX_CLOCK_LAG(500); constexpr std::chrono::milliseconds RECONNECT_DELAY(2000); // One GBA frame, rounded down: polling faster only repeats a frame number. constexpr std::chrono::milliseconds POLL_PERIOD(16); + +// Forwards to "logger" every line but a repeat of the last one forwarded: +// a bridge that stays away fails the same way on every retry. +class RepeatSuppressingLogger : public Logger{ +public: + explicit RepeatSuppressingLogger(Logger& logger) + : m_logger(logger) + {} + + virtual void log(const std::string& msg, Color color) override{ + { + std::lock_guard lg(m_lock); + if (msg == m_last){ + return; + } + m_last = msg; + } + m_logger.log(msg, color); + } + +private: + Logger& m_logger; + Mutex m_lock; + std::string m_last; +}; + +bool is_local_host(std::string_view host){ + return host == "127.0.0.1" || host == "localhost"; +} + } // namespace ImageRGB32 compose_mgba_frame(const ImageViewRGB32& gba_frame, const MgbaComposeGeometry& geometry){ + if (geometry.box_x + geometry.box_width > geometry.canvas_width || + geometry.box_y + geometry.box_height > geometry.canvas_height + ){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + std::format( + "mGBA box {}x{} at ({}, {}) does not fit in a {}x{} canvas.", + geometry.box_width, geometry.box_height, geometry.box_x, geometry.box_y, + geometry.canvas_width, geometry.canvas_height + ) + ); + } ImageRGB32 canvas(geometry.canvas_width, geometry.canvas_height); canvas.fill(0xff000000); const size_t src_width = gba_frame.width(); @@ -67,7 +113,11 @@ std::optional> parse_mgba_address(std::string_v if (port_text.empty() || result.ec != std::errc() || result.ptr != port_text.data() + port_text.size() || port == 0){ return std::nullopt; } - return std::make_pair(std::string(address.substr(0, colon)), port); + std::string_view host = address.substr(0, colon); + if (!is_local_host(host)){ + return std::nullopt; + } + return std::make_pair(std::string(host), port); } @@ -107,7 +157,7 @@ std::unique_ptr VideoSourceDescriptor_MgbaLink::make_VideoSource( VideoSource_MgbaLink::VideoSource_MgbaLink(Logger& logger, std::string address) - : VideoSource(logger, false) + : VideoSource(logger, true) , m_logger(logger) , m_address(std::move(address)) { @@ -120,6 +170,9 @@ VideoSource_MgbaLink::~VideoSource_MgbaLink(){ { std::lock_guard lg(m_lock); m_stopping = true; + if (m_connection != nullptr){ + m_connection->abort(); + } } m_cv.notify_all(); m_poller.wait_and_ignore_exceptions(); @@ -132,6 +185,9 @@ VideoSnapshot VideoSource_MgbaLink::snapshot_latest_blocking(){ } VideoSnapshot VideoSource_MgbaLink::snapshot_recent_nonblocking(WallClock min_time){ std::lock_guard lg(m_lock); + if (m_last.timestamp < min_time){ + return VideoSnapshot(); + } return m_last; } @@ -144,23 +200,50 @@ bool VideoSource_MgbaLink::wait_or_stop(std::chrono::milliseconds duration){ void VideoSource_MgbaLink::poll_loop(){ std::optional> address = parse_mgba_address(m_address); if (!address){ - m_logger.log("MgbaLink: invalid address \"" + m_address + "\"; expected host:port.", COLOR_RED); + m_logger.log( + "MgbaLink: invalid address \"" + m_address + "\"; expected 127.0.0.1: or localhost:.", + COLOR_RED + ); return; } + m_logger.log(std::format( + "MgbaLink: polling {}; retrying every {} s while it is away.", + m_address, std::chrono::duration_cast(RECONNECT_DELAY).count() + )); + RepeatSuppressingLogger logger(m_logger); do{ try{ - MgbaLink_Connection connection( - m_logger, GlobalThreadPools::unlimited_realtime(), address->first, address->second - ); - connection.open(); - poll_connected(connection); - }catch (Exception& e){ - m_logger.log(e.message() + " Retrying in 2 s.", COLOR_ORANGE); + run_connection(logger, address->first, address->second); + }catch (Exception&){ + // Already logged (once) by the exception; retry. } }while (!wait_or_stop(RECONNECT_DELAY)); } -void VideoSource_MgbaLink::poll_connected(MgbaLink_Connection& connection){ +void VideoSource_MgbaLink::run_connection(Logger& logger, const std::string& host, uint16_t port){ + Connection connection(logger, GlobalThreadPools::unlimited_realtime(), host, port); + { + std::lock_guard lg(m_lock); + if (m_stopping){ + return; + } + m_connection = &connection; + } + try{ + connection.open(); + poll_connected(connection); + }catch (...){ + std::lock_guard lg(m_lock); + m_connection = nullptr; + m_last = VideoSnapshot(); + throw; + } + std::lock_guard lg(m_lock); + m_connection = nullptr; + m_last = VideoSnapshot(); +} + +void VideoSource_MgbaLink::poll_connected(Connection& connection){ std::optional clock; uint64_t last_frame = 0; do{ @@ -171,19 +254,47 @@ void VideoSource_MgbaLink::poll_connected(MgbaLink_Connection& connection){ } if (!clock){ clock.emplace(now, reply.frame); - }else if (clock->resync(now, reply.frame, MAX_CLOCK_DRIFT)){ - m_logger.log("MgbaLink: frame clock re-anchored at frame " + std::to_string(reply.frame) + "."); + } + FrameClock::Stamp stamp = clock->stamp(now, reply.frame, MAX_CLOCK_LAG); + if (stamp.reanchored_after_lag){ + m_logger.log(std::format("MgbaLink: frame clock re-anchored at frame {} (mGBA paused or slow).", reply.frame)); } last_frame = reply.frame; // Read the PNG before the next FRAME overwrites it. ImageRGB32 gba_frame(reply.path); - VideoSnapshot snapshot(compose_mgba_frame(gba_frame), clock->timestamp(reply.frame)); - std::lock_guard lg(m_lock); - m_last = std::move(snapshot); + if (gba_frame.width() != reply.width || gba_frame.height() != reply.height){ + throw ConnectionException(&m_logger, std::format( + "MgbaLink: FRAME {} said {}x{}, but {} is {}x{}.", + reply.frame, reply.width, reply.height, reply.path, gba_frame.width(), gba_frame.height() + )); + } + publish_frame(compose_mgba_frame(gba_frame), stamp.time); }while (!wait_or_stop(POLL_PERIOD)); } +void VideoSource_MgbaLink::publish_frame(ImageRGB32 frame, WallClock timestamp){ + VideoSnapshot snapshot(std::move(frame), timestamp); + { + std::lock_guard lg(m_lock); + m_last = snapshot; + } + + // For the FPS readout, stream history and the no-video watchdog. The + // QImage shares the snapshot's pixels and keeps them alive. + const ImageRGB32& pixels = *snapshot.frame; + QImage image( + (const uchar*)pixels.data(), + (int)pixels.width(), + (int)pixels.height(), + (qsizetype)pixels.bytes_per_row(), + QImage::Format_ARGB32, + [](void* owner){ delete static_cast*>(owner); }, + new std::shared_ptr(snapshot.frame) + ); + report_source_frame(std::make_shared(timestamp, QVideoFrame(image))); +} + class VideoWidget_MgbaLink : public QWidget{ @@ -206,6 +317,7 @@ class VideoWidget_MgbaLink : public QWidget{ } QPainter painter(this); painter.drawImage(QRect(0, 0, width(), height()), to_QImage_ref(*snapshot.frame)); + m_source.report_rendered_frame(current_time()); } private: diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h index 0aec4a9a60..9330ba0402 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h @@ -5,8 +5,8 @@ * Shows the picture of an mGBA instance running "bridge.lua" (MgbaLink v1). * Each new emulated frame is composed into a 1920x1080 frame at FRLG's * GAME_BOX with nearest-neighbour scaling, so detectors see console - * geometry, and is stamped with the time its frame number maps to. - * Developer mode only (fork lab tooling). + * geometry, and is stamped with the time its frame number maps to, never + * later than the time it was read. Developer mode only (fork lab tooling). * */ @@ -45,13 +45,16 @@ struct MgbaComposeGeometry{ // Draws "gba_frame" into a black canvas at the box, nearest-neighbour with // pixel-centre sampling (as Pillow's NEAREST, which the geometry gate used): // canvas pixel x takes source column floor((2 (x - box_x) + 1) w / (2 box_width)). +// Throws InternalProgramError if the box does not fit inside the canvas. ImageRGB32 compose_mgba_frame( const ImageViewRGB32& gba_frame, const MgbaComposeGeometry& geometry = MgbaComposeGeometry() ); -// "host:port" -> (host, port). nullopt if the port is missing or invalid. +// "host:port" -> (host, port). nullopt if the port is missing or invalid, +// or the host is not this machine ("127.0.0.1" or "localhost"): FRAME +// names a PNG on the bridge's disk, which this app opens as a local path. std::optional> parse_mgba_address(std::string_view address); @@ -88,7 +91,13 @@ class VideoSourceDescriptor_MgbaLink : public VideoSourceDescriptor{ -// Polls FRAME on its own connection and keeps the latest composed frame. +// Polls FRAME on its own connection and keeps the latest composed frame; +// drops it when the connection is lost, so a dead bridge shows no video. +// +// The bridge holds one client at a time, so this source takes its only +// slot. A controller for the same mGBA (slice 2: KEYS, GRANT) must not open +// a second connection: it has to share this one (Connection serializes +// commands from several threads). class VideoSource_MgbaLink : public VideoSource{ public: VideoSource_MgbaLink(Logger& logger, std::string address); @@ -113,8 +122,12 @@ class VideoSource_MgbaLink : public VideoSource{ virtual QWidget* make_display_QtWidget(QWidget* parent) override; private: + friend class VideoWidget_MgbaLink; + void poll_loop(); - void poll_connected(MgbaLink::MgbaLink_Connection& connection); + void run_connection(Logger& logger, const std::string& host, uint16_t port); + void poll_connected(MgbaLink::Connection& connection); + void publish_frame(ImageRGB32 frame, WallClock timestamp); bool wait_or_stop(std::chrono::milliseconds duration); private: @@ -126,6 +139,9 @@ class VideoSource_MgbaLink : public VideoSource{ ConditionVariable m_cv; bool m_stopping = false; VideoSnapshot m_last; + // The poller's open connection, if any; the destructor aborts it so it + // never waits out a reply timeout. + MgbaLink::Connection* m_connection = nullptr; AsyncTask m_poller; }; diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp index 61ea0e952b..f0ce9056f9 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp @@ -6,6 +6,7 @@ #include #include +#include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "VideoSource_MgbaLink.h" #include "VideoSource_MgbaLink_Tests.h" @@ -48,7 +49,7 @@ struct ComposeCase{ class Test_Compose : public UnitTest{ public: Test_Compose(ComposeCase test_case) - : UnitTest("CommonFramework::VideoSource_MgbaLink - compose - " + test_case.name) + : UnitTest(std::format("CommonFramework::VideoSource_MgbaLink - compose - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -86,20 +87,67 @@ struct AddressCase{ class Test_Address : public UnitTest{ public: Test_Address(AddressCase test_case) - : UnitTest("CommonFramework::VideoSource_MgbaLink - address - " + test_case.name) + : UnitTest(std::format("CommonFramework::VideoSource_MgbaLink - address - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - if (parse_mgba_address(m_case.input.address) != m_case.expectation.host_and_port){ - return std::format("unexpected result for <{}>", m_case.input.address); + std::optional> parsed = parse_mgba_address(m_case.input.address); + if (parsed != m_case.expectation.host_and_port){ + return std::format( + "parsed {}, expected {}", + describe(parsed), describe(m_case.expectation.host_and_port) + ); } return true; } private: + static std::string describe(const std::optional>& host_and_port){ + return host_and_port + ? std::format("<{}> port {}", host_and_port->first, host_and_port->second) + : std::string("nothing"); + } + AddressCase m_case; }; + +// compose_mgba_frame() refuses a box that does not fit the canvas. +struct GeometryInput{ + MgbaComposeGeometry geometry; +}; +struct GeometryExpectation{ + bool accepted = false; +}; +struct GeometryCase{ + std::string name; + GeometryInput input; + GeometryExpectation expectation; +}; + +class Test_Geometry : public UnitTest{ +public: + Test_Geometry(GeometryCase test_case) + : UnitTest(std::format("CommonFramework::VideoSource_MgbaLink - geometry - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + bool accepted = true; + try{ + compose_mgba_frame(make_gba_frame(), m_case.input.geometry); + }catch (InternalProgramError&){ + accepted = false; + } + if (accepted != m_case.expectation.accepted){ + return std::format("accepted={}, expected {}", accepted, m_case.expectation.accepted); + } + return true; + } + +private: + GeometryCase m_case; +}; + } // namespace @@ -132,10 +180,21 @@ void add_tests_VideoSource_MgbaLink(UnitTestDatabase& database){ {.name = "port 0", .input = {"127.0.0.1:0"}, .expectation = {std::nullopt}}, {.name = "port too large", .input = {"127.0.0.1:70000"}, .expectation = {std::nullopt}}, {.name = "no host", .input = {":47801"}, .expectation = {std::nullopt}}, + {.name = "remote host refused", .input = {"192.168.1.20:47801"}, .expectation = {std::nullopt}}, }; for (const AddressCase& test_case : ADDRESS_CASES){ database.add(test_case); } + + const GeometryCase GEOMETRY_CASES[] = { + {.name = "GAME_BOX at 1080p fits", .input = {MgbaComposeGeometry()}, .expectation = {true}}, + {.name = "box fills the canvas", .input = {{.canvas_width = 240, .canvas_height = 160, .box_x = 0, .box_y = 0, .box_width = 240, .box_height = 160}}, .expectation = {true}}, + {.name = "box past the right edge", .input = {{.box_x = 400}}, .expectation = {false}}, + {.name = "box past the bottom edge", .input = {{.box_y = 50}}, .expectation = {false}}, + }; + for (const GeometryCase& test_case : GEOMETRY_CASES){ + database.add(test_case); + } } diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp index 3be13fe948..d905f8daf6 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp @@ -8,6 +8,7 @@ #include "Common/Cpp/Json/JsonValue.h" #include "Common/Cpp/Json/JsonArray.h" #include "Common/Cpp/Json/JsonObject.h" +#include "CommonFramework/StaticGlobals.h" #include "Sources/VideoSource_Null.h" #include "Sources/VideoSource_StillImage.h" @@ -147,7 +148,9 @@ void VideoSourceOption::load_json(const JsonValue& json){ load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::StillImage); load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::Camera); - load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::MgbaLink); + if (STATIC_GLOBALS.DEVELOPER_MODE){ + load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::MgbaLink); + } auto iter = m_descriptor_cache.find(VIDEO_TYPE_STRINGS.get_enum(*type, VideoSourceType::None)); if (iter == m_descriptor_cache.end()){ diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp index 7a58f2eaa0..e0998ebdfc 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Color.h" #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Logging/AbstractLogger.h" @@ -14,7 +15,7 @@ namespace MgbaLink{ -MgbaLink_Connection::MgbaLink_Connection( +Connection::Connection( Logger& logger, ThreadPool& thread_pool, std::string host, @@ -29,42 +30,66 @@ MgbaLink_Connection::MgbaLink_Connection( { m_socket.add_listener(*this); } -MgbaLink_Connection::~MgbaLink_Connection(){ +Connection::~Connection(){ close(); m_socket.remove_listener(*this); } -HelloReply MgbaLink_Connection::open(){ +HelloReply Connection::open(){ + bool opened_before; + { + std::lock_guard lg(m_lock); + opened_before = m_opened; + m_opened = true; + } + if (opened_before){ + throw InternalProgramError( + &m_logger, PA_CURRENT_FUNCTION, + "MgbaLink: open() called twice on one connection; make a new one to reconnect." + ); + } connect_socket(); std::string reply = command("HELLO 1"); std::optional hello = parse_hello_reply(reply); if (!hello){ - fail("Unexpected HELLO reply: " + reply); + fail(std::format("Unexpected HELLO reply: {}", reply)); } - m_logger.log( - "MgbaLink: connected to " + m_host + ":" + std::to_string(m_port) - + ", game " + hello->game_code + ", " + hello->bridge_version + ", mode " + hello->mode + "." - ); + m_logger.log(std::format( + "MgbaLink: connected to {}:{}, game {}, {}, mode {}.", + m_host, m_port, hello->game_code, hello->bridge_version, hello->mode + )); return *hello; } -void MgbaLink_Connection::connect_socket(){ +void Connection::connect_socket(){ m_socket.connect(m_host, m_port); - std::unique_lock lg(m_lock); - if (!m_cv.wait_for(lg, m_timeout, [this]{ return m_connect_error.has_value(); })){ - lg.unlock(); - fail("Timed out connecting to " + m_host + ":" + std::to_string(m_port) + "."); + std::optional error; + { + std::unique_lock lg(m_lock); + bool finished = m_cv.wait_for(lg, m_timeout, [this]{ + return m_aborted || m_connect_error.has_value(); + }); + if (m_aborted){ + error = "aborted"; + }else if (!finished){ + error = "timed out"; + }else if (!m_connect_error->empty()){ + error = *m_connect_error; + } } - if (!m_connect_error->empty()){ - std::string error = *m_connect_error; - lg.unlock(); - fail("Unable to connect to " + m_host + ":" + std::to_string(m_port) + ": " + error); + if (error){ + fail(std::format("Unable to connect to {}:{}: {}", m_host, m_port, *error)); } } -void MgbaLink_Connection::close() noexcept{ - if (m_socket.state() == AbstractClientSocket::State::CONNECTED){ +void Connection::close() noexcept{ + bool aborted = true; + try{ + std::lock_guard lg(m_lock); + aborted = m_aborted; + }catch (...){} + if (!aborted && m_socket.state() == AbstractClientSocket::State::CONNECTED){ try{ command("BYE"); }catch (...){} @@ -72,8 +97,17 @@ void MgbaLink_Connection::close() noexcept{ m_socket.close(); } +void Connection::abort() noexcept{ + try{ + std::lock_guard lg(m_lock); + m_aborted = true; + m_cv.notify_all(); + }catch (...){} + m_socket.close(); +} + -std::string MgbaLink_Connection::command(std::string_view line){ +std::string Connection::command(std::string_view line){ std::lock_guard command_lg(m_command_lock); std::string_view name = line.substr(0, line.find(' ')); // A line the bridge sent unasked ("ERR busy" right after connecting) @@ -81,71 +115,77 @@ std::string MgbaLink_Connection::command(std::string_view line){ std::string data(line); data += '\n'; bool sent = m_socket.send(data.data(), data.size(), m_timeout) == data.size(); + bool nothing_to_read; { std::lock_guard lg(m_lock); - if (!sent && m_replies.empty()){ - fail("Unable to send " + std::string(name) + "."); - } + nothing_to_read = !sent && m_replies.empty() && !m_aborted; + } + if (nothing_to_read){ + fail(std::format("Unable to send {}.", name)); } return wait_for_reply(name); } -std::string MgbaLink_Connection::wait_for_reply(std::string_view command_name){ +std::string Connection::wait_for_reply(std::string_view command_name){ std::unique_lock lg(m_lock); bool ready = m_cv.wait_for(lg, m_timeout, [this]{ - return !m_replies.empty() || m_disconnect_error.has_value(); + return m_aborted || !m_replies.empty() || m_disconnect_error.has_value(); }); - if (m_replies.empty()){ - std::string reason = ready ? "connection lost: " + *m_disconnect_error : std::string("timed out"); - if (!ready){ + if (m_aborted || m_replies.empty()){ + std::string reason; + if (m_aborted){ + reason = "aborted"; + }else if (ready){ + reason = std::format("connection lost: {}", *m_disconnect_error); + }else{ + reason = "timed out"; // A late reply would pair with the next command: drop the link. - m_disconnect_error = "no reply to " + std::string(command_name); + m_disconnect_error = std::format("no reply to {}", command_name); } lg.unlock(); m_socket.close(); - fail("No reply to " + std::string(command_name) + " (" + reason + ")."); + fail(std::format("No reply to {} ({}).", command_name, reason)); } std::string reply = std::move(m_replies.front()); m_replies.pop_front(); lg.unlock(); if (reply.starts_with("ERR")){ - fail(std::string(command_name) + " -> " + reply); + fail(std::format("{} -> {}", command_name, reply)); } return reply; } -FrameReply MgbaLink_Connection::frame(){ +FrameReply Connection::frame(){ std::string reply = command("FRAME"); std::optional frame = parse_frame_reply(reply); if (!frame){ - fail("Unexpected FRAME reply: " + reply); + fail(std::format("Unexpected FRAME reply: {}", reply)); } return std::move(*frame); } -uint64_t MgbaLink_Connection::keys(uint64_t frame, uint16_t mask){ - std::string reply = command("KEYS " + std::to_string(frame) + " " + std::to_string(mask)); +uint64_t Connection::keys(uint64_t frame, uint16_t mask){ + std::string reply = command(std::format("KEYS {} {}", frame, mask)); std::optional late = parse_keys_reply(reply); if (!late){ - fail("Unexpected KEYS reply: " + reply); + fail(std::format("Unexpected KEYS reply: {}", reply)); } if (*late > 0){ m_logger.log( - "MgbaLink: KEYS for frame " + std::to_string(frame) + " was " - + std::to_string(*late) + " frame(s) late.", + std::format("MgbaLink: KEYS for frame {} was {} frame(s) late.", frame, *late), COLOR_ORANGE ); } return *late; } -void MgbaLink_Connection::fail(std::string message){ - throw ConnectionException(&m_logger, "MgbaLink: " + std::move(message)); +void Connection::fail(std::string message){ + throw ConnectionException(&m_logger, std::format("MgbaLink: {}", message)); } -void MgbaLink_Connection::on_connect_finished(const std::string& error_message){ +void Connection::on_connect_finished(const std::string& error_message){ std::lock_guard lg(m_lock); m_connect_error = error_message; m_disconnect_error.reset(); @@ -153,7 +193,7 @@ void MgbaLink_Connection::on_connect_finished(const std::string& error_message){ m_cv.notify_all(); } -void MgbaLink_Connection::on_receive_data(const void* data, size_t bytes){ +void Connection::on_receive_data(const void* data, size_t bytes){ bool overflowed; { std::lock_guard lg(m_lock); @@ -162,7 +202,7 @@ void MgbaLink_Connection::on_receive_data(const void* data, size_t bytes){ } overflowed = m_splitter.overflowed(); if (overflowed){ - m_disconnect_error = "reply line longer than " + std::to_string(LineSplitter::MAX_LINE_BYTES) + " bytes"; + m_disconnect_error = std::format("reply line longer than {} bytes", LineSplitter::MAX_LINE_BYTES); } m_cv.notify_all(); } @@ -171,7 +211,7 @@ void MgbaLink_Connection::on_receive_data(const void* data, size_t bytes){ } } -void MgbaLink_Connection::on_disconnect(const std::string& error_message){ +void Connection::on_disconnect(const std::string& error_message){ std::lock_guard lg(m_lock); if (!m_disconnect_error){ m_disconnect_error = error_message.empty() ? "closed by the bridge" : error_message; diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h index ca74611c98..1ea367da7d 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h @@ -6,7 +6,9 @@ * lab/mgba/PROTOCOL.md on the fork's agents branch). Every command is one * line and gets exactly one reply line; commands from several threads are * serialized. The bridge holds one client at a time, so the destructor - * always says BYE, which also releases every key and grant it holds. + * says BYE, which also releases every key and grant it holds (unless the + * connection was aborted: then it only closes the socket, which the bridge + * treats the same way). * * Qt-free. The socket's receive thread comes from the thread pool passed in. * @@ -33,25 +35,32 @@ namespace MgbaLink{ // A synchronous MgbaLink v1 client: open(), then one command at a time. -class MgbaLink_Connection final : private AbstractClientSocket::Listener{ +class Connection final : private AbstractClientSocket::Listener{ public: - MgbaLink_Connection( + Connection( Logger& logger, ThreadPool& thread_pool, std::string host, uint16_t port, std::chrono::milliseconds timeout = std::chrono::seconds(5) ); - ~MgbaLink_Connection(); + ~Connection(); // Connects and says "HELLO 1". Throws ConnectionException if the bridge - // cannot be reached, refuses the client ("ERR busy") or replies with - // anything but a v1 HELLO. + // cannot be reached, refuses the client ("ERR busy"), replies with + // anything but a v1 HELLO, or the connection is aborted. Call it once + // per object (a second call throws InternalProgramError); to reconnect, + // make a new Connection. HelloReply open(); // Sends "BYE" (best effort) and closes. Safe to call more than once. void close() noexcept; + // Ends the connection without waiting for the bridge: open() or a + // command in progress throws ConnectionException at once, as does every + // later one, and close() skips BYE. Safe to call from any thread. + void abort() noexcept; + // Sends one command line and returns its reply line. Throws // ConnectionException on a timeout, a lost connection or an "ERR" reply. std::string command(std::string_view line); @@ -86,6 +95,8 @@ class MgbaLink_Connection final : private AbstractClientSocket::Listener{ ConditionVariable m_cv; std::optional m_connect_error; std::optional m_disconnect_error; + bool m_opened = false; + bool m_aborted = false; LineSplitter m_splitter; std::deque m_replies; diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp index c521a254ca..44fe179482 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp @@ -141,14 +141,21 @@ WallClock FrameClock::timestamp(uint64_t frame) const{ } -bool FrameClock::resync(WallClock now, uint64_t frame, WallDuration max_drift){ - WallDuration drift = timestamp(frame) - now; - if (drift <= max_drift && -drift <= max_drift){ - return false; +FrameClock::Stamp FrameClock::stamp(WallClock now, uint64_t frame, WallDuration max_lag){ + Stamp result; + WallClock time = timestamp(frame); + if (time > now || now - time > max_lag){ + result.reanchored_after_lag = time < now; + m_t0 = now; + m_f0 = frame; + time = now; } - m_t0 = now; - m_f0 = frame; - return true; + if (m_last_stamp && time < *m_last_stamp){ + time = *m_last_stamp; + } + m_last_stamp = time; + result.time = time; + return result; } } // namespace MgbaLink diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h index dbbe5a03d4..9a1e9aec49 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h @@ -32,9 +32,9 @@ struct HelloReply{ // The fields of a "FRAME" reply (0.10.5: the frame is a PNG on disk). struct FrameReply{ - uint64_t frame; - size_t width; - size_t height; + uint64_t frame = 0; + size_t width = 0; + size_t height = 0; std::string path; // PNG written by the bridge; may contain spaces. }; @@ -92,6 +92,13 @@ class FrameClock{ // GBA frame rate: 16.78 MHz / 280,896 cycles per frame. static constexpr double GBA_FRAMES_PER_SECOND = 59.7275; + // The time a frame is stamped with, and whether the clock had to catch + // up with a paused or slow mGBA to get it. + struct Stamp{ + WallClock time{}; + bool reanchored_after_lag = false; + }; + FrameClock(WallClock t0, uint64_t f0) : m_t0(t0) , m_f0(f0) @@ -99,11 +106,14 @@ class FrameClock{ WallClock timestamp(uint64_t frame) const; - // While mGBA runs free (no GRANT pacing yet), its clock drifts against - // the app's and stops while it is paused. If "frame" maps more than - // "max_drift" away from "now", re-anchors at (now, frame) and returns - // true. - bool resync(WallClock now, uint64_t frame, WallDuration max_drift); + // The stamp for "frame", read from the bridge at "now". While mGBA runs + // free (no GRANT pacing yet), its clock drifts against the app's and + // stops while it is paused, so the clock re-anchors at (now, frame) + // when "frame" maps after "now" (mGBA running ahead: a frame cannot be + // stamped later than it was read) or more than "max_lag" before it + // (mGBA paused or slow). Stamps never go backwards: a frame that maps + // before the previous stamp (a LOAD) gets the previous stamp. + Stamp stamp(WallClock now, uint64_t frame, WallDuration max_lag); WallClock anchor_time() const{ return m_t0; @@ -115,10 +125,10 @@ class FrameClock{ private: WallClock m_t0; uint64_t m_f0; + std::optional m_last_stamp; }; - } // namespace MgbaLink } // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp index a1e5913953..4163405e9a 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp @@ -5,11 +5,13 @@ */ #include +#include #include #include #include #include #include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Concurrency/AsyncTask.h" #include "Common/Cpp/Concurrency/ConditionVariable.h" #include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/TestRunners/UnitTest.h" @@ -53,7 +55,7 @@ struct LineFramingCase{ class Test_LineFraming : public UnitTest{ public: Test_LineFraming(LineFramingCase test_case) - : UnitTest("Controllers::MgbaLink - line framing - " + test_case.name) + : UnitTest(std::format("Controllers::MgbaLink - line framing - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -128,7 +130,7 @@ std::optional describe_reply(const ReplyInput& input){ class Test_Reply : public UnitTest{ public: Test_Reply(ReplyCase test_case) - : UnitTest("Controllers::MgbaLink - reply - " + test_case.name) + : UnitTest(std::format("Controllers::MgbaLink - reply - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -163,7 +165,7 @@ const WallClock CLOCK_T0 = WallClock() + std::chrono::hours(1000); class Test_FrameClock : public UnitTest{ public: Test_FrameClock(FrameClockCase test_case) - : UnitTest("Controllers::MgbaLink - frame clock - " + test_case.name) + : UnitTest(std::format("Controllers::MgbaLink - frame clock - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -181,51 +183,81 @@ class Test_FrameClock : public UnitTest{ -// Re-anchoring when frame 1060 (1004.6 ms after the anchor) is read late. -struct ResyncInput{ - std::chrono::milliseconds read_after_anchor; +// Stamps for a series of reads, the clock anchored at (t0, frame 1000). +// Frame 1060 maps to 1004.6 ms after the anchor. +struct ClockRead{ + uint64_t frame = 0; + std::chrono::milliseconds read_after_anchor{}; }; -struct ResyncExpectation{ - bool resynced; - std::chrono::milliseconds frame_1060_after_anchor; +ClockRead frame_read_at(uint64_t frame, std::chrono::milliseconds read_after_anchor){ + return ClockRead{frame, read_after_anchor}; +} +struct StampInput{ + std::vector reads; +}; +struct StampExpectation{ + std::vector stamps_after_anchor; + size_t lag_reanchors = 0; }; -struct ResyncCase{ +struct StampCase{ std::string name; - ResyncInput input; - ResyncExpectation expectation; + StampInput input; + StampExpectation expectation; }; -class Test_Resync : public UnitTest{ +std::string join_milliseconds(const std::vector& values){ + std::string text; + for (std::chrono::milliseconds value : values){ + text += text.empty() ? "" : ", "; + text += std::to_string(value.count()); + } + return text; +} + +class Test_Stamp : public UnitTest{ public: - Test_Resync(ResyncCase test_case) - : UnitTest("Controllers::MgbaLink - clock resync - " + test_case.name) + Test_Stamp(StampCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - frame stamps - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ FrameClock clock(CLOCK_T0, 1000); - bool resynced = clock.resync(CLOCK_T0 + m_case.input.read_after_anchor, 1060, std::chrono::milliseconds(500)); - WallDuration offset = clock.timestamp(1060) - CLOCK_T0; - if (resynced != m_case.expectation.resynced || - std::chrono::duration_cast(offset) != m_case.expectation.frame_1060_after_anchor - ){ - return std::format("resynced={}", resynced); + std::vector stamps; + size_t lag_reanchors = 0; + for (const ClockRead& read : m_case.input.reads){ + FrameClock::Stamp stamp = clock.stamp(CLOCK_T0 + read.read_after_anchor, read.frame, std::chrono::milliseconds(500)); + stamps.emplace_back(std::chrono::duration_cast(stamp.time - CLOCK_T0)); + lag_reanchors += stamp.reanchored_after_lag ? 1 : 0; + } + if (stamps != m_case.expectation.stamps_after_anchor || lag_reanchors != m_case.expectation.lag_reanchors){ + return std::format( + "stamps [{}] ms with {} lag re-anchor(s), expected [{}] ms with {}", + join_milliseconds(stamps), lag_reanchors, + join_milliseconds(m_case.expectation.stamps_after_anchor), m_case.expectation.lag_reanchors + ); } return true; } private: - ResyncCase m_case; + StampCase m_case; }; -// A bridge stand-in: answers each command line from a script, or refuses -// the client with "ERR busy" as a bridge that already has one. +// How the fake bridge treats its client. +enum class BridgeMode{ + ANSWERS, // Answers each command line from a script. + BUSY, // Refuses the client with "ERR busy", as a bridge that has one. + HANGS, // Answers HELLO, then never answers again. +}; + +// A bridge stand-in. class FakeBridge : public AbstractServerSocket::Listener{ public: - FakeBridge(ThreadPool& thread_pool, bool busy) + FakeBridge(ThreadPool& thread_pool, BridgeMode mode) : m_server(thread_pool) - , m_busy(busy) + , m_mode(mode) { m_server.add_listener(*this); } @@ -254,7 +286,7 @@ class FakeBridge : public AbstractServerSocket::Listener{ m_cv.notify_all(); } virtual void on_connect() override{ - if (m_busy){ + if (m_mode == BridgeMode::BUSY){ // Closed once the client's first line is read (no reset that // could drop this line before the client reads it). send("ERR busy\n"); @@ -270,7 +302,7 @@ class FakeBridge : public AbstractServerSocket::Listener{ private: FakeServerSocket m_server; - const bool m_busy; + const BridgeMode m_mode; LineSplitter m_splitter; Mutex m_lock; ConditionVariable m_cv; @@ -297,7 +329,7 @@ std::optional FakeBridge::start(){ } void FakeBridge::on_receive_data(const void* data, size_t bytes){ - if (m_busy){ + if (m_mode == BridgeMode::BUSY){ // Record what the client sent, then refuse it. std::lock_guard lg(m_lock); m_commands.emplace_back("(refused)"); @@ -312,6 +344,9 @@ void FakeBridge::on_receive_data(const void* data, size_t bytes){ m_commands.emplace_back(line); m_cv.notify_all(); } + if (m_mode == BridgeMode::HANGS && line != "HELLO 1"){ + continue; + } std::string reply = reply_for(line); // Two writes, so the client has to reassemble the line. send(std::string_view(reply).substr(0, 3)); @@ -339,15 +374,18 @@ std::string FakeBridge::reply_for(std::string_view line){ if (line == "BYE"){ return "OK BYE\n"; } - return "ERR unknown " + std::string(line.substr(0, line.find(' '))) + "\n"; + return std::format("ERR unknown {}\n", line.substr(0, line.find(' '))); } -// A whole session against the fake bridge: open, FRAME, KEYS late and on -// time, an ERR reply, then close (BYE). +// A whole session against the fake bridge: open (twice, if asked), FRAME, +// KEYS late and on time, an ERR reply, then close (BYE). struct SessionInput{ - bool bridge_busy; + BridgeMode bridge_mode = BridgeMode::ANSWERS; + bool open_twice = false; + // Abort from another thread once the bridge has seen FRAME. + bool abort_after_frame = false; }; struct SessionExpectation{ std::vector results; @@ -359,45 +397,78 @@ struct SessionCase{ SessionExpectation expectation; }; -std::vector run_session(Logger& logger, ThreadPool& thread_pool, uint16_t port){ +std::string join_lines(const std::vector& lines){ + std::string text; + for (const std::string& line : lines){ + text += text.empty() ? "<" : ", <"; + text += line; + text += ">"; + } + return text; +} + +void run_commands(Connection& connection, std::vector& results){ + FrameReply frame = connection.frame(); + results.emplace_back(std::format("frame {} {}", frame.frame, frame.path)); + results.emplace_back(std::format("late {}", connection.keys(100, 1))); + results.emplace_back(std::format("late {}", connection.keys(130, 1))); + connection.command("FLY"); + results.emplace_back("FLY accepted"); +} + +std::vector run_session( + Logger& logger, ThreadPool& thread_pool, FakeBridge& bridge, uint16_t port, const SessionInput& input +){ std::vector results; - MgbaLink_Connection connection(logger, thread_pool, "127.0.0.1", port); + Connection connection(logger, thread_pool, "127.0.0.1", port); + AsyncTask aborter; try{ HelloReply hello = connection.open(); results.emplace_back(std::format("hello {} {}", hello.game_code, hello.mode)); - FrameReply frame = connection.frame(); - results.emplace_back(std::format("frame {} {}", frame.frame, frame.path)); - results.emplace_back(std::format("late {}", connection.keys(100, 1))); - results.emplace_back(std::format("late {}", connection.keys(130, 1))); - connection.command("FLY"); - results.emplace_back("FLY accepted"); + if (input.open_twice){ + try{ + connection.open(); + results.emplace_back("second open accepted"); + }catch (InternalProgramError&){ + results.emplace_back("second open refused"); + } + } + if (input.abort_after_frame){ + aborter = thread_pool.dispatch_now_blocking([&]{ + bridge.wait_for_commands(2); + connection.abort(); + }); + } + run_commands(connection, results); }catch (ConnectionException& e){ bool busy = e.message().find("ERR busy") != std::string::npos; - results.emplace_back(busy ? "failed: ERR busy" : "failed: " + e.message()); + results.emplace_back(busy ? "failed: ERR busy" : std::format("failed: {}", e.message())); } + aborter.wait_and_ignore_exceptions(); return results; } class Test_Session : public UnitTest{ public: Test_Session(SessionCase test_case) - : UnitTest("Controllers::MgbaLink - fake bridge - " + test_case.name) + : UnitTest(std::format("Controllers::MgbaLink - fake bridge - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ ThreadPool& thread_pool = GlobalThreadPools::unlimited_normal(); - FakeBridge bridge(thread_pool, m_case.input.bridge_busy); + FakeBridge bridge(thread_pool, m_case.input.bridge_mode); std::optional port = bridge.start(); if (!port){ return UnitTestResult(UnitTestResult::SKIPPED, "No free port for the fake bridge."); } - std::vector results = run_session(logger, thread_pool, *port); + std::vector results = run_session(logger, thread_pool, bridge, *port, m_case.input); if (results != m_case.expectation.results){ - return std::format("result {}: <{}>", results.size(), results.empty() ? "" : results.back()); + return std::format("results {}, expected {}", join_lines(results), join_lines(m_case.expectation.results)); } bridge.wait_for_commands(m_case.expectation.bridge_saw.size()); - if (bridge.commands() != m_case.expectation.bridge_saw){ - return std::format("the bridge saw {} command(s)", bridge.commands().size()); + std::vector saw = bridge.commands(); + if (saw != m_case.expectation.bridge_saw){ + return std::format("the bridge saw {}, expected {}", join_lines(saw), join_lines(m_case.expectation.bridge_saw)); } return true; } @@ -446,7 +517,7 @@ void add_framing_and_reply_tests(UnitTestDatabase& database){ } } -void add_clock_and_session_tests(UnitTestDatabase& database){ +void add_clock_tests(UnitTestDatabase& database){ // One frame = 1000 / 59.7275 ms = 16742.7 us. const FrameClockCase FRAME_CLOCK_CASES[] = { {.name = "anchor frame", .input = {1000}, .expectation = {std::chrono::microseconds(0)}}, @@ -458,18 +529,32 @@ void add_clock_and_session_tests(UnitTestDatabase& database){ database.add(test_case); } - const ResyncCase RESYNC_CASES[] = { - {.name = "small drift keeps the anchor", .input = {std::chrono::milliseconds(1100)}, .expectation = {false, std::chrono::milliseconds(1004)}}, - {.name = "mGBA paused re-anchors", .input = {std::chrono::milliseconds(10000)}, .expectation = {true, std::chrono::milliseconds(10000)}}, + using std::chrono::milliseconds; + const StampCase STAMP_CASES[] = { + {.name = "small lag keeps the anchor", .input = {{frame_read_at(1060, milliseconds(1100))}}, .expectation = {{milliseconds(1004)}, 0}}, + {.name = "mGBA paused re-anchors at the read", .input = {{frame_read_at(1060, milliseconds(10000))}}, .expectation = {{milliseconds(10000)}, 1}}, + {.name = "mGBA running ahead is stamped at the read", .input = {{frame_read_at(1060, milliseconds(900))}}, .expectation = {{milliseconds(900)}, 0}}, + { + .name = "mGBA running ahead stays at or before each read", + .input = {{frame_read_at(1060, milliseconds(900)), frame_read_at(1120, milliseconds(1900))}}, + .expectation = {{milliseconds(900), milliseconds(1900)}, 0}, + }, + { + .name = "frame number going back keeps stamps monotonic", + .input = {{frame_read_at(1060, milliseconds(1010)), frame_read_at(1059, milliseconds(1012))}}, + .expectation = {{milliseconds(1004), milliseconds(1004)}, 0}, + }, }; - for (const ResyncCase& test_case : RESYNC_CASES){ - database.add(test_case); + for (const StampCase& test_case : STAMP_CASES){ + database.add(test_case); } +} +void add_session_tests(UnitTestDatabase& database){ const SessionCase SESSION_CASES[] = { { .name = "session", - .input = {false}, + .input = {}, .expectation = { {"hello BPRE paced", "frame 120 /tmp/mgba bridge.png", "late 20", "late 0", "failed: MgbaLink: FLY -> ERR unknown FLY"}, {"HELLO 1", "FRAME", "KEYS 100 1", "KEYS 130 1", "FLY", "BYE"}, @@ -477,9 +562,27 @@ void add_clock_and_session_tests(UnitTestDatabase& database){ }, { .name = "busy", - .input = {true}, + .input = {.bridge_mode = BridgeMode::BUSY}, .expectation = {{"failed: ERR busy"}, {"(refused)"}}, }, + { + .name = "second open refused", + .input = {.open_twice = true}, + .expectation = { + {"hello BPRE paced", "second open refused", "frame 120 /tmp/mgba bridge.png", "late 20", "late 0", "failed: MgbaLink: FLY -> ERR unknown FLY"}, + {"HELLO 1", "FRAME", "KEYS 100 1", "KEYS 130 1", "FLY", "BYE"}, + }, + }, + { + // Without abort() FRAME would wait out the 5 s timeout, then + // report "timed out"; aborted, it fails at once and skips BYE. + .name = "abort while waiting for a reply", + .input = {.bridge_mode = BridgeMode::HANGS, .abort_after_frame = true}, + .expectation = { + {"hello BPRE paced", "failed: MgbaLink: No reply to FRAME (aborted)."}, + {"HELLO 1", "FRAME"}, + }, + }, }; for (const SessionCase& test_case : SESSION_CASES){ database.add(test_case); @@ -491,7 +594,8 @@ void add_clock_and_session_tests(UnitTestDatabase& database){ void add_tests(UnitTestDatabase& database){ add_framing_and_reply_tests(database); - add_clock_and_session_tests(database); + add_clock_tests(database); + add_session_tests(database); } From 1d3d6a9c9c899cabd4de7d06b439cc3ffe18c442 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 16:39:47 -0800 Subject: [PATCH 5/5] MgbaLink: opt out of the no-video watchdog again; log a bad frame size once A paused mGBA or a missing bridge reports no new frames, so the session watchdog rebuilt the source every 5 s. The source reconnects itself every 2 s, so it passes allow_watchdog_reset = false. The FRAME size mismatch now goes through the repeat-suppressing logger, and the header says frames are stamped from their frame number only while mGBA is not running ahead. --- .../VideoPipeline/Sources/VideoSource_MgbaLink.cpp | 12 ++++++------ .../VideoPipeline/Sources/VideoSource_MgbaLink.h | 9 ++++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp index c0f3066d3e..6bddd0252b 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp @@ -157,7 +157,7 @@ std::unique_ptr VideoSourceDescriptor_MgbaLink::make_VideoSource( VideoSource_MgbaLink::VideoSource_MgbaLink(Logger& logger, std::string address) - : VideoSource(logger, true) + : VideoSource(logger, false) , m_logger(logger) , m_address(std::move(address)) { @@ -231,7 +231,7 @@ void VideoSource_MgbaLink::run_connection(Logger& logger, const std::string& hos } try{ connection.open(); - poll_connected(connection); + poll_connected(logger, connection); }catch (...){ std::lock_guard lg(m_lock); m_connection = nullptr; @@ -243,7 +243,7 @@ void VideoSource_MgbaLink::run_connection(Logger& logger, const std::string& hos m_last = VideoSnapshot(); } -void VideoSource_MgbaLink::poll_connected(Connection& connection){ +void VideoSource_MgbaLink::poll_connected(Logger& logger, Connection& connection){ std::optional clock; uint64_t last_frame = 0; do{ @@ -264,7 +264,7 @@ void VideoSource_MgbaLink::poll_connected(Connection& connection){ // Read the PNG before the next FRAME overwrites it. ImageRGB32 gba_frame(reply.path); if (gba_frame.width() != reply.width || gba_frame.height() != reply.height){ - throw ConnectionException(&m_logger, std::format( + throw ConnectionException(&logger, std::format( "MgbaLink: FRAME {} said {}x{}, but {} is {}x{}.", reply.frame, reply.width, reply.height, reply.path, gba_frame.width(), gba_frame.height() )); @@ -280,8 +280,8 @@ void VideoSource_MgbaLink::publish_frame(ImageRGB32 frame, WallClock timestamp){ m_last = snapshot; } - // For the FPS readout, stream history and the no-video watchdog. The - // QImage shares the snapshot's pixels and keeps them alive. + // For the FPS readout and stream history. The QImage shares the + // snapshot's pixels and keeps them alive. const ImageRGB32& pixels = *snapshot.frame; QImage image( (const uchar*)pixels.data(), diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h index 9330ba0402..a167ec857c 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h @@ -5,8 +5,11 @@ * Shows the picture of an mGBA instance running "bridge.lua" (MgbaLink v1). * Each new emulated frame is composed into a 1920x1080 frame at FRLG's * GAME_BOX with nearest-neighbour scaling, so detectors see console - * geometry, and is stamped with the time its frame number maps to, never - * later than the time it was read. Developer mode only (fork lab tooling). + * geometry. While mGBA runs no faster than 59.7275 fps, a frame is stamped + * with the time its frame number maps to; when it runs ahead (e.g. synced + * to a 60 Hz display), with the time it was read. Never later than the + * read time. The source reconnects itself, so it opts out of the session + * no-video watchdog. Developer mode only (fork lab tooling). * */ @@ -126,7 +129,7 @@ class VideoSource_MgbaLink : public VideoSource{ void poll_loop(); void run_connection(Logger& logger, const std::string& host, uint16_t port); - void poll_connected(MgbaLink::Connection& connection); + void poll_connected(Logger& logger, MgbaLink::Connection& connection); void publish_frame(ImageRGB32 frame, WallClock timestamp); bool wait_or_stop(std::chrono::milliseconds duration);