Инициализация движка для WebRTC
This commit is contained in:
64
sfu/sfu.go
Normal file
64
sfu/sfu.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package sfu
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"g365sfu/logger"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pion/interceptor"
|
||||||
|
"github.com/pion/webrtc/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
api *webrtc.API
|
||||||
|
pcConfig webrtc.Configuration
|
||||||
|
)
|
||||||
|
|
||||||
|
// Коллбек для обработки новых ICE кандидатов
|
||||||
|
var OnLocalICECandidate func(roomID, peerID string, candidate webrtc.ICECandidateInit)
|
||||||
|
|
||||||
|
// Ошибки
|
||||||
|
var (
|
||||||
|
ErrRoomNotFound = errors.New("room not found")
|
||||||
|
ErrPeerNotFound = errors.New("peer not found")
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitWebRTCEngines() {
|
||||||
|
publicIP := os.Getenv("PUBLIC_IP")
|
||||||
|
fromPort := os.Getenv("PORT_RANGE_FROM")
|
||||||
|
toPort := os.Getenv("PORT_RANGE_TO")
|
||||||
|
if publicIP == "" || fromPort == "" || toPort == "" {
|
||||||
|
// Если не указаны необходимые переменные окружения, логируем ошибку и завершаем процесс сервера
|
||||||
|
logger.LogErrorMessage("PUBLIC_IP, PORT_RANGE_FROM and PORT_RANGE_TO environment variables must be set")
|
||||||
|
os.Exit(-1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m := &webrtc.MediaEngine{}
|
||||||
|
_ = m.RegisterDefaultCodecs()
|
||||||
|
|
||||||
|
i := &interceptor.Registry{}
|
||||||
|
_ = webrtc.RegisterDefaultInterceptors(m, i)
|
||||||
|
|
||||||
|
se := webrtc.SettingEngine{}
|
||||||
|
_ = se.SetEphemeralUDPPortRange(40000, 50000)
|
||||||
|
|
||||||
|
if publicIP := os.Getenv("PUBLIC_IP"); publicIP != "" {
|
||||||
|
se.SetICEAddressRewriteRules(webrtc.ICEAddressRewriteRule{
|
||||||
|
External: []string{publicIP},
|
||||||
|
AsCandidateType: webrtc.ICECandidateTypeHost,
|
||||||
|
Mode: webrtc.ICEAddressRewriteReplace,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
api = webrtc.NewAPI(
|
||||||
|
webrtc.WithMediaEngine(m),
|
||||||
|
webrtc.WithInterceptorRegistry(i),
|
||||||
|
webrtc.WithSettingEngine(se),
|
||||||
|
)
|
||||||
|
|
||||||
|
pcConfig = webrtc.Configuration{
|
||||||
|
ICEServers: []webrtc.ICEServer{
|
||||||
|
{URLs: []string{"stun:stun.l.google.com:19302"}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user