-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathconsumer.go
179 lines (147 loc) · 4.35 KB
/
consumer.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
179
package rtsp
import (
"time"
"github.com/AlexxIT/go2rtc/pkg/aac"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/h264"
"github.com/AlexxIT/go2rtc/pkg/h265"
"github.com/AlexxIT/go2rtc/pkg/mjpeg"
"github.com/AlexxIT/go2rtc/pkg/pcm"
"github.com/pion/rtp"
)
func (c *Conn) GetMedias() []*core.Media {
//core.Assert(c.Medias != nil)
return c.Medias
}
func (c *Conn) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) (err error) {
var channel byte
switch c.mode {
case core.ModeActiveProducer: // backchannel
c.stateMu.Lock()
defer c.stateMu.Unlock()
if c.state == StatePlay {
if err = c.Reconnect(); err != nil {
return
}
}
if channel, err = c.SetupMedia(media); err != nil {
return
}
c.state = StateSetup
case core.ModePassiveConsumer:
channel = byte(len(c.Senders)) * 2
// for consumer is better to use original track codec
codec = track.Codec.Clone()
// generate new payload type, starting from 96
codec.PayloadType = byte(96 + len(c.Senders))
default:
panic(core.Caller())
}
// save original codec to sender (can have Codec.Name = ANY)
sender := core.NewSender(media, codec)
// important to send original codec for valid IsRTP check
sender.Handler = c.packetWriter(track.Codec, channel, codec.PayloadType)
if c.mode == core.ModeActiveProducer && track.Codec.Name == core.CodecPCMA {
// Fix Reolink Doorbell https://github.com/AlexxIT/go2rtc/issues/331
sender.Handler = pcm.RepackG711(true, sender.Handler)
}
sender.HandleRTP(track)
c.Senders = append(c.Senders, sender)
return nil
}
const (
startVideoBuf = 32 * 1024 // 32KB
startAudioBuf = 2 * 1024 // 2KB
maxBuf = 1024 * 1024 // 1MB
rtpHdr = 12 // basic RTP header size
intHdr = 4 // interleaved header size
)
func (c *Conn) packetWriter(codec *core.Codec, channel, payloadType uint8) core.HandlerFunc {
var buf []byte
var n int
video := codec.IsVideo()
if video {
buf = make([]byte, startVideoBuf)
} else {
buf = make([]byte, startAudioBuf)
}
flushBuf := func() {
if err := c.conn.SetWriteDeadline(time.Now().Add(Timeout)); err != nil {
return
}
//log.Printf("[rtsp] channel:%2d write_size:%6d buffer_size:%6d", channel, n, len(buf))
if _, err := c.conn.Write(buf[:n]); err == nil {
c.Send += n
}
n = 0
}
handlerFunc := func(packet *rtp.Packet) {
if c.state == StateNone {
return
}
clone := rtp.Packet{
Header: rtp.Header{
Version: 2,
Marker: packet.Marker,
PayloadType: payloadType,
SequenceNumber: packet.SequenceNumber,
Timestamp: packet.Timestamp,
SSRC: packet.SSRC,
},
Payload: packet.Payload,
}
if !video {
packet.Marker = true // better to have marker on all audio packets
}
size := rtpHdr + len(packet.Payload)
if l := len(buf); n+intHdr+size > l {
if l < maxBuf {
buf = append(buf, make([]byte, l)...) // double buffer size
} else {
flushBuf()
}
}
//log.Printf("[RTP] codec: %s, size: %6d, ts: %10d, pt: %2d, ssrc: %d, seq: %d, mark: %v", codec.Name, len(packet.Payload), packet.Timestamp, packet.PayloadType, packet.SSRC, packet.SequenceNumber, packet.Marker)
chunk := buf[n:]
_ = chunk[4] // bounds
chunk[0] = '$'
chunk[1] = channel
chunk[2] = byte(size >> 8)
chunk[3] = byte(size)
if _, err := clone.MarshalTo(chunk[4:]); err != nil {
return
}
n += 4 + size
if !packet.Marker || !c.playOK {
// collect continious video packets to buffer
// or wait OK for PLAY command for backchannel
//log.Printf("[rtsp] collecting buffer ok=%t", c.playOK)
return
}
flushBuf()
}
if !codec.IsRTP() {
switch codec.Name {
case core.CodecH264:
handlerFunc = h264.RTPPay(c.PacketSize, handlerFunc)
case core.CodecH265:
handlerFunc = h265.RTPPay(c.PacketSize, handlerFunc)
case core.CodecAAC:
handlerFunc = aac.RTPPay(handlerFunc)
case core.CodecJPEG:
handlerFunc = mjpeg.RTPPay(handlerFunc)
}
} else if codec.Name == core.CodecPCML {
handlerFunc = pcm.LittleToBig(handlerFunc)
} else if c.PacketSize != 0 {
switch codec.Name {
case core.CodecH264:
handlerFunc = h264.RTPPay(c.PacketSize, handlerFunc)
handlerFunc = h264.RTPDepay(codec, handlerFunc)
case core.CodecH265:
handlerFunc = h265.RTPPay(c.PacketSize, handlerFunc)
handlerFunc = h265.RTPDepay(codec, handlerFunc)
}
}
return handlerFunc
}