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

@ -190,6 +190,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[1];
uint32_t n_params = 0;
Uint32 sdl_format;
void *d;
@ -234,7 +235,7 @@ on_stream_param_changed(void *_data, uint32_t id, const struct spa_pod *param)
/* a SPA_TYPE_OBJECT_ParamBuffers object defines the acceptable size,
* number, stride etc of the buffers */
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),
@ -243,7 +244,7 @@ on_stream_param_changed(void *_data, uint32_t id, const struct spa_pod *param)
SPA_PARAM_BUFFERS_dataType, SPA_POD_CHOICE_FLAGS_Int((1<<SPA_DATA_MemPtr)));
/* we are done */
pw_stream_update_params(stream, params, 1);
pw_stream_update_params(stream, params, n_params);
}
/* these are the stream events we listen for */
@ -256,15 +257,16 @@ static const struct pw_stream_events stream_events = {
static int build_format(struct data *data, struct spa_pod_builder *b, const struct spa_pod **params)
{
uint32_t n_params = 0;
SDL_RendererInfo info;
SDL_GetRendererInfo(data->renderer, &info);
params[0] = sdl_build_formats(&info, b);
params[n_params++] = sdl_build_formats(&info, b);
fprintf(stderr, "supported SDL formats:\n");
spa_debug_format(2, NULL, params[0]);
return 1;
return n_params;
}
static int reneg_format(struct data *data)
@ -272,6 +274,7 @@ static int reneg_format(struct data *data)
uint8_t buffer[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
const struct spa_pod *params[2];
uint32_t n_params = 0;
int32_t width, height;
if (data->format.info.raw.format == 0)
@ -281,7 +284,7 @@ static int reneg_format(struct data *data)
height = data->counter & 1 ? 240 : 480;
fprintf(stderr, "renegotiate to %dx%d:\n", width, height);
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_raw),
@ -289,7 +292,7 @@ static int reneg_format(struct data *data)
SPA_FORMAT_VIDEO_size, SPA_POD_Rectangle(&SPA_RECTANGLE(width, height)),
SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&data->format.info.raw.framerate));
pw_stream_update_params(data->stream, params, 1);
pw_stream_update_params(data->stream, params, n_params);
data->counter++;
return 0;
@ -300,16 +303,17 @@ static int reneg_buffers(struct data *data)
uint8_t buffer[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
const struct spa_pod *params[2];
uint32_t n_params = 0;
fprintf(stderr, "renegotiate buffers\n");
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->size.height),
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(data->stride));
pw_stream_update_params(data->stream, params, 1);
pw_stream_update_params(data->stream, params, n_params);
data->counter++;
return 0;