bundlepolicy.go 2.1 KB

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