mirror of
https://github.com/rustdesk/rustdesk.git
synced 2024-11-23 19:49:05 +08:00
fix mis-align problem when converting &[u8] to &[f32] (#9986)
* fix: windows, improve audio buffer (#9770) * . * fix statics does not record and avoid channel changing when drio audio when audio is stero * add some commence * fix mis-align problem when converting &[u8] to &[f32] * add safety commence * revert client.rs * avoid tmp lifetime extends * avoid move in loop * avoid use after drop * another use after free * another use after free * make code more reasonable --------- Co-authored-by: zylthinking <zhaoyulong@qianxin.com>
This commit is contained in:
parent
d26fea41ee
commit
74dd0c8fa0
@ -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;
|
||||
|
14
libs/hbb_common/src/mem.rs
Normal file
14
libs/hbb_common/src/mem.rs
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -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
|
||||
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user