hylarana_transport/
adapter.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::{
    fmt,
    sync::{
        atomic::{AtomicBool, AtomicU8},
        mpsc::{channel, Receiver, Sender},
    },
};

use bytes::{Bytes, BytesMut};
use hylarana_common::atomic::{AtomicOption, EasyAtomic};
use parking_lot::Mutex;

struct Channel<T>(Sender<Option<T>>, Mutex<Receiver<Option<T>>>);

impl<T> Default for Channel<T> {
    fn default() -> Self {
        let (tx, rx) = channel();
        Self(tx, Mutex::new(rx))
    }
}

impl<T> Channel<T> {
    fn send(&self, item: Option<T>) -> bool {
        self.0.send(item).is_ok()
    }

    fn recv(&self) -> Option<T> {
        self.1.lock().recv().ok().flatten()
    }
}

#[derive(Default)]
struct PacketFilter {
    initialized: AtomicBool,
    readable: AtomicBool,
}

impl PacketFilter {
    fn filter(&self, flag: i32, keyframe: bool) -> bool {
        // First check whether the decoder has been initialized. Here, it is judged
        // whether the configuration information has arrived. If the configuration
        // information has arrived, the decoder initialization is marked as completed.
        if !self.initialized.get() {
            if flag != BufferFlag::Config as i32 {
                return false;
            }

            self.initialized.update(true);
            return true;
        }

        // The configuration information only needs to be filled into the decoder once.
        // If it has been initialized, it means that the configuration information has
        // been received. It is meaningless to receive it again later. Here, duplicate
        // configuration information is filtered out.
        if flag == BufferFlag::Config as i32 {
            return false;
        }

        // The audio does not have keyframes
        if keyframe {
            // Check whether the current stream is in a readable state. When packet loss
            // occurs, the entire stream should be paused and wait for the next key frame to
            // arrive.
            if !self.readable.get() {
                if flag == BufferFlag::KeyFrame as i32 {
                    self.readable.update(true);
                } else {
                    return false;
                }
            }
        }

        true
    }

    fn loss(&self) {
        self.readable.update(false);
    }
}

#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BufferFlag {
    KeyFrame = 1,
    Config = 2,
    EndOfStream = 4,
    Partial = 8,
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamKind {
    Video = 0,
    Audio = 1,
}

#[derive(Debug, Clone, Copy)]
pub struct StreamKindTryFromError;

impl std::error::Error for StreamKindTryFromError {}

impl fmt::Display for StreamKindTryFromError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "StreamKindTryFromError")
    }
}

impl TryFrom<u8> for StreamKind {
    type Error = StreamKindTryFromError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Ok(match value {
            0 => Self::Video,
            1 => Self::Audio,
            _ => return Err(StreamKindTryFromError),
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamBufferInfo {
    Video(i32, u64),
    Audio(i32, u64),
}

#[derive(Default)]
struct ConfigCache {
    video: AtomicOption<BytesMut>,
    audio: AtomicOption<BytesMut>,
}

#[derive(Default)]
struct AutoInsertOfConfigInfo {
    audio: AtomicU8,
}

impl AutoInsertOfConfigInfo {
    const AUDIO_INTERVAL: u8 = 30;
}

/// Video Audio Streaming Send Processing
///
/// Because the receiver will normally join the stream in the middle of the
/// stream, and in the face of this situation, it is necessary to process the
/// sps and pps as well as the key frame information.
#[derive(Default)]
pub struct StreamSenderAdapter {
    channel: Channel<(BytesMut, StreamKind, i32, u64)>,
    aioci: AutoInsertOfConfigInfo,
    config: ConfigCache,
}

impl StreamSenderAdapter {
    pub(crate) fn close(&self) {
        self.channel.send(None);
    }

    // h264 decoding any p-frames and i-frames requires sps and pps
    // frames, so the configuration frames are saved here, although it
    // should be noted that the configuration frames will only be
    // generated once.
    pub fn send(&self, buf: BytesMut, info: StreamBufferInfo) -> bool {
        if buf.is_empty() {
            return true;
        }

        match info {
            StreamBufferInfo::Video(flags, timestamp) => {
                if flags == BufferFlag::Config as i32 {
                    self.config.video.swap(Some(buf.clone()));
                }

                // Add SPS and PPS units in front of each keyframe (only use android)
                if flags == BufferFlag::KeyFrame as i32 {
                    if let Some(config) = self.config.video.get() {
                        if !self.channel.send(Some((
                            config.clone(),
                            StreamKind::Video,
                            BufferFlag::Config as i32,
                            timestamp,
                        ))) {
                            return false;
                        }
                    }
                }

                self.channel
                    .send(Some((buf, StreamKind::Video, flags, timestamp)))
            }
            StreamBufferInfo::Audio(flags, timestamp) => {
                if flags == BufferFlag::Config as i32 {
                    self.config.audio.swap(Some(buf.clone()));
                }

                // Insert a configuration package into every 30 audio packages.
                let count = self.aioci.audio.get();
                self.aioci
                    .audio
                    .update(if count == AutoInsertOfConfigInfo::AUDIO_INTERVAL {
                        if let Some(config) = self.config.audio.get() {
                            if !self.channel.send(Some((
                                config.clone(),
                                StreamKind::Audio,
                                BufferFlag::Config as i32,
                                timestamp,
                            ))) {
                                return false;
                            }
                        }

                        0
                    } else {
                        count + 1
                    });

                self.channel
                    .send(Some((buf, StreamKind::Audio, flags, timestamp)))
            }
        }
    }

    pub fn next(&self) -> Option<(BytesMut, StreamKind, i32, u64)> {
        self.channel.recv()
    }
}

pub trait StreamReceiverAdapterAbstract: Sync + Send {
    fn send(&self, buf: Bytes, kind: StreamKind, flags: i32, timestamp: u64) -> bool;
    fn close(&self);
    fn lose(&self);
}

#[derive(Default)]
struct Filter {
    video: PacketFilter,
    audio: PacketFilter,
}

/// Video Audio Streaming Receiver Processing
///
/// The main purpose is to deal with cases where packet loss occurs at the
/// receiver side, since the SRT communication protocol does not completely
/// guarantee no packet loss.
#[derive(Default)]
pub struct StreamReceiverAdapter {
    channel: Channel<(Bytes, StreamKind, i32, u64)>,
    filter: Filter,
}

impl StreamReceiverAdapter {
    pub fn next(&self) -> Option<(Bytes, StreamKind, i32, u64)> {
        self.channel.recv()
    }
}

impl StreamReceiverAdapterAbstract for StreamReceiverAdapter {
    fn close(&self) {
        self.channel.send(None);
    }

    fn lose(&self) {
        self.filter.video.loss();

        log::warn!(
            "Packet loss has occurred and the data stream is currently \
            paused, waiting for the key frame to arrive.",
        );
    }

    /// As soon as a keyframe is received, the keyframe is cached, and when a
    /// packet loss occurs, the previous keyframe is retransmitted directly into
    /// the decoder.
    fn send(&self, buf: Bytes, kind: StreamKind, flags: i32, timestamp: u64) -> bool {
        if buf.is_empty() {
            return true;
        }

        if match kind {
            StreamKind::Video => self.filter.video.filter(flags, true),
            StreamKind::Audio => self.filter.audio.filter(flags, false),
        } {
            return self.channel.send(Some((buf, kind, flags, timestamp)));
        }

        true
    }
}

#[derive(Default)]
struct MultiChannels {
    video: Channel<(Bytes, i32, u64)>,
    audio: Channel<(Bytes, i32, u64)>,
}

/// Video Audio Streaming Receiver Processing
///
/// The main purpose is to deal with cases where packet loss occurs at the
/// receiver side, since the SRT communication protocol does not completely
/// guarantee no packet loss.
#[derive(Default)]
pub struct StreamMultiReceiverAdapter {
    channel: MultiChannels,
    filter: Filter,
}

impl StreamMultiReceiverAdapter {
    pub fn next(&self, kind: StreamKind) -> Option<(Bytes, i32, u64)> {
        match kind {
            StreamKind::Video => self.channel.video.recv(),
            StreamKind::Audio => self.channel.audio.recv(),
        }
    }
}

impl StreamReceiverAdapterAbstract for StreamMultiReceiverAdapter {
    fn close(&self) {
        self.channel.video.send(None);
        self.channel.audio.send(None);
    }

    fn lose(&self) {
        self.filter.video.loss();

        log::warn!(
            "Packet loss has occurred and the data stream is currently \
            paused, waiting for the key frame to arrive.",
        );
    }

    /// As soon as a keyframe is received, the keyframe is cached, and when a
    /// packet loss occurs, the previous keyframe is retransmitted directly into
    /// the decoder.
    fn send(&self, buf: Bytes, kind: StreamKind, flags: i32, timestamp: u64) -> bool {
        if buf.is_empty() {
            return true;
        }

        match kind {
            StreamKind::Video => {
                if self.filter.video.filter(flags, true) {
                    return self.channel.video.send(Some((buf, flags, timestamp)));
                }
            }
            StreamKind::Audio => {
                if self.filter.audio.filter(flags, false) {
                    return self.channel.audio.send(Some((buf, flags, timestamp)));
                }
            }
        }

        true
    }
}