diff --git a/libobs/build/libobs.core+.h.c b/libobs/build/libobs.core+.h.c index 12343e2bdd..6d5240ecde 100644 --- a/libobs/build/libobs.core+.h.c +++ b/libobs/build/libobs.core+.h.c @@ -69,6 +69,7 @@ #include "libobs/obs-module.c.h" #include "libobs/obs-object-id-fields.c.h" #include "libobs/obs-object-ref-fields.c.h" +#include "libobs/obs-registry-by-name.c.h" #include "libobs/obs-render-canvas-texture-fields.c.h" #include "libobs/obs-reset-audio-fields.c.h" #include "libobs/obs-reset-audio2-fields.c.h" diff --git a/libobs/build/libobs.pch.h b/libobs/build/libobs.pch.h index a6c44d799f..5746ba0f68 100644 --- a/libobs/build/libobs.pch.h +++ b/libobs/build/libobs.pch.h @@ -11,6 +11,7 @@ #include #include "obs-internal.h" +#include "obs-context-name-lookup.h" /* obs-missing-files.c */ #include "util/threading.h" diff --git a/libobs/obs-context-name-lookup.h b/libobs/obs-context-name-lookup.h new file mode 100644 index 0000000000..a41073cd90 --- /dev/null +++ b/libobs/obs-context-name-lookup.h @@ -0,0 +1,45 @@ +/* Private shared name lookup engine for context registry getters. */ +#pragma once + +static inline void *get_context_by_name(void *vfirst, const char *name, pthread_mutex_t *mutex, void *(*addref)(void *)) +{ + struct obs_context_data **first = vfirst; + struct obs_context_data *context; + + pthread_mutex_lock(mutex); + + /* If context list head has a hash table, look the name up in there */ + if (*first && (*first)->hh.tbl) { + HASH_FIND_STR(*first, name, context); + } else { + context = *first; + while (context) { + if (!context->private && strcmp(context->name, name) == 0) { + break; + } + + context = context->next; + } + } + + if (context) + addref(context); + + pthread_mutex_unlock(mutex); + return context; +} + +static inline void *obs_output_addref_safe_(void *ref) +{ + return obs_output_get_ref(ref); +} + +static inline void *obs_encoder_addref_safe_(void *ref) +{ + return obs_encoder_get_ref(ref); +} + +static inline void *obs_service_addref_safe_(void *ref) +{ + return obs_service_get_ref(ref); +} diff --git a/libobs/obs-registry-by-name.c.h b/libobs/obs-registry-by-name.c.h new file mode 100644 index 0000000000..0c3916bf6e --- /dev/null +++ b/libobs/obs-registry-by-name.c.h @@ -0,0 +1,16 @@ +#include "libobs/build/libobs.pch.h" + +obs_output_t *obs_get_output_by_name(const char *name) +{ + return get_context_by_name(&obs->data.first_output, name, &obs->data.outputs_mutex, obs_output_addref_safe_); +} + +obs_encoder_t *obs_get_encoder_by_name(const char *name) +{ + return get_context_by_name(&obs->data.first_encoder, name, &obs->data.encoders_mutex, obs_encoder_addref_safe_); +} + +obs_service_t *obs_get_service_by_name(const char *name) +{ + return get_context_by_name(&obs->data.first_service, name, &obs->data.services_mutex, obs_service_addref_safe_); +} diff --git a/libobs/obs.c.h b/libobs/obs.c.h index 036440c98e..454ecfb010 100644 --- a/libobs/obs.c.h +++ b/libobs/obs.c.h @@ -1648,34 +1648,6 @@ static int obs_reset_video_internal(struct obs_video_info *ovi) _Static_assert(OBS_CORE_SETUP_RULES_AUDIO_OUTPUT_FRAMES == AUDIO_OUTPUT_FRAMES, "AUDIO_OUTPUT_FRAMES"); _Static_assert(OBS_CORE_SETUP_RULES_SEC_TO_MSEC == SEC_TO_MSEC, "SEC_TO_MSEC"); -static inline void *get_context_by_name(void *vfirst, const char *name, pthread_mutex_t *mutex, void *(*addref)(void *)) -{ - struct obs_context_data **first = vfirst; - struct obs_context_data *context; - - pthread_mutex_lock(mutex); - - /* If context list head has a hash table, look the name up in there */ - if (*first && (*first)->hh.tbl) { - HASH_FIND_STR(*first, name, context); - } else { - context = *first; - while (context) { - if (!context->private && strcmp(context->name, name) == 0) { - break; - } - - context = context->next; - } - } - - if (context) - addref(context); - - pthread_mutex_unlock(mutex); - return context; -} - static void *get_context_by_uuid(void *ptable, const char *uuid, pthread_mutex_t *mutex, void *(*addref)(void *)) { struct obs_context_data **ht = ptable; @@ -1696,21 +1668,6 @@ static inline void *obs_source_addref_safe_(void *ref) return obs_source_get_ref(ref); } -static inline void *obs_output_addref_safe_(void *ref) -{ - return obs_output_get_ref(ref); -} - -static inline void *obs_encoder_addref_safe_(void *ref) -{ - return obs_encoder_get_ref(ref); -} - -static inline void *obs_service_addref_safe_(void *ref) -{ - return obs_service_get_ref(ref); -} - static inline void *obs_canvas_addref_safe_(void *ref) { return obs_canvas_get_ref(ref); @@ -1793,21 +1750,6 @@ obs_source_t *obs_get_transition_by_uuid(const char *uuid) return NULL; } -obs_output_t *obs_get_output_by_name(const char *name) -{ - return get_context_by_name(&obs->data.first_output, name, &obs->data.outputs_mutex, obs_output_addref_safe_); -} - -obs_encoder_t *obs_get_encoder_by_name(const char *name) -{ - return get_context_by_name(&obs->data.first_encoder, name, &obs->data.encoders_mutex, obs_encoder_addref_safe_); -} - -obs_service_t *obs_get_service_by_name(const char *name) -{ - return get_context_by_name(&obs->data.first_service, name, &obs->data.services_mutex, obs_service_addref_safe_); -} - /* obs-source-load.h copies these */ _Static_assert(OBS_SOURCE_LOAD_MONITOR_ONLY_BEFORE == MAKE_SEMANTIC_VERSION(23, 2, 2), "23.2.2"); _Static_assert(OBS_SOURCE_LOAD_MONITORING_ENABLED_BEFORE == MAKE_SEMANTIC_VERSION(33, 0, 0), "33.0.0"); diff --git a/test/cmocka/build/test-graph.d/test_registry_by_name.json b/test/cmocka/build/test-graph.d/test_registry_by_name.json new file mode 100644 index 0000000000..abe7ded6a1 --- /dev/null +++ b/test/cmocka/build/test-graph.d/test_registry_by_name.json @@ -0,0 +1,38 @@ +{ + "schema": 1, + "targets": { + "test_registry_by_name": { + "sources": [ + "test/cmocka/test_registry_by_name.c" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "SIMDe::SIMDe", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ] + } + }, + "tests": { + "test_registry_by_name": { + "target": "test_registry_by_name", + "command": [ + "${target}" + ], + "optional_prefixes": [ + [ + "xvfb-run", + "-a", + "-s", + "-screen 0 1280x1024x24 -noreset" + ] + ], + "timeout_seconds": 30 + } + } +} diff --git a/test/cmocka/test_registry_by_name.c b/test/cmocka/test_registry_by_name.c new file mode 100644 index 0000000000..b0fe5ff1ae --- /dev/null +++ b/test/cmocka/test_registry_by_name.c @@ -0,0 +1,194 @@ +/* Direct characterization of the three public registry name getters. No media + * pipeline or external plugin is needed: fallback output/encoder contexts and + * a registered synthetic service use real registries and destruction paths. */ +#include +#include +#include +#include +#include + +static const char *service_name(void *data) +{ + (void)data; + return "Registry service"; +} + +static void *service_create(obs_data_t *settings, obs_service_t *service) +{ + (void)settings; + return service; +} + +static void service_destroy(void *data) +{ + (void)data; +} + +static const char *service_protocol(void *data) +{ + (void)data; + return "RTMP"; +} + +static int setup(void **state) +{ + (void)state; + if (!obs_startup("en-US", NULL, NULL)) + return -1; + struct obs_service_info info = { + .id = "registry-service", + .get_name = service_name, + .create = service_create, + .destroy = service_destroy, + .get_protocol = service_protocol, + }; + obs_register_service(&info); + return 0; +} + +static int teardown(void **state) +{ + (void)state; + obs_shutdown(); + return 0; +} + +static void output_lookup(void **state) +{ + (void)state; + assert_null(obs_get_output_by_name("registry name")); + assert_null(obs_get_output_by_name("")); + obs_output_t *older = obs_output_create("registry-missing-output", "registry name", NULL, NULL); + obs_output_t *newer = obs_output_create("registry-missing-output", "registry name", NULL, NULL); + assert_non_null(older); + assert_non_null(newer); + assert_null(obs_get_output_by_name("Registry name")); + assert_null(obs_get_output_by_name("registry")); + assert_null(obs_get_output_by_name("")); + /* Duplicate names return the newest list entry: characterized, not endorsed. */ + obs_output_t *found = obs_get_output_by_name("registry name"); + assert_ptr_equal(found, newer); + obs_weak_output_t *weak = obs_output_get_weak_output(newer); + obs_output_release(newer); + obs_output_t *promoted = obs_weak_output_get_output(weak); + assert_ptr_equal(promoted, found); + obs_output_release(promoted); + obs_output_t *again = obs_get_output_by_name("registry name"); + assert_ptr_equal(again, found); + obs_output_release(again); + obs_output_release(found); + assert_null(obs_weak_output_get_output(weak)); + obs_weak_output_release(weak); + found = obs_get_output_by_name("registry name"); + assert_ptr_equal(found, older); + obs_output_release(found); + obs_output_release(older); + assert_null(obs_get_output_by_name("registry name")); +} + +static void encoder_lookup(void **state) +{ + (void)state; + assert_null(obs_get_encoder_by_name("encoder name")); + assert_null(obs_get_encoder_by_name("")); + obs_encoder_t *older = obs_video_encoder_create("registry-missing-encoder", "encoder name", NULL, NULL); + obs_encoder_t *newer = obs_video_encoder_create("registry-missing-encoder", "encoder name", NULL, NULL); + assert_non_null(older); + assert_non_null(newer); + assert_null(obs_get_encoder_by_name("Encoder name")); + assert_null(obs_get_encoder_by_name("encoder")); + assert_null(obs_get_encoder_by_name("")); + obs_encoder_t *found = obs_get_encoder_by_name("encoder name"); + assert_ptr_equal(found, newer); + obs_weak_encoder_t *weak = obs_encoder_get_weak_encoder(newer); + obs_encoder_release(newer); + obs_encoder_t *promoted = obs_weak_encoder_get_encoder(weak); + assert_ptr_equal(promoted, found); + obs_encoder_release(promoted); + obs_encoder_set_name(found, "renamed encoder"); + obs_encoder_t *again = obs_get_encoder_by_name("renamed encoder"); + assert_ptr_equal(again, found); + obs_encoder_release(again); + obs_encoder_release(found); + assert_null(obs_weak_encoder_get_encoder(weak)); + obs_weak_encoder_release(weak); + assert_null(obs_get_encoder_by_name("renamed encoder")); + found = obs_get_encoder_by_name("encoder name"); + assert_ptr_equal(found, older); + obs_encoder_release(found); + obs_encoder_release(older); + assert_null(obs_get_encoder_by_name("encoder name")); +} + +static void service_lookup(void **state) +{ + (void)state; + assert_null(obs_get_service_by_name("service name")); + assert_null(obs_get_service_by_name("")); + obs_service_t *older = obs_service_create("registry-service", "service name", NULL, NULL); + obs_service_t *newer = obs_service_create("registry-service", "service name", NULL, NULL); + obs_service_t *private = obs_service_create_private("registry-service", "service name", NULL); + assert_non_null(older); + assert_non_null(newer); + assert_non_null(private); + assert_null(obs_get_service_by_name("Service name")); + assert_null(obs_get_service_by_name("service")); + assert_null(obs_get_service_by_name("")); + obs_service_t *found = obs_get_service_by_name("service name"); + assert_ptr_equal(found, newer); + obs_weak_service_t *weak = obs_service_get_weak_service(newer); + obs_service_release(newer); + obs_service_t *promoted = obs_weak_service_get_service(weak); + assert_ptr_equal(promoted, found); + obs_service_release(promoted); + obs_service_t *again = obs_get_service_by_name("service name"); + assert_ptr_equal(again, found); + obs_service_release(again); + obs_service_release(found); + assert_null(obs_weak_service_get_service(weak)); + obs_weak_service_release(weak); + found = obs_get_service_by_name("service name"); + assert_ptr_equal(found, older); + obs_service_release(found); + obs_service_release(older); + /* The remaining private entry is skipped, even with an exact name match. */ + assert_null(obs_get_service_by_name("service name")); + obs_service_release(private); +} + +static void separate_registries(void **state) +{ + (void)state; + obs_output_t *output = obs_output_create("registry-missing-output", "shared name", NULL, NULL); + obs_encoder_t *encoder = obs_video_encoder_create("registry-missing-encoder", "shared name", NULL, NULL); + obs_service_t *service = obs_service_create("registry-service", "shared name", NULL, NULL); + assert_non_null(output); + assert_non_null(encoder); + assert_non_null(service); + obs_output_t *found_output = obs_get_output_by_name("shared name"); + obs_encoder_t *found_encoder = obs_get_encoder_by_name("shared name"); + obs_service_t *found_service = obs_get_service_by_name("shared name"); + assert_ptr_equal(found_output, output); + assert_ptr_equal(found_encoder, encoder); + assert_ptr_equal(found_service, service); + obs_output_release(found_output); + obs_encoder_release(found_encoder); + obs_service_release(found_service); + obs_output_release(output); + obs_encoder_release(encoder); + obs_service_release(service); + assert_null(obs_get_output_by_name("shared name")); + assert_null(obs_get_encoder_by_name("shared name")); + assert_null(obs_get_service_by_name("shared name")); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(output_lookup), + cmocka_unit_test(encoder_lookup), + cmocka_unit_test(service_lookup), + cmocka_unit_test(separate_registries), + }; + return cmocka_run_group_tests(tests, setup, teardown); +}