Browse Source

📦 NEW: 暴露插件对象WebRTCPlugin

v4
dexter 2 years ago
parent
commit
cfafbd73d3
  1. 40
      main.go
  2. 7
      subscriber.go

40
main.go

@ -84,9 +84,9 @@ func (conf *WebRTCConfig) OnEvent(event any) {
Port: conf.IceUdpMux, Port: conf.IceUdpMux,
}) })
if err != nil { if err != nil {
plugin.Fatal("webrtc listener udp", zap.Error(err)) WebRTCPlugin.Fatal("webrtc listener udp", zap.Error(err))
} }
plugin.Info("webrtc start listen", zap.Int("port", conf.IceUdpMux)) WebRTCPlugin.Info("webrtc start listen", zap.Int("port", conf.IceUdpMux))
conf.s.SetICEUDPMux(NewICEUDPMux(nil, udpListener)) conf.s.SetICEUDPMux(NewICEUDPMux(nil, udpListener))
} }
@ -118,7 +118,7 @@ func (conf *WebRTCConfig) Play_(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
if err = plugin.Subscribe(streamPath, &suber); err != nil { if err = WebRTCPlugin.Subscribe(streamPath, &suber); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
@ -152,7 +152,7 @@ func (conf *WebRTCConfig) Push_(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
if err = plugin.Publish(streamPath, &puber); err != nil { if err = WebRTCPlugin.Publish(streamPath, &puber); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
@ -174,4 +174,34 @@ var webrtcConfig = &WebRTCConfig{
PLI: time.Second * 2, PLI: time.Second * 2,
} }
var plugin = engine.InstallPlugin(webrtcConfig) var WebRTCPlugin = engine.InstallPlugin(webrtcConfig)
func (conf *WebRTCConfig) Batch(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/sdp")
bytes, err := ioutil.ReadAll(r.Body)
var suber WebRTCSubscriber
suber.SDP = string(bytes)
if suber.PeerConnection, err = conf.api.NewPeerConnection(Configuration{}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
suber.OnICECandidate(func(ice *ICECandidate) {
if ice != nil {
suber.Info(ice.ToJSON().Candidate)
}
})
if err = suber.SetRemoteDescription(SessionDescription{Type: SDPTypeOffer, SDP: suber.SDP}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// if err = WebRTCPlugin.Subscribe(streamPath, &suber); err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// return
// }
if sdp, err := suber.GetAnswer(); err == nil {
w.Write([]byte(sdp))
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}

7
subscriber.go

@ -78,3 +78,10 @@ func (suber *WebRTCSubscriber) OnEvent(event any) {
suber.Subscriber.OnEvent(event) suber.Subscriber.OnEvent(event)
} }
} }
// WebRTCBatcher 批量订阅者
type WebRTCBatcher struct {
WebRTCIO
PageSize int
PageNum int
}

Loading…
Cancel
Save