Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try new egui-video player #924

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 226 additions & 42 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions gossip-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ default-run = "gossip"
edition = "2021"

[features]
default = [ "rustls-tls-native" ]
default = [ "rustls-tls-native", "video-ffmpeg" ]
lang-cjk = [ "gossip-lib/lang-cjk" ]
video-ffmpeg = [ "egui-video", "sdl2" ]
video-ffmpeg = [ "egui-video" ]
native-tls = [ "gossip-lib/native-tls" ]
rustls-tls = [ "gossip-lib/rustls-tls" ]
rustls-tls-native = [ "gossip-lib/rustls-tls-native" ]
Expand All @@ -26,7 +26,7 @@ egui-winit = { git = "https://github.com/bu5hm4nn/egui", rev = "f40370c48ae2e07d
egui_extras = { git = "https://github.com/bu5hm4nn/egui", rev = "f40370c48ae2e07d2bc1d7ec33d094d29dc34e70", features = [ "syntect" ] }
egui-file-dialog = { git = "https://github.com/mikedilger/egui-file-dialog", rev = "537561cdc805c97d480fde82f6a647abc6eee1af" }
## egui = { git = "https://github.com/bu5hm4nn/egui", rev = "f40370c48ae2e07d2bc1d7ec33d094d29dc34e70", features = [ "deadlock_detection" ] }
egui-video = { git = "https://github.com/mikedilger/egui-video", rev = "8ff342e20a3806bfa6c8dd7a9cd44305210695c7", features = [ "from_bytes" ], optional = true }
egui-video = { git = "https://github.com/v0l/egui-video.git", rev = "cf880dd14b1451e08f2cd3877d27b4914ad6e6ac", optional = true }
gossip-lib = { path = "../gossip-lib" }
humansize = "2.1"
image = { version = "0.25", features = [ "png", "jpeg" ] }
Expand All @@ -37,7 +37,6 @@ paste = "1.0"
qrcode = "0.14"
resvg = "0.35.0"
rpassword = "7.3"
sdl2 = { version = "0.37", features = ["bundled"], optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
time = { version = "0.3", features = [ "formatting", "macros" ] }
Expand Down
8 changes: 4 additions & 4 deletions gossip-bin/src/notedata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ impl NoteData {
_ => Person::new(author_pubkey),
};

let lists = match GLOBALS.db().read_person_lists(&author_pubkey) {
Ok(lists) => lists,
_ => HashMap::new(),
};
let lists = GLOBALS
.db()
.read_person_lists(&author_pubkey)
.unwrap_or_default();

let seen_on = GLOBALS
.db()
Expand Down
19 changes: 4 additions & 15 deletions gossip-bin/src/ui/feed/note/content/media.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::ui::GossipUi;
use eframe::egui::vec2;
use eframe::{egui, epaint};
use egui::{Image, Response, RichText, Ui};
use egui_video::PlayerControls;
use epaint::Vec2;
use gossip_lib::{MediaLoadingResult, GLOBALS};
use nostr_types::{FileMetadata, Url};
Expand Down Expand Up @@ -292,22 +294,9 @@ fn try_render_video(
}
MediaLoadingResult::Ready(player_ref) => {
if let Ok(mut player) = player_ref.try_borrow_mut() {
let size = media_scale(
show_full_width,
ui,
Vec2 {
x: player.width as f32,
y: player.height as f32,
},
);
let size = media_scale(show_full_width, ui, vec2(1280.0, 720.0));

// show the player
if !show_full_width {
player.stop();
}
let response = player.ui(ui, [size.x, size.y]);

// stop the player when it scrolls out of view
let response = ui.allocate_ui(size, |ui| player.render(ui)).response;
if !ui.is_rect_visible(response.rect) {
player.stop();
}
Expand Down
3 changes: 2 additions & 1 deletion gossip-bin/src/ui/feed/note/content/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ pub(super) fn render_hyperlink(
if let Some(mimetype) = gossip_lib::media_url_mimetype(url.path()) {
if mimetype.starts_with("image/") {
media::show_image(app, ui, nurl, privacy_issue, note.volatile, file_metadata);
} else if mimetype.starts_with("video/") {
} else if mimetype.starts_with("video/") || mimetype == "application/vnd.apple.mpegurl"
{
media::show_video(app, ui, nurl, privacy_issue, note.volatile, file_metadata);
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions gossip-bin/src/ui/feed/note/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ pub fn render_dm_note(app: &mut GossipUi, ui: &mut Ui, feed_note_params: FeedNot

if let Some(note_ref) = app.notecache.try_update_and_get(&id) {
if let Ok(note_data) = note_ref.try_borrow() {
let viewed = match GLOBALS.db().is_event_viewed(note_data.event.id) {
Ok(answer) => answer,
_ => false,
};
let viewed = GLOBALS
.db()
.is_event_viewed(note_data.event.id)
.unwrap_or_default();

if let Ok(mut note_data) = note_ref.try_borrow_mut() {
note_data.repost = Some(RepostType::GenericRepost);
Expand Down
2 changes: 1 addition & 1 deletion gossip-bin/src/ui/feed/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ fn do_replacements(draft: &str, replacements: &HashMap<String, ContentSegment>)
fn offer_attachment(app: &mut GossipUi, ctx: &Context, ui: &mut Ui, dm: bool) {
// Skip if no blossom servers configured:
let blossom_servers = GLOBALS.db().read_setting_blossom_servers();
if blossom_servers.split_whitespace().next() == None {
if blossom_servers.split_whitespace().next().is_none() {
return;
}

Expand Down
104 changes: 11 additions & 93 deletions gossip-bin/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use egui::{
};
use egui_file_dialog::FileDialog;
#[cfg(feature = "video-ffmpeg")]
use egui_video::{AudioDevice, Player};
use egui_video::Player;
use egui_winit::egui::Rect;
use egui_winit::egui::Response;
use egui_winit::egui::ViewportBuilder;
Expand Down Expand Up @@ -414,14 +414,8 @@ impl DraftData {
}

struct GossipUi {
#[cfg(feature = "video-ffmpeg")]
audio_device: Option<AudioDevice>,
#[cfg(feature = "video-ffmpeg")]
video_players: HashMap<Url, Rc<RefCell<egui_video::Player>>>,
// dismissed libsdl2 warning
#[cfg(feature = "video-ffmpeg")]
warn_no_libsdl2_dismissed: bool,

initializing: bool,

// Rendering
Expand Down Expand Up @@ -631,19 +625,6 @@ impl GossipUi {
)
};

#[cfg(feature = "video-ffmpeg")]
let audio_device = {
let mut device = None;
if let Ok(init) = sdl2::init() {
if let Ok(audio) = init.audio() {
if let Ok(dev) = egui_video::init_audio_device(&audio) {
device = Some(dev);
}
}
}
device
};

// start with a fallback DPI here, unless we are overriding anyways
// we won't know the native DPI until the `Viewport` has been created
let (override_dpi, override_dpi_value): (bool, u32) = match read_setting!(override_dpi) {
Expand Down Expand Up @@ -706,12 +687,8 @@ impl GossipUi {
}

GossipUi {
#[cfg(feature = "video-ffmpeg")]
audio_device,
#[cfg(feature = "video-ffmpeg")]
video_players: HashMap::new(),
#[cfg(feature = "video-ffmpeg")]
warn_no_libsdl2_dismissed: false,
initializing: true,
next_frame: Instant::now(),
override_dpi,
Expand Down Expand Up @@ -1706,38 +1683,10 @@ impl GossipUi {
return MediaLoadingResult::Ready(player.to_owned());
}

match GLOBALS.media.get_data(&url, volatile, file_metadata) {
MediaLoadingResult::Disabled => MediaLoadingResult::Disabled,
MediaLoadingResult::Loading => MediaLoadingResult::Loading,
MediaLoadingResult::Ready(bytes) => {
if let Ok(player) = Player::new_from_bytes(ctx, &bytes) {
if let Some(audio) = &mut self.audio_device {
if let Ok(player) = player.with_audio(audio) {
let player_ref = Rc::new(RefCell::new(player));
self.video_players.insert(url.clone(), player_ref.clone());
MediaLoadingResult::Ready(player_ref)
} else {
let failure = "Player setup with audio failed".to_owned();
GLOBALS
.media
.set_has_failed(&url.to_unchecked_url(), failure.clone());
MediaLoadingResult::Failed(failure)
}
} else {
let player_ref = Rc::new(RefCell::new(player));
self.video_players.insert(url.clone(), player_ref.clone());
MediaLoadingResult::Ready(player_ref)
}
} else {
let failure = "Player setup failed".to_owned();
GLOBALS
.media
.set_has_failed(&url.to_unchecked_url(), failure.clone());
MediaLoadingResult::Failed(failure)
}
}
MediaLoadingResult::Failed(s) => MediaLoadingResult::Failed(s),
}
let player = Player::new(ctx, &url.to_string());
let player_ref = Rc::new(RefCell::new(player));
self.video_players.insert(url.clone(), player_ref.clone());
MediaLoadingResult::Ready(player_ref)
}

pub fn show_qr(&mut self, ui: &mut Ui, key: &str) {
Expand Down Expand Up @@ -2236,17 +2185,6 @@ impl eframe::App for GossipUi {
(false, false)
};

let has_warning = {
#[cfg(feature = "video-ffmpeg")]
{
!self.warn_no_libsdl2_dismissed && self.audio_device.is_none()
}
#[cfg(not(feature = "video-ffmpeg"))]
{
false
}
};

egui::TopBottomPanel::top("top-panel")
.frame(
egui::Frame::side_top_panel(&self.theme.get_style()).inner_margin(egui::Margin {
Expand All @@ -2257,32 +2195,12 @@ impl eframe::App for GossipUi {
}),
)
.resizable(true)
.show_animated(
ctx,
show_top_post_area || has_warning,
|ui| {
self.begin_ui(ui);
#[cfg(feature = "video-ffmpeg")]
{
if has_warning {
widgets::warning_frame(ui, self, |ui, app| {
ui.label("You have compiled gossip with 'video-ffmpeg' option but no audio device was found on your system. Make sure you have followed the instructions at ");
ui.hyperlink("https://github.com/Rust-SDL2/rust-sdl2");
ui.label("and installed 'libsdl2-dev' package for your system.");
ui.end_row();
ui.with_layout(egui::Layout::right_to_left(egui::Align::default()), |ui| {
if ui.link("Dismiss message").clicked() {
app.warn_no_libsdl2_dismissed = true;
}
});
});
}
}
if show_top_post_area {
feed::post::posting_area(self, ctx, frame, ui);
}
},
);
.show_animated(ctx, show_top_post_area, |ui| {
self.begin_ui(ui);
if show_top_post_area {
feed::post::posting_area(self, ctx, frame, ui);
}
});

let resizable = true;

Expand Down
2 changes: 1 addition & 1 deletion gossip-bin/src/ui/relays/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr

widgets::page_header(
ui,
&format!(
format!(
"{} ({} relays)",
Page::RelaysActivityMonitor.name(),
relays.len()
Expand Down
2 changes: 1 addition & 1 deletion gossip-bin/src/ui/relays/known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr

widgets::page_header(
ui,
&format!(
format!(
"{} ({} relays)",
Page::RelaysKnownNetwork(None).name(),
relays.len()
Expand Down
2 changes: 1 addition & 1 deletion gossip-bin/src/ui/relays/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr

widgets::page_header(
ui,
&format!("{} ({} relays)", Page::RelaysMine.name(), relays.len()),
format!("{} ({} relays)", Page::RelaysMine.name(), relays.len()),
|ui| {
if is_editing {
ui.disable();
Expand Down
2 changes: 1 addition & 1 deletion gossip-bin/src/ui/settings/posting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr

ui.checkbox(
&mut app.unsaved_settings.set_user_agent,
&format!(
format!(
"Send User-Agent Header to Relays: gossip/{}",
app.about.version
),
Expand Down
2 changes: 1 addition & 1 deletion gossip-bin/src/ui/widgets/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'a> Switch<'a> {
// }
}

impl<'a> Widget for Switch<'a> {
impl Widget for Switch<'_> {
fn ui(self, ui: &mut Ui) -> Response {
self.show(ui)
}
Expand Down
2 changes: 1 addition & 1 deletion gossip-bin/src/ui/widgets/textedit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl<'t> TextEdit<'t> {
}
}

impl<'t> Widget for TextEdit<'t> {
impl Widget for TextEdit<'_> {
fn ui(self, ui: &mut egui_winit::egui::Ui) -> egui_winit::egui::Response {
let output = self.show(ui);
output.response
Expand Down
8 changes: 4 additions & 4 deletions gossip-bin/src/ui/you/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra

fn view_line(ui: &mut Ui, field: &str, data: Option<&String>) {
ui.horizontal(|ui| {
ui.label(&format!("{}: ", field));
ui.label(format!("{}: ", field));
if let Some(value) = data {
ui.label(value);
} else {
Expand All @@ -161,7 +161,7 @@ fn view_line(ui: &mut Ui, field: &str, data: Option<&String>) {

fn edit_line(ui: &mut Ui, field: &str, data: &mut Option<String>, edit_color: Color32) {
ui.horizontal(|ui| {
ui.label(&format!("{}: ", field));
ui.label(format!("{}: ", field));
ui.with_layout(Layout::right_to_left(Align::TOP), |ui| {
if data.is_some() {
if ui.button("Remove").clicked() {
Expand Down Expand Up @@ -189,7 +189,7 @@ fn edit_line(ui: &mut Ui, field: &str, data: &mut Option<String>, edit_color: Co
fn view_lines_other(ui: &mut Ui, other: &Map<String, Value>) {
for (field, jsonvalue) in other.iter() {
ui.horizontal(|ui| {
ui.label(&format!("{}: ", field));
ui.label(format!("{}: ", field));
if let Value::String(s) = jsonvalue {
ui.label(s.to_owned());
} else if let Ok(s) = serde_json::to_string(&jsonvalue) {
Expand All @@ -206,7 +206,7 @@ fn edit_lines_other(ui: &mut Ui, other: &mut Map<String, Value>, edit_color: Col
let mut to_remove: Vec<String> = Vec::new();
for (field, jsonvalue) in other.iter_mut() {
ui.horizontal(|ui| {
ui.label(&format!("{}: ", field));
ui.label(format!("{}: ", field));
if let Value::String(s) = jsonvalue {
ui.with_layout(Layout::right_to_left(Align::TOP), |ui| {
if ui.button("Remove").clicked() {
Expand Down
Loading