diff --git a/libobs/build/libobs.core-output+.h.c b/libobs/build/libobs.core-output+.h.c index c90a3691c4..6589ee65e5 100644 --- a/libobs/build/libobs.core-output+.h.c +++ b/libobs/build/libobs.core-output+.h.c @@ -37,6 +37,7 @@ #include "libobs/obs-encoder-settings-fields.c.h" #include "libobs/obs-encoder-state-queries-fields.c.h" #include "libobs/obs-encoder-type-info-fields.c.h" +#include "libobs/obs-encoder-video-tex-active.c.h" #include "libobs/obs-encoder.c.h" #include "libobs/obs-output-active-fields.c.h" #include "libobs/obs-output-can-pause-fields.c.h" diff --git a/libobs/obs-encoder-video-tex-active.c.h b/libobs/obs-encoder-video-tex-active.c.h new file mode 100644 index 0000000000..ca8b01c852 --- /dev/null +++ b/libobs/obs-encoder-video-tex-active.c.h @@ -0,0 +1,17 @@ +#include "libobs/build/libobs.pch.h" + +bool obs_encoder_video_tex_active(const obs_encoder_t *encoder, enum video_format format) +{ + /* No mix owns the video of an encoder with no video set, or of one + * whose canvas mix is gone (#2267): no texture input then. */ + const struct obs_core_video_mix *mix = get_mix_for_video(encoder->media); + if (!mix) + return false; + + if (format == VIDEO_FORMAT_NV12) + return mix->using_nv12_tex; + if (format == VIDEO_FORMAT_P010) + return mix->using_p010_tex; + + return false; +} diff --git a/libobs/obs-encoder.c.h b/libobs/obs-encoder.c.h index 7156b8bfb6..a66e984d60 100644 --- a/libobs/obs-encoder.c.h +++ b/libobs/obs-encoder.c.h @@ -1645,22 +1645,6 @@ void obs_encoder_enum_roi(obs_encoder_t *encoder, void (*enum_proc)(void *, stru pthread_mutex_unlock(&encoder->roi_mutex); } -bool obs_encoder_video_tex_active(const obs_encoder_t *encoder, enum video_format format) -{ - /* No mix owns the video of an encoder with no video set, or of one - * whose canvas mix is gone (#2267): no texture input then. */ - const struct obs_core_video_mix *mix = get_mix_for_video(encoder->media); - if (!mix) - return false; - - if (format == VIDEO_FORMAT_NV12) - return mix->using_nv12_tex; - if (format == VIDEO_FORMAT_P010) - return mix->using_p010_tex; - - return false; -} - /* File-local macros: keep them out of the next source of a unity build. */ #undef get_weak diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 2028cfc8f8..a1746ee807 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -4037,6 +4037,25 @@ "libobs/data" ] }, + "test_encoder_video_tex_active": { + "sources": [ + "test/cmocka/test_encoder_video_tex_active.c", + "libobs/obs-encoder-video-tex-active.c.h" + ], + "definitions": [ + "get_mix_for_video=tex_active_recording_mix_lookup" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ] + }, "test_encoding_lag_rescale": { "sources": [ "test/cmocka/test_encoding_lag_rescale.c" @@ -22954,6 +22973,12 @@ "working_directory": "${runtime}", "timeout_seconds": 120 }, + "test_encoder_video_tex_active": { + "target": "test_encoder_video_tex_active", + "command": [ + "${target}" + ] + }, "test_encoding_lag_rescale": { "target": "test_encoding_lag_rescale", "command": [ diff --git a/test/cmocka/test_encoder_video_tex_active.c b/test/cmocka/test_encoder_video_tex_active.c new file mode 100644 index 0000000000..834955ed69 --- /dev/null +++ b/test/cmocka/test_encoder_video_tex_active.c @@ -0,0 +1,113 @@ +/* C-source characterization of obs_encoder_video_tex_active (#5212). + * Compile the current production body with a recording mix lookup and real + * private layouts. This does not characterize GPU support or replace the + * unchanged shared-library checks in test_encoder_tex_active_null.c. A later + * Rust swap must relink this fixture through real private field adapters. */ +#include +#include +#include +#include +#include +#include "obs-internal.h" + +static struct obs_encoder encoder; +static struct obs_core_video_mix mix; +static unsigned char video_token; +static video_t *expected_media; +static struct obs_core_video_mix *lookup_result; +static unsigned lookup_calls; +static bool nv12_after_lookup, p010_after_lookup; + +struct obs_core_video_mix *get_mix_for_video(video_t *video) +{ + assert_ptr_equal(video, expected_media); + lookup_calls++; + /* The query must read flags after the lookup returns its selected mix. */ + mix.using_nv12_tex = nv12_after_lookup; + mix.using_p010_tex = p010_after_lookup; + return lookup_result; +} + +static void check_query(enum video_format format, bool expected) +{ + unsigned char encoder_before[sizeof(encoder)], mix_before[sizeof(mix)]; + mix.using_nv12_tex = nv12_after_lookup; + mix.using_p010_tex = p010_after_lookup; + memcpy(encoder_before, &encoder, sizeof(encoder)); + memcpy(mix_before, &mix, sizeof(mix)); + lookup_calls = 0; + assert_int_equal(obs_encoder_video_tex_active(&encoder, format), expected); + assert_int_equal(lookup_calls, 1); + assert_memory_equal(&encoder, encoder_before, sizeof(encoder)); + assert_memory_equal(&mix, mix_before, sizeof(mix)); +} + +static int setup(void **state) +{ + (void)state; + memset(&encoder, 0, sizeof(encoder)); + memset(&mix, 0, sizeof(mix)); + expected_media = (video_t *)&video_token; + encoder.media = expected_media; + lookup_result = &mix; + nv12_after_lookup = p010_after_lookup = false; + lookup_calls = 0; + return 0; +} + +static void missing_mix(void **state) +{ + (void)state; + lookup_result = NULL; + nv12_after_lookup = p010_after_lookup = true; + check_query(VIDEO_FORMAT_NV12, false); + check_query(VIDEO_FORMAT_P010, false); + check_query(VIDEO_FORMAT_RGBA, false); + /* A live encoder with no video still performs the exact NULL lookup. */ + encoder.media = expected_media = NULL; + check_query(VIDEO_FORMAT_NV12, false); + check_query(VIDEO_FORMAT_P010, false); + check_query(VIDEO_FORMAT_NONE, false); +} + +static void format_and_flag_matrix(void **state) +{ + (void)state; + for (unsigned flags = 0; flags < 4; flags++) { + nv12_after_lookup = (flags & 1) != 0; + p010_after_lookup = (flags & 2) != 0; + /* The real enum is contiguous, NONE through R10L; no host defaults. */ + for (enum video_format format = VIDEO_FORMAT_NONE; format <= VIDEO_FORMAT_R10L; format++) { + bool expected = (format == VIDEO_FORMAT_NV12 && nv12_after_lookup) || + (format == VIDEO_FORMAT_P010 && p010_after_lookup); + check_query(format, expected); + } + } +} + +static void reads_selected_mix_after_lookup(void **state) +{ + (void)state; + for (unsigned flags = 0; flags < 4; flags++) { + nv12_after_lookup = (flags & 1) != 0; + p010_after_lookup = (flags & 2) != 0; + mix.using_nv12_tex = !nv12_after_lookup; + mix.using_p010_tex = !p010_after_lookup; + lookup_calls = 0; + assert_int_equal(obs_encoder_video_tex_active(&encoder, VIDEO_FORMAT_NV12), nv12_after_lookup); + assert_int_equal(lookup_calls, 1); + mix.using_nv12_tex = !nv12_after_lookup; + mix.using_p010_tex = !p010_after_lookup; + lookup_calls = 0; + assert_int_equal(obs_encoder_video_tex_active(&encoder, VIDEO_FORMAT_P010), p010_after_lookup); + assert_int_equal(lookup_calls, 1); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = {cmocka_unit_test_setup(missing_mix, setup), + cmocka_unit_test_setup(format_and_flag_matrix, setup), + cmocka_unit_test_setup(reads_selected_mix_after_lookup, setup)}; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_encoder_video_tex_active.cmake b/test/cmocka/tests.d/test_encoder_video_tex_active.cmake new file mode 100644 index 0000000000..d85a840326 --- /dev/null +++ b/test/cmocka/tests.d/test_encoder_video_tex_active.cmake @@ -0,0 +1 @@ +# Registered in the canonical native test graph (test/cmocka/build/test-graph.json).