github.com/pion/webrtc/v4@v4.0.1/sdpsemantics.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  // SDPSemantics determines which style of SDP offers and answers
    11  // can be used
    12  type SDPSemantics int
    13  
    14  const (
    15  	// SDPSemanticsUnifiedPlan uses unified-plan offers and answers
    16  	// (the default in Chrome since M72)
    17  	// https://tools.ietf.org/html/draft-roach-mmusic-unified-plan-00
    18  	SDPSemanticsUnifiedPlan SDPSemantics = iota
    19  
    20  	// SDPSemanticsPlanB uses plan-b offers and answers
    21  	// NB: This format should be considered deprecated
    22  	// https://tools.ietf.org/html/draft-uberti-rtcweb-plan-00
    23  	SDPSemanticsPlanB
    24  
    25  	// SDPSemanticsUnifiedPlanWithFallback prefers unified-plan
    26  	// offers and answers, but will respond to a plan-b offer
    27  	// with a plan-b answer
    28  	SDPSemanticsUnifiedPlanWithFallback
    29  )
    30  
    31  const (
    32  	sdpSemanticsUnifiedPlanWithFallback = "unified-plan-with-fallback"
    33  	sdpSemanticsUnifiedPlan             = "unified-plan"
    34  	sdpSemanticsPlanB                   = "plan-b"
    35  )
    36  
    37  func newSDPSemantics(raw string) SDPSemantics {
    38  	switch raw {
    39  	case sdpSemanticsPlanB:
    40  		return SDPSemanticsPlanB
    41  	case sdpSemanticsUnifiedPlanWithFallback:
    42  		return SDPSemanticsUnifiedPlanWithFallback
    43  	default:
    44  		return SDPSemanticsUnifiedPlan
    45  	}
    46  }
    47  
    48  func (s SDPSemantics) String() string {
    49  	switch s {
    50  	case SDPSemanticsUnifiedPlanWithFallback:
    51  		return sdpSemanticsUnifiedPlanWithFallback
    52  	case SDPSemanticsUnifiedPlan:
    53  		return sdpSemanticsUnifiedPlan
    54  	case SDPSemanticsPlanB:
    55  		return sdpSemanticsPlanB
    56  	default:
    57  		return ErrUnknownType.Error()
    58  	}
    59  }
    60  
    61  // UnmarshalJSON parses the JSON-encoded data and stores the result
    62  func (s *SDPSemantics) UnmarshalJSON(b []byte) error {
    63  	var val string
    64  	if err := json.Unmarshal(b, &val); err != nil {
    65  		return err
    66  	}
    67  
    68  	*s = newSDPSemantics(val)
    69  	return nil
    70  }
    71  
    72  // MarshalJSON returns the JSON encoding
    73  func (s SDPSemantics) MarshalJSON() ([]byte, error) {
    74  	return json.Marshal(s.String())
    75  }