Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libobs/build/libobs.core-source+.h.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions libobs/obs-canvas-has-valid-video-info.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/******************************************************************************
Copyright (C) 2025 by Dennis Sädtler <saedtler@twitch.tv>

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 <http://www.gnu.org/licenses/>.
******************************************************************************/

#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);
}
8 changes: 0 additions & 8 deletions libobs/obs-canvas.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions test/cmocka/build/test-graph.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": [
Expand Down
118 changes: 118 additions & 0 deletions test/cmocka/test_canvas_valid_video_info.c
Original file line number Diff line number Diff line change
@@ -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 <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <limits.h>
#include <string.h>
#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);
}
1 change: 1 addition & 0 deletions test/cmocka/tests.d/test_canvas_valid_video_info.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Registered in the canonical native test graph (test/cmocka/build/test-graph.json).
Loading