forked from Monibuca/plugin-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
publisher.go
78 lines (74 loc) · 1.79 KB
/
publisher.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
package webrtc
import (
"fmt"
"time"
"github.com/pion/rtcp"
. "github.com/pion/webrtc/v3"
. "m7s.live/engine/v4"
. "m7s.live/engine/v4/track"
)
type WebRTCPublisher struct {
Publisher
WebRTCIO
}
func (puber *WebRTCPublisher) OnEvent(event any) {
switch v := event.(type) {
case IPublisher:
puber.OnICEConnectionStateChange(func(connectionState ICEConnectionState) {
puber.Info("Connection State has changed:" + connectionState.String())
switch connectionState {
case ICEConnectionStateDisconnected, ICEConnectionStateFailed:
puber.Stop()
}
})
puber.OnTrack(func(track *TrackRemote, receiver *RTPReceiver) {
if codec := track.Codec(); track.Kind() == RTPCodecTypeAudio {
if puber.Equal(v) || puber.AudioTrack == nil {
switch codec.PayloadType {
case 8:
puber.AudioTrack = NewG711(puber.Stream, true)
case 0:
puber.AudioTrack = NewG711(puber.Stream, false)
default:
puber.AudioTrack = nil
return
}
}
for {
b := make([]byte, 1460)
if i, _, err := track.Read(b); err == nil {
puber.AudioTrack.WriteRTP(b[:i])
} else {
return
}
}
} else {
go func() {
ticker := time.NewTicker(time.Millisecond * webrtcConfig.PLI)
for {
select {
case <-ticker.C:
if rtcpErr := puber.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}}); rtcpErr != nil {
fmt.Println(rtcpErr)
}
case <-puber.Done():
return
}
}
}()
if puber.Equal(v) {
puber.VideoTrack = NewH264(puber.Stream)
}
for {
b := make([]byte, 1460)
if i, _, err := track.Read(b); err == nil {
puber.VideoTrack.WriteRTP(b[:i])
} else {
return
}
}
}
})
}
puber.Publisher.OnEvent(event)
}