github.com/pion/webrtc/v3@v3.2.24/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  	// BundlePolicyBalanced indicates to gather ICE candidates for each
    18  	// media type in use (audio, video, and data). If the remote endpoint is
    19  	// not bundle-aware, negotiate only one audio and video track on separate
    20  	// transports.
    21  	BundlePolicyBalanced BundlePolicy = iota + 1
    22  
    23  	// BundlePolicyMaxCompat indicates to gather ICE candidates for each
    24  	// track. If the remote endpoint is not bundle-aware, negotiate all media
    25  	// tracks on separate transports.
    26  	BundlePolicyMaxCompat
    27  
    28  	// BundlePolicyMaxBundle indicates to gather ICE candidates for only
    29  	// one track. If the remote endpoint is not bundle-aware, negotiate only
    30  	// one media track.
    31  	BundlePolicyMaxBundle
    32  )
    33  
    34  // This is done this way because of a linter.
    35  const (
    36  	bundlePolicyBalancedStr  = "balanced"
    37  	bundlePolicyMaxCompatStr = "max-compat"
    38  	bundlePolicyMaxBundleStr = "max-bundle"
    39  )
    40  
    41  func newBundlePolicy(raw string) BundlePolicy {
    42  	switch raw {
    43  	case bundlePolicyBalancedStr:
    44  		return BundlePolicyBalanced
    45  	case bundlePolicyMaxCompatStr:
    46  		return BundlePolicyMaxCompat
    47  	case bundlePolicyMaxBundleStr:
    48  		return BundlePolicyMaxBundle
    49  	default:
    50  		return BundlePolicy(Unknown)
    51  	}
    52  }
    53  
    54  func (t BundlePolicy) String() string {
    55  	switch t {
    56  	case BundlePolicyBalanced:
    57  		return bundlePolicyBalancedStr
    58  	case BundlePolicyMaxCompat:
    59  		return bundlePolicyMaxCompatStr
    60  	case BundlePolicyMaxBundle:
    61  		return bundlePolicyMaxBundleStr
    62  	default:
    63  		return ErrUnknownType.Error()
    64  	}
    65  }
    66  
    67  // UnmarshalJSON parses the JSON-encoded data and stores the result
    68  func (t *BundlePolicy) UnmarshalJSON(b []byte) error {
    69  	var val string
    70  	if err := json.Unmarshal(b, &val); err != nil {
    71  		return err
    72  	}
    73  
    74  	*t = newBundlePolicy(val)
    75  	return nil
    76  }
    77  
    78  // MarshalJSON returns the JSON encoding
    79  func (t BundlePolicy) MarshalJSON() ([]byte, error) {
    80  	return json.Marshal(t.String())
    81  }