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