github.com/pion/webrtc/v3@v3.2.24/sdptype.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  	"strings"
     9  )
    10  
    11  // SDPType describes the type of an SessionDescription.
    12  type SDPType int
    13  
    14  const (
    15  	// SDPTypeOffer indicates that a description MUST be treated as an SDP
    16  	// offer.
    17  	SDPTypeOffer SDPType = iota + 1
    18  
    19  	// SDPTypePranswer indicates that a description MUST be treated as an
    20  	// SDP answer, but not a final answer. A description used as an SDP
    21  	// pranswer may be applied as a response to an SDP offer, or an update to
    22  	// a previously sent SDP pranswer.
    23  	SDPTypePranswer
    24  
    25  	// SDPTypeAnswer indicates that a description MUST be treated as an SDP
    26  	// final answer, and the offer-answer exchange MUST be considered complete.
    27  	// A description used as an SDP answer may be applied as a response to an
    28  	// SDP offer or as an update to a previously sent SDP pranswer.
    29  	SDPTypeAnswer
    30  
    31  	// SDPTypeRollback indicates that a description MUST be treated as
    32  	// canceling the current SDP negotiation and moving the SDP offer and
    33  	// answer back to what it was in the previous stable state. Note the
    34  	// local or remote SDP descriptions in the previous stable state could be
    35  	// null if there has not yet been a successful offer-answer negotiation.
    36  	SDPTypeRollback
    37  )
    38  
    39  // This is done this way because of a linter.
    40  const (
    41  	sdpTypeOfferStr    = "offer"
    42  	sdpTypePranswerStr = "pranswer"
    43  	sdpTypeAnswerStr   = "answer"
    44  	sdpTypeRollbackStr = "rollback"
    45  )
    46  
    47  // NewSDPType creates an SDPType from a string
    48  func NewSDPType(raw string) SDPType {
    49  	switch raw {
    50  	case sdpTypeOfferStr:
    51  		return SDPTypeOffer
    52  	case sdpTypePranswerStr:
    53  		return SDPTypePranswer
    54  	case sdpTypeAnswerStr:
    55  		return SDPTypeAnswer
    56  	case sdpTypeRollbackStr:
    57  		return SDPTypeRollback
    58  	default:
    59  		return SDPType(Unknown)
    60  	}
    61  }
    62  
    63  func (t SDPType) String() string {
    64  	switch t {
    65  	case SDPTypeOffer:
    66  		return sdpTypeOfferStr
    67  	case SDPTypePranswer:
    68  		return sdpTypePranswerStr
    69  	case SDPTypeAnswer:
    70  		return sdpTypeAnswerStr
    71  	case SDPTypeRollback:
    72  		return sdpTypeRollbackStr
    73  	default:
    74  		return ErrUnknownType.Error()
    75  	}
    76  }
    77  
    78  // MarshalJSON enables JSON marshaling of a SDPType
    79  func (t SDPType) MarshalJSON() ([]byte, error) {
    80  	return json.Marshal(t.String())
    81  }
    82  
    83  // UnmarshalJSON enables JSON unmarshaling of a SDPType
    84  func (t *SDPType) UnmarshalJSON(b []byte) error {
    85  	var s string
    86  	if err := json.Unmarshal(b, &s); err != nil {
    87  		return err
    88  	}
    89  	switch strings.ToLower(s) {
    90  	default:
    91  		return ErrUnknownType
    92  	case "offer":
    93  		*t = SDPTypeOffer
    94  	case "pranswer":
    95  		*t = SDPTypePranswer
    96  	case "answer":
    97  		*t = SDPTypeAnswer
    98  	case "rollback":
    99  		*t = SDPTypeRollback
   100  	}
   101  
   102  	return nil
   103  }