forked from bluenviron/gortsplib
/
transport.go
360 lines (285 loc) · 7.33 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package headers
import (
"encoding/hex"
"fmt"
"net"
"strconv"
"strings"
"github.com/Ariorhx/gortsplib/pkg/base"
)
func parsePorts(val string) (*[2]int, error) {
ports := strings.Split(val, "-")
if len(ports) == 2 {
port1, err := strconv.ParseUint(ports[0], 10, 31)
if err != nil {
return &[2]int{0, 0}, fmt.Errorf("invalid ports (%v)", val)
}
port2, err := strconv.ParseUint(ports[1], 10, 31)
if err != nil {
return &[2]int{0, 0}, fmt.Errorf("invalid ports (%v)", val)
}
return &[2]int{int(port1), int(port2)}, nil
}
if len(ports) == 1 {
port1, err := strconv.ParseUint(ports[0], 10, 31)
if err != nil {
return &[2]int{0, 0}, fmt.Errorf("invalid ports (%v)", val)
}
return &[2]int{int(port1), int(port1 + 1)}, nil
}
return &[2]int{0, 0}, fmt.Errorf("invalid ports (%v)", val)
}
// TransportProtocol is a transport protocol.
type TransportProtocol int
// transport protocols.
const (
TransportProtocolUDP TransportProtocol = iota
TransportProtocolTCP
)
// TransportDelivery is a delivery method.
type TransportDelivery int
// transport delivery methods.
const (
TransportDeliveryUnicast TransportDelivery = iota
TransportDeliveryMulticast
)
// TransportMode is a transport mode.
type TransportMode int
const (
// TransportModePlay is the "play" transport mode
TransportModePlay TransportMode = iota
// TransportModeRecord is the "record" transport mode
TransportModeRecord
)
// Transport is a Transport header.
type Transport struct {
// protocol of the stream
Protocol TransportProtocol
// (optional) delivery method of the stream
Delivery *TransportDelivery
// (optional) Source IP
Source *net.IP
// (optional) destination IP
Destination *net.IP
// (optional) interleaved frame ids
InterleavedIDs *[2]int
// (optional) TTL
TTL *uint
// (optional) ports
Ports *[2]int
// (optional) client ports
ClientPorts *[2]int
// (optional) server ports
ServerPorts *[2]int
// (optional) SSRC of the packets of the stream
SSRC *uint32
// (optional) mode
Mode *TransportMode
}
// Unmarshal decodes a Transport header.
func (h *Transport) Unmarshal(v base.HeaderValue) error {
if len(v) == 0 {
return fmt.Errorf("value not provided")
}
if len(v) > 1 {
return fmt.Errorf("value provided multiple times (%v)", v)
}
v0 := v[0]
kvs, err := keyValParse(v0, ';')
if err != nil {
return err
}
protocolFound := false
for k, rv := range kvs {
v := rv
switch k {
case "RTP/AVP", "RTP/AVP/UDP":
h.Protocol = TransportProtocolUDP
protocolFound = true
case "RTP/AVP/TCP":
h.Protocol = TransportProtocolTCP
protocolFound = true
case "unicast":
v := TransportDeliveryUnicast
h.Delivery = &v
case "multicast":
v := TransportDeliveryMulticast
h.Delivery = &v
case "source":
if v != "" {
ip := net.ParseIP(v)
if ip == nil {
addrs, err := net.LookupHost(v)
if err != nil {
return fmt.Errorf("invalid source (%v)", v)
}
ip = net.ParseIP(addrs[0])
if ip == nil {
return fmt.Errorf("invalid source (%v)", v)
}
}
h.Source = &ip
}
case "destination":
if v != "" {
ip := net.ParseIP(v)
if ip == nil {
return fmt.Errorf("invalid destination (%v)", v)
}
h.Destination = &ip
}
case "interleaved":
ports, err := parsePorts(v)
if err != nil {
return err
}
h.InterleavedIDs = ports
case "ttl":
tmp, err := strconv.ParseUint(v, 10, 32)
if err != nil {
return err
}
vu := uint(tmp)
h.TTL = &vu
case "port":
ports, err := parsePorts(v)
if err != nil {
return err
}
h.Ports = ports
case "client_port":
ports, err := parsePorts(v)
if err != nil {
return err
}
h.ClientPorts = ports
case "server_port":
ports, err := parsePorts(v)
if err != nil {
return err
}
h.ServerPorts = ports
case "ssrc":
v = strings.TrimLeft(v, " ")
if (len(v) % 2) != 0 {
v = "0" + v
}
if tmp, err := hex.DecodeString(v); err == nil && len(tmp) <= 4 {
var ssrc [4]byte
copy(ssrc[4-len(tmp):], tmp)
v := uint32(ssrc[0])<<24 | uint32(ssrc[1])<<16 | uint32(ssrc[2])<<8 | uint32(ssrc[3])
h.SSRC = &v
}
case "mode":
str := strings.ToLower(v)
str = strings.TrimPrefix(str, "\"")
str = strings.TrimSuffix(str, "\"")
switch str {
case "play":
v := TransportModePlay
h.Mode = &v
// receive is an old alias for record, used by ffmpeg with the
// -listen flag, and by Darwin Streaming Server
case "record", "receive":
v := TransportModeRecord
h.Mode = &v
default:
return fmt.Errorf("invalid transport mode: '%s'", str)
}
default:
// ignore non-standard keys
}
}
if !protocolFound {
return fmt.Errorf("protocol not found (%v)", v[0])
}
return nil
}
// Marshal encodes a Transport header.
func (h Transport) Marshal() base.HeaderValue {
var rets []string
if h.Protocol == TransportProtocolUDP {
rets = append(rets, "RTP/AVP")
} else {
rets = append(rets, "RTP/AVP/TCP")
}
if h.Delivery != nil {
if *h.Delivery == TransportDeliveryUnicast {
rets = append(rets, "unicast")
} else {
rets = append(rets, "multicast")
}
}
if h.Source != nil {
rets = append(rets, "source="+h.Source.String())
}
if h.Destination != nil {
rets = append(rets, "destination="+h.Destination.String())
}
if h.InterleavedIDs != nil {
rets = append(rets, "interleaved="+strconv.FormatInt(int64(h.InterleavedIDs[0]), 10)+
"-"+strconv.FormatInt(int64(h.InterleavedIDs[1]), 10))
}
if h.Ports != nil {
rets = append(rets, "port="+strconv.FormatInt(int64(h.Ports[0]), 10)+
"-"+strconv.FormatInt(int64(h.Ports[1]), 10))
}
if h.TTL != nil {
rets = append(rets, "ttl="+strconv.FormatUint(uint64(*h.TTL), 10))
}
if h.ClientPorts != nil {
rets = append(rets, "client_port="+strconv.FormatInt(int64(h.ClientPorts[0]), 10)+
"-"+strconv.FormatInt(int64(h.ClientPorts[1]), 10))
}
if h.ServerPorts != nil {
rets = append(rets, "server_port="+strconv.FormatInt(int64(h.ServerPorts[0]), 10)+
"-"+strconv.FormatInt(int64(h.ServerPorts[1]), 10))
}
if h.SSRC != nil {
tmp := make([]byte, 4)
tmp[0] = byte(*h.SSRC >> 24)
tmp[1] = byte(*h.SSRC >> 16)
tmp[2] = byte(*h.SSRC >> 8)
tmp[3] = byte(*h.SSRC)
rets = append(rets, "ssrc="+strings.ToUpper(hex.EncodeToString(tmp)))
}
if h.Mode != nil {
if *h.Mode == TransportModePlay {
rets = append(rets, "mode=play")
} else {
rets = append(rets, "mode=record")
}
}
return base.HeaderValue{strings.Join(rets, ";")}
}
// Transports is a Transport header with multiple transports.
type Transports []Transport
// Unmarshal decodes a Transport header.
func (ts *Transports) Unmarshal(v base.HeaderValue) error {
if len(v) == 0 {
return fmt.Errorf("value not provided")
}
if len(v) > 1 {
return fmt.Errorf("value provided multiple times (%v)", v)
}
v0 := v[0]
transports := strings.Split(v0, ",") // , separated per RFC2326 section 12.39
*ts = make([]Transport, len(transports))
for i, transport := range transports {
var tr Transport
err := tr.Unmarshal(base.HeaderValue{strings.TrimLeft(transport, " ")})
if err != nil {
return err
}
(*ts)[i] = tr
}
return nil
}
// Marshal encodes a Transport header.
func (ts Transports) Marshal() base.HeaderValue {
vals := make([]string, len(ts))
for i, th := range ts {
vals[i] = th.Marshal()[0]
}
return base.HeaderValue{strings.Join(vals, ",")}
}