tx5_go_pion/
evt.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
use crate::*;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use tx5_go_pion_sys::Event as SysEvent;
use tx5_go_pion_sys::API;

/// PeerConnectionState events.
#[derive(Debug, Clone, Copy)]
pub enum PeerConnectionState {
    /// <https://pkg.go.dev/github.com/pion/webrtc/v3#PeerConnectionState>
    New = 0x01,

    /// <https://pkg.go.dev/github.com/pion/webrtc/v3#PeerConnectionState>
    Connecting = 0x02,

    /// <https://pkg.go.dev/github.com/pion/webrtc/v3#PeerConnectionState>
    Connected = 0x03,

    /// <https://pkg.go.dev/github.com/pion/webrtc/v3#PeerConnectionState>
    Disconnected = 0x04,

    /// <https://pkg.go.dev/github.com/pion/webrtc/v3#PeerConnectionState>
    Failed = 0x05,

    /// <https://pkg.go.dev/github.com/pion/webrtc/v3#PeerConnectionState>
    Closed = 0x06,
}

impl PeerConnectionState {
    /// Construct from a raw integer value.
    pub fn from_raw(raw: usize) -> Self {
        match raw {
            0x01 => PeerConnectionState::New,
            0x02 => PeerConnectionState::Connecting,
            0x03 => PeerConnectionState::Connected,
            0x04 => PeerConnectionState::Disconnected,
            0x05 => PeerConnectionState::Failed,
            0x06 => PeerConnectionState::Closed,
            _ => panic!("invalid raw PeerConnectionState value: {raw}"),
        }
    }
}

/// Incoming events related to a PeerConnection.
#[derive(Debug)]
pub enum PeerConnectionEvent {
    /// PeerConnection error.
    Error(Error),

    /// PeerConnectionState event.
    State(PeerConnectionState),

    /// Received a trickle ICE candidate.
    ICECandidate(GoBuf),

    /// Received an incoming data channel.
    /// Warning: This returns an unbounded channel,
    /// you should process this as quickly and synchronously as possible
    /// to avoid a backlog filling up memory.
    DataChannel(
        DataChannel,
        tokio::sync::mpsc::UnboundedReceiver<DataChannelEvent>,
    ),
}

impl From<Error> for PeerConnectionEvent {
    fn from(err: Error) -> Self {
        Self::Error(err)
    }
}

/// Incoming events related to a DataChannel.
#[derive(Debug)]
pub enum DataChannelEvent {
    /// Data channel error.
    Error(Error),

    /// Data channel has transitioned to "open".
    Open,

    /// Data channel has transitioned to "closed".
    Close,

    /// Received incoming message on the data channel.
    Message(GoBuf),

    /// Data channel buffered amount is now low.
    BufferedAmountLow,
}

impl From<Error> for DataChannelEvent {
    fn from(err: Error) -> Self {
        Self::Error(err)
    }
}

#[inline]
pub(crate) fn init_evt_manager() {
    // ensure initialization
    let _ = &*MANAGER;
}

enum Cmd {
    Evt(SysEvent),
    PeerReg(usize, peer_con::WeakPeerCon),
    PeerUnreg(usize),
    DataReg(usize, data_chan::WeakDataChan),
    DataUnreg(usize),
}

struct Manager {
    rt_send: std::sync::mpsc::Sender<Cmd>,
}

impl Manager {
    pub fn new() -> Self {
        struct D;

        impl Drop for D {
            fn drop(&mut self) {
                tracing::error!("tx5-go-pion offload EVENT LOOP EXITED");
            }
        }

        let drop_trace = D;

        let (rt_send, rt_recv) = std::sync::mpsc::channel();

        // we need to offload the event handling to another thread
        // because it's not safe to invoke other go apis within the
        // same sync call:
        // https://github.com/pion/webrtc/issues/2404
        std::thread::Builder::new()
            .name("tx5-evt-offload".to_string())
            .spawn(move || {
                let _drop_trace = drop_trace;

                let mut peer_map: HashMap<usize, peer_con::WeakPeerCon> =
                    HashMap::new();
                let mut data_map: HashMap<usize, data_chan::WeakDataChan> =
                    HashMap::new();

                trait Wk<E> {
                    fn se(&self, evt: E) -> Result<()>;
                }

                impl Wk<PeerConnectionEvent> for peer_con::WeakPeerCon {
                    fn se(&self, evt: PeerConnectionEvent) -> Result<()> {
                        self.send_evt(evt)
                    }
                }

                impl Wk<DataChannelEvent> for data_chan::WeakDataChan {
                    fn se(&self, evt: DataChannelEvent) -> Result<()> {
                        self.send_evt(evt)
                    }
                }

                fn smap<E, W: Wk<E>>(
                    map: &mut HashMap<usize, W>,
                    id: usize,
                    evt: E,
                ) {
                    if let std::collections::hash_map::Entry::Occupied(o) =
                        map.entry(id)
                    {
                        if o.get().se(evt).is_err() {
                            o.remove();
                        }
                    }
                }

                while let Ok(cmd) = rt_recv.recv() {
                    match cmd {
                        Cmd::Evt(evt) => match evt {
                            SysEvent::Error(error) => {
                                tracing::error!(
                                    ?error,
                                    "tx5-go-pion error event"
                                );
                            }
                            SysEvent::PeerConICECandidate {
                                peer_con_id,
                                candidate,
                            } => {
                                smap(
                                    &mut peer_map,
                                    peer_con_id,
                                    PeerConnectionEvent::ICECandidate(GoBuf(
                                        candidate,
                                    )),
                                );
                            }
                            SysEvent::PeerConStateChange {
                                peer_con_id,
                                peer_con_state,
                            } => {
                                smap(
                                    &mut peer_map,
                                    peer_con_id,
                                    PeerConnectionEvent::State(
                                        PeerConnectionState::from_raw(
                                            peer_con_state,
                                        ),
                                    ),
                                );
                            }
                            SysEvent::PeerConDataChan {
                                peer_con_id,
                                data_chan_id,
                            } => {
                                let (chan, recv) =
                                    DataChannel::new(data_chan_id);
                                smap(
                                    &mut peer_map,
                                    peer_con_id,
                                    PeerConnectionEvent::DataChannel(
                                        chan, recv,
                                    ),
                                );
                            }
                            SysEvent::DataChanClose(data_chan_id) => {
                                smap(
                                    &mut data_map,
                                    data_chan_id,
                                    DataChannelEvent::Close,
                                );
                            }
                            SysEvent::DataChanOpen(data_chan_id) => {
                                smap(
                                    &mut data_map,
                                    data_chan_id,
                                    DataChannelEvent::Open,
                                );
                            }
                            SysEvent::DataChanMessage {
                                data_chan_id,
                                buffer_id,
                            } => {
                                let buf = GoBuf(buffer_id);
                                smap(
                                    &mut data_map,
                                    data_chan_id,
                                    DataChannelEvent::Message(buf),
                                );
                            }
                            SysEvent::DataChanBufferedAmountLow(
                                data_chan_id,
                            ) => {
                                smap(
                                    &mut data_map,
                                    data_chan_id,
                                    DataChannelEvent::BufferedAmountLow,
                                );
                            }
                        },
                        Cmd::PeerReg(id, peer_con) => {
                            peer_map.insert(id, peer_con);
                        }
                        Cmd::PeerUnreg(id) => {
                            peer_map.remove(&id);
                        }
                        Cmd::DataReg(id, data_chan) => {
                            data_map.insert(id, data_chan);
                        }
                        Cmd::DataUnreg(id) => {
                            data_map.remove(&id);
                        }
                    }
                }
            })
            .expect("Failed to spawn offload thread");

        Self { rt_send }
    }

    pub fn register_peer_con(
        &self,
        id: usize,
        peer_con: peer_con::WeakPeerCon,
    ) {
        let _ = self.rt_send.send(Cmd::PeerReg(id, peer_con));
    }

    pub fn unregister_peer_con(&self, id: usize) {
        let _ = self.rt_send.send(Cmd::PeerUnreg(id));
    }

    pub fn register_data_chan(
        &self,
        id: usize,
        data_chan: data_chan::WeakDataChan,
    ) {
        let _ = self.rt_send.send(Cmd::DataReg(id, data_chan));
    }

    pub fn unregister_data_chan(&self, id: usize) {
        let _ = self.rt_send.send(Cmd::DataUnreg(id));
    }

    pub fn handle_event(&self, evt: SysEvent) {
        let _ = self.rt_send.send(Cmd::Evt(evt));
    }
}
pub(crate) fn register_peer_con(id: usize, peer_con: peer_con::WeakPeerCon) {
    MANAGER.register_peer_con(id, peer_con);
}

pub(crate) fn unregister_peer_con(id: usize) {
    MANAGER.unregister_peer_con(id);
}

pub(crate) fn register_data_chan(
    id: usize,
    data_chan: data_chan::WeakDataChan,
) {
    MANAGER.register_data_chan(id, data_chan);
}

pub(crate) fn unregister_data_chan(id: usize) {
    MANAGER.unregister_data_chan(id);
}

static MANAGER: Lazy<Manager> = Lazy::new(|| {
    unsafe {
        API.on_event(move |evt| {
            MANAGER.handle_event(evt);
        });
    }

    Manager::new()
});