-
Notifications
You must be signed in to change notification settings - Fork 6
/
peerconnection_explainer.go
114 lines (89 loc) · 3.61 KB
/
peerconnection_explainer.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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
// Package explainer provides APIs to make debugging and learning WebRTC easier
package explainer
import (
"encoding/base64"
"strings"
"github.com/pion/explainer/internal/sdp"
"github.com/pion/explainer/pkg/output"
)
// PeerConnectionExplainer mocks the PeerConnection API and returns analysis and suggestions
type PeerConnectionExplainer interface {
// SetLocalDescription updates the PeerConnectionExplainer with the provided SessionDescription
SetLocalDescription(input string)
// GetLocalDescription returns the current SDP we are using from SetLocalDescription
GetLocalDescription() string
// SetRemoteDescription updates the PeerConnectionExplainer with the provided SessionDescription
SetRemoteDescription(input string)
// GetRemoteDescription returns the current SDP we are using from SetRemoteDescription
GetRemoteDescription() string
// Explain returns the result of the current PeerConnectionExplainer.
Explain() Result
}
// NewPeerConnectionExplainer returns a new PeerConnectionExplainer
func NewPeerConnectionExplainer() PeerConnectionExplainer {
return &peerConnectionExplainer{}
}
type peerConnectionExplainer struct {
localDescription, remoteDescription sessionDescription
}
func (pe *peerConnectionExplainer) String() string {
return "PeerConnection Explainer"
}
func generateSessionDescription(input string) sessionDescription {
if possiblyDecoded, err := base64.StdEncoding.DecodeString(input); err == nil {
input = string(possiblyDecoded)
}
s := sessionDescription{}
if s.unmarshal(input); s.Type == "" && s.SDP == "" {
s.SDP = input
}
s.SDP = strings.ReplaceAll(s.SDP, "\\r\\n", "\n")
return s
}
func (pe *peerConnectionExplainer) SetLocalDescription(input string) {
pe.localDescription = generateSessionDescription(input)
}
func (pe *peerConnectionExplainer) SetRemoteDescription(input string) {
pe.remoteDescription = generateSessionDescription(input)
}
func (pe *peerConnectionExplainer) Explain() (result Result) {
result.init()
if pe.localDescription.SDP == "" {
result.Warnings = append(result.Warnings, output.NewMessage(warnLocalDescriptionUnset, nil))
}
if pe.remoteDescription.SDP == "" {
result.Warnings = append(result.Warnings, output.NewMessage(warnRemoteDescriptionUnset, nil))
}
if len(result.Warnings) == 2 {
return // No SessionDescriptions we can check
}
if pe.localDescription.Type != "" && pe.localDescription.Type == pe.remoteDescription.Type {
result.Errors = append(result.Errors, output.NewMessage(errLocalAndRemoteSameType, nil))
}
parsed := &sdp.SessionDescription{}
if pe.localDescription.SDP != "" {
if m := parsed.Unmarshal(pe.localDescription.SDP); m.Message != "" {
m.Sources[0].Type = output.SourceTypeLocal
result.Errors = append(result.Errors, m)
} else {
errors := result.LocalDetails.Populate(parsed, output.SourceTypeLocal)
setSourcesType(errors, output.SourceTypeLocal)
result.Errors = append(result.Errors, errors...)
}
}
if pe.remoteDescription.SDP != "" {
if m := parsed.Unmarshal(pe.localDescription.SDP); m.Message != "" {
m.Sources[0].Type = output.SourceTypeRemote
result.Errors = append(result.Errors, m)
} else {
errors := result.LocalDetails.Populate(parsed, output.SourceTypeRemote)
setSourcesType(errors, output.SourceTypeRemote)
result.Errors = append(result.Errors, errors...)
}
}
return result
}
func (pe *peerConnectionExplainer) GetLocalDescription() string { return pe.localDescription.SDP }
func (pe *peerConnectionExplainer) GetRemoteDescription() string { return pe.remoteDescription.SDP }