+ +
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 |
| 6 | * include/avx_web.h - Embedded HTTP and fragmented MP4 preview server |
| 7 | */ |
| 8 | |
| 9 | #ifndef AVX_WEB_H |
| 10 | #define AVX_WEB_H |
| 11 | |
| 12 | #include "openavx.h" |
| 13 | #include "avx_rtsp.h" |
| 14 | |
| 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
| 19 | /* Maximum concurrent browser preview clients */ |
| 20 | #define AVX_WEB_MAX_CLIENTS 32 |
| 21 | |
| 22 | /* Magic number for binary web frame envelopes ('OAVX' = 0x5856414F) */ |
| 23 | #define AVX_WEB_FRAME_MAGIC 0x5856414FU |
| 24 | |
| 25 | /* Binary frame header transmitted by browser WebSocket clients */ |
| 26 | typedef struct avx_web_frame_header_t { |
| 27 | uint32_t magic; /* AVX_WEB_FRAME_MAGIC */ |
| 28 | uint32_t fourcc; /* OAVX_FOURCC_VIDEO_TYPE_H264 or 'MJPG' (0x47504A4D) */ |
| 29 | uint16_t width; /* Video frame width */ |
| 30 | uint16_t height; /* Video frame height */ |
| 31 | uint32_t flags; /* bit 0: 1 = keyframe / IDR */ |
| 32 | } avx_web_frame_header_t; |
| 33 | |
| 34 | /* Callback invoked when a browser streams a video frame in (phone camera ingress) */ |
| 35 | typedef void (*avx_web_ingress_frame_cb)( |
| 36 | const uint8_t *frame_data, |
| 37 | size_t frame_size, |
| 38 | uint32_t fourcc, |
| 39 | uint32_t width, |
| 40 | uint32_t height, |
| 41 | bool is_keyframe, |
| 42 | void *user_ctx |
| 43 | ); |
| 44 | |
| 45 | /* Opaque server handle */ |
| 46 | typedef struct avx_web_server_t avx_web_server_t; |
| 47 | |
| 48 | /* Server configuration */ |
| 49 | typedef struct avx_web_server_config_t { |
| 50 | uint16_t port; /* HTTP port (e.g. 8080, or 0 for ephemeral/auto-assign) */ |
| 51 | const char *stream_name; /* Stream title displayed on the web monitor HUD */ |
| 52 | avx_rtsp_codec_e codec; /* Video codec: AVX_RTSP_CODEC_H264, HEVC, AV1 */ |
| 53 | void (*on_keyframe_request)(void *user_ctx); /* Called when a browser connects to request instant IDR */ |
| 54 | avx_web_ingress_frame_cb on_ingress_frame; /* Ingress callback for smartphone camera streaming */ |
| 55 | void (*on_tally_change)(void *user_ctx, const oavx_tally_t *tally); /* Ingress callback for REST tally set */ |
| 56 | void *user_ctx; /* User context passed to callbacks */ |
| 57 | } avx_web_server_config_t; |
| 58 | |
| 59 | /* ========================================================================= */ |
| 60 | /* 1. EMBEDDED HTTP / fMP4 SERVER ENGINE */ |
| 61 | /* ========================================================================= */ |
| 62 | |
| 63 | /** |
| 64 | * Creates and starts the embedded HTTP/fMP4 web preview server on a background thread. |
| 65 | * Returns server handle, or NULL on failure. |
| 66 | */ |
| 67 | OPENAVX_API avx_web_server_t* avx_web_server_create(const avx_web_server_config_t *config); |
| 68 | |
| 69 | /** |
| 70 | * Destroys the web server, gracefully closing all active HTTP client connections. |
| 71 | */ |
| 72 | OPENAVX_API void avx_web_server_destroy(avx_web_server_t *server); |
| 73 | |
| 74 | /** |
| 75 | * Returns the active TCP listening port (useful if port 0 was passed to auto-assign). |
| 76 | */ |
| 77 | OPENAVX_API uint16_t avx_web_server_get_port(const avx_web_server_t *server); |
| 78 | |
| 79 | /** |
| 80 | * Returns the number of currently connected browser clients. |
| 81 | */ |
| 82 | OPENAVX_API int avx_web_server_get_client_count(const avx_web_server_t *server); |
| 83 | |
| 84 | /** |
| 85 | * Feeds an encoded video frame (H.264/HEVC/AV1) into the web server. |
| 86 | * The server packages the frame into fMP4 fragments (moof + mdat) and transmits them |
| 87 | * to all connected browser clients. |
| 88 | * Returns true if frame was processed and enqueued to clients. |
| 89 | */ |
| 90 | OPENAVX_API bool avx_web_server_feed_frame( |
| 91 | avx_web_server_t *server, |
| 92 | const uint8_t *frame_data, |
| 93 | size_t frame_size, |
| 94 | uint64_t pts_ns, |
| 95 | bool is_keyframe |
| 96 | ); |
| 97 | |
| 98 | /** |
| 99 | * Registers or updates the camera ingress frame callback at runtime. |
| 100 | */ |
| 101 | OPENAVX_API void avx_web_server_set_ingress_callback( |
| 102 | avx_web_server_t *server, |
| 103 | avx_web_ingress_frame_cb cb, |
| 104 | void *user_ctx |
| 105 | ); |
| 106 | |
| 107 | /** |
| 108 | * Broadcasts updated tally status (Program / Preview) to all active WebSocket clients. |
| 109 | */ |
| 110 | OPENAVX_API void avx_web_server_broadcast_tally(avx_web_server_t *server, const oavx_tally_t *tally); |
| 111 | |
| 112 | /** |
| 113 | * Computes standard FIPS 180-1 SHA-1 hash (20 bytes output). |
| 114 | */ |
| 115 | OPENAVX_API void avx_sha1_hash(const uint8_t *data, size_t len, uint8_t digest[20]); |
| 116 | |
| 117 | /** |
| 118 | * Computes RFC 6455 Sec-WebSocket-Accept token from a client's Sec-WebSocket-Key. |
| 119 | * out_accept must be at least 32 bytes. Returns length of token. |
| 120 | */ |
| 121 | OPENAVX_API size_t avx_websocket_compute_accept(const char *client_key, char *out_accept, size_t out_max); |
| 122 | |
| 123 | /* ========================================================================= */ |
| 124 | /* 2. PURE C99 ISOBMFF / fMP4 PACKAGING ENGINE */ |
| 125 | /* ========================================================================= */ |
| 126 | |
| 127 | /** |
| 128 | * Pure C99 fMP4 Initialization Segment Generator (ftyp + moov). |
| 129 | * Produces the ISO BMFF init segment for MSE SourceBuffer initialization. |
| 130 | * Returns bytes written, or 0 on error/insufficient buffer. |
| 131 | */ |
| 132 | OPENAVX_API size_t avx_fmp4_generate_init_segment( |
| 133 | avx_rtsp_codec_e codec, |
| 134 | uint32_t width, |
| 135 | uint32_t height, |
| 136 | const uint8_t *sps, |
| 137 | size_t sps_len, |
| 138 | const uint8_t *pps, |
| 139 | size_t pps_len, |
| 140 | const uint8_t *vps, |
| 141 | size_t vps_len, |
| 142 | uint8_t *out_buf, |
| 143 | size_t out_max |
| 144 | ); |
| 145 | |
| 146 | /** |
| 147 | * Pure C99 fMP4 Media Fragment Generator (moof + mdat). |
| 148 | * Produces the movie fragment for a single video sample. |
| 149 | * Returns bytes written, or 0 on error/insufficient buffer. |
| 150 | */ |
| 151 | OPENAVX_API size_t avx_fmp4_generate_media_fragment( |
| 152 | uint32_t sequence_number, |
| 153 | uint64_t base_decode_time_90khz, |
| 154 | uint32_t sample_duration_90khz, |
| 155 | bool is_keyframe, |
| 156 | const uint8_t *sample_avcc_data, |
| 157 | size_t sample_avcc_len, |
| 158 | uint8_t *out_buf, |
| 159 | size_t out_max |
| 160 | ); |
| 161 | |
| 162 | #ifdef __cplusplus |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | #endif /* AVX_WEB_H */ |