examples: count the params as we add them

Count the params as we add them to the param arrays and use that to
update the stream params instead of using hardcoded indexes and sizes.

This makes it easier to add params and it also revealed a miscounted
param.
This commit is contained in:
Wim Taymans 2025-09-19 13:07:19 +02:00
parent 83242a5c3c
commit b57c6d3729
15 changed files with 115 additions and 89 deletions

View file

@ -238,6 +238,7 @@ on_stream_param_changed(void *_data, uint32_t id, const struct spa_pod *param)
uint8_t params_buffer[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
const struct spa_pod *params[5];
uint32_t n_params = 0;
if (param != NULL && id == SPA_PARAM_Tag) {
spa_debug_pod(0, NULL, param);
@ -250,38 +251,38 @@ on_stream_param_changed(void *_data, uint32_t id, const struct spa_pod *param)
data->stride = SPA_ROUND_UP_N(data->position->video.size.width * BPP, 4);
params[0] = spa_pod_builder_add_object(&b,
params[n_params++] = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(8, 2, MAX_BUFFERS),
SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
SPA_PARAM_BUFFERS_size, SPA_POD_Int(data->stride * data->position->video.size.height),
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(data->stride));
params[1] = spa_pod_builder_add_object(&b,
params[n_params++] = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header),
SPA_PARAM_META_size, SPA_POD_Int(sizeof(struct spa_meta_header)));
params[2] = spa_pod_builder_add_object(&b,
params[n_params++] = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoDamage),
SPA_PARAM_META_size, SPA_POD_CHOICE_RANGE_Int(
sizeof(struct spa_meta_region) * 16,
sizeof(struct spa_meta_region) * 1,
sizeof(struct spa_meta_region) * 16));
params[3] = spa_pod_builder_add_object(&b,
params[n_params++] = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoCrop),
SPA_PARAM_META_size, SPA_POD_Int(sizeof(struct spa_meta_region)));
#define CURSOR_META_SIZE(w,h) (sizeof(struct spa_meta_cursor) + \
sizeof(struct spa_meta_bitmap) + w * h * CURSOR_BPP)
params[4] = spa_pod_builder_add_object(&b,
params[n_params++] = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Cursor),
SPA_PARAM_META_size, SPA_POD_Int(
CURSOR_META_SIZE(CURSOR_WIDTH,CURSOR_HEIGHT)));
pw_stream_update_params(stream, params, 5);
pw_stream_update_params(stream, params, n_params);
}
static void
@ -309,6 +310,7 @@ int main(int argc, char *argv[])
{
struct data data = { 0, };
const struct spa_pod *params[2];
uint32_t n_params = 0;
uint8_t buffer[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
@ -335,7 +337,7 @@ int main(int argc, char *argv[])
PW_KEY_MEDIA_CLASS, "Video/Source",
NULL));
params[0] = spa_pod_builder_add_object(&b,
params[n_params++] = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_dsp),
@ -348,7 +350,7 @@ int main(int argc, char *argv[])
spa_tag_build_start(&b, &f, SPA_PARAM_Tag, SPA_DIRECTION_OUTPUT);
items[0] = SPA_DICT_ITEM_INIT("my-tag-key", "my-special-tag-value");
spa_tag_build_add_dict(&b, &SPA_DICT_INIT(items, 1));
params[1] = spa_tag_build_end(&b, &f);
params[n_params++] = spa_tag_build_end(&b, &f);
}
pw_stream_add_listener(data.stream,
@ -361,7 +363,7 @@ int main(int argc, char *argv[])
PW_ID_ANY,
PW_STREAM_FLAG_DRIVER |
PW_STREAM_FLAG_MAP_BUFFERS,
params, 2);
params, n_params);
pw_main_loop_run(data.loop);