+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 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_web_server_test.c - Unit & Integration Test Suite for Embedded Web Preview 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 | #include <sys/socket.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <arpa/inet.h> |
| 22 | |
| 23 | #include "openavx.h" |
| 24 | #include "avx_web.h" |
| 25 | #include "avx_rtsp.h" |
| 26 | #include "avx_platform.h" |
| 27 | |
| 28 | #define ASSERT_TRUE(cond) do { \ |
| 29 | if (!(cond)) { \ |
| 30 | fprintf(stderr, "[FAIL] %s:%d: Assertion '%s' failed\n", __FILE__, __LINE__, #cond); \ |
| 31 | exit(1); \ |
| 32 | } \ |
| 33 | } while(0) |
| 34 | |
| 35 | #define ASSERT_EQ(a, b) do { \ |
| 36 | if ((a) != (b)) { \ |
| 37 | fprintf(stderr, "[FAIL] %s:%d: Assertion '%s == %s' failed (%ld != %ld)\n", \ |
| 38 | __FILE__, __LINE__, #a, #b, (long)(a), (long)(b)); \ |
| 39 | exit(1); \ |
| 40 | } \ |
| 41 | } while(0) |
| 42 | |
| 43 | /* Helper: read exact number of bytes from a TCP socket with timeout */ |
| 44 | static bool socket_read_exact(int fd, uint8_t *buf, size_t len, int timeout_ms) { |
| 45 | size_t received = 0; |
| 46 | while (received < len) { |
| 47 | avx_pollfd_t pfd; |
| 48 | pfd.fd = fd; |
| 49 | pfd.events = POLLIN; |
| 50 | pfd.revents = 0; |
| 51 | int pr = avx_poll(&pfd, 1, timeout_ms); |
| 52 | if (pr <= 0) return false; |
| 53 | |
| 54 | ssize_t n = recv(fd, buf + received, len - received, 0); |
| 55 | if (n <= 0) return false; |
| 56 | received += (size_t)n; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | /* ========================================================================= */ |
| 62 | /* TEST 1: ISOBMFF INIT SEGMENT GENERATOR (ftyp + moov) */ |
| 63 | /* ========================================================================= */ |
| 64 | |
| 65 | static void test_fmp4_init_segment(void) { |
| 66 | printf("[*] Testing fMP4 Init Segment Generator (ftyp + moov)...\n"); |
| 67 | |
| 68 | /* Synthetic H.264 SPS & PPS */ |
| 69 | uint8_t sps[] = { 0x67, 0x64, 0x00, 0x28, 0xAC, 0xD9, 0x40, 0x78, 0x02, 0x27, 0xE5, 0x84 }; |
| 70 | uint8_t pps[] = { 0x68, 0xEB, 0xEF, 0x20 }; |
| 71 | |
| 72 | uint8_t init_buf[4096]; |
| 73 | size_t len = avx_fmp4_generate_init_segment( |
| 74 | AVX_RTSP_CODEC_H264, |
| 75 | 1920, 1080, |
| 76 | sps, sizeof(sps), |
| 77 | pps, sizeof(pps), |
| 78 | NULL, 0, |
| 79 | init_buf, sizeof(init_buf) |
| 80 | ); |
| 81 | |
| 82 | ASSERT_TRUE(len > 100); |
| 83 | ASSERT_TRUE(len < sizeof(init_buf)); |
| 84 | |
| 85 | /* Verify ftyp Box */ |
| 86 | uint32_t ftyp_size = (init_buf[0] << 24) | (init_buf[1] << 16) | (init_buf[2] << 8) | init_buf[3]; |
| 87 | ASSERT_EQ(ftyp_size, 32); |
| 88 | ASSERT_TRUE(memcmp(init_buf + 4, "ftyp", 4) == 0); |
| 89 | ASSERT_TRUE(memcmp(init_buf + 8, "isom", 4) == 0); |
| 90 | |
| 91 | /* Verify moov Box follows ftyp */ |
| 92 | const uint8_t *moov = init_buf + ftyp_size; |
| 93 | uint32_t moov_size = (moov[0] << 24) | (moov[1] << 16) | (moov[2] << 8) | moov[3]; |
| 94 | ASSERT_TRUE(memcmp(moov + 4, "moov", 4) == 0); |
| 95 | ASSERT_EQ(ftyp_size + moov_size, len); |
| 96 | |
| 97 | /* Test HEVC init segment */ |
| 98 | uint8_t vps_hevc[] = { 0x40, 0x01, 0x0C, 0x01, 0xFF, 0xFF, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0x90, 0x00 }; |
| 99 | uint8_t sps_hevc[] = { 0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0x90, 0x00, 0x00 }; |
| 100 | uint8_t pps_hevc[] = { 0x44, 0x01, 0xC0, 0xF7, 0xC0 }; |
| 101 | |
| 102 | size_t hevc_len = avx_fmp4_generate_init_segment( |
| 103 | AVX_RTSP_CODEC_HEVC, |
| 104 | 3840, 2160, |
| 105 | sps_hevc, sizeof(sps_hevc), |
| 106 | pps_hevc, sizeof(pps_hevc), |
| 107 | vps_hevc, sizeof(vps_hevc), |
| 108 | init_buf, sizeof(init_buf) |
| 109 | ); |
| 110 | ASSERT_TRUE(hevc_len > 100); |
| 111 | |
| 112 | /* Test AV1 init segment */ |
| 113 | uint8_t seq_obu[] = { 0x0A, 0x0E, 0x00, 0x00, 0x00, 0x4A, 0xAB, 0xBF, 0xC3, 0x70, 0x0B, 0x80, 0x20, 0x1D, 0x80 }; |
| 114 | size_t av1_len = avx_fmp4_generate_init_segment( |
| 115 | AVX_RTSP_CODEC_AV1, |
| 116 | 1280, 720, |
| 117 | seq_obu, sizeof(seq_obu), |
| 118 | NULL, 0, |
| 119 | NULL, 0, |
| 120 | init_buf, sizeof(init_buf) |
| 121 | ); |
| 122 | ASSERT_TRUE(av1_len > 100); |
| 123 | |
| 124 | /* Test bounds safety with undersized buffer */ |
| 125 | size_t fail_len = avx_fmp4_generate_init_segment( |
| 126 | AVX_RTSP_CODEC_H264, |
| 127 | 1920, 1080, |
| 128 | sps, sizeof(sps), |
| 129 | pps, sizeof(pps), |
| 130 | NULL, 0, |
| 131 | init_buf, 64 |
| 132 | ); |
| 133 | ASSERT_EQ(fail_len, 0); |
| 134 | |
| 135 | printf(" [+] fMP4 Init Segments (H.264, HEVC, AV1) generated and verified!\n"); |
| 136 | } |
| 137 | |
| 138 | /* ========================================================================= */ |
| 139 | /* TEST 2: ISOBMFF MEDIA FRAGMENT GENERATOR (moof + mdat) */ |
| 140 | /* ========================================================================= */ |
| 141 | |
| 142 | static void test_fmp4_media_fragment(void) { |
| 143 | printf("[*] Testing fMP4 Media Fragment Generator (moof + mdat)...\n"); |
| 144 | |
| 145 | /* Synthetic AVCC sample payload (4-byte length + NALU) */ |
| 146 | uint8_t sample_avcc[] = { |
| 147 | 0x00, 0x00, 0x00, 0x05, /* 5 bytes NALU length */ |
| 148 | 0x65, 0x88, 0x84, 0x00, 0x10 /* IDR slice NALU */ |
| 149 | }; |
| 150 | |
| 151 | uint8_t frag_buf[1024]; |
| 152 | size_t frag_len = avx_fmp4_generate_media_fragment( |
| 153 | 1, /* sequence_number */ |
| 154 | 90000, /* base_decode_time (1.0s at 90kHz) */ |
| 155 | 1500, /* duration (60fps) */ |
| 156 | true, /* is_keyframe */ |
| 157 | sample_avcc, |
| 158 | sizeof(sample_avcc), |
| 159 | frag_buf, |
| 160 | sizeof(frag_buf) |
| 161 | ); |
| 162 | |
| 163 | ASSERT_TRUE(frag_len > sizeof(sample_avcc)); |
| 164 | ASSERT_TRUE(frag_len < sizeof(frag_buf)); |
| 165 | |
| 166 | /* Verify moof Box */ |
| 167 | uint32_t moof_size = (frag_buf[0] << 24) | (frag_buf[1] << 16) | (frag_buf[2] << 8) | frag_buf[3]; |
| 168 | ASSERT_TRUE(memcmp(frag_buf + 4, "moof", 4) == 0); |
| 169 | |
| 170 | /* Verify mdat Box follows moof */ |
| 171 | const uint8_t *mdat = frag_buf + moof_size; |
| 172 | uint32_t mdat_size = (mdat[0] << 24) | (mdat[1] << 16) | (mdat[2] << 8) | mdat[3]; |
| 173 | ASSERT_TRUE(memcmp(mdat + 4, "mdat", 4) == 0); |
| 174 | ASSERT_EQ(mdat_size, 8 + sizeof(sample_avcc)); |
| 175 | ASSERT_TRUE(memcmp(mdat + 8, sample_avcc, sizeof(sample_avcc)) == 0); |
| 176 | ASSERT_EQ(moof_size + mdat_size, frag_len); |
| 177 | |
| 178 | /* Verify trun data_offset matches moof_size + 8 */ |
| 179 | uint32_t expected_data_offset = moof_size + 8; |
| 180 | /* In trun, data_offset is at offset: moof(8) + mfhd(16) + traf(8) + tfhd(16) + tfdt(20) + trun_hdr(12) + sample_cnt(4) = 84 */ |
| 181 | const uint8_t *p_trun_do = frag_buf + 84; |
| 182 | uint32_t trun_do = (p_trun_do[0] << 24) | (p_trun_do[1] << 16) | (p_trun_do[2] << 8) | p_trun_do[3]; |
| 183 | ASSERT_EQ(trun_do, expected_data_offset); |
| 184 | |
| 185 | /* Test bounds safety */ |
| 186 | size_t fail_len = avx_fmp4_generate_media_fragment( |
| 187 | 1, 90000, 1500, true, sample_avcc, sizeof(sample_avcc), frag_buf, 32 |
| 188 | ); |
| 189 | ASSERT_EQ(fail_len, 0); |
| 190 | |
| 191 | printf(" [+] fMP4 Media Fragments generated and box offsets verified!\n"); |
| 192 | } |
| 193 | |
| 194 | /* ========================================================================= */ |
| 195 | /* TEST 3: WEB SERVER LIFECYCLE & HTTP ENDPOINTS */ |
| 196 | /* ========================================================================= */ |
| 197 | |
| 198 | static void test_web_server_endpoints(void) { |
| 199 | printf("[*] Testing Web Server Lifecycle & HTTP Endpoints (GET /, GET /status)...\n"); |
| 200 | |
| 201 | avx_web_server_config_t cfg; |
| 202 | memset(&cfg, 0, sizeof(cfg)); |
| 203 | cfg.port = 0; /* Auto-assign ephemeral port */ |
| 204 | cfg.stream_name = "Studio-Cam-1"; |
| 205 | cfg.codec = AVX_RTSP_CODEC_H264; |
| 206 | |
| 207 | avx_web_server_t *srv = avx_web_server_create(&cfg); |
| 208 | ASSERT_TRUE(srv != NULL); |
| 209 | |
| 210 | uint16_t port = avx_web_server_get_port(srv); |
| 211 | ASSERT_TRUE(port > 1024); |
| 212 | ASSERT_EQ(avx_web_server_get_client_count(srv), 0); |
| 213 | |
| 214 | /* 1. Test GET / (HTML Player) */ |
| 215 | int fd = socket(AF_INET, SOCK_STREAM, 0); |
| 216 | ASSERT_TRUE(fd >= 0); |
| 217 | |
| 218 | struct sockaddr_in sin; |
| 219 | memset(&sin, 0, sizeof(sin)); |
| 220 | sin.sin_family = AF_INET; |
| 221 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 222 | sin.sin_port = htons(port); |
| 223 | |
| 224 | int cr = connect(fd, (struct sockaddr*)&sin, sizeof(sin)); |
| 225 | ASSERT_EQ(cr, 0); |
| 226 | |
| 227 | const char *get_root = "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"; |
| 228 | send(fd, get_root, strlen(get_root), 0); |
| 229 | |
| 230 | char resp[4096] = {0}; |
| 231 | size_t total = 0; |
| 232 | while (total < sizeof(resp) - 1) { |
| 233 | ssize_t n = recv(fd, resp + total, sizeof(resp) - 1 - total, 0); |
| 234 | if (n <= 0) break; |
| 235 | total += (size_t)n; |
| 236 | if (strstr(resp, "</html>")) break; |
| 237 | } |
| 238 | ASSERT_TRUE(total > 0); |
| 239 | ASSERT_TRUE(strstr(resp, "HTTP/1.1 200 OK") != NULL); |
| 240 | ASSERT_TRUE(strstr(resp, "Content-Type: text/html") != NULL); |
| 241 | ASSERT_TRUE(strstr(resp, "MediaSource") != NULL); |
| 242 | ASSERT_TRUE(strstr(resp, "<video") != NULL); |
| 243 | close(fd); |
| 244 | |
| 245 | /* 2. Test GET /status (JSON Stats) */ |
| 246 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 247 | ASSERT_TRUE(fd >= 0); |
| 248 | cr = connect(fd, (struct sockaddr*)&sin, sizeof(sin)); |
| 249 | ASSERT_EQ(cr, 0); |
| 250 | |
| 251 | const char *get_status = "GET /status HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"; |
| 252 | send(fd, get_status, strlen(get_status), 0); |
| 253 | |
| 254 | memset(resp, 0, sizeof(resp)); |
| 255 | ssize_t n = recv(fd, resp, sizeof(resp) - 1, 0); |
| 256 | ASSERT_TRUE(n > 0); |
| 257 | ASSERT_TRUE(strstr(resp, "HTTP/1.1 200 OK") != NULL); |
| 258 | ASSERT_TRUE(strstr(resp, "Content-Type: application/json") != NULL); |
| 259 | ASSERT_TRUE(strstr(resp, "\"status\":\"online\"") != NULL); |
| 260 | ASSERT_TRUE(strstr(resp, "\"Studio-Cam-1\"") != NULL); |
| 261 | close(fd); |
| 262 | |
| 263 | /* 3. Test OPTIONS /live.mp4 (CORS Pre-Flight) */ |
| 264 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 265 | ASSERT_TRUE(fd >= 0); |
| 266 | cr = connect(fd, (struct sockaddr*)&sin, sizeof(sin)); |
| 267 | ASSERT_EQ(cr, 0); |
| 268 | |
| 269 | const char *opt_req = "OPTIONS /live.mp4 HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"; |
| 270 | send(fd, opt_req, strlen(opt_req), 0); |
| 271 | |
| 272 | memset(resp, 0, sizeof(resp)); |
| 273 | n = recv(fd, resp, sizeof(resp) - 1, 0); |
| 274 | ASSERT_TRUE(n > 0); |
| 275 | ASSERT_TRUE(strstr(resp, "HTTP/1.1 204 No Content") != NULL); |
| 276 | ASSERT_TRUE(strstr(resp, "Access-Control-Allow-Origin: *") != NULL); |
| 277 | close(fd); |
| 278 | |
| 279 | avx_web_server_destroy(srv); |
| 280 | printf(" [+] Web server lifecycle and HTTP responses verified!\n"); |
| 281 | } |
| 282 | |
| 283 | /* ========================================================================= */ |
| 284 | /* TEST 4: LIVE fMP4 STREAMING (GET /live.mp4 + Feed Frame) */ |
| 285 | /* ========================================================================= */ |
| 286 | |
| 287 | static volatile bool g_keyframe_callback_called = false; |
| 288 | static void on_keyframe_req(void *user_ctx) { |
| 289 | (void)user_ctx; |
| 290 | g_keyframe_callback_called = true; |
| 291 | } |
| 292 | |
| 293 | static void test_live_fmp4_streaming(void) { |
| 294 | printf("[*] Testing Live fMP4 Streaming (GET /live.mp4 + Feed Frame)...\n"); |
| 295 | |
| 296 | avx_web_server_config_t cfg; |
| 297 | memset(&cfg, 0, sizeof(cfg)); |
| 298 | cfg.port = 0; |
| 299 | cfg.stream_name = "Cam-Live"; |
| 300 | cfg.codec = AVX_RTSP_CODEC_H264; |
| 301 | cfg.on_keyframe_request = on_keyframe_req; |
| 302 | |
| 303 | g_keyframe_callback_called = false; |
| 304 | avx_web_server_t *srv = avx_web_server_create(&cfg); |
| 305 | ASSERT_TRUE(srv != NULL); |
| 306 | uint16_t port = avx_web_server_get_port(srv); |
| 307 | |
| 308 | /* Connect client socket */ |
| 309 | int fd = socket(AF_INET, SOCK_STREAM, 0); |
| 310 | ASSERT_TRUE(fd >= 0); |
| 311 | |
| 312 | struct sockaddr_in sin; |
| 313 | memset(&sin, 0, sizeof(sin)); |
| 314 | sin.sin_family = AF_INET; |
| 315 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 316 | sin.sin_port = htons(port); |
| 317 | |
| 318 | int cr = connect(fd, (struct sockaddr*)&sin, sizeof(sin)); |
| 319 | ASSERT_EQ(cr, 0); |
| 320 | |
| 321 | /* Issue GET /live.mp4 */ |
| 322 | const char *get_live = "GET /live.mp4 HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"; |
| 323 | send(fd, get_live, strlen(get_live), 0); |
| 324 | |
| 325 | /* Give server worker thread a few ms to process GET request */ |
| 326 | avx_sleep_ms(50); |
| 327 | ASSERT_TRUE(g_keyframe_callback_called); |
| 328 | ASSERT_EQ(avx_web_server_get_client_count(srv), 1); |
| 329 | |
| 330 | /* Feed an H.264 IDR Frame with SPS + PPS + IDR Slice in Annex-B */ |
| 331 | uint8_t h264_keyframe[] = { |
| 332 | /* SPS */ |
| 333 | 0x00, 0x00, 0x00, 0x01, 0x67, 0x64, 0x00, 0x28, 0xAC, 0xD9, 0x40, 0x78, 0x02, 0x27, 0xE5, 0x84, |
| 334 | /* PPS */ |
| 335 | 0x00, 0x00, 0x00, 0x01, 0x68, 0xEB, 0xEF, 0x20, |
| 336 | /* IDR Slice */ |
| 337 | 0x00, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0x00, 0x10, 0xFF, 0xEE, 0xDD |
| 338 | }; |
| 339 | |
| 340 | bool feed_ok = avx_web_server_feed_frame(srv, h264_keyframe, sizeof(h264_keyframe), 1000000000ULL, true); |
| 341 | ASSERT_TRUE(feed_ok); |
| 342 | |
| 343 | /* Read response header from client socket */ |
| 344 | char hdr_buf[1024] = {0}; |
| 345 | size_t hdr_len = 0; |
| 346 | while (hdr_len < sizeof(hdr_buf) - 1) { |
| 347 | char ch; |
| 348 | ssize_t n = recv(fd, &ch, 1, 0); |
| 349 | ASSERT_TRUE(n > 0); |
| 350 | hdr_buf[hdr_len++] = ch; |
| 351 | if (hdr_len >= 4 && memcmp(hdr_buf + hdr_len - 4, "\r\n\r\n", 4) == 0) { |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | ASSERT_TRUE(strstr(hdr_buf, "HTTP/1.1 200 OK") != NULL); |
| 356 | ASSERT_TRUE(strstr(hdr_buf, "Content-Type: video/mp4") != NULL); |
| 357 | |
| 358 | /* Next bytes must be the fMP4 init segment: 'ftyp' */ |
| 359 | uint8_t box_hdr[8]; |
| 360 | ASSERT_TRUE(socket_read_exact(fd, box_hdr, 8, 500)); |
| 361 | uint32_t ftyp_size = (box_hdr[0] << 24) | (box_hdr[1] << 16) | (box_hdr[2] << 8) | box_hdr[3]; |
| 362 | ASSERT_EQ(ftyp_size, 32); |
| 363 | ASSERT_TRUE(memcmp(box_hdr + 4, "ftyp", 4) == 0); |
| 364 | |
| 365 | /* Read remaining ftyp bytes (24 bytes) */ |
| 366 | uint8_t ftyp_rest[24]; |
| 367 | ASSERT_TRUE(socket_read_exact(fd, ftyp_rest, 24, 500)); |
| 368 | |
| 369 | /* Next box must be 'moov' */ |
| 370 | ASSERT_TRUE(socket_read_exact(fd, box_hdr, 8, 500)); |
| 371 | uint32_t moov_size = (box_hdr[0] << 24) | (box_hdr[1] << 16) | (box_hdr[2] << 8) | box_hdr[3]; |
| 372 | ASSERT_TRUE(memcmp(box_hdr + 4, "moov", 4) == 0); |
| 373 | uint8_t *moov_body = (uint8_t*)malloc(moov_size - 8); |
| 374 | ASSERT_TRUE(socket_read_exact(fd, moov_body, moov_size - 8, 500)); |
| 375 | free(moov_body); |
| 376 | |
| 377 | /* Next box must be 'moof' (media fragment) */ |
| 378 | ASSERT_TRUE(socket_read_exact(fd, box_hdr, 8, 500)); |
| 379 | uint32_t moof_size = (box_hdr[0] << 24) | (box_hdr[1] << 16) | (box_hdr[2] << 8) | box_hdr[3]; |
| 380 | ASSERT_TRUE(memcmp(box_hdr + 4, "moof", 4) == 0); |
| 381 | uint8_t *moof_body = (uint8_t*)malloc(moof_size - 8); |
| 382 | ASSERT_TRUE(socket_read_exact(fd, moof_body, moof_size - 8, 500)); |
| 383 | free(moof_body); |
| 384 | |
| 385 | /* Next box must be 'mdat' */ |
| 386 | ASSERT_TRUE(socket_read_exact(fd, box_hdr, 8, 500)); |
| 387 | uint32_t mdat_size = (box_hdr[0] << 24) | (box_hdr[1] << 16) | (box_hdr[2] << 8) | box_hdr[3]; |
| 388 | ASSERT_TRUE(memcmp(box_hdr + 4, "mdat", 4) == 0); |
| 389 | uint8_t *mdat_body = (uint8_t*)malloc(mdat_size - 8); |
| 390 | ASSERT_TRUE(socket_read_exact(fd, mdat_body, mdat_size - 8, 500)); |
| 391 | free(mdat_body); |
| 392 | |
| 393 | /* Feed a subsequent P-Frame */ |
| 394 | uint8_t h264_pframe[] = { |
| 395 | 0x00, 0x00, 0x00, 0x01, 0x41, 0x9A, 0x12, 0x34 |
| 396 | }; |
| 397 | feed_ok = avx_web_server_feed_frame(srv, h264_pframe, sizeof(h264_pframe), 1016666666ULL, false); |
| 398 | ASSERT_TRUE(feed_ok); |
| 399 | |
| 400 | /* Verify next 'moof' box arrives */ |
| 401 | ASSERT_TRUE(socket_read_exact(fd, box_hdr, 8, 500)); |
| 402 | ASSERT_TRUE(memcmp(box_hdr + 4, "moof", 4) == 0); |
| 403 | |
| 404 | close(fd); |
| 405 | avx_sleep_ms(50); |
| 406 | |
| 407 | /* Check that client count updates after socket close */ |
| 408 | avx_web_server_feed_frame(srv, h264_pframe, sizeof(h264_pframe), 1033333333ULL, false); |
| 409 | ASSERT_EQ(avx_web_server_get_client_count(srv), 0); |
| 410 | |
| 411 | avx_web_server_destroy(srv); |
| 412 | printf(" [+] Live fMP4 stream negotiation and fragment delivery verified!\n"); |
| 413 | } |
| 414 | |
| 415 | /* ========================================================================= */ |
| 416 | /* TEST 5: UNIFIED PIPELINE INTEGRATION */ |
| 417 | /* ========================================================================= */ |
| 418 | |
| 419 | static void test_pipeline_web_preview(void) { |
| 420 | printf("[*] Testing Unified Pipeline Integration (oavx_stream_enable_web_preview)...\n"); |
| 421 | |
| 422 | oavx_context_t *ctx = oavx_context_create(); |
| 423 | ASSERT_TRUE(ctx != NULL); |
| 424 | |
| 425 | oavx_stream_config_t config; |
| 426 | memset(&config, 0, sizeof(config)); |
| 427 | config.name = "Web-Pipeline-Test"; |
| 428 | config.direction = OAVX_DIR_TRANSMIT; |
| 429 | config.clock_sync = false; |
| 430 | |
| 431 | oavx_stream_t *stream = oavx_stream_open(ctx, &config); |
| 432 | ASSERT_TRUE(stream != NULL); |
| 433 | |
| 434 | /* Enable Web Preview */ |
| 435 | oavx_status_t status = oavx_stream_enable_web_preview(stream, 0); |
| 436 | ASSERT_EQ(status, OAVX_SUCCESS); |
| 437 | |
| 438 | uint16_t port = oavx_stream_get_web_preview_port(stream); |
| 439 | ASSERT_TRUE(port > 1024); |
| 440 | |
| 441 | char url[128] = {0}; |
| 442 | const char *url_ptr = oavx_stream_get_web_preview_url(stream, url, sizeof(url)); |
| 443 | ASSERT_TRUE(url_ptr != NULL); |
| 444 | ASSERT_TRUE(strstr(url, "http://") != NULL); |
| 445 | printf(" [+] Web preview URL generated: %s\n", url); |
| 446 | |
| 447 | /* Feed an H.264 frame through the unified pipeline */ |
| 448 | uint8_t h264_frame[] = { |
| 449 | 0x00, 0x00, 0x00, 0x01, 0x67, 0x64, 0x00, 0x28, 0xAC, 0xD9, 0x40, 0x78, 0x02, 0x27, 0xE5, 0x84, |
| 450 | 0x00, 0x00, 0x00, 0x01, 0x68, 0xEB, 0xEF, 0x20, |
| 451 | 0x00, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0x00, 0x10, 0xAA, 0xBB, 0xCC |
| 452 | }; |
| 453 | |
| 454 | oavx_video_frame_t vf; |
| 455 | memset(&vf, 0, sizeof(vf)); |
| 456 | vf.fourcc = OAVX_FOURCC_VIDEO_TYPE_H264; |
| 457 | vf.scan_type = OAVX_SCAN_PROGRESSIVE; |
| 458 | vf.width = 1920; |
| 459 | vf.height = 1080; |
| 460 | vf.planes[0] = h264_frame; |
| 461 | vf.strides[0] = sizeof(h264_frame); |
| 462 | vf.framerate.num = 60000; |
| 463 | vf.framerate.den = 1000; |
| 464 | vf.pts_ns = 1000000000ULL; |
| 465 | |
| 466 | oavx_status_t send_st = oavx_stream_send_video(stream, &vf); |
| 467 | ASSERT_EQ(send_st, OAVX_SUCCESS); |
| 468 | |
| 469 | /* Disable Web Preview */ |
| 470 | oavx_stream_disable_web_preview(stream); |
| 471 | ASSERT_EQ(oavx_stream_get_web_preview_port(stream), 0); |
| 472 | |
| 473 | oavx_stream_close(stream); |
| 474 | oavx_context_destroy(ctx); |
| 475 | |
| 476 | printf(" [+] Pipeline web preview lifecycle and frame dispatch verified!\n"); |
| 477 | } |
| 478 | |
| 479 | static void test_web_server_av1_and_hevc_cra_keyframes(void) { |
| 480 | printf("[*] Testing AV1 & HEVC CRA Auto-Keyframe Detection in Web Preview Server...\n"); |
| 481 | |
| 482 | /* 1. HEVC CRA keyframe detection when caller passes is_keyframe=false */ |
| 483 | avx_web_server_config_t hcfg; |
| 484 | memset(&hcfg, 0, sizeof(hcfg)); |
| 485 | hcfg.port = 0; |
| 486 | hcfg.stream_name = "HEVC-CRA-Test"; |
| 487 | hcfg.codec = AVX_RTSP_CODEC_HEVC; |
| 488 | |
| 489 | avx_web_server_t *hsrv = avx_web_server_create(&hcfg); |
| 490 | ASSERT_TRUE(hsrv != NULL); |
| 491 | uint16_t hport = avx_web_server_get_port(hsrv); |
| 492 | |
| 493 | int hfd = socket(AF_INET, SOCK_STREAM, 0); |
| 494 | ASSERT_TRUE(hfd >= 0); |
| 495 | struct sockaddr_in sin; |
| 496 | memset(&sin, 0, sizeof(sin)); |
| 497 | sin.sin_family = AF_INET; |
| 498 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 499 | sin.sin_port = htons(hport); |
| 500 | ASSERT_EQ(connect(hfd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 501 | |
| 502 | const char *req = "GET /live.mp4 HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"; |
| 503 | send(hfd, req, strlen(req), 0); |
| 504 | avx_sleep_ms(50); |
| 505 | |
| 506 | /* HEVC CRA_NUT (type 21 => (21 << 1) = 0x2A, layer/tid byte 0x01) with is_keyframe=false */ |
| 507 | uint8_t hevc_cra[] = { |
| 508 | 0x00, 0x00, 0x00, 0x01, 0x2A, 0x01, 0xDE, 0xAD, 0xBE, 0xEF |
| 509 | }; |
| 510 | ASSERT_TRUE(avx_web_server_feed_frame(hsrv, hevc_cra, sizeof(hevc_cra), 1000000000ULL, false)); |
| 511 | |
| 512 | char hdr_buf[1024] = {0}; |
| 513 | size_t hdr_len = 0; |
| 514 | while (hdr_len < sizeof(hdr_buf) - 1) { |
| 515 | char ch; |
| 516 | ssize_t n = recv(hfd, &ch, 1, 0); |
| 517 | ASSERT_TRUE(n > 0); |
| 518 | hdr_buf[hdr_len++] = ch; |
| 519 | if (hdr_len >= 4 && memcmp(hdr_buf + hdr_len - 4, "\r\n\r\n", 4) == 0) break; |
| 520 | } |
| 521 | ASSERT_TRUE(strstr(hdr_buf, "HTTP/1.1 200 OK") != NULL); |
| 522 | uint8_t box_hdr[8]; |
| 523 | ASSERT_TRUE(socket_read_exact(hfd, box_hdr, 8, 500)); |
| 524 | ASSERT_TRUE(memcmp(box_hdr + 4, "ftyp", 4) == 0); |
| 525 | close(hfd); |
| 526 | avx_web_server_destroy(hsrv); |
| 527 | |
| 528 | /* 2. AV1 OBU_FRAME keyframe detection when caller passes is_keyframe=false */ |
| 529 | avx_web_server_config_t acfg; |
| 530 | memset(&acfg, 0, sizeof(acfg)); |
| 531 | acfg.port = 0; |
| 532 | acfg.stream_name = "AV1-KF-Test"; |
| 533 | acfg.codec = AVX_RTSP_CODEC_AV1; |
| 534 | |
| 535 | avx_web_server_t *asrv = avx_web_server_create(&acfg); |
| 536 | ASSERT_TRUE(asrv != NULL); |
| 537 | uint16_t aport = avx_web_server_get_port(asrv); |
| 538 | |
| 539 | int afd = socket(AF_INET, SOCK_STREAM, 0); |
| 540 | ASSERT_TRUE(afd >= 0); |
| 541 | sin.sin_port = htons(aport); |
| 542 | ASSERT_EQ(connect(afd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 543 | send(afd, req, strlen(req), 0); |
| 544 | avx_sleep_ms(50); |
| 545 | |
| 546 | /* AV1 OBU_FRAME (obu_type=6 => (6<<3)|has_sz = 0x32, len=1, byte0=0x00 => KEY_FRAME) */ |
| 547 | uint8_t av1_kf[] = { 0x32, 0x01, 0x00 }; |
| 548 | ASSERT_TRUE(avx_web_server_feed_frame(asrv, av1_kf, sizeof(av1_kf), 1000000000ULL, false)); |
| 549 | |
| 550 | memset(hdr_buf, 0, sizeof(hdr_buf)); |
| 551 | hdr_len = 0; |
| 552 | while (hdr_len < sizeof(hdr_buf) - 1) { |
| 553 | char ch; |
| 554 | ssize_t n = recv(afd, &ch, 1, 0); |
| 555 | ASSERT_TRUE(n > 0); |
| 556 | hdr_buf[hdr_len++] = ch; |
| 557 | if (hdr_len >= 4 && memcmp(hdr_buf + hdr_len - 4, "\r\n\r\n", 4) == 0) break; |
| 558 | } |
| 559 | ASSERT_TRUE(strstr(hdr_buf, "HTTP/1.1 200 OK") != NULL); |
| 560 | ASSERT_TRUE(socket_read_exact(afd, box_hdr, 8, 500)); |
| 561 | ASSERT_TRUE(memcmp(box_hdr + 4, "ftyp", 4) == 0); |
| 562 | close(afd); |
| 563 | avx_web_server_destroy(asrv); |
| 564 | |
| 565 | printf(" [+] HEVC CRA and AV1 keyframe auto-detection verified!\n"); |
| 566 | } |
| 567 | |
| 568 | /* ========================================================================= */ |
| 569 | /* TEST 7: RFC 3174 SHA-1 & RFC 6455 WEBSOCKET ACCEPT KEY GENERATION */ |
| 570 | /* ========================================================================= */ |
| 571 | |
| 572 | static void test_sha1_and_websocket_accept(void) { |
| 573 | printf("[*] Testing SHA-1 & RFC 6455 WebSocket Handshake Cryptography...\n"); |
| 574 | |
| 575 | /* Vector 1: SHA-1 of "The quick brown fox jumps over the lazy dog" */ |
| 576 | const char *fox_str = "The quick brown fox jumps over the lazy dog"; |
| 577 | uint8_t fox_digest[20]; |
| 578 | avx_sha1_hash((const uint8_t*)fox_str, strlen(fox_str), fox_digest); |
| 579 | uint8_t fox_expected[20] = { |
| 580 | 0x2f, 0xd4, 0xe1, 0xc6, 0x7a, 0x2d, 0x28, 0xfc, 0xed, 0x84, |
| 581 | 0x9e, 0xe1, 0xbb, 0x76, 0xe7, 0x39, 0x1b, 0x93, 0xeb, 0x12 |
| 582 | }; |
| 583 | ASSERT_TRUE(memcmp(fox_digest, fox_expected, 20) == 0); |
| 584 | |
| 585 | /* Vector 2: RFC 6455 Section 1.3 Key Accept test vector */ |
| 586 | /* Key: "dGhlIHNhbXBsZSBub25jZQ==" -> Accept: "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" */ |
| 587 | char accept_out[64] = {0}; |
| 588 | size_t alen = avx_websocket_compute_accept("dGhlIHNhbXBsZSBub25jZQ==", accept_out, sizeof(accept_out)); |
| 589 | ASSERT_EQ(alen, 28); |
| 590 | ASSERT_TRUE(strcmp(accept_out, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=") == 0); |
| 591 | |
| 592 | printf(" [+] SHA-1 NIST vector and RFC 6455 Sec-WebSocket-Accept token validated!\n"); |
| 593 | } |
| 594 | |
| 595 | /* ========================================================================= */ |
| 596 | /* TEST 8: SMARTPHONE WEB CAMERA INGRESS & WEBSOCKET PROTOCOL ENGINE */ |
| 597 | /* ========================================================================= */ |
| 598 | |
| 599 | static volatile int g_ingress_frame_count = 0; |
| 600 | static uint32_t g_ingress_fourcc = 0; |
| 601 | static uint32_t g_ingress_width = 0; |
| 602 | static uint32_t g_ingress_height = 0; |
| 603 | static bool g_ingress_is_keyframe = false; |
| 604 | static uint8_t g_ingress_received_data[256]; |
| 605 | static size_t g_ingress_received_size = 0; |
| 606 | |
| 607 | static void test_camera_ingress_cb( |
| 608 | const uint8_t *frame_data, |
| 609 | size_t frame_size, |
| 610 | uint32_t fourcc, |
| 611 | uint32_t width, |
| 612 | uint32_t height, |
| 613 | bool is_keyframe, |
| 614 | void *user_ctx |
| 615 | ) { |
| 616 | (void)user_ctx; |
| 617 | g_ingress_frame_count++; |
| 618 | g_ingress_fourcc = fourcc; |
| 619 | g_ingress_width = width; |
| 620 | g_ingress_height = height; |
| 621 | g_ingress_is_keyframe = is_keyframe; |
| 622 | if (frame_size <= sizeof(g_ingress_received_data)) { |
| 623 | memcpy(g_ingress_received_data, frame_data, frame_size); |
| 624 | g_ingress_received_size = frame_size; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | static void test_camera_page_and_websocket_ingress(void) { |
| 629 | printf("[*] Testing Smartphone Web Camera Ingress (GET /camera + RFC 6455 WS)...\n"); |
| 630 | |
| 631 | g_ingress_frame_count = 0; |
| 632 | g_ingress_received_size = 0; |
| 633 | |
| 634 | avx_web_server_config_t cfg = { |
| 635 | .port = 0, |
| 636 | .stream_name = "Phone Camera Test", |
| 637 | .codec = AVX_RTSP_CODEC_H264, |
| 638 | .on_ingress_frame = test_camera_ingress_cb, |
| 639 | .user_ctx = NULL |
| 640 | }; |
| 641 | |
| 642 | avx_web_server_t *srv = avx_web_server_create(&cfg); |
| 643 | ASSERT_TRUE(srv != NULL); |
| 644 | uint16_t port = avx_web_server_get_port(srv); |
| 645 | ASSERT_TRUE(port > 0); |
| 646 | |
| 647 | /* 1. Test HTTP GET /camera returns 200 OK and HTML5 camera UI */ |
| 648 | int http_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 649 | ASSERT_TRUE(http_fd >= 0); |
| 650 | struct sockaddr_in sin; |
| 651 | memset(&sin, 0, sizeof(sin)); |
| 652 | sin.sin_family = AF_INET; |
| 653 | sin.sin_port = htons(port); |
| 654 | inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr); |
| 655 | ASSERT_EQ(connect(http_fd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 656 | |
| 657 | const char *get_cam_req = "GET /camera HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n"; |
| 658 | send(http_fd, get_cam_req, strlen(get_cam_req), 0); |
| 659 | |
| 660 | char http_resp[32768] = {0}; |
| 661 | size_t read_bytes = 0; |
| 662 | while (read_bytes < sizeof(http_resp) - 1) { |
| 663 | ssize_t n = recv(http_fd, http_resp + read_bytes, sizeof(http_resp) - 1 - read_bytes, 0); |
| 664 | if (n <= 0) break; |
| 665 | read_bytes += (size_t)n; |
| 666 | } |
| 667 | close(http_fd); |
| 668 | |
| 669 | ASSERT_TRUE(strstr(http_resp, "HTTP/1.1 200 OK") != NULL); |
| 670 | ASSERT_TRUE(strstr(http_resp, "Content-Type: text/html") != NULL); |
| 671 | ASSERT_TRUE(strstr(http_resp, "OpenAVX Mobile Camera") != NULL); |
| 672 | ASSERT_TRUE(strstr(http_resp, "OPENAVX CAM") != NULL); |
| 673 | |
| 674 | /* 2. Test WebSocket Handshake & Frame Ingress on /camera/ws */ |
| 675 | int ws_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 676 | ASSERT_TRUE(ws_fd >= 0); |
| 677 | ASSERT_EQ(connect(ws_fd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 678 | |
| 679 | const char *ws_upgrade_req = |
| 680 | "GET /camera/ws HTTP/1.1\r\n" |
| 681 | "Host: 127.0.0.1\r\n" |
| 682 | "Upgrade: websocket\r\n" |
| 683 | "Connection: Upgrade\r\n" |
| 684 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 685 | "Sec-WebSocket-Version: 13\r\n\r\n"; |
| 686 | send(ws_fd, ws_upgrade_req, strlen(ws_upgrade_req), 0); |
| 687 | |
| 688 | char ws_resp[1024] = {0}; |
| 689 | size_t ws_read = 0; |
| 690 | while (ws_read < sizeof(ws_resp) - 1) { |
| 691 | ssize_t n = recv(ws_fd, ws_resp + ws_read, sizeof(ws_resp) - 1 - ws_read, 0); |
| 692 | if (n <= 0) break; |
| 693 | ws_read += (size_t)n; |
| 694 | if (strstr(ws_resp, "\r\n\r\n")) break; |
| 695 | } |
| 696 | |
| 697 | ASSERT_TRUE(strstr(ws_resp, "HTTP/1.1 101 Switching Protocols") != NULL); |
| 698 | ASSERT_TRUE(strstr(ws_resp, "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=") != NULL); |
| 699 | |
| 700 | /* 3. Send RFC 6455 Masked Binary Frame with OAVX packet header */ |
| 701 | uint8_t payload[16 + 64]; |
| 702 | avx_web_frame_header_t *whdr = (avx_web_frame_header_t*)payload; |
| 703 | whdr->magic = AVX_WEB_FRAME_MAGIC; |
| 704 | whdr->fourcc = OAVX_FOURCC_VIDEO_TYPE_H264; |
| 705 | whdr->width = 1920; |
| 706 | whdr->height = 1080; |
| 707 | whdr->flags = 1; /* Keyframe */ |
| 708 | |
| 709 | /* Fill payload with synthetic video bytes */ |
| 710 | for (size_t i = 0; i < 64; ++i) { |
| 711 | payload[16 + i] = (uint8_t)(0xA0 + i); |
| 712 | } |
| 713 | |
| 714 | /* Build RFC 6455 Frame: FIN=1, Opcode=0x2 (Binary), Masked=1 */ |
| 715 | size_t plen = sizeof(payload); |
| 716 | uint8_t ws_frame[256]; |
| 717 | ws_frame[0] = 0x82; /* 0x80 | 0x02 */ |
| 718 | ws_frame[1] = 0x80 | (uint8_t)plen; /* Masked bit + 80-byte payload */ |
| 719 | |
| 720 | uint8_t mask[4] = { 0x12, 0x34, 0x56, 0x78 }; |
| 721 | memcpy(ws_frame + 2, mask, 4); |
| 722 | |
| 723 | /* Mask payload and copy into ws_frame */ |
| 724 | for (size_t i = 0; i < plen; ++i) { |
| 725 | ws_frame[6 + i] = payload[i] ^ mask[i & 3]; |
| 726 | } |
| 727 | |
| 728 | send(ws_fd, ws_frame, 6 + plen, 0); |
| 729 | |
| 730 | /* Wait for server worker to process packet */ |
| 731 | for (int retry = 0; retry < 50 && g_ingress_frame_count == 0; ++retry) { |
| 732 | avx_sleep_ms(10); |
| 733 | } |
| 734 | |
| 735 | ASSERT_EQ(g_ingress_frame_count, 1); |
| 736 | ASSERT_EQ(g_ingress_fourcc, OAVX_FOURCC_VIDEO_TYPE_H264); |
| 737 | ASSERT_EQ(g_ingress_width, 1920); |
| 738 | ASSERT_EQ(g_ingress_height, 1080); |
| 739 | ASSERT_TRUE(g_ingress_is_keyframe); |
| 740 | ASSERT_EQ(g_ingress_received_size, 64); |
| 741 | ASSERT_TRUE(memcmp(g_ingress_received_data, payload + 16, 64) == 0); |
| 742 | |
| 743 | /* Send WebSocket Close frame (opcode 0x8) */ |
| 744 | uint8_t close_pkt[6] = { 0x88, 0x80, 0x11, 0x22, 0x33, 0x44 }; |
| 745 | send(ws_fd, close_pkt, sizeof(close_pkt), 0); |
| 746 | close(ws_fd); |
| 747 | |
| 748 | avx_web_server_destroy(srv); |
| 749 | printf(" [+] /camera endpoint, RFC 6455 WebSocket handshake, and frame ingress verified!\n"); |
| 750 | } |
| 751 | |
| 752 | /* ========================================================================= */ |
| 753 | /* TEST 9: REMOTE CAMERA TALLY REST API & WEBSOCKET BROADCAST */ |
| 754 | /* ========================================================================= */ |
| 755 | |
| 756 | static void test_camera_tally_broadcast_and_rest_api(void) { |
| 757 | printf("[*] Testing Camera Tally Broadcast & REST API (/api/tally + WS push)...\n"); |
| 758 | |
| 759 | avx_web_server_config_t cfg = { |
| 760 | .port = 0, |
| 761 | .stream_name = "Tally-Test-Cam", |
| 762 | .codec = AVX_RTSP_CODEC_H264 |
| 763 | }; |
| 764 | |
| 765 | avx_web_server_t *srv = avx_web_server_create(&cfg); |
| 766 | ASSERT_TRUE(srv != NULL); |
| 767 | uint16_t port = avx_web_server_get_port(srv); |
| 768 | ASSERT_TRUE(port > 0); |
| 769 | |
| 770 | struct sockaddr_in sin; |
| 771 | memset(&sin, 0, sizeof(sin)); |
| 772 | sin.sin_family = AF_INET; |
| 773 | sin.sin_port = htons(port); |
| 774 | inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr); |
| 775 | |
| 776 | /* 1. GET /api/tally (Initial default: Off-Air / Program=false, Preview=false, Recording=false) */ |
| 777 | int http_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 778 | ASSERT_TRUE(http_fd >= 0); |
| 779 | ASSERT_EQ(connect(http_fd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 780 | |
| 781 | const char *get_tally_req = "GET /api/tally HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n"; |
| 782 | send(http_fd, get_tally_req, strlen(get_tally_req), 0); |
| 783 | |
| 784 | char resp[1024] = {0}; |
| 785 | ssize_t n = recv(http_fd, resp, sizeof(resp) - 1, 0); |
| 786 | ASSERT_TRUE(n > 0); |
| 787 | close(http_fd); |
| 788 | |
| 789 | ASSERT_TRUE(strstr(resp, "HTTP/1.1 200 OK") != NULL); |
| 790 | ASSERT_TRUE(strstr(resp, "\"program\":false") != NULL); |
| 791 | ASSERT_TRUE(strstr(resp, "\"preview\":false") != NULL); |
| 792 | ASSERT_TRUE(strstr(resp, "\"recording\":false") != NULL); |
| 793 | ASSERT_TRUE(strstr(resp, "\"flags\":0") != NULL); |
| 794 | |
| 795 | /* 1b. GET /api/tally/text (Plaintext microcontroller endpoint) */ |
| 796 | http_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 797 | ASSERT_TRUE(http_fd >= 0); |
| 798 | ASSERT_EQ(connect(http_fd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 799 | const char *get_txt_req = "GET /api/tally/text HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n"; |
| 800 | send(http_fd, get_txt_req, strlen(get_txt_req), 0); |
| 801 | memset(resp, 0, sizeof(resp)); |
| 802 | n = recv(http_fd, resp, sizeof(resp) - 1, 0); |
| 803 | ASSERT_TRUE(n > 0); |
| 804 | close(http_fd); |
| 805 | ASSERT_TRUE(strstr(resp, "Content-Type: text/plain") != NULL); |
| 806 | ASSERT_TRUE(strstr(resp, "OFF") != NULL); |
| 807 | |
| 808 | /* 2. Broadcast ON AIR (Program: Red) */ |
| 809 | oavx_tally_t t_prog = { .flags = OAVX_TALLY_PROGRAM }; |
| 810 | avx_web_server_broadcast_tally(srv, &t_prog); |
| 811 | |
| 812 | /* 3. GET /tally to verify updated status */ |
| 813 | http_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 814 | ASSERT_TRUE(http_fd >= 0); |
| 815 | ASSERT_EQ(connect(http_fd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 816 | const char *get_tally2 = "GET /tally HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n"; |
| 817 | send(http_fd, get_tally2, strlen(get_tally2), 0); |
| 818 | memset(resp, 0, sizeof(resp)); |
| 819 | n = recv(http_fd, resp, sizeof(resp) - 1, 0); |
| 820 | ASSERT_TRUE(n > 0); |
| 821 | close(http_fd); |
| 822 | |
| 823 | ASSERT_TRUE(strstr(resp, "\"program\":true") != NULL); |
| 824 | ASSERT_TRUE(strstr(resp, "\"preview\":false") != NULL); |
| 825 | ASSERT_TRUE(strstr(resp, "\"recording\":false") != NULL); |
| 826 | ASSERT_TRUE(strstr(resp, "\"flags\":1") != NULL); |
| 827 | |
| 828 | /* 3b. Verify plaintext shows PROGRAM */ |
| 829 | http_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 830 | ASSERT_TRUE(http_fd >= 0); |
| 831 | ASSERT_EQ(connect(http_fd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 832 | send(http_fd, get_txt_req, strlen(get_txt_req), 0); |
| 833 | memset(resp, 0, sizeof(resp)); |
| 834 | n = recv(http_fd, resp, sizeof(resp) - 1, 0); |
| 835 | ASSERT_TRUE(n > 0); |
| 836 | close(http_fd); |
| 837 | ASSERT_TRUE(strstr(resp, "PROGRAM") != NULL); |
| 838 | |
| 839 | /* 4. Connect WebSocket client and verify initial tally push upon upgrade */ |
| 840 | int ws_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 841 | ASSERT_TRUE(ws_fd >= 0); |
| 842 | ASSERT_EQ(connect(ws_fd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 843 | |
| 844 | const char *ws_upgrade_req = |
| 845 | "GET /camera/ws HTTP/1.1\r\n" |
| 846 | "Host: 127.0.0.1\r\n" |
| 847 | "Upgrade: websocket\r\n" |
| 848 | "Connection: Upgrade\r\n" |
| 849 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 850 | "Sec-WebSocket-Version: 13\r\n\r\n"; |
| 851 | send(ws_fd, ws_upgrade_req, strlen(ws_upgrade_req), 0); |
| 852 | |
| 853 | char ws_hdr[1024] = {0}; |
| 854 | size_t ws_hdr_len = 0; |
| 855 | while (ws_hdr_len < sizeof(ws_hdr) - 1) { |
| 856 | char ch; |
| 857 | ssize_t r = recv(ws_fd, &ch, 1, 0); |
| 858 | ASSERT_TRUE(r > 0); |
| 859 | ws_hdr[ws_hdr_len++] = ch; |
| 860 | if (ws_hdr_len >= 4 && memcmp(ws_hdr + ws_hdr_len - 4, "\r\n\r\n", 4) == 0) break; |
| 861 | } |
| 862 | ASSERT_TRUE(strstr(ws_hdr, "HTTP/1.1 101 Switching Protocols") != NULL); |
| 863 | |
| 864 | /* Read initial WS frame from server */ |
| 865 | uint8_t frame_hdr[2]; |
| 866 | ASSERT_TRUE(socket_read_exact(ws_fd, frame_hdr, 2, 1000)); |
| 867 | ASSERT_EQ(frame_hdr[0], 0x81); /* FIN + Text */ |
| 868 | uint8_t payload_len = frame_hdr[1] & 0x7F; |
| 869 | ASSERT_TRUE(payload_len > 0); |
| 870 | |
| 871 | char ws_payload[256] = {0}; |
| 872 | ASSERT_TRUE(socket_read_exact(ws_fd, (uint8_t*)ws_payload, payload_len, 1000)); |
| 873 | ASSERT_TRUE(strstr(ws_payload, "\"type\":\"tally\"") != NULL); |
| 874 | ASSERT_TRUE(strstr(ws_payload, "\"program\":true") != NULL); |
| 875 | ASSERT_TRUE(strstr(ws_payload, "\"recording\":false") != NULL); |
| 876 | ASSERT_TRUE(strstr(ws_payload, "\"flags\":1") != NULL); |
| 877 | |
| 878 | /* 5. Live broadcast transition to CUED + RECORDING (Preview: Green, Recording: Amber) */ |
| 879 | oavx_tally_t t_prev_rec = { .flags = OAVX_TALLY_PREVIEW | OAVX_TALLY_RECORDING }; |
| 880 | avx_web_server_broadcast_tally(srv, &t_prev_rec); |
| 881 | |
| 882 | /* Read pushed live frame from WebSocket */ |
| 883 | ASSERT_TRUE(socket_read_exact(ws_fd, frame_hdr, 2, 1000)); |
| 884 | ASSERT_EQ(frame_hdr[0], 0x81); |
| 885 | payload_len = frame_hdr[1] & 0x7F; |
| 886 | memset(ws_payload, 0, sizeof(ws_payload)); |
| 887 | ASSERT_TRUE(socket_read_exact(ws_fd, (uint8_t*)ws_payload, payload_len, 1000)); |
| 888 | ASSERT_TRUE(strstr(ws_payload, "\"type\":\"tally\"") != NULL); |
| 889 | ASSERT_TRUE(strstr(ws_payload, "\"program\":false") != NULL); |
| 890 | ASSERT_TRUE(strstr(ws_payload, "\"preview\":true") != NULL); |
| 891 | ASSERT_TRUE(strstr(ws_payload, "\"recording\":true") != NULL); |
| 892 | ASSERT_TRUE(strstr(ws_payload, "\"flags\":6") != NULL); |
| 893 | |
| 894 | /* Close WS */ |
| 895 | uint8_t close_pkt[6] = { 0x88, 0x80, 0x11, 0x22, 0x33, 0x44 }; |
| 896 | send(ws_fd, close_pkt, sizeof(close_pkt), 0); |
| 897 | close(ws_fd); |
| 898 | |
| 899 | /* 6. Test Server-Sent Events (SSE) Stream: GET /api/tally/sse */ |
| 900 | int sse_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 901 | ASSERT_TRUE(sse_fd >= 0); |
| 902 | ASSERT_EQ(connect(sse_fd, (struct sockaddr*)&sin, sizeof(sin)), 0); |
| 903 | |
| 904 | const char *get_sse_req = "GET /api/tally/sse HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"; |
| 905 | send(sse_fd, get_sse_req, strlen(get_sse_req), 0); |
| 906 | |
| 907 | char sse_resp[1024] = {0}; |
| 908 | size_t sse_read = 0; |
| 909 | while (sse_read < sizeof(sse_resp) - 1) { |
| 910 | char ch; |
| 911 | ssize_t r = recv(sse_fd, &ch, 1, 0); |
| 912 | ASSERT_TRUE(r > 0); |
| 913 | sse_resp[sse_read++] = ch; |
| 914 | if (strstr(sse_resp, "\n\n")) break; |
| 915 | } |
| 916 | ASSERT_TRUE(strstr(sse_resp, "HTTP/1.1 200 OK") != NULL); |
| 917 | ASSERT_TRUE(strstr(sse_resp, "Content-Type: text/event-stream") != NULL); |
| 918 | ASSERT_TRUE(strstr(sse_resp, "data: {\"type\":\"tally\"") != NULL); |
| 919 | ASSERT_TRUE(strstr(sse_resp, "\"recording\":true") != NULL); |
| 920 | |
| 921 | /* 7. Broadcast Program + Recording while SSE client is connected */ |
| 922 | oavx_tally_t t_prog_rec = { .flags = OAVX_TALLY_PROGRAM | OAVX_TALLY_RECORDING }; |
| 923 | avx_web_server_broadcast_tally(srv, &t_prog_rec); |
| 924 | |
| 925 | /* Read pushed live SSE event */ |
| 926 | memset(sse_resp, 0, sizeof(sse_resp)); |
| 927 | sse_read = 0; |
| 928 | while (sse_read < sizeof(sse_resp) - 1) { |
| 929 | char ch; |
| 930 | ssize_t r = recv(sse_fd, &ch, 1, 0); |
| 931 | ASSERT_TRUE(r > 0); |
| 932 | sse_resp[sse_read++] = ch; |
| 933 | if (strstr(sse_resp, "\n\n")) break; |
| 934 | } |
| 935 | ASSERT_TRUE(strstr(sse_resp, "data: {\"type\":\"tally\"") != NULL); |
| 936 | ASSERT_TRUE(strstr(sse_resp, "\"program\":true") != NULL); |
| 937 | ASSERT_TRUE(strstr(sse_resp, "\"preview\":false") != NULL); |
| 938 | ASSERT_TRUE(strstr(sse_resp, "\"recording\":true") != NULL); |
| 939 | ASSERT_TRUE(strstr(sse_resp, "\"flags\":5") != NULL); |
| 940 | |
| 941 | close(sse_fd); |
| 942 | |
| 943 | avx_web_server_destroy(srv); |
| 944 | printf(" [+] Camera tally broadcast, initial push, and /api/tally validated!\n"); |
| 945 | } |
| 946 | |
| 947 | /* ========================================================================= */ |
| 948 | /* MAIN RUNNER */ |
| 949 | /* ========================================================================= */ |
| 950 | |
| 951 | int main(void) { |
| 952 | printf("=== OpenAVX Embedded HTTP / fMP4 Web Preview Tests ===\n\n"); |
| 953 | |
| 954 | test_fmp4_init_segment(); |
| 955 | test_fmp4_media_fragment(); |
| 956 | test_web_server_endpoints(); |
| 957 | test_live_fmp4_streaming(); |
| 958 | test_pipeline_web_preview(); |
| 959 | test_web_server_av1_and_hevc_cra_keyframes(); |
| 960 | test_sha1_and_websocket_accept(); |
| 961 | test_camera_page_and_websocket_ingress(); |
| 962 | test_camera_tally_broadcast_and_rest_api(); |
| 963 | |
| 964 | printf("\n[PASS] openavx_web_server_test\n"); |
| 965 | return 0; |
| 966 | } |