|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
đŚ Network compression utilities for ascii-chat More...
Files | |
| file | compression.c |
| đď¸ Fast zstd compression/decompression utilities for network payload optimization | |
| file | compression.h |
| đŚ Network Packet Compression Utilities | |
Functions | |
| asciichat_error_t | compress_data (const void *input, size_t input_size, void **output, size_t *output_size, int compression_level) |
| Compress data using zstd with configurable compression level. | |
| asciichat_error_t | decompress_data (const void *input, size_t input_size, void *output, size_t output_size) |
| Decompress data using zstd. | |
| bool | should_compress (size_t original_size, size_t compressed_size) |
| Determine if compression should be used for given data sizes. | |
Compression Settings | |
| #define | COMPRESSION_RATIO_THRESHOLD 0.8f |
| Compression ratio threshold - only use if <80% original size. | |
| #define | COMPRESSION_MIN_SIZE 1024 |
| Minimum packet size to attempt compression (1KB) | |
đŚ Network compression utilities for ascii-chat
This header provides compression and decompression utilities for network packets in ascii-chat. The system uses zstd compression to reduce bandwidth usage for large packets like video frames.
Compression is only applied when:
This prevents:
Welcome to the Compression module! This is where we shrink your data down to save bandwidth. Compression is all about balanceâwe want to save bandwidth without wasting CPU, so we compress intelligently only when it actually helps.
The compression module provides fast zstd compression/decompression utilities for optimizing network payload size in ascii-chat. Compression reduces bandwidth usage for large packets like video frames and audio batches, which improves performance over low-bandwidth connections.
Here's what it gives you:
Implementation: lib/compression.c, lib/compression.h
Compression isn't always worth itâsometimes the compressed data is actually larger than the original, or the CPU cost isn't worth the bandwidth savings. We've designed the compression system to be smart about when to compress.
Compression is automatically applied when:
This strategy prevents several common problems:
We use zstd (Zstandard) compression, which is a modern compression algorithm developed by Facebook/Meta. Here's why we chose it:
zstd level 1 is perfect for our use caseâit's fast enough that it doesn't slow things down, but still gives us significant bandwidth savings.
Compression is automatically integrated into the packet protocol:
Compression performance is all about balancing speed and compression ratio. We've tuned the system for real-time video streaming, so speed is critical, but we still want good compression. Let's look at what you can expect:
Compression Speed:
The speed varies depending on your CPU, but even on a modest CPU, you can compress at 200 MB/s, which is more than enough for real-time video.
Compression Ratio:
ASCII frames compress really well because they're textâall those repeated spaces and characters compress down nicely. You can often get 70-90% compression, which means a 6 MB frame becomes 600 KB-1.8 MB. That's a huge bandwidth savings!
Memory Usage:
Memory usage is pretty reasonableâwe allocate the compressed output buffer (which is usually smaller than the input), but there's no persistent state, so memory usage is minimal.
Compression settings control when and how we compress. We've tuned these values for real-time video streaming, but let's understand what they mean:
Compression Thresholds:
COMPRESSION_MIN_SIZE** (1024 bytes): Minimum packet size to attempt compressionâwe don't compress packets smaller than 1KB because the overhead isn't worth itCOMPRESSION_RATIO_THRESHOLD** (0.8): Maximum acceptable compression ratio (80% of original)âif compression doesn't reduce size by at least 20%, we skip itThese thresholds ensure we only compress when it's actually beneficial. Small packets and packets that don't compress well are skipped, saving CPU without wasting bandwidth.
Compression Level:
We could use higher compression levels (which would save more bandwidth), but the CPU cost would be too high for real-time streaming. Level 1 is the perfect balance.
Compression Errors:
Decompression Errors:
| #define COMPRESSION_MIN_SIZE 1024 |
#include <compression.h>
Minimum packet size to attempt compression (1KB)
Definition at line 61 of file compression.h.
| #define COMPRESSION_RATIO_THRESHOLD 0.8f |
#include <compression.h>
Compression ratio threshold - only use if <80% original size.
Definition at line 58 of file compression.h.
| asciichat_error_t compress_data | ( | const void * | input, |
| size_t | input_size, | ||
| void ** | output, | ||
| size_t * | output_size, | ||
| int | compression_level | ||
| ) |
#include <compression.h>
Compress data using zstd with configurable compression level.
| input | Input data to compress (must not be NULL) |
| input_size | Size of input data in bytes |
| output | Output buffer pointer (pointer-to-pointer; function allocates and stores address here) |
| output_size | Size of compressed data in bytes (output parameter, must not be NULL) |
| compression_level | zstd compression level (1-9)
|
Compresses input data using zstd's compression algorithm with the specified compression level. The output buffer is automatically allocated by the function and must be freed by the caller using SAFE_FREE() or the appropriate memory management function.
Definition at line 14 of file compression.c.
References ASCIICHAT_OK, ERROR_GENERAL, ERROR_INVALID_PARAM, ERROR_MEMORY, SAFE_FREE, SAFE_MALLOC, and SET_ERRNO.
Referenced by send_packet_secure().
| asciichat_error_t decompress_data | ( | const void * | input, |
| size_t | input_size, | ||
| void * | output, | ||
| size_t | output_size | ||
| ) |
#include <compression.h>
Decompress data using zstd.
| input | Compressed input data (must not be NULL) |
| input_size | Size of compressed data in bytes |
| output | Pre-allocated output buffer (must not be NULL) |
| output_size | Size of output buffer in bytes (must be >= decompressed size) |
Decompresses zstd-compressed data into a pre-allocated output buffer. The output buffer must be large enough to hold the decompressed data.
Definition at line 58 of file compression.c.
References ASCIICHAT_OK, ERROR_GENERAL, ERROR_INVALID_PARAM, and SET_ERRNO.
Referenced by handle_image_frame_packet(), packet_decode_frame_data_buffer(), and packet_decode_frame_data_malloc().
| bool should_compress | ( | size_t | original_size, |
| size_t | compressed_size | ||
| ) |
#include <compression.h>
Determine if compression should be used for given data sizes.
| original_size | Original (uncompressed) data size in bytes |
| compressed_size | Compressed data size in bytes |
Determines whether compression is beneficial based on size thresholds and compression ratio. Returns true if:
This prevents:
Definition at line 74 of file compression.c.
References COMPRESSION_RATIO_THRESHOLD.
Referenced by send_packet_secure().