use channel and jpeg compression to speed up playback
This commit is contained in:
Generated
+26
@@ -635,6 +635,12 @@ dependencies = [
|
||||
"objc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "color_quant"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
|
||||
|
||||
[[package]]
|
||||
name = "combine"
|
||||
version = "4.6.7"
|
||||
@@ -2197,6 +2203,19 @@ dependencies = [
|
||||
"icu_properties",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "image"
|
||||
version = "0.24.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5690139d2f55868e080017335e4b94cb7414274c74f1669c84fb5feba2c9f69d"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"byteorder",
|
||||
"color_quant",
|
||||
"jpeg-decoder",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.9.3"
|
||||
@@ -2337,6 +2356,12 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jpeg-decoder"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00810f1d8b74be64b13dbf3db89ac67740615d6c891f0e7b6179326533011a07"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.77"
|
||||
@@ -2467,6 +2492,7 @@ dependencies = [
|
||||
"cpal",
|
||||
"daw-backend",
|
||||
"ffmpeg-next",
|
||||
"image",
|
||||
"log",
|
||||
"lru",
|
||||
"rtrb",
|
||||
|
||||
@@ -39,6 +39,7 @@ rtrb = "0.3"
|
||||
# Video decoding
|
||||
ffmpeg-next = "7.0"
|
||||
lru = "0.12"
|
||||
image = { version = "0.24", default-features = false, features = ["jpeg"] }
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 1 # Enable basic optimizations in debug mode for audio decoding performance
|
||||
|
||||
+52
-6
@@ -1,8 +1,10 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::num::NonZeroUsize;
|
||||
use std::io::Cursor;
|
||||
use ffmpeg_next as ffmpeg;
|
||||
use lru::LruCache;
|
||||
use daw_backend::WaveformPeak;
|
||||
use image::{RgbaImage, ImageEncoder};
|
||||
|
||||
#[derive(serde::Serialize, Clone)]
|
||||
pub struct VideoFileMetadata {
|
||||
@@ -453,16 +455,16 @@ fn generate_waveform(audio_data: &[f32], channels: u32, target_peaks: usize) ->
|
||||
waveform
|
||||
}
|
||||
|
||||
// Use a custom serializer wrapper for efficient binary transfer
|
||||
#[derive(serde::Serialize)]
|
||||
struct BinaryFrame(#[serde(with = "serde_bytes")] Vec<u8>);
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn video_get_frame(
|
||||
state: tauri::State<'_, Arc<Mutex<VideoState>>>,
|
||||
pool_index: usize,
|
||||
timestamp: f64,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
use_jpeg: bool,
|
||||
channel: tauri::ipc::Channel,
|
||||
) -> Result<(), String> {
|
||||
use std::time::Instant;
|
||||
|
||||
let video_state = state.lock().unwrap();
|
||||
|
||||
let decoder = video_state.pool.get(pool_index)
|
||||
@@ -472,7 +474,51 @@ pub async fn video_get_frame(
|
||||
drop(video_state);
|
||||
|
||||
let mut decoder = decoder.lock().unwrap();
|
||||
decoder.get_frame(timestamp)
|
||||
let frame_data = decoder.get_frame(timestamp)?;
|
||||
|
||||
let data_to_send = if use_jpeg {
|
||||
let t_compress_start = Instant::now();
|
||||
|
||||
// Get frame dimensions from decoder
|
||||
let width = decoder.output_width;
|
||||
let height = decoder.output_height;
|
||||
|
||||
// Create image from raw RGBA data
|
||||
let img = RgbaImage::from_raw(width, height, frame_data)
|
||||
.ok_or("Failed to create image from frame data")?;
|
||||
|
||||
// Convert RGBA to RGB (JPEG doesn't support alpha)
|
||||
let rgb_img = image::DynamicImage::ImageRgba8(img).to_rgb8();
|
||||
|
||||
// Encode to JPEG with quality 85 (good balance of size/quality)
|
||||
let mut jpeg_data = Vec::new();
|
||||
let mut encoder = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg_data, 85);
|
||||
encoder.encode(
|
||||
rgb_img.as_raw(),
|
||||
rgb_img.width(),
|
||||
rgb_img.height(),
|
||||
image::ColorType::Rgb8
|
||||
).map_err(|e| format!("JPEG encoding failed: {}", e))?;
|
||||
|
||||
let compress_time = t_compress_start.elapsed().as_millis();
|
||||
let original_size = width as usize * height as usize * 4;
|
||||
let compressed_size = jpeg_data.len();
|
||||
let ratio = original_size as f32 / compressed_size as f32;
|
||||
|
||||
eprintln!("[Video JPEG] Compressed {}KB -> {}KB ({}x) in {}ms",
|
||||
original_size / 1024, compressed_size / 1024, ratio, compress_time);
|
||||
|
||||
jpeg_data
|
||||
} else {
|
||||
frame_data
|
||||
};
|
||||
|
||||
// Send binary data through channel (bypasses JSON serialization)
|
||||
// InvokeResponseBody::Raw sends raw binary data without JSON encoding
|
||||
channel.send(tauri::ipc::InvokeResponseBody::Raw(data_to_send))
|
||||
.map_err(|e| format!("Channel send error: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
|
||||
Reference in New Issue
Block a user