Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ICEServers to be configured #152

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ The backend can be configured with the following environment variables.
- `INCLUDE_PUBLIC_IP_IN_NAT_1_TO_1_IP` - Like `NAT_1_TO_1_IP` but autoconfigured
- `INTERFACE_FILTER` - Only use a certain interface for UDP traffic
- `NAT_ICE_CANDIDATE_TYPE` - By default setting a NAT_1_TO_1_IP overrides. Set this to `srflx` to instead append IPs
- `STUN_SERVERS` - List of STUN servers delineated by '|'. Useful if Broadcast Box is running behind a NAT
- `CLIENT_ICE_SERVERS` - A JSON array of ICE Servers, same format as browser. These ICE servers will be used by clients.
- `BACKEND_ICE_SERVERS` - A JSON array of ICE Servers, same format as browser. These ICE servers will be used by server.

- `UDP_MUX_PORT_WHEP` - Like `UDP_MUX_PORT` but only for WHEP traffic
- `UDP_MUX_PORT_WHIP` - Like `UDP_MUX_PORT` but only for WHIP traffic
Expand Down
41 changes: 36 additions & 5 deletions internal/webrtc/webrtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,9 @@ func PopulateMediaEngine(m *webrtc.MediaEngine) error {
func newPeerConnection(api *webrtc.API) (*webrtc.PeerConnection, error) {
cfg := webrtc.Configuration{}

if stunServers := os.Getenv("STUN_SERVERS"); stunServers != "" {
for _, stunServer := range strings.Split(stunServers, "|") {
cfg.ICEServers = append(cfg.ICEServers, webrtc.ICEServer{
URLs: []string{"stun:" + stunServer},
})
if iceServers := os.Getenv("BACKEND_ICE_SERVERS"); iceServers != "" {
if err := json.Unmarshal([]byte(iceServers), &cfg.ICEServers); err != nil {
return nil, err
}
}

Expand Down Expand Up @@ -458,3 +456,36 @@ func GetStreamStatuses() []StreamStatus {

return out
}

func AddICELinks(res http.ResponseWriter) error {
if clientICEServers := os.Getenv("CLIENT_ICE_SERVERS"); clientICEServers != "" {
var iceServers []webrtc.ICEServer
if err := json.Unmarshal([]byte(clientICEServers), &iceServers); err != nil {
return err
}

for _, server := range iceServers {
linkValue := ""

for i, url := range server.URLs {
linkValue += "<" + url + ">; rel=\"ice-server\"; "
if server.Username != "" {
linkValue += "username=\"" + server.Username + "\"; "
}

if cred, ok := server.Credential.(string); ok && cred != "" {
linkValue += "credential=\"" + cred + "\"; "
linkValue += "credential-type=\"" + server.CredentialType.String() + "\""
}

if i+1 != len(server.URLs) {
linkValue += ","
}
}

res.Header().Add("Link", linkValue)
}
}

return nil
}
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
networkTestIntroMessage = "\033[0;33mNETWORK_TEST_ON_START is enabled. If the test fails Broadcast Box will exit.\nSee the README for how to debug or disable NETWORK_TEST_ON_START\033[0m"
networkTestSuccessMessage = "\033[0;32mNetwork Test passed.\nHave fun using Broadcast Box.\033[0m"
networkTestFailedMessage = "\033[0;31mNetwork Test failed.\n%s\nPlease see the README and join Discord for help\033[0m"

deprecatedConfigSTUNServers = "\033[0;31mSTUN_SERVERS has been deprecated, use BACKEND_ICE_SERVERS instead\033[0m"
)

var noBuildDirectoryErr = errors.New("\033[0;31mBuild directory does not exist, run `npm install` and `npm run build` in the web directory.\033[0m")
Expand Down Expand Up @@ -87,6 +89,10 @@ func whipHandler(res http.ResponseWriter, r *http.Request) {

res.Header().Add("Location", "/api/whip")
res.Header().Add("Content-Type", "application/sdp")
if err = webrtc.AddICELinks(res); err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
return
}
res.WriteHeader(http.StatusCreated)
fmt.Fprint(res, answer)
}
Expand Down Expand Up @@ -119,6 +125,10 @@ func whepHandler(res http.ResponseWriter, req *http.Request) {
apiPath := req.Host + strings.TrimSuffix(req.URL.RequestURI(), "whep")
res.Header().Add("Link", `<`+apiPath+"sse/"+whepSessionId+`>; rel="urn:ietf:params:whep:ext:core:server-sent-events"; events="layers"`)
res.Header().Add("Link", `<`+apiPath+"layer/"+whepSessionId+`>; rel="urn:ietf:params:whep:ext:core:layer"`)
if err = webrtc.AddICELinks(res); err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
return
}
res.Header().Add("Location", "/api/whep")
res.Header().Add("Content-Type", "application/sdp")
res.WriteHeader(http.StatusCreated)
Expand Down Expand Up @@ -195,6 +205,13 @@ func corsHandler(next func(w http.ResponseWriter, r *http.Request)) http.Handler
}
}

func checkDeprecatedConfigs() {
if os.Getenv("STUN_SERVERS") != "" {
fmt.Println(deprecatedConfigSTUNServers)
os.Exit(1)
}
}

func main() {
loadConfigs := func() error {
if os.Getenv("APP_ENV") == "development" {
Expand Down Expand Up @@ -231,6 +248,7 @@ func main() {
}
}

checkDeprecatedConfigs()
webrtc.Configure()

if os.Getenv("NETWORK_TEST_ON_START") == "true" {
Expand Down
Loading