Skip to content

Commit

Permalink
debug log, remove me
Browse files Browse the repository at this point in the history
Signed-off-by: 21pages <pages21@163.com>
  • Loading branch information
21pages committed Apr 20, 2023
1 parent 93c829a commit 61f2f4e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 38 deletions.
108 changes: 72 additions & 36 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use scrap::{
ImageFormat,
};

use crate::common::{self, is_keyboard_mode_supported};
use crate::common::{self, flog, is_keyboard_mode_supported};

#[cfg(not(any(target_os = "android", target_os = "ios")))]
use crate::common::{check_clipboard, ClipboardContext, CLIPBOARD_INTERVAL};
Expand Down Expand Up @@ -828,6 +828,10 @@ impl AudioHandler {
log::info!("Remote input format: {:?}", format0);
let config: StreamConfig = config.into();
self.device_channel = config.channels;
flog(
"start_audio ",
&format!("device:{:?}, config:{:?}", device.name(), config),
);
match sample_format {
cpal::SampleFormat::I8 => self.build_output_stream::<i8>(&config, &device)?,
cpal::SampleFormat::I16 => self.build_output_stream::<i16>(&config, &device)?,
Expand All @@ -847,6 +851,7 @@ impl AudioHandler {

/// Handle audio format and create an audio decoder.
pub fn handle_format(&mut self, f: AudioFormat) {
flog("handle_format ", &format!("{:?}", f));
match AudioDecoder::new(f.sample_rate, if f.channels > 1 { Stereo } else { Mono }) {
Ok(d) => {
let buffer = vec![0.; f.sample_rate as usize * f.channels as usize];
Expand All @@ -863,8 +868,10 @@ impl AudioHandler {
/// Handle audio frame and play it.
#[inline]
pub fn handle_frame(&mut self, frame: AudioFrame) {
flog("handle_frame recv auido frame ", "");
#[cfg(not(any(target_os = "android", target_os = "linux")))]
if self.audio_stream.is_none() || !self.ready.lock().unwrap().clone() {
flog("handle_frame not ready ", "");
return;
}
#[cfg(target_os = "linux")]
Expand All @@ -877,48 +884,68 @@ impl AudioHandler {
return;
}
self.audio_decoder.as_mut().map(|(d, buffer)| {
if let Ok(n) = d.decode_float(&frame.data, buffer, false) {
let channels = self.channels;
let n = n * (channels as usize);
#[cfg(not(any(target_os = "android", target_os = "linux")))]
{
let sample_rate0 = self.sample_rate.0;
let sample_rate = self.sample_rate.1;
let audio_buffer = self.audio_buffer.0.clone();
let mut buffer = buffer[0..n].to_owned();
if sample_rate != sample_rate0 {
buffer = crate::audio_resample(
&buffer[0..n],
sample_rate0,
sample_rate,
channels,
);
match d.decode_float(&frame.data, buffer, false) {
Ok(n) => {
flog(
"handle_frame decode_float success data size ",
&format!("{}", n),
);
let channels = self.channels;
let n = n * (channels as usize);
#[cfg(not(any(target_os = "android", target_os = "linux")))]
{
let sample_rate0 = self.sample_rate.0;
let sample_rate = self.sample_rate.1;
let audio_buffer = self.audio_buffer.0.clone();
let mut buffer = buffer[0..n].to_owned();
let oldlen = buffer.len();
if sample_rate != sample_rate0 {
buffer = crate::audio_resample(
&buffer[0..n],
sample_rate0,
sample_rate,
channels,
);
}
if self.channels != self.device_channel {
buffer = crate::audio_rechannel(
buffer,
sample_rate,
sample_rate,
self.channels,
self.device_channel,
);
flog(
"handle_frame audio_rechannel ",
&format!(
"channels: {}->{}, datalen: {}->{}",
self.channels,
self.device_channel,
oldlen,
buffer.len()
),
);
}
audio_buffer.lock().unwrap().push_slice_overwrite(&buffer);
}
if self.channels != self.device_channel {
buffer = crate::audio_rechannel(
buffer,
sample_rate,
sample_rate,
self.channels,
self.device_channel,
);
#[cfg(target_os = "android")]
{
self.oboe.as_mut().map(|x| x.push(&buffer[0..n]));
}
#[cfg(target_os = "linux")]
{
let data_u8 = unsafe {
std::slice::from_raw_parts::<u8>(buffer.as_ptr() as _, n * 4)
};
self.simple.as_mut().map(|x| x.write(data_u8));
}
audio_buffer.lock().unwrap().push_slice_overwrite(&buffer);
}
#[cfg(target_os = "android")]
{
self.oboe.as_mut().map(|x| x.push(&buffer[0..n]));
}
#[cfg(target_os = "linux")]
{
let data_u8 =
unsafe { std::slice::from_raw_parts::<u8>(buffer.as_ptr() as _, n * 4) };
self.simple.as_mut().map(|x| x.write(data_u8));
Err(e) => {
flog("handle_frame decode_float failed ", &format!("{}", e));
}
}
});
}

/// Build audio output stream for current device.
#[cfg(not(any(target_os = "android", target_os = "linux")))]
fn build_output_stream<T: cpal::Sample + cpal::SizedSample + cpal::FromSample<f32>>(
Expand All @@ -929,6 +956,10 @@ impl AudioHandler {
let err_fn = move |err| {
// too many errors, will improve later
log::trace!("an error occurred on stream: {}", err);
flog(
"build_output_stream err callback ",
&format!("err:{:?}", err),
);
};
let audio_buffer = self.audio_buffer.0.clone();
let ready = self.ready.clone();
Expand All @@ -944,6 +975,10 @@ impl AudioHandler {
if lock.occupied_len() < n {
n = lock.occupied_len();
}
flog(
"build_output_stream data callback ",
&format!("data len:{:?}, n:{}", data.len(), n),
);
let mut elems = vec![0.0f32; n];
lock.pop_slice(&mut elems);
drop(lock);
Expand All @@ -959,6 +994,7 @@ impl AudioHandler {
timeout,
)?;
stream.play()?;
flog("build_output_stream play ok", "");
self.audio_stream = Some(Box::new(stream));
Ok(())
}
Expand Down
26 changes: 26 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
future::Future,
sync::{Arc, Mutex},
time::Instant,
};

#[derive(Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -1004,3 +1005,28 @@ pub fn pk_to_fingerprint(pk: Vec<u8>) -> String {
})
.collect()
}

lazy_static::lazy_static! {
static ref INSTANTS: Arc::<Mutex<std::collections::HashMap<String,Instant>>> = Default::default();
}

pub fn flog(tag: &str, s: &str) {
use hbb_common::chrono::prelude::*;
use std::io::Write;
let mut lock = INSTANTS.lock().unwrap();
let log = if lock.contains_key(tag) {
lock.get(tag)
.map(|i| i.elapsed() > std::time::Duration::from_secs(1))
.unwrap_or(false)
} else {
true
};
if log {
let mut option = std::fs::OpenOptions::new();
if let Ok(mut f) = option.append(true).create(true).open("D:/log.txt") {
write!(&mut f, "{:?} {} {}\n", Local::now(), tag, s).ok();
}
println!("{} {}", tag, s);
lock.insert(tag.to_string(), Instant::now());
}
}
51 changes: 49 additions & 2 deletions src/server/audio_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// https://wiki.debian.org/audio-loopback
// https://github.com/krruzic/pulsectl

use crate::common::flog;

use super::*;
use magnum_opus::{Application::*, Channels::*, Encoder};
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -101,6 +103,8 @@ mod pa_impl {

#[cfg(not(any(target_os = "linux", target_os = "android")))]
mod cpal_impl {
use crate::common::flog;

use super::*;
use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
Expand Down Expand Up @@ -148,9 +152,20 @@ mod cpal_impl {
encoder: &mut Encoder,
sp: &GenericService,
) {
let oldlen = data.len();
let mut data = data;
if sample_rate0 != sample_rate {
data = crate::common::audio_resample(&data, sample_rate0, sample_rate, device_channel);
flog(
"audio servie send resample",
&format!(
" {}-> {}, {} -> {}",
sample_rate0,
sample_rate,
oldlen,
data.len()
),
);
}
if device_channel != encode_channel {
data = crate::common::audio_rechannel(
Expand All @@ -159,7 +174,17 @@ mod cpal_impl {
sample_rate,
device_channel,
encode_channel,
)
);
flog(
"audio servie send rechannel",
&format!(
"{}-> {}, {} -> {}",
device_channel,
encode_channel,
oldlen,
data.len()
),
);
}
send_f32(&data, encoder, sp);
}
Expand Down Expand Up @@ -271,6 +296,10 @@ mod cpal_impl {
let err_fn = move |err| {
// too many UnknownErrno, will improve later
log::trace!("an error occurred on stream: {}", err);
flog(
"build_input_stream err callback",
&format!("err: {:?}", err),
);
};
let sample_rate_0 = config.sample_rate().0;
log::debug!("Audio sample rate : {}", sample_rate);
Expand All @@ -284,6 +313,13 @@ mod cpal_impl {
let frame_size = sample_rate as usize / 100; // 10 ms
let encode_len = frame_size * encode_channel as usize;
let rechannel_len = encode_len * device_channel as usize / encode_channel as usize;
flog(
"build_input_stream",
&format!(
"device_channel:{}, encode_channel:{}, encode_len:{}, rechannel_len:{}",
device_channel, encode_channel as u16, encode_len, rechannel_len
),
);
INPUT_BUFFER.lock().unwrap().clear();
let timeout = None;
let stream_config = StreamConfig {
Expand All @@ -299,6 +335,10 @@ mod cpal_impl {
lock.extend(buffer);
while lock.len() >= rechannel_len {
let frame: Vec<f32> = lock.drain(0..rechannel_len).collect();
flog(
"build_input_stream data callback",
&format!("data len: {:?}, lock len:{}", data.len(), lock.len()),
);
send(
frame,
sample_rate_0,
Expand All @@ -313,6 +353,7 @@ mod cpal_impl {
err_fn,
timeout,
)?;
flog("build_input_stream ok", "");
Ok(stream)
}
}
Expand Down Expand Up @@ -386,13 +427,19 @@ fn send_f32(data: &[f32], encoder: &mut Encoder, sp: &GenericService) {
#[cfg(not(target_os = "android"))]
match encoder.encode_vec_float(data, data.len() * 6) {
Ok(data) => {
flog(
"send_f32 encode_vec_float success",
&format!(" data len {:?}", data.len()),
);
let mut msg_out = Message::new();
msg_out.set_audio_frame(AudioFrame {
data: data.into(),
..Default::default()
});
sp.send(msg_out);
}
Err(_) => {}
Err(e) => {
flog("send_f32 encode_vec_float err", &format!("{:?}", e));
}
}
}

0 comments on commit 61f2f4e

Please sign in to comment.