diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:28:17 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:28:17 +0000 |
commit | 7a46c07230b8d8108c0e8e80df4522d0ac116538 (patch) | |
tree | d483300dab478b994fe199a5d19d18d74153718a /src/tests | |
parent | Initial commit. (diff) | |
download | pipewire-7a46c07230b8d8108c0e8e80df4522d0ac116538.tar.xz pipewire-7a46c07230b8d8108c0e8e80df4522d0ac116538.zip |
Adding upstream version 0.3.65.upstream/0.3.65upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/meson.build | 52 | ||||
-rw-r--r-- | src/tests/test-cpp.cpp | 35 | ||||
-rw-r--r-- | src/tests/test-endpoint.c | 469 | ||||
-rw-r--r-- | src/tests/test-filter.c | 375 | ||||
-rw-r--r-- | src/tests/test-interfaces.c | 382 | ||||
-rw-r--r-- | src/tests/test-stream.c | 257 |
6 files changed, 1570 insertions, 0 deletions
diff --git a/src/tests/meson.build b/src/tests/meson.build new file mode 100644 index 0000000..f7c54f2 --- /dev/null +++ b/src/tests/meson.build @@ -0,0 +1,52 @@ +test_apps = [ + 'test-endpoint', + 'test-interfaces', + # 'test-remote', + 'test-stream', + 'test-filter', +] + +foreach a : test_apps + test('pw-' + a, + executable('pw-' + a, a + '.c', + dependencies : [pipewire_dep], + include_directories: [includes_inc], + install : installed_tests_enabled, + install_dir : installed_tests_execdir), + env : [ + 'SPA_PLUGIN_DIR=@0@'.format(spa_dep.get_variable('plugindir')), + 'PIPEWIRE_CONFIG_DIR=@0@'.format(pipewire_dep.get_variable('confdatadir')), + 'PIPEWIRE_MODULE_DIR=@0@'.format(pipewire_dep.get_variable('moduledir')), + ]) + + if installed_tests_enabled + test_conf = configuration_data() + test_conf.set('exec', installed_tests_execdir / 'pw-' + a) + configure_file( + input: installed_tests_template, + output: 'pw-' + a + '.test', + install_dir: installed_tests_metadir, + configuration: test_conf + ) + endif +endforeach + + +if have_cpp + test_cpp = executable('pw-test-cpp', 'test-cpp.cpp', + dependencies : [pipewire_dep], + install : installed_tests_enabled, + install_dir : installed_tests_execdir) + test('pw-test-cpp', test_cpp) + + if installed_tests_enabled + test_conf = configuration_data() + test_conf.set('exec', installed_tests_execdir / 'pw-test-cpp') + configure_file( + input: installed_tests_template, + output: 'pw-test-cpp.test', + install_dir: installed_tests_metadir, + configuration: test_conf + ) + endif +endif diff --git a/src/tests/test-cpp.cpp b/src/tests/test-cpp.cpp new file mode 100644 index 0000000..e905724 --- /dev/null +++ b/src/tests/test-cpp.cpp @@ -0,0 +1,35 @@ +/* PipeWire + * + * Copyright © 2018 Wim Taymans <wim.taymans@gmail.com> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <pipewire/pipewire.h> +#include <pipewire/impl.h> +#include <pipewire/extensions/client-node.h> +#include <pipewire/extensions/protocol-native.h> + + +int main(int argc, char *argv[]) +{ + pw_init(&argc, &argv); + return 0; +} diff --git a/src/tests/test-endpoint.c b/src/tests/test-endpoint.c new file mode 100644 index 0000000..dd516a6 --- /dev/null +++ b/src/tests/test-endpoint.c @@ -0,0 +1,469 @@ +/* PipeWire + * + * Copyright © 2020 Collabora Ltd. + * @author George Kiagiadakis <george.kiagiadakis@collabora.com> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <unistd.h> + +#include <pipewire/pipewire.h> +#include <pipewire/extensions/session-manager.h> + +#include <spa/utils/string.h> +#include <spa/pod/builder.h> +#include <spa/pod/parser.h> +#include <spa/pod/filter.h> + +#include <valgrind/valgrind.h> + +struct props +{ + float volume; + bool mute; +}; + +struct endpoint +{ + struct spa_interface iface; + struct spa_hook_list hooks; + struct pw_properties *properties; + struct pw_endpoint_info info; + uint32_t n_subscribe_ids; + uint32_t subscribe_ids[2]; + struct props props; +}; + +#define pw_endpoint_emit(hooks,method,version,...) \ + spa_hook_list_call_simple(hooks, struct pw_endpoint_events, \ + method, version, ##__VA_ARGS__) + +#define pw_endpoint_emit_info(hooks,...) pw_endpoint_emit(hooks, info, 0, ##__VA_ARGS__) +#define pw_endpoint_emit_param(hooks,...) pw_endpoint_emit(hooks, param, 0, ##__VA_ARGS__) + +static int +endpoint_add_listener(void *object, + struct spa_hook *listener, + const struct pw_endpoint_events *events, + void *data) +{ + struct endpoint *self = object; + struct spa_hook_list save; + + spa_hook_list_isolate(&self->hooks, &save, listener, events, data); + pw_endpoint_emit_info(&self->hooks, &self->info); + spa_hook_list_join(&self->hooks, &save); + return 0; +} + +static int +endpoint_enum_params (void *object, int seq, + uint32_t id, uint32_t start, uint32_t num, + const struct spa_pod *filter) +{ + struct endpoint *self = object; + struct spa_pod *param, *result; + struct spa_pod_builder b = { 0 }; + uint8_t buffer[1024]; + uint32_t count = 0, index, next = start; + + next = start; + next: + index = next++; + + spa_pod_builder_init(&b, buffer, sizeof(buffer)); + + switch (id) { + case SPA_PARAM_PropInfo: + { + struct props *p = &self->props; + + switch (index) { + case 0: + param = spa_pod_builder_add_object(&b, + SPA_TYPE_OBJECT_PropInfo, id, + SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_volume), + SPA_PROP_INFO_description, SPA_POD_String("volume"), + SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Float(p->volume, 0.0, 1.0)); + break; + case 1: + param = spa_pod_builder_add_object(&b, + SPA_TYPE_OBJECT_PropInfo, id, + SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_mute), + SPA_PROP_INFO_description, SPA_POD_String("mute"), + SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(p->mute)); + break; + default: + return 0; + } + break; + } + case SPA_PARAM_Props: + { + struct props *p = &self->props; + + switch (index) { + case 0: + param = spa_pod_builder_add_object(&b, + SPA_TYPE_OBJECT_Props, id, + SPA_PROP_volume, SPA_POD_Float(p->volume), + SPA_PROP_mute, SPA_POD_Bool(p->mute)); + break; + default: + return 0; + } + break; + } + default: + return -ENOENT; + } + + if (spa_pod_filter(&b, &result, param, filter) < 0) + goto next; + + pw_endpoint_emit_param(&self->hooks, seq, id, index, next, result); + + if (++count != num) + goto next; + + return 0; +} + +static int +endpoint_subscribe_params (void *object, uint32_t *ids, uint32_t n_ids) +{ + struct endpoint *self = object; + + n_ids = SPA_MIN(n_ids, SPA_N_ELEMENTS(self->subscribe_ids)); + self->n_subscribe_ids = n_ids; + + for (uint32_t i = 0; i < n_ids; i++) { + self->subscribe_ids[i] = ids[i]; + endpoint_enum_params(object, 1, ids[i], 0, UINT32_MAX, NULL); + } + return 0; +} + +static int +endpoint_set_param (void *object, uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct endpoint *self = object; + + if (id == SPA_PARAM_Props) { + struct props *p = &self->props; + spa_pod_parse_object(param, + SPA_TYPE_OBJECT_Props, NULL, + SPA_PROP_volume, SPA_POD_OPT_Float(&p->volume), + SPA_PROP_mute, SPA_POD_OPT_Bool(&p->mute)); + } + else { + spa_assert_not_reached(); + return -ENOENT; + } + + for (uint32_t i = 0; i < self->n_subscribe_ids; i++) { + if (id == self->subscribe_ids[i]) + endpoint_enum_params (self, 1, id, 0, UINT32_MAX, NULL); + } + + return 0; +} + +static int +endpoint_create_link (void *object, const struct spa_dict *props) +{ + spa_assert_not_reached(); + return -ENOTSUP; +} + +static const struct pw_endpoint_methods endpoint_methods = { + PW_VERSION_ENDPOINT_METHODS, + .add_listener = endpoint_add_listener, + .subscribe_params = endpoint_subscribe_params, + .enum_params = endpoint_enum_params, + .set_param = endpoint_set_param, + .create_link = endpoint_create_link, +}; + +static struct spa_param_info param_info[] = { + SPA_PARAM_INFO (SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE), + SPA_PARAM_INFO (SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ) +}; + +static void +endpoint_init(struct endpoint * self) +{ + self->iface = SPA_INTERFACE_INIT ( + PW_TYPE_INTERFACE_Endpoint, + PW_VERSION_ENDPOINT, + &endpoint_methods, self); + spa_hook_list_init (&self->hooks); + + self->info.version = PW_VERSION_ENDPOINT_INFO; + self->info.change_mask = PW_ENDPOINT_CHANGE_MASK_ALL; + self->info.name = "test-endpoint"; + self->info.media_class = "Audio/Sink"; + self->info.direction = PW_DIRECTION_OUTPUT; + self->info.n_streams = 0; + self->info.session_id = SPA_ID_INVALID; + + self->properties = pw_properties_new( + PW_KEY_ENDPOINT_NAME, self->info.name, + PW_KEY_MEDIA_CLASS, self->info.media_class, + NULL); + self->info.props = &self->properties->dict; + + self->info.params = param_info; + self->info.n_params = SPA_N_ELEMENTS (param_info); + + self->props.volume = 0.9; + self->props.mute = false; +} + +static void +endpoint_clear(struct endpoint * self) +{ + spa_hook_list_clean(&self->hooks); + pw_properties_free(self->properties); +} + +struct test_endpoint_data +{ + struct pw_main_loop *loop; + struct pw_context *context; + struct pw_core *core; + + struct pw_registry *registry; + struct spa_hook registry_listener; + + struct endpoint endpoint; + struct pw_proxy *export_proxy; + struct pw_proxy *bound_proxy; + struct spa_hook object_listener; + struct spa_hook proxy_listener; + + struct props props; + bool info_received; + int params_received; +}; + +static void +endpoint_event_info(void *data, const struct pw_endpoint_info *info) +{ + struct test_endpoint_data *d = data; + const char *val; + + spa_assert_se(info); + spa_assert_se(info->version == PW_VERSION_ENDPOINT_INFO); + spa_assert_se(info->id == pw_proxy_get_bound_id(d->bound_proxy)); + spa_assert_se(info->id == pw_proxy_get_bound_id(d->export_proxy)); + spa_assert_se(info->change_mask == PW_ENDPOINT_CHANGE_MASK_ALL); + spa_assert_se(spa_streq(info->name, "test-endpoint")); + spa_assert_se(spa_streq(info->media_class, "Audio/Sink")); + spa_assert_se(info->direction == PW_DIRECTION_OUTPUT); + spa_assert_se(info->n_streams == 0); + spa_assert_se(info->session_id == SPA_ID_INVALID); + spa_assert_se(info->n_params == SPA_N_ELEMENTS (param_info)); + spa_assert_se(info->n_params == 2); + spa_assert_se(info->params[0].id == param_info[0].id); + spa_assert_se(info->params[0].flags == param_info[0].flags); + spa_assert_se(info->params[1].id == param_info[1].id); + spa_assert_se(info->params[1].flags == param_info[1].flags); + spa_assert_se(info->props != NULL); + val = spa_dict_lookup(info->props, PW_KEY_ENDPOINT_NAME); + spa_assert_se(val && spa_streq(val, "test-endpoint")); + val = spa_dict_lookup(info->props, PW_KEY_MEDIA_CLASS); + spa_assert_se(val && spa_streq(val, "Audio/Sink")); + + d->info_received = true; + pw_main_loop_quit(d->loop); +} + +static void +endpoint_event_param(void *data, int seq, + uint32_t id, uint32_t index, uint32_t next, + const struct spa_pod *param) +{ + struct test_endpoint_data *d = data; + + if (id == SPA_PARAM_Props) { + struct props *p = &d->props; + spa_assert_se(param); + spa_pod_parse_object(param, + SPA_TYPE_OBJECT_Props, &id, + SPA_PROP_volume, SPA_POD_OPT_Float(&p->volume), + SPA_PROP_mute, SPA_POD_OPT_Bool(&p->mute)); + spa_assert_se(id == SPA_PARAM_Props); + } + + d->params_received++; + pw_main_loop_quit(d->loop); +} + +static const struct pw_endpoint_events endpoint_events = { + PW_VERSION_ENDPOINT_EVENTS, + .info = endpoint_event_info, + .param = endpoint_event_param, +}; + +static void +endpoint_proxy_destroy(void *data) +{ + struct test_endpoint_data *d = data; + d->bound_proxy = NULL; + pw_main_loop_quit(d->loop); +} + +static const struct pw_proxy_events proxy_events = { + PW_VERSION_PROXY_EVENTS, + .destroy = endpoint_proxy_destroy, +}; + +static void +test_endpoint_global(void *data, uint32_t id, + uint32_t permissions, const char *type, uint32_t version, + const struct spa_dict *props) +{ + struct test_endpoint_data *d = data; + const char *val; + + if (!spa_streq(type, PW_TYPE_INTERFACE_Endpoint)) + return; + + d->bound_proxy = pw_registry_bind(d->registry, id, type, + PW_VERSION_ENDPOINT, 0); + spa_assert_se(d->bound_proxy != NULL); + + spa_assert_se(props != NULL); + val = spa_dict_lookup(props, PW_KEY_ENDPOINT_NAME); + spa_assert_se(val && spa_streq(val, "test-endpoint")); + val = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS); + spa_assert_se(val && spa_streq(val, "Audio/Sink")); + + pw_endpoint_add_listener(d->bound_proxy, &d->object_listener, + &endpoint_events, d); + pw_proxy_add_listener(d->bound_proxy, &d->proxy_listener, + &proxy_events, d); +} + +static void +test_endpoint_global_remove(void *data, uint32_t id) +{ + struct test_endpoint_data *d = data; + if (d->bound_proxy && id == pw_proxy_get_bound_id(d->bound_proxy)) + pw_proxy_destroy(d->bound_proxy); +} + +static const struct pw_registry_events registry_events = { + PW_VERSION_REGISTRY_EVENTS, + .global = test_endpoint_global, + .global_remove = test_endpoint_global_remove, +}; + +static void test_endpoint(void) +{ + struct test_endpoint_data d; + uint32_t ids[] = { SPA_PARAM_Props }; + uint8_t buffer[1024]; + struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, 1024); + + d.loop = pw_main_loop_new(NULL); + d.context = pw_context_new(pw_main_loop_get_loop(d.loop), NULL, 0); + spa_assert_se(d.context != NULL); + + d.core = pw_context_connect_self(d.context, NULL, 0); + spa_assert_se(d.core != NULL); + + d.registry = pw_core_get_registry(d.core, PW_VERSION_REGISTRY, 0); + pw_registry_add_listener(d.registry, + &d.registry_listener, + ®istry_events, &d); + + /* export and expect to get a global on the registry, along with info */ + d.info_received = false; + endpoint_init(&d.endpoint); + d.export_proxy = pw_core_export(d.core, PW_TYPE_INTERFACE_Endpoint, + d.endpoint.info.props, &d.endpoint.iface, 0); + spa_assert_se(d.export_proxy != NULL); + pw_main_loop_run(d.loop); + spa_assert_se(d.bound_proxy); + spa_assert_se(d.info_received == true); + + /* request params */ + d.params_received = 0; + d.props.volume = 0.0; + d.props.mute = true; + pw_endpoint_subscribe_params(d.bound_proxy, ids, SPA_N_ELEMENTS(ids)); + pw_main_loop_run(d.loop); + spa_assert_se(d.params_received == 1); + spa_assert_se(d.props.volume > 0.89 && d.props.volume < 0.91); + spa_assert_se(d.props.mute == false); + + /* set param from the client */ + d.params_received = 0; + pw_endpoint_set_param(d.bound_proxy, SPA_PARAM_Props, 0, + spa_pod_builder_add_object(&b, + SPA_TYPE_OBJECT_Props, SPA_PARAM_Props, + SPA_PROP_volume, SPA_POD_Float(0.5))); + pw_main_loop_run(d.loop); + spa_assert_se(d.params_received == 1); + spa_assert_se(d.props.volume > 0.49 && d.props.volume < 0.51); + spa_assert_se(d.props.mute == false); + + /* set param from the impl */ + d.params_received = 0; + pw_endpoint_set_param(&d.endpoint.iface, SPA_PARAM_Props, 0, + spa_pod_builder_add_object(&b, + SPA_TYPE_OBJECT_Props, SPA_PARAM_Props, + SPA_PROP_volume, SPA_POD_Float(0.2), + SPA_PROP_mute, SPA_POD_Bool(true))); + pw_main_loop_run(d.loop); + spa_assert_se(d.params_received == 1); + spa_assert_se(d.props.volume > 0.19 && d.props.volume < 0.21); + spa_assert_se(d.props.mute == true); + + /* stop exporting and expect to see that reflected on the registry */ + pw_proxy_destroy(d.export_proxy); + pw_main_loop_run(d.loop); + spa_assert_se(!d.bound_proxy); + + endpoint_clear(&d.endpoint); + pw_proxy_destroy((struct pw_proxy*)d.registry); + pw_context_destroy(d.context); + pw_main_loop_destroy(d.loop); +} + +int main(int argc, char *argv[]) +{ + /* FIXME: This test has a leak and a use of uninitialized buffer + * that needs to be debugged and fixed (or excluded). Meanwhile - + * skip it from valgrind so we can at least use the others. */ + if (RUNNING_ON_VALGRIND) + return 77; + + pw_init(&argc, &argv); + + alarm(5); /* watchdog; terminate after 5 seconds */ + test_endpoint(); + + return 0; +} diff --git a/src/tests/test-filter.c b/src/tests/test-filter.c new file mode 100644 index 0000000..93f0037 --- /dev/null +++ b/src/tests/test-filter.c @@ -0,0 +1,375 @@ +/* PipeWire + * + * Copyright © 2022 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <pipewire/pipewire.h> +#include <pipewire/main-loop.h> +#include <pipewire/filter.h> + +#include <spa/utils/string.h> + +#define TEST_FUNC(a,b,func) \ +do { \ + a.func = b.func; \ + spa_assert_se(SPA_PTRDIFF(&a.func, &a) == SPA_PTRDIFF(&b.func, &b)); \ +} while(0) + +static void test_abi(void) +{ + static const struct { + uint32_t version; + void (*destroy) (void *data); + void (*state_changed) (void *data, enum pw_filter_state old, + enum pw_filter_state state, const char *error); + void (*io_changed) (void *data, void *port_data, uint32_t id, void *area, uint32_t size); + void (*param_changed) (void *data, void *port_data, uint32_t id, const struct spa_pod *param); + void (*add_buffer) (void *data, void *port_data, struct pw_buffer *buffer); + void (*remove_buffer) (void *data, void *port_data, struct pw_buffer *buffer); + void (*process) (void *data, struct spa_io_position *position); + void (*drained) (void *data); + void (*command) (void *data, const struct spa_command *command); + } test = { PW_VERSION_FILTER_EVENTS, NULL }; + + struct pw_filter_events ev; + + TEST_FUNC(ev, test, destroy); + TEST_FUNC(ev, test, state_changed); + TEST_FUNC(ev, test, io_changed); + TEST_FUNC(ev, test, param_changed); + TEST_FUNC(ev, test, add_buffer); + TEST_FUNC(ev, test, remove_buffer); + TEST_FUNC(ev, test, process); + TEST_FUNC(ev, test, drained); + TEST_FUNC(ev, test, command); + + spa_assert_se(PW_VERSION_FILTER_EVENTS == 1); + spa_assert_se(sizeof(ev) == sizeof(test)); + + spa_assert_se(PW_FILTER_STATE_ERROR == -1); + spa_assert_se(PW_FILTER_STATE_UNCONNECTED == 0); + spa_assert_se(PW_FILTER_STATE_CONNECTING == 1); + spa_assert_se(PW_FILTER_STATE_PAUSED == 2); + spa_assert_se(PW_FILTER_STATE_STREAMING == 3); + + spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_ERROR) != NULL); + spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_UNCONNECTED) != NULL); + spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_CONNECTING) != NULL); + spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_PAUSED) != NULL); + spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_STREAMING) != NULL); +} + +static void filter_destroy_error(void *data) +{ + spa_assert_not_reached(); +} +static void filter_state_changed_error(void *data, enum pw_filter_state old, + enum pw_filter_state state, const char *error) +{ + spa_assert_not_reached(); +} +static void filter_io_changed_error(void *data, void *port_data, uint32_t id, void *area, uint32_t size) +{ + spa_assert_not_reached(); +} +static void filter_param_changed_error(void *data, void *port_data, uint32_t id, const struct spa_pod *format) +{ + spa_assert_not_reached(); +} +static void filter_add_buffer_error(void *data, void *port_data, struct pw_buffer *buffer) +{ + spa_assert_not_reached(); +} +static void filter_remove_buffer_error(void *data, void *port_data, struct pw_buffer *buffer) +{ + spa_assert_not_reached(); +} +static void filter_process_error(void *data, struct spa_io_position *position) +{ + spa_assert_not_reached(); +} +static void filter_drained_error(void *data) +{ + spa_assert_not_reached(); +} + +static const struct pw_filter_events filter_events_error = +{ + PW_VERSION_FILTER_EVENTS, + .destroy = filter_destroy_error, + .state_changed = filter_state_changed_error, + .io_changed = filter_io_changed_error, + .param_changed = filter_param_changed_error, + .add_buffer = filter_add_buffer_error, + .remove_buffer = filter_remove_buffer_error, + .process = filter_process_error, + .drained = filter_drained_error +}; + +static int destroy_count = 0; +static void filter_destroy_count(void *data) +{ + destroy_count++; +} +static void test_create(void) +{ + struct pw_main_loop *loop; + struct pw_context *context; + struct pw_core *core; + struct pw_filter *filter; + struct pw_filter_events filter_events = filter_events_error; + struct spa_hook listener = { 0, }; + const char *error = NULL; + + loop = pw_main_loop_new(NULL); + context = pw_context_new(pw_main_loop_get_loop(loop), NULL, 12); + spa_assert_se(context != NULL); + core = pw_context_connect_self(context, NULL, 0); + spa_assert_se(core != NULL); + filter = pw_filter_new(core, "test", NULL); + spa_assert_se(filter != NULL); + pw_filter_add_listener(filter, &listener, &filter_events, filter); + + /* check state */ + spa_assert_se(pw_filter_get_state(filter, &error) == PW_FILTER_STATE_UNCONNECTED); + spa_assert_se(error == NULL); + /* check name */ + spa_assert_se(spa_streq(pw_filter_get_name(filter), "test")); + + /* check id, only when connected */ + spa_assert_se(pw_filter_get_node_id(filter) == SPA_ID_INVALID); + + /* check destroy */ + destroy_count = 0; + filter_events.destroy = filter_destroy_count; + pw_filter_destroy(filter); + spa_assert_se(destroy_count == 1); + + pw_context_destroy(context); + pw_main_loop_destroy(loop); +} + +static void test_properties(void) +{ + struct pw_main_loop *loop; + struct pw_context *context; + struct pw_core *core; + const struct pw_properties *props; + struct pw_filter *filter; + struct pw_filter_events filter_events = filter_events_error; + struct spa_hook listener = { { NULL }, }; + struct spa_dict_item items[3]; + + loop = pw_main_loop_new(NULL); + context = pw_context_new(pw_main_loop_get_loop(loop), NULL, 12); + spa_assert_se(context != NULL); + core = pw_context_connect_self(context, NULL, 0); + spa_assert_se(core != NULL); + filter = pw_filter_new(core, "test", + pw_properties_new("foo", "bar", + "biz", "fuzz", + NULL)); + spa_assert_se(filter != NULL); + pw_filter_add_listener(filter, &listener, &filter_events, filter); + + props = pw_filter_get_properties(filter, NULL); + spa_assert_se(props != NULL); + spa_assert_se(spa_streq(pw_properties_get(props, "foo"), "bar")); + spa_assert_se(spa_streq(pw_properties_get(props, "biz"), "fuzz")); + spa_assert_se(pw_properties_get(props, "buzz") == NULL); + + /* remove foo */ + items[0] = SPA_DICT_ITEM_INIT("foo", NULL); + /* change biz */ + items[1] = SPA_DICT_ITEM_INIT("biz", "buzz"); + /* add buzz */ + items[2] = SPA_DICT_ITEM_INIT("buzz", "frizz"); + pw_filter_update_properties(filter, NULL, &SPA_DICT_INIT(items, 3)); + + spa_assert_se(props == pw_filter_get_properties(filter, NULL)); + spa_assert_se(pw_properties_get(props, "foo") == NULL); + spa_assert_se(spa_streq(pw_properties_get(props, "biz"), "buzz")); + spa_assert_se(spa_streq(pw_properties_get(props, "buzz"), "frizz")); + + /* check destroy */ + destroy_count = 0; + filter_events.destroy = filter_destroy_count; + pw_context_destroy(context); + spa_assert_se(destroy_count == 1); + + pw_main_loop_destroy(loop); +} + +struct roundtrip_data +{ + struct pw_main_loop *loop; + int pending; + int done; +}; + +static void core_event_done(void *object, uint32_t id, int seq) +{ + struct roundtrip_data *data = object; + if (id == PW_ID_CORE && seq == data->pending) { + data->done = 1; + printf("done %d\n", seq); + pw_main_loop_quit(data->loop); + } +} + +static int roundtrip(struct pw_core *core, struct pw_main_loop *loop) +{ + struct spa_hook core_listener; + struct roundtrip_data data = { .loop = loop }; + const struct pw_core_events core_events = { + PW_VERSION_CORE_EVENTS, + .done = core_event_done, + }; + spa_zero(core_listener); + pw_core_add_listener(core, &core_listener, + &core_events, &data); + + data.pending = pw_core_sync(core, PW_ID_CORE, 0); + printf("sync %d\n", data.pending); + + while (!data.done) { + pw_main_loop_run(loop); + } + spa_hook_remove(&core_listener); + return 0; +} + +static int node_count = 0; +static int port_count = 0; +static void registry_event_global(void *data, uint32_t id, + uint32_t permissions, const char *type, uint32_t version, + const struct spa_dict *props) +{ + printf("object: id:%u type:%s/%d\n", id, type, version); + if (spa_streq(type, PW_TYPE_INTERFACE_Port)) + port_count++; + else if (spa_streq(type, PW_TYPE_INTERFACE_Node)) + node_count++; + +} +static void registry_event_global_remove(void *data, uint32_t id) +{ + printf("object: id:%u\n", id); +} + +struct port { + struct pw_filter *filter; +}; + +static void test_create_port(void) +{ + struct pw_main_loop *loop; + struct pw_context *context; + struct pw_core *core; + struct pw_registry *registry; + struct pw_filter *filter; + struct spa_hook registry_listener = { 0, }; + static const struct pw_registry_events registry_events = { + PW_VERSION_REGISTRY_EVENTS, + .global = registry_event_global, + .global_remove = registry_event_global_remove, + }; + int res; + struct port *port; + enum pw_filter_state state; + + loop = pw_main_loop_new(NULL); + context = pw_context_new(pw_main_loop_get_loop(loop), NULL, 12); + spa_assert_se(context != NULL); + core = pw_context_connect_self(context, NULL, 0); + spa_assert_se(core != NULL); + filter = pw_filter_new(core, "test", NULL); + spa_assert_se(filter != NULL); + + registry = pw_core_get_registry(core, PW_VERSION_REGISTRY, 0); + spa_assert_se(registry != NULL); + pw_registry_add_listener(registry, ®istry_listener, + ®istry_events, NULL); + + state = pw_filter_get_state(filter, NULL); + printf("state %s\n", pw_filter_state_as_string(state)); + res = pw_filter_connect(filter, PW_FILTER_FLAG_RT_PROCESS, NULL, 0); + spa_assert_se(res >= 0); + + printf("wait connect\n"); + while (true) { + state = pw_filter_get_state(filter, NULL); + printf("state %s\n", pw_filter_state_as_string(state)); + spa_assert_se(state != PW_FILTER_STATE_ERROR); + + if (state == PW_FILTER_STATE_PAUSED) + break; + + roundtrip(core, loop); + } + spa_assert_se(node_count == 1); + + printf("add port\n"); + /* make an audio DSP output port */ + port = pw_filter_add_port(filter, + PW_DIRECTION_OUTPUT, + PW_FILTER_PORT_FLAG_MAP_BUFFERS, + sizeof(struct port), + pw_properties_new( + PW_KEY_FORMAT_DSP, "32 bit float mono audio", + PW_KEY_PORT_NAME, "output", + NULL), + NULL, 0); + + printf("wait port\n"); + roundtrip(core, loop); + + spa_assert_se(port_count == 1); + printf("port added\n"); + + printf("remove port\n"); + pw_filter_remove_port(port); + roundtrip(core, loop); + + printf("destroy\n"); + /* check destroy */ + pw_filter_destroy(filter); + + pw_proxy_destroy((struct pw_proxy*)registry); + + pw_context_destroy(context); + pw_main_loop_destroy(loop); +} + +int main(int argc, char *argv[]) +{ + pw_init(&argc, &argv); + + test_abi(); + test_create(); + test_properties(); + test_create_port(); + + pw_deinit(); + + return 0; +} diff --git a/src/tests/test-interfaces.c b/src/tests/test-interfaces.c new file mode 100644 index 0000000..86f772f --- /dev/null +++ b/src/tests/test-interfaces.c @@ -0,0 +1,382 @@ +/* PipeWire + * + * Copyright © 2019 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <pipewire/pipewire.h> + +#define TEST_FUNC(a,b,func) \ +do { \ + a.func = b.func; \ + spa_assert_se(SPA_PTRDIFF(&a.func, &a) == SPA_PTRDIFF(&b.func, &b)); \ +} while(0) + +static void test_core_abi(void) +{ + static const struct { + uint32_t version; + int (*add_listener) (void *object, + struct spa_hook *listener, + const struct pw_core_events *events, + void *data); + int (*hello) (void *object, uint32_t version); + int (*sync) (void *object, uint32_t id, int seq); + int (*pong) (void *object, uint32_t id, int seq); + int (*error) (void *object, uint32_t id, int seq, int res, const char *error); + struct pw_registry * (*get_registry) (void *object, + uint32_t version, size_t user_data_size); + void * (*create_object) (void *object, + const char *factory_name, + const char *type, + uint32_t version, + const struct spa_dict *props, + size_t user_data_size); + int (*destroy) (void *object, void *proxy); + } methods = { PW_VERSION_CORE_METHODS, }; + static const struct { + uint32_t version; + void (*info) (void *data, const struct pw_core_info *info); + void (*done) (void *data, uint32_t id, int seq); + void (*ping) (void *data, uint32_t id, int seq); + void (*error) (void *data, uint32_t id, int seq, int res, const char *error); + void (*remove_id) (void *data, uint32_t id); + void (*bound_id) (void *data, uint32_t id, uint32_t global_id); + void (*add_mem) (void *data, uint32_t id, uint32_t type, int fd, uint32_t flags); + void (*remove_mem) (void *data, uint32_t id); + } events = { PW_VERSION_CORE_EVENTS, }; + + struct pw_core_events e; + struct pw_core_methods m; + + TEST_FUNC(m, methods, version); + TEST_FUNC(m, methods, add_listener); + TEST_FUNC(m, methods, hello); + TEST_FUNC(m, methods, sync); + TEST_FUNC(m, methods, pong); + TEST_FUNC(m, methods, error); + TEST_FUNC(m, methods, get_registry); + TEST_FUNC(m, methods, create_object); + TEST_FUNC(m, methods, destroy); + spa_assert_se(PW_VERSION_CORE_METHODS == 0); + spa_assert_se(sizeof(m) == sizeof(methods)); + + TEST_FUNC(e, events, version); + TEST_FUNC(e, events, info); + TEST_FUNC(e, events, done); + TEST_FUNC(e, events, ping); + TEST_FUNC(e, events, error); + TEST_FUNC(e, events, remove_id); + TEST_FUNC(e, events, bound_id); + TEST_FUNC(e, events, add_mem); + TEST_FUNC(e, events, remove_mem); + spa_assert_se(PW_VERSION_CORE_EVENTS == 0); + spa_assert_se(sizeof(e) == sizeof(events)); +} + +static void test_registry_abi(void) +{ + struct pw_registry_methods m; + struct pw_registry_events e; + struct { + uint32_t version; + int (*add_listener) (void *object, + struct spa_hook *listener, + const struct pw_registry_events *events, + void *data); + void * (*bind) (void *object, uint32_t id, const char *type, uint32_t version, + size_t user_data_size); + int (*destroy) (void *object, uint32_t id); + } methods = { PW_VERSION_REGISTRY_METHODS, }; + struct { + uint32_t version; + void (*global) (void *data, uint32_t id, + uint32_t permissions, const char *type, uint32_t version, + const struct spa_dict *props); + void (*global_remove) (void *data, uint32_t id); + } events = { PW_VERSION_REGISTRY_EVENTS, }; + + TEST_FUNC(m, methods, version); + TEST_FUNC(m, methods, add_listener); + TEST_FUNC(m, methods, bind); + TEST_FUNC(m, methods, destroy); + spa_assert_se(PW_VERSION_REGISTRY_METHODS == 0); + spa_assert_se(sizeof(m) == sizeof(methods)); + + TEST_FUNC(e, events, version); + TEST_FUNC(e, events, global); + TEST_FUNC(e, events, global_remove); + spa_assert_se(PW_VERSION_REGISTRY_EVENTS == 0); + spa_assert_se(sizeof(e) == sizeof(events)); +} + +static void test_module_abi(void) +{ + struct pw_module_methods m; + struct pw_module_events e; + struct { + uint32_t version; + int (*add_listener) (void *object, + struct spa_hook *listener, + const struct pw_module_events *events, + void *data); + } methods = { PW_VERSION_MODULE_METHODS, }; + struct { + uint32_t version; + void (*info) (void *data, const struct pw_module_info *info); + } events = { PW_VERSION_MODULE_EVENTS, }; + + TEST_FUNC(m, methods, version); + TEST_FUNC(m, methods, add_listener); + spa_assert_se(PW_VERSION_MODULE_METHODS == 0); + spa_assert_se(sizeof(m) == sizeof(methods)); + + TEST_FUNC(e, events, version); + TEST_FUNC(e, events, info); + spa_assert_se(PW_VERSION_MODULE_EVENTS == 0); + spa_assert_se(sizeof(e) == sizeof(events)); +} + +static void test_device_abi(void) +{ + struct pw_device_methods m; + struct pw_device_events e; + struct { + uint32_t version; + int (*add_listener) (void *object, + struct spa_hook *listener, + const struct pw_device_events *events, + void *data); + int (*subscribe_params) (void *object, uint32_t *ids, uint32_t n_ids); + int (*enum_params) (void *object, int seq, uint32_t id, + uint32_t start, uint32_t num, + const struct spa_pod *filter); + int (*set_param) (void *object, uint32_t id, uint32_t flags, + const struct spa_pod *param); + } methods = { PW_VERSION_DEVICE_METHODS, }; + struct { + uint32_t version; + void (*info) (void *data, const struct pw_device_info *info); + void (*param) (void *data, int seq, + uint32_t id, uint32_t index, uint32_t next, + const struct spa_pod *param); + } events = { PW_VERSION_DEVICE_EVENTS, }; + + TEST_FUNC(m, methods, version); + TEST_FUNC(m, methods, add_listener); + TEST_FUNC(m, methods, subscribe_params); + TEST_FUNC(m, methods, enum_params); + TEST_FUNC(m, methods, set_param); + spa_assert_se(PW_VERSION_DEVICE_METHODS == 0); + spa_assert_se(sizeof(m) == sizeof(methods)); + + TEST_FUNC(e, events, version); + TEST_FUNC(e, events, info); + TEST_FUNC(e, events, param); + spa_assert_se(PW_VERSION_DEVICE_EVENTS == 0); + spa_assert_se(sizeof(e) == sizeof(events)); +} + +static void test_node_abi(void) +{ + struct pw_node_methods m; + struct pw_node_events e; + struct { + uint32_t version; + int (*add_listener) (void *object, + struct spa_hook *listener, + const struct pw_node_events *events, + void *data); + int (*subscribe_params) (void *object, uint32_t *ids, uint32_t n_ids); + int (*enum_params) (void *object, int seq, uint32_t id, + uint32_t start, uint32_t num, const struct spa_pod *filter); + int (*set_param) (void *object, uint32_t id, uint32_t flags, + const struct spa_pod *param); + int (*send_command) (void *object, const struct spa_command *command); + } methods = { PW_VERSION_NODE_METHODS, }; + struct { + uint32_t version; + void (*info) (void *data, const struct pw_node_info *info); + void (*param) (void *data, int seq, + uint32_t id, uint32_t index, uint32_t next, + const struct spa_pod *param); + } events = { PW_VERSION_NODE_EVENTS, }; + + TEST_FUNC(m, methods, version); + TEST_FUNC(m, methods, add_listener); + TEST_FUNC(m, methods, subscribe_params); + TEST_FUNC(m, methods, enum_params); + TEST_FUNC(m, methods, set_param); + TEST_FUNC(m, methods, send_command); + spa_assert_se(PW_VERSION_NODE_METHODS == 0); + spa_assert_se(sizeof(m) == sizeof(methods)); + + TEST_FUNC(e, events, version); + TEST_FUNC(e, events, info); + TEST_FUNC(e, events, param); + spa_assert_se(PW_VERSION_NODE_EVENTS == 0); + spa_assert_se(sizeof(e) == sizeof(events)); +} + +static void test_port_abi(void) +{ + struct pw_port_methods m; + struct pw_port_events e; + struct { + uint32_t version; + int (*add_listener) (void *object, + struct spa_hook *listener, + const struct pw_port_events *events, + void *data); + int (*subscribe_params) (void *object, uint32_t *ids, uint32_t n_ids); + int (*enum_params) (void *object, int seq, uint32_t id, + uint32_t start, uint32_t num, const struct spa_pod *filter); + } methods = { PW_VERSION_PORT_METHODS, }; + struct { + uint32_t version; + void (*info) (void *data, const struct pw_port_info *info); + void (*param) (void *data, int seq, + uint32_t id, uint32_t index, uint32_t next, + const struct spa_pod *param); + } events = { PW_VERSION_PORT_EVENTS, }; + + TEST_FUNC(m, methods, version); + TEST_FUNC(m, methods, add_listener); + TEST_FUNC(m, methods, enum_params); + spa_assert_se(PW_VERSION_PORT_METHODS == 0); + spa_assert_se(sizeof(m) == sizeof(methods)); + + TEST_FUNC(e, events, version); + TEST_FUNC(e, events, info); + TEST_FUNC(e, events, param); + spa_assert_se(PW_VERSION_PORT_EVENTS == 0); + spa_assert_se(sizeof(e) == sizeof(events)); +} + +static void test_factory_abi(void) +{ + struct pw_factory_methods m; + struct pw_factory_events e; + struct { + uint32_t version; + int (*add_listener) (void *object, + struct spa_hook *listener, + const struct pw_factory_events *events, + void *data); + } methods = { PW_VERSION_FACTORY_METHODS, }; + struct { + uint32_t version; + void (*info) (void *data, const struct pw_factory_info *info); + } events = { PW_VERSION_FACTORY_EVENTS, }; + + TEST_FUNC(m, methods, version); + TEST_FUNC(m, methods, add_listener); + spa_assert_se(PW_VERSION_FACTORY_METHODS == 0); + spa_assert_se(sizeof(m) == sizeof(methods)); + + TEST_FUNC(e, events, version); + TEST_FUNC(e, events, info); + spa_assert_se(PW_VERSION_FACTORY_EVENTS == 0); + spa_assert_se(sizeof(e) == sizeof(events)); +} + +static void test_client_abi(void) +{ + struct pw_client_methods m; + struct pw_client_events e; + struct { + uint32_t version; + int (*add_listener) (void *object, + struct spa_hook *listener, + const struct pw_client_events *events, + void *data); + int (*error) (void *object, uint32_t id, int res, const char *error); + int (*update_properties) (void *object, const struct spa_dict *props); + int (*get_permissions) (void *object, uint32_t index, uint32_t num); + int (*update_permissions) (void *object, uint32_t n_permissions, + const struct pw_permission *permissions); + } methods = { PW_VERSION_CLIENT_METHODS, }; + struct { + uint32_t version; + void (*info) (void *data, const struct pw_client_info *info); + void (*permissions) (void *data, uint32_t index, + uint32_t n_permissions, const struct pw_permission *permissions); + } events = { PW_VERSION_CLIENT_EVENTS, }; + + TEST_FUNC(m, methods, version); + TEST_FUNC(m, methods, add_listener); + TEST_FUNC(m, methods, error); + TEST_FUNC(m, methods, update_properties); + TEST_FUNC(m, methods, get_permissions); + TEST_FUNC(m, methods, update_permissions); + spa_assert_se(PW_VERSION_CLIENT_METHODS == 0); + spa_assert_se(sizeof(m) == sizeof(methods)); + + TEST_FUNC(e, events, version); + TEST_FUNC(e, events, info); + TEST_FUNC(e, events, permissions); + spa_assert_se(PW_VERSION_CLIENT_EVENTS == 0); + spa_assert_se(sizeof(e) == sizeof(events)); +} + +static void test_link_abi(void) +{ + struct pw_link_methods m; + struct pw_link_events e; + struct { + uint32_t version; + int (*add_listener) (void *object, + struct spa_hook *listener, + const struct pw_link_events *events, + void *data); + } methods = { PW_VERSION_LINK_METHODS, }; + struct { + uint32_t version; + void (*info) (void *data, const struct pw_link_info *info); + } events = { PW_VERSION_LINK_EVENTS, }; + + TEST_FUNC(m, methods, version); + TEST_FUNC(m, methods, add_listener); + spa_assert_se(PW_VERSION_LINK_METHODS == 0); + spa_assert_se(sizeof(m) == sizeof(methods)); + + TEST_FUNC(e, events, version); + TEST_FUNC(e, events, info); + spa_assert_se(PW_VERSION_LINK_EVENTS == 0); + spa_assert_se(sizeof(e) == sizeof(events)); +} + +int main(int argc, char *argv[]) +{ + pw_init(&argc, &argv); + + test_core_abi(); + test_registry_abi(); + test_module_abi(); + test_device_abi(); + test_node_abi(); + test_port_abi(); + test_factory_abi(); + test_client_abi(); + test_link_abi(); + + return 0; +} diff --git a/src/tests/test-stream.c b/src/tests/test-stream.c new file mode 100644 index 0000000..1e54fed --- /dev/null +++ b/src/tests/test-stream.c @@ -0,0 +1,257 @@ +/* PipeWire + * + * Copyright © 2019 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <pipewire/pipewire.h> +#include <pipewire/main-loop.h> +#include <pipewire/stream.h> + +#include <spa/utils/string.h> + +#define TEST_FUNC(a,b,func) \ +do { \ + a.func = b.func; \ + spa_assert_se(SPA_PTRDIFF(&a.func, &a) == SPA_PTRDIFF(&b.func, &b)); \ +} while(0) + +static void test_abi(void) +{ + static const struct { + uint32_t version; + void (*destroy) (void *data); + void (*state_changed) (void *data, enum pw_stream_state old, + enum pw_stream_state state, const char *error); + void (*control_info) (void *data, uint32_t id, const struct pw_stream_control *control); + void (*io_changed) (void *data, uint32_t id, void *area, uint32_t size); + void (*param_changed) (void *data, uint32_t id, const struct spa_pod *param); + void (*add_buffer) (void *data, struct pw_buffer *buffer); + void (*remove_buffer) (void *data, struct pw_buffer *buffer); + void (*process) (void *data); + void (*drained) (void *data); + void (*command) (void *data, const struct spa_command *command); + void (*trigger_done) (void *data); + } test = { PW_VERSION_STREAM_EVENTS, NULL }; + + struct pw_stream_events ev; + + TEST_FUNC(ev, test, destroy); + TEST_FUNC(ev, test, state_changed); + TEST_FUNC(ev, test, control_info); + TEST_FUNC(ev, test, io_changed); + TEST_FUNC(ev, test, param_changed); + TEST_FUNC(ev, test, add_buffer); + TEST_FUNC(ev, test, remove_buffer); + TEST_FUNC(ev, test, process); + TEST_FUNC(ev, test, drained); + TEST_FUNC(ev, test, command); + TEST_FUNC(ev, test, trigger_done); + +#if defined(__x86_64__) && defined(__LP64__) + spa_assert_se(sizeof(struct pw_buffer) == 32); + spa_assert_se(sizeof(struct pw_time) == 56); +#else + fprintf(stderr, "%zd\n", sizeof(struct pw_buffer)); + fprintf(stderr, "%zd\n", sizeof(struct pw_time)); +#endif + + spa_assert_se(PW_VERSION_STREAM_EVENTS == 2); + spa_assert_se(sizeof(ev) == sizeof(test)); + + spa_assert_se(PW_STREAM_STATE_ERROR == -1); + spa_assert_se(PW_STREAM_STATE_UNCONNECTED == 0); + spa_assert_se(PW_STREAM_STATE_CONNECTING == 1); + spa_assert_se(PW_STREAM_STATE_PAUSED == 2); + spa_assert_se(PW_STREAM_STATE_STREAMING == 3); + + spa_assert_se(pw_stream_state_as_string(PW_STREAM_STATE_ERROR) != NULL); + spa_assert_se(pw_stream_state_as_string(PW_STREAM_STATE_UNCONNECTED) != NULL); + spa_assert_se(pw_stream_state_as_string(PW_STREAM_STATE_CONNECTING) != NULL); + spa_assert_se(pw_stream_state_as_string(PW_STREAM_STATE_PAUSED) != NULL); + spa_assert_se(pw_stream_state_as_string(PW_STREAM_STATE_STREAMING) != NULL); +} + +static void stream_destroy_error(void *data) +{ + spa_assert_not_reached(); +} +static void stream_state_changed_error(void *data, enum pw_stream_state old, + enum pw_stream_state state, const char *error) +{ + spa_assert_not_reached(); +} +static void stream_io_changed_error(void *data, uint32_t id, void *area, uint32_t size) +{ + spa_assert_not_reached(); +} +static void stream_param_changed_error(void *data, uint32_t id, const struct spa_pod *format) +{ + spa_assert_not_reached(); +} +static void stream_add_buffer_error(void *data, struct pw_buffer *buffer) +{ + spa_assert_not_reached(); +} +static void stream_remove_buffer_error(void *data, struct pw_buffer *buffer) +{ + spa_assert_not_reached(); +} +static void stream_process_error(void *data) +{ + spa_assert_not_reached(); +} +static void stream_drained_error(void *data) +{ + spa_assert_not_reached(); +} + +static const struct pw_stream_events stream_events_error = +{ + PW_VERSION_STREAM_EVENTS, + .destroy = stream_destroy_error, + .state_changed = stream_state_changed_error, + .io_changed = stream_io_changed_error, + .param_changed = stream_param_changed_error, + .add_buffer = stream_add_buffer_error, + .remove_buffer = stream_remove_buffer_error, + .process = stream_process_error, + .drained = stream_drained_error +}; + +static int destroy_count = 0; +static void stream_destroy_count(void *data) +{ + destroy_count++; +} +static void test_create(void) +{ + struct pw_main_loop *loop; + struct pw_context *context; + struct pw_core *core; + struct pw_stream *stream; + struct pw_stream_events stream_events = stream_events_error; + struct spa_hook listener = { 0, }; + const char *error = NULL; + struct pw_time tm; + + loop = pw_main_loop_new(NULL); + context = pw_context_new(pw_main_loop_get_loop(loop), NULL, 12); + spa_assert_se(context != NULL); + core = pw_context_connect_self(context, NULL, 0); + spa_assert_se(core != NULL); + stream = pw_stream_new(core, "test", NULL); + spa_assert_se(stream != NULL); + pw_stream_add_listener(stream, &listener, &stream_events, stream); + + /* check state */ + spa_assert_se(pw_stream_get_state(stream, &error) == PW_STREAM_STATE_UNCONNECTED); + spa_assert_se(error == NULL); + /* check name */ + spa_assert_se(spa_streq(pw_stream_get_name(stream), "test")); + + /* check id, only when connected */ + spa_assert_se(pw_stream_get_node_id(stream) == SPA_ID_INVALID); + + spa_assert_se(pw_stream_get_time_n(stream, &tm, sizeof(tm)) == 0); + spa_assert_se(tm.now == 0); + spa_assert_se(tm.rate.num == 0); + spa_assert_se(tm.rate.denom == 0); + spa_assert_se(tm.ticks == 0); + spa_assert_se(tm.delay == 0); + spa_assert_se(tm.queued == 0); + spa_assert_se(tm.buffered == 0); + + spa_assert_se(pw_stream_dequeue_buffer(stream) == NULL); + + /* check destroy */ + destroy_count = 0; + stream_events.destroy = stream_destroy_count; + pw_stream_destroy(stream); + spa_assert_se(destroy_count == 1); + + pw_context_destroy(context); + pw_main_loop_destroy(loop); +} + +static void test_properties(void) +{ + struct pw_main_loop *loop; + struct pw_context *context; + struct pw_core *core; + const struct pw_properties *props; + struct pw_stream *stream; + struct pw_stream_events stream_events = stream_events_error; + struct spa_hook listener = { { NULL }, }; + struct spa_dict_item items[3]; + + loop = pw_main_loop_new(NULL); + context = pw_context_new(pw_main_loop_get_loop(loop), NULL, 12); + spa_assert_se(context != NULL); + core = pw_context_connect_self(context, NULL, 0); + spa_assert_se(core != NULL); + stream = pw_stream_new(core, "test", + pw_properties_new("foo", "bar", + "biz", "fuzz", + NULL)); + spa_assert_se(stream != NULL); + pw_stream_add_listener(stream, &listener, &stream_events, stream); + + props = pw_stream_get_properties(stream); + spa_assert_se(props != NULL); + spa_assert_se(spa_streq(pw_properties_get(props, "foo"), "bar")); + spa_assert_se(spa_streq(pw_properties_get(props, "biz"), "fuzz")); + spa_assert_se(pw_properties_get(props, "buzz") == NULL); + + /* remove foo */ + items[0] = SPA_DICT_ITEM_INIT("foo", NULL); + /* change biz */ + items[1] = SPA_DICT_ITEM_INIT("biz", "buzz"); + /* add buzz */ + items[2] = SPA_DICT_ITEM_INIT("buzz", "frizz"); + pw_stream_update_properties(stream, &SPA_DICT_INIT(items, 3)); + + spa_assert_se(props == pw_stream_get_properties(stream)); + spa_assert_se(pw_properties_get(props, "foo") == NULL); + spa_assert_se(spa_streq(pw_properties_get(props, "biz"), "buzz")); + spa_assert_se(spa_streq(pw_properties_get(props, "buzz"), "frizz")); + + /* check destroy */ + destroy_count = 0; + stream_events.destroy = stream_destroy_count; + pw_context_destroy(context); + spa_assert_se(destroy_count == 1); + + pw_main_loop_destroy(loop); +} + +int main(int argc, char *argv[]) +{ + pw_init(&argc, &argv); + + test_abi(); + test_create(); + test_properties(); + + pw_deinit(); + + return 0; +} |