improve readability of new.c

This commit is contained in:
Alexander Jacobsen 2026-07-17 22:25:19 +02:00
parent 92500e1432
commit 5b8ce3781c
2 changed files with 240 additions and 126 deletions

18
Makefile Normal file
View file

@ -0,0 +1,18 @@
CC ?= cc
CFLAGS ?= -std=c11 -O2 -pthread \
-Wall -Wextra -Wpedantic -Wshadow -Wformat=2 \
-Wcast-align -Wstrict-prototypes -Wmissing-prototypes \
-Wpointer-arith -Wwrite-strings
LDFLAGS ?= -pthread
TARGET := pixelflut-render
SRC := new.c
$(TARGET): $(SRC)
$(CC) $(CFLAGS) -o $@ $(SRC) $(LDFLAGS)
clean:
rm -f $(TARGET)
.PHONY: clean

320
new.c
View file

@ -1,143 +1,239 @@
#include <netinet/in.h> /*
#include <net/if.h> * Pixelflut UDP renderer.
*
* Reads raw RGB24 frames that main.py continuously writes to INPUT_FILE
* (via mmap) and floods them at a Pixelflut server using the binary UDP
* protocol. Pixels are picked in a random order so the picture "dissolves"
* onto the wall instead of being drawn scanline by scanline.
*
* Binary Pixelflut packet layout (little-endian on the wire):
*
* +------+---------+ pixelflut_packet
* | cmd | subcmd | 2-byte header
* +------+---------+---------+----+----+----+ pixelflut_pixel[0]
* | x (u16) | y (u16) | r | g | b | 7 bytes
* +----------------+---------+----+----+----+
* | ... | PIXELS_PER_PACKET pixels
* +----------------------------------------+
*/
#define _GNU_SOURCE /* sendmmsg(), struct mmsghdr, SO_BINDTODEVICE */
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdint.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#define PIXELS 120 #include <fcntl.h>
#define MSGSIZE (2 + 7 * PIXELS) #include <sys/mman.h>
#define WIDTH (640) #include <sys/stat.h>
#define HEIGHT (360)
#define OFFSET_X 1200 #include <pthread.h>
#define OFFSET_Y 40
//#define DISPLAY_HOST "100.65.0.2" /* Portable host -> little-endian 16-bit conversion (matches the wire format). */
#if defined(__linux__)
# include <endian.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
# include <sys/endian.h>
#elif defined(__APPLE__)
# include <libkern/OSByteOrder.h>
# define htole16(v) OSSwapHostToLittleInt16(v)
#endif
/* ---- Frame geometry (must match the producer, see main.py). ------------- */
#define FRAME_WIDTH 640
#define FRAME_HEIGHT 360
#define FRAME_CHANNELS 3 /* RGB, one byte per channel */
#define FRAME_PIXELS (FRAME_WIDTH * FRAME_HEIGHT)
#define FRAME_BYTES (FRAME_PIXELS * FRAME_CHANNELS)
/* Top-left corner where the frame is drawn on the shared wall. */
#define CANVAS_OFFSET_X 1200
#define CANVAS_OFFSET_Y 40
/* ---- Pixelflut binary protocol. ---------------------------------------- */
#define PIXELFLUT_CMD_SET 0x00 /* "set pixels" command ... */
#define PIXELFLUT_SUBCMD_RGB 0x01 /* ... with 3-byte RGB colours */
#define PIXELS_PER_PACKET 120 /* pixels batched into one UDP datagram */
/* Number of datagrams needed to cover a whole frame once. */
#define PACKET_COUNT (FRAME_PIXELS / PIXELS_PER_PACKET)
/* ---- Target / input. --------------------------------------------------- */
/* #define DISPLAY_HOST "100.65.0.2" */
#define DISPLAY_HOST "fcda:f100::d" #define DISPLAY_HOST "fcda:f100::d"
#define DISPLAY_PORT 5005 #define DISPLAY_PORT 5005
#define SIZE WIDTH*HEIGHT*3
#define INPUT_FILE "/tmp/video" #define INPUT_FILE "/tmp/video"
#define RANDOMNESS_LEN (WIDTH*HEIGHT)
#include <pthread.h>
struct mmsghdr msg[RANDOMNESS_LEN/PIXELS];
struct iovec iovec[RANDOMNESS_LEN/PIXELS];
uint8_t buf[RANDOMNESS_LEN/PIXELS][MSGSIZE];
uint64_t randomness[RANDOMNESS_LEN];
int randomness_o =0;
static unsigned long x=123456789, y=362436069, z=521288629;
unsigned long xorshf96(void) { //period 2^96-1 /* A single pixel as it appears on the wire. */
unsigned long t; #pragma pack(push, 1)
x ^= x << 16; struct pixelflut_pixel {
x ^= x >> 5; uint16_t x; /* little-endian */
x ^= x << 1; uint16_t y; /* little-endian */
uint8_t r;
uint8_t g;
uint8_t b;
};
t = x; /* A full datagram: header followed by a batch of pixels. */
x = y; struct pixelflut_packet {
y = z; uint8_t cmd;
z = t ^ x ^ y; uint8_t subcmd;
struct pixelflut_pixel pixels[PIXELS_PER_PACKET];
};
#pragma pack(pop)
return z; _Static_assert(sizeof(struct pixelflut_pixel) == 7,
} "pixel must be exactly 7 bytes on the wire");
void shuffle(uint64_t *array, size_t n) _Static_assert(FRAME_PIXELS % PIXELS_PER_PACKET == 0,
"frame pixels must divide evenly into packets");
/* Pre-built datagrams and the scatter/gather metadata sendmmsg() needs. */
static struct pixelflut_packet packets[PACKET_COUNT];
static struct mmsghdr messages[PACKET_COUNT];
static struct iovec iovecs[PACKET_COUNT];
/* xorshf96 PRNG state (Marsaglia), period 2^96-1. */
static unsigned long rng_x = 123456789;
static unsigned long rng_y = 362436069;
static unsigned long rng_z = 521288629;
static unsigned long xorshf96(void)
{ {
if (n > 1) rng_x ^= rng_x << 16;
rng_x ^= rng_x >> 5;
rng_x ^= rng_x << 1;
unsigned long t = rng_x;
rng_x = rng_y;
rng_y = rng_z;
rng_z = t ^ rng_x ^ rng_y;
return rng_z;
}
/* Continuously blast the pre-built packets at the display. */
static void *send_thread(void *arg)
{ {
size_t i; const char *ifname = arg;
for (i = 0; i < n - 1; i++)
{ int fd = socket(AF_INET6, SOCK_DGRAM, 0);
size_t j = i + rand() / (RAND_MAX / (n - i) + 1); if (fd == -1) {
unsigned long t = array[j]; perror("socket");
array[j] = array[i];
array[i] = t;
}
}
}
void *send_thread(void * ifname){
int sockfd;
struct sockaddr_in6 addr;
sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("socket()");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#ifdef SO_BINDTODEVICE
/* Best effort: pin traffic to the given interface (needs privileges). */
if (ifname != NULL && ifname[0] != '\0') {
struct ifreq ifr; struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr)); memset(&ifr, 0, sizeof ifr);
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name),(char *) ifname); snprintf(ifr.ifr_name, sizeof ifr.ifr_name, "%s", ifname);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr,
sizeof ifr) == -1) {
perror("setsockopt(SO_BINDTODEVICE)");
}
}
#else
(void)ifname;
#endif
struct sockaddr_in6 addr;
memset(&addr, 0, sizeof addr);
addr.sin6_family = AF_INET6; addr.sin6_family = AF_INET6;
addr.sin6_port = htons(DISPLAY_PORT); addr.sin6_port = htons(DISPLAY_PORT);
inet_pton(AF_INET6, DISPLAY_HOST, &addr.sin6_addr); if (inet_pton(AF_INET6, DISPLAY_HOST, &addr.sin6_addr) != 1) {
fprintf(stderr, "invalid display host: %s\n", DISPLAY_HOST);
if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
perror("connect()");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int from = 0;
sleep(1); if (connect(fd, (struct sockaddr *)&addr, sizeof addr) == -1) {
while(1){ perror("connect");
int retval = sendmmsg(sockfd, &msg[from], (RANDOMNESS_LEN/PIXELS)-from, 0); exit(EXIT_FAILURE);
if (retval == -1) {
perror("sendmmsg()");
} }
from += retval;
if(from <= (RANDOMNESS_LEN/PIXELS)){ sleep(1); /* give the producer time to fill the first frame */
from = 0;
int next = 0;
for (;;) {
int sent = sendmmsg(fd, &messages[next], PACKET_COUNT - next, 0);
if (sent == -1) {
perror("sendmmsg");
continue;
} }
next += sent; /* resume after a partial send, wrap at the end */
if (next >= PACKET_COUNT)
next = 0;
} }
return NULL; /* unreachable */
} }
int main(int argc, char *argv[]) {
printf(argv[1]); int main(int argc, char *argv[])
x=rand(); {
y=rand(); if (argc != 2) {
z=rand(); fprintf(stderr, "usage: %s <interface>\n", argv[0]);
for(int i = 0; i < RANDOMNESS_LEN; i++) { return EXIT_FAILURE;
randomness[i] = i;
} }
shuffle(randomness,RANDOMNESS_LEN);
for(int i = 0; i < RANDOMNESS_LEN/PIXELS; i++){ rng_x = (unsigned long)rand();
iovec[i].iov_base = buf[i]; rng_y = (unsigned long)rand();
iovec[i].iov_len = MSGSIZE; rng_z = (unsigned long)rand();
msg[i].msg_hdr.msg_iov = &iovec[i];
msg[i].msg_hdr.msg_iovlen = 1; /* Pre-fill the constant parts of every datagram once. */
buf[i][0] = 0x00; for (int i = 0; i < PACKET_COUNT; i++) {
buf[i][1] = 0x01; packets[i].cmd = PIXELFLUT_CMD_SET;
packets[i].subcmd = PIXELFLUT_SUBCMD_RGB;
iovecs[i].iov_base = &packets[i];
iovecs[i].iov_len = sizeof packets[i];
messages[i].msg_hdr.msg_iov = &iovecs[i];
messages[i].msg_hdr.msg_iovlen = 1;
} }
int fd = open(INPUT_FILE,O_RDONLY);
printf("fd: %d\n", fd); int frame_fd = open(INPUT_FILE, O_RDONLY);
uint8_t * data = mmap(NULL, SIZE, PROT_READ , MAP_SHARED, fd, 0); if (frame_fd == -1) {
if (data == MAP_FAILED) { perror("open " INPUT_FILE);
return EXIT_FAILURE;
}
const uint8_t *frame =
mmap(NULL, FRAME_BYTES, PROT_READ, MAP_SHARED, frame_fd, 0);
if (frame == MAP_FAILED) {
perror("mmap"); perror("mmap");
} return EXIT_FAILURE;
pthread_t th1,th2,th3,th4; }
pthread_create(&th1, NULL, send_thread, argv[1]);
while (1) { pthread_t sender;
for(int i = 0; i < (int)(RANDOMNESS_LEN/PIXELS); i++){ if (pthread_create(&sender, NULL, send_thread, argv[1]) != 0) {
for(int j=0; j<PIXELS;j++){ perror("pthread_create");
uint64_t pixel = xorshf96() % RANDOMNESS_LEN; return EXIT_FAILURE;
uint16_t x = pixel%WIDTH; }
uint16_t y = pixel/WIDTH;
uint16_t draw_x = OFFSET_X+x; /* Repeatedly repaint the frame in a random pixel order. */
uint16_t draw_y = OFFSET_Y+y; for (;;) {
buf[i][2+j*7+0] = draw_x & 0xff; for (int i = 0; i < PACKET_COUNT; i++) {
buf[i][2+j*7+1] = (draw_x >> 8) & 0xff; for (int j = 0; j < PIXELS_PER_PACKET; j++) {
buf[i][2+j*7+2] = draw_y & 0xff; uint64_t src = xorshf96() % FRAME_PIXELS;
buf[i][2+j*7+3] = (draw_y >> 8) & 0xff; uint16_t fx = (uint16_t)(src % FRAME_WIDTH);
buf[i][2+j*7+4] = *(data+pixel*3+0) & 0xff; uint16_t fy = (uint16_t)(src / FRAME_WIDTH);
buf[i][2+j*7+5] = *(data+pixel*3+1) & 0xff; const uint8_t *rgb = &frame[src * FRAME_CHANNELS];
buf[i][2+j*7+6] = *(data+pixel*3+2) & 0xff;
struct pixelflut_pixel *px = &packets[i].pixels[j];
} px->x = htole16((uint16_t)(CANVAS_OFFSET_X + fx));
} px->y = htole16((uint16_t)(CANVAS_OFFSET_Y + fy));
} px->r = rgb[0];
exit(0); px->g = rgb[1];
px->b = rgb[2];
}
}
}
} }