-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
266 lines (219 loc) · 4.81 KB
/
transport.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package rtsp
import (
"errors"
"fmt"
"strconv"
"strings"
)
type TransportType int
type RtpProfile int
const (
TransportTypeTcp TransportType = iota
TransportTypeUdp
)
const (
RtpProfileInvalid = iota - 1 // invalid
RtpProfileAVP
RtpProfileAVPF
RtpProfileSAVP
RtpProfileSAVPF
)
const (
RtpProfileAVPStr = "rtp/avp"
RtpProfileAVPFStr = "rtp/avpf"
RtpProfileSAVPStr = "rtp/savp"
RtpProfileSAVPFStr = "rtp/savpf"
)
type Transport struct {
profile RtpProfile
ty TransportType
interleaveds []int
unicast bool
clientPorts []int
serverPorts []int
ssrc int64
mode string
}
func NewUdpTransport(profile RtpProfile, clientPorts []int) *Transport {
return &Transport{
profile: profile,
ty: TransportTypeUdp,
unicast: true,
clientPorts: clientPorts,
}
}
func NewTcpTransport(profile RtpProfile, interleaveds []int) *Transport {
return &Transport{
profile: profile,
ty: TransportTypeTcp,
interleaveds: interleaveds,
}
}
func (r RtpProfile) String() string {
switch r {
case RtpProfileAVP:
return RtpProfileAVPStr
case RtpProfileAVPF:
return RtpProfileAVPFStr
case RtpProfileSAVP:
return RtpProfileSAVPStr
case RtpProfileSAVPF:
return RtpProfileSAVPFStr
}
return ""
}
func (r *RtpProfile) Parse(s string) error {
if strings.Contains(s, RtpProfileAVPStr) {
*r = RtpProfileAVP
return nil
} else if strings.Contains(s, RtpProfileAVPFStr) {
*r = RtpProfileAVPF
return nil
} else if strings.Contains(s, RtpProfileSAVPStr) {
*r = RtpProfileSAVP
return nil
} else if strings.Contains(s, RtpProfileSAVPFStr) {
*r = RtpProfileSAVPF
return nil
}
return fmt.Errorf("invalid rtp profile %s", s)
}
func (t *Transport) SetSSRC(s int32) {
t.ssrc = int64(s)
}
func (t *Transport) String() string {
s := "Transport: "
s += t.profile.String()
if t.ty == TransportTypeTcp {
s += "TCP;"
s += "interleaved="
for i, v := range t.interleaveds {
s += strconv.Itoa(v)
if i < len(t.interleaveds)-1 {
s += "-"
}
}
} else {
s += "UDP;"
if t.unicast {
s += "unicast;"
}
if len(t.clientPorts) > 0 {
s += "client_port="
for i, v := range t.clientPorts {
s += strconv.Itoa(v)
if i < len(t.clientPorts)-1 {
s += "-"
}
}
}
if len(t.serverPorts) > 0 {
s += "server_port="
for i, v := range t.serverPorts {
s += strconv.Itoa(v)
if i < len(t.serverPorts)-1 {
s += "-"
}
}
}
}
if t.ssrc != 0 {
s += fmt.Sprintf(";ssrc=%x", t.ssrc)
}
return s
}
func UnmarshalTransport(s string) (*Transport, error) {
t := &Transport{
profile: RtpProfileInvalid,
ty: TransportTypeUdp,
interleaveds: make([]int, 2),
unicast: true,
clientPorts: make([]int, 2),
serverPorts: make([]int, 2),
ssrc: 0,
}
kvs := strings.Split(s, ";")
for _, p := range kvs {
var key, val string
kv := strings.Split(p, "=")
if len(kv) == 2 {
key = strings.ToLower(kv[0])
val = kv[1]
switch key {
case "interleaved":
t.interleaveds = make([]int, 2)
iv := strings.Split(val, "-")
if len(iv) == 2 {
t.interleaveds[0], _ = strconv.Atoi(iv[0])
t.interleaveds[1], _ = strconv.Atoi(iv[1])
t.ty = TransportTypeTcp
}
case "client_port":
iv := strings.Split(val, "-")
if len(iv) == 2 {
t.clientPorts[0], _ = strconv.Atoi(iv[0])
t.clientPorts[1], _ = strconv.Atoi(iv[1])
t.ty = TransportTypeUdp
}
case "server_port":
iv := strings.Split(val, "-")
if len(iv) == 2 {
t.clientPorts[0], _ = strconv.Atoi(iv[0])
t.clientPorts[1], _ = strconv.Atoi(iv[1])
t.ty = TransportTypeUdp
}
case "ssrc":
t.ssrc, _ = strconv.ParseInt(val, 16, 32)
case "mode":
t.mode = val
}
} else {
key = strings.ToLower(p)
if strings.Contains(key, "rtp/") {
err := t.profile.Parse(key)
if err != nil {
return nil, err
}
} else if strings.Contains(key, "unicast") {
t.unicast = true
}
}
}
if t.ty == TransportTypeUdp && len(t.clientPorts) != 2 {
return nil, errors.New("invalid transport ports")
}
if t.ty == TransportTypeTcp && len(t.interleaveds) != 2 {
return nil, errors.New("invalid transport interleaved")
}
if t.profile == RtpProfileInvalid {
return nil, errors.New("invalid rtp profile")
}
// if t.ssrc == 0 {
// return nil, errors.New("invalid ssrc")
// }
return t, nil
}
func (t *Transport) RtpPort() int {
if len(t.clientPorts) == 0 {
return -1
}
return t.clientPorts[0]
}
func (t *Transport) RtcpPort() int {
if len(t.clientPorts) == 0 {
return -1
}
return t.clientPorts[1]
}
func (t *Transport) RtpInterleaved() int {
if len(t.interleaveds) == 0 {
return -1
}
return t.interleaveds[0]
}
func (t *Transport) RtcpInterleaved() int {
if len(t.interleaveds) == 0 {
return -1
}
return t.interleaveds[1]
}