-
Notifications
You must be signed in to change notification settings - Fork 36
/
batcher.go
178 lines (166 loc) · 4.67 KB
/
batcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package webrtc
import (
"encoding/json"
"fmt"
. "github.com/pion/webrtc/v3"
"go.uber.org/zap"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/util"
)
type Signal struct {
Type string `json:"type"`
StreamList []string `json:"streamList"`
Offer string `json:"offer"`
Answer string `json:"answer"`
StreamPath string `json:"streamPath"`
}
type SignalStreamPath struct {
Type string `json:"type"`
StreamPath string `json:"streamPath"`
}
func NewRemoveSingal(streamPath string) string {
s := SignalStreamPath{
Type: "remove",
StreamPath: streamPath,
}
b, _ := json.Marshal(s)
return string(b)
}
type SignalSDP struct {
Type string `json:"type"`
SDP string `json:"sdp"`
}
func NewAnswerSingal(sdp string) string {
s := SignalSDP{
Type: "answer",
SDP: sdp,
}
b, _ := json.Marshal(s)
return string(b)
}
type WebRTCBatcher struct {
PageSize int
PageNum int
subscribers util.Map[string, *WebRTCBatchSubscriber]
signalChannel *DataChannel
WebRTCPublisher
}
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:
zr := zap.String("reason", pcs.String())
suber.subscribers.Range(func(key string, value *WebRTCBatchSubscriber) {
value.Stop(zr)
})
if suber.Publisher.Stream != nil {
suber.Publisher.Stop(zr)
}
suber.PeerConnection.Close()
}
})
return
}
func (suber *WebRTCBatcher) RemoveSubscribe(streamPath string) {
suber.signalChannel.SendText(NewRemoveSingal(streamPath))
}
func (suber *WebRTCBatcher) Answer() (err error) {
var answer string
if answer, err = suber.GetAnswer(); err == nil {
err = suber.signalChannel.SendText(NewAnswerSingal(answer))
}
if err != nil {
WebRTCPlugin.Error("Signal GetAnswer", zap.Error(err))
}
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
}
for _, streamPath := range s.StreamList {
if suber.subscribers.Has(streamPath) {
continue
}
sub := &WebRTCBatchSubscriber{}
sub.ID = fmt.Sprintf("%s_%s", suber.ID, streamPath)
sub.WebRTCIO = suber.WebRTCIO
if err = WebRTCPlugin.SubscribeExist(streamPath, sub); err == nil {
suber.subscribers.Add(streamPath, sub)
go func(streamPath string) {
if sub.DC == nil {
sub.PlayRTP()
if sub.audio.RTPSender != nil {
suber.RemoveTrack(sub.audio.RTPSender)
}
if sub.video.RTPSender != nil {
suber.RemoveTrack(sub.video.RTPSender)
}
suber.RemoveSubscribe(streamPath)
} else {
sub.DC.OnOpen(func() {
sub.DC.Send(codec.FLVHeader)
go func() {
sub.PlayFLV()
sub.DC.Close()
suber.RemoveSubscribe(streamPath)
}()
})
}
}(streamPath)
} else {
WebRTCPlugin.Error("subscribe", zap.String("streamPath", streamPath), zap.Error(err))
suber.RemoveSubscribe(streamPath)
}
}
err = suber.Answer()
// if offer, err = suber.CreateOffer(nil); err == nil {
// b, _ := json.Marshal(offer)
// err = suber.signalChannel.SendText(string(b))
// suber.SetLocalDescription(offer)
// }
case "publish", "unpublish":
if err = suber.SetRemoteDescription(SessionDescription{Type: SDPTypeOffer, SDP: s.Offer}); err != nil {
WebRTCPlugin.Error("Signal SetRemoteDescription", zap.Error(err))
return
}
if err = suber.Answer(); err == nil {
switch s.Type {
case "publish":
WebRTCPlugin.Publish(s.StreamPath, suber)
case "unpublish":
suber.Stop()
}
}
case "answer":
if err = suber.SetRemoteDescription(SessionDescription{Type: SDPTypeAnswer, SDP: s.Answer}); err != nil {
WebRTCPlugin.Error("Signal SetRemoteDescription", zap.Error(err))
return
}
}
WebRTCPlugin.Info(s.Type)
}
}