hylarana_transport/
receiver.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::{
    io::Error,
    net::{IpAddr, SocketAddr},
    sync::Arc,
    thread,
};

use crate::{
    adapter::StreamReceiverAdapterAbstract, MulticastSocket, StreamInfo, StreamInfoKind,
    StreamMultiReceiverAdapter, StreamReceiverAdapter, TransmissionDescriptor,
    TransmissionFragmentDecoder, TransmissionSocket, TransportDescriptor, TransportStrategy,
    UnPackage,
};

enum Socket {
    MulticastSocket(Arc<MulticastSocket>),
    TransmissionSocket(Arc<TransmissionSocket>),
}

pub struct Receiver<T: StreamReceiverAdapterAbstract> {
    socket: Option<Socket>,
    adapter: Arc<T>,
}

impl<T: Default + StreamReceiverAdapterAbstract> Default for Receiver<T> {
    fn default() -> Self {
        Self {
            adapter: Arc::new(T::default()),
            socket: None,
        }
    }
}

impl<T: StreamReceiverAdapterAbstract> Receiver<T> {
    pub fn get_adapter(&self) -> Arc<T> {
        self.adapter.clone()
    }

    pub fn close(&self) {
        self.adapter.close();
    }
}

impl<T: StreamReceiverAdapterAbstract> Drop for Receiver<T> {
    fn drop(&mut self) {
        self.close();

        if let Some(socket) = self.socket.as_ref() {
            match socket {
                Socket::MulticastSocket(socket) => socket.close(),
                Socket::TransmissionSocket(socket) => socket.close(),
            }
        }
    }
}

fn create_multicast_receiver<T>(id: String, addr: SocketAddr) -> Result<Receiver<T>, Error>
where
    T: Default + StreamReceiverAdapterAbstract + 'static,
{
    let mut receiver = Receiver::<T>::default();

    // Creating a multicast receiver
    let socket = Arc::new(MulticastSocket::new(
        match addr.ip() {
            IpAddr::V4(v4) => v4,
            IpAddr::V6(_) => unimplemented!("not supports ipv6 multicast"),
        },
        SocketAddr::new("0.0.0.0".parse().unwrap(), addr.port()),
    )?);

    log::info!("create multicast receiver, id={}, addr={}", id, addr);
    receiver.socket = Some(Socket::MulticastSocket(socket.clone()));

    let mut sequence = 0;
    let adapter_ = Arc::downgrade(&receiver.adapter);
    thread::Builder::new()
        .name("HylaranaStreamMulticastReceiverThread".to_string())
        .spawn(move || {
            while let Some((seq, bytes)) = socket.read() {
                if bytes.is_empty() {
                    break;
                }

                if let Some(adapter) = adapter_.upgrade() {
                    // Check whether the sequence number is continuous, in
                    // order to check whether packet loss has occurred
                    if seq == 0 || seq - 1 == sequence {
                        if let Some((info, package)) = UnPackage::unpack(bytes) {
                            if !adapter.send(package, info.kind, info.flags, info.timestamp) {
                                log::error!("adapter on buf failed.");

                                break;
                            }
                        } else {
                            adapter.lose();
                        }
                    } else {
                        adapter.lose()
                    }

                    sequence = seq;
                } else {
                    break;
                }
            }

            log::warn!("multicast receiver is closed, id={}, addr={}", id, addr);

            if let Some(adapter) = adapter_.upgrade() {
                adapter.close();
            }
        })?;

    Ok(receiver)
}

fn create_srt_receiver<T>(id: String, addr: SocketAddr, mtu: usize) -> Result<Receiver<T>, Error>
where
    T: Default + StreamReceiverAdapterAbstract + 'static,
{
    let mut receiver = Receiver::<T>::default();

    // Create an srt configuration and carry stream information
    let mut opt = TransmissionDescriptor::default();
    opt.fc = 32;
    opt.latency = 20;
    opt.mtu = mtu as u32;
    opt.stream_id = Some(
        StreamInfo {
            kind: StreamInfoKind::Subscriber,
            id: id.clone(),
        }
        .to_string(),
    );

    // Create an srt connection to the server
    let socket = Arc::new(TransmissionSocket::connect(addr, opt)?);

    log::info!("receiver connect to srt server, id={}, addr={}", id, addr);
    receiver.socket = Some(Socket::TransmissionSocket(socket.clone()));

    let mut sequence = 0;
    let adapter_ = Arc::downgrade(&receiver.adapter);
    thread::Builder::new()
        .name("HylaranaStreamReceiverThread".to_string())
        .spawn(move || {
            let mut buf = [0u8; 2000];
            let mut decoder = TransmissionFragmentDecoder::new();

            loop {
                match socket.read(&mut buf) {
                    Ok(size) => {
                        if size == 0 {
                            break;
                        }

                        // All the fragments received from SRT are split and need to be
                        // reassembled here
                        if let Some((seq, bytes)) = decoder.decode(&buf[..size]) {
                            if let Some(adapter) = adapter_.upgrade() {
                                // Check whether the sequence number is continuous, in
                                // order to
                                // check whether packet loss has
                                // occurred
                                if seq == 0 || seq - 1 == sequence {
                                    if let Some((info, package)) = UnPackage::unpack(bytes) {
                                        if !adapter.send(
                                            package,
                                            info.kind,
                                            info.flags,
                                            info.timestamp,
                                        ) {
                                            log::error!("adapter on buf failed.");

                                            break;
                                        }
                                    } else {
                                        adapter.lose();
                                    }
                                } else {
                                    adapter.lose()
                                }

                                sequence = seq;
                            } else {
                                break;
                            }
                        }
                    }
                    Err(e) => {
                        log::error!("{:?}", e);

                        break;
                    }
                }
            }

            log::warn!("srt receiver is closed, id={}, addr={}", id, addr);

            if let Some(adapter) = adapter_.upgrade() {
                adapter.close();
            }
        })?;

    Ok(receiver)
}

fn create_receiver<T: Default + StreamReceiverAdapterAbstract + 'static>(
    id: String,
    options: TransportDescriptor,
) -> Result<Receiver<T>, Error> {
    match options.strategy {
        TransportStrategy::Multicast(addr) => create_multicast_receiver(id, addr),
        TransportStrategy::Direct(addr) | TransportStrategy::Relay(addr) => {
            create_srt_receiver(id, addr, options.mtu)
        }
    }
}

/// Create channel-separated receivers where audio and video channels are
/// received independently, so that a channel can be easily processed separately
/// from different threads.
pub fn create_split_receiver(
    id: String,
    options: TransportDescriptor,
) -> Result<Receiver<StreamMultiReceiverAdapter>, Error> {
    create_receiver::<StreamMultiReceiverAdapter>(id, options)
}

/// Creating a mixed channel is the opposite of separating channels, where the
/// data from all channels is mixed together, and the data received from the
/// receiver is mixed, and you need to process it yourself by data type.
pub fn create_mix_receiver(
    id: String,
    options: TransportDescriptor,
) -> Result<Receiver<StreamReceiverAdapter>, Error> {
    create_receiver::<StreamReceiverAdapter>(id, options)
}