/
encoder.go
207 lines (173 loc) · 4.34 KB
/
encoder.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package rtpaac
import (
"crypto/rand"
"encoding/binary"
"time"
"github.com/kuartis/rtp"
)
const (
rtpVersion = 0x02
rtpPayloadMaxSize = 1460 // 1500 (mtu) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
)
func randUint32() uint32 {
var b [4]byte
rand.Read(b[:])
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
}
// Encoder is a RTP/AAC encoder.
type Encoder struct {
payloadType uint8
sampleRate float64
sequenceNumber uint16
ssrc uint32
initialTs uint32
}
// NewEncoder allocates an Encoder.
func NewEncoder(payloadType uint8,
sampleRate int,
sequenceNumber *uint16,
ssrc *uint32,
initialTs *uint32) *Encoder {
return &Encoder{
payloadType: payloadType,
sampleRate: float64(sampleRate),
sequenceNumber: func() uint16 {
if sequenceNumber != nil {
return *sequenceNumber
}
return uint16(randUint32())
}(),
ssrc: func() uint32 {
if ssrc != nil {
return *ssrc
}
return randUint32()
}(),
initialTs: func() uint32 {
if initialTs != nil {
return *initialTs
}
return randUint32()
}(),
}
}
func (e *Encoder) encodeTimestamp(ts time.Duration) uint32 {
return e.initialTs + uint32(ts.Seconds()*e.sampleRate)
}
// Encode encodes AUs into RTP/AAC packets.
func (e *Encoder) Encode(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packet, error) {
var rets []*rtp.Packet
var batch [][]byte
pts := firstPTS
// split AUs into batches
for _, au := range aus {
if e.lenAggregated(batch, au) <= rtpPayloadMaxSize {
// add to existing batch
batch = append(batch, au)
} else {
// write last batch
if batch != nil {
pkts, err := e.writeBatch(batch, pts)
if err != nil {
return nil, err
}
rets = append(rets, pkts...)
pts += time.Duration(len(batch)) * 1000 * time.Second / time.Duration(e.sampleRate)
}
// initialize new batch
batch = [][]byte{au}
}
}
// write last batch
pkts, err := e.writeBatch(batch, pts)
if err != nil {
return nil, err
}
rets = append(rets, pkts...)
return rets, nil
}
func (e *Encoder) writeBatch(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packet, error) {
if len(aus) == 1 {
// the AU fits into a single RTP packet
if len(aus[0]) < rtpPayloadMaxSize {
return e.writeAggregated(aus, firstPTS)
}
// split the AU into multiple fragmentation packet
return e.writeFragmented(aus[0], firstPTS)
}
return e.writeAggregated(aus, firstPTS)
}
func (e *Encoder) writeFragmented(au []byte, pts time.Duration) ([]*rtp.Packet, error) {
packetCount := len(au) / (rtpPayloadMaxSize - 4)
lastPacketSize := len(au) % (rtpPayloadMaxSize - 4)
if lastPacketSize > 0 {
packetCount++
}
ret := make([]*rtp.Packet, packetCount)
encPTS := e.encodeTimestamp(pts)
for i := range ret {
le := rtpPayloadMaxSize - 4
if i == (packetCount - 1) {
le = lastPacketSize
}
data := make([]byte, 4+le)
binary.BigEndian.PutUint16(data, 16)
binary.BigEndian.PutUint16(data[2:], uint16(le))
copy(data[4:], au[:le])
au = au[le:]
ret[i] = &rtp.Packet{
Header: rtp.Header{
Version: rtpVersion,
PayloadType: e.payloadType,
SequenceNumber: e.sequenceNumber,
Timestamp: encPTS,
SSRC: e.ssrc,
Marker: (i == (packetCount - 1)),
},
Payload: data,
}
e.sequenceNumber++
}
return ret, nil
}
func (e *Encoder) lenAggregated(aus [][]byte, addAU []byte) int {
ret := 2 // AU-headers-length
for _, au := range aus {
ret += 2 // AU-header
ret += len(au) // AU
}
if addAU != nil {
ret += 2 // AU-header
ret += len(addAU) // AU
}
return ret
}
func (e *Encoder) writeAggregated(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packet, error) {
payload := make([]byte, e.lenAggregated(aus, nil))
// AU-headers-length
binary.BigEndian.PutUint16(payload, uint16(len(aus)*16))
pos := 2
// AU-headers
for _, au := range aus {
binary.BigEndian.PutUint16(payload[pos:], uint16(len(au))<<3)
pos += 2
}
// AUs
for _, au := range aus {
auLen := copy(payload[pos:], au)
pos += auLen
}
pkt := &rtp.Packet{
Header: rtp.Header{
Version: rtpVersion,
PayloadType: e.payloadType,
SequenceNumber: e.sequenceNumber,
Timestamp: e.encodeTimestamp(firstPTS),
SSRC: e.ssrc,
Marker: true,
},
Payload: payload,
}
e.sequenceNumber++
return []*rtp.Packet{pkt}, nil
}