Commit graph

43 commits

Author SHA1 Message Date
Carlos Rafael Giani
955c9ae837 module-rtp: Get the current stream time in a reusable manner
That way, redundant pw_stream_get_nsec() and clock_gettime()
calls can be avoided.
2025-10-27 22:40:22 +01:00
Carlos Rafael Giani
caf72fd9bc module-rtp: Synchronize access to timer_running flag 2025-08-25 10:33:50 +00:00
Carlos Rafael Giani
37e597ff0a module-rtp: Replace "started" boolean with internal state enum
The started boolean is insufficient to fully cover the possible internal
states. For this reason, it needs to be replaced by an enum that covers
these states.

Also, due to potential access by both the dataloop and the mainloop,
access to that internal state needs to be synchronized.

Finally, a variable "internal_state" makes for code that is easier to
read, since it emphasizes that this is state that is fully internal
inside the stream (and is not visible to the rtp-sink and rtp-source
modules for example).
2025-08-25 10:33:50 +00:00
Carlos Rafael Giani
2f22c1d595 module-rtp: Stop any ongoing timer when starting stream 2025-08-25 10:33:50 +00:00
Carlos Rafael Giani
97a1609b29 module-rtp: Reset ring buffer contents when stream starts 2025-08-05 17:54:56 +00:00
Carlos Rafael Giani
c9a8b8629f module-rtp: Limit actual max buffer size to an integer multiple of stride
Opus and MIDI code get TODOs added, since it is currently unclear how to
implement that fix for them.
2025-08-05 17:54:56 +00:00
Carlos Rafael Giani
e5be9cce4f module-rtp: Reorder sync checks and resynchronization code
This fixes the case when synchronization is established but actually not
valid anymore. In such a case, the code would _first_ write to the ring
buffer (at the wrong position due to the invalid sync), and _then_ detect
the bogus synchronization. Reorder the code blocks to _first_ check the
current sync, then resynchronize if neeeded (or perform initial sync if
no sync is established yet), and _then_ write to the ring buffer.
2025-08-05 17:54:56 +00:00
Carlos Rafael Giani
b8d98d03fe module-rtp: Fix timestamp check and add discontinuity check
Until now, the timestamp check was comparing the timestamp delta against
the value of the "quantum" variable. However, the timestamps use clock
samples as units, while the "quantum" variable uses nanoseconds. The
outcome is that this check virtually never returned true. Use the
spa_io_clock duration instead of that quantum nanosecond duration to make
the check actually work.

Also, do not just rely on vast timestamp deltas to detect discontinuities;
instead, check first for the presence of the SPA_IO_CLOCK_FLAG_DISCONT
flag to detect said discontinuities.
2025-08-05 17:54:56 +00:00
Carlos Rafael Giani
2a460e18e3 module-rtp: Rename timestamp to actual_timestamp for clarity 2025-08-05 17:54:56 +00:00
Carlos Rafael Giani
91ebfac75b module-rtp: Clear after reading in direct timestamp mode 2025-07-29 17:24:09 +02:00
Carlos Rafael Giani
2bcc8589fa module-rtp: Fix and improve direct timestamp mode and documentation
Direct timestamp mode was incorrectly using over/underrun detection logic
and fill level tracking logic that is actually meant for the other mode
(referred to from now on as "constant latency mode"). Over/underruns are
tracked implicitly in the direct timestamp mode, and the absolute fill
level is not relevant in that mode, since the latency is not needed to
be constant then.

Also improve log lines and the RTP module documentation to define these
buffer modes clearly and explain their differences and use cases.

Opus and MIDI code get TODOs added, since their direct timestamp mode
implementations still may be incorrect. Fixing those will be done in
a separate commit.
2025-07-24 07:28:53 +00:00
Martin Geier
f8b0d0a43c rtp: include stream delay to a read position
When a stream has some delay, a time t1 + delay has to be read in time
t1 to play it when expected.
Decrease target_buffer by delay to start playback sooner, so sound
is played at correct time when delay is applied.

Signed-off-by: Martin Geier <martin.geier@streamunlimited.com>
2025-07-24 07:28:53 +00:00
Wim Taymans
47ee9ef10a module-rtp: set the EMPTY flag on empty buffers
And make sure other flags are reset.
2025-07-03 20:57:19 +02:00
Wim Taymans
e825a6ae6c modules: reduce some errors from warn to info
Some of the more common errors (caused by packet loss, network jitter, ...)
should be reported with INFO unless there is some indication about how
to fix the problem.

Fixes #4559
2025-02-18 16:24:52 +01:00
Wim Taymans
cfc8d414a9 module-rtp: fix SSRC warnings
Fix indentation and also suppress the SSRC warning for other formats
than audio.
2025-02-17 10:21:17 +01:00
Arun Raghavan
25e58995f5 module-rtp-sap: Silently ignore other SSRCs if we know the receiver SSRC
If we know the receiver SSRC from the SAP, we can happily ignore packets
on other SSRCs.
2025-02-15 22:32:12 -05:00
Arun Raghavan
974117f41a module-rtp: Fix previous typo fix
We want to track the difference between the PTP timestamp (now) and the
last RTP send, not the synthesized next RTP timestamp (which will always
be smoothly incrementing).
2024-12-31 15:24:46 -05:00
Arun Raghavan
c143e89118 module-rtp: Fix typo in check for lagging sender 2024-12-31 11:58:03 -05:00
Arun Raghavan
45eee02a99 module-rtp: Account for in-flight samples in RTP receive
When not using PTP as the driver, it is possible that packet receive and
the process() callback are out of sync, meaning that the target buffer
fill level might be off by upto one ptime's worth of samples
occasionally. This would make the DLL hunt for the target rate, and
cause a constantly varying delay.

Accounting for the delta between the packet receive time and the
process() time allows us to eliminate this jitter, resulting in much
more consistent rate matching.
2024-11-27 11:48:50 +00:00
Wim Taymans
e1fc3de595 modules: use pw_stream_set_rate() some more 2024-11-22 09:55:36 +01:00
Wim Taymans
804df3389a modules: use pw_stream_set_rate() when we can 2024-11-22 09:49:27 +01:00
Wim Taymans
a53bc035c0 module-rtp: calculate payload_size based on MTU
The actual payload size depends on the MTU but should not include the
IP/UDP and RTP headers.

Fixes #4396
2024-11-11 11:49:20 +01:00
Jonas Holmberg
6223715918 module-rtp: Fix rtp timestamps
Because of operator precedence the timestamps where set to 0 if
set_timestamp was 0.
2024-10-23 16:12:55 +02:00
Arun Raghavan
292d6f5ca2 module-rtp: More u64 format fixes
Fixes: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/4241
2024-08-28 21:55:15 -04:00
Wim Taymans
a77a6f3959 modules: for format string for u64
Fixes #4241
2024-08-28 16:21:30 +02:00
Arun Raghavan
48c2e95165 module-rtp: Clamp buffer fill level check on send side
This is based on target_buffer which is likely to be much smaller than
BUFFER_SIZE (currently 4MB). This is already done on the receive side.
2024-08-27 01:38:10 +00:00
Arun Raghavan
9f643fec7e module-rtp: Allow aes67 send with a non PTP clock
Our current AES67 sender setup requires that that PTP driver drive the
entire graph. This adds support for allowing the AES67 RTP sink to be
driven by an arbitrary driver, while still using the PTP driver for
sending data on the network.

When aes67.driver-group is specified a pw_filter is created with no
ports, node.always-process = true and node.group set to the
aes67.driver-group. When set to PTP, this gives us process callbacks at
the PTP rate which we use to get the current PTP time in the RTP sender
by interpolating the clock snapshots from the pw-filter.

Implementation ideas from Wim Taymans. Co-authored with Sanchayan Maity.

For a detailed reference, refer the following papers by Fons Adriaensen.
- Using a DLL to filter time
  (https://kokkinizita.linuxaudio.org/papers/usingdll.pdf)
- Controlling adaptive resampling
  (http://kokkinizita.linuxaudio.org/papers/adapt-resamp.pdf)
2024-08-27 01:38:10 +00:00
Arun Raghavan
9ccf62d4f6 module-rtp: Increase write timestamp tolerance
We allow a quantum of jitter in the write timestamp. The previous value
of 32 seems to be empirically determined, using the actual quantum
allows us to reason about this better.
2024-08-27 01:38:10 +00:00
Wim Taymans
1ae4374ccf Fix compilation with -Werror=float-conversion
Better make the conversions explicit so that we don't get any surprises.

Fixes #4065
2024-06-18 12:17:56 +02:00
Jonas Holmberg
64d75b6b2e module-rtp-sink: Send all remaining packets when stopping
Don't close rtp_fd until all packets have been sent by the timer, if
the timer is running when the stream is stopped.
2024-04-16 07:50:33 +00:00
Wim Taymans
5082e9f458 module-rtp: only stop timer when it was running 2024-02-15 17:48:58 +01:00
Wim Taymans
78055736a2 module-rtp: add packet timer
When multiple packets need to be flushed (because sess.latency is set
and larger than ptime) use a timer to space the packets uniformly in
the current quantum to avoid bursts.

See !1873
2024-01-30 10:06:27 +01:00
Wim Taymans
9a5609de2b modules: move some spa_debug_mem to the log
Instead of dumping to stderr, write it to the log file.
2024-01-11 17:49:50 +01:00
Dmitry Sharshakov
1fe6feac56 module-rtp: improve logging priorities
Previous state was useless for real debug at the current implementation level
2023-12-20 09:35:22 +00:00
Christian Glombek
9eba60a635 module-rtp/stream: Add ability to set marker on first packet 2023-10-09 10:52:25 +02:00
Wim Taymans
126e03ec73 rtp: add option to ignore SSRC
This is useful when there is a fixed receiver and the sender can be
restarted.
2023-07-06 12:55:28 +02:00
Wim Taymans
51a970f5b7 module-rtp: fix writing of audio samples
Always write samples according to the current write position, only use
the graph timestamp to align.
2023-03-13 15:14:41 +01:00
Wim Taymans
bcc052f5f1 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.
2023-03-12 18:40:36 +01:00
Wim Taymans
f841a0d3f1 module-rtp: send journal feedback
Parse the journal and send feedback.
Handle the NO and RS commands.
2023-03-10 10:47:03 +01:00
Wim Taymans
c5effbd979 module-rtp: add timer for ck requests
Scale RTP timestamps against the clock, allow some jitter.
Make method to query current RTP timestamps.
2023-03-09 13:14:23 +01:00
Wim Taymans
8e5b9da177 module-rtp: fix direct timestamps
fix some other properties.
2023-03-09 13:14:23 +01:00
Wim Taymans
7a31278511 module-rtp: improve properties and some cleanups 2023-03-09 13:14:22 +01:00
Wim Taymans
7da031c969 module-rtp: add new rtp-session module
The module uses the apple session setup for managing peer connections.

Make a generic rtp stream object, make midi and audio implementations.
2023-03-09 13:14:21 +01:00