srt_protocol/protocol/output/
mod.rs

1use std::{
2    collections::VecDeque,
3    time::{Duration, Instant},
4};
5
6use crate::{
7    connection::ConnectionSettings,
8    packet::*,
9    protocol::time::{TimeBase, Timer},
10};
11
12#[derive(Debug)]
13pub struct Output {
14    remote_sockid: SocketId,
15    time_base: TimeBase,
16    packets: VecDeque<Packet>,
17    keepalive: Timer,
18}
19
20impl Output {
21    pub fn new(settings: &ConnectionSettings) -> Self {
22        Self {
23            remote_sockid: settings.remote_sockid,
24            time_base: TimeBase::new(settings.socket_start_time),
25            packets: VecDeque::new(),
26            keepalive: Timer::new(settings.socket_start_time, Duration::from_secs(1)),
27        }
28    }
29
30    pub fn is_empty(&self) -> bool {
31        self.packets.is_empty()
32    }
33
34    pub fn send_control(&mut self, now: Instant, control: ControlTypes) {
35        self.keepalive.reset(now);
36        self.packets.push_back(Packet::Control(ControlPacket {
37            timestamp: self.time_base.timestamp_from(now),
38            dest_sockid: self.remote_sockid,
39            control_type: control,
40        }));
41    }
42
43    pub fn send_data(&mut self, now: Instant, data: DataPacket) {
44        self.keepalive.reset(now);
45        self.packets.push_back(Packet::Data(data));
46    }
47
48    pub fn ensure_alive(&mut self, now: Instant) {
49        if self.keepalive.check_expired(now).is_some() {
50            self.send_control(now, ControlTypes::KeepAlive)
51        }
52    }
53
54    pub fn pop_packet(&mut self) -> Option<Packet> {
55        self.packets.pop_front()
56    }
57}