sdpsemantics.go 1.9 KB

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