From 4ffd74ef46b576a7aa4b3fd481cde6db79ea71bd Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 25 Mar 2024 12:22:11 +0100 Subject: [PATCH] module-rtp: handle state change errors better Make a new function to set the rtp stream in the error state. When we fail to start the stream, set the error state. Otherwise (like when we try to use an invalid interface name) the socket create will fail but the stream will still try to send data to the invalid socket. --- src/modules/module-rtp-sink.c | 1 + src/modules/module-rtp-source.c | 9 ++++++--- src/modules/module-rtp/stream.c | 6 ++++++ src/modules/module-rtp/stream.h | 1 + 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/modules/module-rtp-sink.c b/src/modules/module-rtp-sink.c index 298a18545..62f5b7a0d 100644 --- a/src/modules/module-rtp-sink.c +++ b/src/modules/module-rtp-sink.c @@ -307,6 +307,7 @@ static void stream_state_changed(void *data, bool started, const char *error) impl->mcast_loop, impl->ttl, impl->dscp, impl->ifname)) < 0) { pw_log_error("can't make socket: %s", spa_strerror(res)); + rtp_stream_set_error(impl->stream, res, "Can't make socket"); return; } impl->rtp_fd = res; diff --git a/src/modules/module-rtp-source.c b/src/modules/module-rtp-source.c index 936b6b487..22312734e 100644 --- a/src/modules/module-rtp-source.c +++ b/src/modules/module-rtp-source.c @@ -305,7 +305,7 @@ static int stream_start(struct impl *impl) if ((fd = make_socket((const struct sockaddr *)&impl->src_addr, impl->src_len, impl->ifname)) < 0) { pw_log_error("failed to create socket: %m"); - return fd; + return -errno; } impl->source = pw_loop_add_io(impl->data_loop, fd, @@ -338,13 +338,16 @@ static void stream_destroy(void *d) static void stream_state_changed(void *data, bool started, const char *error) { struct impl *impl = data; + int res; if (error) { pw_log_error("stream error: %s", error); pw_impl_module_schedule_destroy(impl->module); } else if (started) { - if ((errno = -stream_start(impl)) < 0) - pw_log_error("failed to start RTP stream: %m"); + if ((res = stream_start(impl)) < 0) { + pw_log_error("failed to start RTP stream: %s", spa_strerror(res)); + rtp_stream_set_error(impl->stream, res, "Can't start RTP stream"); + } } else { if (!impl->always_process) stream_stop(impl); diff --git a/src/modules/module-rtp/stream.c b/src/modules/module-rtp/stream.c index e0aa64498..c42bae786 100644 --- a/src/modules/module-rtp/stream.c +++ b/src/modules/module-rtp/stream.c @@ -642,6 +642,12 @@ void rtp_stream_set_first(struct rtp_stream *s) impl->first = true; } +void rtp_stream_set_error(struct rtp_stream *s, int res, const char *error) +{ + struct impl *impl = (struct impl*)s; + pw_stream_set_error(impl->stream, res, "%s: %s", error, spa_strerror(res)); +} + enum pw_stream_state rtp_stream_get_state(struct rtp_stream *s, const char **error) { struct impl *impl = (struct impl*)s; diff --git a/src/modules/module-rtp/stream.h b/src/modules/module-rtp/stream.h index e81422392..28854fb3c 100644 --- a/src/modules/module-rtp/stream.h +++ b/src/modules/module-rtp/stream.h @@ -54,6 +54,7 @@ uint16_t rtp_stream_get_seq(struct rtp_stream *s); void rtp_stream_set_first(struct rtp_stream *s); +void rtp_stream_set_error(struct rtp_stream *s, int res, const char *error); enum pw_stream_state rtp_stream_get_state(struct rtp_stream *s, const char **error); int rtp_stream_set_param(struct rtp_stream *s, uint32_t id, const struct spa_pod *param);