-
Notifications
You must be signed in to change notification settings - Fork 76
/
ice.go
107 lines (95 loc) · 2.31 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
package webrtc
import (
"encoding/json"
)
// See: https://w3c.github.io/webrtc-pc/#idl-def-RTCIceCandidate
type (
IceProtocol int
IceCandidateType int
IceTcpCandidateType int
)
const (
IceProtocolUPD IceProtocol = iota
IceProtocolTCP
)
func (p IceProtocol) String() string {
return EnumToStringSafe(int(p), []string{
"udp",
"tcp",
})
}
const (
IceCandidateTypeHost IceCandidateType = iota
IceCandidateTypeSrflx
IceCandidateTypePrflx
IceCandidateTypeRelay
)
func (t IceCandidateType) String() string {
return EnumToStringSafe(int(t), []string{
"host",
"srflx",
"prflx",
"relay",
})
}
const (
IceTcpCandidateTypeActive IceTcpCandidateType = iota
IceTcpCandidateTypePassive
IceTcpCandidateTypeSo
)
func (t IceTcpCandidateType) String() string {
return EnumToStringSafe(int(t), []string{
"active",
"passive",
"so",
})
}
type IceCandidate struct {
Candidate string `json:"candidate"`
SdpMid string `json:"sdpMid"`
SdpMLineIndex int `json:"sdpMLineIndex"`
// Foundation string
// Priority C.ulong
// IP net.IP
// Protocol IceProtocol
// Port C.ushort
// Type IceCandidateType
// TcpType IceTcpCandidateType
// RelatedAddress string
// RelatedPort C.ushort
}
// Serialize an IceCandidate into a JSON string.
func (candidate *IceCandidate) Serialize() string {
bytes, err := json.Marshal(candidate)
if nil != err {
ERROR.Println(err)
return ""
}
return string(bytes)
}
// Deserialize a received json string into an IceCandidate, if possible.
func DeserializeIceCandidate(msg string) *IceCandidate {
var parsed map[string]interface{}
err := json.Unmarshal([]byte(msg), &parsed)
if nil != err {
ERROR.Println(err)
return nil
}
if _, ok := parsed["candidate"]; !ok {
ERROR.Println("Cannot deserialize IceCandidate without candidate field.")
return nil
}
if _, ok := parsed["sdpMid"]; !ok {
ERROR.Println("Cannot deserialize IceCandidate without sdpMid field.")
return nil
}
if _, ok := parsed["sdpMLineIndex"]; !ok {
ERROR.Println("Cannot deserialize IceCandidate without sdpMLineIndex field.")
return nil
}
ice := new(IceCandidate)
ice.Candidate = parsed["candidate"].(string)
ice.SdpMid = parsed["sdpMid"].(string)
ice.SdpMLineIndex = int(parsed["sdpMLineIndex"].(float64))
return ice
}