Use the DSP format for dsp formats

Use the DSP media subtype to describe DSP formats. DSP formats
don't include the rate, channels and channel position in the
format and must use the rate and duration from the position io. This
makes it possible to later change the samplerate dynamically without
having to renegotiate the graph.

The same goes for the video DSP format, which uses the io_video_size
from the io_position to get the size/stride. Set this up in the node
based on the defaults from the context.

Make it possible to define defaults in the daemon config file, such
as samplerate, quantum, video size and framerate. This is stored in
the context and used for the DSP formats.
This commit is contained in:
Wim Taymans 2020-01-09 13:40:48 +01:00
parent 5a6da7d5e1
commit 852ac043d3
20 changed files with 402 additions and 270 deletions

View file

@ -4,6 +4,15 @@
#set-prop context.data-loop.library.name.system support/libspa-support
#set-prop link.max-buffers 64
#set-prop default.clock.rate 48000
#set-prop default.clock.quantum 1024
#set-prop default.clock.min-quantum 32
#set-prop default.clock.max-quantum 8192
#set-prop default.video.width 320
#set-prop default.video.height 240
#set-prop default.video.rate.num 25
#set-prop default.video.rate.denom 1
add-spa-lib audio.convert* audioconvert/libspa-audioconvert
add-spa-lib api.alsa.* alsa/libspa-alsa
add-spa-lib api.v4l2.* v4l2/libspa-v4l2

View file

@ -26,6 +26,7 @@
#include <unistd.h>
#include <sys/mman.h>
#include <spa/utils/result.h>
#include <spa/param/video/format-utils.h>
#include <spa/param/props.h>
#include <spa/debug/format.h>
@ -34,7 +35,6 @@
#define WIDTH 640
#define HEIGHT 480
#define BPP 3
#define MAX_BUFFERS 64
@ -57,8 +57,11 @@ struct data {
struct pw_stream *stream;
struct spa_hook stream_listener;
struct spa_video_info_raw format;
struct spa_io_position *position;
struct spa_video_info format;
int32_t stride;
struct spa_rectangle size;
int counter;
SDL_Rect rect;
@ -177,10 +180,10 @@ on_process(void *_data)
src = sdata;
dst = ddata;
if (data->format.format == SPA_VIDEO_FORMAT_RGBA_F32) {
for (i = 0; i < data->format.size.height; i++) {
if (data->format.media_subtype == SPA_MEDIA_SUBTYPE_dsp) {
for (i = 0; i < data->size.height; i++) {
struct pixel *p = (struct pixel *) src;
for (j = 0; j < data->format.size.width; j++) {
for (j = 0; j < data->size.width; j++) {
dst[j * 4 + 0] = SPA_CLAMP(lrintf(p[j].r * 255.0f), 0, 255);
dst[j * 4 + 1] = SPA_CLAMP(lrintf(p[j].g * 255.0f), 0, 255);
dst[j * 4 + 2] = SPA_CLAMP(lrintf(p[j].b * 255.0f), 0, 255);
@ -191,7 +194,7 @@ on_process(void *_data)
}
}
else {
for (i = 0; i < data->format.size.height; i++) {
for (i = 0; i < data->size.height; i++) {
memcpy(dst, src, ostride);
src += sstride;
dst += dstride;
@ -229,6 +232,18 @@ static void on_stream_state_changed(void *_data, enum pw_stream_state old,
}
}
static void
on_stream_io_changed(void *_data, uint32_t id, void *area, uint32_t size)
{
struct data *data = _data;
switch (id) {
case SPA_IO_Position:
data->position = area;
break;
}
}
/* Be notified when the stream param changes. We're only looking at the
* format changes.
*
@ -258,13 +273,32 @@ on_stream_param_changed(void *_data, uint32_t id, const struct spa_pod *param)
fprintf(stderr, "got format:\n");
spa_debug_format(2, NULL, param);
/* call a helper function to parse the format for us. */
spa_format_video_raw_parse(param, &data->format);
if (spa_format_parse(param, &data->format.media_type, &data->format.media_subtype) < 0)
return;
if (data->format.format == SPA_VIDEO_FORMAT_RGBA_F32)
if (data->format.media_type != SPA_MEDIA_TYPE_video)
return;
switch (data->format.media_subtype) {
case SPA_MEDIA_SUBTYPE_raw:
/* call a helper function to parse the format for us. */
spa_format_video_raw_parse(param, &data->format.info.raw);
sdl_format = id_to_sdl_format(data->format.info.raw.format);
data->size = SPA_RECTANGLE(data->format.info.raw.size.width,
data->format.info.raw.size.height);
break;
case SPA_MEDIA_SUBTYPE_dsp:
spa_format_video_dsp_parse(param, &data->format.info.dsp);
if (data->format.info.dsp.format != SPA_VIDEO_FORMAT_DSP_F32)
return;
sdl_format = SDL_PIXELFORMAT_RGBA32;
else
sdl_format = id_to_sdl_format(data->format.format);
data->size = SPA_RECTANGLE(data->position->video.size.width,
data->position->video.size.height);
break;
default:
sdl_format = SDL_PIXELFORMAT_UNKNOWN;
break;
}
if (sdl_format == SDL_PIXELFORMAT_UNKNOWN) {
pw_stream_set_error(stream, -EINVAL, "unknown pixel format");
@ -274,15 +308,15 @@ on_stream_param_changed(void *_data, uint32_t id, const struct spa_pod *param)
data->texture = SDL_CreateTexture(data->renderer,
sdl_format,
SDL_TEXTUREACCESS_STREAMING,
data->format.size.width,
data->format.size.height);
data->size.width,
data->size.height);
SDL_LockTexture(data->texture, NULL, &d, &data->stride);
SDL_UnlockTexture(data->texture);
data->rect.x = 0;
data->rect.y = 0;
data->rect.w = data->format.size.width;
data->rect.h = data->format.size.height;
data->rect.w = data->size.width;
data->rect.h = data->size.height;
/* a SPA_TYPE_OBJECT_ParamBuffers object defines the acceptable size,
* number, stride etc of the buffers */
@ -290,7 +324,7 @@ on_stream_param_changed(void *_data, uint32_t id, const struct spa_pod *param)
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->format.size.height),
SPA_PARAM_BUFFERS_size, SPA_POD_Int(data->stride * data->size.height),
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(data->stride),
SPA_PARAM_BUFFERS_align, SPA_POD_Int(16));
@ -323,6 +357,7 @@ on_stream_param_changed(void *_data, uint32_t id, const struct spa_pod *param)
static const struct pw_stream_events stream_events = {
PW_VERSION_STREAM_EVENTS,
.state_changed = on_stream_state_changed,
.io_changed = on_stream_io_changed,
.param_changed = on_stream_param_changed,
.process = on_process,
};
@ -334,18 +369,28 @@ static int build_format(struct data *data, struct spa_pod_builder *b, const stru
SDL_GetRendererInfo(data->renderer, &info);
params[0] = sdl_build_formats(&info, b);
fprintf(stderr, "supported formats:\n");
fprintf(stderr, "supported SDL formats:\n");
spa_debug_format(2, NULL, params[0]);
return 0;
params[1] = 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),
SPA_FORMAT_VIDEO_format, SPA_POD_Id(SPA_VIDEO_FORMAT_DSP_F32));
fprintf(stderr, "supported DSP formats:\n");
spa_debug_format(2, NULL, params[1]);
return 2;
}
int main(int argc, char *argv[])
{
struct data data = { 0, };
const struct spa_pod *params[1];
const struct spa_pod *params[2];
uint8_t buffer[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
int res, n_params;
pw_init(&argc, &argv);
@ -390,19 +435,22 @@ int main(int argc, char *argv[])
/* build the extra parameters to connect with. To connect, we can provide
* a list of supported formats. We use a builder that writes the param
* object to the stack. */
build_format(&data, &b, params);
n_params = build_format(&data, &b, params);
/* now connect the stream, we need a direction (input/output),
* an optional target node to connect to, some flags and parameters
*/
pw_stream_connect(data.stream,
if ((res = pw_stream_connect(data.stream,
PW_DIRECTION_INPUT,
data.path ? (uint32_t)atoi(data.path) : SPA_ID_INVALID,
PW_STREAM_FLAG_AUTOCONNECT | /* try to automatically connect this stream */
PW_STREAM_FLAG_INACTIVE | /* we will activate ourselves */
PW_STREAM_FLAG_EXCLUSIVE | /* require exclusive access */
PW_STREAM_FLAG_MAP_BUFFERS, /* mmap the buffer data for us */
params, 1); /* extra parameters, see above */
params, n_params)) /* extra parameters, see above */ < 0) {
fprintf(stderr, "can't connect: %s\n", spa_strerror(res));
return -1;
}
/* do things until we quit the mainloop */
pw_main_loop_run(data.loop);

View file

@ -43,6 +43,16 @@
#define NAME "context"
#define DEFAULT_CLOCK_RATE 48000u
#define DEFAULT_CLOCK_QUANTUM 1024u
#define DEFAULT_CLOCK_MIN_QUANTUM 32u
#define DEFAULT_CLOCK_MAX_QUANTUM 8192u
#define DEFAULT_VIDEO_WIDTH 640
#define DEFAULT_VIDEO_HEIGHT 480
#define DEFAULT_VIDEO_RATE_NUM 25u
#define DEFAULT_VIDEO_RATE_DENOM 1u
/** \cond */
struct impl {
struct pw_context this;
@ -109,6 +119,29 @@ static void fill_properties(struct pw_context *context)
pw_properties_set(properties, PW_KEY_CORE_NAME, context->core->info.name);
}
static uint32_t get_default(struct pw_properties *properties, const char *name, uint32_t def)
{
uint32_t val;
const char *str;
if ((str = pw_properties_get(properties, name)) != NULL)
val = atoi(str);
else
val = def;
return val;
}
static void fill_defaults(struct pw_context *this)
{
struct pw_properties *p = this->properties;
this->defaults.clock_rate = get_default(p, "default.clock.rate", DEFAULT_CLOCK_RATE);
this->defaults.clock_quantum = get_default(p, "default.clock.quantum", DEFAULT_CLOCK_QUANTUM);
this->defaults.clock_min_quantum = get_default(p, "default.clock.min-quantum", DEFAULT_CLOCK_MIN_QUANTUM);
this->defaults.clock_max_quantum = get_default(p, "default.clock.max-quantum", DEFAULT_CLOCK_MAX_QUANTUM);
this->defaults.video_size.width = get_default(p, "default.video.width", DEFAULT_VIDEO_WIDTH);
this->defaults.video_size.height = get_default(p, "default.video.height", DEFAULT_VIDEO_HEIGHT);
this->defaults.video_rate.num = get_default(p, "default.video.rate.num", DEFAULT_VIDEO_RATE_NUM);
this->defaults.video_rate.denom = get_default(p, "default.video.rate.denom", DEFAULT_VIDEO_RATE_DENOM);
}
/** Create a new context object
*
@ -154,6 +187,8 @@ struct pw_context *pw_context_new(struct pw_loop *main_loop,
this->properties = properties;
fill_defaults(this);
pr = pw_properties_copy(properties);
if ((str = pw_properties_get(pr, "context.data-loop." PW_KEY_LIBRARY_NAME_SYSTEM)))
pw_properties_set(pr, PW_KEY_LIBRARY_NAME_SYSTEM, str);
@ -716,11 +751,13 @@ static int collect_nodes(struct pw_impl_node *driver)
quantum = min_quantum;
if (quantum == 0)
quantum = DEFAULT_QUANTUM;
quantum = driver->context->defaults.clock_quantum;
/* for now, we try to limit the latency between min and default, We can
* go to max but we should really only do this when in power save mode */
driver->quantum_current = SPA_CLAMP(quantum, MIN_QUANTUM, DEFAULT_QUANTUM);
driver->quantum_current = SPA_CLAMP(quantum,
driver->context->defaults.clock_min_quantum,
driver->context->defaults.clock_quantum);
return 0;
}
@ -779,7 +816,8 @@ int pw_context_recalc_graph(struct pw_context *context)
if (target != NULL) {
if (n->quantum_size > 0 && n->quantum_size < target->quantum_current)
target->quantum_current = SPA_MAX(MIN_QUANTUM, n->quantum_size);
target->quantum_current =
SPA_MAX(context->defaults.clock_min_quantum, n->quantum_size);
}
pw_impl_node_set_driver(n, target);
pw_impl_node_set_state(n, target && n->active ?

View file

@ -1266,11 +1266,8 @@ static void add_audio_dsp_port_params(struct filter *impl, struct port *port)
spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_audio),
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
SPA_FORMAT_AUDIO_format, SPA_POD_Id(SPA_AUDIO_FORMAT_F32P),
SPA_FORMAT_AUDIO_rate, SPA_POD_CHOICE_RANGE_Int(
48000, 1, INT32_MAX),
SPA_FORMAT_AUDIO_channels, SPA_POD_Int(1)));
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_dsp),
SPA_FORMAT_AUDIO_format, SPA_POD_Id(SPA_AUDIO_FORMAT_DSP_F32)));
spa_pod_builder_init(&b, buffer, sizeof(buffer));
add_param(impl, port, SPA_PARAM_Buffers,
@ -1297,16 +1294,8 @@ static void add_video_dsp_port_params(struct filter *impl, struct port *port)
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),
SPA_FORMAT_VIDEO_format, SPA_POD_Id(SPA_VIDEO_FORMAT_RGBA_F32),
SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(
&SPA_RECTANGLE(320, 240),
&SPA_RECTANGLE(1,1),
&SPA_RECTANGLE(INT32_MAX, INT32_MAX)),
SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(
&SPA_FRACTION(25,1),
&SPA_FRACTION(0,1),
&SPA_FRACTION(INT32_MAX,1))));
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_dsp),
SPA_FORMAT_VIDEO_format, SPA_POD_Id(SPA_VIDEO_FORMAT_DSP_F32)));
}
SPA_EXPORT

View file

@ -908,12 +908,17 @@ static void reset_segment(struct spa_io_segment *seg)
seg->rate = 1.0;
}
static void reset_position(struct spa_io_position *pos)
static void reset_position(struct pw_impl_node *this, struct spa_io_position *pos)
{
uint32_t i;
struct defaults *def = &this->context->defaults;
pos->clock.rate = SPA_FRACTION(1, 48000);
pos->clock.duration = DEFAULT_QUANTUM;
pos->clock.rate = SPA_FRACTION(1, def->clock_rate);
pos->clock.duration = def->clock_quantum;
pos->video.flags = SPA_IO_VIDEO_SIZE_VALID;
pos->video.size = def->video_size;
pos->video.stride = pos->video.size.width * 16;
pos->video.framerate = def->video_rate;
pos->offset = INT64_MIN;
pos->n_segments = 1;
@ -1008,7 +1013,7 @@ struct pw_impl_node *pw_context_create_node(struct pw_context *context,
this->rt.target.data = this;
this->rt.driver_target.signal = process_node;
reset_position(&this->rt.activation->position);
reset_position(this, &this->rt.activation->position);
this->rt.activation->sync_timeout = 5 * SPA_NSEC_PER_SEC;
this->rt.activation->sync_left = 0;

View file

@ -502,13 +502,13 @@ static int setup_mixer(struct pw_impl_port *port, const struct spa_pod *param)
switch (media_type) {
case SPA_MEDIA_TYPE_audio:
switch (media_subtype) {
case SPA_MEDIA_SUBTYPE_raw:
case SPA_MEDIA_SUBTYPE_dsp:
{
struct spa_audio_info_raw info;
if ((res = spa_format_audio_raw_parse(param, &info)) < 0)
struct spa_audio_info_dsp info;
if ((res = spa_format_audio_dsp_parse(param, &info)) < 0)
return res;
if (info.format != SPA_AUDIO_FORMAT_F32P || info.channels != 1)
if (info.format != SPA_AUDIO_FORMAT_DSP_F32)
return -ENOTSUP;
fallback_lib = "audiomixer/libspa-audiomixer";

View file

@ -48,9 +48,14 @@ struct ucred {
#define spa_debug pw_log_trace
#endif
#define DEFAULT_QUANTUM 1024u
#define MIN_QUANTUM 32u
#define MAX_QUANTUM 8192u
struct defaults {
uint32_t clock_rate;
uint32_t clock_quantum;
uint32_t clock_min_quantum;
uint32_t clock_max_quantum;
struct spa_rectangle video_size;
struct spa_fraction video_rate;
};
#define MAX_PARAMS 32
@ -228,6 +233,8 @@ struct pw_context {
struct pw_properties *properties; /**< properties of the context */
struct defaults defaults; /**< default parameters */
struct pw_mempool *pool; /**< global memory pool */
struct pw_map globals; /**< map of globals */