forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_stream_media.go
114 lines (93 loc) · 2.21 KB
/
server_stream_media.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
package gortsplib
import (
"time"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/bluenviron/gortsplib/v3/pkg/media"
"github.com/bluenviron/gortsplib/v3/pkg/rtcpsender"
)
type serverStreamMedia struct {
trackID int
media *media.Media
formats map[uint8]*serverStreamFormat
multicastWriter *serverMulticastWriter
}
func newServerStreamMedia(st *ServerStream, medi *media.Media, trackID int) *serverStreamMedia {
sm := &serverStreamMedia{
trackID: trackID,
media: medi,
}
sm.formats = make(map[uint8]*serverStreamFormat)
for _, forma := range medi.Formats {
tr := &serverStreamFormat{
format: forma,
}
cmedia := medi
tr.rtcpSender = rtcpsender.New(
forma.ClockRate(),
func(pkt rtcp.Packet) {
st.WritePacketRTCP(cmedia, pkt)
},
)
sm.formats[forma.PayloadType()] = tr
}
return sm
}
func (sm *serverStreamMedia) close() {
for _, tr := range sm.formats {
if tr.rtcpSender != nil {
tr.rtcpSender.Close()
}
}
if sm.multicastWriter != nil {
sm.multicastWriter.close()
}
}
func (sm *serverStreamMedia) allocateMulticastHandler(s *Server) error {
if sm.multicastWriter == nil {
mh, err := newServerMulticastWriter(s)
if err != nil {
return err
}
sm.multicastWriter = mh
}
return nil
}
func (sm *serverStreamMedia) WritePacketRTPWithNTP(ss *ServerStream, pkt *rtp.Packet, ntp time.Time) {
byts := make([]byte, udpMaxPayloadSize)
n, err := pkt.MarshalTo(byts)
if err != nil {
return
}
byts = byts[:n]
forma := sm.formats[pkt.PayloadType]
forma.rtcpSender.ProcessPacket(pkt, ntp, forma.format.PTSEqualsDTS(pkt))
// send unicast
for r := range ss.activeUnicastReaders {
sm, ok := r.setuppedMedias[sm.media]
if ok {
sm.writePacketRTP(byts)
}
}
// send multicast
if sm.multicastWriter != nil {
sm.multicastWriter.writePacketRTP(byts)
}
}
func (sm *serverStreamMedia) writePacketRTCP(ss *ServerStream, pkt rtcp.Packet) {
byts, err := pkt.Marshal()
if err != nil {
return
}
// send unicast
for r := range ss.activeUnicastReaders {
sm, ok := r.setuppedMedias[sm.media]
if ok {
sm.writePacketRTCP(byts)
}
}
// send multicast
if sm.multicastWriter != nil {
sm.multicastWriter.writePacketRTCP(byts)
}
}