Skip to content

Commit

Permalink
Merge pull request #3 from wiiznokes/rusty-tcp
Browse files Browse the repository at this point in the history
better args parsing
  • Loading branch information
wiiznokes committed Aug 9, 2023
2 parents 4d366ff + c567db5 commit 6093114
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 51 deletions.
29 changes: 29 additions & 0 deletions android-mic-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android-mic-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ byteorder = "1.4.3"
rtrb = "0.2.3"
crossterm = {version = "0.27.0"}
clap = { version = "4.3.19", features = ["derive"] }
strum = { version = "0.25", features = ["derive"] }
20 changes: 18 additions & 2 deletions android-mic-rs/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
experimental project written in Rust

backend for Widows and Linux
support only UDP
backend for Widows and Linux.
Technically, the code could run on Android

supported
- UDP
- TCP

how to build

Expand Down Expand Up @@ -40,4 +43,17 @@ Options:
```

example:
```
cargo run --release -- --ip 192.168.1.79 -m UDP
```



clear && cargo r -- -i 192.168.1.79


todo:
- support f32 format
- stereo
- parse ipv4/v6
- choose output device
17 changes: 16 additions & 1 deletion android-mic-rs/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ use cpal::{

use rtrb::{chunks::ChunkError, Consumer};

pub fn setup_audio(mut consumer: Consumer<u8>) -> Result<cpal::Stream, BuildStreamError> {
use crate::user_action::Args;

pub fn setup_audio(
mut consumer: Consumer<u8>,
_args: &Args,
) -> Result<cpal::Stream, BuildStreamError> {
let host = cpal::default_host();
let device = host.default_output_device().unwrap();

let support = device.supported_output_configs().unwrap();

for conf in support {

dbg!(conf);

}



// let config = StreamConfig{
// channels: 1,
// sample_rate: SampleRate(16000),
Expand Down
25 changes: 6 additions & 19 deletions android-mic-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ use user_action::UserAction;
use std::sync::mpsc;

use crate::{
audio::setup_audio,
streamer::WriteError,
tcp_streamer::TcpStreamer,
udp_streamer::UdpStreamer,
user_action::{ask_connection_mode, str_to_connection_mode, SettingsArg},
audio::setup_audio, streamer::WriteError, tcp_streamer::TcpStreamer, udp_streamer::UdpStreamer,
user_action::Args,
};

mod audio;
Expand All @@ -39,14 +36,14 @@ impl App {
const SHARED_BUF_SIZE: usize = 5 * 1024;

fn main() {
let settings_arg = SettingsArg::parse();
let args = Args::parse();

let mut app = App::new();

// Buffer to store received data
let (producer, consumer) = RingBuffer::<u8>::new(SHARED_BUF_SIZE);

match setup_audio(consumer) {
match setup_audio(consumer, &args) {
Err(e) => {
eprintln!("{:?}", e);
return;
Expand All @@ -60,23 +57,13 @@ fn main() {
},
}

let connection_mode = if let Some(mode) = settings_arg.mode {
if let Some(connection_mode) = str_to_connection_mode(mode.as_str()) {
connection_mode
} else {
ask_connection_mode()
}
} else {
ask_connection_mode()
};

let ip = if let Some(ip) = settings_arg.ip {
let ip = if let Some(ip) = args.ip {
ip
} else {
user_action::ask_ip()
};

match connection_mode {
match args.connection_mode {
user_action::ConnectionMode::Udp => {
let streamer = UdpStreamer::new(producer, ip).unwrap();
app.streamer = Some(Box::new(streamer))
Expand Down
47 changes: 18 additions & 29 deletions android-mic-rs/src/user_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{io, sync::mpsc::Sender, thread};

use clap::Parser;
use crossterm::event::{self, Event, KeyCode, KeyEvent};
use strum::{Display, EnumString};

pub enum UserAction {
Quit,
Expand Down Expand Up @@ -34,14 +35,18 @@ pub fn print_avaible_action() {

#[derive(Parser, Debug)]
#[clap(author = "wiiznokes", version, about = "AndroidMic", long_about = None)]
pub struct SettingsArg {
#[arg(long = "ip")]
pub struct Args {
#[arg(short, long, help = "example: -i 192.168.1.79")]
pub ip: Option<String>,

#[arg(short = 'm', long = "mode", value_name = "CONNECTION MODE (UPD/TCP)")]
pub mode: Option<String>,
#[arg(short = 'm', long = "mode", id = "connection mode", help = "UDP or TCP", default_value_t = ConnectionMode::Udp)]
pub connection_mode: ConnectionMode,

#[arg(short = 'c', long = "channel", id = "channel count", help = "1 or 2", default_value_t = ChannelCount::Mono)]
pub channel_count: ChannelCount,
}

// todo: parse it
pub fn ask_ip() -> String {
println!("Please enter the ip of the host (The IP of your PC)");
println!("Help: something like: 192.168.1.79");
Expand All @@ -54,34 +59,18 @@ pub fn ask_ip() -> String {
input.trim().into()
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, EnumString, PartialEq, Display)]
pub enum ConnectionMode {
#[strum(serialize = "udp", serialize = "UDP")]
Udp,
#[strum(serialize = "tcp", serialize = "TCP")]
Tcp,
}

pub fn str_to_connection_mode(str: &str) -> Option<ConnectionMode> {
match str {
"UDP" => Some(ConnectionMode::Udp),
"TCP" => Some(ConnectionMode::Tcp),
_ => None,
}
}

pub fn ask_connection_mode() -> ConnectionMode {
loop {
println!("Please enter the connection mode (UDP/TCP):");

let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");

let trimmed_input = input.trim();
if let Some(connection_mode) = str_to_connection_mode(trimmed_input) {
return connection_mode;
} else {
println!("Invalid input. Please enter 'UDP' or 'TCP'.");
}
}
#[derive(Debug, Clone, EnumString, PartialEq, Display)]
pub enum ChannelCount {
#[strum(serialize = "mono", serialize = "MONO", serialize = "1")]
Mono,
#[strum(serialize = "stereo", serialize = "STEREO", serialize = "2")]
Stereo,
}

0 comments on commit 6093114

Please sign in to comment.