feat: ✨ First microphone stream from server to client
This commit is contained in:
parent
7e5a235382
commit
669dbcc0e8
8 changed files with 229 additions and 92 deletions
|
@ -6,12 +6,16 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.4"
|
||||
axum = { version = "0.7.4", features = ["macros"] }
|
||||
axum-server = { version = "0.6.0", features = ["tls-rustls"] }
|
||||
cpal = "0.15.3"
|
||||
futures-util = "0.3.30"
|
||||
rand = "0.8.5"
|
||||
ringbuf = "0.3.3"
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
serde_json = "1.0.114"
|
||||
tokio = { version = "1.36.0", features = ["full"] }
|
||||
tokio-tungstenite = "0.21.0"
|
||||
#tokio-stream = { version = "0.1.15", features = ["full"] }
|
||||
tokio-util = { version = "0.7.10", features = ["full"] }
|
||||
tower-http = { version = "0.5.2", features = ["full"] }
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod routing;
|
||||
pub mod streaming;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AppState{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use back::{routing, AppState};
|
||||
use back::{AppState, routing, streaming};
|
||||
use std::{env, net::SocketAddr};
|
||||
use axum_server::tls_rustls::RustlsConfig;
|
||||
|
||||
|
@ -23,6 +23,7 @@ async fn main() {
|
|||
};
|
||||
let app = routing::routing(axum::extract::State(state)).await;
|
||||
let addr = SocketAddr::from(take_args().parse::<SocketAddr>().unwrap());
|
||||
tokio::spawn(streaming::start());
|
||||
axum_server::bind_rustls(addr, config)
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{AppState, ServerStatus, CoinStatus};
|
||||
use crate::{AppState, ServerStatus, CoinStatus, streaming};
|
||||
use axum::{body::Body, extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
|
||||
use tokio::fs::File;
|
||||
use tokio_util::io::ReaderStream;
|
||||
|
@ -36,7 +36,10 @@ async fn flip_coin() -> impl IntoResponse {
|
|||
(StatusCode::OK, Json(coin_json))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn stream() -> impl IntoResponse {
|
||||
println!("Stream");
|
||||
streaming::start().await;
|
||||
let file = File::open("audios/audio.mp3").await.unwrap();
|
||||
let stream = ReaderStream::new(file);
|
||||
Body::from_stream(stream)
|
||||
|
|
71
back/src/streaming.rs
Normal file
71
back/src/streaming.rs
Normal file
|
@ -0,0 +1,71 @@
|
|||
use std::{mem::MaybeUninit, sync::Arc};
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use ringbuf::{Consumer, HeapRb, Producer, SharedRb};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use futures_util::SinkExt;
|
||||
use tokio_tungstenite::WebSocketStream;
|
||||
|
||||
pub async fn start() {
|
||||
let socket = TcpListener::bind("127.0.0.1:2424").await.unwrap();
|
||||
while let Ok((tcp_stream, _)) = socket.accept().await {
|
||||
println!("Dude Someone Triggered");
|
||||
let ring = HeapRb::<f32>::new(1000000);
|
||||
let (producer, consumer) = ring.split();
|
||||
let ws_stream = tokio_tungstenite::accept_async(tcp_stream).await.unwrap();
|
||||
|
||||
tokio::spawn(record(producer));
|
||||
std::thread::sleep(std::time::Duration::from_secs(3));
|
||||
tokio::spawn(stream(ws_stream, consumer));
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn stream(mut ws_stream:WebSocketStream<TcpStream>, mut consumer: Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>) {
|
||||
println!("Waiting");
|
||||
loop {
|
||||
if !consumer.is_empty() {
|
||||
match consumer.pop() {
|
||||
Some(data) => {
|
||||
ws_stream.send(data.to_string().into()).await.unwrap();
|
||||
}
|
||||
None => {
|
||||
//ws_stream.send(0.0.to_string().into()).await.unwrap();
|
||||
}
|
||||
}
|
||||
ws_stream.flush().await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn record(mut producer: Producer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>) {
|
||||
println!("Hello, world!");
|
||||
let host = cpal::default_host();
|
||||
let input_device = host.default_input_device().unwrap();
|
||||
|
||||
println!("Input Device: {}", input_device.name().unwrap());
|
||||
|
||||
let config:cpal::StreamConfig = input_device.default_input_config().unwrap().into();
|
||||
|
||||
let input_data_fn = move |data: &[f32], _:&cpal::InputCallbackInfo| {
|
||||
for &sample in data {
|
||||
match producer.push(sample) {
|
||||
Ok(_) => {},
|
||||
Err(_) => {},
|
||||
}
|
||||
println!("{}", sample);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let input_stream = input_device.build_input_stream(&config, input_data_fn, err_fn, None).unwrap();
|
||||
|
||||
|
||||
println!("STREAMIN");
|
||||
input_stream.play().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(100));
|
||||
println!("DONE I HOPE");
|
||||
}
|
||||
|
||||
fn err_fn(err: cpal::StreamError) {
|
||||
eprintln!("Something Happened: {}", err);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue