Refactor (De)SerializeSessionDescription as common utils

This commit is contained in:
Arlo Breault 2020-03-17 15:18:25 -04:00
parent c11461d339
commit d10af300c1
6 changed files with 72 additions and 109 deletions

View file

@ -1,11 +1,8 @@
package lib
import (
"encoding/json"
"log"
"time"
"github.com/pion/webrtc/v2"
)
const (
@ -86,52 +83,3 @@ func (b *BytesSyncLogger) AddInbound(amount int) {
}
b.InboundChan <- amount
}
func deserializeSessionDescription(msg string) *webrtc.SessionDescription {
var parsed map[string]interface{}
err := json.Unmarshal([]byte(msg), &parsed)
if nil != err {
log.Println(err)
return nil
}
if _, ok := parsed["type"]; !ok {
log.Println("Cannot deserialize SessionDescription without type field.")
return nil
}
if _, ok := parsed["sdp"]; !ok {
log.Println("Cannot deserialize SessionDescription without sdp field.")
return nil
}
var stype webrtc.SDPType
switch parsed["type"].(string) {
default:
log.Println("Unknown SDP type")
return nil
case "offer":
stype = webrtc.SDPTypeOffer
case "pranswer":
stype = webrtc.SDPTypePranswer
case "answer":
stype = webrtc.SDPTypeAnswer
case "rollback":
stype = webrtc.SDPTypeRollback
}
if err != nil {
log.Println(err)
return nil
}
return &webrtc.SessionDescription{
Type: stype,
SDP: parsed["sdp"].(string),
}
}
func serializeSessionDescription(desc *webrtc.SessionDescription) string {
bytes, err := json.Marshal(*desc)
if nil != err {
log.Println(err)
return ""
}
return string(bytes)
}