rtcpmuxpolicy.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package webrtc
  2. import (
  3. "encoding/json"
  4. )
  5. // RTCPMuxPolicy affects what ICE candidates are gathered to support
  6. // non-multiplexed RTCP.
  7. type RTCPMuxPolicy int
  8. const (
  9. // RTCPMuxPolicyNegotiate indicates to gather ICE candidates for both
  10. // RTP and RTCP candidates. If the remote-endpoint is capable of
  11. // multiplexing RTCP, multiplex RTCP on the RTP candidates. If it is not,
  12. // use both the RTP and RTCP candidates separately.
  13. RTCPMuxPolicyNegotiate RTCPMuxPolicy = iota + 1
  14. // RTCPMuxPolicyRequire indicates to gather ICE candidates only for
  15. // RTP and multiplex RTCP on the RTP candidates. If the remote endpoint is
  16. // not capable of rtcp-mux, session negotiation will fail.
  17. RTCPMuxPolicyRequire
  18. )
  19. // This is done this way because of a linter.
  20. const (
  21. rtcpMuxPolicyNegotiateStr = "negotiate"
  22. rtcpMuxPolicyRequireStr = "require"
  23. )
  24. func newRTCPMuxPolicy(raw string) RTCPMuxPolicy {
  25. switch raw {
  26. case rtcpMuxPolicyNegotiateStr:
  27. return RTCPMuxPolicyNegotiate
  28. case rtcpMuxPolicyRequireStr:
  29. return RTCPMuxPolicyRequire
  30. default:
  31. return RTCPMuxPolicy(Unknown)
  32. }
  33. }
  34. func (t RTCPMuxPolicy) String() string {
  35. switch t {
  36. case RTCPMuxPolicyNegotiate:
  37. return rtcpMuxPolicyNegotiateStr
  38. case RTCPMuxPolicyRequire:
  39. return rtcpMuxPolicyRequireStr
  40. default:
  41. return ErrUnknownType.Error()
  42. }
  43. }
  44. // UnmarshalJSON parses the JSON-encoded data and stores the result
  45. func (t *RTCPMuxPolicy) UnmarshalJSON(b []byte) error {
  46. var val string
  47. if err := json.Unmarshal(b, &val); err != nil {
  48. return err
  49. }
  50. *t = newRTCPMuxPolicy(val)
  51. return nil
  52. }
  53. // MarshalJSON returns the JSON encoding
  54. func (t RTCPMuxPolicy) MarshalJSON() ([]byte, error) {
  55. return json.Marshal(t.String())
  56. }