modules: use safer pod parsing for control sequence

This commit is contained in:
Wim Taymans 2025-07-31 11:50:11 +02:00
parent 28510f3117
commit 8495bffee5
5 changed files with 73 additions and 80 deletions

View file

@ -317,21 +317,21 @@ static inline void fix_midi_event(uint8_t *data, size_t size)
static void midi_to_ffado(struct port *p, float *src, uint32_t n_samples)
{
struct spa_pod *pod;
struct spa_pod_sequence *seq;
struct spa_pod_control *c;
struct spa_pod_parser parser;
struct spa_pod_frame frame;
struct spa_pod_sequence seq;
struct spa_pod_control c;
const void *seq_body, *c_body;
uint32_t i, index = 0, unhandled = 0;
uint32_t *dst = p->buffer;
if (src == NULL)
return;
if ((pod = spa_pod_from_data(src, n_samples * sizeof(float), 0, n_samples * sizeof(float))) == NULL)
spa_pod_parser_init_from_data(&parser, src, n_samples * sizeof(float),
0, n_samples * sizeof(float));
if (spa_pod_parser_push_sequence_body(&parser, &frame, &seq, &seq_body) < 0)
return;
if (!spa_pod_is_sequence(pod))
return;
seq = (struct spa_pod_sequence*)pod;
clear_port_buffer(p, n_samples);
@ -342,20 +342,19 @@ static void midi_to_ffado(struct port *p, float *src, uint32_t n_samples)
}
p->event_pos = 0;
SPA_POD_SEQUENCE_FOREACH(seq, c) {
while (spa_pod_parser_get_control_body(&parser, &c, &c_body) >= 0) {
uint8_t data[16];
int j, size;
if (c->type != SPA_CONTROL_UMP)
if (c.type != SPA_CONTROL_UMP)
continue;
size = spa_ump_to_midi(SPA_POD_BODY(&c->value),
SPA_POD_BODY_SIZE(&c->value), data, sizeof(data));
size = spa_ump_to_midi(c_body, c.value.size, data, sizeof(data));
if (size <= 0)
continue;
if (index < c->offset)
index = SPA_ROUND_UP_N(c->offset, 8);
if (index < c.offset)
index = SPA_ROUND_UP_N(c.offset, 8);
for (j = 0; j < size; j++) {
if (index >= n_samples) {
/* keep events that don't fit for the next cycle */

View file

@ -252,9 +252,11 @@ static inline void fix_midi_event(uint8_t *data, size_t size)
static void midi_to_jack(struct impl *impl, float *dst, float *src, uint32_t n_samples)
{
struct spa_pod *pod;
struct spa_pod_sequence *seq;
struct spa_pod_control *c;
struct spa_pod_parser parser;
struct spa_pod_frame frame;
struct spa_pod_sequence seq;
struct spa_pod_control c;
const void *seq_body, *c_body;
int res;
bool in_sysex = false;
uint8_t tmp[n_samples * 4];
@ -264,21 +266,18 @@ static void midi_to_jack(struct impl *impl, float *dst, float *src, uint32_t n_s
if (src == NULL)
return;
if ((pod = spa_pod_from_data(src, n_samples * sizeof(float), 0, n_samples * sizeof(float))) == NULL)
return;
if (!spa_pod_is_sequence(pod))
spa_pod_parser_init_from_data(&parser, src, n_samples * sizeof(float),
0, n_samples * sizeof(float));
if (spa_pod_parser_push_sequence_body(&parser, &frame, &seq, &seq_body) < 0)
return;
seq = (struct spa_pod_sequence*)pod;
SPA_POD_SEQUENCE_FOREACH(seq, c) {
while (spa_pod_parser_get_control_body(&parser, &c, &c_body) >= 0) {
int size;
if (c->type != SPA_CONTROL_UMP)
if (c.type != SPA_CONTROL_UMP)
continue;
size = spa_ump_to_midi(SPA_POD_BODY(&c->value),
SPA_POD_BODY_SIZE(&c->value), &tmp[tmp_size], sizeof(tmp) - tmp_size);
size = spa_ump_to_midi(c_body, c.value.size, &tmp[tmp_size], sizeof(tmp) - tmp_size);
if (size <= 0)
continue;
@ -293,7 +292,7 @@ static void midi_to_jack(struct impl *impl, float *dst, float *src, uint32_t n_s
in_sysex = false;
if (!in_sysex) {
if ((res = jack.midi_event_write(dst, c->offset, tmp, tmp_size)) < 0)
if ((res = jack.midi_event_write(dst, c.offset, tmp, tmp_size)) < 0)
pw_log_warn("midi %p: can't write event: %s", dst,
spa_strerror(res));
tmp_size = 0;

View file

@ -307,9 +307,11 @@ static inline void n2j_midi_buffer_append(struct nj2_midi_buffer *buf,
static void midi_to_netjack2(struct netjack2_peer *peer,
struct nj2_midi_buffer *buf, float *src, uint32_t n_samples)
{
struct spa_pod *pod;
struct spa_pod_sequence *seq;
struct spa_pod_control *c;
struct spa_pod_parser parser;
struct spa_pod_frame frame;
struct spa_pod_sequence seq;
struct spa_pod_control c;
const void *seq_body, *c_body;
bool in_sysex = false;
buf->magic = MIDI_BUFFER_MAGIC;
@ -322,28 +324,24 @@ static void midi_to_netjack2(struct netjack2_peer *peer,
if (src == NULL)
return;
if ((pod = spa_pod_from_data(src, n_samples * sizeof(float),
0, n_samples * sizeof(float))) == NULL)
return;
if (!spa_pod_is_sequence(pod))
spa_pod_parser_init_from_data(&parser, src, n_samples * sizeof(float),
0, n_samples * sizeof(float));
if (spa_pod_parser_push_sequence_body(&parser, &frame, &seq, &seq_body) < 0)
return;
seq = (struct spa_pod_sequence*)pod;
SPA_POD_SEQUENCE_FOREACH(seq, c) {
while (spa_pod_parser_get_control_body(&parser, &c, &c_body) >= 0) {
int size;
uint8_t data[16];
bool was_sysex = in_sysex;
if (c->type != SPA_CONTROL_UMP)
if (c.type != SPA_CONTROL_UMP)
continue;
size = spa_ump_to_midi(SPA_POD_BODY(&c->value),
SPA_POD_BODY_SIZE(&c->value), data, sizeof(data));
size = spa_ump_to_midi(c_body, c.value.size, data, sizeof(data));
if (size <= 0)
continue;
if (c->offset >= n_samples) {
if (c.offset >= n_samples) {
buf->lost_events++;
continue;
}
@ -360,7 +358,7 @@ static void midi_to_netjack2(struct netjack2_peer *peer,
if (was_sysex)
n2j_midi_buffer_append(buf, data, size);
else
n2j_midi_buffer_write(buf, c->offset, data, size);
n2j_midi_buffer_write(buf, c.offset, data, size);
}
if (buf->write_pos > 0)
memmove(SPA_PTROFF(buf, sizeof(*buf) + buf->event_count * sizeof(struct nj2_midi_event), void),

View file

@ -56,7 +56,8 @@ static void rtp_midi_process_playback(void *data)
goto done;
/* the ringbuffer contains series of sequences, one for each
* received packet */
* received packet. This is not in shared mem so we can safely use
* the iterators here. */
SPA_POD_SEQUENCE_FOREACH((struct spa_pod_sequence*)pod, c) {
/* try to render with given delay */
uint32_t target = c->offset + impl->target_buffer;
@ -406,9 +407,10 @@ static int write_event(uint8_t *p, uint32_t buffer_size, uint32_t value, void *e
}
static void rtp_midi_flush_packets(struct impl *impl,
struct spa_pod_sequence *sequence, uint32_t timestamp, uint32_t rate)
struct spa_pod_parser *parser, uint32_t timestamp, uint32_t rate)
{
struct spa_pod_control *c;
struct spa_pod_control c;
const void *c_body;
struct rtp_header header;
struct rtp_midi_header midi_header;
struct iovec iov[3];
@ -431,20 +433,19 @@ static void rtp_midi_flush_packets(struct impl *impl,
prev_offset = len = base = 0;
max_size = impl->payload_size - sizeof(midi_header);
SPA_POD_SEQUENCE_FOREACH(sequence, c) {
while (spa_pod_parser_get_control_body(parser, &c, &c_body) >= 0) {
uint32_t delta, offset;
uint8_t event[16];
size_t size;
if (c->type != SPA_CONTROL_UMP)
if (c.type != SPA_CONTROL_UMP)
continue;
size = spa_ump_to_midi(SPA_POD_BODY(&c->value),
SPA_POD_BODY_SIZE(&c->value), event, sizeof(event));
size = spa_ump_to_midi(c_body, c.value.size, event, sizeof(event));
if (size <= 0)
continue;
offset = c->offset * impl->rate / rate;
offset = c.offset * impl->rate / rate;
if (len > 0 && (len + size > max_size ||
offset - base > impl->psamples)) {
@ -513,9 +514,11 @@ static void rtp_midi_process_capture(void *data)
struct impl *impl = data;
struct pw_buffer *buf;
struct spa_data *d;
uint32_t offs, size, timestamp, rate;
struct spa_pod *pod;
void *ptr;
uint32_t timestamp, rate;
struct spa_pod_parser parser;
struct spa_pod_frame frame;
struct spa_pod_sequence seq;
const void *seq_body;
if ((buf = pw_stream_dequeue_buffer(impl->stream)) == NULL) {
pw_log_info("Out of stream buffers: %m");
@ -523,9 +526,6 @@ static void rtp_midi_process_capture(void *data)
}
d = buf->buffer->datas;
offs = SPA_MIN(d[0].chunk->offset, d[0].maxsize);
size = SPA_MIN(d[0].chunk->size, d[0].maxsize - offs);
if (SPA_LIKELY(impl->io_position)) {
rate = impl->io_position->clock.rate.denom;
timestamp = impl->io_position->clock.position * impl->rate / rate;
@ -534,11 +534,10 @@ static void rtp_midi_process_capture(void *data)
timestamp = 0;
}
ptr = SPA_PTROFF(d[0].data, offs, void);
if ((pod = spa_pod_from_data(ptr, size, 0, size)) == NULL)
goto done;
if (!spa_pod_is_sequence(pod))
spa_pod_parser_init_from_data(&parser, d[0].data, d[0].maxsize,
d[0].chunk->offset, d[0].chunk->size);
if (spa_pod_parser_push_sequence_body(&parser, &frame, &seq, &seq_body) < 0)
goto done;
if (!impl->have_sync) {
@ -547,7 +546,7 @@ static void rtp_midi_process_capture(void *data)
impl->have_sync = true;
}
rtp_midi_flush_packets(impl, (struct spa_pod_sequence*)pod, timestamp, rate);
rtp_midi_flush_packets(impl, &parser, timestamp, rate);
done:
pw_stream_queue_buffer(impl->stream, buf);

View file

@ -48,7 +48,8 @@ static void vban_midi_process_playback(void *data)
goto done;
/* the ringbuffer contains series of sequences, one for each
* received packet */
* received packet. This is not share mem so we can use the
* iterator. */
SPA_POD_SEQUENCE_FOREACH((struct spa_pod_sequence*)pod, c) {
#if 0
/* try to render with given delay */
@ -218,9 +219,10 @@ static int vban_midi_receive(struct impl *impl, uint8_t *buffer, ssize_t len)
}
static void vban_midi_flush_packets(struct impl *impl,
struct spa_pod_sequence *sequence, uint32_t timestamp, uint32_t rate)
struct spa_pod_parser *parser, uint32_t timestamp, uint32_t rate)
{
struct spa_pod_control *c;
struct spa_pod_control c;
const void *c_body;
struct vban_header header;
struct iovec iov[2];
uint32_t len;
@ -234,15 +236,14 @@ static void vban_midi_flush_packets(struct impl *impl,
len = 0;
SPA_POD_SEQUENCE_FOREACH(sequence, c) {
while (spa_pod_parser_get_control_body(parser, &c, &c_body) >= 0) {
int size;
uint8_t event[16];
if (c->type != SPA_CONTROL_UMP)
if (c.type != SPA_CONTROL_UMP)
continue;
size = spa_ump_to_midi(SPA_POD_BODY(&c->value),
SPA_POD_BODY_SIZE(&c->value), event, sizeof(event));
size = spa_ump_to_midi(c_body, c.value.size, event, sizeof(event));
if (size <= 0)
continue;
@ -275,9 +276,11 @@ static void vban_midi_process_capture(void *data)
struct impl *impl = data;
struct pw_buffer *buf;
struct spa_data *d;
uint32_t offs, size, timestamp, rate;
struct spa_pod *pod;
void *ptr;
uint32_t timestamp, rate;
struct spa_pod_parser parser;
struct spa_pod_frame frame;
struct spa_pod_sequence seq;
const void *seq_body;
if ((buf = pw_stream_dequeue_buffer(impl->stream)) == NULL) {
pw_log_debug("Out of stream buffers: %m");
@ -285,9 +288,6 @@ static void vban_midi_process_capture(void *data)
}
d = buf->buffer->datas;
offs = SPA_MIN(d[0].chunk->offset, d[0].maxsize);
size = SPA_MIN(d[0].chunk->size, d[0].maxsize - offs);
if (SPA_LIKELY(impl->io_position)) {
rate = impl->io_position->clock.rate.denom;
timestamp = impl->io_position->clock.position * impl->rate / rate;
@ -296,11 +296,9 @@ static void vban_midi_process_capture(void *data)
timestamp = 0;
}
ptr = SPA_PTROFF(d[0].data, offs, void);
if ((pod = spa_pod_from_data(ptr, size, 0, size)) == NULL)
goto done;
if (!spa_pod_is_sequence(pod))
spa_pod_parser_init_from_data(&parser, d[0].data, d[0].maxsize,
d[0].chunk->offset, d[0].chunk->size);
if (spa_pod_parser_push_sequence_body(&parser, &frame, &seq, &seq_body) < 0)
goto done;
if (!impl->have_sync) {
@ -309,7 +307,7 @@ static void vban_midi_process_capture(void *data)
impl->have_sync = true;
}
vban_midi_flush_packets(impl, (struct spa_pod_sequence*)pod, timestamp, rate);
vban_midi_flush_packets(impl, &parser, timestamp, rate);
done:
pw_stream_queue_buffer(impl->stream, buf);