Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2026 The TokTok team. |
3 | | * Copyright © 2013-2015 Tox project. |
4 | | */ |
5 | | #ifndef C_TOXCORE_TOXAV_RTP_H |
6 | | #define C_TOXCORE_TOXAV_RTP_H |
7 | | |
8 | | #include <stdbool.h> |
9 | | #include <stddef.h> |
10 | | #include <stdint.h> |
11 | | |
12 | | #include "../toxcore/logger.h" |
13 | | #include "../toxcore/mono_time.h" |
14 | | |
15 | | #ifdef __cplusplus |
16 | | extern "C" { |
17 | | #endif |
18 | | |
19 | | /** |
20 | | * RTPHeader serialised size in bytes. |
21 | | */ |
22 | 593k | #define RTP_HEADER_SIZE 80 |
23 | | |
24 | | /** |
25 | | * Number of 32 bit padding fields between @ref RTPHeader::offset_lower and |
26 | | * everything before it. |
27 | | */ |
28 | 858k | #define RTP_PADDING_FIELDS 11 |
29 | | |
30 | | /** |
31 | | * Payload type identifier. Also used as rtp callback prefix. |
32 | | */ |
33 | | typedef enum RTP_Type { |
34 | | RTP_TYPE_AUDIO = 192, |
35 | | RTP_TYPE_VIDEO = 193, |
36 | | } RTP_Type; |
37 | | |
38 | | /** |
39 | | * A bit mask (up to 64 bits) specifying features of the current frame affecting |
40 | | * the behaviour of the decoder. |
41 | | */ |
42 | | typedef enum RTPFlags { |
43 | | /** |
44 | | * Support frames larger than 64KiB. The full 32 bit length and offset are |
45 | | * set in @ref RTPHeader::data_length_full and @ref RTPHeader::offset_full. |
46 | | */ |
47 | | RTP_LARGE_FRAME = 1 << 0, |
48 | | /** |
49 | | * Whether the packet is part of a key frame. |
50 | | */ |
51 | | RTP_KEY_FRAME = 1 << 1, |
52 | | } RTPFlags; |
53 | | |
54 | | typedef struct RTPHeader RTPHeader; |
55 | | typedef struct RTPMessage RTPMessage; |
56 | | typedef struct RTPSession RTPSession; |
57 | | |
58 | | /* RTPMessage accessors */ |
59 | | const uint8_t *_Nonnull rtp_message_data(const RTPMessage *_Nonnull msg); |
60 | | uint32_t rtp_message_len(const RTPMessage *_Nonnull msg); |
61 | | uint8_t rtp_message_pt(const RTPMessage *_Nonnull msg); |
62 | | uint16_t rtp_message_sequnum(const RTPMessage *_Nonnull msg); |
63 | | uint64_t rtp_message_flags(const RTPMessage *_Nonnull msg); |
64 | | uint32_t rtp_message_data_length_full(const RTPMessage *_Nonnull msg); |
65 | | |
66 | | /* RTPSession accessors */ |
67 | | bool rtp_session_is_receiving_active(const RTPSession *_Nullable session); |
68 | | uint32_t rtp_session_get_ssrc(const RTPSession *_Nonnull session); |
69 | | void rtp_session_set_ssrc(RTPSession *_Nonnull session, uint32_t ssrc); |
70 | | |
71 | 142 | #define USED_RTP_WORKBUFFER_COUNT 3 |
72 | 131 | #define DISMISS_FIRST_LOST_VIDEO_PACKET_COUNT 10 |
73 | | |
74 | | typedef int rtp_m_cb(const Mono_Time *_Nonnull mono_time, void *_Nonnull cs, RTPMessage *_Nonnull msg); |
75 | | |
76 | | typedef int rtp_send_packet_cb(void *_Nullable user_data, const uint8_t *_Nonnull data, uint16_t length); |
77 | | typedef void rtp_add_recv_cb(void *_Nullable user_data, uint32_t bytes); |
78 | | typedef void rtp_add_lost_cb(void *_Nullable user_data, uint32_t bytes); |
79 | | |
80 | | void rtp_receive_packet(RTPSession *_Nonnull session, const uint8_t *_Nonnull data, size_t length); |
81 | | |
82 | | /** |
83 | | * Serialise an RTPHeader to bytes to be sent over the network. |
84 | | * |
85 | | * @param rdata A byte array of length RTP_HEADER_SIZE. Does not need to be |
86 | | * initialised. All RTP_HEADER_SIZE bytes will be initialised after a call |
87 | | * to this function. |
88 | | * @param header The RTPHeader to serialise. |
89 | | */ |
90 | | size_t rtp_header_pack(uint8_t *_Nonnull rdata, const struct RTPHeader *_Nonnull header); |
91 | | |
92 | | /** |
93 | | * Deserialise an RTPHeader from bytes received over the network. |
94 | | * |
95 | | * @param data A byte array of length RTP_HEADER_SIZE. |
96 | | * @param header The RTPHeader to write the unpacked values to. |
97 | | */ |
98 | | size_t rtp_header_unpack(const uint8_t *_Nonnull data, struct RTPHeader *_Nonnull header); |
99 | | |
100 | | RTPSession *_Nullable rtp_new(const Logger *_Nonnull log, int payload_type, Mono_Time *_Nonnull mono_time, |
101 | | rtp_send_packet_cb *_Nullable send_packet, void *_Nullable send_packet_user_data, |
102 | | rtp_add_recv_cb *_Nullable add_recv, rtp_add_lost_cb *_Nullable add_lost, void *_Nullable bwc_user_data, |
103 | | void *_Nonnull cs, rtp_m_cb *_Nonnull mcb); |
104 | | void rtp_kill(const Logger *_Nonnull log, RTPSession *_Nullable session); |
105 | | void rtp_allow_receiving_mark(RTPSession *_Nullable session); |
106 | | void rtp_stop_receiving_mark(RTPSession *_Nullable session); |
107 | | |
108 | | /** |
109 | | * @brief Send a frame of audio or video data, chunked in @ref RTPMessage instances. |
110 | | * |
111 | | * @param session The A/V session to send the data for. |
112 | | * @param data A byte array of length @p length. |
113 | | * @param length The number of bytes to send from @p data. |
114 | | * @param is_keyframe Whether this video frame is a key frame. If it is an |
115 | | * audio frame, this parameter is ignored. |
116 | | */ |
117 | | int rtp_send_data(const Logger *_Nonnull log, RTPSession *_Nonnull session, const uint8_t *_Nonnull data, uint32_t length, |
118 | | bool is_keyframe); |
119 | | |
120 | | #ifdef __cplusplus |
121 | | } /* extern "C" */ |
122 | | #endif |
123 | | |
124 | | #endif /* C_TOXCORE_TOXAV_RTP_H */ |