switched to jpeg from h264 compression
10x larger over the wire but no dependency on libstdc++
This commit is contained in:
parent
2dab4515fb
commit
f5bc322862
3 changed files with 46 additions and 56 deletions
1
encoder/Cargo.lock
generated
1
encoder/Cargo.lock
generated
|
|
@ -423,6 +423,7 @@ dependencies = [
|
|||
"nokhwa",
|
||||
"openh264",
|
||||
"tokio",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
|||
|
|
@ -9,3 +9,4 @@ image = "0.25.5"
|
|||
nokhwa = { version = "0.10.7", features = ["input-native"] }
|
||||
openh264 = "0.6.6"
|
||||
tokio = { version = "1.42.0", features = ["full"] }
|
||||
zune-jpeg = "0.4.14"
|
||||
|
|
|
|||
|
|
@ -1,14 +1,13 @@
|
|||
use std::{net::SocketAddr, result, sync::Arc, thread::{self, sleep}, time::Duration};
|
||||
use std::{array::from_ref, net::SocketAddr, result, sync::Arc, thread::{self, sleep}, time::Duration};
|
||||
|
||||
use image::{ImageBuffer, Rgb};
|
||||
use image::{codecs::jpeg::JpegEncoder, ImageBuffer, Rgb};
|
||||
use nokhwa::{pixel_format::RgbFormat, utils::{ApiBackend, RequestedFormat, RequestedFormatType, Resolution}, Camera};
|
||||
use anyhow::{Context, Ok, Result};
|
||||
use openh264::{encoder::{Encoder, EncoderConfig, UsageType}, formats::{RgbSliceU8, YUVBuffer}, nal_units, OpenH264API};
|
||||
use tokio::{io::AsyncWriteExt, net::{TcpListener, TcpStream}, runtime::Runtime, sync::{Notify, RwLock}, task::LocalSet};
|
||||
|
||||
fn main() -> Result<()>{
|
||||
let await_frame = Arc::new(Notify::new());
|
||||
let latest_frame = Arc::new(RwLock::new(YUVBuffer::new(0, 0)));
|
||||
let latest_frame = Arc::new(RwLock::new(Vec::new()));
|
||||
|
||||
{
|
||||
let runtime = Runtime::new()?;
|
||||
|
|
@ -34,7 +33,7 @@ fn main() -> Result<()>{
|
|||
loop {}
|
||||
}
|
||||
|
||||
async fn camera_manager(await_frame: Arc<Notify>, latest_frame: Arc<RwLock<YUVBuffer>>) -> Result<()>{
|
||||
async fn camera_manager(await_frame: Arc<Notify>, latest_frame: Arc<RwLock<Vec<u8>>>) -> Result<()>{
|
||||
let cameras = nokhwa::query(ApiBackend::Auto)?;
|
||||
|
||||
let camera = cameras.get(0).context("no cameras")?;
|
||||
|
|
@ -47,41 +46,34 @@ async fn camera_manager(await_frame: Arc<Notify>, latest_frame: Arc<RwLock<YUVBu
|
|||
|
||||
loop {
|
||||
let frame = camera.frame()?;
|
||||
let resolution = frame.resolution();
|
||||
let frame: ImageBuffer<Rgb<u8>, Vec<u8>> = frame.decode_image::<RgbFormat>()?;
|
||||
let buf = RgbSliceU8::new(frame.as_raw(), (resolution.width_x as usize, resolution.height_y as usize));
|
||||
let yuv = YUVBuffer::from_rgb8_source(buf);
|
||||
|
||||
*latest_frame.write().await = yuv;
|
||||
let mut output = Vec::new();
|
||||
|
||||
let mut encoder = JpegEncoder::new_with_quality(&mut output, 30);
|
||||
|
||||
encoder.encode_image(&frame).unwrap();
|
||||
|
||||
*latest_frame.write().await = output;
|
||||
await_frame.notify_waiters();
|
||||
}
|
||||
}
|
||||
|
||||
async fn stream_video(await_frame: Arc<Notify>, latest_frame: Arc<RwLock<YUVBuffer>>, mut client: TcpStream) -> Result<()>{
|
||||
|
||||
let mut encoder = Encoder::with_api_config(
|
||||
OpenH264API::from_source(),
|
||||
EncoderConfig::new()
|
||||
.usage_type(UsageType::CameraVideoRealTime
|
||||
))?;
|
||||
|
||||
|
||||
async fn stream_video(await_frame: Arc<Notify>, latest_frame: Arc<RwLock<Vec<u8>>>, mut client: TcpStream) -> Result<()>{
|
||||
loop {
|
||||
await_frame.notified().await;
|
||||
let data = encoder.encode(&*latest_frame.read().await)?;
|
||||
let data = data.to_vec();
|
||||
let data = latest_frame.read().await;
|
||||
println!("len: {}", data.len());
|
||||
|
||||
let len = data.len();
|
||||
|
||||
let data2 = data.clone();
|
||||
drop(data);
|
||||
client.write(&data2).await?;
|
||||
// holding data across await, likely nonissue
|
||||
client.write_u32(len as u32).await?;
|
||||
client.write(&data).await?;
|
||||
}
|
||||
}
|
||||
|
||||
async fn server(await_frame: Arc<Notify>, latest_frame: Arc<RwLock<YUVBuffer>>) -> Result<()> {
|
||||
async fn server(await_frame: Arc<Notify>, latest_frame: Arc<RwLock<Vec<u8>>>) -> Result<()> {
|
||||
let port = 2993;
|
||||
let listener = TcpListener::bind(format!("0.0.0.0:{port}")).await?;
|
||||
println!("listening on {port}");
|
||||
|
|
|
|||
Loading…
Reference in a new issue