mirror of
https://github.com/Monibuca/plugin-webrtc.git
synced 2025-10-05 06:46:57 +08:00
📦 NEW: 增加批量订阅功能
This commit is contained in:
96
batcher.go
Normal file
96
batcher.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package webrtc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
. "github.com/pion/webrtc/v3"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Signal struct {
|
||||
Type string `json:"type"`
|
||||
StreamList []string `json:"streamList"`
|
||||
Offer string `json:"offer"`
|
||||
Answer string `json:"answer"`
|
||||
}
|
||||
|
||||
type WebRTCBatcher struct {
|
||||
WebRTCIO
|
||||
PageSize int
|
||||
PageNum int
|
||||
subscribers []*WebRTCBatchSubscriber
|
||||
signalChannel *DataChannel
|
||||
}
|
||||
|
||||
func (suber *WebRTCBatcher) Start() (err error) {
|
||||
suber.OnICECandidate(func(ice *ICECandidate) {
|
||||
if ice != nil {
|
||||
WebRTCPlugin.Info(ice.ToJSON().Candidate)
|
||||
}
|
||||
})
|
||||
suber.OnDataChannel(func(d *DataChannel) {
|
||||
WebRTCPlugin.Info("OnDataChannel:" + d.Label())
|
||||
suber.signalChannel = d
|
||||
suber.signalChannel.OnMessage(suber.Signal)
|
||||
})
|
||||
if err = suber.SetRemoteDescription(SessionDescription{Type: SDPTypeOffer, SDP: suber.SDP}); err != nil {
|
||||
return
|
||||
}
|
||||
suber.OnConnectionStateChange(func(pcs PeerConnectionState) {
|
||||
WebRTCPlugin.Info("Connection State has changed:" + pcs.String())
|
||||
switch pcs {
|
||||
case PeerConnectionStateConnected:
|
||||
|
||||
case PeerConnectionStateDisconnected, PeerConnectionStateFailed:
|
||||
for _, sub := range suber.subscribers {
|
||||
go sub.Stop()
|
||||
}
|
||||
suber.PeerConnection.Close()
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (suber *WebRTCBatcher) Signal(msg DataChannelMessage) {
|
||||
var s Signal
|
||||
// var offer SessionDescription
|
||||
if err := json.Unmarshal(msg.Data, &s); err != nil {
|
||||
WebRTCPlugin.Error("Signal", zap.Error(err))
|
||||
} else {
|
||||
switch s.Type {
|
||||
case "subscribe":
|
||||
|
||||
// if err = suber.SetRemoteDescription(SessionDescription{Type: SDPTypeOffer, SDP: s.Offer}); err != nil {
|
||||
// WebRTCPlugin.Error("Signal SetRemoteDescription", zap.Error(err))
|
||||
// return
|
||||
// }
|
||||
// var answer string
|
||||
// if answer, err = suber.GetAnswer(); err == nil {
|
||||
// b, _ := json.Marshal(map[string]string{"type": "answer", "answer": answer})
|
||||
// err = suber.signalChannel.Send(b)
|
||||
// }
|
||||
// if err != nil {
|
||||
// WebRTCPlugin.Error("Signal GetAnswer", zap.Error(err))
|
||||
// return
|
||||
// }
|
||||
sub := &WebRTCBatchSubscriber{}
|
||||
sub.WebRTCIO = suber.WebRTCIO
|
||||
for _, streamPath := range s.StreamList {
|
||||
if err = WebRTCPlugin.Subscribe(streamPath, sub); err == nil {
|
||||
suber.subscribers = append(suber.subscribers, sub)
|
||||
go sub.PlayRTP()
|
||||
}
|
||||
}
|
||||
// if offer, err = suber.CreateOffer(nil); err == nil {
|
||||
// b, _ := json.Marshal(offer)
|
||||
// err = suber.signalChannel.SendText(string(b))
|
||||
// suber.SetLocalDescription(offer)
|
||||
// }
|
||||
case "answer":
|
||||
if err = suber.SetRemoteDescription(SessionDescription{Type: SDPTypeAnswer, SDP: s.Answer}); err != nil {
|
||||
WebRTCPlugin.Error("Signal SetRemoteDescription", zap.Error(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user