Add kiosk desktop app

Tauri app for the Pi. Media runs through the native LiveKit SDK because
WebKitGTK has no WebRTC, with frames encoded in Rust and drawn to a canvas
under the annotation overlay.
This commit is contained in:
2026-08-09 17:09:16 -04:00
parent 18384c0cbc
commit 7d0a973638
85 changed files with 8330 additions and 0 deletions
+157
View File
@@ -0,0 +1,157 @@
use std::fs;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use tauri::{AppHandle, Manager};
const CONFIG_FILE_NAME: &str = "kiosk.json";
const PROVISION_PATHS: [&str; 2] = ["/boot/firmware/pistation.json", "/boot/pistation.json"];
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct KioskConfig {
#[serde(default)]
pub server_url: String,
#[serde(default)]
pub enrollment_token: String,
#[serde(default)]
pub kiosk_token: String,
#[serde(default)]
pub kiosk_id: String,
#[serde(default)]
pub room_name: String,
#[serde(default)]
pub livekit_url: String,
#[serde(default)]
pub join_url: String,
}
fn config_path(app: &AppHandle) -> Result<PathBuf, String> {
let directory = app
.path()
.app_config_dir()
.map_err(|error| format!("no config directory: {error}"))?;
fs::create_dir_all(&directory).map_err(|error| format!("cannot create config dir: {error}"))?;
Ok(directory.join(CONFIG_FILE_NAME))
}
fn read_provisioned() -> Option<KioskConfig> {
for path in PROVISION_PATHS {
let Ok(contents) = fs::read_to_string(path) else {
continue;
};
if let Ok(config) = serde_json::from_str::<KioskConfig>(&contents) {
return Some(config);
}
}
None
}
fn env_value(key: &str) -> Option<String> {
std::env::var(key).ok().filter(|value| !value.is_empty())
}
/// Works out the effective configuration.
///
/// Addresses come from, in order of increasing authority: what was saved last, the file on
/// the boot partition, then the environment. Someone who edits the boot file or sets a
/// variable is stating where this screen should point, and that has to beat whatever was
/// saved on a previous run, otherwise a kiosk can never be moved to a new server.
///
/// Credentials work the other way. The kiosk token is earned at enrolment and is only
/// discarded when it is provably useless, which is when the server address changes.
fn merge_provisioned(mut config: KioskConfig) -> KioskConfig {
let provisioned = read_provisioned();
let mut server_url = config.server_url.clone();
let mut join_url = config.join_url.clone();
let mut enrollment_token = config.enrollment_token.clone();
if let Some(provisioned) = provisioned {
if !provisioned.server_url.is_empty() {
server_url = provisioned.server_url;
}
if !provisioned.join_url.is_empty() {
join_url = provisioned.join_url;
}
if !provisioned.enrollment_token.is_empty() {
enrollment_token = provisioned.enrollment_token;
}
}
if let Some(value) = env_value("PISTATION_SERVER_URL") {
server_url = value;
}
if let Some(value) = env_value("PISTATION_JOIN_URL") {
join_url = value;
}
if let Some(value) = env_value("PISTATION_ENROLLMENT_TOKEN") {
enrollment_token = value;
}
// A token issued by one server means nothing to another, so pointing somewhere new
// has to force a fresh enrolment rather than looping on rejected requests.
let is_moving = !config.server_url.is_empty() && config.server_url != server_url;
if is_moving {
eprintln!(
"[pistation] server changed from {} to {}, re-enrolling",
config.server_url, server_url
);
config.kiosk_token.clear();
config.kiosk_id.clear();
}
config.server_url = server_url;
config.join_url = join_url;
// An enrolment token is only of use while there is no kiosk token to replace it.
config.enrollment_token = if config.kiosk_token.is_empty() {
enrollment_token
} else {
String::new()
};
config
}
#[tauri::command]
pub fn load_config(app: AppHandle) -> Result<KioskConfig, String> {
let path = config_path(&app)?;
let stored = fs::read_to_string(&path)
.ok()
.and_then(|contents| serde_json::from_str::<KioskConfig>(&contents).ok())
.unwrap_or_default();
Ok(merge_provisioned(stored))
}
#[tauri::command]
pub fn save_config(app: AppHandle, config: KioskConfig) -> Result<(), String> {
let path = config_path(&app)?;
let contents = serde_json::to_string_pretty(&config)
.map_err(|error| format!("cannot serialize config: {error}"))?;
fs::write(&path, contents).map_err(|error| format!("cannot write config: {error}"))
}
#[tauri::command]
pub fn hardware_id() -> String {
for path in ["/etc/machine-id", "/var/lib/dbus/machine-id"] {
if let Ok(contents) = fs::read_to_string(path) {
let trimmed = contents.trim();
if !trimmed.is_empty() {
return trimmed.to_string();
}
}
}
hostname()
}
fn hostname() -> String {
fs::read_to_string("/etc/hostname")
.map(|value| value.trim().to_string())
.unwrap_or_else(|_| "unknown-kiosk".to_string())
}
+48
View File
@@ -0,0 +1,48 @@
mod config;
mod metrics;
mod room;
use tauri::Manager;
#[tauri::command]
fn is_kiosk_mode() -> bool {
kiosk_mode_enabled()
}
fn kiosk_mode_enabled() -> bool {
match std::env::var("PISTATION_KIOSK").ok().as_deref() {
Some("1") | Some("true") => true,
Some("0") | Some("false") => false,
_ => !cfg!(debug_assertions),
}
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.manage(room::RoomHandle::default())
.invoke_handler(tauri::generate_handler![
config::load_config,
config::save_config,
config::hardware_id,
metrics::collect_metrics,
is_kiosk_mode,
room::room_connect,
room::room_disconnect,
room::video_subscribe,
room::video_unsubscribe
])
.setup(|app| {
if let Some(window) = app.get_webview_window("main") {
if kiosk_mode_enabled() {
let _ = window.set_fullscreen(true);
let _ = window.set_always_on_top(true);
let _ = window.set_cursor_visible(false);
}
}
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
+6
View File
@@ -0,0 +1,6 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
pistation_lib::run()
}
+154
View File
@@ -0,0 +1,154 @@
use std::fs;
use std::sync::Mutex;
use serde::Serialize;
/// Previous CPU sample, so usage can be reported as a delta between heartbeats rather
/// than as the meaningless since-boot average.
static LAST_CPU_SAMPLE: Mutex<Option<CpuSample>> = Mutex::new(None);
#[derive(Clone, Copy)]
struct CpuSample {
total: u64,
idle: u64,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WifiMetrics {
pub interface: String,
pub link_quality: f32,
pub signal_dbm: f32,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Metrics {
pub cpu_percent: Option<f32>,
pub memory_used_bytes: u64,
pub memory_total_bytes: u64,
pub uptime_seconds: u64,
pub temperature_celsius: Option<f32>,
pub wifi: Option<WifiMetrics>,
}
#[tauri::command]
pub fn collect_metrics() -> Metrics {
let memory = read_memory().unwrap_or((0, 0));
Metrics {
cpu_percent: read_cpu_percent(),
memory_used_bytes: memory.0,
memory_total_bytes: memory.1,
uptime_seconds: read_uptime().unwrap_or(0),
temperature_celsius: read_temperature(),
wifi: read_wifi(),
}
}
fn read_cpu_percent() -> Option<f32> {
let contents = fs::read_to_string("/proc/stat").ok()?;
let line = contents.lines().next()?;
if !line.starts_with("cpu ") {
return None;
}
let values: Vec<u64> = line
.split_whitespace()
.skip(1)
.filter_map(|value| value.parse().ok())
.collect();
if values.len() < 5 {
return None;
}
let total: u64 = values.iter().sum();
let idle = values[3] + values[4];
let sample = CpuSample { total, idle };
let mut guard = LAST_CPU_SAMPLE.lock().ok()?;
let previous = guard.replace(sample);
let previous = previous?;
let total_delta = total.checked_sub(previous.total)?;
let idle_delta = idle.checked_sub(previous.idle)?;
if total_delta == 0 {
return None;
}
let busy = total_delta.saturating_sub(idle_delta) as f32;
Some((busy / total_delta as f32) * 100.0)
}
fn read_memory() -> Option<(u64, u64)> {
let contents = fs::read_to_string("/proc/meminfo").ok()?;
let mut total_kb = 0u64;
let mut available_kb = 0u64;
for line in contents.lines() {
let mut parts = line.split_whitespace();
let key = parts.next()?;
let value: u64 = parts.next().and_then(|value| value.parse().ok())?;
match key {
"MemTotal:" => total_kb = value,
"MemAvailable:" => available_kb = value,
_ => {}
}
if total_kb > 0 && available_kb > 0 {
break;
}
}
if total_kb == 0 {
return None;
}
let used_kb = total_kb.saturating_sub(available_kb);
Some((used_kb * 1024, total_kb * 1024))
}
fn read_uptime() -> Option<u64> {
let contents = fs::read_to_string("/proc/uptime").ok()?;
let seconds: f64 = contents.split_whitespace().next()?.parse().ok()?;
Some(seconds as u64)
}
fn read_temperature() -> Option<f32> {
let raw = fs::read_to_string("/sys/class/thermal/thermal_zone0/temp").ok()?;
let millidegrees: f32 = raw.trim().parse().ok()?;
Some(millidegrees / 1000.0)
}
fn read_wifi() -> Option<WifiMetrics> {
let contents = fs::read_to_string("/proc/net/wireless").ok()?;
for line in contents.lines().skip(2) {
let Some((interface, rest)) = line.split_once(':') else {
continue;
};
let values: Vec<&str> = rest.split_whitespace().collect();
if values.len() < 3 {
continue;
}
let link_quality = parse_wireless_number(values[1])?;
let signal_dbm = parse_wireless_number(values[2])?;
return Some(WifiMetrics {
interface: interface.trim().to_string(),
link_quality,
signal_dbm,
});
}
None
}
fn parse_wireless_number(raw: &str) -> Option<f32> {
raw.trim_end_matches('.').parse().ok()
}
@@ -0,0 +1,180 @@
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use livekit::track::{RemoteTrack, TrackSource};
use livekit::{Room, RoomEvent, RoomOptions};
use tauri::{AppHandle, Emitter};
use tokio::sync::Mutex;
use super::events::{
DataPayload, ParticipantView, ParticipantsPayload, StatusPayload, DATA_EVENT,
PARTICIPANTS_EVENT, STATUS_EVENT,
};
use super::video;
/// Bumped on every connect attempt. Tasks belonging to a superseded connection compare
/// against this before emitting anything, so a room we deliberately replaced cannot
/// report itself as disconnected and trigger a pointless reconnect.
static GENERATION: AtomicU64 = AtomicU64::new(0);
pub fn is_current(generation: u64) -> bool {
GENERATION.load(Ordering::SeqCst) == generation
}
#[derive(Default)]
pub struct RoomHandle {
room: Mutex<Option<Arc<Room>>>,
}
impl RoomHandle {
pub async fn connect(&self, app: AppHandle, url: String, token: String) -> Result<(), String> {
let generation = GENERATION.fetch_add(1, Ordering::SeqCst) + 1;
self.close_current().await;
video::stop(&app);
emit_status(&app, generation, StatusPayload::new("connecting"));
let (room, mut events) = Room::connect(&url, &token, RoomOptions::default())
.await
.map_err(|error| error.to_string())?;
if !is_current(generation) {
let _ = room.close().await;
return Ok(());
}
*self.room.lock().await = Some(Arc::new(room));
emit_status(&app, generation, StatusPayload::new("connected"));
let room_for_task = self.room.lock().await.clone();
tauri::async_runtime::spawn(async move {
if let Some(room) = room_for_task.as_ref() {
emit_participants(&app, generation, room);
}
while let Some(event) = events.recv().await {
if !is_current(generation) {
return;
}
if matches!(
event,
RoomEvent::ParticipantConnected(_) | RoomEvent::ParticipantDisconnected(_)
) {
if let Some(room) = room_for_task.as_ref() {
emit_participants(&app, generation, room);
}
}
handle_event(&app, generation, event);
}
if is_current(generation) {
video::stop(&app);
emit_status(&app, generation, StatusPayload::new("disconnected"));
}
});
Ok(())
}
pub async fn disconnect(&self) {
GENERATION.fetch_add(1, Ordering::SeqCst);
self.close_current().await;
}
async fn close_current(&self) {
let existing = self.room.lock().await.take();
if let Some(room) = existing {
let _ = room.close().await;
}
}
}
fn handle_event(app: &AppHandle, generation: u64, event: RoomEvent) {
match event {
RoomEvent::Connected { .. } => emit_status(app, generation, StatusPayload::new("connected")),
RoomEvent::Reconnecting => {
emit_status(app, generation, StatusPayload::new("reconnecting"))
}
RoomEvent::Reconnected => emit_status(app, generation, StatusPayload::new("connected")),
RoomEvent::Disconnected { reason } => {
video::stop(app);
emit_status(
app,
generation,
StatusPayload::with_detail("disconnected", format!("{reason:?}")),
);
}
RoomEvent::DataReceived { payload, .. } => {
if let Ok(envelope) = String::from_utf8(payload.to_vec()) {
let _ = app.emit(DATA_EVENT, DataPayload { envelope });
}
}
RoomEvent::TrackSubscribed {
track, publication, ..
} => {
let priority = match publication.source() {
TrackSource::Screenshare => video::PRIORITY_SCREEN,
TrackSource::Camera => video::PRIORITY_CAMERA,
_ => return,
};
if let RemoteTrack::Video(video_track) = track {
video::start(
app.clone(),
generation,
video_track,
priority,
publication.sid().to_string(),
);
}
}
RoomEvent::TrackUnsubscribed { publication, .. } => {
video::stop_track(app, &publication.sid().to_string());
}
_ => {}
}
}
fn emit_participants(app: &AppHandle, generation: u64, room: &Room) {
if !is_current(generation) {
return;
}
let participants = room
.remote_participants()
.values()
.map(|participant| {
let identity = participant.identity().to_string();
let name = participant.name();
ParticipantView {
display_name: if name.is_empty() {
identity.clone()
} else {
name
},
identity,
}
})
.collect();
let _ = app.emit(PARTICIPANTS_EVENT, ParticipantsPayload { participants });
}
fn emit_status(app: &AppHandle, generation: u64, payload: StatusPayload) {
if !is_current(generation) {
return;
}
let _ = app.emit(STATUS_EVENT, payload);
}
@@ -0,0 +1,56 @@
use serde::Serialize;
pub const STATUS_EVENT: &str = "room://status";
pub const DATA_EVENT: &str = "room://data";
pub const VIDEO_EVENT: &str = "room://video";
pub const PARTICIPANTS_EVENT: &str = "room://participants";
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ParticipantView {
pub identity: String,
pub display_name: String,
}
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ParticipantsPayload {
pub participants: Vec<ParticipantView>,
}
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StatusPayload {
pub status: &'static str,
pub detail: Option<String>,
}
impl StatusPayload {
pub fn new(status: &'static str) -> Self {
Self {
status,
detail: None,
}
}
pub fn with_detail(status: &'static str, detail: String) -> Self {
Self {
status,
detail: Some(detail),
}
}
}
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DataPayload {
pub envelope: String,
}
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VideoPayload {
pub active: bool,
pub width: u32,
pub height: u32,
}
@@ -0,0 +1,131 @@
use std::sync::Mutex;
use std::time::{Duration, Instant};
use image::codecs::jpeg::JpegEncoder;
use image::ExtendedColorType;
use tauri::ipc::{Channel, InvokeResponseBody};
/// Where encoded frames go. The webview opens this channel once and keeps it for the life
/// of the app, so video never has to travel as an event payload.
static SINK: Mutex<Option<Channel<InvokeResponseBody>>> = Mutex::new(None);
static LAST_SENT: Mutex<Option<Instant>> = Mutex::new(None);
const DEFAULT_MAX_WIDTH: u32 = 1920;
const DEFAULT_FPS: u32 = 60;
const DEFAULT_QUALITY: u8 = 85;
/// Read once at startup. A Pi will not keep up with the desktop defaults, so these are
/// tunable without a rebuild: PISTATION_VIDEO_MAX_WIDTH, _FPS and _QUALITY.
pub struct VideoSettings {
pub max_width: u32,
pub frame_interval: Duration,
pub quality: u8,
}
fn settings() -> &'static VideoSettings {
static SETTINGS: std::sync::OnceLock<VideoSettings> = std::sync::OnceLock::new();
SETTINGS.get_or_init(|| {
let max_width = env_number("PISTATION_VIDEO_MAX_WIDTH", DEFAULT_MAX_WIDTH as u64) as u32;
let fps = (env_number("PISTATION_VIDEO_FPS", DEFAULT_FPS as u64) as u32).clamp(1, 120);
let quality = env_number("PISTATION_VIDEO_QUALITY", DEFAULT_QUALITY as u64).clamp(1, 100);
VideoSettings {
max_width: max_width.max(160),
frame_interval: Duration::from_secs_f64(1.0 / fps as f64),
quality: quality as u8,
}
})
}
fn env_number(key: &str, fallback: u64) -> u64 {
std::env::var(key)
.ok()
.and_then(|value| value.parse().ok())
.unwrap_or(fallback)
}
pub fn set_sink(channel: Option<Channel<InvokeResponseBody>>) {
if let Ok(mut guard) = SINK.lock() {
*guard = channel;
}
if let Ok(mut guard) = LAST_SENT.lock() {
*guard = None;
}
}
pub fn has_sink() -> bool {
SINK.lock().map(|guard| guard.is_some()).unwrap_or(false)
}
/// Returns the size this frame should be encoded at, or None when it should be dropped
/// because the previous one went out too recently.
pub fn next_target(width: u32, height: u32) -> Option<(u32, u32)> {
if width == 0 || height == 0 || !has_sink() {
return None;
}
let config = settings();
let mut guard = LAST_SENT.lock().ok()?;
if let Some(last) = *guard {
if last.elapsed() < config.frame_interval {
return None;
}
}
*guard = Some(Instant::now());
if width <= config.max_width {
return Some((width, height));
}
let scaled_height = ((height as f32) * (config.max_width as f32 / width as f32)).round() as u32;
Some((config.max_width, scaled_height.max(1)))
}
/// Packs RGB into `rgb` from an R,G,B,A source and encodes into `encoded`.
///
/// Both buffers are owned by the caller and reused across frames. At sixty frames a second
/// the allocations alone were costing more than the encode.
pub fn send(source: &[u8], rgb: &mut Vec<u8>, encoded: &mut Vec<u8>, width: u32, height: u32) {
let pixels = (width as usize) * (height as usize);
rgb.clear();
rgb.reserve(pixels * 3);
for pixel in source.chunks_exact(4) {
rgb.extend_from_slice(&pixel[..3]);
}
encoded.clear();
let mut encoder = JpegEncoder::new_with_quality(&mut *encoded, settings().quality);
if let Err(error) = encoder.encode(rgb, width, height, ExtendedColorType::Rgb8) {
report_once(&format!("jpeg encode failed: {error}"));
return;
}
let Ok(guard) = SINK.lock() else {
return;
};
if let Some(channel) = guard.as_ref() {
if let Err(error) = channel.send(InvokeResponseBody::Raw(std::mem::take(encoded))) {
report_once(&format!("could not push a frame to the webview: {error}"));
}
}
}
/// Frames arrive many times a second, so a failure that repeats must not flood the log.
fn report_once(message: &str) {
static REPORTED: Mutex<Option<String>> = Mutex::new(None);
if let Ok(mut guard) = REPORTED.lock() {
if guard.as_deref() == Some(message) {
return;
}
*guard = Some(message.to_string());
}
eprintln!("[pistation] {message}");
}
@@ -0,0 +1,38 @@
pub mod client;
mod events;
mod frames;
mod video;
pub use client::RoomHandle;
use tauri::ipc::{Channel, InvokeResponseBody};
use tauri::{AppHandle, State};
/// The webview opens one channel for decoded video and keeps it open. Frames are JPEG
/// encoded in Rust and drawn onto a canvas, which keeps the annotation overlay working
/// exactly as it does in the browser.
#[tauri::command]
pub fn video_subscribe(channel: Channel<InvokeResponseBody>) {
frames::set_sink(Some(channel));
}
#[tauri::command]
pub fn video_unsubscribe() {
frames::set_sink(None);
}
#[tauri::command]
pub async fn room_connect(
app: AppHandle,
handle: State<'_, RoomHandle>,
url: String,
token: String,
) -> Result<(), String> {
handle.connect(app, url, token).await
}
#[tauri::command]
pub async fn room_disconnect(handle: State<'_, RoomHandle>) -> Result<(), String> {
handle.disconnect().await;
Ok(())
}
@@ -0,0 +1,156 @@
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicU8, Ordering};
use std::sync::Mutex;
use futures_util::StreamExt;
use livekit::track::RemoteVideoTrack;
use livekit::webrtc::video_frame::native::VideoFrameBufferExt;
use livekit::webrtc::video_frame::VideoFormatType;
use livekit::webrtc::video_stream::native::NativeVideoStream;
use tauri::{AppHandle, Emitter};
use super::client::is_current;
use super::events::{VideoPayload, VIDEO_EVENT};
use super::frames;
static IS_STREAMING: AtomicBool = AtomicBool::new(false);
/// Bumped whenever the displayed track changes, so a frame loop for a track we have moved
/// off stops without having to be cancelled directly.
static STREAM_EPOCH: AtomicU64 = AtomicU64::new(0);
static CURRENT_PRIORITY: AtomicU8 = AtomicU8::new(0);
static CURRENT_SID: Mutex<Option<String>> = Mutex::new(None);
/// A shared screen always wins over a camera, so plugging in a presentation takes the
/// display back from whoever was pointing a phone at the room.
pub const PRIORITY_CAMERA: u8 = 1;
pub const PRIORITY_SCREEN: u8 = 2;
/// Consumes decoded frames from the subscribed screen share.
///
/// Frames are converted to BGRA, which on a little endian machine is the same layout as
/// cairo's ARgb32, and handed to the native drawing surface sitting under the webview.
pub fn start(
app: AppHandle,
generation: u64,
track: RemoteVideoTrack,
priority: u8,
sid: String,
) {
// A camera must not displace a screen share that is already on the display.
if IS_STREAMING.load(Ordering::SeqCst) && CURRENT_PRIORITY.load(Ordering::SeqCst) > priority {
return;
}
let epoch = STREAM_EPOCH.fetch_add(1, Ordering::SeqCst) + 1;
CURRENT_PRIORITY.store(priority, Ordering::SeqCst);
IS_STREAMING.store(true, Ordering::SeqCst);
if let Ok(mut guard) = CURRENT_SID.lock() {
*guard = Some(sid);
}
tauri::async_runtime::spawn(async move {
let mut stream = NativeVideoStream::new(track.rtc_track());
let mut last_size = (0u32, 0u32);
// Reused across frames so a sixty frame per second stream does not allocate
// several megabytes per frame.
let mut rgba = Vec::new();
let mut rgb = Vec::new();
let mut encoded = Vec::new();
while let Some(frame) = stream.next().await {
if STREAM_EPOCH.load(Ordering::SeqCst) != epoch || !is_current(generation) {
return;
}
let width = frame.buffer.width();
let height = frame.buffer.height();
if width == 0 || height == 0 {
continue;
}
if (width, height) != last_size {
last_size = (width, height);
let _ = app.emit(
VIDEO_EVENT,
VideoPayload {
active: true,
width,
height,
},
);
}
let Some((target_width, target_height)) = frames::next_target(width, height) else {
continue;
};
// to_argb converts, it does not resample, so the buffer has to be scaled
// first. Doing it in I420 is cheaper than scaling four channels of RGBA.
let mut i420 = frame.buffer.to_i420();
let source = if (target_width, target_height) == (width, height) {
i420
} else {
i420.scale(target_width as i32, target_height as i32)
};
let stride = target_width * 4;
rgba.resize((stride * target_height) as usize, 0);
// libyuv names formats after the 32 bit word, not the byte order. On little
// endian its "RGBA" lands in memory as A,B,G,R, so taking the first three
// bytes as red, green and blue picked up alpha as red and tinted everything.
// "ABGR" is the one that lays out as R,G,B,A in memory.
source.to_argb(
VideoFormatType::ABGR,
&mut rgba,
stride,
target_width as i32,
target_height as i32,
);
frames::send(&rgba, &mut rgb, &mut encoded, target_width, target_height);
}
// The stream ended on its own rather than being replaced.
if STREAM_EPOCH.load(Ordering::SeqCst) == epoch && is_current(generation) {
stop(&app);
}
});
}
/// Clears the display only if the track that went away is the one being shown. A camera
/// leaving must not blank a screen share that took over from it.
pub fn stop_track(app: &AppHandle, sid: &str) {
let is_current_track = CURRENT_SID
.lock()
.map(|guard| guard.as_deref() == Some(sid))
.unwrap_or(false);
if is_current_track {
stop(app);
}
}
pub fn stop(app: &AppHandle) {
STREAM_EPOCH.fetch_add(1, Ordering::SeqCst);
CURRENT_PRIORITY.store(0, Ordering::SeqCst);
if let Ok(mut guard) = CURRENT_SID.lock() {
*guard = None;
}
if !IS_STREAMING.swap(false, Ordering::SeqCst) {
return;
}
let _ = app.emit(
VIDEO_EVENT,
VideoPayload {
active: false,
width: 0,
height: 0,
},
);
}