👕 refactor
parent
d93b3036bb
commit
28daa28d90
164
src/main.rs
164
src/main.rs
|
@ -3,7 +3,10 @@ use std::{
|
|||
marker::Send,
|
||||
sync::mpsc,
|
||||
thread,
|
||||
time::Duration,
|
||||
time::{
|
||||
Duration,
|
||||
Instant,
|
||||
},
|
||||
};
|
||||
|
||||
use rayon::ThreadPool;
|
||||
|
@ -112,21 +115,9 @@ impl <'a> Driver <'_>
|
|||
dbg! (&output);
|
||||
match output
|
||||
{
|
||||
TaskOutput::TxCapture ((cap, buf_raw)) =>
|
||||
{
|
||||
self.capture.stop (cap)?;
|
||||
self.ctl.handle_capture_frame (buf_raw).unwrap ();
|
||||
},
|
||||
TaskOutput::TxEncode (enc) =>
|
||||
{
|
||||
let _metadata = self.encoder.stop (enc)?;
|
||||
self.ctl.handle_encoder_finished ();
|
||||
},
|
||||
TaskOutput::TxTransmit ((tx, busy)) =>
|
||||
{
|
||||
self.transmitter.stop (tx)?;
|
||||
self.ctl.handle_network_busy (busy);
|
||||
}
|
||||
TaskOutput::TxCapture (work) => work.finish (self)?,
|
||||
TaskOutput::TxEncode (work) => work.finish (self)?,
|
||||
TaskOutput::TxTransmit (work) => work.finish (self)?,
|
||||
}
|
||||
|
||||
Ok (())
|
||||
|
@ -134,28 +125,13 @@ impl <'a> Driver <'_>
|
|||
|
||||
fn handle_tx_event (&mut self, event: TxPipelineEvent) -> Result <(), TaskError>
|
||||
{
|
||||
let dur_capture = Duration::from_millis (30);
|
||||
let dur_encode = Duration::from_millis (20);
|
||||
let dur_transmit = Duration::from_millis (200);
|
||||
|
||||
let thread_sys = &self.thread_sys;
|
||||
let pool = &self.thread_sys.pool;
|
||||
let send = self.thread_sys.send.clone ();
|
||||
|
||||
match event
|
||||
{
|
||||
TxPipelineEvent::Capture => {
|
||||
let cap = self.capture.start (())?;
|
||||
thread_sys.dispatch (move ||
|
||||
{
|
||||
thread::sleep (dur_capture);
|
||||
let buf_raw = BufRaw
|
||||
{
|
||||
buf: vec![],
|
||||
};
|
||||
TaskOutput::TxCapture ((cap, buf_raw))
|
||||
});
|
||||
},
|
||||
TxPipelineEvent::Capture => CaptureWork::dispatch (self, ())?,
|
||||
TxPipelineEvent::PollEncoder =>
|
||||
{
|
||||
let encoder = self.encoder.try_inner_mut ()?;
|
||||
|
@ -172,24 +148,8 @@ impl <'a> Driver <'_>
|
|||
self.ctl.handle_encoded_packet (None).unwrap ();
|
||||
}
|
||||
}
|
||||
TxPipelineEvent::Encode (buf_raw) =>
|
||||
{
|
||||
let mut encoder = self.encoder.start (EncoderTaskMetadata {})?;
|
||||
thread_sys.dispatch (move ||
|
||||
{
|
||||
thread::sleep (dur_encode);
|
||||
encoder.try_handle_insert_data (&buf_raw.buf).unwrap ();
|
||||
TaskOutput::TxEncode (encoder)
|
||||
});
|
||||
},
|
||||
TxPipelineEvent::Transmit (_buf_enc) => {
|
||||
let tx = self.transmitter.start (())?;
|
||||
thread_sys.dispatch (move ||
|
||||
{
|
||||
thread::sleep (dur_transmit);
|
||||
TaskOutput::TxTransmit ((tx, false))
|
||||
});
|
||||
},
|
||||
TxPipelineEvent::Encode (buf_raw) => EncodeWork::dispatch (self, buf_raw)?,
|
||||
TxPipelineEvent::Transmit (buf_enc) => TransmitWork::dispatch (self, buf_enc)?,
|
||||
}
|
||||
|
||||
Ok (())
|
||||
|
@ -269,9 +229,9 @@ struct EncoderTaskMetadata
|
|||
|
||||
enum TaskOutput
|
||||
{
|
||||
TxCapture ((Capture, BufRaw)),
|
||||
TxEncode (Encoder),
|
||||
TxTransmit ((Transmitter, bool)),
|
||||
TxCapture (CaptureWork),
|
||||
TxEncode (EncodeWork),
|
||||
TxTransmit (TransmitWork),
|
||||
}
|
||||
|
||||
impl fmt::Debug for TaskOutput {
|
||||
|
@ -384,8 +344,104 @@ impl Transmitter
|
|||
/// If Transmitter sees congestion, it may reject the transmission
|
||||
/// until a later time.
|
||||
|
||||
fn try_send (&mut self, _now: std::time::Instant, _buf: &[u8]) -> bool
|
||||
fn try_send (&mut self, _now: Instant, _buf: &[u8]) -> bool
|
||||
{
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
struct CaptureWork
|
||||
{
|
||||
cap: Capture,
|
||||
buf_raw: BufRaw,
|
||||
}
|
||||
|
||||
impl CaptureWork
|
||||
{
|
||||
fn dispatch (driver: &mut Driver, _input: ()) -> Result <(), TaskError>
|
||||
{
|
||||
let metadata = ();
|
||||
let mut cap = driver.capture.start (metadata)?;
|
||||
driver.thread_sys.dispatch (move ||
|
||||
{
|
||||
let dur_capture = Duration::from_millis (30);
|
||||
thread::sleep (dur_capture);
|
||||
let buf_raw = BufRaw
|
||||
{
|
||||
buf: vec![],
|
||||
};
|
||||
TaskOutput::TxCapture (Self {cap, buf_raw})
|
||||
});
|
||||
Ok (())
|
||||
}
|
||||
|
||||
fn finish (self, driver: &mut Driver) -> Result <(), TaskError>
|
||||
{
|
||||
driver.capture.stop (self.cap)?;
|
||||
driver.ctl.handle_capture_frame (self.buf_raw).unwrap ();
|
||||
Ok (())
|
||||
}
|
||||
}
|
||||
|
||||
struct EncodeWork
|
||||
{
|
||||
encoder: Encoder,
|
||||
}
|
||||
|
||||
impl EncodeWork
|
||||
{
|
||||
fn dispatch (driver: &mut Driver, buf_raw: BufRaw) -> Result <(), TaskError>
|
||||
{
|
||||
let mut encoder = driver.encoder.start (EncoderTaskMetadata {})?;
|
||||
driver.thread_sys.dispatch (move ||
|
||||
{
|
||||
let dur_encode = Duration::from_millis (20);
|
||||
thread::sleep (dur_encode);
|
||||
encoder.try_handle_insert_data (&buf_raw.buf).unwrap ();
|
||||
TaskOutput::TxEncode (Self {encoder})
|
||||
});
|
||||
Ok (())
|
||||
}
|
||||
|
||||
fn finish (self, driver: &mut Driver) -> Result <(), TaskError>
|
||||
{
|
||||
let _metadata = driver.encoder.stop (self.encoder)?;
|
||||
driver.ctl.handle_encoder_finished ();
|
||||
Ok (())
|
||||
}
|
||||
}
|
||||
|
||||
struct TransmitWork
|
||||
{
|
||||
tx: Transmitter,
|
||||
busy: bool,
|
||||
}
|
||||
|
||||
impl TransmitWork
|
||||
{
|
||||
fn dispatch (driver: &mut Driver, buf_enc: BufEncoded) -> Result <(), TaskError>
|
||||
{
|
||||
let metadata = ();
|
||||
let mut tx = driver.transmitter.start (metadata)?;
|
||||
driver.thread_sys.dispatch (move ||
|
||||
{
|
||||
let dur_transmit = Duration::from_millis (200);
|
||||
thread::sleep (dur_transmit);
|
||||
let busy = tx.try_send (Instant::now (), &buf_enc.buf);
|
||||
TaskOutput::TxTransmit (Self
|
||||
{
|
||||
tx,
|
||||
busy,
|
||||
})
|
||||
});
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
fn finish (self, driver: &mut Driver) -> Result <(), TaskError>
|
||||
{
|
||||
driver.transmitter.stop (self.tx)?;
|
||||
driver.ctl.handle_network_busy (self.busy);
|
||||
Ok (())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue