module-rtp: Replace state_changed callbacks

The state_changed callbacks fulfill multiple roles, which is both a problem
regarding separation of concerns and regarding code clarity. De facto,
these callbacks cover error reporting, opening connections, and closing
connection, all in one, depending on a state that is arguably an internal
stream detail. The code in these callbacks tie these internal states to
assumptions that opening/closing callbacks is directly tied to specific
state changes in a common way, which is not always true. For example,
stopping the stream may not _actually_ stop it if a background send timer
is still running.

The notion of a "state_changed" callback is also problematic because the
pw_streams that are used in rtp-sink and rtp-source also have a callback
for state changes, causing confusion.

Solve this by replacing state_changed with three new callbacks:

1. report_error : Used for reporting nonrecoverable errors to the caller.
   Note that currently, no one does such error reporting, but the feature
   does exist, so this callback is introduced to preserve said feature.
2. open_connection : Used for opening a connection. Its optional return
   value informs about success or failure.
3. close_connection : Used for opening a connection. Its optional return
   value informs about success or failure.

Importantly, these callbacks do not export any internal stream state. This
improves encapsulation, and also makes it possible to invoke these
callbacks in situations that may not neatly map to a state change. One
example could be to close the connection as part of a stream_start call
to close any connection(s) left over from a previous run. (Followup commits
will in fact introduce such measures.)
This commit is contained in:
Carlos Rafael Giani 2025-08-21 13:30:11 +02:00 committed by Wim Taymans
parent 2f22c1d595
commit 3476e77714
6 changed files with 156 additions and 78 deletions

View file

@ -1545,14 +1545,12 @@ static void stream_destroy(void *d)
impl->stream = NULL;
}
static void stream_state_changed(void *data, bool started, const char *error)
static void stream_report_error(void *data, const char *error)
{
struct impl *impl = data;
if (error) {
pw_log_error("stream error: %s", error);
pw_impl_module_schedule_destroy(impl->module);
return;
}
}
@ -1726,7 +1724,7 @@ static void stream_param_changed(void *data, uint32_t id, const struct spa_pod *
static const struct rtp_stream_events stream_events = {
RTP_VERSION_STREAM_EVENTS,
.destroy = stream_destroy,
.state_changed = stream_state_changed,
.report_error = stream_report_error,
.param_changed = stream_param_changed,
.send_packet = stream_send_packet
};