From 42d17f9d2bd3a0ec2b39623290da44bf020b90f0 Mon Sep 17 00:00:00 2001 From: 21pages Date: Sun, 11 Sep 2022 21:46:53 +0800 Subject: [PATCH] fix audio no sound, add missing VideoFrame timestamp move get_time to hbb_common Signed-off-by: 21pages --- libs/hbb_common/src/lib.rs | 8 ++++++++ libs/scrap/src/common/hwcodec.rs | 6 ++++-- libs/scrap/src/common/vpxcodec.rs | 5 +++-- src/common.rs | 10 +--------- src/server/audio_service.rs | 5 +++-- src/server/connection.rs | 14 +++++++------- src/server/input_service.rs | 6 +++--- src/server/video_service.rs | 2 +- 8 files changed, 30 insertions(+), 26 deletions(-) diff --git a/libs/hbb_common/src/lib.rs b/libs/hbb_common/src/lib.rs index 48fbfe23c..50fcc07b2 100644 --- a/libs/hbb_common/src/lib.rs +++ b/libs/hbb_common/src/lib.rs @@ -228,6 +228,14 @@ pub fn get_uuid() -> Vec { Config::get_key_pair().1 } +#[inline] +pub fn get_time() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_millis()) + .unwrap_or(0) as _ +} + #[cfg(test)] mod tests { use super::*; diff --git a/libs/scrap/src/common/hwcodec.rs b/libs/scrap/src/common/hwcodec.rs index 7431bc952..ee81627d8 100644 --- a/libs/scrap/src/common/hwcodec.rs +++ b/libs/scrap/src/common/hwcodec.rs @@ -4,10 +4,11 @@ use crate::{ }; use hbb_common::{ anyhow::{anyhow, Context}, + bytes::Bytes, config::HwCodecConfig, - lazy_static, log, + get_time, lazy_static, log, message_proto::{EncodedVideoFrame, EncodedVideoFrames, Message, VideoFrame}, - ResultType, bytes::Bytes, + ResultType, }; use hwcodec::{ decode::{DecodeContext, DecodeFrame, Decoder}, @@ -105,6 +106,7 @@ impl EncoderApi for HwEncoder { DataFormat::H264 => vf.set_h264s(frames), DataFormat::H265 => vf.set_h265s(frames), } + vf.timestamp = get_time(); msg_out.set_video_frame(vf); Ok(msg_out) } else { diff --git a/libs/scrap/src/common/vpxcodec.rs b/libs/scrap/src/common/vpxcodec.rs index 0fda53fa3..47b3df3a6 100644 --- a/libs/scrap/src/common/vpxcodec.rs +++ b/libs/scrap/src/common/vpxcodec.rs @@ -4,15 +4,15 @@ use hbb_common::anyhow::{anyhow, Context}; use hbb_common::message_proto::{EncodedVideoFrame, EncodedVideoFrames, Message, VideoFrame}; -use hbb_common::ResultType; +use hbb_common::{ResultType, get_time}; use crate::codec::EncoderApi; use crate::STRIDE_ALIGN; use super::vpx::{vp8e_enc_control_id::*, vpx_codec_err_t::*, *}; +use hbb_common::bytes::Bytes; use std::os::raw::{c_int, c_uint}; use std::{ptr, slice}; -use hbb_common::bytes::Bytes; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub enum VpxVideoCodecId { @@ -285,6 +285,7 @@ impl VpxEncoder { frames: vp9s.into(), ..Default::default() }); + vf.timestamp = get_time(); msg_out.set_video_frame(vf); msg_out } diff --git a/src/common.rs b/src/common.rs index 68656853a..d5ccf0e1b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -426,14 +426,6 @@ pub fn refresh_rendezvous_server() { }); } -#[inline] -pub fn get_time() -> i64 { - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_millis()) - .unwrap_or(0) as _ -} - pub fn run_me>(args: Vec) -> std::io::Result { #[cfg(not(feature = "appimage"))] { @@ -676,4 +668,4 @@ lazy_static::lazy_static! { #[cfg(target_os = "linux")] lazy_static::lazy_static! { pub static ref IS_X11: Mutex = Mutex::new("x11" == hbb_common::platform::linux::get_display_server()); -} \ No newline at end of file +} diff --git a/src/server/audio_service.rs b/src/server/audio_service.rs index addc06644..c7a720ded 100644 --- a/src/server/audio_service.rs +++ b/src/server/audio_service.rs @@ -13,6 +13,7 @@ // https://github.com/krruzic/pulsectl use super::*; +use hbb_common::get_time; use magnum_opus::{Application::*, Channels::*, Encoder}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -348,7 +349,7 @@ fn send_f32(data: &[f32], encoder: &mut Encoder, sp: &GenericService) { let mut msg_out = Message::new(); msg_out.set_audio_frame(AudioFrame { data: data.into(), - timestamp: crate::common::get_time(), + timestamp: get_time(), ..Default::default() }); sp.send(msg_out); @@ -368,7 +369,7 @@ fn send_f32(data: &[f32], encoder: &mut Encoder, sp: &GenericService) { let mut msg_out = Message::new(); msg_out.set_audio_frame(AudioFrame { data: data.into(), - timestamp: crate::common::get_time(), + timestamp: get_time(), ..Default::default() }); sp.send(msg_out); diff --git a/src/server/connection.rs b/src/server/connection.rs index 42f4bc940..161c058f8 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -12,7 +12,7 @@ use hbb_common::{ fs, fs::can_enable_overwrite_detection, futures::{SinkExt, StreamExt}, - get_version_number, + get_time, get_version_number, message_proto::{option_message::BoolOption, permission_info::Permission}, password_security as password, sleep, timeout, tokio::{ @@ -397,7 +397,7 @@ impl Connection { conn.on_close("Timeout", true).await; break; } - let time = crate::get_time(); + let time = get_time(); if time > 0 && conn.last_test_delay == 0 { conn.last_test_delay = time; let mut msg_out = Message::new(); @@ -983,7 +983,7 @@ impl Connection { .get(&self.ip) .map(|x| x.clone()) .unwrap_or((0, 0, 0)); - let time = (crate::get_time() / 60_000) as i32; + let time = (get_time() / 60_000) as i32; if failure.2 > 30 { self.send_login_error("Too many wrong password attempts") .await; @@ -1022,7 +1022,7 @@ impl Connection { self.inner.send(msg_out.into()); } else { self.last_test_delay = 0; - let new_delay = (crate::get_time() - t.time) as u32; + let new_delay = (get_time() - t.time) as u32; video_service::VIDEO_QOS .lock() .unwrap() @@ -1038,9 +1038,9 @@ impl Connection { #[cfg(not(any(target_os = "android", target_os = "ios")))] if self.keyboard { if is_left_up(&me) { - CLICK_TIME.store(crate::get_time(), Ordering::SeqCst); + CLICK_TIME.store(get_time(), Ordering::SeqCst); } else { - MOUSE_MOVE_TIME.store(crate::get_time(), Ordering::SeqCst); + MOUSE_MOVE_TIME.store(get_time(), Ordering::SeqCst); } self.input_mouse(me, self.inner.id()); } @@ -1049,7 +1049,7 @@ impl Connection { #[cfg(not(any(target_os = "android", target_os = "ios")))] if self.keyboard { if is_enter(&me) { - CLICK_TIME.store(crate::get_time(), Ordering::SeqCst); + CLICK_TIME.store(get_time(), Ordering::SeqCst); } // handle all down as press // fix unexpected repeating key on remote linux, seems also fix abnormal alt/shift, which diff --git a/src/server/input_service.rs b/src/server/input_service.rs index d78441a18..f36f2c50e 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -3,7 +3,7 @@ use crate::common::IS_X11; #[cfg(target_os = "macos")] use dispatch::Queue; use enigo::{Enigo, Key, KeyboardControllable, MouseButton, MouseControllable}; -use hbb_common::{config::COMPRESS_LEVEL, protobuf::EnumOrUnknown}; +use hbb_common::{config::COMPRESS_LEVEL, get_time, protobuf::EnumOrUnknown}; use rdev::{simulate, EventType, Key as RdevKey}; use std::{ convert::TryFrom, @@ -111,7 +111,7 @@ fn run_pos(sp: GenericService, state: &mut StatePos) -> ResultType<()> { ..Default::default() }); let exclude = { - let now = crate::get_time(); + let now = get_time(); let lock = LATEST_INPUT.lock().unwrap(); if now - lock.time < 300 { lock.conn @@ -365,7 +365,7 @@ fn handle_mouse_(evt: &MouseEvent, conn: i32) { let buttons = evt.mask >> 3; let evt_type = evt.mask & 0x7; if evt_type == 0 { - let time = crate::get_time(); + let time = get_time(); *LATEST_INPUT.lock().unwrap() = Input { time, conn }; } let mut en = ENIGO.lock().unwrap(); diff --git a/src/server/video_service.rs b/src/server/video_service.rs index b22418398..eee9e4255 100644 --- a/src/server/video_service.rs +++ b/src/server/video_service.rs @@ -601,7 +601,7 @@ fn create_msg(vp9s: Vec) -> Message { frames: vp9s.into(), ..Default::default() }); - vf.timestamp = crate::common::get_time(); + vf.timestamp = hbb_common::get_time(); msg_out.set_video_frame(vf); msg_out }