Zephyr API Documentation 4.4.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;
122
140typedef void (*rtp_rx_cb_t)(struct rtp_session *session, struct rtp_packet *packet,
141 void *user_data);
142
152
156 struct net_if *iface;
157
159 struct net_sockaddr_storage sock_addr;
162
165
172};
173
176#ifdef CONFIG_RTP_TRANSPORT_SOCKET
180 RTP_TRANSPORT_SOCKET,
181#endif /* CONFIG_RTP_TRANSPORT_SOCKET */
182#ifdef CONFIG_RTP_TRANSPORT_NET_PKT
187 RTP_TRANSPORT_NET_PKT,
188#endif /* CONFIG_RTP_TRANSPORT_NET_PKT */
189
194};
195
196BUILD_ASSERT(RTP_TRANSPORT_NUM > 0, "Please select at least 1 RTP transport backend");
197
204
206 union {
207#ifdef CONFIG_RTP_TRANSPORT_SOCKET
208 struct {
210 int socket_rx_fd;
212 int socket_tx_fd;
213 };
214#endif /* CONFIG_RTP_TRANSPORT_SOCKET */
215#ifdef CONFIG_RTP_TRANSPORT_NET_PKT
216 struct {
218 struct net_conn_handle *net_handle_rtp;
219 };
220#endif /* CONFIG_RTP_TRANSPORT_NET_PKT */
221 };
222};
223
235
237 struct k_mutex lock;
238
245
250
254 size_t csrc_len;
255
256#if CONFIG_RTP_LOG_LEVEL >= LOG_LEVEL_DBG
258 const char *name;
259#endif
260};
261
263#if CONFIG_RTP_LOG_LEVEL >= LOG_LEVEL_DBG
264#define RTP_SESSION_NAME(_name) .name = STRINGIFY(_name),
265#else
266#define RTP_SESSION_NAME(_name)
267#endif
269
283#define RTP_SESSION_DEFINE(_name, _local_port) \
284 struct rtp_session _name = {.local_port = _local_port, RTP_SESSION_NAME(_name)}
285
298 uint8_t *data, size_t len);
299
321int rtp_session_init(struct rtp_session *session, struct net_if *iface,
322 struct net_sockaddr *sock_addr, enum rtp_role role, uint8_t payload_type,
323 rtp_rx_cb_t callback, void *user_data, enum rtp_transport_type transport_type);
324
341static inline int rtp_session_init_rx(struct rtp_session *session, struct net_if *iface,
342 struct net_sockaddr *sock_addr, rtp_rx_cb_t callback,
343 void *user_data, enum rtp_transport_type transport_type)
344{
345 return rtp_session_init(session, iface, sock_addr, RTP_ROLE_SINK, 0, callback, user_data,
346 transport_type);
347}
348
364static inline int rtp_session_init_tx(struct rtp_session *session, struct net_if *iface,
365 struct net_sockaddr *sock_addr, uint8_t payload_type,
366 enum rtp_transport_type transport_type)
367{
368 return rtp_session_init(session, iface, sock_addr, RTP_ROLE_SOURCE, payload_type, NULL,
369 NULL, transport_type);
370}
371
383int rtp_session_start(struct rtp_session *session);
384
395int rtp_session_stop(struct rtp_session *session);
396
406int rtp_session_add_csrc(struct rtp_session *session, uint32_t csrc);
407
418
436int rtp_session_send(struct rtp_session *session, void *data, size_t len, uint32_t delta_ts,
437 uint8_t padding, uint8_t marker, struct rtp_header_extension *hdr_x,
438 uint32_t *timestamp);
439
454static inline int rtp_session_send_simple(struct rtp_session *session, void *data, size_t len,
455 uint32_t delta_ts)
456{
457 return rtp_session_send(session, data, len, delta_ts, 0, 0, NULL, NULL);
458}
459
467static inline uint8_t rtp_header_get_v(const struct rtp_header *h)
468{
469 __ASSERT_NO_MSG(h != NULL);
470
471 return FIELD_GET(RTP_HDR_V_MASK, h->vpxcc);
472}
473
481static inline uint8_t rtp_header_get_p(const struct rtp_header *h)
482{
483 __ASSERT_NO_MSG(h != NULL);
484
485 return FIELD_GET(RTP_HDR_P_MASK, h->vpxcc);
486}
487
495static inline uint8_t rtp_header_get_x(const struct rtp_header *h)
496{
497 __ASSERT_NO_MSG(h != NULL);
498
499 return FIELD_GET(RTP_HDR_X_MASK, h->vpxcc);
500}
501
509static inline uint8_t rtp_header_get_cc(const struct rtp_header *h)
510{
511 __ASSERT_NO_MSG(h != NULL);
512
513 return FIELD_GET(RTP_HDR_CC_MASK, h->vpxcc);
514}
515
523static inline uint8_t rtp_header_get_m(const struct rtp_header *h)
524{
525 __ASSERT_NO_MSG(h != NULL);
526
527 return FIELD_GET(RTP_HDR_M_MASK, h->mpt);
528}
529
537static inline uint8_t rtp_header_get_pt(const struct rtp_header *h)
538{
539 __ASSERT_NO_MSG(h != NULL);
540
541 return FIELD_GET(RTP_HDR_PT_MASK, h->mpt);
542}
543
550static inline void rtp_header_set_v(struct rtp_header *h, uint8_t value)
551{
552 __ASSERT_NO_MSG(h != NULL);
553
555 h->vpxcc |= FIELD_PREP(RTP_HDR_V_MASK, value);
556}
557
564static inline void rtp_header_set_p(struct rtp_header *h, uint8_t value)
565{
566 __ASSERT_NO_MSG(h != NULL);
567
569 h->vpxcc |= FIELD_PREP(RTP_HDR_P_MASK, value);
570}
571
578static inline void rtp_header_set_x(struct rtp_header *h, uint8_t value)
579{
580 __ASSERT_NO_MSG(h != NULL);
581
583 h->vpxcc |= FIELD_PREP(RTP_HDR_X_MASK, value);
584}
585
592static inline void rtp_header_set_cc(struct rtp_header *h, uint8_t value)
593{
594 __ASSERT_NO_MSG(h != NULL);
595
597 h->vpxcc |= FIELD_PREP(RTP_HDR_CC_MASK, value);
598}
599
606static inline void rtp_header_set_m(struct rtp_header *h, uint8_t value)
607{
608 __ASSERT_NO_MSG(h != NULL);
609
610 h->mpt &= ~RTP_HDR_M_MASK;
611 h->mpt |= FIELD_PREP(RTP_HDR_M_MASK, value);
612}
613
620static inline void rtp_header_set_pt(struct rtp_header *h, uint8_t value)
621{
622 __ASSERT_NO_MSG(h != NULL);
623
624 h->mpt &= ~RTP_HDR_PT_MASK;
625 h->mpt |= FIELD_PREP(RTP_HDR_PT_MASK, value);
626}
627
628#ifdef __cplusplus
629}
630#endif
631
635
636#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:620
rtp_transport_type
Transport backend used by an RTP session.
Definition rtp.h:175
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:537
static void rtp_header_set_cc(struct rtp_header *h, uint8_t value)
Set the CSRC count (CC) field.
Definition rtp.h:592
static uint8_t rtp_header_get_m(const struct rtp_header *h)
Get the marker (M) bit.
Definition rtp.h:523
static void rtp_header_set_m(struct rtp_header *h, uint8_t value)
Set the marker (M) bit.
Definition rtp.h:606
static void rtp_header_set_x(struct rtp_header *h, uint8_t value)
Set the extension (X) flag.
Definition rtp.h:578
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:454
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:341
#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:509
#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:140
#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:495
static uint8_t rtp_header_get_p(const struct rtp_header *h)
Get the padding (P) flag.
Definition rtp.h:481
#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:467
#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:564
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:364
#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:144
static void rtp_header_set_v(struct rtp_header *h, uint8_t value)
Set the RTP version (V) field.
Definition rtp.h:550
@ RTP_TRANSPORT_NUM
Shall not be used as a transport type.
Definition rtp.h:193
@ RTP_ROLE_BOTH
Session both transmits and receives packets.
Definition rtp.h:150
@ RTP_ROLE_SOURCE
Session transmits packets only.
Definition rtp.h:148
@ RTP_ROLE_SINK
Session receives packets only.
Definition rtp.h:146
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:3572
Network Interface structure.
Definition net_if.h:744
(Generalized) Precision Time Protocol Timestamp format.
Definition ptp_time.h:111
Generic sockaddr struct.
Definition net_ip.h:455
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 CONFIG_RTP_MAX_CSRC_COUNT entries.
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:154
void * user_data
Opaque user data forwarded to callback.
Definition rtp.h:171
rtp_rx_cb_t callback
Callback invoked upon packet reception; required when role is RTP_ROLE_SINK or RTP_ROLE_BOTH.
Definition rtp.h:169
struct net_sockaddr_storage sock_addr
Session address and port (multicast group address or unicast peer).
Definition rtp.h:159
enum rtp_role role
Role of the session: sink, source, or both.
Definition rtp.h:161
uint8_t payload_type
RTP payload type field value used when transmitting.
Definition rtp.h:164
struct net_if * iface
Network interface used by the session.
Definition rtp.h:156
RTP session state.
Definition rtp.h:230
uint32_t timestamp
Current RTP timestamp, advanced with each transmitted packet.
Definition rtp.h:242
uint16_t local_port
Local UDP port used when transmitting; 0 to auto-select (see CONFIG_RTP_LOCAL_PORT_BASE).
Definition rtp.h:249
struct rtp_transport transport
Transport info.
Definition rtp.h:232
size_t csrc_len
Number of active entries in csrc.
Definition rtp.h:254
const char * name
Human-readable session name used in debug log messages.
Definition rtp.h:258
struct k_mutex lock
Mutex protecting concurrent access to session state.
Definition rtp.h:237
uint32_t csrc[CONFIG_RTP_MAX_CSRC_COUNT]
Contributing source (CSRC) identifier list.
Definition rtp.h:252
uint16_t sequence_number
Sequence number of the next packet to be transmitted.
Definition rtp.h:244
struct rtp_session_context rtp_context
Session configuration context.
Definition rtp.h:234
uint32_t ssrc
Synchronization source (SSRC) identifier for this session.
Definition rtp.h:240
Internal transport state embedded in rtp_session.
Definition rtp.h:201
enum rtp_transport_type type
Active transport backend for this session.
Definition rtp.h:203
Misc utilities.
Macro utilities.