From e6887ed9724f4ca99cc08f1761dd751efc37cb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Tue, 29 Sep 2026 02:14:07 +0200 Subject: [PATCH] test/libmpv_common: add temp_path() and wait_event() --- test/libmpv_common.h | 31 +++++++++++++++++++++++++-- test/libmpv_encode.c | 21 +------------------ test/libmpv_test_aes_ctr.c | 43 ++++++-------------------------------- 3 files changed, 36 insertions(+), 59 deletions(-) diff --git a/test/libmpv_common.h b/test/libmpv_common.h index a692569fdb..8bb9604867 100644 --- a/test/libmpv_common.h +++ b/test/libmpv_common.h @@ -22,6 +22,12 @@ #include #include +#ifdef _WIN32 +#include +#else +#include +#endif + #include #include "osdep/compiler.h" @@ -58,7 +64,23 @@ static inline void exit_cleanup(void) ctx = NULL; } -static inline mpv_event *wrap_wait_event(void) +// Return the path for a temporary file. +static inline const char *temp_path(void) +{ + static char path[] = "./testout.XXXXXX"; +#ifdef _WIN32 + if (!_mktemp(path) || !*path) + fail("tmpfile failed\n"); +#else + int fd = mkstemp(path); + if (fd == -1) + fail("tmpfile failed\n"); + close(fd); +#endif + return path; +} + +static inline mpv_event *wait_event(bool fail_on_error) { while (1) { mpv_event *ev = mpv_wait_event(ctx, 1); @@ -68,7 +90,7 @@ static inline mpv_event *wrap_wait_event(void) if (ev->event_id == MPV_EVENT_LOG_MESSAGE) { mpv_event_log_message *msg = (mpv_event_log_message*)ev->data; printf("[%s:%s] %s", msg->prefix, msg->level, msg->text); - if (msg->log_level <= MPV_LOG_LEVEL_ERROR) + if (fail_on_error && msg->log_level <= MPV_LOG_LEVEL_ERROR) fail("error was logged"); } else { return ev; @@ -76,6 +98,11 @@ static inline mpv_event *wrap_wait_event(void) } } +static inline mpv_event *wrap_wait_event(void) +{ + return wait_event(true); +} + static inline void command_impl(const char *file, int line, const char *cmd[]) { int ret = mpv_command(ctx, cmd); diff --git a/test/libmpv_encode.c b/test/libmpv_encode.c index 861b9d2537..3777727a14 100644 --- a/test/libmpv_encode.c +++ b/test/libmpv_encode.c @@ -21,13 +21,6 @@ #include #include -#ifdef _WIN32 -#include -#include -#else -#include -#endif - #include #include "libmpv_common.h" @@ -80,19 +73,7 @@ int main(int argc, char *argv[]) atexit(cleanup); - static char path[] = "./testout.XXXXXX"; - -#ifdef _WIN32 - out_path = _mktemp(path); - if (!out_path || !*out_path) - fail("tmpfile failed\n"); -#else - int fd = mkstemp(path); - if (fd == -1) - fail("tmpfile failed\n"); - out_path = path; -#endif - + out_path = temp_path(); set_property_string("o", out_path); set_property_string("of", "matroska"); set_property_string("end", "1.5"); diff --git a/test/libmpv_test_aes_ctr.c b/test/libmpv_test_aes_ctr.c index 3cbc6c59df..36fcc73308 100644 --- a/test/libmpv_test_aes_ctr.c +++ b/test/libmpv_test_aes_ctr.c @@ -15,12 +15,6 @@ * License along with mpv. If not, see . */ -#ifdef _WIN32 -#include -#else -#include -#endif - #include #include @@ -222,25 +216,13 @@ static void compare(const char *name, const uint8_t *data, int64_t data_len, num_checks++; } -// Like wrap_wait_event(), but for streams that log errors as they fail. -static mpv_event *wait_event_with_errors(void) -{ - while (1) { - mpv_event *event = mpv_wait_event(ctx, -1); - if (event->event_id != MPV_EVENT_LOG_MESSAGE) - return event; - mpv_event_log_message *msg = event->data; - printf("[%s:%s] %s", msg->prefix, msg->level, msg->text); - } -} - // Load the URL and return the reason it ended with. -static mpv_end_file_reason load(const char *url, mpv_event *(*wait)(void)) +static mpv_end_file_reason load(const char *url, bool fail_on_error) { const char *cmd[] = {"loadfile", url, NULL}; command(cmd); while (1) { - mpv_event *event = wait(); + mpv_event *event = wait_event(fail_on_error); if (event->event_id == MPV_EVENT_END_FILE) return ((mpv_event_end_file *)event->data)->reason; } @@ -249,7 +231,7 @@ static mpv_end_file_reason load(const char *url, mpv_event *(*wait)(void)) // Dump the stream and compare it with the expected data. static void check_stream(const char *url, const uint8_t *expected, int64_t len) { - if (load(url, wrap_wait_event) != MPV_END_FILE_REASON_EOF) + if (load(url, true) != MPV_END_FILE_REASON_EOF) fail("%s: dumping failed\n", url); // One byte more than expected shows a stream that is too long. @@ -266,7 +248,7 @@ static void check_stream(const char *url, const uint8_t *expected, int64_t len) // Require that the stream fails to open. static void check_failure(const char *url) { - if (load(url, wait_event_with_errors) != MPV_END_FILE_REASON_ERROR) + if (load(url, false) != MPV_END_FILE_REASON_ERROR) fail("%s: did not fail\n", url); num_checks++; } @@ -388,20 +370,7 @@ int main(int argc, char *argv[]) atexit(cleanup); - static char path[] = "./testout.XXXXXX"; - -#ifdef _WIN32 - out_path = _mktemp(path); - if (!out_path || !*out_path) - fail("tmpfile failed\n"); -#else - int fd = mkstemp(path); - if (fd == -1) - fail("tmpfile failed\n"); - close(fd); - out_path = path; -#endif - + out_path = temp_path(); set_property_string("stream-dump", out_path); initialize(); @@ -422,7 +391,7 @@ int main(int argc, char *argv[]) printf("%d checks\n", num_checks); command_string("quit"); - while (wait_event_with_errors()->event_id != MPV_EVENT_SHUTDOWN) {} + while (wait_event(false)->event_id != MPV_EVENT_SHUTDOWN) {} return 0; }