simple not working tui for device select
This commit is contained in:
Generated
+1245
-19
File diff suppressed because it is too large
Load Diff
@@ -13,3 +13,6 @@ tokio = { version = "1.53.1", features = ["full"] }
|
||||
rand = "0.10.2"
|
||||
clap = { version = "4.6.6", features = ["derive"] }
|
||||
ropus = "0.12.18"
|
||||
ratatui = "0.30.2"
|
||||
crossterm = "0.29.0"
|
||||
gag = "1.0.0"
|
||||
|
||||
+22
-26
@@ -1,5 +1,5 @@
|
||||
use cpal::{
|
||||
Device,
|
||||
Device, StreamConfig,
|
||||
traits::{DeviceTrait, StreamTrait},
|
||||
};
|
||||
use ringbuf::{
|
||||
@@ -21,17 +21,13 @@ use crate::models::{
|
||||
|
||||
pub async fn audio_stream_receiver(
|
||||
output_device: Device,
|
||||
output_device_config: StreamConfig,
|
||||
active_input_audio_streams_producers: InputAudioStreamsProducers,
|
||||
active_input_audio_streams_consumers: InputAudioStreamsConsumers,
|
||||
active_input_audio_streams_encoders: InputAudioStreamsEncoders,
|
||||
active_input_audio_streams_info: InputAudioStreamsInfo,
|
||||
udp_socket: Arc<UdpSocket>,
|
||||
) {
|
||||
let speaker_config: cpal::StreamConfig = output_device
|
||||
.default_output_config()
|
||||
.expect("stream config init failed")
|
||||
.into();
|
||||
|
||||
let output_data_fn = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
|
||||
let mut streams = active_input_audio_streams_consumers.lock().unwrap();
|
||||
for sample in data {
|
||||
@@ -50,7 +46,7 @@ pub async fn audio_stream_receiver(
|
||||
|
||||
let err_fn = |err| eprintln!("stream error: {}", err);
|
||||
let output_stream = output_device
|
||||
.build_output_stream(speaker_config, output_data_fn, err_fn, None)
|
||||
.build_output_stream(output_device_config, output_data_fn, err_fn, None)
|
||||
.expect("building output stream failed");
|
||||
|
||||
output_stream.play().expect("failed to play output stream");
|
||||
@@ -106,11 +102,11 @@ pub async fn audio_stream_receiver(
|
||||
let mut active_streams_map = active_input_audio_streams_producers.lock().unwrap();
|
||||
|
||||
if let Some(producer) = active_streams_map.get_mut(&(client_id, stream_id)) {
|
||||
if stream_info.channels == speaker_config.channels {
|
||||
if stream_info.channels == output_device_config.channels {
|
||||
producer.push_slice(
|
||||
&decoded_samples[..decoded_len * stream_info.channels as usize],
|
||||
);
|
||||
} else if stream_info.channels > speaker_config.channels {
|
||||
} else if stream_info.channels > output_device_config.channels {
|
||||
for (i, frame) in decoded_samples
|
||||
[..decoded_len * stream_info.channels as usize]
|
||||
.chunks_exact(2)
|
||||
@@ -125,7 +121,7 @@ pub async fn audio_stream_receiver(
|
||||
);
|
||||
} else {
|
||||
for (i, frame) in decoded_samples_converted
|
||||
[..decoded_len * speaker_config.channels as usize]
|
||||
[..decoded_len * output_device_config.channels as usize]
|
||||
.chunks_exact_mut(2)
|
||||
.enumerate()
|
||||
{
|
||||
@@ -135,7 +131,7 @@ pub async fn audio_stream_receiver(
|
||||
|
||||
producer.push_slice(
|
||||
&decoded_samples_converted
|
||||
[..decoded_len * speaker_config.channels as usize],
|
||||
[..decoded_len * output_device_config.channels as usize],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -147,16 +143,12 @@ pub async fn audio_stream_receiver(
|
||||
|
||||
pub async fn audio_stream_spawner(
|
||||
input_device: Device,
|
||||
input_device_config: StreamConfig,
|
||||
global_event_tx: Sender<ClientEvent>,
|
||||
mut global_event_rx: Receiver<ClientEvent>,
|
||||
udp_socket: Arc<UdpSocket>,
|
||||
clinet_info: Arc<RwLock<ClientInfo>>,
|
||||
) {
|
||||
let config: cpal::StreamConfig = input_device
|
||||
.default_input_config()
|
||||
.expect("input stream config init failed")
|
||||
.into();
|
||||
|
||||
let current_stream_id: u8 = rand::random();
|
||||
|
||||
loop {
|
||||
@@ -168,8 +160,8 @@ pub async fn audio_stream_spawner(
|
||||
let _ = global_event_tx.send(ClientEvent::NewAudioStreamRequest(AudioStreamInfo {
|
||||
client_id: client_id,
|
||||
stream_id: current_stream_id,
|
||||
channels: config.channels,
|
||||
sample_rate: config.sample_rate,
|
||||
channels: input_device_config.channels,
|
||||
sample_rate: input_device_config.sample_rate,
|
||||
}));
|
||||
break;
|
||||
}
|
||||
@@ -190,8 +182,8 @@ pub async fn audio_stream_spawner(
|
||||
}
|
||||
|
||||
let ring_buffer_ms = 150.0;
|
||||
let ring_buffer_frames = (ring_buffer_ms / 1000.0) * config.sample_rate as f32;
|
||||
let ring_buffer_samples = ring_buffer_frames as usize * config.channels as usize;
|
||||
let ring_buffer_frames = (ring_buffer_ms / 1000.0) * input_device_config.sample_rate as f32;
|
||||
let ring_buffer_samples = ring_buffer_frames as usize * input_device_config.channels as usize;
|
||||
let ring_buffer_size = ring_buffer_samples;
|
||||
let ring_buffer = HeapRb::<f32>::new(ring_buffer_size);
|
||||
let (mut producer, mut consumer) = ring_buffer.split();
|
||||
@@ -204,25 +196,29 @@ pub async fn audio_stream_spawner(
|
||||
let err_fn = |err| eprintln!("stream error: {}", err);
|
||||
|
||||
let input_stream = input_device
|
||||
.build_input_stream(config, input_data_fn, err_fn, None)
|
||||
.build_input_stream(input_device_config, input_data_fn, err_fn, None)
|
||||
.expect("building input stream failed");
|
||||
|
||||
input_stream.play().expect("failed to play input stream");
|
||||
|
||||
let frames_needed = ((20.0 / 1000.0) * config.sample_rate as f32) as usize;
|
||||
let frames_needed = ((20.0 / 1000.0) * input_device_config.sample_rate as f32) as usize;
|
||||
let mut frame_buffer = vec![0.0; frames_needed];
|
||||
|
||||
let channels: ropus::Channels = {
|
||||
if config.channels == 1 {
|
||||
if input_device_config.channels == 1 {
|
||||
ropus::Channels::Mono
|
||||
} else {
|
||||
ropus::Channels::Stereo
|
||||
}
|
||||
};
|
||||
|
||||
let mut opus_encoder = Encoder::builder(config.sample_rate, channels, ropus::Application::Voip)
|
||||
.build()
|
||||
.unwrap();
|
||||
let mut opus_encoder = Encoder::builder(
|
||||
input_device_config.sample_rate,
|
||||
channels,
|
||||
ropus::Application::Voip,
|
||||
)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let mut encoded_frame_buffer = [0u8; 1024];
|
||||
|
||||
|
||||
+69
-45
@@ -1,9 +1,10 @@
|
||||
mod audio_streams;
|
||||
mod models;
|
||||
mod tcp_connection;
|
||||
mod tui;
|
||||
|
||||
use clap::Parser;
|
||||
use cpal::traits::HostTrait;
|
||||
use cpal::traits::{DeviceTrait, HostTrait};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
@@ -18,18 +19,13 @@ use crate::{
|
||||
InputAudioStreamsInfo, InputAudioStreamsProducers,
|
||||
},
|
||||
tcp_connection::tcp_connection,
|
||||
tui::Tui,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let cli = CliArgs::parse();
|
||||
|
||||
let client_info: Arc<RwLock<ClientInfo>> = Arc::new(RwLock::new(ClientInfo {
|
||||
client_id: 0,
|
||||
server_address: cli.server_ip.unwrap(),
|
||||
connected: false,
|
||||
}));
|
||||
|
||||
let (global_event_tx, _) = broadcast::channel::<ClientEvent>(16);
|
||||
|
||||
let active_input_audio_streams_producers: InputAudioStreamsProducers =
|
||||
@@ -43,49 +39,77 @@ async fn main() {
|
||||
let active_input_audio_streams_info: InputAudioStreamsInfo =
|
||||
Arc::new(RwLock::new(HashMap::new()));
|
||||
|
||||
tokio::spawn(tcp_connection(
|
||||
client_info.clone(),
|
||||
global_event_tx.clone(),
|
||||
global_event_tx.subscribe(),
|
||||
active_input_audio_streams_producers.clone(),
|
||||
active_input_audio_streams_consumers.clone(),
|
||||
active_input_audio_streams_encoders.clone(),
|
||||
active_input_audio_streams_info.clone(),
|
||||
));
|
||||
|
||||
let _ = global_event_tx.send(ClientEvent::NewConnection);
|
||||
|
||||
let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await.unwrap());
|
||||
|
||||
let host = cpal::default_host();
|
||||
if cli.connect_with_defaults == None {
|
||||
ratatui::run(|terminal| {
|
||||
Tui::new().run(
|
||||
terminal,
|
||||
active_input_audio_streams_producers.clone(),
|
||||
active_input_audio_streams_consumers.clone(),
|
||||
active_input_audio_streams_encoders.clone(),
|
||||
active_input_audio_streams_info.clone(),
|
||||
udp_socket.clone(),
|
||||
)
|
||||
});
|
||||
} else {
|
||||
let client_info: Arc<RwLock<ClientInfo>> = Arc::new(RwLock::new(ClientInfo {
|
||||
client_id: 0,
|
||||
server_address: cli.connect_with_defaults.unwrap(),
|
||||
connected: false,
|
||||
}));
|
||||
|
||||
let input_device = host
|
||||
.default_input_device()
|
||||
.expect("no input device available");
|
||||
let output_device = host
|
||||
.default_output_device()
|
||||
.expect("no output device available");
|
||||
tokio::spawn(tcp_connection(
|
||||
client_info.clone(),
|
||||
global_event_tx.clone(),
|
||||
global_event_tx.subscribe(),
|
||||
active_input_audio_streams_producers.clone(),
|
||||
active_input_audio_streams_consumers.clone(),
|
||||
active_input_audio_streams_encoders.clone(),
|
||||
active_input_audio_streams_info.clone(),
|
||||
));
|
||||
|
||||
println!("input device: {}", input_device);
|
||||
println!("output device: {}", output_device);
|
||||
let _ = global_event_tx.send(ClientEvent::NewConnection);
|
||||
|
||||
tokio::spawn(audio_stream_receiver(
|
||||
output_device,
|
||||
active_input_audio_streams_producers.clone(),
|
||||
active_input_audio_streams_consumers.clone(),
|
||||
active_input_audio_streams_encoders.clone(),
|
||||
active_input_audio_streams_info.clone(),
|
||||
udp_socket.clone(),
|
||||
));
|
||||
let host = cpal::default_host();
|
||||
|
||||
tokio::spawn(audio_stream_spawner(
|
||||
input_device,
|
||||
global_event_tx.clone(),
|
||||
global_event_tx.subscribe(),
|
||||
udp_socket.clone(),
|
||||
client_info.clone(),
|
||||
));
|
||||
let input_device = host
|
||||
.default_input_device()
|
||||
.expect("no input device available");
|
||||
let input_device_config = input_device
|
||||
.default_input_config()
|
||||
.expect("failed to get default input config");
|
||||
|
||||
tokio::signal::ctrl_c().await.expect("failed to close");
|
||||
println!("shutting down");
|
||||
let output_device = host
|
||||
.default_output_device()
|
||||
.expect("no output device available");
|
||||
let output_device_config = output_device
|
||||
.default_output_config()
|
||||
.expect("failed to get default output config");
|
||||
|
||||
println!("input device: {}", input_device);
|
||||
println!("output device: {}", output_device);
|
||||
|
||||
tokio::spawn(audio_stream_receiver(
|
||||
output_device,
|
||||
output_device_config.into(),
|
||||
active_input_audio_streams_producers.clone(),
|
||||
active_input_audio_streams_consumers.clone(),
|
||||
active_input_audio_streams_encoders.clone(),
|
||||
active_input_audio_streams_info.clone(),
|
||||
udp_socket.clone(),
|
||||
));
|
||||
|
||||
tokio::spawn(audio_stream_spawner(
|
||||
input_device,
|
||||
input_device_config.into(),
|
||||
global_event_tx.clone(),
|
||||
global_event_tx.subscribe(),
|
||||
udp_socket.clone(),
|
||||
client_info.clone(),
|
||||
));
|
||||
|
||||
tokio::signal::ctrl_c().await.expect("failed to close");
|
||||
println!("shutting down");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,14 @@ use std::{
|
||||
#[derive(Parser)]
|
||||
#[command(version, about)]
|
||||
pub struct CliArgs {
|
||||
#[arg(short, long, required(true))]
|
||||
pub server_ip: Option<String>,
|
||||
#[arg(short, long, required(false))]
|
||||
pub connect_with_defaults: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TuiScreen {
|
||||
DeviceSelect,
|
||||
Exit,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
use cpal::{
|
||||
Device, SAMPLE_RATE_48K, StreamConfig,
|
||||
traits::{DeviceTrait, HostTrait},
|
||||
};
|
||||
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
||||
use gag::Gag;
|
||||
use ratatui::{
|
||||
DefaultTerminal, Frame,
|
||||
layout::{Constraint, Direction, Layout},
|
||||
style::Style,
|
||||
widgets::{Block, Row, Table, TableState},
|
||||
};
|
||||
use std::{sync::Arc, vec};
|
||||
use tokio::net::UdpSocket;
|
||||
|
||||
use crate::models::{
|
||||
InputAudioStreamsConsumers, InputAudioStreamsEncoders, InputAudioStreamsInfo,
|
||||
InputAudioStreamsProducers, TuiScreen,
|
||||
};
|
||||
|
||||
pub struct Tui {
|
||||
current_screen: TuiScreen,
|
||||
}
|
||||
|
||||
impl Tui {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
current_screen: TuiScreen::DeviceSelect,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(
|
||||
&mut self,
|
||||
terminal: &mut DefaultTerminal,
|
||||
active_input_audio_streams_producers: InputAudioStreamsProducers,
|
||||
active_input_audio_streams_consumers: InputAudioStreamsConsumers,
|
||||
active_input_audio_streams_encoders: InputAudioStreamsEncoders,
|
||||
active_input_audio_streams_info: InputAudioStreamsInfo,
|
||||
udp_socket: Arc<UdpSocket>,
|
||||
) {
|
||||
loop {
|
||||
match self.current_screen {
|
||||
TuiScreen::Exit => {
|
||||
break;
|
||||
}
|
||||
TuiScreen::DeviceSelect => {
|
||||
self.current_screen = DeviceSelectScreen::new().run(terminal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct DeviceSelectScreen {
|
||||
exit: bool,
|
||||
input_device_state: TableState,
|
||||
output_device_state: TableState,
|
||||
input_devices: Vec<(Device, StreamConfig)>,
|
||||
output_devices: Vec<(Device, StreamConfig)>,
|
||||
selected_selection: u8,
|
||||
}
|
||||
|
||||
impl DeviceSelectScreen {
|
||||
pub fn new() -> Self {
|
||||
let _print_gag = Gag::stderr().ok();
|
||||
|
||||
let host = cpal::default_host();
|
||||
|
||||
let mut input_devices: Vec<(Device, StreamConfig)> = Vec::new();
|
||||
|
||||
if let Ok(possible_input_devices) = host.input_devices() {
|
||||
for possible_input_device in possible_input_devices {
|
||||
if let Ok(supported_configs) = possible_input_device.supported_input_configs() {
|
||||
for supported_config in supported_configs {
|
||||
if let Some(input_config) =
|
||||
supported_config.try_with_sample_rate(SAMPLE_RATE_48K)
|
||||
{
|
||||
input_devices.push((possible_input_device, input_config.into()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut output_devices: Vec<(Device, StreamConfig)> = Vec::new();
|
||||
|
||||
if let Ok(possible_output_devices) = host.output_devices() {
|
||||
for possible_output_device in possible_output_devices {
|
||||
if let Ok(supported_configs) = possible_output_device.supported_output_configs() {
|
||||
for supported_config in supported_configs {
|
||||
if let Some(output_config) =
|
||||
supported_config.try_with_sample_rate(SAMPLE_RATE_48K)
|
||||
{
|
||||
output_devices.push((possible_output_device, output_config.into()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
exit: false,
|
||||
input_device_state: TableState::default(),
|
||||
output_device_state: TableState::default(),
|
||||
input_devices: input_devices,
|
||||
output_devices: output_devices,
|
||||
selected_selection: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> TuiScreen {
|
||||
while !self.exit {
|
||||
terminal
|
||||
.draw(|frame| self.draw(frame))
|
||||
.expect("failed to draw");
|
||||
self.handle_events();
|
||||
}
|
||||
|
||||
return TuiScreen::Exit;
|
||||
}
|
||||
|
||||
fn draw(&mut self, frame: &mut Frame) {
|
||||
let outer_layout = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||
.split(frame.area());
|
||||
|
||||
let block = Block::new();
|
||||
let left_block = block.inner(outer_layout[0]);
|
||||
let right_block = block.inner(outer_layout[1]);
|
||||
|
||||
let widths = [
|
||||
Constraint::Percentage(100),
|
||||
Constraint::Min(8),
|
||||
Constraint::Min(11),
|
||||
];
|
||||
|
||||
let mut input_device_rows: Vec<Row> = Vec::new();
|
||||
for (device, config) in self.input_devices.iter() {
|
||||
input_device_rows.push(Row::new(vec![
|
||||
device.description().unwrap().name().to_string(),
|
||||
config.channels.to_string(),
|
||||
config.sample_rate.to_string(),
|
||||
]));
|
||||
}
|
||||
let input_device_table = Table::new(input_device_rows, widths)
|
||||
.header(
|
||||
Row::new(vec!["device name", "channels", "sample rate"])
|
||||
.style(Style::new().bold())
|
||||
.bottom_margin(1),
|
||||
)
|
||||
.block(
|
||||
Block::bordered()
|
||||
.border_style({
|
||||
if self.selected_selection == 0 {
|
||||
Style::new().bold()
|
||||
} else {
|
||||
Style::default()
|
||||
}
|
||||
})
|
||||
.border_type({
|
||||
if self.selected_selection == 0 {
|
||||
ratatui::widgets::BorderType::Thick
|
||||
} else {
|
||||
ratatui::widgets::BorderType::Plain
|
||||
}
|
||||
})
|
||||
.title("select input device"),
|
||||
)
|
||||
.row_highlight_style(Style::new().reversed());
|
||||
|
||||
frame.render_stateful_widget(input_device_table, left_block, &mut self.input_device_state);
|
||||
|
||||
let mut output_device_rows: Vec<Row> = Vec::new();
|
||||
for (device, config) in self.output_devices.iter() {
|
||||
output_device_rows.push(Row::new(vec![
|
||||
device.description().unwrap().name().to_string(),
|
||||
config.channels.to_string(),
|
||||
config.sample_rate.to_string(),
|
||||
]));
|
||||
}
|
||||
let output_device_table = Table::new(output_device_rows, widths)
|
||||
.header(
|
||||
Row::new(vec!["device name", "channels", "sample rate"])
|
||||
.style(Style::new().bold())
|
||||
.bottom_margin(1),
|
||||
)
|
||||
.block(
|
||||
Block::bordered()
|
||||
.title("select output device")
|
||||
.border_style({
|
||||
if self.selected_selection == 1 {
|
||||
Style::new().bold()
|
||||
} else {
|
||||
Style::default()
|
||||
}
|
||||
})
|
||||
.border_type({
|
||||
if self.selected_selection == 1 {
|
||||
ratatui::widgets::BorderType::Thick
|
||||
} else {
|
||||
ratatui::widgets::BorderType::Plain
|
||||
}
|
||||
}),
|
||||
)
|
||||
.row_highlight_style(Style::new().reversed());
|
||||
|
||||
frame.render_stateful_widget(
|
||||
output_device_table,
|
||||
right_block,
|
||||
&mut self.output_device_state,
|
||||
);
|
||||
}
|
||||
|
||||
fn handle_events(&mut self) {
|
||||
match event::read().expect("failed to read event") {
|
||||
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
|
||||
self.handle_key_events(key_event);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_key_events(&mut self, key_event: KeyEvent) {
|
||||
match key_event.code {
|
||||
KeyCode::Char('q') => self.exit(),
|
||||
KeyCode::Down => self.increment_table(),
|
||||
KeyCode::Up => self.decrement_table(),
|
||||
KeyCode::Left => self.selected_selection = 0,
|
||||
KeyCode::Right => self.selected_selection = 1,
|
||||
// KeyCode::Down => self.increment_list(),
|
||||
// KeyCode::Enter => self.get_value(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn exit(&mut self) {
|
||||
self.exit = true;
|
||||
}
|
||||
|
||||
fn increment_table(&mut self) {
|
||||
if self.selected_selection == 0 {
|
||||
self.input_device_state.scroll_down_by(1);
|
||||
} else {
|
||||
self.output_device_state.scroll_down_by(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn decrement_table(&mut self) {
|
||||
if self.selected_selection == 0 {
|
||||
self.input_device_state.scroll_up_by(1);
|
||||
} else {
|
||||
self.output_device_state.scroll_up_by(1);
|
||||
}
|
||||
}
|
||||
|
||||
// fn get_value(&mut self) {
|
||||
// self.table_state.selected();
|
||||
// }
|
||||
|
||||
// fn increment_list(&mut self) {
|
||||
// self.list_state.scroll_down_by(1);
|
||||
// self.table_state.scroll_down_by(1);
|
||||
// }
|
||||
|
||||
// fn decrement_list(&mut self) {
|
||||
// self.list_state.scroll_up_by(1);
|
||||
// self.table_state.scroll_up_by(1);
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user