1 #ifndef HAVE_A12_INT
2 #define HAVE_A12_INT
3 
4 #include "blake3.h"
5 #include "pack.h"
6 
7 #if defined(WANT_H264_DEC) || defined(WANT_H264_ENC)
8 #include <libavcodec/avcodec.h>
9 #include <libavcodec/version.h>
10 #include <libavutil/opt.h>
11 #include <libavutil/imgutils.h>
12 #include <libavformat/avformat.h>
13 #include <libswscale/swscale.h>
14 #include <libswresample/swresample.h>
15 #endif
16 
17 #ifndef VIDEO_FRAME_DRIFT_WINDOW
18 #define VIDEO_FRAME_DRIFT_WINDOW 8
19 #endif
20 
21 #define MAC_BLOCK_SZ 16
22 #define NONCE_SIZE 8
23 #define CONTROL_PACKET_SIZE 128
24 #define CIPHER_ROUNDS 8
25 
26 #ifndef DYNAMIC_FREE
27 #define DYNAMIC_FREE free
28 #endif
29 
30 #ifndef DYNAMIC_MALLOC
31 #define DYNAMIC_MALLOC malloc
32 #endif
33 
34 #ifndef DYNAMIC_REALLOC
35 #define DYNAMIC_REALLOC realloc
36 #endif
37 
38 #ifndef COUNT_OF
39 #define COUNT_OF(x) \
40 	((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
41 #endif
42 
43 enum {
44 	STATE_NOPACKET       = 0, /* nopacket -> control                         */
45 	STATE_CONTROL_PACKET = 1, /* control  -> a, v, e, blob, broken, nopacket */
46 	STATE_EVENT_PACKET   = 2, /* event    -> nopacket, broken                */
47 	STATE_AUDIO_PACKET   = 3, /* audio    -> nopacket, broken                */
48 	STATE_VIDEO_PACKET   = 4, /* video    -> nopacket, broken                */
49 	STATE_BLOB_PACKET    = 5, /* blob     -> nopacket, broken                */
50 	STATE_1STSRV_PACKET  = 6, /* mac+nonce -> control                        */
51 	STATE_BROKEN
52 };
53 
54 enum control_commands {
55 	COMMAND_HELLO        = 0, /* initial keynegotiation          */
56 	COMMAND_SHUTDOWN     = 1, /* graceful exit                   */
57 	COMMAND_NEWCH        = 2, /* channel = triple (A|V|meta)     */
58 	COMMAND_CANCELSTREAM = 3, /* abort ongoing video transfer    */
59 	COMMAND_VIDEOFRAME   = 4, /* next packets will be video data */
60 	COMMAND_AUDIOFRAME   = 5, /* next packets will be audio data */
61 	COMMAND_BINARYSTREAM = 6, /* cut and paste, state, font, ... */
62 	COMMAND_PING         = 7, /* keepalive, latency masurement   */
63 	COMMAND_REKEY        = 8  /* new x25519 change               */
64 };
65 
66 enum hello_mode {
67 	HELLO_MODE_NOASYM  = 0,
68 	HELLO_MODE_REALPK  = 1,
69 	HELLO_MODE_EPHEMPK = 2
70 };
71 
72 enum channel_cfg {
73 	CHANNEL_INACTIVE = 0, /* nothing mapped in the channel          */
74 	CHANNEL_SHMIF    = 1, /* shmif context set                      */
75 	CHANNEL_RAW      = 2  /* raw user-callback provided destination */
76 };
77 
78 #define SEQUENCE_NUMBER_SIZE 8
79 
80 #ifdef _DEBUG
81 #define DEBUG 1
82 #else
83 #define DEBUG 0
84 #endif
85 
86 enum {
87 	POSTPROCESS_VIDEO_RGBA   = 0,
88 	POSTPROCESS_VIDEO_RGB    = 1,
89 	POSTPROCESS_VIDEO_RGB565 = 2,
90 	POSTPROCESS_VIDEO_H264   = 5, /* ffmpeg or native decompressor        */
91 	POSTPROCESS_VIDEO_TZSTD  = 7, /* ZSTD+tpack                           */
92 	POSTPROCESS_VIDEO_DZSTD  = 8, /* ZSTD - P frame                       */
93 	POSTPROCESS_VIDEO_ZSTD   = 9  /* ZSTD - I frame                       */
94 };
95 
96 size_t a12int_header_size(int type);
97 
98 struct ZSTD_CCtx_s;
99 struct ZSTD_DCtx_s;
100 
101 struct audio_frame {
102 	uint32_t id;
103 
104 	uint32_t rate;
105 	uint8_t encoding;
106 	uint8_t channels;
107 	uint8_t format;
108 	uint16_t nsamples;
109 	uint8_t commit;
110 
111 /* only used for some postprocessing mode (i.e. decompression) */
112 	uint8_t* inbuf;
113 	size_t inbuf_pos;
114 	size_t inbuf_sz;
115 	size_t expanded_sz;
116 };
117 
118 struct binary_frame {
119 	int tmp_fd;
120 	int type;
121 	bool active;
122 	uint64_t size;
123 	uint8_t checksum[16];
124 	int64_t streamid; /* actual type is uint32 but -1 for cancel */
125 };
126 
127 struct video_frame {
128 	uint32_t id;
129 	uint16_t sw, sh;
130 	uint16_t w, h;
131 	uint16_t x, y;
132 	uint32_t flags;
133 	uint8_t postprocess;
134 	uint8_t commit; /* finish after this transfer? */
135 
136 	uint8_t* inbuf; /* decode buffer, not used for all modes */
137 	uint32_t inbuf_pos;
138 	uint32_t inbuf_sz; /* bytes-total counter */
139  /* separation between input-frame buffer and
140 	* decompression postprocessing, avoid 'zip-bombing' */
141 	uint32_t expanded_sz;
142 	size_t row_left;
143 	size_t out_pos;
144 
145 	uint8_t pxbuf[4];
146 	uint8_t carry;
147 
148 /* these allocations are not kept in the frame struct, but
149  * rather contained in the ffmpeg structure for that channel */
150 #ifdef WANT_H264_DEC
151 	struct {
152 		AVCodecParserContext* parser;
153 		AVCodecContext* context;
154 		AVPacket* packet;
155 		AVFrame* frame;
156 		struct SwsContext* scaler;
157 	} ffmpeg;
158 #endif
159 
160 	struct ZSTD_DCtx_s* zstd;
161 
162 	/* bytes left on current row for raw-dec */
163 };
164 
165 struct blob_out;
166 struct blob_out {
167 	uint8_t checksum[16];
168 	int fd;
169 	uint8_t chid;
170 	int type;
171 	size_t left;
172 	bool streaming;
173 	bool active;
174 	uint64_t streamid;
175 	struct blob_out* next;
176 };
177 
178 struct a12_channel {
179 	int active;
180 	struct arcan_shmif_cont* cont;
181 	struct a12_unpack_cfg raw;
182 
183 /* can have one of each stream- type being prepared for unpack at the same time */
184 	struct {
185 		struct video_frame vframe;
186 		struct audio_frame aframe;
187 		struct binary_frame bframe;
188 	} unpack_state;
189 
190 /* used for both encoding and decoding, state is aliased into unpack_state */
191 	struct shmifsrv_vbuffer acc;
192 	struct {
193 		uint8_t* compression;
194 		struct ZSTD_CCtx_s* zstd;
195 #if defined(WANT_H264_ENC) || defined(WANT_H264_DEC)
196 		struct {
197 			AVCodecParserContext* parser;
198 			AVCodecContext* encdec;
199 			AVCodec* codec;
200 			AVFrame* frame;
201 			AVPacket* packet;
202 			struct SwsContext* scaler;
203 			size_t w, h;
204 			bool failed;
205 		} videnc;
206 #endif
207 	};
208 };
209 
210 struct a12_state;
211 struct a12_state {
212 	struct a12_context_options* opts;
213 	uint8_t last_mac_in[MAC_BLOCK_SZ];
214 
215 /* data needed to synthesize the next package */
216 	uint64_t current_seqnr;
217 	uint64_t last_seen_seqnr;
218 	uint64_t out_stream;
219 	bool advenc_broken;
220 
221 /* The biggest concern of congestion is video frames as that tends to be most
222  * primary data. The decision to act upon this is still up to the tool feeding
223  * the state machine, there might be other priorities and factors to weigh in
224  * on. While we indirectly do measure latency and so on, the better congestion
225  * control channel for that is left up to the carrier. */
226 	struct {
227 		uint32_t frame_window[VIDEO_FRAME_DRIFT_WINDOW]; /* seqnrs tied to vframes */
228 		size_t pending; /* updated whenever we send something out */
229 	} congestion_stats;
230 	struct a12_iostat stats;
231 
232 /* populate and forwarded output buffer */
233 	size_t buf_sz[2];
234 	uint8_t* bufs[2];
235 	uint8_t buf_ind;
236 	size_t buf_ofs;
237 
238 /* linked list of pending binary transfers, can be re-ordered and affect
239  * blocking / transfer state of events on the other side */
240 	struct blob_out* pending;
241 	size_t active_blobs;
242 
243 /* current event handler for binary transfer cache oracle */
244 	struct a12_bhandler_res
245 		(*binary_handler)(struct a12_state*, struct a12_bhandler_meta, void*);
246 	void* binary_handler_tag;
247 
248 /* multiple- channels over the same state tracker for subsegment handling */
249 	struct a12_channel channels[256];
250 
251 /* current decoding state, tracked / used by the process_* functions */
252 	int in_channel;
253 	uint32_t in_stream;
254 
255 /* current encoding state, manipulate with set_channel */
256 	int out_channel;
257 
258 	void (*on_auth)(struct a12_state*, void*);
259 	void* auth_tag;
260 
261 /*
262  * Incoming buffer, size of the buffer == size of the type - when there
263  * is nothing left in the current frame, forward / dispatch to the correct
264  * decode vframe/aframe/bframe routine.
265  */
266 	uint8_t decode[65536];
267 	uint16_t decode_pos;
268 	uint16_t left;
269 	uint8_t state;
270 
271 /* overflow state tracking cookie */
272 	volatile uint32_t cookie;
273 
274 /* curve25519 keys (client), and rekeying sequence number (both) */
275 	struct {
276 		uint8_t ephem_priv[32];
277 		uint8_t real_priv[32];
278 		uint64_t rekey_pos;
279 	} keys;
280 
281 /* client side needs to send the first packet with MAC+nonce, server side
282  * needs to interpret first packet with MAC+nonce */
283 	bool server;
284 	bool cl_firstout;
285 	int authentic;
286 
287 /* saved between calls to unpack, see end of a12_unpack for explanation */
288 	bool auth_latched;
289 	size_t prepend_unpack_sz;
290 	uint8_t* prepend_unpack;
291 
292 	blake3_hasher out_mac, in_mac;
293 
294 	struct chacha_ctx* enc_state;
295 	struct chacha_ctx* dec_state;
296 };
297 
298 enum {
299 	STREAM_FAIL_OUTDATED = 0,
300 	STREAM_FAIL_UNKNOWN = 1,
301 	STREAM_FAIL_ALREADY_KNOWN = 2
302 };
303 void a12int_stream_fail(struct a12_state* S, uint8_t ch, uint32_t id, int fail);
304 void a12int_stream_ack(struct a12_state* S, uint8_t ch, uint32_t id);
305 
306 void a12int_append_out(
307 	struct a12_state* S, uint8_t type, uint8_t* out, size_t out_sz,
308 	uint8_t* prepend, size_t prepend_sz);
309 
310 void a12int_step_vstream(struct a12_state* S, uint32_t id);
311 
312 #endif
313