Scale and convert share frames on the gpu
Browse files at this commitd9eb9638ea95729617a41736d546d1e49e6428d8Parent: c6ed976- perf
- scale and convert share frames on the gpu
9 changed files +104 -75
- crates/client/Cargo.toml +16 -16
- crates/client/src/gui/chat.rs +1 -1
- crates/client/src/gui/share_view.rs +25 -29
- crates/client/src/gui/voice_stats.rs +1 -4
- crates/client/src/network.rs +2 -2
- crates/client/src/share/encode.rs +35 -17
- crates/client/src/share/fake.rs +12 -2
- crates/client/src/share/mod.rs +2 -2
- crates/client/src/share/session.rs +10 -2
crates/client/Cargo.tomlmodified+16 -16
| @@ -10,22 +10,22 @@ tokio.workspace = true | ||
| 10 | 10 | quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] } |
| 11 | 11 | |
| 12 | 12 | gpui-kit = { version = "0.11.0", features = ["syntect"] } |
| 13 | -ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 14 | -ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 15 | -ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 16 | -ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 17 | -ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 18 | -ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 19 | -ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 20 | -ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 21 | -ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 22 | -ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 23 | -ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 24 | -ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 25 | -ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 26 | -ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 27 | -ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 28 | -ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } | |
| 13 | +ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 14 | +ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 15 | +ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 16 | +ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 17 | +ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 18 | +ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 19 | +ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 20 | +ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 21 | +ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 22 | +ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 23 | +ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 24 | +ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 25 | +ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 26 | +ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 27 | +ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 28 | +ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } | |
| 29 | 29 | ureq = "3.5.2" |
| 30 | 30 | bytes = "1.12.2" |
| 31 | 31 | tracing = "0.1.44" |
crates/client/src/gui/chat.rsmodified+1 -1
| @@ -8,7 +8,6 @@ use std::{ | ||
| 8 | 8 | |
| 9 | 9 | use crate::gui::social::InviteLookup; |
| 10 | 10 | use crate::gui::voice_stats::{SAMPLE_HISTORY, StatsTab}; |
| 11 | -use crate::share::session::ShareSample; | |
| 12 | 11 | use crate::gui::{ |
| 13 | 12 | FIND_HINT, Palette, Tooltip as _, Yumush, avatar, badge, |
| 14 | 13 | builder::{BuilderTab, Transfer}, |
| @@ -26,6 +25,7 @@ use crate::gui::{ | ||
| 26 | 25 | snippet, |
| 27 | 26 | voice_note::{self, VoiceNote}, |
| 28 | 27 | }; |
| 28 | +use crate::share::session::ShareSample; | |
| 29 | 29 | use common::{ |
| 30 | 30 | constant::MESSAGE_PAGE_LIMIT, |
| 31 | 31 | event::Event, |
crates/client/src/gui/share_view.rsmodified+25 -29
| @@ -3,8 +3,8 @@ use std::{sync::Arc, time::Duration}; | ||
| 3 | 3 | use common::share::{Codec, Fps, Quality, Resolution, Source, options}; |
| 4 | 4 | use gpui_kit::{ |
| 5 | 5 | Anchor, AnyElement, AppContext, Context, Div, Entity, FontWeight, InteractiveElement, |
| 6 | - IntoElement, MouseButton, ObjectFit, ParentElement, RenderImage, Styled, StyledImage, | |
| 7 | - StatefulInteractiveElement, Subscription, Task, Window, | |
| 6 | + IntoElement, MouseButton, ObjectFit, ParentElement, RenderImage, StatefulInteractiveElement, | |
| 7 | + Styled, StyledImage, Subscription, Task, Window, | |
| 8 | 8 | assets::IconName, |
| 9 | 9 | component::{ |
| 10 | 10 | Icon, |
| @@ -636,11 +636,10 @@ fn audio_list(entity: &Entity<Yumush>, yumush: &Yumush, palette: Palette) -> Opt | ||
| 636 | 636 | .filter(|app| app.kind == AudioKind::App) |
| 637 | 637 | .collect(); |
| 638 | 638 | (!apps.is_empty()).then(|| { |
| 639 | - let rows = div() | |
| 640 | - .flex() | |
| 641 | - .flex_col() | |
| 642 | - .gap(px(2.)) | |
| 643 | - .children(apps.into_iter().map(|app| exclude_row(entity, settings, app))); | |
| 639 | + let rows = div().flex().flex_col().gap(px(2.)).children( | |
| 640 | + apps.into_iter() | |
| 641 | + .map(|app| exclude_row(entity, settings, app)), | |
| 642 | + ); | |
| 644 | 643 | field("Exclude", app_list(rows), palette).into_any_element() |
| 645 | 644 | }) |
| 646 | 645 | } |
| @@ -664,12 +663,7 @@ fn app_list(rows: impl IntoElement) -> impl IntoElement { | ||
| 664 | 663 | .child(rows) |
| 665 | 664 | } |
| 666 | 665 | |
| 667 | -fn pick_list( | |
| 668 | - entity: &Entity<Yumush>, | |
| 669 | - selected: &str, | |
| 670 | - apps: &[AudioApp], | |
| 671 | - palette: Palette, | |
| 672 | -) -> Div { | |
| 666 | +fn pick_list(entity: &Entity<Yumush>, selected: &str, apps: &[AudioApp], palette: Palette) -> Div { | |
| 673 | 667 | let groups = [ |
| 674 | 668 | (AudioKind::App, "Apps"), |
| 675 | 669 | (AudioKind::Output, "Outputs"), |
| @@ -732,26 +726,28 @@ fn name_cell(name: String) -> Div { | ||
| 732 | 726 | div().flex_1().min_w_0().truncate().child(name) |
| 733 | 727 | } |
| 734 | 728 | |
| 735 | -fn exclude_row(entity: &Entity<Yumush>, settings: &ShareSettings, app: &AudioApp) -> impl IntoElement { | |
| 729 | +fn exclude_row( | |
| 730 | + entity: &Entity<Yumush>, | |
| 731 | + settings: &ShareSettings, | |
| 732 | + app: &AudioApp, | |
| 733 | +) -> impl IntoElement { | |
| 736 | 734 | let entity = entity.clone(); |
| 737 | 735 | let name = app.name.clone(); |
| 738 | 736 | let included = !settings.excluded.contains(&app.name); |
| 739 | 737 | |
| 740 | - row_style(div()) | |
| 741 | - .child(name_cell(app.name.clone())) | |
| 742 | - .child( | |
| 743 | - div().flex_shrink_0().child( | |
| 744 | - Switch::new(format!("share_app_{}", app.node)) | |
| 745 | - .checked(included) | |
| 746 | - .on_change(move |checked, _, cx| { | |
| 747 | - let name = name.clone(); | |
| 748 | - let included = *checked; | |
| 749 | - entity.update(cx, |yumush, cx| { | |
| 750 | - yumush.set_share_excluded(name, !included, cx) | |
| 751 | - }); | |
| 752 | - }), | |
| 753 | - ), | |
| 754 | - ) | |
| 738 | + row_style(div()).child(name_cell(app.name.clone())).child( | |
| 739 | + div().flex_shrink_0().child( | |
| 740 | + Switch::new(format!("share_app_{}", app.node)) | |
| 741 | + .checked(included) | |
| 742 | + .on_change(move |checked, _, cx| { | |
| 743 | + let name = name.clone(); | |
| 744 | + let included = *checked; | |
| 745 | + entity.update(cx, |yumush, cx| { | |
| 746 | + yumush.set_share_excluded(name, !included, cx) | |
| 747 | + }); | |
| 748 | + }), | |
| 749 | + ), | |
| 750 | + ) | |
| 755 | 751 | } |
| 756 | 752 | |
| 757 | 753 | fn pick_row( |
crates/client/src/gui/voice_stats.rsmodified+1 -4
| @@ -214,10 +214,7 @@ fn voice_body(samples: &VecDeque<VoiceSample>, palette: Palette) -> Div { | ||
| 214 | 214 | fn sharing_body(layers: &[LayerStats], palette: Palette) -> Div { |
| 215 | 215 | let upload: u32 = layers.iter().map(|layer| layer.kbps).sum(); |
| 216 | 216 | column() |
| 217 | - .child(tile_grid( | |
| 218 | - [("Upload", format!("{upload} kbps"))], | |
| 219 | - palette, | |
| 220 | - )) | |
| 217 | + .child(tile_grid([("Upload", format!("{upload} kbps"))], palette)) | |
| 221 | 218 | .children(layers.iter().map(|stat| { |
| 222 | 219 | row_line( |
| 223 | 220 | describe(Some(stat.layer), Some(stat.engine)), |
crates/client/src/network.rsmodified+2 -2
| @@ -1957,8 +1957,8 @@ async fn start_share( | ||
| 1957 | 1957 | settings.encode(), |
| 1958 | 1958 | settings.audio_mode(), |
| 1959 | 1959 | ) |
| 1960 | - .await | |
| 1961 | - .map_err(Error::Network)?; | |
| 1960 | + .await | |
| 1961 | + .map_err(Error::Network)?; | |
| 1962 | 1962 | { |
| 1963 | 1963 | let mut share = share.lock().unwrap(); |
| 1964 | 1964 | share.sharing = Some(sharing); |
crates/client/src/share/encode.rsmodified+35 -17
| @@ -6,7 +6,7 @@ use std::thread; | ||
| 6 | 6 | |
| 7 | 7 | use common::share::{Codec, Layer}; |
| 8 | 8 | |
| 9 | -use super::convert::{self, I420}; | |
| 9 | +use super::convert; | |
| 10 | 10 | use super::frame::{Encoded, Frame}; |
| 11 | 11 | use crate::media::decode::{Engine, VideoDecoder}; |
| 12 | 12 | |
| @@ -106,10 +106,16 @@ impl Backend { | ||
| 106 | 106 | } |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - fn encode(&mut self, planes: I420, timestamp_ms: u32, force: bool) -> Option<(Vec<u8>, bool)> { | |
| 109 | + fn encode( | |
| 110 | + &mut self, | |
| 111 | + frame: &Frame, | |
| 112 | + settings: &Settings, | |
| 113 | + force: bool, | |
| 114 | + ) -> Option<(Vec<u8>, bool)> { | |
| 110 | 115 | match self { |
| 111 | - Self::Hardware(encoder) => encoder.encode(planes, timestamp_ms, force), | |
| 116 | + Self::Hardware(encoder) => encoder.encode(frame, settings, force), | |
| 112 | 117 | Self::H264(encoder) => { |
| 118 | + let planes = convert::bgra_to_i420(frame, settings.width, settings.height); | |
| 113 | 119 | let picture = ec_h264::PictureView::i420( |
| 114 | 120 | planes.width, |
| 115 | 121 | planes.height, |
| @@ -134,29 +140,42 @@ mod hardware { | ||
| 134 | 140 | use common::share::Codec; |
| 135 | 141 | |
| 136 | 142 | use super::{Settings, report}; |
| 137 | - use crate::share::convert::I420; | |
| 143 | + use crate::share::{convert, frame::Frame}; | |
| 138 | 144 | |
| 139 | 145 | pub struct Encoder(ec_hw::HwEncoder); |
| 140 | 146 | |
| 141 | 147 | impl Encoder { |
| 142 | 148 | pub fn encode( |
| 143 | 149 | &mut self, |
| 144 | - planes: I420, | |
| 145 | - timestamp_ms: u32, | |
| 150 | + frame: &Frame, | |
| 151 | + settings: &Settings, | |
| 146 | 152 | force: bool, |
| 147 | 153 | ) -> Option<(Vec<u8>, bool)> { |
| 148 | - let frame = ec_hw::I420 { | |
| 149 | - y: planes.y, | |
| 150 | - u: planes.u, | |
| 151 | - v: planes.v, | |
| 152 | - width: planes.width, | |
| 153 | - height: planes.height, | |
| 154 | - }; | |
| 155 | 154 | let meta = ec_hw::FrameMetadata { |
| 156 | - timestamp: i64::from(timestamp_ms), | |
| 155 | + timestamp: i64::from(frame.timestamp_ms), | |
| 157 | 156 | force_keyframe: force, |
| 158 | 157 | }; |
| 159 | - report(self.0.encode(&frame, meta)).map(|coded| (coded.data, coded.is_keyframe)) | |
| 158 | + let coded = if frame.width % 2 == 0 && frame.height % 2 == 0 && frame.stride % 4 == 0 { | |
| 159 | + let rgb = ec_hw::RgbFrame { | |
| 160 | + data: &frame.bgra, | |
| 161 | + width: frame.width, | |
| 162 | + height: frame.height, | |
| 163 | + stride: frame.stride, | |
| 164 | + order: ec_hw::RgbOrder::Bgra, | |
| 165 | + }; | |
| 166 | + self.0.encode_rgb(&rgb, meta) | |
| 167 | + } else { | |
| 168 | + let planes = convert::bgra_to_i420(frame, settings.width, settings.height); | |
| 169 | + let picture = ec_hw::I420 { | |
| 170 | + y: planes.y, | |
| 171 | + u: planes.u, | |
| 172 | + v: planes.v, | |
| 173 | + width: planes.width, | |
| 174 | + height: planes.height, | |
| 175 | + }; | |
| 176 | + self.0.encode(&picture, meta) | |
| 177 | + }; | |
| 178 | + report(coded).map(|coded| (coded.data, coded.is_keyframe)) | |
| 160 | 179 | } |
| 161 | 180 | } |
| 162 | 181 | |
| @@ -222,8 +241,7 @@ fn run( | ||
| 222 | 241 | } |
| 223 | 242 | last = Some(frame.timestamp_ms); |
| 224 | 243 | let force = request.swap(false, Ordering::Relaxed); |
| 225 | - let planes = convert::bgra_to_i420(&frame, settings.width, settings.height); | |
| 226 | - let Some((data, keyframe)) = backend.encode(planes, frame.timestamp_ms, force) else { | |
| 244 | + let Some((data, keyframe)) = backend.encode(&frame, &settings, force) else { | |
| 227 | 245 | continue; |
| 228 | 246 | }; |
| 229 | 247 | let encoded = Encoded { |
crates/client/src/share/fake.rsmodified+12 -2
| @@ -52,7 +52,11 @@ pub fn start(spec: &str) -> (u32, u32, u32, Receiver<Frame>, Generator) { | ||
| 52 | 52 | } |
| 53 | 53 | if y < 16 { |
| 54 | 54 | for x in 0..(32 * 16).min(width as usize) { |
| 55 | - let value = if (number >> (x / 16)) & 1 == 1 { 255 } else { 0 }; | |
| 55 | + let value = if (number >> (x / 16)) & 1 == 1 { | |
| 56 | + 255 | |
| 57 | + } else { | |
| 58 | + 0 | |
| 59 | + }; | |
| 56 | 60 | bgra[(row + x) * BYTES_PER_PIXEL..][..3].fill(value); |
| 57 | 61 | } |
| 58 | 62 | } |
| @@ -72,5 +76,11 @@ pub fn start(spec: &str) -> (u32, u32, u32, Receiver<Frame>, Generator) { | ||
| 72 | 76 | }) |
| 73 | 77 | .ok() |
| 74 | 78 | }; |
| 75 | - (width, height, refresh, frames_rx, Generator { stop, thread }) | |
| 79 | + ( | |
| 80 | + width, | |
| 81 | + height, | |
| 82 | + refresh, | |
| 83 | + frames_rx, | |
| 84 | + Generator { stop, thread }, | |
| 85 | + ) | |
| 76 | 86 | } |
crates/client/src/share/mod.rsmodified+2 -2
| @@ -9,10 +9,10 @@ pub mod capture; | ||
| 9 | 9 | #[cfg(target_os = "windows")] |
| 10 | 10 | #[path = "capture_windows.rs"] |
| 11 | 11 | pub mod capture; |
| 12 | -#[cfg(any(target_os = "linux", target_os = "windows"))] | |
| 13 | -pub mod fake; | |
| 14 | 12 | pub mod convert; |
| 15 | 13 | pub mod encode; |
| 14 | +#[cfg(any(target_os = "linux", target_os = "windows"))] | |
| 15 | +pub mod fake; | |
| 16 | 16 | pub mod frame; |
| 17 | 17 | pub mod reassembly; |
| 18 | 18 | pub mod session; |
crates/client/src/share/session.rsmodified+10 -2
| @@ -824,7 +824,12 @@ mod tests { | ||
| 824 | 824 | } |
| 825 | 825 | assert_eq!(pictures.len(), 10, "{codec:?} {:?}", decoder.engine()); |
| 826 | 826 | let last = pictures.pop().expect("picture"); |
| 827 | - assert_eq!((last.width, last.height), (width, height), "{codec:?} {:?}", decoder.engine()); | |
| 827 | + assert_eq!( | |
| 828 | + (last.width, last.height), | |
| 829 | + (width, height), | |
| 830 | + "{codec:?} {:?}", | |
| 831 | + decoder.engine() | |
| 832 | + ); | |
| 828 | 833 | (0..height as usize) |
| 829 | 834 | .flat_map(|row| last.y[row * last.y_stride..][..width as usize].to_vec()) |
| 830 | 835 | .collect() |
| @@ -836,7 +841,10 @@ mod tests { | ||
| 836 | 841 | .map(|(a, b)| u64::from(a.abs_diff(*b))) |
| 837 | 842 | .sum::<u64>() |
| 838 | 843 | / lumas[0].len() as u64; |
| 839 | - assert!(difference < 4, "{codec:?} hardware vs software luma differs by {difference}"); | |
| 844 | + assert!( | |
| 845 | + difference < 4, | |
| 846 | + "{codec:?} hardware vs software luma differs by {difference}" | |
| 847 | + ); | |
| 840 | 848 | } |
| 841 | 849 | } |
| 842 | 850 | } |