Move
Some checks failed
Check / formatting (push) Failing after 11s
Check / linting (push) Failing after 14s
Check / build (push) Failing after 1m26s

This commit is contained in:
2025-01-26 19:28:51 -08:00
parent af1ce3c723
commit 3d084b7543
13 changed files with 21 additions and 3 deletions

17
server/.cargo/config.toml Normal file
View File

@ -0,0 +1,17 @@
[build]
target = "armv7-unknown-linux-gnueabihf"
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
rustflags = [
"-C",
"link-arg=-march=armv7-a",
"-C",
"link-arg=-marm",
"-C",
"link-arg=-mfpu=neon",
"-C",
"link-arg=-mfloat-abi=hard",
"-C",
"link-arg=-mcpu=cortex-a9",
]

230
server/Cargo.lock generated Normal file
View File

@ -0,0 +1,230 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anstream"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca"
[[package]]
name = "clap"
version = "4.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33e92c5c1a78c62968ec57dbc2440366a2d6e5a23faf829970ff1585dc6b18e2"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4323769dc8a61e2c39ad7dc26f6f2800524691a44d74fe3d1071a5c24db6370"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "proc-macro2"
version = "1.0.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]]
name = "restream"
version = "1.3.1"
dependencies = [
"anyhow",
"clap",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"

9
server/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "restream"
version = "1.3.1"
authors = ["Rien Maertens <rien.maertens@posteo.be>"]
edition = "2018"
[dependencies]
anyhow = "1.0"
clap = { version = "4.4", features = ["derive"] }

171
server/src/main.rs Normal file
View File

@ -0,0 +1,171 @@
#![feature(portable_simd)]
#![feature(stdarch_arm_neon_intrinsics)]
use anyhow::{anyhow, Context, Result};
use clap::{Parser, ValueEnum};
use std::{
fs::File,
io::{BufRead, BufReader, Read, Write},
net::{TcpListener, TcpStream},
process::Command,
time::Duration,
};
mod reduce;
mod rmframes;
use rmframes::RMFrames;
// Parameters only for RM2, firmware version >= 3.7.0.1930
/// Height (in pixels) of the framebuffer.
const HEIGHT: usize = 1404;
/// Width (in pixels) of the framebuffer.
const WIDTH: usize = 1872;
/// How many bytes represent one pixel in the framebuffer.
const BYTES_PER_PIXEL: usize = 2;
const IN_SIZE: usize = WIDTH * HEIGHT * BYTES_PER_PIXEL;
#[derive(Parser, Debug)]
#[command(author, version)]
pub struct Opts {
#[arg(long, name = "port", short = 'p')]
port: usize,
#[arg(long, name = "format", short = 'f')]
format: Format,
}
#[derive(ValueEnum, Debug, Clone, Copy)]
pub enum Format {
Mono,
MonoSimd,
Gray8,
Gray8Simd,
Gray16be,
}
impl Format {
fn run<W: Write>(&self, mut src: RMFrames, mut tgt: W) -> Result<()> {
const IN_SIZE: usize = WIDTH * HEIGHT * BYTES_PER_PIXEL;
match self {
Self::Gray16be => {
let mut buf = [0u8; IN_SIZE];
loop {
src.read_exact(&mut buf)?;
tgt.write_all(&buf[0..IN_SIZE])?;
}
}
Self::Gray8 => {
const OUT: usize = reduce::gray8::out_size(IN_SIZE);
let mut buf = [0u8; IN_SIZE];
loop {
src.read_exact(&mut buf)?;
reduce::gray8::run(&mut buf);
tgt.write_all(&buf[0..OUT])?;
}
}
Self::Gray8Simd => {
const OUT: usize = reduce::gray8_simd::out_size(IN_SIZE);
let mut buf = [0u8; IN_SIZE];
loop {
src.read_exact(&mut buf)?;
reduce::gray8_simd::run(&mut buf);
tgt.write_all(&buf[0..OUT])?;
}
}
Self::Mono => {
const OUT: usize = reduce::mono::out_size(IN_SIZE);
let mut buf = [0u8; IN_SIZE];
loop {
src.read_exact(&mut buf)
.context("while reading framebuffer")?;
reduce::mono::run(&mut buf);
tgt.write_all(&buf[0..OUT]).context("while sending data")?;
}
}
Self::MonoSimd => {
const OUT: usize = reduce::mono_simd::out_size(IN_SIZE);
let mut buf = [0u8; IN_SIZE];
loop {
src.read_exact(&mut buf)?;
reduce::mono_simd::run(&mut buf);
tgt.write_all(&buf[0..OUT])?;
}
}
}
}
}
fn main() -> Result<()> {
let opts: Opts = Opts::parse();
let (file, offset) = {
let pid = xochitl_pid()?;
let offset = rm2_fb_offset(pid)?;
let mem = format!("/proc/{}/mem", pid);
(mem, offset)
};
let src = RMFrames::init(&file, offset, IN_SIZE)?;
let tgt = listen_timeout(opts.port, Duration::from_secs(60))?;
opts.format.run(src, tgt)?;
Ok(())
}
fn listen_timeout(port: usize, timeout: Duration) -> Result<TcpStream> {
let listen_addr = format!("0.0.0.0:{}", port);
let listen = TcpListener::bind(&listen_addr)?;
eprintln!("[rM] listening for a TCP connection on {}", listen_addr);
let (conn, conn_addr) = listen.accept().unwrap();
eprintln!("[rM] connection received from {}", conn_addr);
conn.set_write_timeout(Some(timeout))?;
Ok(conn)
}
fn xochitl_pid() -> Result<usize> {
let output = Command::new("/bin/pidof")
.args(["xochitl"])
.output()
.context("Failed to run `/bin/pidof xochitl`")?;
if output.status.success() {
let pid = &output.stdout;
let pid_str = std::str::from_utf8(pid)?.trim();
pid_str
.parse()
.with_context(|| format!("Failed to parse xochitl's pid: {}", pid_str))
} else {
Err(anyhow!(
"Could not find pid of xochitl, is xochitl running?"
))
}
}
fn rm2_fb_offset(pid: usize) -> Result<usize> {
let file = File::open(format!("/proc/{}/maps", &pid))?;
let line = BufReader::new(file)
.lines()
.skip_while(|line| matches!(line, Ok(l) if !l.ends_with("/dev/fb0")))
.nth(1)
.with_context(|| format!("No line containing /dev/fb0 in /proc/{}/maps file", pid))?
.with_context(|| format!("Error reading file /proc/{}/maps", pid))?;
let addr = line
.split("-")
.next()
.with_context(|| format!("Error parsing line in /proc/{}/maps", pid))?;
let address = usize::from_str_radix(addr, 16).context("Error parsing framebuffer address")?;
println!("Found framebuffer at 0x{:X}", address);
Ok(address + 8)
}

View File

@ -0,0 +1,16 @@
const GROUP_BY: usize = 2;
pub const fn out_size(in_size: usize) -> usize {
in_size / 2
}
pub fn run(buf: &mut [u8]) {
let n_raw = buf.len();
let mut in_cursor = 0;
let mut out_cursor = 0;
while in_cursor + GROUP_BY <= n_raw {
buf[out_cursor] = buf[in_cursor];
out_cursor += 1;
in_cursor += GROUP_BY;
}
}

View File

@ -0,0 +1,33 @@
use std::{
arch::arm::{vld1q_u8, vst1q_u8, vuzpq_u8},
convert::TryInto,
};
const GROUP_BY: usize = 32;
pub const fn out_size(in_size: usize) -> usize {
in_size / 2
}
pub fn run(buf: &mut [u8]) {
let n_raw = buf.len();
let mut in_cursor = 0;
let mut out_cursor = 0;
let mut res = [0u8; 16];
while in_cursor + GROUP_BY <= n_raw {
let a: &[u8; 16] = buf[in_cursor..in_cursor + 16].try_into().unwrap();
let b: &[u8; 16] = buf[in_cursor + 16..in_cursor + 32].try_into().unwrap();
unsafe {
let a = vld1q_u8(a as *const u8);
let b = vld1q_u8(b as *const u8);
let z = vuzpq_u8(a, b);
vst1q_u8(&mut res as *mut u8, z.0);
}
buf[out_cursor..out_cursor + 16].copy_from_slice(&res);
out_cursor += 16;
in_cursor += GROUP_BY;
}
}

4
server/src/reduce/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub mod gray8;
pub mod gray8_simd;
pub mod mono;
pub mod mono_simd;

47
server/src/reduce/mono.rs Normal file
View File

@ -0,0 +1,47 @@
use std::convert::TryInto;
const GROUP_BY: usize = 16;
pub const fn out_size(in_size: usize) -> usize {
in_size / 16
}
pub fn run(buf: &mut [u8]) {
let n_raw = buf.len();
let mut in_cursor = 0;
let mut out_cursor = 0;
while in_cursor + GROUP_BY <= n_raw {
let a: &[u8; 16] = buf[in_cursor..in_cursor + 16].try_into().unwrap();
let mut out = 0u8;
if a[0] == 0x1E {
out |= 0b10000000;
}
if a[2] == 0x1E {
out |= 0b10000000 >> 1;
}
if a[4] == 0x1E {
out |= 0b10000000 >> 2;
}
if a[6] == 0x1E {
out |= 0b10000000 >> 3;
}
if a[8] == 0x1E {
out |= 0b10000000 >> 4;
}
if a[10] == 0x1E {
out |= 0b10000000 >> 5;
}
if a[12] == 0x1E {
out |= 0b10000000 >> 6;
}
if a[14] == 0x1E {
out |= 0b10000000 >> 7;
}
buf[out_cursor] = out;
out_cursor += 1;
in_cursor += GROUP_BY;
}
}

View File

@ -0,0 +1,69 @@
use std::{
arch::arm::{
vandq_u8, vgetq_lane_u64, vld1q_s8, vld1q_u8, vpaddlq_u16, vpaddlq_u32, vpaddlq_u8,
vshlq_u8, vshrq_n_u8, vuzpq_u8,
},
convert::TryInto,
};
const GROUP_BY: usize = 32;
pub const fn out_size(in_size: usize) -> usize {
in_size / 16
}
pub fn run(buf: &mut [u8]) {
let n_raw = buf.len();
let mut in_cursor = 0;
let mut out_cursor = 0;
let m = unsafe {
let mask = &[0x01u8; 16];
vld1q_u8(mask as *const u8)
};
let h = unsafe {
let mask = &[
0x07i8, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02,
0x01, 0x00,
];
vld1q_s8(mask as *const i8)
};
while in_cursor + GROUP_BY <= n_raw {
let a: &[u8; 16] = buf[in_cursor..in_cursor + 16].try_into().unwrap();
let b: &[u8; 16] = buf[in_cursor + 16..in_cursor + 32].try_into().unwrap();
let res: (u8, u8) = unsafe {
// Load 32 bytes
let a = vld1q_u8(a as *const u8);
let b = vld1q_u8(b as *const u8);
// Unzip and get first byte of each pair
let a = vuzpq_u8(a, b).0;
// White = 0b1110, so >> 4.
let a = vshrq_n_u8::<4>(a);
// and with 0x01 mask
let a = vandq_u8(a, m);
// shift each bit left by an appropriate amount
// (h is [0x07, 0x06, .., 0x00, 0x07, .., 0x00])
let a = vshlq_u8(a, h);
// Sum everything
let s = vpaddlq_u8(a);
let s = vpaddlq_u16(s);
let s = vpaddlq_u32(s);
(
vgetq_lane_u64(s, 0).try_into().unwrap(),
vgetq_lane_u64(s, 1).try_into().unwrap(),
)
};
buf[out_cursor] = res.0;
buf[out_cursor + 1] = res.1;
out_cursor += 2;
in_cursor += GROUP_BY;
}
}

54
server/src/rmframes.rs Normal file
View File

@ -0,0 +1,54 @@
use anyhow::Result;
use std::{
fs::File,
io::{Read, Seek, SeekFrom},
};
pub struct RMFrames {
file: File,
start: u64,
cursor: usize,
framebuffer_size: usize,
}
impl RMFrames {
pub fn init(path: &str, offset: usize, framebuffer_size: usize) -> Result<Self> {
let start = offset as u64;
let cursor = 0;
let file = File::open(path)?;
let mut streamer = RMFrames {
file,
start,
cursor,
framebuffer_size,
};
streamer.next_frame()?;
Ok(streamer)
}
pub fn next_frame(&mut self) -> std::io::Result<()> {
self.file.seek(SeekFrom::Start(self.start))?;
self.cursor = 0;
Ok(())
}
}
impl Read for RMFrames {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let requested = buf.len();
let bytes_read = if self.cursor + requested < self.framebuffer_size {
self.file.read(buf)?
} else {
let rest = self.framebuffer_size - self.cursor;
self.file.read(&mut buf[0..rest])?
};
self.cursor += bytes_read;
if self.cursor == self.framebuffer_size {
self.next_frame()?;
}
Ok(bytes_read)
}
}

70
server/src/streamlsb.rs Normal file
View File

@ -0,0 +1,70 @@
use std::io::Read;
const BYTE_FACTOR: usize = 2;
/// Read every other byte from `R`.
///
/// If `R` is a stream of u16be, this should return the least-significant byte of each u16.
pub struct StreamLSB<R: Read> {
/// The source to read from
src: R,
/// Number of filled bits in fragbuf
arity: usize,
/// Hold a partial u16, in case we read half of one
fragbuf: [u8; BYTE_FACTOR],
}
impl<R: Read> StreamLSB<R> {
pub fn new(src: R) -> Self {
Self {
src,
arity: 0,
fragbuf: [0; BYTE_FACTOR],
}
}
}
impl<R: Read> Read for StreamLSB<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let n_raw = self.src.read(buf)?;
let mut in_cursor = 0;
let mut out_cursor = 0;
if self.arity != 0 {
// Number of bytes we need to read
let need_read = BYTE_FACTOR - self.arity;
let can_read = need_read.min(buf.len());
self.fragbuf[self.arity..self.arity + can_read].copy_from_slice(&buf[0..can_read]);
in_cursor += can_read;
self.arity += can_read;
self.arity %= BYTE_FACTOR;
if self.arity != 0 {
// We did not read a full set of bytes,
// wait for more data.
return Ok(0);
} else {
// Extract least-significant byte
buf[0] = self.fragbuf[0];
out_cursor += 1;
}
}
for _ in in_cursor..(n_raw / BYTE_FACTOR) {
// Extract least-significant byte
buf[out_cursor] = buf[in_cursor];
in_cursor += BYTE_FACTOR;
out_cursor += 1;
}
self.arity = n_raw - in_cursor;
self.fragbuf[0..self.arity].copy_from_slice(&buf[in_cursor..n_raw]);
return Ok(out_cursor);
}
}