+ +
36b1ca8 test(failover): fix race between stall detection and bak_tx connection on windows
saifmukhtar · 2026-11-25
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 |
| 6 | * test/openavx_failover_test.c - Automatic Failover Source Switching Verification Suite |
| 7 | * |
| 8 | * Verifies: |
| 9 | * 1. Instant Socket Disconnect Failover (0 ms trigger on remote RST/FIN). |
| 10 | * 2. Stream Stall / Frame Timeout Failover (hang/deadlock detection). |
| 11 | * 3. Dynamic Sender-Advised Failover (metadata pushed to downstream receivers). |
| 12 | * 4. Latching Non-Reverting Mode vs. Debounced Auto-Revert (Flap Prevention). |
| 13 | */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <stdbool.h> |
| 19 | #include <assert.h> |
| 20 | #include <unistd.h> |
| 21 | #include "openavx.h" |
| 22 | #include "avx_platform.h" |
| 23 | |
| 24 | static void fill_test_video(oavx_video_frame_t* vframe, uint8_t fill_val) { |
| 25 | memset(vframe, 0, sizeof(*vframe)); |
| 26 | vframe->width = 1920; |
| 27 | vframe->height = 1080; |
| 28 | vframe->fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY; |
| 29 | vframe->framerate.num = 60000; |
| 30 | vframe->framerate.den = 1000; |
| 31 | vframe->aspect_ratio.num = 16; |
| 32 | vframe->aspect_ratio.den = 9; |
| 33 | vframe->scan_type = OAVX_SCAN_PROGRESSIVE; |
| 34 | vframe->pts_ns = avx_time_ns(); |
| 35 | vframe->strides[0] = 1920 * 2; |
| 36 | |
| 37 | size_t sz = 1920 * 1080 * 2; |
| 38 | vframe->planes[0] = (uint8_t*)malloc(sz); |
| 39 | memset(vframe->planes[0], fill_val, sz); |
| 40 | } |
| 41 | |
| 42 | int main(void) { |
| 43 | printf("=== OpenAVX Automatic Failover & Source Switching Tests ===\n\n"); |
| 44 | |
| 45 | oavx_context_t *ctx = oavx_context_create(); |
| 46 | if (!ctx) { |
| 47 | fprintf(stderr, "[-] Failed to initialize openavx context\n"); |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | /* ------------------------------------------------------------------ */ |
| 52 | /* 1. Instant Socket Disconnect Failover */ |
| 53 | /* ------------------------------------------------------------------ */ |
| 54 | printf("[*] 1. Testing Instant Socket Disconnect Failover (0 ms reaction)...\n"); |
| 55 | { |
| 56 | oavx_stream_config_t pri_tx_cfg = { |
| 57 | .direction = OAVX_DIR_TRANSMIT, |
| 58 | .name = "CAM_PRIMARY_1", |
| 59 | .bind_ip = "127.0.0.1" |
| 60 | }; |
| 61 | oavx_stream_t *pri_tx = oavx_stream_open(ctx, &pri_tx_cfg); |
| 62 | |
| 63 | oavx_stream_config_t bak_tx_cfg = { |
| 64 | .direction = OAVX_DIR_TRANSMIT, |
| 65 | .name = "CAM_BACKUP_1", |
| 66 | .bind_ip = "127.0.0.1" |
| 67 | }; |
| 68 | oavx_stream_t *bak_tx = oavx_stream_open(ctx, &bak_tx_cfg); |
| 69 | |
| 70 | if (!pri_tx || !bak_tx) { |
| 71 | fprintf(stderr, "[-] Failed to create primary or backup transmitters\n"); |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | const oavx_endpoint_t *pri_ep = oavx_stream_get_endpoint(pri_tx); |
| 76 | const oavx_endpoint_t *bak_ep = oavx_stream_get_endpoint(bak_tx); |
| 77 | |
| 78 | /* Receiver connecting to primary, with backup failover endpoint specified */ |
| 79 | oavx_stream_config_t rx_cfg = { |
| 80 | .direction = OAVX_DIR_RECEIVE, |
| 81 | .name = "STUDIO_VIEWER_1", |
| 82 | .endpoint = pri_ep->address, |
| 83 | .color_format = OAVX_COLOR_FORMAT_FASTEST |
| 84 | }; |
| 85 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 86 | if (!rx) { |
| 87 | fprintf(stderr, "[-] Failed to create receiver\n"); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | oavx_stream_set_failover(rx, bak_ep); |
| 92 | |
| 93 | /* Wait for primary connection */ |
| 94 | int conns = 0; |
| 95 | for (int i = 0; i < 20; ++i) { |
| 96 | conns = oavx_stream_get_connection_count(pri_tx, 50); |
| 97 | if (conns > 0) break; |
| 98 | avx_sleep_ms(20); |
| 99 | } |
| 100 | if (conns == 0) { |
| 101 | fprintf(stderr, "[-] Receiver failed to connect to primary\n"); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | /* Transmit video frame from primary */ |
| 106 | oavx_video_frame_t v_pri; |
| 107 | fill_test_video(&v_pri, 0x11); |
| 108 | oavx_stream_send_video(pri_tx, &v_pri); |
| 109 | |
| 110 | oavx_video_frame_t v_recv; |
| 111 | oavx_audio_frame_t a_recv; |
| 112 | oavx_frame_type_t ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 500); |
| 113 | if (ft != OAVX_FRAME_TYPE_VIDEO || v_recv.planes[0][0] != 0x11) { |
| 114 | fprintf(stderr, "[-] Failed to receive primary video frame\n"); |
| 115 | return 1; |
| 116 | } |
| 117 | oavx_stream_release_video(rx, &v_recv); |
| 118 | free(v_pri.planes[0]); |
| 119 | |
| 120 | if (oavx_stream_is_on_failover(rx)) { |
| 121 | fprintf(stderr, "[-] Receiver erroneously reported on failover before primary drop\n"); |
| 122 | return 1; |
| 123 | } |
| 124 | printf(" [+] Captured primary frame (fill=0x11), is_on_failover=false\n"); |
| 125 | |
| 126 | /* Abruptly kill primary transmitter (simulating crash/unplug) */ |
| 127 | printf(" [~] Killing primary transmitter to simulate sudden process crash...\n"); |
| 128 | oavx_stream_close(pri_tx); |
| 129 | |
| 130 | /* Transmit backup video frame */ |
| 131 | oavx_video_frame_t v_bak; |
| 132 | fill_test_video(&v_bak, 0x22); |
| 133 | |
| 134 | /* Receiver should detect disconnect, execute failover, and connect to backup */ |
| 135 | bool received_bak = false; |
| 136 | for (int retry = 0; retry < 60; ++retry) { |
| 137 | oavx_stream_send_video(bak_tx, &v_bak); |
| 138 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 50); |
| 139 | if (ft == OAVX_FRAME_TYPE_VIDEO && v_recv.planes[0][0] == 0x22) { |
| 140 | received_bak = true; |
| 141 | oavx_stream_release_video(rx, &v_recv); |
| 142 | break; |
| 143 | } |
| 144 | avx_sleep_ms(20); |
| 145 | } |
| 146 | |
| 147 | if (!received_bak) { |
| 148 | fprintf(stderr, "[-] Receiver failed to receive backup frame after primary disconnect\n"); |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | if (!oavx_stream_is_on_failover(rx)) { |
| 153 | fprintf(stderr, "[-] Receiver did not mark is_on_failover=true after cutover\n"); |
| 154 | return 1; |
| 155 | } |
| 156 | printf(" [+] Socket disconnect failover SUCCESS: Receiver cut to backup (fill=0x22, is_on_failover=true)!\n"); |
| 157 | |
| 158 | free(v_bak.planes[0]); |
| 159 | oavx_stream_close(rx); |
| 160 | oavx_stream_close(bak_tx); |
| 161 | } |
| 162 | |
| 163 | /* ------------------------------------------------------------------ */ |
| 164 | /* 2. Stream Stall Failover (Frame Timeout Threshold) */ |
| 165 | /* ------------------------------------------------------------------ */ |
| 166 | printf("\n[*] 2. Testing Stream Stall Failover (Encoder Hang with Active Socket)...\n"); |
| 167 | { |
| 168 | oavx_stream_config_t pri_tx_cfg = { |
| 169 | .direction = OAVX_DIR_TRANSMIT, |
| 170 | .name = "CAM_PRIMARY_2", |
| 171 | .bind_ip = "127.0.0.1" |
| 172 | }; |
| 173 | oavx_stream_t *pri_tx = oavx_stream_open(ctx, &pri_tx_cfg); |
| 174 | |
| 175 | oavx_stream_config_t bak_tx_cfg = { |
| 176 | .direction = OAVX_DIR_TRANSMIT, |
| 177 | .name = "CAM_BACKUP_2", |
| 178 | .bind_ip = "127.0.0.1" |
| 179 | }; |
| 180 | oavx_stream_t *bak_tx = oavx_stream_open(ctx, &bak_tx_cfg); |
| 181 | |
| 182 | const oavx_endpoint_t *pri_ep = oavx_stream_get_endpoint(pri_tx); |
| 183 | const oavx_endpoint_t *bak_ep = oavx_stream_get_endpoint(bak_tx); |
| 184 | |
| 185 | oavx_stream_config_t rx_cfg = { |
| 186 | .direction = OAVX_DIR_RECEIVE, |
| 187 | .name = "STUDIO_VIEWER_2", |
| 188 | .endpoint = pri_ep->address, |
| 189 | .color_format = OAVX_COLOR_FORMAT_FASTEST |
| 190 | }; |
| 191 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 192 | |
| 193 | oavx_stream_set_failover(rx, bak_ep); |
| 194 | /* Set sensitive 200 ms stall threshold for fast testing */ |
| 195 | oavx_stream_set_failover_timeout(rx, 200); |
| 196 | |
| 197 | /* Wait for primary connection */ |
| 198 | for (int i = 0; i < 20; ++i) { |
| 199 | if (oavx_stream_get_connection_count(pri_tx, 50) > 0) break; |
| 200 | avx_sleep_ms(20); |
| 201 | } |
| 202 | |
| 203 | /* Prime stream with initial frame */ |
| 204 | oavx_video_frame_t v_pri; |
| 205 | fill_test_video(&v_pri, 0x33); |
| 206 | oavx_video_frame_t v_recv; |
| 207 | oavx_audio_frame_t a_recv; |
| 208 | oavx_frame_type_t ft = OAVX_FRAME_TYPE_NONE; |
| 209 | for (int i = 0; i < 20; ++i) { |
| 210 | oavx_stream_send_video(pri_tx, &v_pri); |
| 211 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 100); |
| 212 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 213 | oavx_stream_release_video(rx, &v_recv); |
| 214 | break; |
| 215 | } |
| 216 | avx_sleep_ms(20); |
| 217 | } |
| 218 | assert(ft == OAVX_FRAME_TYPE_VIDEO); |
| 219 | free(v_pri.planes[0]); |
| 220 | |
| 221 | /* Now: Keep primary TCP socket open, but stop sending frames (stream stall) */ |
| 222 | printf(" [~] Freezing primary encoder (socket remains connected, no frames sent)...\n"); |
| 223 | |
| 224 | /* |
| 225 | * Phase 1: Pump oavx_stream_receive so the stall detection logic runs |
| 226 | * and the receiver triggers failover (is_on_failover becomes true). |
| 227 | * The 200 ms timeout is measured against the last-received frame PTS, |
| 228 | * so we need to call receive() periodically for the stall clock to tick. |
| 229 | * Allow up to ~3 s on slow Windows CI runners (60 x 50 ms poll timeout). |
| 230 | */ |
| 231 | for (int i = 0; i < 60; ++i) { |
| 232 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 50); |
| 233 | if (ft == OAVX_FRAME_TYPE_VIDEO) |
| 234 | oavx_stream_release_video(rx, &v_recv); |
| 235 | if (oavx_stream_is_on_failover(rx)) break; |
| 236 | avx_sleep_ms(20); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Phase 2: Wait for the receiver to establish a TCP connection to |
| 241 | * bak_tx. After failover fires, the receiver performs a non-blocking |
| 242 | * connect + XML handshake that can take up to ~2 s on Windows CI. |
| 243 | * Frames sent to bak_tx while active_clients == 0 are silently |
| 244 | * dropped, so we MUST confirm the connection is live before sending. |
| 245 | * Poll bak_tx every ~50 ms for up to ~4 s (80 iterations). |
| 246 | * Continue pumping the receiver so its network state machine advances. |
| 247 | */ |
| 248 | for (int i = 0; i < 80; ++i) { |
| 249 | if (oavx_stream_get_connection_count(bak_tx, 50) > 0) break; |
| 250 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 20); |
| 251 | if (ft == OAVX_FRAME_TYPE_VIDEO) |
| 252 | oavx_stream_release_video(rx, &v_recv); |
| 253 | avx_sleep_ms(30); |
| 254 | } |
| 255 | |
| 256 | /* Transmit frame on backup stream */ |
| 257 | oavx_video_frame_t v_bak; |
| 258 | fill_test_video(&v_bak, 0x44); |
| 259 | |
| 260 | bool received_bak = false; |
| 261 | for (int retry = 0; retry < 60; ++retry) { |
| 262 | oavx_stream_send_video(bak_tx, &v_bak); |
| 263 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 50); |
| 264 | if (ft == OAVX_FRAME_TYPE_VIDEO && v_recv.planes[0][0] == 0x44) { |
| 265 | received_bak = true; |
| 266 | oavx_stream_release_video(rx, &v_recv); |
| 267 | break; |
| 268 | } |
| 269 | avx_sleep_ms(20); |
| 270 | } |
| 271 | |
| 272 | if (!received_bak) { |
| 273 | fprintf(stderr, "[-] Receiver failed to trigger failover on stream stall " |
| 274 | "(is_on_failover=%d, ft=%d, bak_clients=%d)\n", |
| 275 | oavx_stream_is_on_failover(rx), (int)ft, |
| 276 | oavx_stream_get_connection_count(bak_tx, 0)); |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | if (!oavx_stream_is_on_failover(rx)) { |
| 281 | fprintf(stderr, "[-] Receiver did not transition to is_on_failover=true after stall\n"); |
| 282 | return 1; |
| 283 | } |
| 284 | printf(" [+] Stream stall failover SUCCESS: Receiver triggered failover after 200 ms timeout!\n"); |
| 285 | |
| 286 | free(v_bak.planes[0]); |
| 287 | oavx_stream_close(rx); |
| 288 | oavx_stream_close(pri_tx); |
| 289 | oavx_stream_close(bak_tx); |
| 290 | } |
| 291 | |
| 292 | /* ------------------------------------------------------------------ */ |
| 293 | /* 3. Sender-Advised Dynamic Failover (Connection Metadata XML) */ |
| 294 | /* ------------------------------------------------------------------ */ |
| 295 | printf("\n[*] 3. Testing Dynamic Sender-Advised Failover via Connection Metadata...\n"); |
| 296 | { |
| 297 | oavx_stream_config_t pri_tx_cfg = { |
| 298 | .direction = OAVX_DIR_TRANSMIT, |
| 299 | .name = "CAM_PRIMARY_3", |
| 300 | .bind_ip = "127.0.0.1" |
| 301 | }; |
| 302 | oavx_stream_t *pri_tx = oavx_stream_open(ctx, &pri_tx_cfg); |
| 303 | |
| 304 | oavx_stream_config_t bak_tx_cfg = { |
| 305 | .direction = OAVX_DIR_TRANSMIT, |
| 306 | .name = "CAM_BACKUP_3", |
| 307 | .bind_ip = "127.0.0.1" |
| 308 | }; |
| 309 | oavx_stream_t *bak_tx = oavx_stream_open(ctx, &bak_tx_cfg); |
| 310 | |
| 311 | const oavx_endpoint_t *pri_ep = oavx_stream_get_endpoint(pri_tx); |
| 312 | const oavx_endpoint_t *bak_ep = oavx_stream_get_endpoint(bak_tx); |
| 313 | |
| 314 | /* Transmitter advertises backup endpoint downstream to receivers */ |
| 315 | oavx_stream_set_failover(pri_tx, bak_ep); |
| 316 | |
| 317 | /* Receiver creates connection WITHOUT explicitly knowing backup URL */ |
| 318 | oavx_stream_config_t rx_cfg = { |
| 319 | .direction = OAVX_DIR_RECEIVE, |
| 320 | .name = "STUDIO_VIEWER_3", |
| 321 | .endpoint = pri_ep->address, |
| 322 | .color_format = OAVX_COLOR_FORMAT_FASTEST |
| 323 | }; |
| 324 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 325 | |
| 326 | /* Wait for connection and metadata propagation */ |
| 327 | for (int i = 0; i < 20; ++i) { |
| 328 | if (oavx_stream_get_connection_count(pri_tx, 50) > 0) break; |
| 329 | avx_sleep_ms(20); |
| 330 | } |
| 331 | |
| 332 | /* Prime stream: frame from primary */ |
| 333 | oavx_video_frame_t v_pri; |
| 334 | fill_test_video(&v_pri, 0x55); |
| 335 | oavx_video_frame_t v_recv; |
| 336 | oavx_audio_frame_t a_recv; |
| 337 | oavx_frame_type_t ft = OAVX_FRAME_TYPE_NONE; |
| 338 | for (int i = 0; i < 20; ++i) { |
| 339 | oavx_stream_send_video(pri_tx, &v_pri); |
| 340 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 100); |
| 341 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 342 | oavx_stream_release_video(rx, &v_recv); |
| 343 | break; |
| 344 | } |
| 345 | avx_sleep_ms(20); |
| 346 | } |
| 347 | assert(ft == OAVX_FRAME_TYPE_VIDEO); |
| 348 | free(v_pri.planes[0]); |
| 349 | |
| 350 | /* Terminate primary stream */ |
| 351 | printf(" [~] Killing sender with advised failover...\n"); |
| 352 | oavx_stream_close(pri_tx); |
| 353 | |
| 354 | /* Stream on backup */ |
| 355 | oavx_video_frame_t v_bak; |
| 356 | fill_test_video(&v_bak, 0x66); |
| 357 | |
| 358 | bool received_bak = false; |
| 359 | for (int retry = 0; retry < 60; ++retry) { |
| 360 | oavx_stream_send_video(bak_tx, &v_bak); |
| 361 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 50); |
| 362 | if (ft == OAVX_FRAME_TYPE_VIDEO && v_recv.planes[0][0] == 0x66) { |
| 363 | received_bak = true; |
| 364 | oavx_stream_release_video(rx, &v_recv); |
| 365 | break; |
| 366 | } |
| 367 | avx_sleep_ms(20); |
| 368 | } |
| 369 | |
| 370 | if (!received_bak) { |
| 371 | fprintf(stderr, "[-] Receiver failed to failover using sender-advised metadata\n"); |
| 372 | return 1; |
| 373 | } |
| 374 | printf(" [+] Sender-advised failover SUCCESS: Receiver auto-learned failover target via XML!\n"); |
| 375 | |
| 376 | free(v_bak.planes[0]); |
| 377 | oavx_stream_close(rx); |
| 378 | oavx_stream_close(bak_tx); |
| 379 | } |
| 380 | |
| 381 | /* ------------------------------------------------------------------ */ |
| 382 | /* 4. Latching Policy vs. Debounced Auto-Revert */ |
| 383 | /* ------------------------------------------------------------------ */ |
| 384 | printf("\n[*] 4. Testing Latching Policy & Debounced Auto-Revert (Flap Prevention)...\n"); |
| 385 | { |
| 386 | oavx_stream_config_t pri_tx_cfg = { |
| 387 | .direction = OAVX_DIR_TRANSMIT, |
| 388 | .name = "CAM_PRIMARY_4", |
| 389 | .bind_ip = "127.0.0.1" |
| 390 | }; |
| 391 | oavx_stream_t *pri_tx = oavx_stream_open(ctx, &pri_tx_cfg); |
| 392 | |
| 393 | oavx_stream_config_t bak_tx_cfg = { |
| 394 | .direction = OAVX_DIR_TRANSMIT, |
| 395 | .name = "CAM_BACKUP_4", |
| 396 | .bind_ip = "127.0.0.1" |
| 397 | }; |
| 398 | oavx_stream_t *bak_tx = oavx_stream_open(ctx, &bak_tx_cfg); |
| 399 | |
| 400 | const oavx_endpoint_t *pri_ep = oavx_stream_get_endpoint(pri_tx); |
| 401 | const oavx_endpoint_t *bak_ep = oavx_stream_get_endpoint(bak_tx); |
| 402 | |
| 403 | char saved_pri_url[64]; |
| 404 | strncpy(saved_pri_url, pri_ep->address, sizeof(saved_pri_url) - 1); |
| 405 | saved_pri_url[sizeof(saved_pri_url) - 1] = '\0'; |
| 406 | |
| 407 | oavx_stream_config_t rx_cfg = { |
| 408 | .direction = OAVX_DIR_RECEIVE, |
| 409 | .name = "STUDIO_VIEWER_4", |
| 410 | .endpoint = pri_ep->address, |
| 411 | .color_format = OAVX_COLOR_FORMAT_FASTEST |
| 412 | }; |
| 413 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 414 | |
| 415 | oavx_stream_set_failover(rx, bak_ep); |
| 416 | |
| 417 | /* Wait for initial primary connection */ |
| 418 | for (int i = 0; i < 20; ++i) { |
| 419 | if (oavx_stream_get_connection_count(pri_tx, 50) > 0) break; |
| 420 | avx_sleep_ms(20); |
| 421 | } |
| 422 | |
| 423 | /* Initial frame from primary */ |
| 424 | oavx_video_frame_t v_pri; |
| 425 | fill_test_video(&v_pri, 0x77); |
| 426 | oavx_stream_send_video(pri_tx, &v_pri); |
| 427 | |
| 428 | oavx_video_frame_t v_recv; |
| 429 | oavx_audio_frame_t a_recv; |
| 430 | oavx_frame_type_t ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 500); |
| 431 | if (ft == OAVX_FRAME_TYPE_VIDEO) oavx_stream_release_video(rx, &v_recv); |
| 432 | free(v_pri.planes[0]); |
| 433 | |
| 434 | /* Trigger failover by closing primary */ |
| 435 | oavx_stream_close(pri_tx); |
| 436 | |
| 437 | oavx_video_frame_t v_bak; |
| 438 | fill_test_video(&v_bak, 0x88); |
| 439 | |
| 440 | for (int retry = 0; retry < 60; ++retry) { |
| 441 | oavx_stream_send_video(bak_tx, &v_bak); |
| 442 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 50); |
| 443 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 444 | oavx_stream_release_video(rx, &v_recv); |
| 445 | break; |
| 446 | } |
| 447 | avx_sleep_ms(20); |
| 448 | } |
| 449 | |
| 450 | if (!oavx_stream_is_on_failover(rx)) { |
| 451 | fprintf(stderr, "[-] Expected receiver to be on failover\n"); |
| 452 | return 1; |
| 453 | } |
| 454 | |
| 455 | /* 4A. Verify LATCHING: Re-open Primary transmitter on the same port/endpoint, verify receiver stays on backup */ |
| 456 | pri_tx = oavx_stream_open(ctx, &pri_tx_cfg); |
| 457 | fill_test_video(&v_pri, 0x99); |
| 458 | oavx_stream_send_video(pri_tx, &v_pri); |
| 459 | oavx_stream_send_video(bak_tx, &v_bak); |
| 460 | |
| 461 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 100); |
| 462 | if (ft == OAVX_FRAME_TYPE_VIDEO) oavx_stream_release_video(rx, &v_recv); |
| 463 | |
| 464 | if (!oavx_stream_is_on_failover(rx)) { |
| 465 | fprintf(stderr, "[-] Latching mode failed: receiver switched prematurely without auto_revert enabled\n"); |
| 466 | return 1; |
| 467 | } |
| 468 | printf(" [+] Latching Policy verified: Receiver remains locked on backup despite primary recovery.\n"); |
| 469 | |
| 470 | /* 4B. Enable Auto-Revert with short 200 ms debounce window */ |
| 471 | printf(" [~] Enabling auto-revert (200 ms hysteresis hold window)...\n"); |
| 472 | oavx_stream_set_auto_revert(rx, true, 200); |
| 473 | |
| 474 | /* Wait for debounce hold window to elapse with stable primary */ |
| 475 | bool reverted = false; |
| 476 | for (int i = 0; i < 40; ++i) { |
| 477 | oavx_stream_send_video(pri_tx, &v_pri); |
| 478 | oavx_stream_send_video(bak_tx, &v_bak); |
| 479 | ft = oavx_stream_receive(rx, &v_recv, &a_recv, NULL, 50); |
| 480 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 481 | if (!oavx_stream_is_on_failover(rx)) { |
| 482 | reverted = true; |
| 483 | oavx_stream_release_video(rx, &v_recv); |
| 484 | break; |
| 485 | } |
| 486 | oavx_stream_release_video(rx, &v_recv); |
| 487 | } |
| 488 | avx_sleep_ms(50); |
| 489 | } |
| 490 | |
| 491 | if (!reverted) { |
| 492 | fprintf(stderr, "[-] Auto-revert failed to cut back to primary after debounce window\n"); |
| 493 | return 1; |
| 494 | } |
| 495 | printf(" [+] Debounced Auto-Revert SUCCESS: Receiver returned cleanly to primary after stability hold!\n"); |
| 496 | |
| 497 | free(v_pri.planes[0]); |
| 498 | free(v_bak.planes[0]); |
| 499 | oavx_stream_close(rx); |
| 500 | oavx_stream_close(pri_tx); |
| 501 | oavx_stream_close(bak_tx); |
| 502 | } |
| 503 | |
| 504 | oavx_context_destroy(ctx); |
| 505 | printf("\n[PASS] openavx_failover_test\n"); |
| 506 | return 0; |
| 507 | } |