Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.

Commit

Permalink
Add ephemeral-udp-port-range support
Browse files Browse the repository at this point in the history
When running inside Docker we want to only listen on a range of ports.
This makes it much easier to expose from the container.

Relates to #62
  • Loading branch information
Sean-Der committed Mar 22, 2020
1 parent 4a77e17 commit 91f6502
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.
11 changes: 10 additions & 1 deletion cmd/sfu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ import (
)

func init() {
var icePortStart, icePortEnd uint16

if len(conf.WebRTC.ICEPortRange) == 2 {
icePortStart = conf.WebRTC.ICEPortRange[0]
icePortEnd = conf.WebRTC.ICEPortRange[1]
}

log.Init(conf.Log.Level)
rtc.Init(conf.Rtp.Port, conf.WebRTC.ICE, "", "")
if err := rtc.Init(conf.Rtp.Port, conf.WebRTC.ICE, icePortStart, icePortEnd, "", ""); err != nil {
panic(err)
}
}

func main() {
Expand Down
3 changes: 3 additions & 0 deletions configs/sfu.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ addr = "127.0.0.1"
# ice = ["stun:stun.l.google.com:19302"]
# ice = ["stun:stun.stunprotocol.org:3478"]

# Range of ports that ion accepts WebRTC traffic on
ephemeral-udp-port-range = []

[rtp]
# listen port
port = 6666
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services:
volumes:
- "./docker/sfu.toml:/configs/sfu.toml"
ports:
- 6666:6666 # rtp
- "5000-5200:5000-5200/udp"
depends_on:
- nats
- etcd
Expand Down
7 changes: 4 additions & 3 deletions docker/sfu.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ dc = "dc1"
addr = "127.0.0.1"

[webrtc]
# if ion behind nat, uncomment one
# ice = ["stun:stun.l.google.com:19302"]
# ice = ["stun:stun.stunprotocol.org:3478"]
ice = ["stun:stun.l.google.com:19302"]

# Range of ports that ion accepts WebRTC traffic on
ephemeral-udp-port-range = [5000, 6000]

[rtp]
# listen port
Expand Down
3 changes: 2 additions & 1 deletion pkg/conf/sfu/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ type nats struct {
}

type webrtc struct {
ICE []string `mapstructure:"ice"`
ICE []string `mapstructure:"ice"`
ICEPortRange []uint16 `mapstructure:"ephemeral-udp-port-range"`
}

type rtp struct {
Expand Down
8 changes: 5 additions & 3 deletions pkg/rtc/rtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ var (
)

// Init port and ice urls
func Init(port int, ices []string, kcpKey, kcpSalt string) error {
func Init(port int, ices []string, icePortStart, icePortEnd uint16, kcpKey, kcpSalt string) error {

//init ice urls and trickle-ICE
transport.InitWebRTC(ices, true, false)
//init ice urls and ICE settings
if err := transport.InitWebRTC(ices, icePortStart, icePortEnd); err != nil {
return err
}

// show stat about all pipelines
go check()
Expand Down
12 changes: 0 additions & 12 deletions pkg/rtc/transport/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,3 @@ func TestKvOK(t *testing.T) {
t.Fatal("flag is not true!")
}
}

func TestValUpper(t *testing.T) {
m := make(map[string]interface{})
m["abc"] = "true"
if ValUpper(m, "abc") != "TRUE" {
t.Fatal("val is not true!")
}
m["abc"] = 1
if ValUpper(m, "abc") != "" {
t.Fatal("ValUpper error!")
}
}
13 changes: 7 additions & 6 deletions pkg/rtc/transport/webrtctransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ var (
)

// InitWebRTC init WebRTCTransport setting
func InitWebRTC(ices []string, trickICE bool, liteICE bool) {
func InitWebRTC(ices []string, icePortStart, icePortEnd uint16) error {
var err error
if icePortStart != 0 || icePortEnd != 0 {
err = setting.SetEphemeralUDPPortRange(icePortStart, icePortEnd)
}

cfg.ICEServers = []webrtc.ICEServer{
{
URLs: ices,
},
}
if trickICE {
setting.SetTrickle(trickICE)
} else {
setting.SetLite(liteICE)
}
return err
}

// WebRTCTransport contains pc incoming and outgoing tracks
Expand Down

0 comments on commit 91f6502

Please sign in to comment.