Overview
The Audio/Video Networking module provides the network protocol implementation for audio, video, and ASCII frame packets in ascii-chat. The system handles packet serialization, compression, encryption integration, and message formatting for real-time media streaming over TCP.
Implementation: lib/network/av.h
Key Features:
- ASCII frame packet transmission (text-based video frames)
- Image frame packet transmission (raw pixel data)
- Audio packet transmission (single and batched)
- Protocol message handling (size, audio, text)
- Compression integration for large packets
- Cryptographic encryption support
- Error handling and validation
Packet Types
The system handles multiple packet types:
Frame Packets:
- ASCII frames: Text-based video frames for terminal display
- Image frames: Raw pixel data (RGB, RGBA, BGR, etc.)
Audio Packets:
- Single audio packets: Legacy single-packet format (PACKET_TYPE_AUDIO)
- Audio batch packets: Batched audio samples (PACKET_TYPE_AUDIO_BATCH) - efficient
Message Packets:
- Size messages: Terminal dimension notifications (PACKET_TYPE_SIZE_MESSAGE)
- Audio messages: Audio metadata announcements (PACKET_TYPE_AUDIO_MESSAGE)
- Text messages: Text chat messages (PACKET_TYPE_TEXT_MESSAGE)
Operations
Frame Operations
ASCII Frame Transmission:
int result = av_send_ascii_frame(sockfd, frame_data, frame_size);
if (result < 0) {
}
#define log_error(...)
Log an ERROR message.
Image Frame Transmission:
int result = av_send_image_frame(sockfd, image_data, width, height, format);
if (result < 0) {
}
Audio Operations
Single Audio Packet (Legacy):
int result = av_send_audio(sockfd, samples, num_samples);
if (result < 0) {
}
Audio Batch Packet (Efficient):
batch_count, crypto_ctx);
if (result < 0) {
}
result = av_send_audio_batch(sockfd, samples, total_samples, sample_rate);
asciichat_error_t send_audio_batch_packet(socket_t sockfd, const float *samples, int num_samples, int batch_count, crypto_context_t *crypto_ctx)
Send a batched audio packet with encryption support.
Benefits of Batching:
- Reduced packet overhead (75% fewer packets)
- Better bandwidth efficiency (80% reduction in overhead)
- At 44.1 kHz: 47 packets/second (was 188)
Message Operations
Terminal Size Message:
int result = av_send_size_message(sockfd, width, height);
if (result < 0) {
}
Audio Message:
int result = av_send_audio_message(sockfd, num_samples);
if (result < 0) {
}
Text Message:
int result = av_send_text_message(sockfd, "Hello, world!");
if (result < 0) {
}
Message Parsing:
unsigned short width, height;
int result = av_parse_size_message("SIZE:160,45\n", &width, &height);
if (result < 0) {
}
Protocol Integration
The Audio/Video Networking module integrates with:
Packet System:
- network/packet.h: Packet header system for framing
- network/packet_types.h: Packet type definitions
Compression:
- compression.h: Automatic compression for large packets
- Compression threshold-based decision making
- Decompression on receive path
Cryptography:
- crypto/crypto.h: Encryption/decryption support
- Automatic encryption when crypto context provided
- Encryption policy enforcement
Network Layer:
Message Formats
Protocol messages use text-based formats for compatibility:
Size Message:
- Format:
"SIZE:width,height\n"
- Example:
"SIZE:160,45\n"
- Prefix:
SIZE_MESSAGE_PREFIX ("SIZE:")
- Format string:
SIZE_MESSAGE_FORMAT ("SIZE:%u,%u\n")
Audio Message:
- Format:
"AUDIO:num_samples\n"
- Example:
"AUDIO:1024\n"
- Prefix:
AUDIO_MESSAGE_PREFIX ("AUDIO:")
- Format string:
AUDIO_MESSAGE_FORMAT ("AUDIO:%u\n")
Text Message:
- Format: Plain text with packet headers
- No special formatting required
Compression
Large packets automatically support compression:
- ASCII frames: Compressed if size exceeds threshold
- Image frames: Compressed for large images
- Audio batches: Compressed for large audio data
Compression Decision:
- Size threshold-based (e.g., 100KB)
- Compression ratio check (only if >80% size reduction)
- Automatic compression/decompression
Encryption
All packet functions handle encryption when crypto context is provided:
Automatic Encryption:
batch_count, crypto_ctx);
Encryption Policy:
- Handshake packets: Always unencrypted (plaintext)
- Session packets: Encrypted when crypto context provided
- Encryption enforcement: Optional or required based on policy
Error Handling
All functions return error codes:
Error Reporting:
if (av_send_ascii_frame(sockfd, frame_data, frame_size) < 0) {
}
const char * network_error_string()
Get human-readable error string for network errors.
Performance
Batched Audio Packets:
- 75% reduction in packet count (4 chunks per packet)
- 80% reduction in packet overhead
- At 44.1 kHz: 47 packets/second (was 188)
- Packet overhead: 846 bytes/sec (was 3.4 KB/sec)
Compression Benefits:
- Reduces bandwidth for large frames
- Automatic compression threshold-based decision
- Only compresses if beneficial (>80% size reduction)
Message Format:
- Text-based for compatibility
- Simple parsing (no complex binary formats)
- Maximum message length: 32 bytes (SIZE_MESSAGE_MAX_LEN, AUDIO_MESSAGE_MAX_LEN)
Best Practices
- Use batched audio for efficiency:
av_send_audio_batch(sockfd, samples, total_samples, sample_rate);
- Handle compression automatically:
av_send_ascii_frame(sockfd, frame_data, frame_size);
- Use encryption when available:
batch_count, crypto_ctx);
- Validate message formats:
if (av_parse_size_message(message, &width, &height) < 0) {
}
- Handle errors gracefully:
if (av_send_ascii_frame(sockfd, frame_data, frame_size) < 0) {
handle_network_error();
}
Packet Details
ASCII Frame Packet (PACKET_TYPE_ASCII_FRAME):
- Contains ASCII frame data with metadata
- Metadata: width, height, compression status, checksum
- Payload: ASCII frame string (may be compressed)
Image Frame Packet (PACKET_TYPE_IMAGE_FRAME):
- Contains raw pixel data with metadata
- Metadata: dimensions, format, compression status, checksum, timestamp
- Payload: Pixel data (RGB, RGBA, BGR, etc.) - may be compressed
Audio Batch Packet (PACKET_TYPE_AUDIO_BATCH):
- Contains multiple audio chunks aggregated
- Metadata: batch count, total samples, sample rate, channels
- Payload: Float samples array (interleaved if stereo)
Message Packets:
- Size message: Terminal dimension notification
- Audio message: Audio metadata announcement
- Text message: Plain text chat message
- See also
- network/av.h
-
network/packet.h
-
network/packet_types.h