sdpsemantics.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package webrtc
  4. import (
  5. "encoding/json"
  6. )
  7. // SDPSemantics determines which style of SDP offers and answers
  8. // can be used
  9. type SDPSemantics int
  10. const (
  11. // SDPSemanticsUnifiedPlan uses unified-plan offers and answers
  12. // (the default in Chrome since M72)
  13. // https://tools.ietf.org/html/draft-roach-mmusic-unified-plan-00
  14. SDPSemanticsUnifiedPlan SDPSemantics = iota
  15. // SDPSemanticsPlanB uses plan-b offers and answers
  16. // NB: This format should be considered deprecated
  17. // https://tools.ietf.org/html/draft-uberti-rtcweb-plan-00
  18. SDPSemanticsPlanB
  19. // SDPSemanticsUnifiedPlanWithFallback prefers unified-plan
  20. // offers and answers, but will respond to a plan-b offer
  21. // with a plan-b answer
  22. SDPSemanticsUnifiedPlanWithFallback
  23. )
  24. const (
  25. sdpSemanticsUnifiedPlanWithFallback = "unified-plan-with-fallback"
  26. sdpSemanticsUnifiedPlan = "unified-plan"
  27. sdpSemanticsPlanB = "plan-b"
  28. )
  29. func newSDPSemantics(raw string) SDPSemantics {
  30. switch raw {
  31. case sdpSemanticsUnifiedPlan:
  32. return SDPSemanticsUnifiedPlan
  33. case sdpSemanticsPlanB:
  34. return SDPSemanticsPlanB
  35. case sdpSemanticsUnifiedPlanWithFallback:
  36. return SDPSemanticsUnifiedPlanWithFallback
  37. default:
  38. return SDPSemantics(Unknown)
  39. }
  40. }
  41. func (s SDPSemantics) String() string {
  42. switch s {
  43. case SDPSemanticsUnifiedPlanWithFallback:
  44. return sdpSemanticsUnifiedPlanWithFallback
  45. case SDPSemanticsUnifiedPlan:
  46. return sdpSemanticsUnifiedPlan
  47. case SDPSemanticsPlanB:
  48. return sdpSemanticsPlanB
  49. default:
  50. return ErrUnknownType.Error()
  51. }
  52. }
  53. // UnmarshalJSON parses the JSON-encoded data and stores the result
  54. func (s *SDPSemantics) UnmarshalJSON(b []byte) error {
  55. var val string
  56. if err := json.Unmarshal(b, &val); err != nil {
  57. return err
  58. }
  59. *s = newSDPSemantics(val)
  60. return nil
  61. }
  62. // MarshalJSON returns the JSON encoding
  63. func (s SDPSemantics) MarshalJSON() ([]byte, error) {
  64. return json.Marshal(s.String())
  65. }