修复iceserver配置能力

This commit is contained in:
langhuihui
2023-04-15 08:49:54 +08:00
parent 7f9f901198
commit 4cf8d33477
2 changed files with 28 additions and 5 deletions

View File

@@ -20,6 +20,18 @@ webrtc:
port: tcp:9000 # 可以是udp:8000-9000 范围端口也可以udp:9000 单个端口
pli: 2s # 2s
```
### ICE服务器配置格式
```yaml
webrtc:
iceservers:
- urls:
- stun:stun.l.google.com:19302
- turn:turn.example.org
username: user
credential: pass
```
### 本地测试无需修改配置如果远程访问则需要配置publicip

21
main.go
View File

@@ -59,7 +59,7 @@ var (
type WebRTCConfig struct {
config.Publish
config.Subscribe
ICEServers []string
ICEServers []ICEServer
PublicIP []string
Port string `default:"tcp:9000"`
PLI time.Duration `default:"2s"` // 视频流丢包后发送PLI请求
@@ -71,12 +71,17 @@ type WebRTCConfig struct {
func (conf *WebRTCConfig) OnEvent(event any) {
switch event.(type) {
case engine.FirstConfig:
if len(conf.ICEServers) > 0 {
for i := range conf.ICEServers {
b, _ := conf.ICEServers[i].MarshalJSON()
conf.ICEServers[i].UnmarshalJSON(b)
}
}
webrtc.RegisterCodecs(&conf.m)
i := &interceptor.Registry{}
if len(conf.PublicIP) > 0 {
conf.s.SetNAT1To1IPs(conf.PublicIP, ICECandidateTypeHost)
}
protocol, port, _ := strings.Cut(conf.Port, ":")
if protocol == "tcp" {
tcpport, _ := strconv.Atoi(port)
@@ -126,7 +131,9 @@ func (conf *WebRTCConfig) Play_(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadAll(r.Body)
var suber WebRTCSubscriber
suber.SDP = string(bytes)
if suber.PeerConnection, err = conf.api.NewPeerConnection(Configuration{}); err != nil {
if suber.PeerConnection, err = conf.api.NewPeerConnection(Configuration{
ICEServers: conf.ICEServers,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -156,7 +163,9 @@ func (conf *WebRTCConfig) Push_(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadAll(r.Body)
var puber WebRTCPublisher
puber.SDP = string(bytes)
if puber.PeerConnection, err = conf.api.NewPeerConnection(Configuration{}); err != nil {
if puber.PeerConnection, err = conf.api.NewPeerConnection(Configuration{
ICEServers: conf.ICEServers,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -235,7 +244,9 @@ func (conf *WebRTCConfig) Batch(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadAll(r.Body)
var suber WebRTCBatcher
suber.SDP = string(bytes)
if suber.PeerConnection, err = conf.api.NewPeerConnection(Configuration{}); err != nil {
if suber.PeerConnection, err = conf.api.NewPeerConnection(Configuration{
ICEServers: conf.ICEServers,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}