forked from pion/sdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathice.go
143 lines (119 loc) · 3.21 KB
/
ice.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
package sdp
import (
"fmt"
"strconv"
"strings"
)
// ICECandidate is used to (un)marshal ICE candidates.
type ICECandidate struct {
Foundation string
Component uint16
Priority uint32
Address string
Protocol string
Port uint16
Typ string
RelatedAddress string
RelatedPort uint16
ExtensionAttributes []ICECandidateAttribute
}
// ICECandidateAttribute represents an ICE candidate extension attribute
type ICECandidateAttribute struct {
Key string
Value string
}
// https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-24#section-4.1
// candidate-attribute = "candidate" ":" foundation SP component-id SP
// transport SP
// priority SP
// connection-address SP ;from RFC 4566
// port ;port from RFC 4566
// SP cand-type
// [SP rel-addr]
// [SP rel-port]
// *(SP extension-att-name SP
// extension-att-value)
// Marshal returns the string representation of the ICECandidate
func (c ICECandidate) Marshal() string {
val := fmt.Sprintf("%s %d %s %d %s %d typ %s",
c.Foundation,
c.Component,
c.Protocol,
c.Priority,
c.Address,
c.Port,
c.Typ)
if len(c.RelatedAddress) > 0 {
val = fmt.Sprintf("%s raddr %s rport %d",
val,
c.RelatedAddress,
c.RelatedPort)
}
for _, attr := range c.ExtensionAttributes {
val = fmt.Sprintf("%s %s %s",
val,
attr.Key,
attr.Value)
}
return val
}
// Unmarshal popuulates the ICECandidate from its string representation
func (c *ICECandidate) Unmarshal(raw string) error {
split := strings.Fields(raw)
if len(split) < 8 {
return fmt.Errorf("attribute not long enough to be ICE candidate (%d)", len(split))
}
// Foundation
c.Foundation = split[0]
// Component
component, err := strconv.ParseUint(split[1], 10, 16)
if err != nil {
return fmt.Errorf("could not parse component: %v", err)
}
c.Component = uint16(component)
// Protocol
c.Protocol = split[2]
// Priority
priority, err := strconv.ParseUint(split[3], 10, 32)
if err != nil {
return fmt.Errorf("could not parse priority: %v", err)
}
c.Priority = uint32(priority)
// Address
c.Address = split[4]
// Port
port, err := strconv.ParseUint(split[5], 10, 16)
if err != nil {
return fmt.Errorf("could not parse port: %v", err)
}
c.Port = uint16(port)
c.Typ = split[7]
if len(split) <= 8 {
return nil
}
split = split[8:]
if split[0] == "raddr" {
if len(split) < 4 {
return fmt.Errorf("could not parse related addresses: incorrect length")
}
// RelatedAddress
c.RelatedAddress = split[1]
// RelatedPort
relatedPort, err := strconv.ParseUint(split[3], 10, 16)
if err != nil {
return fmt.Errorf("could not parse port: %v", err)
}
c.RelatedPort = uint16(relatedPort)
if len(split) <= 4 {
return nil
}
split = split[4:]
}
for i := 0; len(split) > i+1; i += 2 {
c.ExtensionAttributes = append(c.ExtensionAttributes, ICECandidateAttribute{
Key: split[i],
Value: split[i+1],
})
}
return nil
}