rtcpmuxpolicy.go 1.8 KB

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