fix chatgpt review

This commit is contained in:
rustdesk
2025-07-10 16:17:23 +08:00
parent 458090b737
commit a92d2301d9

View File

@@ -621,49 +621,49 @@ mod ios_impl {
Ok(()) Ok(())
} }
lazy_static::lazy_static! {
static ref AUDIO_SENDER: Arc<Mutex<Option<Sender<Vec<f32>>>>> = Arc::new(Mutex::new(None));
}
fn setup_ios_audio_callback(sender: Sender<Vec<f32>>) { fn setup_ios_audio_callback(sender: Sender<Vec<f32>>) {
// Set up the audio callback from iOS // Set up the audio callback from iOS
// Check current audio permission setting
let audio_enabled = Config::get_option("enable-audio") != "N";
unsafe { unsafe {
// Check current audio permission setting
let audio_enabled = Config::get_option("enable-audio") != "N";
scrap::ios::ffi::enable_audio(audio_enabled, false); scrap::ios::ffi::enable_audio(audio_enabled, false);
// Set the audio callback // Set the audio callback
scrap::ios::ffi::set_audio_callback(Some(audio_callback)); scrap::ios::ffi::set_audio_callback(Some(audio_callback));
// Store sender in a global for the callback
AUDIO_SENDER = Some(Box::into_raw(Box::new(sender)));
} }
// Store sender in a thread-safe way
*AUDIO_SENDER.lock().unwrap() = Some(sender);
} }
static mut AUDIO_SENDER: Option<*mut Sender<Vec<f32>>> = None;
extern "C" fn audio_callback(data: *const u8, size: u32, is_mic: bool) { extern "C" fn audio_callback(data: *const u8, size: u32, is_mic: bool) {
// Only process microphone audio when enabled // Only process microphone audio when enabled
if !is_mic { if !is_mic {
return; return;
} }
unsafe { if let Some(ref sender) = *AUDIO_SENDER.lock().unwrap() {
if let Some(sender_ptr) = AUDIO_SENDER { // Convert audio data from bytes to f32
let sender = &*sender_ptr; // Assuming audio comes as 16-bit PCM stereo at 48kHz
let samples = size as usize / 2; // 16-bit = 2 bytes per sample
// Convert audio data from bytes to f32 let mut float_data = Vec::with_capacity(samples);
// Assuming audio comes as 16-bit PCM stereo at 48kHz
let samples = size as usize / 2; // 16-bit = 2 bytes per sample unsafe {
let mut float_data = Vec::with_capacity(samples);
let data_slice = std::slice::from_raw_parts(data as *const i16, samples); let data_slice = std::slice::from_raw_parts(data as *const i16, samples);
for &sample in data_slice { for &sample in data_slice {
// Convert i16 to f32 normalized to [-1.0, 1.0] // Convert i16 to f32 normalized to [-1.0, 1.0]
float_data.push(sample as f32 / 32768.0); float_data.push(sample as f32 / 32768.0);
} }
}
// Send in chunks matching our frame size
for chunk in float_data.chunks(FRAMES_PER_BUFFER * CHANNELS as usize) { // Send in chunks matching our frame size
if chunk.len() == FRAMES_PER_BUFFER * CHANNELS as usize { for chunk in float_data.chunks(FRAMES_PER_BUFFER * CHANNELS as usize) {
let _ = sender.send(chunk.to_vec()); if chunk.len() == FRAMES_PER_BUFFER * CHANNELS as usize {
} let _ = sender.send(chunk.to_vec());
} }
} }
} }