]> code.octet-stream.net Git - m17rt/blob - m17core/src/reflector/convert.rs
Voice UDP to RF conversion, and better sample rate management in codec2
[m17rt] / m17core / src / reflector / convert.rs
1 //! Utilities for converting streams between UDP and RF representations
2
3 use crate::protocol::{LsfFrame, StreamFrame};
4
5 use super::packet::Voice;
6
7 /// Accepts `Voice` packets from a reflector and turns them into LSF and Stream frames.
8 ///
9 /// This is the format required for the voice data to cross the KISS protocol boundary.
10 pub struct VoiceToRf {
11 /// Link Setup most recently acquired
12 lsf: Option<LsfFrame>,
13 /// Which LICH part we are going to emit next, 0-5
14 lich_cnt: usize,
15 }
16
17 impl VoiceToRf {
18 pub fn new() -> Self {
19 Self {
20 lsf: None,
21 lich_cnt: 0,
22 }
23 }
24
25 /// For a Voice packet received from a reflector, return the frames that would be transmitted
26 /// on RF, including by reconstructing the LICH parts of the stream frame.
27 ///
28 /// If this is the start of a new or different stream transmission, this returns the Link Setup
29 /// Frame which comes first, then the first associated Stream frame.
30 ///
31 /// If this is a continuation of a transmission matching the previous LSF, then it returns only
32 /// the Stream frame.
33 pub fn next(&mut self, voice: &Voice) -> (Option<LsfFrame>, StreamFrame) {
34 let this_lsf = voice.link_setup_frame();
35 let emit_lsf = if Some(&this_lsf) != self.lsf.as_ref() {
36 self.lsf = Some(this_lsf.clone());
37 self.lich_cnt = 0;
38 true
39 } else {
40 false
41 };
42 let lsf = self.lsf.as_ref().unwrap();
43 let stream = StreamFrame {
44 lich_idx: self.lich_cnt as u8,
45 lich_part: (&lsf.0[self.lich_cnt * 5..(self.lich_cnt + 1) * 5])
46 .try_into()
47 .unwrap(),
48 frame_number: voice.frame_number(),
49 end_of_stream: voice.is_end_of_stream(),
50 stream_data: voice.payload().try_into().unwrap(),
51 };
52 let lsf = if emit_lsf { self.lsf.clone() } else { None };
53 if voice.is_end_of_stream() {
54 self.lsf = None;
55 }
56 (lsf, stream)
57 }
58 }