github.com/pion/webrtc/v4@v4.0.1/bundlepolicy.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package webrtc 5 6 import ( 7 "encoding/json" 8 ) 9 10 // BundlePolicy affects which media tracks are negotiated if the remote 11 // endpoint is not bundle-aware, and what ICE candidates are gathered. If the 12 // remote endpoint is bundle-aware, all media tracks and data channels are 13 // bundled onto the same transport. 14 type BundlePolicy int 15 16 const ( 17 // BundlePolicyUnknown is the enum's zero-value 18 BundlePolicyUnknown BundlePolicy = iota 19 20 // BundlePolicyBalanced indicates to gather ICE candidates for each 21 // media type in use (audio, video, and data). If the remote endpoint is 22 // not bundle-aware, negotiate only one audio and video track on separate 23 // transports. 24 BundlePolicyBalanced 25 26 // BundlePolicyMaxCompat indicates to gather ICE candidates for each 27 // track. If the remote endpoint is not bundle-aware, negotiate all media 28 // tracks on separate transports. 29 BundlePolicyMaxCompat 30 31 // BundlePolicyMaxBundle indicates to gather ICE candidates for only 32 // one track. If the remote endpoint is not bundle-aware, negotiate only 33 // one media track. 34 BundlePolicyMaxBundle 35 ) 36 37 // This is done this way because of a linter. 38 const ( 39 bundlePolicyBalancedStr = "balanced" 40 bundlePolicyMaxCompatStr = "max-compat" 41 bundlePolicyMaxBundleStr = "max-bundle" 42 ) 43 44 func newBundlePolicy(raw string) BundlePolicy { 45 switch raw { 46 case bundlePolicyBalancedStr: 47 return BundlePolicyBalanced 48 case bundlePolicyMaxCompatStr: 49 return BundlePolicyMaxCompat 50 case bundlePolicyMaxBundleStr: 51 return BundlePolicyMaxBundle 52 default: 53 return BundlePolicyUnknown 54 } 55 } 56 57 func (t BundlePolicy) String() string { 58 switch t { 59 case BundlePolicyBalanced: 60 return bundlePolicyBalancedStr 61 case BundlePolicyMaxCompat: 62 return bundlePolicyMaxCompatStr 63 case BundlePolicyMaxBundle: 64 return bundlePolicyMaxBundleStr 65 default: 66 return ErrUnknownType.Error() 67 } 68 } 69 70 // UnmarshalJSON parses the JSON-encoded data and stores the result 71 func (t *BundlePolicy) UnmarshalJSON(b []byte) error { 72 var val string 73 if err := json.Unmarshal(b, &val); err != nil { 74 return err 75 } 76 77 *t = newBundlePolicy(val) 78 return nil 79 } 80 81 // MarshalJSON returns the JSON encoding 82 func (t BundlePolicy) MarshalJSON() ([]byte, error) { 83 return json.Marshal(t.String()) 84 }