Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * include/avx_rtsp.h - Native RFC 2326 RTSP 1.0 & RFC 3550/6184/7798/9584 RTP Engine
7 */
8  
9 #ifndef AVX_RTSP_H
10 #define AVX_RTSP_H
11  
12 #include <stdint.h>
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include "openavx.h"
16  
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20  
21 /* Codec Identifiers for Elementary Streams */
22 typedef enum avx_rtsp_codec_e {
23 AVX_RTSP_CODEC_UNKNOWN = 0,
24 AVX_RTSP_CODEC_H264 = 1, /* Advanced Video Coding (AVC / H.264) */
25 AVX_RTSP_CODEC_HEVC = 2, /* High Efficiency Video Coding (H.265 / HEVC) */
26 AVX_RTSP_CODEC_AV1 = 3 /* AOMedia Video 1 (AV1) */
27 } avx_rtsp_codec_e;
28  
29 /* Reassembled Frame Type */
30 typedef enum avx_rtsp_frame_type_e {
31 AVX_RTSP_FRAME_TYPE_UNKNOWN = 0,
32 AVX_RTSP_FRAME_TYPE_KEYFRAME = 1, /* IDR / Keyframe (Instant Random Access) */
33 AVX_RTSP_FRAME_TYPE_DELTA = 2, /* P-Frame / B-Frame predicted slice */
34 AVX_RTSP_FRAME_TYPE_CONFIG = 3 /* Out-of-band parameter set update (SPS/PPS/VPS) */
35 } avx_rtsp_frame_type_e;
36  
37 /* Reassembled Elementary Stream Frame */
38 typedef struct avx_rtsp_frame_t {
39 avx_rtsp_codec_e codec;
40 avx_rtsp_frame_type_e frame_type;
41 uint32_t fourcc; /* OAVX_FOURCC_VIDEO_TYPE_H264, HEVC, AV01 */
42 int width; /* Display resolution extracted from SPS */
43 int height;
44 int frame_rate_n; /* Frame rate numerator (e.g. 60000) */
45 int frame_rate_d; /* Frame rate denominator (e.g. 1000) */
46 uint32_t rtp_timestamp;
47 int64_t timecode;
48 const uint8_t* data; /* Complete Annex-B byte stream with start codes */
49 size_t size;
50 } avx_rtsp_frame_t;
51  
52 /* Opaque Demuxer Context */
53 typedef struct avx_rtsp_demuxer_t avx_rtsp_demuxer_t;
54  
55 /* Opaque RTSP 1.0 Client Context */
56 typedef struct avx_rtsp_t avx_rtsp_t;
57  
58 /* ========================================================================= */
59 /* 1. DEMUXER LIFECYCLE & INGESTION APIS */
60 /* ========================================================================= */
61  
62 /**
63 * Creates an elementary stream demuxer instance.
64 * default_codec: Initial expected codec (AVX_RTSP_CODEC_H264 or AVX_RTSP_CODEC_HEVC).
65 */
66 OPENAVX_API avx_rtsp_demuxer_t* avx_rtsp_demuxer_create(avx_rtsp_codec_e default_codec);
67  
68 /**
69 * Destroys a demuxer instance and frees all internal reassembly buffers.
70 */
71 OPENAVX_API void avx_rtsp_demuxer_destroy(avx_rtsp_demuxer_t* demux);
72  
73 /**
74 * Resets demuxer reassembly buffers and sequence counters (retains cached parameter sets).
75 */
76 OPENAVX_API void avx_rtsp_demuxer_reset(avx_rtsp_demuxer_t* demux);
77  
78 /**
79 * Ingests an incoming RTP packet (RFC 3550, RFC 6184, RFC 7798, RFC 9584).
80 * Returns true if an entire Access Unit (video frame) was completed by this packet.
81 */
82 OPENAVX_API bool avx_rtsp_demuxer_feed_rtp(
83 avx_rtsp_demuxer_t* demux,
84 const uint8_t* rtp_packet,
85 size_t packet_size
86 );
87  
88 /**
89 * Ingests a raw NAL unit (e.g. from an OpenAVX stream socket or length-prefixed AVCC source).
90 * is_last_slice: true if this NAL completes the current video frame.
91 */
92 OPENAVX_API bool avx_rtsp_demuxer_feed_nalu(
93 avx_rtsp_demuxer_t* demux,
94 const uint8_t* nalu_data,
95 size_t nalu_size,
96 bool is_last_slice,
97 uint32_t timestamp
98 );
99  
100 /**
101 * Retrieves the most recently completed reassembled Annex-B frame.
102 * The buffer in out_frame->data remains valid until the next feed_* or reset call.
103 */
104 OPENAVX_API bool avx_rtsp_demuxer_get_frame(
105 avx_rtsp_demuxer_t* demux,
106 avx_rtsp_frame_t* out_frame
107 );
108  
109 /**
110 * Manually injects out-of-band parameter sets (e.g. from RTSP SDP sprop-parameter-sets).
111 * Cached parameter sets are automatically prepended to incoming orphan IDR keyframes.
112 */
113 OPENAVX_API bool avx_rtsp_demuxer_set_parameter_sets(
114 avx_rtsp_demuxer_t* demux,
115 avx_rtsp_codec_e codec,
116 const uint8_t* sps, size_t sps_len,
117 const uint8_t* pps, size_t pps_len,
118 const uint8_t* vps, size_t vps_len
119 );
120  
121 /* ========================================================================= */
122 /* 2. STANDALONE BITSTREAM & PARAMETER PARSING */
123 /* ========================================================================= */
124  
125 /**
126 * Parses an H.264 Sequence Parameter Set (SPS) in pure C99 (Exp-Golomb decoder).
127 * Extracts display width, height, profile, and level with cropping applied.
128 */
129 OPENAVX_API bool avx_rtsp_parse_sps_h264(
130 const uint8_t* sps,
131 size_t size,
132 int* out_width,
133 int* out_height,
134 int* out_profile,
135 int* out_level
136 );
137  
138 /**
139 * Parses an HEVC (H.265) Sequence Parameter Set (SPS) in pure C99.
140 * Extracts display width and height with conformance window offsets applied.
141 */
142 OPENAVX_API bool avx_rtsp_parse_sps_hevc(
143 const uint8_t* sps,
144 size_t size,
145 int* out_width,
146 int* out_height,
147 int* out_profile,
148 int* out_level
149 );
150  
151 /**
152 * Parses an AV1 Sequence Header OBU in pure C99.
153 * Extracts display width, height, profile, and level.
154 */
155 OPENAVX_API bool avx_rtsp_parse_sequence_header_av1(
156 const uint8_t* obu,
157 size_t size,
158 int* out_width,
159 int* out_height,
160 int* out_profile,
161 int* out_level
162 );
163  
164 /**
165 * Inspects a NAL unit to determine whether it is an IDR / Keyframe.
166 */
167 OPENAVX_API bool avx_rtsp_is_keyframe(avx_rtsp_codec_e codec, const uint8_t* nalu, size_t size);
168  
169 /* ========================================================================= */
170 /* 3. RTSP 1.0 CLIENT ENGINE (RFC 2326) */
171 /* ========================================================================= */
172  
173 /**
174 * Connects to an RTSP server (e.g. rtsp://192.168.1.100:554/live/ch0).
175 * Performs OPTIONS, DESCRIBE, parses SDP for tracks/codecs, and negotiates SETUP.
176 * Uses RTP-over-TCP interleaved mode by default for zero firewall issues.
177 */
178 OPENAVX_API avx_rtsp_t* avx_rtsp_connect(const char* url, int timeout_ms);
179  
180 /**
181 * Closes an RTSP session and issues TEARDOWN.
182 */
183 OPENAVX_API void avx_rtsp_close(avx_rtsp_t* rtsp);
184  
185 /**
186 * Sends an RTCP Picture Loss Indication (PLI / RFC 4585) or Full Intra Request (FIR)
187 * to request an instant IDR refresh from the camera.
188 */
189 OPENAVX_API bool avx_rtsp_send_keyframe_request(avx_rtsp_t* rtsp);
190  
191 /**
192 * Reads the next interleaved RTP packet or demuxed frame from the RTSP session.
193 * Automatically feeds demuxer and returns true when a complete frame is ready.
194 */
195 OPENAVX_API int avx_rtsp_read_frame(
196 avx_rtsp_t* rtsp,
197 avx_rtsp_demuxer_t* demux,
198 avx_rtsp_frame_t* out_frame,
199 int timeout_ms
200 );
201  
202 /* ========================================================================= */
203 /* 4. RTP PACKETIZATION ENGINE (RFC 6184, RFC 7798, RFC 9584) */
204 /* ========================================================================= */
205  
206 typedef void (*avx_rtsp_rtp_packet_cb)(const uint8_t *rtp_pkt, size_t rtp_len, void *user_data);
207  
208 /**
209 * Pure C99 Base64 Encoder (RFC 4648).
210 * dst must have capacity of at least ((src_len + 2) / 3) * 4 + 1.
211 * Returns length of generated base64 string excluding null terminator.
212 */
213 OPENAVX_API size_t avx_base64_encode(const uint8_t *src, size_t src_len, char *dst, size_t dst_max);
214  
215 /**
216 * Packetizes a NAL unit into standard RTP packets.
217 * For H.264: Single NAL (nalu_len <= 1400) or FU-A (RFC 6184 Section 5.8).
218 * For HEVC: Single NAL (nalu_len <= 1400) or FU (RFC 7798 Section 4.4.3).
219 * For AV1: Single OBU or fragmented chunks (RFC 9584).
220 * Calls cb(...) for each generated RTP packet.
221 * Returns the number of RTP packets generated.
222 */
223 OPENAVX_API size_t avx_rtsp_rtp_packetize_nalu(
224 avx_rtsp_codec_e codec,
225 const uint8_t *nalu,
226 size_t nalu_len,
227 uint32_t rtp_timestamp,
228 uint16_t *seq_num,
229 uint32_t ssrc,
230 bool is_last_nalu_in_frame,
231 avx_rtsp_rtp_packet_cb cb,
232 void *user_data
233 );
234  
235 /* ========================================================================= */
236 /* 5. NATIVE RTSP 1.0 SERVER ENGINE (RFC 2326, RFC 4566) */
237 /* ========================================================================= */
238  
239 typedef struct avx_rtsp_server_t avx_rtsp_server_t;
240  
241 typedef struct avx_rtsp_server_config_t {
242 uint16_t port; /* RTSP listener port (0 defaults to 8554, or ephemeral if specified) */
243 const char *path; /* RTSP mount path (e.g. "/live", NULL defaults to "/live") */
244 avx_rtsp_codec_e codec; /* Video codec: AVX_RTSP_CODEC_H264, HEVC, AV1 */
245 void (*on_keyframe_request)(void *user_ctx); /* Called when a client issues PLAY or sends RTCP PLI */
246 void *user_ctx; /* User context passed to callbacks */
247 } avx_rtsp_server_config_t;
248  
249 /**
250 * Creates and starts an RTSP 1.0 Server instance on a background thread.
251 * Returns server handle, or NULL on failure.
252 */
253 OPENAVX_API avx_rtsp_server_t* avx_rtsp_server_create(const avx_rtsp_server_config_t *config);
254  
255 /**
256 * Stops and destroys an RTSP 1.0 Server instance, disconnecting all clients.
257 */
258 OPENAVX_API void avx_rtsp_server_destroy(avx_rtsp_server_t *srv);
259  
260 /**
261 * Returns the number of actively playing RTSP clients.
262 */
263 OPENAVX_API int avx_rtsp_server_get_client_count(avx_rtsp_server_t *srv);
264  
265 /**
266 * Returns the active port number the RTSP server is listening on.
267 */
268 OPENAVX_API uint16_t avx_rtsp_server_get_port(avx_rtsp_server_t *srv);
269  
270 /**
271 * Ingests an Annex-B elementary stream frame, parses parameter sets,
272 * packetizes NALUs into RTP, and streams interleaved packets to all active RTSP clients.
273 */
274 OPENAVX_API bool avx_rtsp_server_feed_frame(
275 avx_rtsp_server_t *srv,
276 const uint8_t *data,
277 size_t size,
278 int64_t pts_ns,
279 bool is_keyframe
280 );
281  
282 #ifdef __cplusplus
283 }
284 #endif
285  
286 #endif /* AVX_RTSP_H */