-
Notifications
You must be signed in to change notification settings - Fork 9
/
udpmux.go
53 lines (42 loc) · 916 Bytes
/
udpmux.go
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
package sfu
import (
"context"
"github.com/pion/ice/v3"
)
type UDPMux struct {
Port int
mux *ice.MultiUDPMuxDefault
context context.Context
cancel context.CancelFunc
}
func NewUDPMux(ctx context.Context, port int) *UDPMux {
localCtx, cancel := context.WithCancel(ctx)
opts := []ice.UDPMuxFromPortOption{
ice.UDPMuxFromPortWithReadBufferSize(25_000_000),
ice.UDPMuxFromPortWithWriteBufferSize(25_000_000),
ice.UDPMuxFromPortWithNetworks(ice.NetworkTypeUDP4),
ice.UDPMuxFromPortWithLoopback(),
}
mux, err := ice.NewMultiUDPMuxFromPort(port, opts...)
if err != nil {
panic(err)
}
go func() {
defer mux.Close()
<-localCtx.Done()
cancel()
}()
return &UDPMux{
Port: port,
mux: mux,
context: localCtx,
cancel: cancel,
}
}
func (u *UDPMux) Mux() *ice.MultiUDPMuxDefault {
return u.mux
}
func (u *UDPMux) Close() error {
u.cancel()
return u.mux.Close()
}