forked from Monibuca/plugin-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubscriber.go
80 lines (75 loc) · 2.24 KB
/
subscriber.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
package webrtc
import (
"fmt"
"strings"
"github.com/pion/rtcp"
. "github.com/pion/webrtc/v3"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/track"
)
type WebRTCSubscriber struct {
Subscriber
WebRTCIO
videoTrack *TrackLocalStaticRTP
audioTrack *TrackLocalStaticRTP
}
func (suber *WebRTCSubscriber) OnEvent(event any) {
switch v := event.(type) {
case *track.Video:
if v.CodecID == codec.CodecID_H264 {
pli := "42001f"
pli = fmt.Sprintf("%x", v.GetDecoderConfiguration().Raw[0][1:4])
if !strings.Contains(suber.SDP, pli) {
pli = reg_level.FindAllStringSubmatch(suber.SDP, -1)[0][1]
}
suber.videoTrack, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeH264, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=" + pli}, "video", "m7s")
rtpSender, _ := suber.PeerConnection.AddTrack(suber.videoTrack)
go func() {
rtcpBuf := make([]byte, 1500)
for {
if n, _, rtcpErr := rtpSender.Read(rtcpBuf); rtcpErr != nil {
return
} else {
if p, err := rtcp.Unmarshal(rtcpBuf[:n]); err == nil {
for _, pp := range p {
switch pp.(type) {
case *rtcp.PictureLossIndication:
// fmt.Println("PictureLossIndication")
}
}
}
}
}
}()
suber.Subscriber.AddTrack(v) //接受这个track
}
case *track.Audio:
audioMimeType := MimeTypePCMA
if v.CodecID == codec.CodecID_PCMU {
audioMimeType = MimeTypePCMU
}
if v.CodecID == codec.CodecID_PCMA || v.CodecID == codec.CodecID_PCMU {
suber.audioTrack, _ = NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: audioMimeType}, "audio", "m7s")
suber.PeerConnection.AddTrack(suber.audioTrack)
suber.Subscriber.AddTrack(v) //接受这个track
}
case VideoRTP:
suber.videoTrack.WriteRTP(&v.Packet)
case AudioRTP:
suber.audioTrack.WriteRTP(&v.Packet)
case ISubscriber:
suber.OnConnectionStateChange(func(pcs PeerConnectionState) {
suber.Info("Connection State has changed:" + pcs.String())
switch pcs {
case PeerConnectionStateConnected:
go suber.PlayRTP()
case PeerConnectionStateDisconnected, PeerConnectionStateFailed:
suber.Stop()
suber.PeerConnection.Close()
}
})
default:
suber.Subscriber.OnEvent(event)
}
}