diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index d5b941beb5..9e0462920b 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -4,6 +4,7 @@ #include "libobs/obs-canvas-channel-fields.c.h" #include "libobs/obs-canvas-flags-fields.c.h" +#include "libobs/obs-canvas-has-valid-video-info.c.h" #include "libobs/obs-canvas-public-constructors.c.h" #include "libobs/obs-canvas-readonly-fields.c.h" #include "libobs/obs-canvas-ref-fields.c.h" diff --git a/libobs/obs-canvas-has-valid-video-info.c.h b/libobs/obs-canvas-has-valid-video-info.c.h new file mode 100644 index 0000000000..719f9ff4d2 --- /dev/null +++ b/libobs/obs-canvas-has-valid-video-info.c.h @@ -0,0 +1,26 @@ +/****************************************************************************** + Copyright (C) 2025 by Dennis Sädtler + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#include "libobs/build/libobs.pch.h" + +bool obs_canvas_has_valid_video_info(obs_canvas_t *canvas) +{ + struct obs_video_info *ovi = &canvas->ovi; + return obs_canvas_rules_size_set(ovi->base_width) && obs_canvas_rules_size_set(ovi->base_height) && + obs_canvas_rules_size_set(ovi->output_width) && obs_canvas_rules_size_set(ovi->output_height) && + obs_canvas_rules_format_set((int)ovi->output_format); +} diff --git a/libobs/obs-canvas.c.h b/libobs/obs-canvas.c.h index 2b8a7d9851..492acad352 100644 --- a/libobs/obs-canvas.c.h +++ b/libobs/obs-canvas.c.h @@ -192,14 +192,6 @@ void obs_free_canvas_mixes(void) pthread_mutex_unlock(&obs->data.canvases_mutex); } -bool obs_canvas_has_valid_video_info(obs_canvas_t *canvas) -{ - struct obs_video_info *ovi = &canvas->ovi; - return obs_canvas_rules_size_set(ovi->base_width) && obs_canvas_rules_size_set(ovi->base_height) && - obs_canvas_rules_size_set(ovi->output_width) && obs_canvas_rules_size_set(ovi->output_height) && - obs_canvas_rules_format_set((int)ovi->output_format); -} - bool obs_canvas_reset_video_internal(obs_canvas_t *canvas, struct obs_video_info *ovi) { obs_canvas_clear_mix(canvas); diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 7321935fd5..737a577693 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -1275,6 +1275,22 @@ } ] }, + "test_canvas_valid_video_info": { + "sources": [ + "test/cmocka/test_canvas_valid_video_info.c", + "libobs/obs-canvas-rules.c.h" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ] + }, "test_canvas_video_getter": { "sources": [ "test/cmocka/test_canvas_video_getter.c" @@ -22839,6 +22855,12 @@ ] ] }, + "test_canvas_valid_video_info": { + "target": "test_canvas_valid_video_info", + "command": [ + "${target}" + ] + }, "test_canvas_video_getter": { "target": "test_canvas_video_getter", "command": [ diff --git a/test/cmocka/test_canvas_valid_video_info.c b/test/cmocka/test_canvas_valid_video_info.c new file mode 100644 index 0000000000..0a46825ae1 --- /dev/null +++ b/test/cmocka/test_canvas_valid_video_info.c @@ -0,0 +1,118 @@ +/* C-source characterization of obs_canvas_has_valid_video_info (#5295). + * Real private layout; no core startup, graphics, locks or own threads. + * NULL is deliberately untested: the original C dereferences the canvas. + * The real C rules are compiled too: their symbols are hidden in libobs. + * This source-only fixture does not exercise a future Rust swap. + * External mode lets the later Rust swap reuse these unchanged assertions. */ +#include +#include +#include +#include +#include +#include +#include "obs-internal.h" + +#ifndef CANVAS_VALID_INFO_EXTERNAL +#include "libobs/obs-canvas-has-valid-video-info.c.h" +#endif + +static struct obs_canvas valid_canvas(void) +{ + struct obs_canvas canvas; + memset(&canvas, 0xa5, sizeof(canvas)); + canvas.ovi = (struct obs_video_info){ + .base_width = 1920, + .base_height = 1080, + .output_width = 1280, + .output_height = 720, + .output_format = VIDEO_FORMAT_I420, + }; + return canvas; +} + +static void check_validity(struct obs_canvas *canvas, bool expected) +{ + unsigned char before[sizeof(*canvas)]; + memcpy(before, canvas, sizeof(before)); + long allocations = bnum_allocs(); + assert_int_equal(obs_canvas_has_valid_video_info(canvas), expected); + assert_memory_equal(canvas, before, sizeof(before)); + assert_int_equal(bnum_allocs(), allocations); +} + +static void dimension_boundaries(size_t offset) +{ + static const struct { + uint32_t value; + bool expected; + } cases[] = {{0, false}, {1, true}, {2, true}, {UINT32_MAX / 2, true}, {UINT32_MAX, true}}; + struct obs_canvas canvas = valid_canvas(); + uint32_t *dimension = (uint32_t *)((unsigned char *)&canvas.ovi + offset); + for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + *dimension = cases[i].value; + check_validity(&canvas, cases[i].expected); + } +} + +static void base_width(void **state) +{ + (void)state; + dimension_boundaries(offsetof(struct obs_video_info, base_width)); +} + +static void base_height(void **state) +{ + (void)state; + dimension_boundaries(offsetof(struct obs_video_info, base_height)); +} + +static void output_width(void **state) +{ + (void)state; + dimension_boundaries(offsetof(struct obs_video_info, output_width)); +} + +static void output_height(void **state) +{ + (void)state; + dimension_boundaries(offsetof(struct obs_video_info, output_height)); +} + +static void output_format(void **state) +{ + (void)state; + static const struct { + enum video_format value; + bool expected; + } cases[] = {{VIDEO_FORMAT_NONE, false}, {VIDEO_FORMAT_I420, true}, + {VIDEO_FORMAT_RGBA, true}, {(enum video_format)INT_MIN, true}, + {(enum video_format) - 1, true}, {(enum video_format)INT_MAX, true}}; + struct obs_canvas canvas = valid_canvas(); + for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + /* Out-of-enum formats pass today: characterized, not endorsed. */ + canvas.ovi.output_format = cases[i].value; + check_validity(&canvas, cases[i].expected); + } +} + +static void conjunction(void **state) +{ + (void)state; + for (unsigned mask = 0; mask < 32; mask++) { + struct obs_canvas canvas = valid_canvas(); + canvas.ovi.base_width = (mask & 1) ? 1 : 0; + canvas.ovi.base_height = (mask & 2) ? 1 : 0; + canvas.ovi.output_width = (mask & 4) ? 1 : 0; + canvas.ovi.output_height = (mask & 8) ? 1 : 0; + canvas.ovi.output_format = (mask & 16) ? VIDEO_FORMAT_I420 : VIDEO_FORMAT_NONE; + check_validity(&canvas, mask == 31); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = {cmocka_unit_test(base_width), cmocka_unit_test(base_height), + cmocka_unit_test(output_width), cmocka_unit_test(output_height), + cmocka_unit_test(output_format), cmocka_unit_test(conjunction)}; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_canvas_valid_video_info.cmake b/test/cmocka/tests.d/test_canvas_valid_video_info.cmake new file mode 100644 index 0000000000..d85a840326 --- /dev/null +++ b/test/cmocka/tests.d/test_canvas_valid_video_info.cmake @@ -0,0 +1 @@ +# Registered in the canonical native test graph (test/cmocka/build/test-graph.json).