Skip to content

Commit

Permalink
Merge branch 'rustdesk:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangbo8418 authored Nov 21, 2024
2 parents aa9283a + 74dd0c8 commit 290966e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/hbb_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub use anyhow::{self, bail};
pub use futures_util;
pub mod config;
pub mod fs;
pub mod mem;
pub use lazy_static;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub use mac_address;
Expand Down
14 changes: 14 additions & 0 deletions libs/hbb_common/src/mem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// SAFETY: the returned Vec must not be resized or reserverd
pub unsafe fn aligned_u8_vec(cap: usize, align: usize) -> Vec<u8> {
use std::alloc::*;

let layout =
Layout::from_size_align(cap, align).expect("invalid aligned value, must be power of 2");
unsafe {
let ptr = alloc(layout);
if ptr.is_null() {
panic!("failed to allocate {} bytes", cap);
}
Vec::from_raw_parts(ptr, 0, cap)
}
}
19 changes: 19 additions & 0 deletions src/server/audio_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ pub fn restart() {
#[cfg(any(target_os = "linux", target_os = "android"))]
mod pa_impl {
use super::*;

// SAFETY: constrains of hbb_common::mem::aligned_u8_vec must be held
unsafe fn align_to_32(data: Vec<u8>) -> Vec<u8> {
if (data.as_ptr() as usize & 3) == 0 {
return data;
}

let mut buf = vec![];
buf = unsafe { hbb_common::mem::aligned_u8_vec(data.len(), 4) };
buf.extend_from_slice(data.as_ref());
buf
}

#[tokio::main(flavor = "current_thread")]
pub async fn run(sp: EmptyExtraFieldService) -> ResultType<()> {
hbb_common::sleep(0.1).await; // one moment to wait for _pa ipc
Expand Down Expand Up @@ -106,23 +119,29 @@ mod pa_impl {
sps.send(create_format_msg(crate::platform::PA_SAMPLE_RATE, 2));
Ok(())
})?;

#[cfg(target_os = "linux")]
if let Ok(data) = stream.next_raw().await {
if data.len() == 0 {
send_f32(&zero_audio_frame, &mut encoder, &sp);
continue;
}

if data.len() != AUDIO_DATA_SIZE_U8 {
continue;
}

let data = unsafe { align_to_32(data.into()) };
let data = unsafe {
std::slice::from_raw_parts::<f32>(data.as_ptr() as _, data.len() / 4)
};
send_f32(data, &mut encoder, &sp);
}

#[cfg(target_os = "android")]
if scrap::android::ffi::get_audio_raw(&mut android_data, &mut vec![]).is_some() {
let data = unsafe {
android_data = align_to_32(android_data);
std::slice::from_raw_parts::<f32>(
android_data.as_ptr() as _,
android_data.len() / 4,
Expand Down

0 comments on commit 290966e

Please sign in to comment.