bundlepolicy.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // BundlePolicy affects which media tracks are negotiated if the remote
  8. // endpoint is not bundle-aware, and what ICE candidates are gathered. If the
  9. // remote endpoint is bundle-aware, all media tracks and data channels are
  10. // bundled onto the same transport.
  11. type BundlePolicy int
  12. const (
  13. // BundlePolicyBalanced indicates to gather ICE candidates for each
  14. // media type in use (audio, video, and data). If the remote endpoint is
  15. // not bundle-aware, negotiate only one audio and video track on separate
  16. // transports.
  17. BundlePolicyBalanced BundlePolicy = iota + 1
  18. // BundlePolicyMaxCompat indicates to gather ICE candidates for each
  19. // track. If the remote endpoint is not bundle-aware, negotiate all media
  20. // tracks on separate transports.
  21. BundlePolicyMaxCompat
  22. // BundlePolicyMaxBundle indicates to gather ICE candidates for only
  23. // one track. If the remote endpoint is not bundle-aware, negotiate only
  24. // one media track.
  25. BundlePolicyMaxBundle
  26. )
  27. // This is done this way because of a linter.
  28. const (
  29. bundlePolicyBalancedStr = "balanced"
  30. bundlePolicyMaxCompatStr = "max-compat"
  31. bundlePolicyMaxBundleStr = "max-bundle"
  32. )
  33. func newBundlePolicy(raw string) BundlePolicy {
  34. switch raw {
  35. case bundlePolicyBalancedStr:
  36. return BundlePolicyBalanced
  37. case bundlePolicyMaxCompatStr:
  38. return BundlePolicyMaxCompat
  39. case bundlePolicyMaxBundleStr:
  40. return BundlePolicyMaxBundle
  41. default:
  42. return BundlePolicy(Unknown)
  43. }
  44. }
  45. func (t BundlePolicy) String() string {
  46. switch t {
  47. case BundlePolicyBalanced:
  48. return bundlePolicyBalancedStr
  49. case BundlePolicyMaxCompat:
  50. return bundlePolicyMaxCompatStr
  51. case BundlePolicyMaxBundle:
  52. return bundlePolicyMaxBundleStr
  53. default:
  54. return ErrUnknownType.Error()
  55. }
  56. }
  57. // UnmarshalJSON parses the JSON-encoded data and stores the result
  58. func (t *BundlePolicy) UnmarshalJSON(b []byte) error {
  59. var val string
  60. if err := json.Unmarshal(b, &val); err != nil {
  61. return err
  62. }
  63. *t = newBundlePolicy(val)
  64. return nil
  65. }
  66. // MarshalJSON returns the JSON encoding
  67. func (t BundlePolicy) MarshalJSON() ([]byte, error) {
  68. return json.Marshal(t.String())
  69. }