use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering::Relaxed};
use std::thread::JoinHandle;
use anyhow::{Context, Result};
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::text::Span;
use ratatui::widgets::Paragraph;
use ratatui_image::FontSize;
use ratatui_image::picker::cap_parser::Parser;
use tdlib_rs::types::VideoNote;
use tokio::sync::mpsc::UnboundedSender;
use crate::lang;
use crate::messages::{MediaFile, Msg};
use crate::term::{self, Terminal};
use crate::theme::Colors;
use crate::viewer::SIZES;
pub const MAX_FILE: u64 = 256 << 20;
const FPS: f64 = 30.0;
const MAX_SIDE: i32 = 1024;
#[derive(Clone)]
pub struct Note {
pub file_id: i32,
pub size: u64,
pub duration: i32,
pub length: u32,
}
impl Note {
pub fn new(note: &VideoNote) -> Self {
let size = note.video.size.max(note.video.expected_size);
Self {
file_id: note.video.id,
size: u64::try_from(size).unwrap_or(0),
duration: note.duration.max(0),
length: note.length.clamp(1, MAX_SIDE) as u32,
}
}
}
pub struct VideoEvent {
pub tag: u8,
run: u64,
pub news: News,
}
pub enum News {
Frame,
Moved,
Ended,
Failed(String),
}
pub const TAG_VIEW: u8 = 0;
pub const TAG_INLINE: u8 = 1;
static RUNS: AtomicU64 = AtomicU64::new(0);
const KITTY_ID: u32 = 0x7000_0000;
fn frames_parent() -> PathBuf {
let shm = PathBuf::from("/dev/shm");
if shm.is_dir() {
shm
} else {
std::env::temp_dir()
}
}
enum Run {
Frames(Player),
#[cfg(unix)]
Pepper(PepperPlayer),
}
impl Run {
fn run(&self) -> u64 {
match self {
Run::Frames(p) => p.run,
#[cfg(unix)]
Run::Pepper(p) => p.run,
}
}
fn paused(&self) -> bool {
match self {
Run::Frames(p) => p.paused,
#[cfg(unix)]
Run::Pepper(p) => p.paused,
}
}
fn toggle(&mut self) {
match self {
Run::Frames(p) => p.toggle(),
#[cfg(unix)]
Run::Pepper(p) => p.toggle(),
}
}
fn position(&self, start: f64) -> f64 {
match self {
Run::Frames(p) => start + p.frames.load(Relaxed) as f64 / FPS,
#[cfg(unix)]
Run::Pepper(p) => f64::from_bits(p.seconds.load(Relaxed)),
}
}
fn frames(&self) -> Option<&Player> {
match self {
Run::Frames(p) => Some(p),
#[cfg(unix)]
Run::Pepper(_) => None,
}
}
}
#[cfg(unix)]
struct PepperPlayer {
child: Child,
control: std::os::unix::net::UnixStream,
reader: Option<JoinHandle<()>>,
token: String,
seconds: Arc<AtomicU64>,
placed: Option<Rect>,
run: u64,
paused: bool,
}
#[cfg(unix)]
impl PepperPlayer {
fn send(&mut self, command: serde_json::Value) {
use std::io::Write;
let line = serde_json::json!({ "command": command }).to_string();
let _ = writeln!(self.control, "{line}");
}
fn toggle(&mut self) {
self.paused = !self.paused;
let pause = self.paused;
self.send(serde_json::json!(["set_property", "pause", pause]));
}
fn place(&mut self, terminal: &mut Terminal, area: Option<Rect>) {
if self.placed == area {
return;
}
let code = match area {
Some(area) => crate::pepper::place(&self.token, area),
None => crate::pepper::remove(&self.token),
};
let _ = term::send(terminal, [code]);
self.placed = area;
}
}
#[cfg(unix)]
impl Drop for PepperPlayer {
fn drop(&mut self) {
let _ = self.control.shutdown(std::net::Shutdown::Both);
let _ = self.child.kill();
let _ = self.child.wait();
if let Some(reader) = self.reader.take() {
let _ = reader.join();
}
if self.placed.is_some() {
use std::io::Write;
let _ = std::io::stdout().write_all(crate::pepper::remove(&self.token).as_bytes());
let _ = std::io::stdout().flush();
}
}
}
struct Player {
ffmpeg: Child,
audio: Option<Child>,
pump: Option<JoinHandle<()>>,
dir: PathBuf,
run: u64,
frames: Arc<AtomicU64>,
stop: Arc<AtomicBool>,
paused: bool,
}
impl Player {
fn frame_file(&self) -> PathBuf {
self.dir.join("frame.rgb")
}
#[cfg(unix)]
fn toggle(&mut self) {
let signal = if self.paused {
libc::SIGCONT
} else {
libc::SIGSTOP
};
for child in std::iter::once(&self.ffmpeg).chain(&self.audio) {
let pid: i32 = match child.id().try_into() {
Ok(pid) if pid > 0 => pid,
_ => continue,
};
unsafe { libc::kill(pid, signal) };
}
self.paused = !self.paused;
}
#[cfg(not(unix))]
fn toggle(&mut self) {}
}
impl Drop for Player {
fn drop(&mut self) {
self.stop.store(true, Relaxed);
let _ = self.ffmpeg.kill();
let _ = self.ffmpeg.wait();
if let Some(audio) = &mut self.audio {
let _ = audio.kill();
let _ = audio.wait();
}
if let Some(pump) = self.pump.take() {
let _ = pump.join();
}
let _ = std::fs::remove_dir_all(&self.dir);
}
}
fn pump_frames(
mut pipe: impl Read,
(width, height): (u32, u32),
dir: &Path,
frames: &AtomicU64,
stop: &AtomicBool,
(tag, run): (u8, u64),
tx: &UnboundedSender<VideoEvent>,
) {
let send = |news| tx.send(VideoEvent { tag, run, news }).is_ok();
let size = width as usize * height as usize * 4;
let tmp = dir.join("frame.tmp");
let out = dir.join("frame.rgb");
let mut buf = vec![0u8; size];
loop {
if stop.load(Relaxed) {
return;
}
if let Err(e) = pipe.read_exact(&mut buf) {
if !stop.load(Relaxed) {
send(match e.kind() {
std::io::ErrorKind::UnexpectedEof => News::Ended,
_ => News::Failed(format!("{}: {e}", lang::t("decoding broke"))),
});
}
return;
}
let written = std::fs::write(&tmp, &buf).and_then(|()| std::fs::rename(&tmp, &out));
if let Err(e) = written {
send(News::Failed(format!(
"{}: {e}",
lang::t("the frame file can't be written")
)));
return;
}
frames.fetch_add(1, Relaxed);
if !send(News::Frame) {
return;
}
}
}
#[derive(Clone, Copy)]
struct Round {
side: u32,
backdrop: [u8; 3],
}
impl Round {
fn of(note: &Note, bubble: Color) -> Self {
let backdrop = match bubble {
Color::Rgb(r, g, b) => [r, g, b],
_ => [0, 0, 0],
};
Self {
side: note.length,
backdrop,
}
}
}
fn ffmpeg_args(path: &Path, start: &str, filter: &str) -> Vec<String> {
let input = format!("file:{}", path.to_string_lossy());
[
"-v",
"error",
"-nostdin",
"-re",
"-protocol_whitelist",
"file",
"-f",
"mov",
"-ss",
start,
"-i",
&input,
"-an",
"-vf",
filter,
"-f",
"rawvideo",
"-pix_fmt",
"rgba",
"pipe:1",
]
.map(String::from)
.to_vec()
}
const STRICT: [&str; 7] = [
"--no-config",
"--load-scripts=no",
"--terminal=no",
"--ytdl=no",
"--demuxer=lavf",
"--demuxer-lavf-format=mov",
"--demuxer-lavf-o=protocol_whitelist=file",
];
fn mpv_args(path: &Path, start: &str) -> Vec<String> {
let mut args: Vec<String> = STRICT.map(String::from).into();
args.extend(["--no-video".into(), format!("--start={start}")]);
args.extend(["--".into(), path.to_string_lossy().into_owned()]);
args
}
#[cfg(unix)]
fn pepper_args(path: &Path, start: &str, round: Round) -> Vec<String> {
let [r, g, b] = round.backdrop;
let graph = round_graph(round);
let mut args: Vec<String> = STRICT.map(String::from).into();
args.extend([
"--keep-open=no".into(),
"--input-ipc-client=fd://3".into(),
format!("--start={start}"),
"--hwdec=no".into(),
format!("--vf=lavfi=graph=%{}%{graph}", graph.len()),
format!("--background-color=#{r:02x}{g:02x}{b:02x}"),
]);
args.extend(["--".into(), path.to_string_lossy().into_owned()]);
args
}
fn round_graph(
Round {
side,
backdrop: [r, g, b],
}: Round,
) -> String {
let cover = "clip(min(W,H)/2-hypot(X-(W-1)/2,Y-(H-1)/2)+0.5,0,1)";
format!(
"scale={side}:{side},split[a][b];\
[b]drawbox=c=0x{r:02x}{g:02x}{b:02x}:t=fill[back];\
color=black:s={side}x{side}:r=1:d=1,format=gray,geq=lum='255*{cover}',loop=-1:1[circle];\
[a]format=rgba[video];\
[video][circle]alphamerge=shortest=1[cut];\
[back][cut]overlay"
)
}
fn spawn(
path: &Path,
start: f64,
round: Round,
tag: u8,
tx: UnboundedSender<VideoEvent>,
pepper: Option<&Path>,
) -> Result<Run> {
#[cfg(unix)]
if let Some(play) = pepper
&& let Ok(player) = spawn_pepper(play, path, start, round, tag, tx.clone())
{
return Ok(Run::Pepper(player));
}
#[cfg(not(unix))]
let _ = pepper;
spawn_frames(path, start, round, tag, tx).map(Run::Frames)
}
#[cfg(unix)]
fn spawn_pepper(
play: &Path,
path: &Path,
start: f64,
round: Round,
tag: u8,
tx: UnboundedSender<VideoEvent>,
) -> Result<PepperPlayer> {
use std::os::fd::AsRawFd;
use std::os::unix::process::CommandExt;
let token = crate::pepper::new_token().context("no randomness for Pepper's token")?;
let (ours, theirs) = std::os::unix::net::UnixStream::pair()?;
let reading = ours.try_clone()?;
let fd = theirs.as_raw_fd();
let mut command = Command::new(play);
command
.args(pepper_args(path, &format!("{start:.3}"), round))
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
unsafe {
command.pre_exec(move || {
let ok = if fd == 3 {
libc::fcntl(3, libc::F_SETFD, 0) != -1
} else {
libc::dup2(fd, 3) != -1
};
if ok {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
});
}
let child = command.spawn().context("pepper-play can't start")?;
drop(theirs);
let run = RUNS.fetch_add(1, Relaxed);
let seconds = Arc::new(AtomicU64::new(start.to_bits()));
let reader = {
let seconds = Arc::clone(&seconds);
std::thread::spawn(move || read_pepper(reading, &seconds, (tag, run), &tx))
};
let mut player = PepperPlayer {
child,
control: ours,
reader: Some(reader),
token,
seconds,
placed: None,
run,
paused: false,
};
let token = player.token.clone();
player.send(serde_json::json!([
"script-message-to",
"main",
"pepper-token",
token
]));
player.send(serde_json::json!(["observe_property", 1, "time-pos"]));
Ok(player)
}
#[cfg(unix)]
#[derive(Debug, PartialEq)]
enum Said {
Started,
At(f64),
Ended,
Failed(String),
}
#[cfg(unix)]
fn said(line: &str) -> Option<Said> {
let said: serde_json::Value = serde_json::from_str(line).ok()?;
match said.get("event")?.as_str()? {
"playback-restart" => Some(Said::Started),
"property-change" if said.get("name")? == "time-pos" => {
said.get("data")?.as_f64().map(Said::At)
}
"end-file" if said.get("reason")? == "error" => {
let why = said.get("file_error").and_then(|e| e.as_str());
Some(Said::Failed(crate::text::clean(
why.unwrap_or(lang::t("it can't be played")),
)))
}
"end-file" => Some(Said::Ended),
_ => None,
}
}
#[cfg(unix)]
fn read_pepper(
stream: impl Read,
seconds: &AtomicU64,
(tag, run): (u8, u64),
tx: &UnboundedSender<VideoEvent>,
) {
use std::io::BufRead;
let send = |news| tx.send(VideoEvent { tag, run, news }).is_ok();
let mut started = false;
for line in std::io::BufReader::new(stream).lines() {
let Ok(line) = line else {
break;
};
match said(&line) {
Some(Said::Started) if !started => {
started = true;
if !send(News::Frame) {
return;
}
}
Some(Said::At(at)) => {
let before = f64::from_bits(seconds.swap(at.to_bits(), Relaxed));
if at.floor() != before.floor() && !send(News::Moved) {
return;
}
}
Some(Said::Ended) => {
send(News::Ended);
return;
}
Some(Said::Failed(why)) => {
send(News::Failed(why));
return;
}
_ => {}
}
}
send(if started {
News::Ended
} else {
News::Failed("pepper-play stopped".into())
});
}
fn spawn_frames(
path: &Path,
start: f64,
round: Round,
tag: u8,
tx: UnboundedSender<VideoEvent>,
) -> Result<Player> {
let dir = crate::config::new_private_dir(&frames_parent(), "tuigram-video")?;
let filter = format!("fps={FPS},{}", round_graph(round));
let start = format!("{start:.3}");
let ffmpeg = Command::new("ffmpeg")
.args(ffmpeg_args(path, &start, &filter))
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn();
let ffmpeg = match ffmpeg {
Ok(ffmpeg) => ffmpeg,
Err(e) => {
let _ = std::fs::remove_dir_all(&dir);
return Err(e).context(lang::t("ffmpeg isn't installed"));
}
};
let mut player = Player {
ffmpeg,
audio: None,
pump: None,
dir,
run: RUNS.fetch_add(1, Relaxed),
frames: Arc::new(AtomicU64::new(0)),
stop: Arc::new(AtomicBool::new(false)),
paused: false,
};
let pipe = player
.ffmpeg
.stdout
.take()
.context("ffmpeg's frames are piped")?;
player.audio = Command::new("mpv")
.args(mpv_args(path, &start))
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.ok();
let (frames, stop) = (Arc::clone(&player.frames), Arc::clone(&player.stop));
let (dir, run, side) = (player.dir.clone(), player.run, round.side);
player.pump = Some(std::thread::spawn(move || {
pump_frames(pipe, (side, side), &dir, &frames, &stop, (tag, run), &tx);
}));
Ok(player)
}
pub struct VideoView {
pub chat_id: i64,
pub message_id: i64,
pub file: MediaFile,
pub note: Note,
pub cant_copy: Option<&'static str>,
round: Round,
pub tell: bool,
path: Option<PathBuf>,
player: Option<Run>,
pepper: Option<PathBuf>,
tx: UnboundedSender<VideoEvent>,
start: f64,
zoom: Option<usize>,
rect: Option<Rect>,
side: Option<u32>,
pending: bool,
framed: bool,
done: bool,
pub error: Option<String>,
tmux: bool,
}
impl VideoView {
pub fn of(
chat_id: i64,
message_id: i64,
msg: &Msg,
tx: UnboundedSender<VideoEvent>,
tmux: bool,
pepper: Option<PathBuf>,
colors: &Colors,
) -> Option<Self> {
let note = msg.video.clone()?;
let file = msg.file.clone().filter(|f| f.id == note.file_id)?;
Some(Self {
chat_id,
message_id,
file,
round: Round::of(¬e, colors.other_bubble),
note,
cant_copy: crate::viewer::cant_copy(msg),
tell: false,
path: None,
player: None,
pepper,
tx,
start: 0.0,
zoom: None,
rect: None,
side: None,
pending: false,
framed: false,
done: false,
error: None,
tmux,
})
}
pub fn waits_for(&self, file_id: i32) -> bool {
self.path.is_none() && self.note.file_id == file_id
}
pub fn downloaded(&mut self, path: PathBuf) {
self.path = Some(path);
}
pub fn tell_now(&mut self) -> bool {
let now = self.tell && self.framed;
if now {
self.tell = false;
}
now
}
fn position(&self) -> f64 {
self.player
.as_ref()
.map_or(self.start, |p| p.position(self.start))
}
pub fn status(&self) -> &'static str {
match &self.player {
None => lang::t("loading…"),
Some(p) if p.paused() => lang::t("paused"),
Some(_) => lang::t("playing"),
}
}
pub fn resume_at(&mut self, secs: f64) {
self.start = secs;
}
pub fn lost(&self, chat_id: i64, message_id: i64, msg: Option<&Msg>) -> bool {
chat_id == self.chat_id
&& message_id == self.message_id
&& msg.and_then(|m| m.video.as_ref()).map(|v| v.file_id) != Some(self.note.file_id)
}
pub fn toggle(&mut self) {
if let Some(player) = &mut self.player {
player.toggle();
}
}
pub fn zoom_in(&mut self) {
let now = self.share();
if let Some(i) = SIZES.iter().position(|&s| s > now + 0.001) {
self.zoom = Some(i);
}
}
pub fn zoom_out(&mut self) {
let now = self.share();
if let Some(i) = SIZES.iter().rposition(|&s| s < now - 0.001) {
self.zoom = Some(i);
}
}
fn share(&self) -> f64 {
self.zoom.map_or(1.0, |i| SIZES[i])
}
pub fn on_event(&mut self, event: VideoEvent) -> Result<(), String> {
if self.player.as_ref().is_none_or(|p| p.run() != event.run) {
return Ok(());
}
match event.news {
News::Frame => {
self.pending = true;
self.framed = true;
Ok(())
}
News::Moved => Ok(()),
News::Ended => {
self.player = None;
self.done = true;
Ok(())
}
News::Failed(_)
if !self.framed && self.player.as_ref().is_some_and(|p| p.frames().is_none()) =>
{
self.pepper = None;
self.player = None;
Ok(())
}
News::Failed(why) => Err(why),
}
}
pub fn finished(&self) -> bool {
self.done
}
fn layout(&self, inner: Rect, font: FontSize) -> (Rect, (u32, u32)) {
let share = self.share();
let (fw, fh) = (
f64::from(font.width).max(1.0),
f64::from(font.height).max(1.0),
);
let mut cols = f64::from(inner.width) * share;
let mut rows = f64::from(inner.height) * share;
if cols * fw > rows * fh {
cols = rows * fh / fw;
} else {
rows = cols * fw / fh;
}
let cols = cols.max(1.0) as u16;
let rows = rows.max(1.0) as u16;
let rect = Rect {
x: inner.x + inner.width.saturating_sub(cols) / 2,
y: inner.y + inner.height.saturating_sub(rows) / 2,
width: cols,
height: rows,
};
let pixels = ((f64::from(cols) * fw) as u32, (f64::from(rows) * fh) as u32);
(rect, pixels)
}
fn frame_side(&self, (w, h): (u32, u32)) -> u32 {
self.round.side.min(w).min(h).max(1)
}
pub fn draw(&mut self, frame: &mut Frame, area: Rect, font: FontSize, colors: &Colors) {
let block = crate::ui::bordered(colors);
let inner = block.inner(area);
let (at, pixels) = self.layout(inner, font);
let side = self.frame_side(pixels);
self.rect = Some(at);
let now = self.position().min(f64::from(self.note.duration)) as u32;
let of = self.note.duration.max(0) as u32;
let mut title = format!(
" {} · {} · {}:{:02} {} {}:{:02}",
self.file.label,
self.status(),
now / 60,
now % 60,
lang::t("of"),
of / 60,
of % 60
);
if let Some(i) = self.zoom {
title.push_str(&format!(" · {:.0}%", SIZES[i] * 100.0));
}
frame.render_widget(crate::ui::Cover, area);
frame.render_widget(Paragraph::new(Span::from(title)).block(block), area);
frame.render_widget(crate::ui::Cover, at);
let needs_restart = self.path.is_some()
&& !self.done
&& self
.player
.as_ref()
.is_none_or(|p| p.frames().is_some() && self.side != Some(side));
if needs_restart && self.error.is_none() {
let position = if self.player.is_some() {
self.position()
} else {
self.start
};
let was_paused = self.player.as_ref().is_some_and(Run::paused);
self.player = None;
match spawn(
&self.path.clone().unwrap(),
position,
Round { side, ..self.round },
TAG_VIEW,
self.tx.clone(),
self.pepper.as_deref(),
) {
Ok(mut player) => {
if was_paused {
player.toggle();
}
self.player = Some(player);
self.start = position;
self.side = Some(side);
}
Err(e) => self.error = Some(format!("{e:#}")),
}
}
}
pub fn send_frame(&mut self, terminal: &mut Terminal) {
#[cfg(unix)]
if let Some(Run::Pepper(player)) = &mut self.player {
player.place(terminal, self.rect);
return;
}
let Some(player) = self.player.as_ref().and_then(Run::frames) else {
return;
};
let (Some(at), Some(side)) = (self.rect, self.side) else {
return;
};
if !self.pending {
return;
}
self.pending = false;
let path = kitty_path(&player.frame_file());
let (start, escape, end) = Parser::tmux_start_escape_end(self.tmux);
let codes = [
format!("\x1b[{};{}H", at.y + 1, at.x + 1),
format!(
"{start}{escape}_Ga=T,t=f,f=32,s={side},v={side},i={KITTY_ID},q=2,c={},r={};{path}{escape}\\{end}",
at.width, at.height
),
];
let _ = term::send(terminal, codes);
}
}
fn kitty_path(file: &Path) -> String {
use base64::Engine;
base64::engine::general_purpose::STANDARD.encode(file.as_os_str().as_encoded_bytes())
}
impl Drop for VideoView {
fn drop(&mut self) {
let (start, escape, end) = Parser::tmux_start_escape_end(self.tmux);
let code = format!("{start}{escape}_Ga=d,d=I,i={KITTY_ID},q=2{escape}\\{end}");
use std::io::Write;
let _ = std::io::stdout().write_all(code.as_bytes());
let _ = std::io::stdout().flush();
}
}
const INLINE_KITTY_ID: u32 = 0x7000_0001;
pub struct Inline {
pub chat_id: i64,
pub message_id: i64,
pub note: Note,
pub tell: bool,
path: Option<PathBuf>,
round: Round,
player: Option<Run>,
pepper: Option<PathBuf>,
tx: UnboundedSender<VideoEvent>,
pending: bool,
framed: bool,
medium: bool,
pub seen: bool,
pub placement: Option<(u16, u16)>,
pub area: Option<Rect>,
tmux: bool,
}
impl Inline {
pub fn of(
chat_id: i64,
message_id: i64,
msg: &Msg,
tx: UnboundedSender<VideoEvent>,
tmux: bool,
pepper: Option<PathBuf>,
colors: &Colors,
) -> Option<Self> {
let note = msg.video.clone()?;
msg.preview.as_ref()?;
Some(Self {
chat_id,
message_id,
round: Round::of(
¬e,
if msg.outgoing {
colors.own_bubble
} else {
colors.other_bubble
},
),
note,
tell: false,
path: None,
player: None,
pepper,
tx,
pending: false,
framed: false,
medium: false,
seen: false,
placement: None,
area: None,
tmux,
})
}
pub fn is_on(&self, chat_id: i64, message_id: i64) -> bool {
self.chat_id == chat_id && self.message_id == message_id
}
pub fn waits_for(&self, file_id: i32) -> bool {
self.path.is_none() && self.note.file_id == file_id
}
pub fn downloaded(&mut self, path: PathBuf) -> Result<(), String> {
self.path = Some(path);
self.start_over()
}
fn start_over(&mut self) -> Result<(), String> {
let Some(path) = &self.path else {
return Ok(());
};
let pepper = self.pepper.as_deref();
let run = spawn(path, 0.0, self.round, TAG_INLINE, self.tx.clone(), pepper);
self.player = Some(run.map_err(|e| format!("{e:#}"))?);
self.framed = false;
Ok(())
}
pub fn tell_now(&mut self) -> bool {
let now = self.tell && self.framed;
if now {
self.tell = false;
}
now
}
pub fn started(&self) -> bool {
self.player.is_some()
}
pub fn in_pepper(&self) -> bool {
self.player.as_ref().is_some_and(|p| p.frames().is_none())
}
pub fn position(&self) -> f64 {
self.player.as_ref().map_or(0.0, |p| p.position(0.0))
}
pub fn toggle(&mut self) {
match &mut self.player {
Some(player) => player.toggle(),
None => {
let _ = self.start_over();
}
}
}
pub fn toggle_zoom(&mut self) {
self.medium = !self.medium;
}
pub fn medium(&self) -> bool {
self.medium
}
pub fn lost(&self, chat_id: i64, message_id: i64, msg: Option<&Msg>) -> bool {
chat_id == self.chat_id
&& message_id == self.message_id
&& msg.and_then(|m| m.video.as_ref()).map(|v| v.file_id) != Some(self.note.file_id)
}
pub fn on_event(&mut self, event: VideoEvent) -> Result<(), String> {
if self.player.as_ref().is_none_or(|p| p.run() != event.run) {
return Ok(());
}
match event.news {
News::Frame => {
self.pending = true;
self.framed = true;
Ok(())
}
News::Moved => Ok(()),
News::Ended => {
self.player = None;
Ok(())
}
News::Failed(_) if !self.framed && self.in_pepper() => {
self.pepper = None;
self.start_over()
}
News::Failed(why) => Err(why),
}
}
pub fn send_frame(&mut self, terminal: &mut Terminal) {
#[cfg(unix)]
if let Some(Run::Pepper(player)) = &mut self.player {
player.place(terminal, self.area);
return;
}
let Some(player) = self.player.as_ref().and_then(Run::frames) else {
return;
};
let Some((c, r)) = self.placement else {
return;
};
if !self.pending {
return;
}
self.pending = false;
let path = kitty_path(&player.frame_file());
let (start, escape, end) = Parser::tmux_start_escape_end(self.tmux);
let (s, v) = (self.round.side, self.round.side);
let code = format!(
"{start}{escape}_Gq=2,i={INLINE_KITTY_ID},a=T,U=1,f=32,t=f,s={s},v={v},c={c},r={r};{path}{escape}\\{end}"
);
let _ = term::send(terminal, [code]);
}
}
impl Drop for Inline {
fn drop(&mut self) {
let (start, escape, end) = Parser::tmux_start_escape_end(self.tmux);
let code = format!("{start}{escape}_Ga=d,d=I,i={INLINE_KITTY_ID},q=2{escape}\\{end}");
use std::io::Write;
let _ = std::io::stdout().write_all(code.as_bytes());
let _ = std::io::stdout().flush();
}
}
pub struct InlineImage {
pub skip: u16,
}
impl ratatui::widgets::Widget for InlineImage {
fn render(self, area: Rect, buf: &mut ratatui::buffer::Buffer) {
use ratatui::buffer::CellDiffOption;
use std::num::NonZeroU16;
let [id_extra, r, g, b] = INLINE_KITTY_ID.to_be_bytes();
let fg = ratatui::style::Color::Rgb(r, g, b);
let max = DIACRITICS.len() as u16;
let mut symbol = String::with_capacity(8);
for y in 0..area.height.min(max) {
let row = y + self.skip;
if row >= max {
break;
}
for x in 0..area.width.min(max) {
let Some(cell) = buf.cell_mut((area.x + x, area.y + y)) else {
continue;
};
symbol.clear();
symbol.push(crate::images::PLACEHOLDER);
if x == 0 {
symbol.push(diacritic(row));
symbol.push(diacritic(0));
symbol.push(diacritic(u16::from(id_extra)));
}
cell.set_symbol(&symbol)
.set_fg(fg)
.set_diff_option(CellDiffOption::ForcedWidth(NonZeroU16::new(1).unwrap()));
}
}
}
}
static DIACRITICS: [char; 297] = [
'\u{305}',
'\u{30D}',
'\u{30E}',
'\u{310}',
'\u{312}',
'\u{33D}',
'\u{33E}',
'\u{33F}',
'\u{346}',
'\u{34A}',
'\u{34B}',
'\u{34C}',
'\u{350}',
'\u{351}',
'\u{352}',
'\u{357}',
'\u{35B}',
'\u{363}',
'\u{364}',
'\u{365}',
'\u{366}',
'\u{367}',
'\u{368}',
'\u{369}',
'\u{36A}',
'\u{36B}',
'\u{36C}',
'\u{36D}',
'\u{36E}',
'\u{36F}',
'\u{483}',
'\u{484}',
'\u{485}',
'\u{486}',
'\u{487}',
'\u{592}',
'\u{593}',
'\u{594}',
'\u{595}',
'\u{597}',
'\u{598}',
'\u{599}',
'\u{59C}',
'\u{59D}',
'\u{59E}',
'\u{59F}',
'\u{5A0}',
'\u{5A1}',
'\u{5A8}',
'\u{5A9}',
'\u{5AB}',
'\u{5AC}',
'\u{5AF}',
'\u{5C4}',
'\u{610}',
'\u{611}',
'\u{612}',
'\u{613}',
'\u{614}',
'\u{615}',
'\u{616}',
'\u{617}',
'\u{657}',
'\u{658}',
'\u{659}',
'\u{65A}',
'\u{65B}',
'\u{65D}',
'\u{65E}',
'\u{6D6}',
'\u{6D7}',
'\u{6D8}',
'\u{6D9}',
'\u{6DA}',
'\u{6DB}',
'\u{6DC}',
'\u{6DF}',
'\u{6E0}',
'\u{6E1}',
'\u{6E2}',
'\u{6E4}',
'\u{6E7}',
'\u{6E8}',
'\u{6EB}',
'\u{6EC}',
'\u{730}',
'\u{732}',
'\u{733}',
'\u{735}',
'\u{736}',
'\u{73A}',
'\u{73D}',
'\u{73F}',
'\u{740}',
'\u{741}',
'\u{743}',
'\u{745}',
'\u{747}',
'\u{749}',
'\u{74A}',
'\u{7EB}',
'\u{7EC}',
'\u{7ED}',
'\u{7EE}',
'\u{7EF}',
'\u{7F0}',
'\u{7F1}',
'\u{7F3}',
'\u{816}',
'\u{817}',
'\u{818}',
'\u{819}',
'\u{81B}',
'\u{81C}',
'\u{81D}',
'\u{81E}',
'\u{81F}',
'\u{820}',
'\u{821}',
'\u{822}',
'\u{823}',
'\u{825}',
'\u{826}',
'\u{827}',
'\u{829}',
'\u{82A}',
'\u{82B}',
'\u{82C}',
'\u{82D}',
'\u{951}',
'\u{953}',
'\u{954}',
'\u{F82}',
'\u{F83}',
'\u{F86}',
'\u{F87}',
'\u{135D}',
'\u{135E}',
'\u{135F}',
'\u{17DD}',
'\u{193A}',
'\u{1A17}',
'\u{1A75}',
'\u{1A76}',
'\u{1A77}',
'\u{1A78}',
'\u{1A79}',
'\u{1A7A}',
'\u{1A7B}',
'\u{1A7C}',
'\u{1B6B}',
'\u{1B6D}',
'\u{1B6E}',
'\u{1B6F}',
'\u{1B70}',
'\u{1B71}',
'\u{1B72}',
'\u{1B73}',
'\u{1CD0}',
'\u{1CD1}',
'\u{1CD2}',
'\u{1CDA}',
'\u{1CDB}',
'\u{1CE0}',
'\u{1DC0}',
'\u{1DC1}',
'\u{1DC3}',
'\u{1DC4}',
'\u{1DC5}',
'\u{1DC6}',
'\u{1DC7}',
'\u{1DC8}',
'\u{1DC9}',
'\u{1DCB}',
'\u{1DCC}',
'\u{1DD1}',
'\u{1DD2}',
'\u{1DD3}',
'\u{1DD4}',
'\u{1DD5}',
'\u{1DD6}',
'\u{1DD7}',
'\u{1DD8}',
'\u{1DD9}',
'\u{1DDA}',
'\u{1DDB}',
'\u{1DDC}',
'\u{1DDD}',
'\u{1DDE}',
'\u{1DDF}',
'\u{1DE0}',
'\u{1DE1}',
'\u{1DE2}',
'\u{1DE3}',
'\u{1DE4}',
'\u{1DE5}',
'\u{1DE6}',
'\u{1DFE}',
'\u{20D0}',
'\u{20D1}',
'\u{20D4}',
'\u{20D5}',
'\u{20D6}',
'\u{20D7}',
'\u{20DB}',
'\u{20DC}',
'\u{20E1}',
'\u{20E7}',
'\u{20E9}',
'\u{20F0}',
'\u{2CEF}',
'\u{2CF0}',
'\u{2CF1}',
'\u{2DE0}',
'\u{2DE1}',
'\u{2DE2}',
'\u{2DE3}',
'\u{2DE4}',
'\u{2DE5}',
'\u{2DE6}',
'\u{2DE7}',
'\u{2DE8}',
'\u{2DE9}',
'\u{2DEA}',
'\u{2DEB}',
'\u{2DEC}',
'\u{2DED}',
'\u{2DEE}',
'\u{2DEF}',
'\u{2DF0}',
'\u{2DF1}',
'\u{2DF2}',
'\u{2DF3}',
'\u{2DF4}',
'\u{2DF5}',
'\u{2DF6}',
'\u{2DF7}',
'\u{2DF8}',
'\u{2DF9}',
'\u{2DFA}',
'\u{2DFB}',
'\u{2DFC}',
'\u{2DFD}',
'\u{2DFE}',
'\u{2DFF}',
'\u{A66F}',
'\u{A67C}',
'\u{A67D}',
'\u{A6F0}',
'\u{A6F1}',
'\u{A8E0}',
'\u{A8E1}',
'\u{A8E2}',
'\u{A8E3}',
'\u{A8E4}',
'\u{A8E5}',
'\u{A8E6}',
'\u{A8E7}',
'\u{A8E8}',
'\u{A8E9}',
'\u{A8EA}',
'\u{A8EB}',
'\u{A8EC}',
'\u{A8ED}',
'\u{A8EE}',
'\u{A8EF}',
'\u{A8F0}',
'\u{A8F1}',
'\u{AAB0}',
'\u{AAB2}',
'\u{AAB3}',
'\u{AAB7}',
'\u{AAB8}',
'\u{AABE}',
'\u{AABF}',
'\u{AAC1}',
'\u{FE20}',
'\u{FE21}',
'\u{FE22}',
'\u{FE23}',
'\u{FE24}',
'\u{FE25}',
'\u{FE26}',
'\u{10A0F}',
'\u{10A38}',
'\u{1D185}',
'\u{1D186}',
'\u{1D187}',
'\u{1D188}',
'\u{1D189}',
'\u{1D1AA}',
'\u{1D1AB}',
'\u{1D1AC}',
'\u{1D1AD}',
'\u{1D242}',
'\u{1D243}',
'\u{1D244}',
];
#[inline]
fn diacritic(y: u16) -> char {
*DIACRITICS
.get(usize::from(y))
.unwrap_or_else(|| &DIACRITICS[0])
}
#[cfg(test)]
mod tests {
use super::*;
fn note_msg(length: u32) -> Msg {
let (_, mut msg) = crate::messages::tests::page([1]).remove(0);
msg.file = Some(MediaFile {
id: 5,
label: "Video message".into(),
photo: false,
downloaded: true,
});
msg.video = Some(Note {
file_id: 5,
size: 10,
duration: 1,
length,
});
msg
}
#[test]
fn the_whole_window_plays_frames_no_bigger_than_the_video() {
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
let msg = note_msg(400);
let view = VideoView::of(1, 1, &msg, tx, false, None, &Colors::default()).unwrap();
assert_eq!(view.frame_side((1400, 1380)), 400);
assert_eq!(view.frame_side((300, 320)), 300, "smaller ones shrink");
}
#[test]
fn the_circle_is_drawn_once_on_the_bubbles_color_and_ends_with_the_video() {
let graph = round_graph(Round {
side: 400,
backdrop: [0x31, 0x32, 0x44],
});
assert!(graph.starts_with("scale=400:400,"), "{graph}");
assert!(graph.contains("drawbox=c=0x313244:t=fill"), "{graph}");
assert!(graph.contains("r=1:d=1,"), "one circle: {graph}");
assert!(graph.contains("loop=-1:1"), "laid on every frame: {graph}");
assert!(graph.contains("alphamerge=shortest=1"), "{graph}");
}
#[test]
fn a_senders_file_is_read_only_as_a_local_mp4() {
let path = Path::new("/files/video_notes/file_3.mp4");
let args = ffmpeg_args(path, "0.000", "fps=30");
let input = args.iter().position(|a| a == "-i").unwrap();
let before = &args[..input];
assert!(
before
.windows(2)
.any(|w| w == ["-protocol_whitelist", "file"]),
"{args:?}"
);
assert!(before.windows(2).any(|w| w == ["-f", "mov"]), "{args:?}");
assert_eq!(args[input + 1], "file:/files/video_notes/file_3.mp4");
let mut players = vec![mpv_args(path, "0.000")];
#[cfg(unix)]
players.push(pepper_args(
path,
"0.000",
Round {
side: 400,
backdrop: [0, 0, 0],
},
));
for args in players {
for wanted in [
"--no-config",
"--load-scripts=no",
"--ytdl=no",
"--demuxer=lavf",
"--demuxer-lavf-format=mov",
"--demuxer-lavf-o=protocol_whitelist=file",
] {
assert!(args.iter().any(|a| a == wanted), "{wanted}: {args:?}");
}
assert_eq!(
args[args.len() - 2..],
["--", "/files/video_notes/file_3.mp4"]
);
}
}
#[cfg(unix)]
#[test]
fn pepper_play_cuts_the_picture_round_on_the_bubbles_color() {
let round = Round {
side: 400,
backdrop: [0x31, 0x32, 0x44],
};
let args = pepper_args(Path::new("/f.mp4"), "0.000", round);
assert!(args.iter().any(|a| a == "--hwdec=no"), "{args:?}");
assert!(
args.iter().any(|a| a == "--background-color=#313244"),
"{args:?}"
);
let vf = args
.iter()
.find_map(|a| a.strip_prefix("--vf=lavfi=graph=%"));
let (len, graph) = vf.and_then(|v| v.split_once('%')).unwrap();
assert_eq!(len.parse::<usize>().unwrap(), graph.len());
assert_eq!(graph, round_graph(round));
}
#[test]
fn a_senders_word_on_its_videos_size_is_kept_within_reason() {
let note = |length| {
Note::new(&VideoNote {
duration: 8,
waveform: String::new(),
length,
minithumbnail: None,
thumbnail: None,
speech_recognition_result: None,
video: tdlib_rs::types::File::default(),
})
};
assert_eq!(note(400).length, 400);
assert_eq!(note(60_000).length, MAX_SIDE as u32);
assert_eq!(note(-5).length, 1);
}
#[test]
fn frames_go_into_the_players_own_folder_tagged_with_its_run() {
let dir = crate::config::new_private_dir(&std::env::temp_dir(), "tuigram-test").unwrap();
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let pipe = [255u8; 2 * 2 * 4 * 2];
let stop = AtomicBool::new(false);
pump_frames(
&pipe[..],
(2, 2),
&dir,
&AtomicU64::new(0),
&stop,
(TAG_INLINE, 7),
&tx,
);
let events: Vec<VideoEvent> = std::iter::from_fn(|| rx.try_recv().ok()).collect();
assert!(events.iter().all(|e| e.tag == TAG_INLINE && e.run == 7));
let news: Vec<&News> = events.iter().map(|e| &e.news).collect();
assert!(matches!(news[..], [News::Frame, News::Frame, News::Ended]));
assert_eq!(std::fs::read(dir.join("frame.rgb")).unwrap().len(), 16);
std::fs::remove_dir_all(&dir).unwrap();
}
#[cfg(unix)]
#[test]
fn what_pepper_play_says_is_read_as_decoder_words() {
let lines = [
r#"{"data": null, "request_id": 0, "error": "success"}"#,
r#"{"event": "start-file", "playlist_entry_id": 1}"#,
r#"{"event": "property-change", "id": 1, "name": "time-pos", "data": null}"#,
r#"{"event": "file-loaded"}"#,
r#"{"event": "property-change", "id": 1, "name": "time-pos", "data": 0.5}"#,
r#"{"event": "playback-restart"}"#,
r#"{"event": "property-change", "id": 1, "name": "time-pos", "data": 1.92}"#,
r#"{"event": "property-change", "id": 1, "name": "time-pos", "data": 1.95}"#,
r#"{"event": "playback-restart"}"#,
r#"{"event": "end-file", "reason": "eof", "playlist_entry_id": 1}"#,
];
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let seconds = AtomicU64::new(0);
read_pepper(lines.join("\n").as_bytes(), &seconds, (TAG_VIEW, 3), &tx);
let events: Vec<VideoEvent> = std::iter::from_fn(|| rx.try_recv().ok()).collect();
assert!(events.iter().all(|e| e.tag == TAG_VIEW && e.run == 3));
let news: Vec<&News> = events.iter().map(|e| &e.news).collect();
assert!(
matches!(news[..], [News::Frame, News::Moved, News::Ended]),
"one start, one second"
);
assert_eq!(f64::from_bits(seconds.load(Relaxed)), 1.95);
let failed = r#"{"event": "end-file", "reason": "error", "file_error": "loading failed"}"#;
assert_eq!(said(failed), Some(Said::Failed("loading failed".into())));
read_pepper(&b""[..], &seconds, (TAG_VIEW, 4), &tx);
assert!(matches!(rx.try_recv().unwrap().news, News::Failed(_)));
}
#[cfg(unix)]
#[test]
fn a_video_message_pepper_play_cant_play_plays_as_kitty_frames() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let msg = note_msg(240);
let quits = PathBuf::from("/bin/sh");
let colors = Colors::default();
let mut view = VideoView::of(1, 1, &msg, tx.clone(), false, Some(quits), &colors).unwrap();
let path = Path::new("/nowhere/file_5.mp4");
let run = spawn(path, 0.0, view.round, TAG_VIEW, tx, view.pepper.as_deref()).unwrap();
assert!(run.frames().is_none(), "Pepper's player first");
view.player = Some(run);
let event = rx.blocking_recv().unwrap();
assert!(view.on_event(event).is_ok(), "not an error to show");
assert!(
view.player.is_none() && view.pepper.is_none(),
"kitty frames next"
);
}
}