summaryrefslogtreecommitdiff
path: root/vendor/github.com/pion/webrtc/v3/sdpsemantics.go
blob: b8d396c9b1cda18f709c9eeeee1d0f36f2c60a35 (plain)
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
package webrtc

import (
	"encoding/json"
)

// SDPSemantics determines which style of SDP offers and answers
// can be used
type SDPSemantics int

const (
	// SDPSemanticsUnifiedPlan uses unified-plan offers and answers
	// (the default in Chrome since M72)
	// https://tools.ietf.org/html/draft-roach-mmusic-unified-plan-00
	SDPSemanticsUnifiedPlan SDPSemantics = iota

	// SDPSemanticsPlanB uses plan-b offers and answers
	// NB: This format should be considered deprecated
	// https://tools.ietf.org/html/draft-uberti-rtcweb-plan-00
	SDPSemanticsPlanB

	// SDPSemanticsUnifiedPlanWithFallback prefers unified-plan
	// offers and answers, but will respond to a plan-b offer
	// with a plan-b answer
	SDPSemanticsUnifiedPlanWithFallback
)

const (
	sdpSemanticsUnifiedPlanWithFallback = "unified-plan-with-fallback"
	sdpSemanticsUnifiedPlan             = "unified-plan"
	sdpSemanticsPlanB                   = "plan-b"
)

func newSDPSemantics(raw string) SDPSemantics {
	switch raw {
	case sdpSemanticsUnifiedPlan:
		return SDPSemanticsUnifiedPlan
	case sdpSemanticsPlanB:
		return SDPSemanticsPlanB
	case sdpSemanticsUnifiedPlanWithFallback:
		return SDPSemanticsUnifiedPlanWithFallback
	default:
		return SDPSemantics(Unknown)
	}
}

func (s SDPSemantics) String() string {
	switch s {
	case SDPSemanticsUnifiedPlanWithFallback:
		return sdpSemanticsUnifiedPlanWithFallback
	case SDPSemanticsUnifiedPlan:
		return sdpSemanticsUnifiedPlan
	case SDPSemanticsPlanB:
		return sdpSemanticsPlanB
	default:
		return ErrUnknownType.Error()
	}
}

// UnmarshalJSON parses the JSON-encoded data and stores the result
func (s *SDPSemantics) UnmarshalJSON(b []byte) error {
	var val string
	if err := json.Unmarshal(b, &val); err != nil {
		return err
	}

	*s = newSDPSemantics(val)
	return nil
}

// MarshalJSON returns the JSON encoding
func (s SDPSemantics) MarshalJSON() ([]byte, error) {
	return json.Marshal(s.String())
}