From bcc052f5f121923dfeed4aa5d63200babed9faaf Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 10 Mar 2023 17:33:22 +0100 Subject: [PATCH] module-rtp: move stream init to specific media types Move the stream function setup to a stream specific method. Keep a separate stream format, that can be different later from the rtp format once we add encoding. Rename some methods to make them more unique. --- src/modules/module-rtp/audio.c | 23 ++++++++++++++------- src/modules/module-rtp/midi.c | 25 +++++++++++++++-------- src/modules/module-rtp/stream.c | 36 +++++++++++++++------------------ 3 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/modules/module-rtp/audio.c b/src/modules/module-rtp/audio.c index 452aa28ac..f561d8147 100644 --- a/src/modules/module-rtp/audio.c +++ b/src/modules/module-rtp/audio.c @@ -2,7 +2,7 @@ /* SPDX-FileCopyrightText: Copyright © 2022 Wim Taymans */ /* SPDX-License-Identifier: MIT */ -static void process_audio_playback(void *data) +static void rtp_audio_process_playback(void *data) { struct impl *impl = data; struct pw_buffer *buf; @@ -94,7 +94,7 @@ static void process_audio_playback(void *data) pw_stream_queue_buffer(impl->stream, buf); } -static int receive_rtp_audio(struct impl *impl, uint8_t *buffer, ssize_t len) +static int rtp_audio_receive(struct impl *impl, uint8_t *buffer, ssize_t len) { struct rtp_header *hdr; ssize_t hlen, plen; @@ -202,13 +202,12 @@ set_iovec(struct spa_ringbuffer *rbuf, void *buffer, uint32_t size, iov[1].iov_base = buffer; } -static void flush_audio_packets(struct impl *impl) +static void rtp_audio_flush_packets(struct impl *impl) { - int32_t avail; + int32_t avail, tosend; uint32_t stride, timestamp; struct iovec iov[3]; struct rtp_header header; - int32_t tosend; avail = spa_ringbuffer_get_read_index(&impl->ring, ×tamp); tosend = impl->psamples; @@ -246,7 +245,7 @@ static void flush_audio_packets(struct impl *impl) spa_ringbuffer_read_update(&impl->ring, timestamp); } -static void process_audio_capture(void *data) +static void rtp_audio_process_capture(void *data) { struct impl *impl = data; struct pw_buffer *buf; @@ -299,5 +298,15 @@ static void process_audio_capture(void *data) pw_stream_queue_buffer(impl->stream, buf); - flush_audio_packets(impl); + rtp_audio_flush_packets(impl); +} + +static int rtp_audio_init(struct impl *impl, enum spa_direction direction) +{ + if (direction == SPA_DIRECTION_INPUT) + impl->stream_events.process = rtp_audio_process_capture; + else + impl->stream_events.process = rtp_audio_process_playback; + impl->receive_rtp = rtp_audio_receive; + return 0; } diff --git a/src/modules/module-rtp/midi.c b/src/modules/module-rtp/midi.c index 1eda8fd2a..02c43314b 100644 --- a/src/modules/module-rtp/midi.c +++ b/src/modules/module-rtp/midi.c @@ -2,7 +2,7 @@ /* SPDX-FileCopyrightText: Copyright © 2022 Wim Taymans */ /* SPDX-License-Identifier: MIT */ -static void process_midi_playback(void *data) +static void rtp_midi_process_playback(void *data) { struct impl *impl = data; struct pw_buffer *buf; @@ -150,7 +150,7 @@ static double get_time(struct impl *impl) return t; } -static int receive_midi(struct impl *impl, uint8_t *packet, uint32_t timestamp, +static int rtp_midi_receive_midi(struct impl *impl, uint8_t *packet, uint32_t timestamp, uint16_t seq, uint32_t payload_offset, uint32_t plen) { uint32_t write; @@ -268,11 +268,10 @@ static int receive_midi(struct impl *impl, uint8_t *packet, uint32_t timestamp, write += b.state.offset; spa_ringbuffer_write_update(&impl->ring, write); - return 0; } -static int receive_rtp_midi(struct impl *impl, uint8_t *buffer, ssize_t len) +static int rtp_midi_receive(struct impl *impl, uint8_t *buffer, ssize_t len) { struct rtp_header *hdr; ssize_t hlen; @@ -308,7 +307,7 @@ static int receive_rtp_midi(struct impl *impl, uint8_t *buffer, ssize_t len) impl->receiving = true; - return receive_midi(impl, buffer, timestamp, seq, hlen, len); + return rtp_midi_receive_midi(impl, buffer, timestamp, seq, hlen, len); short_packet: pw_log_warn("short packet received"); @@ -347,7 +346,7 @@ static int write_event(uint8_t *p, uint32_t value, void *ev, uint32_t size) return count + size; } -static void flush_midi_packets(struct impl *impl, +static void rtp_midi_flush_packets(struct impl *impl, struct spa_pod_sequence *sequence, uint32_t timestamp, uint32_t rate) { struct spa_pod_control *c; @@ -442,7 +441,7 @@ static void flush_midi_packets(struct impl *impl, } } -static void process_midi_capture(void *data) +static void rtp_midi_process_capture(void *data) { struct impl *impl = data; struct pw_buffer *buf; @@ -481,8 +480,18 @@ static void process_midi_capture(void *data) impl->have_sync = true; } - flush_midi_packets(impl, (struct spa_pod_sequence*)pod, timestamp, rate); + rtp_midi_flush_packets(impl, (struct spa_pod_sequence*)pod, timestamp, rate); done: pw_stream_queue_buffer(impl->stream, buf); } + +static int rtp_midi_init(struct impl *impl, enum spa_direction direction) +{ + if (direction == SPA_DIRECTION_INPUT) + impl->stream_events.process = rtp_midi_process_capture; + else + impl->stream_events.process = rtp_midi_process_playback; + impl->receive_rtp = rtp_midi_receive; + return 0; +} diff --git a/src/modules/module-rtp/stream.c b/src/modules/module-rtp/stream.c index 568d4fc36..a474c45d3 100644 --- a/src/modules/module-rtp/stream.c +++ b/src/modules/module-rtp/stream.c @@ -35,6 +35,7 @@ struct impl { struct spa_audio_info info; + struct spa_audio_info stream_info; struct pw_stream *stream; struct spa_hook stream_listener; @@ -290,20 +291,23 @@ struct rtp_stream *rtp_stream_new(struct pw_core *core, goto out; } - switch (impl->info.media_type) { - case SPA_MEDIA_TYPE_audio: + switch (impl->info.media_subtype) { + case SPA_MEDIA_SUBTYPE_raw: parse_audio_info(props, &impl->info.info.raw); + impl->stream_info = impl->info; impl->format_info = find_audio_format_info(&impl->info); if (impl->format_info == NULL) { pw_log_error("unsupported audio format:%d channels:%d", - impl->info.info.raw.format, impl->info.info.raw.channels); + impl->stream_info.info.raw.format, + impl->stream_info.info.raw.channels); res = -EINVAL; goto out; } - impl->stride = impl->format_info->size * impl->info.info.raw.channels; - impl->rate = impl->info.info.raw.rate; + impl->stride = impl->format_info->size * impl->stream_info.info.raw.channels; + impl->rate = impl->stream_info.info.raw.rate; break; - case SPA_MEDIA_TYPE_application: + case SPA_MEDIA_SUBTYPE_control: + impl->stream_info = impl->info; impl->format_info = find_audio_format_info(&impl->info); if (impl->format_info == NULL) { res = -EINVAL; @@ -401,27 +405,19 @@ struct rtp_stream *rtp_stream_new(struct pw_core *core, flags = PW_STREAM_FLAG_MAP_BUFFERS | PW_STREAM_FLAG_RT_PROCESS; - switch (impl->info.media_type) { - case SPA_MEDIA_TYPE_audio: + switch (impl->info.media_subtype) { + case SPA_MEDIA_SUBTYPE_raw: params[n_params++] = spa_format_audio_build(&b, - SPA_PARAM_EnumFormat, &impl->info); - if (direction == SPA_DIRECTION_INPUT) - impl->stream_events.process = process_audio_capture; - else - impl->stream_events.process = process_audio_playback; - impl->receive_rtp = receive_rtp_audio; + SPA_PARAM_EnumFormat, &impl->stream_info); flags |= PW_STREAM_FLAG_AUTOCONNECT; + rtp_audio_init(impl, direction); break; - case SPA_MEDIA_TYPE_application: + case SPA_MEDIA_SUBTYPE_control: 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_application), SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_control)); - if (direction == SPA_DIRECTION_INPUT) - impl->stream_events.process = process_midi_capture; - else - impl->stream_events.process = process_midi_playback; - impl->receive_rtp = receive_rtp_midi; + rtp_midi_init(impl, direction); break; default: res = -EINVAL;