-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrtppayload.go
206 lines (182 loc) · 4.85 KB
/
rtppayload.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
package rtsp
import (
"bytes"
"errors"
"fmt"
)
type RtpProfile int
const (
RTP_H264 RtpProfile = 96
RTP_H265 = 97
)
type payload interface {
decode([]byte) error
encode([]byte) error
setOnPacket(onpacket func(data []byte, timestamp uint32))
}
type h264RtpPayload struct {
cache_ bytes.Buffer
//if current rtp pakcet of frame has been losted, different timestamp means different frame
//lastTimestamp help to split frame
onPacket func(data []byte, timestamp uint32)
}
func newH264Payload() *h264RtpPayload {
h264payload := new(h264RtpPayload)
h264payload.cache_.Write([]byte{0x00, 0x00, 0x00, 0x01})
return h264payload
}
type h265RtpPayload struct {
cache_ bytes.Buffer
onPacket func(data []byte, timestamp uint32)
}
func newH265Payload() *h265RtpPayload {
h265payload := new(h265RtpPayload)
h265payload.cache_.Write([]byte{0x00, 0x00, 0x00, 0x01})
return h265payload
}
func createRtpPayload(profile RtpProfile) (payload, error) {
switch {
case profile == RTP_H264:
return newH264Payload(), nil
case profile == RTP_H265:
return newH265Payload(), nil
default:
return nil, errors.New("unsupport rtp profile")
}
}
func createRtpPayloadByName(name string) (payload, error) {
switch {
case name == "H264":
return newH264Payload(), nil
case name == "H265":
return newH265Payload(), nil
default:
return nil, errors.New("unsupport rtp profile")
}
}
func (h264 *h264RtpPayload) decode(packet []byte) error {
var rtppacket rtp
err := rtppacket.decode(packet)
if err != nil {
return err
}
payloadType := rtppacket.payload[0] & 0x1F
switch {
case payloadType >= 1 && payloadType <= 23:
h264.cache_.Write(rtppacket.payload)
if h264.onPacket != nil {
h264.onPacket(h264.cache_.Bytes(), rtppacket.head.timestamp)
}
h264.cache_.Truncate(4)
case payloadType == 28:
return h264.decodeFu(rtppacket.payload, rtppacket.head.timestamp, false)
case payloadType == 29:
return h264.decodeFu(rtppacket.payload, rtppacket.head.timestamp, true)
default:
return errors.New("unsupport packet type")
}
return nil
}
// +---------------+
// |0|1|2|3|4|5|6|7|
// +-+-+-+-+-+-+-+-+
// |S|E|R| Type |
// +---------------+
func (h264 *h264RtpPayload) decodeFu(packet []byte, timestamp uint32, fu_b bool) error {
fuheader := packet[1]
var prefixLen int = 0
if fu_b {
prefixLen = 4
} else {
prefixLen = 2
}
startbit := int2bool(fuheader & 0x80)
endbit := int2bool(fuheader & 0x40)
if startbit {
if h264.cache_.Len() > 4 {
fmt.Println("somthing wrong happend maybe packet lost,discard dirty frame")
h264.cache_.Truncate(4)
}
h264.cache_.WriteByte((packet[0] & 0xE0) | (packet[1] & 0x1F))
}
h264.cache_.Write(packet[prefixLen:])
//fmt.Printf("cache buf len %d\n", h264.cache_.Len())
if endbit {
if h264.onPacket != nil {
h264.onPacket(h264.cache_.Bytes(), timestamp)
}
h264.cache_.Truncate(4)
}
return nil
}
func (h264 *h264RtpPayload) encode([]byte) error {
return nil
}
func (h264 *h264RtpPayload) setOnPacket(onpacket func(data []byte, timestamp uint32)) {
h264.onPacket = onpacket
}
func (h265 *h265RtpPayload) decode(packet []byte) error {
var rtppacket rtp
err := rtppacket.decode(packet)
if err != nil {
return err
}
payloadType := rtppacket.payload[0] >> 1 & 0x3F
//fmt.Printf("payload type %d\n", payloadType)
switch payloadType {
case 48:
return h265.decodeAP(rtppacket.payload, rtppacket.head.timestamp)
case 49:
return h265.decodeFu(rtppacket.payload, rtppacket.head.timestamp)
case 50:
return h265.decodePACI(rtppacket.payload, rtppacket.head.timestamp)
default:
h265.cache_.Write(rtppacket.payload)
if h265.onPacket != nil {
h265.onPacket(h265.cache_.Bytes(), rtppacket.head.timestamp)
}
h265.cache_.Truncate(4)
}
return nil
}
func (h265 *h265RtpPayload) decodeAP(packet []byte, timestamp uint32) error {
return nil
}
// +---------------+
// |0|1|2|3|4|5|6|7|
// +-+-+-+-+-+-+-+-+
// |S|E| FuType |
// +---------------+
func (h265 *h265RtpPayload) decodeFu(packet []byte, timestamp uint32) error {
fuheader := packet[2]
var prefixLen int = 0
prefixLen = 3
startbit := int2bool(fuheader & 0x80)
endbit := int2bool(fuheader & 0x40)
if startbit {
if h265.cache_.Len() > 4 {
fmt.Println("somthing wrong happend maybe packet lost,discard dirty frame")
h265.cache_.Truncate(4)
}
h265.cache_.WriteByte((packet[0] & 0x81) | ((packet[2] & 0x3F) << 1))
h265.cache_.WriteByte(packet[1])
}
h265.cache_.Write(packet[prefixLen:])
//fmt.Printf("cache buf len %d\n", h264.cache_.Len())
if endbit {
if h265.onPacket != nil {
h265.onPacket(h265.cache_.Bytes(), timestamp)
}
h265.cache_.Truncate(4)
}
return nil
}
func (h265 *h265RtpPayload) decodePACI(packet []byte, timestamp uint32) error {
return nil
}
func (h265 *h265RtpPayload) encode([]byte) error {
return nil
}
func (h265 *h265RtpPayload) setOnPacket(onpacket func(data []byte, timestamp uint32)) {
h265.onPacket = onpacket
}