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

@ -478,18 +478,23 @@ static void send_destroy(void *data)
{
}
static void send_state_changed(void *data, bool started, const char *error)
static void send_open_connection(void *data, int *result)
{
struct session *sess = data;
sess->sending = true;
if (result)
*result = 1;
session_establish(sess);
}
if (started) {
sess->sending = true;
session_establish(sess);
} else {
sess->sending = false;
if (!sess->receiving)
session_stop(sess);
}
static void send_close_connection(void *data, int *result)
{
struct session *sess = data;
sess->sending = false;
if (result)
*result = 1;
if (!sess->receiving)
session_stop(sess);
}
static void send_send_packet(void *data, struct iovec *iov, size_t iovlen)
@ -516,17 +521,24 @@ static void send_send_packet(void *data, struct iovec *iov, size_t iovlen)
static void recv_destroy(void *data)
{
}
static void recv_state_changed(void *data, bool started, const char *error)
static void recv_open_connection(void *data, int *result)
{
struct session *sess = data;
if (started) {
sess->receiving = true;
session_establish(sess);
} else {
sess->receiving = false;
if (!sess->sending)
session_stop(sess);
}
sess->receiving = true;
if (result)
*result = 1;
session_establish(sess);
}
static void recv_close_connection(void *data, int *result)
{
struct session *sess = data;
sess->receiving = false;
if (result)
*result = 1;
if (!sess->sending)
session_stop(sess);
}
static void recv_send_feedback(void *data, uint32_t seqnum)
@ -560,14 +572,16 @@ static void recv_send_feedback(void *data, uint32_t seqnum)
static const struct rtp_stream_events send_stream_events = {
RTP_VERSION_STREAM_EVENTS,
.destroy = send_destroy,
.state_changed = send_state_changed,
.open_connection = send_open_connection,
.close_connection = send_close_connection,
.send_packet = send_send_packet,
};
static const struct rtp_stream_events recv_stream_events = {
RTP_VERSION_STREAM_EVENTS,
.destroy = recv_destroy,
.state_changed = recv_state_changed,
.open_connection = recv_open_connection,
.close_connection = recv_close_connection,
.send_feedback = recv_send_feedback,
};