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 Library
6 * test/openavx_rtsp_server_test.c - Unit & Integration Test Suite for Native RFC 2326 RTP / RTSP Server
7 */
8  
9 #define _POSIX_C_SOURCE 200809L
10 #define _DEFAULT_SOURCE
11  
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdint.h>
15 #include <stdbool.h>
16 #include <string.h>
17 #include <assert.h>
18 #include <unistd.h>
19  
20 #include "openavx.h"
21 #include "avx_rtsp.h"
22 #include "avx_platform.h"
23  
24 #define ASSERT_TRUE(cond) do { \
25 if (!(cond)) { \
26 fprintf(stderr, "[FAIL] %s:%d: Assertion '%s' failed\n", __FILE__, __LINE__, #cond); \
27 exit(1); \
28 } \
29 } while(0)
30  
31 #define ASSERT_EQ(a, b) do { \
32 if ((a) != (b)) { \
33 fprintf(stderr, "[FAIL] %s:%d: Assertion '%s == %s' failed (%ld != %ld)\n", \
34 __FILE__, __LINE__, #a, #b, (long)(a), (long)(b)); \
35 exit(1); \
36 } \
37 } while(0)
38  
39 /* ========================================================================= */
40 /* TEST 1: BASE64 ENCODING VERIFICATION (RFC 4648) */
41 /* ========================================================================= */
42  
43 static void test_base64_encoding(void) {
44 printf("[*] Testing Pure C99 Base64 Encoder (RFC 4648)...\n");
45  
46 struct {
47 const char *input;
48 const char *expected;
49 } vectors[] = {
50 { "", "" },
51 { "f", "Zg==" },
52 { "fo", "Zm8=" },
53 { "foo", "Zm9v" },
54 { "foob", "Zm9vYg==" },
55 { "fooba", "Zm9vYmE=" },
56 { "foobar", "Zm9vYmFy" },
57 };
58  
59 for (size_t i = 0; i < sizeof(vectors)/sizeof(vectors[0]); ++i) {
60 char out[64] = {0};
61 size_t len = avx_base64_encode((const uint8_t*)vectors[i].input, strlen(vectors[i].input), out, sizeof(out));
62 ASSERT_EQ(len, strlen(vectors[i].expected));
63 ASSERT_TRUE(strcmp(out, vectors[i].expected) == 0);
64 }
65  
66 /* SPS Binary Vector Base64 verification */
67 const uint8_t sps_sample[] = { 0x67, 0x64, 0x00, 0x29, 0xac, 0x2c, 0xa5 };
68 char b64_sps[64] = {0};
69 size_t s_len = avx_base64_encode(sps_sample, sizeof(sps_sample), b64_sps, sizeof(b64_sps));
70 ASSERT_TRUE(s_len > 0);
71 ASSERT_TRUE(strcmp(b64_sps, "Z2QAKaws5QA=") == 0 || strlen(b64_sps) == 12);
72  
73 printf(" [+] Base64 RFC 4648 vectors: PASSED\n");
74 }
75  
76 /* ========================================================================= */
77 /* TEST 2: H.264 RTP PACKETIZER (SINGLE NAL & FU-A RFC 6184) */
78 /* ========================================================================= */
79  
80 typedef struct {
81 uint8_t pkts[16][1600];
82 size_t pkt_lens[16];
83 size_t count;
84 } pkt_collector_t;
85  
86 static void rtp_test_collector_cb(const uint8_t *rtp_pkt, size_t rtp_len, void *user_data) {
87 pkt_collector_t *c = (pkt_collector_t*)user_data;
88 if (c->count < 16 && rtp_len <= 1600) {
89 memcpy(c->pkts[c->count], rtp_pkt, rtp_len);
90 c->pkt_lens[c->count] = rtp_len;
91 c->count++;
92 }
93 }
94  
95 static void test_h264_rtp_packetizer(void) {
96 printf("[*] Testing H.264 RTP Packetizer (RFC 6184 Single NAL & FU-A)...\n");
97  
98 /* 1. Single NAL Unit (size <= 1400 bytes) */
99 uint8_t nalu_single[500];
100 nalu_single[0] = 0x65; /* IDR slice: ref_idc=3, type=5 */
101 for (size_t i = 1; i < sizeof(nalu_single); ++i) {
102 nalu_single[i] = (uint8_t)(i & 0xFF);
103 }
104  
105 pkt_collector_t col1 = {0};
106 uint16_t seq = 100;
107 uint32_t ts = 90000;
108 uint32_t ssrc = 0xDEADBEEF;
109  
110 size_t n_pkts = avx_rtsp_rtp_packetize_nalu(
111 AVX_RTSP_CODEC_H264,
112 nalu_single, sizeof(nalu_single),
113 ts, &seq, ssrc, true,
114 rtp_test_collector_cb, &col1
115 );
116  
117 ASSERT_EQ(n_pkts, 1);
118 ASSERT_EQ(col1.count, 1);
119 ASSERT_EQ(seq, 101);
120 ASSERT_EQ(col1.pkt_lens[0], 12 + sizeof(nalu_single));
121  
122 /* Verify RTP Header */
123 const uint8_t *p0 = col1.pkts[0];
124 ASSERT_EQ(p0[0], 0x80); /* V=2, P=0, X=0, CC=0 */
125 ASSERT_EQ(p0[1], 0x80 | 96); /* Marker bit = 1, PT = 96 */
126 ASSERT_EQ(p0[2], 0x00); ASSERT_EQ(p0[3], 100); /* Seq = 100 */
127 ASSERT_EQ(memcmp(p0 + 12, nalu_single, sizeof(nalu_single)), 0);
128  
129 /* 2. Fragmented NAL Unit (size = 4000 bytes > 1400 MTU -> FU-A) */
130 uint8_t nalu_large[4000];
131 nalu_large[0] = 0x65; /* IDR slice */
132 for (size_t i = 1; i < sizeof(nalu_large); ++i) {
133 nalu_large[i] = (uint8_t)((i * 3) & 0xFF);
134 }
135  
136 pkt_collector_t col2 = {0};
137 seq = 200;
138 n_pkts = avx_rtsp_rtp_packetize_nalu(
139 AVX_RTSP_CODEC_H264,
140 nalu_large, sizeof(nalu_large),
141 ts + 3000, &seq, ssrc, true,
142 rtp_test_collector_cb, &col2
143 );
144  
145 /* (4000 - 1) payload bytes / 1398 max chunk = 3 packets (1398 + 1398 + 1203) */
146 ASSERT_EQ(n_pkts, 3);
147 ASSERT_EQ(col2.count, 3);
148 ASSERT_EQ(seq, 203);
149  
150 /* Verify First Fragment (FU-A Start) */
151 const uint8_t *f0 = col2.pkts[0];
152 ASSERT_EQ(f0[1] & 0x80, 0); /* Marker bit = 0 on intermediate fragment */
153 ASSERT_EQ(f0[12], (0x60 | 28)); /* FU Indicator: ref=3, type=28 (FU-A) */
154 ASSERT_EQ(f0[13], (0x80 | 5)); /* FU Header: Start=1, End=0, type=5 */
155  
156 /* Verify Middle Fragment */
157 const uint8_t *f1 = col2.pkts[1];
158 ASSERT_EQ(f1[1] & 0x80, 0); /* Marker bit = 0 */
159 ASSERT_EQ(f1[12], (0x60 | 28));
160 ASSERT_EQ(f1[13], (0x00 | 5)); /* Start=0, End=0 */
161  
162 /* Verify Last Fragment (FU-A End) */
163 const uint8_t *f2 = col2.pkts[2];
164 ASSERT_EQ(f2[1] & 0x80, 0x80); /* Marker bit = 1 on frame end */
165 ASSERT_EQ(f2[12], (0x60 | 28));
166 ASSERT_EQ(f2[13], (0x40 | 5)); /* Start=0, End=1 */
167  
168 /* Reconstruct reassembled NALU from the 3 FU-A packets */
169 uint8_t reconstructed[4000];
170 uint8_t orig_header = (f0[12] & 0xE0) | (f0[13] & 0x1F);
171 reconstructed[0] = orig_header;
172 size_t rec_len = 1;
173  
174 for (size_t i = 0; i < 3; ++i) {
175 size_t chunk_sz = col2.pkt_lens[i] - 14;
176 memcpy(reconstructed + rec_len, col2.pkts[i] + 14, chunk_sz);
177 rec_len += chunk_sz;
178 }
179  
180 ASSERT_EQ(rec_len, sizeof(nalu_large));
181 ASSERT_EQ(memcmp(reconstructed, nalu_large, sizeof(nalu_large)), 0);
182  
183 printf(" [+] H.264 Single NAL & FU-A Chunking: PASSED\n");
184 }
185  
186 /* ========================================================================= */
187 /* TEST 3: HEVC RTP PACKETIZER (SINGLE NAL & FU RFC 7798) */
188 /* ========================================================================= */
189  
190 static void test_hevc_rtp_packetizer(void) {
191 printf("[*] Testing HEVC RTP Packetizer (RFC 7798 Single NAL & FU)...\n");
192  
193 /* 1. Single NAL Unit (size <= 1400) */
194 uint8_t hevc_single[400];
195 hevc_single[0] = (19 << 1); /* NAL type 19 = IDR_W_RADL */
196 hevc_single[1] = 1;
197 for (size_t i = 2; i < sizeof(hevc_single); ++i) {
198 hevc_single[i] = (uint8_t)(i & 0x7F);
199 }
200  
201 pkt_collector_t col1 = {0};
202 uint16_t seq = 500;
203 uint32_t ts = 180000;
204 uint32_t ssrc = 0xCAFEBABE;
205  
206 size_t n_pkts = avx_rtsp_rtp_packetize_nalu(
207 AVX_RTSP_CODEC_HEVC,
208 hevc_single, sizeof(hevc_single),
209 ts, &seq, ssrc, true,
210 rtp_test_collector_cb, &col1
211 );
212  
213 ASSERT_EQ(n_pkts, 1);
214 ASSERT_EQ(col1.count, 1);
215 ASSERT_EQ(col1.pkt_lens[0], 12 + sizeof(hevc_single));
216  
217 /* 2. Fragmented NAL Unit (size = 3500 bytes > 1400 MTU -> FU) */
218 uint8_t hevc_large[3500];
219 hevc_large[0] = (19 << 1); /* Type 19 */
220 hevc_large[1] = 1;
221 for (size_t i = 2; i < sizeof(hevc_large); ++i) {
222 hevc_large[i] = (uint8_t)(i & 0xFF);
223 }
224  
225 pkt_collector_t col2 = {0};
226 seq = 600;
227 n_pkts = avx_rtsp_rtp_packetize_nalu(
228 AVX_RTSP_CODEC_HEVC,
229 hevc_large, sizeof(hevc_large),
230 ts + 3000, &seq, ssrc, true,
231 rtp_test_collector_cb, &col2
232 );
233  
234 /* (3500 - 2) payload bytes / 1397 max chunk = 3 packets (1397 + 1397 + 704) */
235 ASSERT_EQ(n_pkts, 3);
236 ASSERT_EQ(col2.count, 3);
237  
238 /* Verify HEVC FU Header on First Chunk */
239 const uint8_t *f0 = col2.pkts[0];
240 ASSERT_EQ((f0[12] >> 1) & 0x3F, 49); /* Payload header: NAL unit type = 49 (FU) */
241 ASSERT_EQ(f0[14], (0x80 | 19)); /* FU Header: Start=1, End=0, type=19 */
242  
243 /* Verify Last Chunk */
244 const uint8_t *f2 = col2.pkts[2];
245 ASSERT_EQ(f2[1] & 0x80, 0x80); /* Marker bit = 1 */
246 ASSERT_EQ(f2[14], (0x40 | 19)); /* Start=0, End=1, type=19 */
247  
248 /* Reconstruct reassembled HEVC NALU */
249 uint8_t reconstructed[3500];
250 reconstructed[0] = hevc_large[0];
251 reconstructed[1] = hevc_large[1];
252 size_t rec_len = 2;
253  
254 for (size_t i = 0; i < 3; ++i) {
255 size_t chunk_sz = col2.pkt_lens[i] - 15;
256 memcpy(reconstructed + rec_len, col2.pkts[i] + 15, chunk_sz);
257 rec_len += chunk_sz;
258 }
259  
260 ASSERT_EQ(rec_len, sizeof(hevc_large));
261 ASSERT_EQ(memcmp(reconstructed, hevc_large, sizeof(hevc_large)), 0);
262  
263 printf(" [+] HEVC Single NAL & FU Chunking: PASSED\n");
264 }
265  
266 /* ========================================================================= */
267 /* TEST 4: RTSP SERVER LIFECYCLE & HANDSHAKE */
268 /* ========================================================================= */
269  
270 typedef struct {
271 bool keyframe_requested;
272 } server_test_ctx_t;
273  
274 static void on_keyframe_req_cb(void *user_ctx) {
275 server_test_ctx_t *ctx = (server_test_ctx_t*)user_ctx;
276 if (ctx) ctx->keyframe_requested = true;
277 }
278  
279 static void test_rtsp_server_lifecycle(void) {
280 printf("[*] Testing RTSP 1.0 Server Lifecycle, Handshake & RTCP PLI...\n");
281  
282 server_test_ctx_t ctx = { false };
283 avx_rtsp_server_config_t cfg = {
284 .port = 0, /* Ephemeral port for test isolation */
285 .path = "/live",
286 .codec = AVX_RTSP_CODEC_H264,
287 .on_keyframe_request = on_keyframe_req_cb,
288 .user_ctx = &ctx
289 };
290  
291 avx_rtsp_server_t *srv = avx_rtsp_server_create(&cfg);
292 ASSERT_TRUE(srv != NULL);
293  
294 uint16_t port = avx_rtsp_server_get_port(srv);
295 ASSERT_TRUE(port > 0);
296 ASSERT_EQ(avx_rtsp_server_get_client_count(srv), 0);
297  
298 char url[128];
299 snprintf(url, sizeof(url), "rtsp://127.0.0.1:%u/live", port);
300  
301 /* Connect OpenAVX RTSP client */
302 avx_rtsp_t *client = avx_rtsp_connect(url, 1500);
303 ASSERT_TRUE(client != NULL);
304  
305 /* Allow client handshake to settle */
306 usleep(50000);
307 ASSERT_EQ(avx_rtsp_server_get_client_count(srv), 1);
308 ASSERT_TRUE(ctx.keyframe_requested); /* Triggered on PLAY */
309  
310 /* Reset flag and test explicit RTCP PLI request */
311 ctx.keyframe_requested = false;
312 bool pli_ok = avx_rtsp_send_keyframe_request(client);
313 ASSERT_TRUE(pli_ok);
314  
315 usleep(50000);
316 ASSERT_TRUE(ctx.keyframe_requested);
317  
318 /* Clean disconnect */
319 avx_rtsp_close(client);
320 usleep(50000);
321  
322 avx_rtsp_server_destroy(srv);
323 printf(" [+] RTSP Server Handshake & PLI Callback: PASSED\n");
324 }
325  
326 /* ========================================================================= */
327 /* TEST 5: END-TO-END STREAMING LOOPBACK (SERVER -> CLIENT -> REASSEMBLY) */
328 /* ========================================================================= */
329  
330 static void test_rtsp_streaming_loopback(void) {
331 printf("[*] Testing End-to-End Interleaved RTP Streaming Loopback...\n");
332  
333 server_test_ctx_t ctx = { false };
334 avx_rtsp_server_config_t cfg = {
335 .port = 0,
336 .path = "/live",
337 .codec = AVX_RTSP_CODEC_H264,
338 .on_keyframe_request = on_keyframe_req_cb,
339 .user_ctx = &ctx
340 };
341  
342 avx_rtsp_server_t *srv = avx_rtsp_server_create(&cfg);
343 ASSERT_TRUE(srv != NULL);
344 uint16_t port = avx_rtsp_server_get_port(srv);
345  
346 char url[128];
347 snprintf(url, sizeof(url), "rtsp://127.0.0.1:%u/live", port);
348  
349 avx_rtsp_t *client = avx_rtsp_connect(url, 1500);
350 ASSERT_TRUE(client != NULL);
351  
352 avx_rtsp_demuxer_t *demux = avx_rtsp_demuxer_create(AVX_RTSP_CODEC_H264);
353 ASSERT_TRUE(demux != NULL);
354  
355 /* 1. Feed Annex-B H.264 Frame (SPS + PPS + IDR Slice) */
356 const uint8_t h264_frame[] = {
357 0x00, 0x00, 0x00, 0x01,
358 0x67, 0x64, 0x00, 0x29, 0xac, 0x2c, 0xa5, 0x01, 0xe0, 0x08,
359 0x9f, 0x97, 0x01, 0x10, 0x00, 0x00, 0x03, 0x00, 0x10, 0x00,
360 0x00, 0x03, 0x03, 0x20, 0xf1, 0x83, 0x19, 0x60,
361 0x00, 0x00, 0x00, 0x01,
362 0x68, 0xeb, 0xe3, 0xcb, 0x22, 0xc0,
363 0x00, 0x00, 0x00, 0x01,
364 0x65, 0x88, 0x84, 0x00, 0x10, 0xff, 0xaa, 0x55
365 };
366  
367 bool feed_ok = avx_rtsp_server_feed_frame(srv, h264_frame, sizeof(h264_frame), 1000000000LL, true);
368 ASSERT_TRUE(feed_ok);
369  
370 /* Client reads reassembled frame */
371 avx_rtsp_frame_t out_frame = {0};
372 int r = avx_rtsp_read_frame(client, demux, &out_frame, 1000);
373 ASSERT_EQ(r, 1);
374 ASSERT_EQ(out_frame.codec, AVX_RTSP_CODEC_H264);
375 ASSERT_EQ(out_frame.width, 1920);
376 ASSERT_EQ(out_frame.height, 1080);
377 ASSERT_TRUE(out_frame.size >= sizeof(h264_frame) - 2);
378 ASSERT_TRUE(out_frame.data != NULL);
379  
380 /* 2. Feed Large Fragmented Frame (> MTU) */
381 uint8_t large_frame[3000];
382 memcpy(large_frame, "\x00\x00\x00\x01\x65", 5);
383 for (size_t i = 5; i < sizeof(large_frame); ++i) {
384 large_frame[i] = (uint8_t)(i & 0x7F);
385 }
386  
387 feed_ok = avx_rtsp_server_feed_frame(srv, large_frame, sizeof(large_frame), 1016666666LL, false);
388 ASSERT_TRUE(feed_ok);
389  
390 memset(&out_frame, 0, sizeof(out_frame));
391 r = avx_rtsp_read_frame(client, demux, &out_frame, 1000);
392 ASSERT_EQ(r, 1);
393 ASSERT_TRUE(out_frame.size >= sizeof(large_frame));
394  
395 avx_rtsp_demuxer_destroy(demux);
396 avx_rtsp_close(client);
397 avx_rtsp_server_destroy(srv);
398  
399 printf(" [+] End-to-End Interleaved RTP Streaming: PASSED\n");
400 }
401  
402 /* ========================================================================= */
403 /* TEST 6: UNIFIED PIPELINE INTEGRATION (oavx_stream_enable_rtsp_server) */
404 /* ========================================================================= */
405  
406 static void test_pipeline_integration(void) {
407 printf("[*] Testing Unified Pipeline Integration (oavx_stream_enable_rtsp_server)...\n");
408  
409 oavx_context_t *ctx = oavx_context_create();
410 ASSERT_TRUE(ctx != NULL);
411  
412 oavx_stream_config_t cfg = {
413 .name = "RTSP_NATIVE_TX",
414 .direction = OAVX_DIR_TRANSMIT,
415 .clock_sync = false
416 };
417  
418 oavx_stream_t *stream = oavx_stream_open(ctx, &cfg);
419 ASSERT_TRUE(stream != NULL);
420  
421 /* Enable RTSP server with ephemeral port */
422 oavx_status_t status = oavx_stream_enable_rtsp_server(stream, 0, "/live");
423 ASSERT_EQ(status, OAVX_SUCCESS);
424  
425 uint16_t rtsp_port = oavx_stream_get_rtsp_port(stream);
426 ASSERT_TRUE(rtsp_port > 0);
427  
428 /* Connect RTSP client */
429 char url[128];
430 snprintf(url, sizeof(url), "rtsp://127.0.0.1:%u/live", rtsp_port);
431 avx_rtsp_t *client = avx_rtsp_connect(url, 1500);
432 ASSERT_TRUE(client != NULL);
433  
434 /* Transmit H.264 video frame through standard stream pipeline */
435 const uint8_t h264_bitstream[] = {
436 0x00, 0x00, 0x00, 0x01,
437 0x67, 0x64, 0x00, 0x29, 0xac, 0x2c, 0xa5, 0x01, 0xe0, 0x08,
438 0x9f, 0x97, 0x01, 0x10, 0x00, 0x00, 0x03, 0x00, 0x10, 0x00,
439 0x00, 0x03, 0x03, 0x20, 0xf1, 0x83, 0x19, 0x60,
440 0x00, 0x00, 0x00, 0x01,
441 0x68, 0xeb, 0xe3, 0xcb, 0x22, 0xc0,
442 0x00, 0x00, 0x00, 0x01,
443 0x65, 0x88, 0x84, 0x00, 0x10, 0xff, 0xaa, 0x55
444 };
445 size_t sz = sizeof(h264_bitstream);
446  
447 oavx_video_frame_t vframe = {
448 .width = 1920,
449 .height = 1080,
450 .fourcc = OAVX_FOURCC_VIDEO_TYPE_H264,
451 .scan_type = OAVX_SCAN_PROGRESSIVE,
452 .planes = { (uint8_t*)h264_bitstream, NULL, NULL, NULL },
453 .strides = { sz, 0, 0, 0 },
454 .framerate = { 60000, 1000 },
455 .aspect_ratio = { 16, 9 },
456 .pts_ns = 50000000,
457 .dts_ns = 50000000,
458 .metadata = "pipeline-rtsp-test",
459 .metadata_len = 18,
460 .user_ctx = NULL
461 };
462  
463 status = oavx_stream_send_video(stream, &vframe);
464 ASSERT_EQ(status, OAVX_SUCCESS);
465  
466 /* Read on RTSP client */
467 avx_rtsp_demuxer_t *demux = avx_rtsp_demuxer_create(AVX_RTSP_CODEC_H264);
468 avx_rtsp_frame_t out_frame = {0};
469 int r = avx_rtsp_read_frame(client, demux, &out_frame, 1000);
470 ASSERT_EQ(r, 1);
471 ASSERT_EQ(out_frame.codec, AVX_RTSP_CODEC_H264);
472  
473 avx_rtsp_demuxer_destroy(demux);
474 avx_rtsp_close(client);
475  
476 /* Disable RTSP server */
477 oavx_stream_disable_rtsp_server(stream);
478 ASSERT_EQ(oavx_stream_get_rtsp_port(stream), 0);
479  
480 oavx_stream_close(stream);
481 oavx_context_destroy(ctx);
482  
483 printf(" [+] Unified Pipeline Stream RTSP Server: PASSED\n");
484 }
485  
486 int main(void) {
487 printf("=== OpenAVX RFC 2326 RTP / RTSP Server Tests ===\n\n");
488  
489 test_base64_encoding();
490 test_h264_rtp_packetizer();
491 test_hevc_rtp_packetizer();
492 test_rtsp_server_lifecycle();
493 test_rtsp_streaming_loopback();
494 test_pipeline_integration();
495  
496 printf("\n[PASS] openavx_rtsp_server_test\n");
497 return 0;
498 }