Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
rtp.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 Basalte bv
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
11
12#ifndef ZEPHYR_INCLUDE_NET_RTP_H_
13#define ZEPHYR_INCLUDE_NET_RTP_H_
14
15#include <zephyr/kernel.h>
17#include <zephyr/net/net_ip.h>
18#include <zephyr/net/net_if.h>
19#include <zephyr/net/socket.h>
20#ifdef CONFIG_NET_PKT_TIMESTAMP
21#include <zephyr/net/ptp_time.h>
22#endif
24#include <zephyr/sys/util.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
37
39#define RTP_VERSION 2
40
42#define RTP_MIN_HEADER_LEN 12
43
45#define RTP_PAYLOAD_TYPE_MAX 127
46
50#define RTP_MARKER 1
51
53#define RTP_HDR_V_MASK GENMASK(7, 6)
55#define RTP_HDR_P_MASK BIT(5)
57#define RTP_HDR_X_MASK BIT(4)
59#define RTP_HDR_CC_MASK GENMASK(3, 0)
61#define RTP_HDR_M_MASK BIT(7)
63#define RTP_HDR_PT_MASK GENMASK(6, 0)
64
65#if defined(CONFIG_RTP_MAX_CSRC_COUNT) || defined(__DOXYGEN__)
67#define RTP_MAX_CSRC_COUNT CONFIG_RTP_MAX_CSRC_COUNT
68#else
69#define RTP_MAX_CSRC_COUNT 0
70#endif
71
81
103
112
113#ifdef CONFIG_NET_PKT_TIMESTAMP
117 struct net_ptp_time timestamp;
118#endif
119};
120
121struct rtp_session;
122struct srtp_policy;
123struct srtp_session_ctx;
124
142typedef void (*rtp_rx_cb_t)(struct rtp_session *session, struct rtp_packet *packet,
143 void *user_data);
144
154
158 struct net_if *iface;
159
161 struct net_sockaddr_storage sock_addr;
164
167
174};
175
178#ifdef CONFIG_RTP_TRANSPORT_SOCKET
182 RTP_TRANSPORT_SOCKET,
183#endif /* CONFIG_RTP_TRANSPORT_SOCKET */
184#ifdef CONFIG_RTP_TRANSPORT_NET_PKT
189 RTP_TRANSPORT_NET_PKT,
190#endif /* CONFIG_RTP_TRANSPORT_NET_PKT */
191
196};
197
198BUILD_ASSERT(RTP_TRANSPORT_NUM > 0, "Please select at least 1 RTP transport backend");
199
206
208 union {
209#ifdef CONFIG_RTP_TRANSPORT_SOCKET
210 struct {
212 int socket_rx_fd;
214 int socket_tx_fd;
215 };
216#endif /* CONFIG_RTP_TRANSPORT_SOCKET */
217#ifdef CONFIG_RTP_TRANSPORT_NET_PKT
218 struct {
220 struct net_conn_handle *net_handle_rtp;
221 };
222#endif /* CONFIG_RTP_TRANSPORT_NET_PKT */
223 };
224};
225
237
239 struct k_mutex lock;
240
247
252
256 size_t csrc_len;
257
258#if defined(CONFIG_SRTP) || defined(__DOXYGEN__)
265#endif /* CONFIG_SRTP */
266
267#if CONFIG_RTP_LOG_LEVEL >= LOG_LEVEL_DBG
269 const char *name;
270#endif
271};
272
274#if CONFIG_RTP_LOG_LEVEL >= LOG_LEVEL_DBG
275#define RTP_SESSION_NAME(_name) .name = STRINGIFY(_name),
276#else
277#define RTP_SESSION_NAME(_name)
278#endif
280
294#define RTP_SESSION_DEFINE(_name, _local_port) \
295 struct rtp_session _name = {.local_port = _local_port, RTP_SESSION_NAME(_name)}
296
309 uint8_t *data, size_t len);
310
337int rtp_session_init(struct rtp_session *session, struct net_if *iface,
338 struct net_sockaddr *sock_addr, enum rtp_role role, uint8_t payload_type,
339 rtp_rx_cb_t callback, void *user_data, enum rtp_transport_type transport_type);
340
357static inline int rtp_session_init_rx(struct rtp_session *session, struct net_if *iface,
358 struct net_sockaddr *sock_addr, rtp_rx_cb_t callback,
359 void *user_data, enum rtp_transport_type transport_type)
360{
361 return rtp_session_init(session, iface, sock_addr, RTP_ROLE_SINK, 0, callback, user_data,
362 transport_type);
363}
364
380static inline int rtp_session_init_tx(struct rtp_session *session, struct net_if *iface,
381 struct net_sockaddr *sock_addr, uint8_t payload_type,
382 enum rtp_transport_type transport_type)
383{
384 return rtp_session_init(session, iface, sock_addr, RTP_ROLE_SOURCE, payload_type, NULL,
385 NULL, transport_type);
386}
387
399int rtp_session_start(struct rtp_session *session);
400
411int rtp_session_stop(struct rtp_session *session);
412
422int rtp_session_add_csrc(struct rtp_session *session, uint32_t csrc);
423
434
452int rtp_session_send(struct rtp_session *session, void *data, size_t len, uint32_t delta_ts,
453 uint8_t padding, uint8_t marker, struct rtp_header_extension *hdr_x,
454 uint32_t *timestamp);
455
470static inline int rtp_session_send_simple(struct rtp_session *session, void *data, size_t len,
471 uint32_t delta_ts)
472{
473 return rtp_session_send(session, data, len, delta_ts, 0, 0, NULL, NULL);
474}
475
476#if defined(CONFIG_SRTP) || defined(__DOXYGEN__)
477
514int rtp_session_set_srtp(struct rtp_session *session, const struct srtp_policy *tx_policy,
515 const struct srtp_policy *rx_policy, struct srtp_session_ctx *srtp_ctx);
516
537
538#endif /* CONFIG_SRTP */
539
547static inline uint8_t rtp_header_get_v(const struct rtp_header *h)
548{
549 __ASSERT_NO_MSG(h != NULL);
550
551 return FIELD_GET(RTP_HDR_V_MASK, h->vpxcc);
552}
553
561static inline uint8_t rtp_header_get_p(const struct rtp_header *h)
562{
563 __ASSERT_NO_MSG(h != NULL);
564
565 return FIELD_GET(RTP_HDR_P_MASK, h->vpxcc);
566}
567
575static inline uint8_t rtp_header_get_x(const struct rtp_header *h)
576{
577 __ASSERT_NO_MSG(h != NULL);
578
579 return FIELD_GET(RTP_HDR_X_MASK, h->vpxcc);
580}
581
589static inline uint8_t rtp_header_get_cc(const struct rtp_header *h)
590{
591 __ASSERT_NO_MSG(h != NULL);
592
593 return FIELD_GET(RTP_HDR_CC_MASK, h->vpxcc);
594}
595
603static inline uint8_t rtp_header_get_m(const struct rtp_header *h)
604{
605 __ASSERT_NO_MSG(h != NULL);
606
607 return FIELD_GET(RTP_HDR_M_MASK, h->mpt);
608}
609
617static inline uint8_t rtp_header_get_pt(const struct rtp_header *h)
618{
619 __ASSERT_NO_MSG(h != NULL);
620
621 return FIELD_GET(RTP_HDR_PT_MASK, h->mpt);
622}
623
630static inline void rtp_header_set_v(struct rtp_header *h, uint8_t value)
631{
632 __ASSERT_NO_MSG(h != NULL);
633
635 h->vpxcc |= FIELD_PREP(RTP_HDR_V_MASK, value);
636}
637
644static inline void rtp_header_set_p(struct rtp_header *h, uint8_t value)
645{
646 __ASSERT_NO_MSG(h != NULL);
647
649 h->vpxcc |= FIELD_PREP(RTP_HDR_P_MASK, value);
650}
651
658static inline void rtp_header_set_x(struct rtp_header *h, uint8_t value)
659{
660 __ASSERT_NO_MSG(h != NULL);
661
663 h->vpxcc |= FIELD_PREP(RTP_HDR_X_MASK, value);
664}
665
672static inline void rtp_header_set_cc(struct rtp_header *h, uint8_t value)
673{
674 __ASSERT_NO_MSG(h != NULL);
675
677 h->vpxcc |= FIELD_PREP(RTP_HDR_CC_MASK, value);
678}
679
686static inline void rtp_header_set_m(struct rtp_header *h, uint8_t value)
687{
688 __ASSERT_NO_MSG(h != NULL);
689
690 h->mpt &= ~RTP_HDR_M_MASK;
691 h->mpt |= FIELD_PREP(RTP_HDR_M_MASK, value);
692}
693
700static inline void rtp_header_set_pt(struct rtp_header *h, uint8_t value)
701{
702 __ASSERT_NO_MSG(h != NULL);
703
704 h->mpt &= ~RTP_HDR_PT_MASK;
705 h->mpt |= FIELD_PREP(RTP_HDR_PT_MASK, value);
706}
707
708#ifdef __cplusplus
709}
710#endif
711
715
716#endif /* ZEPHYR_INCLUDE_NET_RTP_H_ */
int rtp_session_send(struct rtp_session *session, void *data, size_t len, uint32_t delta_ts, uint8_t padding, uint8_t marker, struct rtp_header_extension *hdr_x, uint32_t *timestamp)
Send an RTP packet with an optional header extension and optional padding.
static void rtp_header_set_pt(struct rtp_header *h, uint8_t value)
Set the payload type (PT) field.
Definition rtp.h:700
rtp_transport_type
Transport backend used by an RTP session.
Definition rtp.h:177
int rtp_session_set_srtp(struct rtp_session *session, const struct srtp_policy *tx_policy, const struct srtp_policy *rx_policy, struct srtp_session_ctx *srtp_ctx)
Enable SRTP protection on an RTP session.
int rtp_init_header_extension(struct rtp_header_extension *hdr_x, uint16_t definition, uint8_t *data, size_t len)
Initialize an RTP header extension.
static uint8_t rtp_header_get_pt(const struct rtp_header *h)
Get the payload type (PT) field.
Definition rtp.h:617
static void rtp_header_set_cc(struct rtp_header *h, uint8_t value)
Set the CSRC count (CC) field.
Definition rtp.h:672
static uint8_t rtp_header_get_m(const struct rtp_header *h)
Get the marker (M) bit.
Definition rtp.h:603
static void rtp_header_set_m(struct rtp_header *h, uint8_t value)
Set the marker (M) bit.
Definition rtp.h:686
static void rtp_header_set_x(struct rtp_header *h, uint8_t value)
Set the extension (X) flag.
Definition rtp.h:658
int rtp_session_start(struct rtp_session *session)
Start an RTP session.
int rtp_session_add_csrc(struct rtp_session *session, uint32_t csrc)
Add a contributing source (CSRC) to the session.
static int rtp_session_send_simple(struct rtp_session *session, void *data, size_t len, uint32_t delta_ts)
Send an RTP packet without a header extension, padding or marker.
Definition rtp.h:470
int rtp_session_clear_srtp(struct rtp_session *session)
Disable SRTP protection on an RTP session.
static int rtp_session_init_rx(struct rtp_session *session, struct net_if *iface, struct net_sockaddr *sock_addr, rtp_rx_cb_t callback, void *user_data, enum rtp_transport_type transport_type)
Initialize a receive-only RTP session.
Definition rtp.h:357
#define RTP_HDR_X_MASK
Mask for the extension (X) flag within rtp_header::vpxcc.
Definition rtp.h:57
int rtp_session_init(struct rtp_session *session, struct net_if *iface, struct net_sockaddr *sock_addr, enum rtp_role role, uint8_t payload_type, rtp_rx_cb_t callback, void *user_data, enum rtp_transport_type transport_type)
Initialize an RTP session.
int rtp_session_remove_csrc(struct rtp_session *session, uint32_t csrc)
Remove a contributing source (CSRC) from the session.
static uint8_t rtp_header_get_cc(const struct rtp_header *h)
Get the CSRC count (CC) field.
Definition rtp.h:589
#define RTP_HDR_PT_MASK
Mask for the payload type (PT) field within rtp_header::mpt.
Definition rtp.h:63
#define RTP_HDR_P_MASK
Mask for the padding (P) flag within rtp_header::vpxcc.
Definition rtp.h:55
void(* rtp_rx_cb_t)(struct rtp_session *session, struct rtp_packet *packet, void *user_data)
RTP packet receive callback type.
Definition rtp.h:142
#define RTP_HDR_V_MASK
Mask for the version (V) field within rtp_header::vpxcc.
Definition rtp.h:53
int rtp_session_stop(struct rtp_session *session)
Stop an RTP session.
static uint8_t rtp_header_get_x(const struct rtp_header *h)
Get the extension (X) flag.
Definition rtp.h:575
static uint8_t rtp_header_get_p(const struct rtp_header *h)
Get the padding (P) flag.
Definition rtp.h:561
#define RTP_HDR_CC_MASK
Mask for the CSRC count (CC) field within rtp_header::vpxcc.
Definition rtp.h:59
static uint8_t rtp_header_get_v(const struct rtp_header *h)
Get the RTP version (V) field.
Definition rtp.h:547
#define RTP_MAX_CSRC_COUNT
Maximum number of csrc's that can be stored.
Definition rtp.h:67
static void rtp_header_set_p(struct rtp_header *h, uint8_t value)
Set the padding (P) flag.
Definition rtp.h:644
static int rtp_session_init_tx(struct rtp_session *session, struct net_if *iface, struct net_sockaddr *sock_addr, uint8_t payload_type, enum rtp_transport_type transport_type)
Initialize a transmit-only RTP session.
Definition rtp.h:380
#define RTP_HDR_M_MASK
Mask for the marker (M) bit within rtp_header::mpt.
Definition rtp.h:61
rtp_role
Role of an RTP session.
Definition rtp.h:146
static void rtp_header_set_v(struct rtp_header *h, uint8_t value)
Set the RTP version (V) field.
Definition rtp.h:630
@ RTP_TRANSPORT_NUM
Shall not be used as a transport type.
Definition rtp.h:195
@ RTP_ROLE_BOTH
Session both transmits and receives packets.
Definition rtp.h:152
@ RTP_ROLE_SOURCE
Session transmits packets only.
Definition rtp.h:150
@ RTP_ROLE_SINK
Session receives packets only.
Definition rtp.h:148
Public kernel APIs.
Header file for the logging core.
BSD Sockets compatible API definitions.
Network context definitions.
Public API for network interface.
IPv6 and IPv4 definitions.
Public functions for the Precision Time Protocol time specification.
#define FIELD_PREP(mask, value)
Definition silabs-pinctrl-siwx91x.h:15
#define FIELD_GET(mask, value)
Definition silabs-pinctrl-siwx91x.h:14
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Kernel mutex structure.
Definition kernel.h:3774
Network Interface structure.
Definition net_if.h:875
(Generalized) Precision Time Protocol Timestamp format.
Definition ptp_time.h:111
Generic sockaddr struct.
Definition net_ip.h:462
RTP header extension as defined in RFC 3550, section-5.3.1.
Definition rtp.h:73
uint8_t * data
Pointer to the extension data.
Definition rtp.h:79
uint16_t length
Length of the extension data in 32-bit words.
Definition rtp.h:77
uint16_t definition
Profile-defined extension header identifier.
Definition rtp.h:75
RTP fixed header as defined in RFC 3550, section-5.1.
Definition rtp.h:83
uint8_t mpt
Raw encoding of the marker, and payload type fields.
Definition rtp.h:87
uint16_t seq
Sequence number, incremented by one for each RTP data packet sent.
Definition rtp.h:89
uint32_t ts
Timestamp reflecting the sampling instant of the first octet in the payload.
Definition rtp.h:91
struct rtp_header_extension header_extension
Optional header extension; valid only when the x bit is set.
Definition rtp.h:101
uint32_t ssrc
Synchronization source (SSRC) identifier, chosen randomly and intended to be globally unique.
Definition rtp.h:95
uint8_t vpxcc
Raw encoding of the version, padding, extension, and CC fields.
Definition rtp.h:85
uint32_t csrc[CONFIG_RTP_MAX_CSRC_COUNT]
Contributing source (CSRC) identifiers, up to <code class='doxyxref' data-stype='kconfig:option' dat...
Definition rtp.h:99
Decoded RTP packet with header and payload separated.
Definition rtp.h:105
uint8_t * payload
Pointer to the packet payload.
Definition rtp.h:109
struct rtp_header header
Parsed RTP header.
Definition rtp.h:107
size_t payload_len
Length of the payload in bytes.
Definition rtp.h:111
RTP session configuration context.
Definition rtp.h:156
void * user_data
Opaque user data forwarded to callback.
Definition rtp.h:173
rtp_rx_cb_t callback
Callback invoked upon packet reception; required when role is RTP_ROLE_SINK or RTP_ROLE_BOTH.
Definition rtp.h:171
struct net_sockaddr_storage sock_addr
Session address and port (multicast group address or unicast peer).
Definition rtp.h:161
enum rtp_role role
Role of the session: sink, source, or both.
Definition rtp.h:163
uint8_t payload_type
RTP payload type field value used when transmitting.
Definition rtp.h:166
struct net_if * iface
Network interface used by the session.
Definition rtp.h:158
RTP session state.
Definition rtp.h:232
uint32_t timestamp
Current RTP timestamp, advanced with each transmitted packet.
Definition rtp.h:244
uint16_t local_port
Local UDP port used when transmitting; 0 to auto-select (see <code class='doxyxref' data-stype='kcon...
Definition rtp.h:251
struct rtp_transport transport
Transport info.
Definition rtp.h:234
size_t csrc_len
Number of active entries in csrc.
Definition rtp.h:256
const char * name
Human-readable session name used in debug log messages.
Definition rtp.h:269
struct k_mutex lock
Mutex protecting concurrent access to session state.
Definition rtp.h:239
uint32_t csrc[CONFIG_RTP_MAX_CSRC_COUNT]
Contributing source (CSRC) identifier list.
Definition rtp.h:254
uint16_t sequence_number
Sequence number of the next packet to be transmitted.
Definition rtp.h:246
struct rtp_session_context rtp_context
Session configuration context.
Definition rtp.h:236
struct srtp_session_ctx * srtp
SRTP state installed with rtp_session_set_srtp; NULL when the session runs plain RTP.
Definition rtp.h:264
uint32_t ssrc
Synchronization source (SSRC) identifier for this session.
Definition rtp.h:242
Internal transport state embedded in rtp_session.
Definition rtp.h:203
enum rtp_transport_type type
Active transport backend for this session.
Definition rtp.h:205
SRTP stream protection policy.
Definition srtp.h:89
SRTP state of an RTP session.
Definition srtp.h:279
Misc utilities.
Macro utilities.