2019-01-22 11:02:08 +00:00
|
|
|
package libp2pwebrtcdirect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
multibase "github.com/multiformats/go-multibase"
|
2019-04-06 15:47:28 +00:00
|
|
|
"github.com/pion/webrtc/v2"
|
2019-01-22 11:02:08 +00:00
|
|
|
)
|
|
|
|
|
2019-02-21 14:19:05 +00:00
|
|
|
func encodeSignal(desc webrtc.SessionDescription) (string, error) {
|
2019-01-22 11:02:08 +00:00
|
|
|
descData, err := json.Marshal(desc)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to marshal description: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
descEnc, err := multibase.Encode(multibase.Base58BTC, descData)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to encode description: %v", err)
|
|
|
|
}
|
|
|
|
return descEnc, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:19:05 +00:00
|
|
|
func decodeSignal(descEnc string) (webrtc.SessionDescription, error) {
|
|
|
|
var desc webrtc.SessionDescription
|
2019-01-22 11:02:08 +00:00
|
|
|
|
|
|
|
_, descData, err := multibase.Decode(descEnc)
|
|
|
|
if err != nil {
|
|
|
|
return desc, fmt.Errorf("failed to decode description: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(descData, &desc)
|
|
|
|
if err != nil {
|
|
|
|
return desc, fmt.Errorf("failed to unmarshal description: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return desc, nil
|
|
|
|
}
|