-
Notifications
You must be signed in to change notification settings - Fork 76
/
sdp.go
86 lines (77 loc) · 2.09 KB
/
sdp.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
package webrtc
// #include "peerconnection.h"
// #include <stdlib.h> // Needed for C.free
import "C"
import (
"encoding/json"
"unsafe"
)
/* WebRTC SessionDescription
See: https://w3c.github.io/webrtc-pc/#idl-def-RTCSessionDescription
*/
type SessionDescription struct {
Type string `json:"type"`
Sdp string `json:"sdp"`
}
// TODO: Turn into Enum.
var SdpTypes = []string{"offer", "pranswer", "answer", "rollback"}
func CgoSdpToGoString(sdp C.CGO_sdp) string {
serializedSDP := C.CGO_SerializeSDP(sdp)
defer C.free(unsafe.Pointer(serializedSDP))
return C.GoString(serializedSDP)
}
// Construct a SessionDescription object from a valid msg.
func NewSessionDescription(sdpType string, serializedSDP C.CGO_sdpString) *SessionDescription {
defer C.free(unsafe.Pointer(serializedSDP))
in := false
for i := 0; i < len(SdpTypes); i++ {
if SdpTypes[i] == sdpType {
in = true
}
}
if !in {
ERROR.Println("Invalid SDP type.")
return nil
}
sdp := new(SessionDescription)
sdp.Type = sdpType
sdp.Sdp = C.GoString(serializedSDP)
return sdp
}
// Serialize a SessionDescription into a JSON string.
func (desc *SessionDescription) Serialize() string {
bytes, err := json.Marshal(desc)
if nil != err {
ERROR.Println(err)
return ""
}
return string(bytes)
}
func (desc *SessionDescription) GoStringToCgoSdp() C.CGO_sdp {
t := C.CString(desc.Type)
defer C.free(unsafe.Pointer(t))
s := C.CString(desc.Sdp)
defer C.free(unsafe.Pointer(s))
return C.CGO_DeserializeSDP(t, s)
}
// Deserialize a received json string into a SessionDescription, if possible.
func DeserializeSessionDescription(msg string) *SessionDescription {
var parsed map[string]interface{}
err := json.Unmarshal([]byte(msg), &parsed)
if nil != err {
ERROR.Println(err)
return nil
}
if _, ok := parsed["type"]; !ok {
ERROR.Println("Cannot deserialize SessionDescription without type field.")
return nil
}
if _, ok := parsed["sdp"]; !ok {
ERROR.Println("Cannot deserialize SessionDescription without sdp field.")
return nil
}
return &SessionDescription{
Type: parsed["type"].(string),
Sdp: parsed["sdp"].(string),
}
}