1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sdp
// Marshal takes a SDP struct to text
// https://tools.ietf.org/html/rfc4566#section-5
// Session description
//
// v= (protocol version)
// o= (originator and session identifier)
// s= (session name)
// i=* (session information)
// u=* (URI of description)
// e=* (email address)
// p=* (phone number)
// c=* (connection information -- not required if included in
// all media)
// b=* (zero or more bandwidth information lines)
// One or more time descriptions ("t=" and "r=" lines; see below)
// z=* (time zone adjustments)
// k=* (encryption key)
// a=* (zero or more session attribute lines)
// Zero or more media descriptions
//
// Time description
//
// t= (time the session is active)
// r=* (zero or more repeat times)
//
// Media description, if present
//
// m= (media name and transport address)
// i=* (media title)
// c=* (connection information -- optional if included at
// session level)
// b=* (zero or more bandwidth information lines)
// k=* (encryption key)
// a=* (zero or more media attribute lines)
func (s *SessionDescription) Marshal() ([]byte, error) { //nolint:cyclop
marsh := make(marshaller, 0, s.MarshalSize())
marsh.addKeyValue("v=", s.Version.marshalInto)
marsh.addKeyValue("o=", s.Origin.marshalInto)
marsh.addKeyValue("s=", s.SessionName.marshalInto)
if s.SessionInformation != nil {
marsh.addKeyValue("i=", s.SessionInformation.marshalInto)
}
if s.URI != nil {
marsh = append(marsh, "u="...)
marsh = append(marsh, s.URI.String()...)
marsh = append(marsh, "\r\n"...)
}
if s.EmailAddress != nil {
marsh.addKeyValue("e=", s.EmailAddress.marshalInto)
}
if s.PhoneNumber != nil {
marsh.addKeyValue("p=", s.PhoneNumber.marshalInto)
}
if s.ConnectionInformation != nil {
marsh.addKeyValue("c=", s.ConnectionInformation.marshalInto)
}
for _, b := range s.Bandwidth {
marsh.addKeyValue("b=", b.marshalInto)
}
for _, td := range s.TimeDescriptions {
marsh.addKeyValue("t=", td.Timing.marshalInto)
for _, r := range td.RepeatTimes {
marsh.addKeyValue("r=", r.marshalInto)
}
}
if len(s.TimeZones) > 0 {
marsh = append(marsh, "z="...)
for i, z := range s.TimeZones {
if i > 0 {
marsh = append(marsh, ' ')
}
marsh = z.marshalInto(marsh)
}
marsh = append(marsh, "\r\n"...)
}
if s.EncryptionKey != nil {
marsh.addKeyValue("k=", s.EncryptionKey.marshalInto)
}
for _, a := range s.Attributes {
marsh.addKeyValue("a=", a.marshalInto)
}
for _, md := range s.MediaDescriptions {
marsh.addKeyValue("m=", md.MediaName.marshalInto)
if md.MediaTitle != nil {
marsh.addKeyValue("i=", md.MediaTitle.marshalInto)
}
if md.ConnectionInformation != nil {
marsh.addKeyValue("c=", md.ConnectionInformation.marshalInto)
}
for _, b := range md.Bandwidth {
marsh.addKeyValue("b=", b.marshalInto)
}
if md.EncryptionKey != nil {
marsh.addKeyValue("k=", md.EncryptionKey.marshalInto)
}
for _, a := range md.Attributes {
marsh.addKeyValue("a=", a.marshalInto)
}
}
return marsh, nil
}
// `$type=` and CRLF size.
const lineBaseSize = 4
// MarshalSize returns the size of the SessionDescription once marshaled.
func (s *SessionDescription) MarshalSize() (marshalSize int) { //nolint:cyclop
marshalSize += lineBaseSize + s.Version.marshalSize()
marshalSize += lineBaseSize + s.Origin.marshalSize()
marshalSize += lineBaseSize + s.SessionName.marshalSize()
if s.SessionInformation != nil {
marshalSize += lineBaseSize + s.SessionInformation.marshalSize()
}
if s.URI != nil {
marshalSize += lineBaseSize + len(s.URI.String())
}
if s.EmailAddress != nil {
marshalSize += lineBaseSize + s.EmailAddress.marshalSize()
}
if s.PhoneNumber != nil {
marshalSize += lineBaseSize + s.PhoneNumber.marshalSize()
}
if s.ConnectionInformation != nil {
marshalSize += lineBaseSize + s.ConnectionInformation.marshalSize()
}
for _, b := range s.Bandwidth {
marshalSize += lineBaseSize + b.marshalSize()
}
for _, td := range s.TimeDescriptions {
marshalSize += lineBaseSize + td.Timing.marshalSize()
for _, r := range td.RepeatTimes {
marshalSize += lineBaseSize + r.marshalSize()
}
}
if len(s.TimeZones) > 0 {
marshalSize += lineBaseSize
for i, z := range s.TimeZones {
if i > 0 {
marshalSize++
}
marshalSize += z.marshalSize()
}
}
if s.EncryptionKey != nil {
marshalSize += lineBaseSize + s.EncryptionKey.marshalSize()
}
for _, a := range s.Attributes {
marshalSize += lineBaseSize + a.marshalSize()
}
for _, md := range s.MediaDescriptions {
marshalSize += lineBaseSize + md.MediaName.marshalSize()
if md.MediaTitle != nil {
marshalSize += lineBaseSize + md.MediaTitle.marshalSize()
}
if md.ConnectionInformation != nil {
marshalSize += lineBaseSize + md.ConnectionInformation.marshalSize()
}
for _, b := range md.Bandwidth {
marshalSize += lineBaseSize + b.marshalSize()
}
if md.EncryptionKey != nil {
marshalSize += lineBaseSize + md.EncryptionKey.marshalSize()
}
for _, a := range md.Attributes {
marshalSize += lineBaseSize + a.marshalSize()
}
}
return marshalSize
}
// marshaller contains state during marshaling.
type marshaller []byte
func (m *marshaller) addKeyValue(key string, value func([]byte) []byte) {
*m = append(*m, key...)
*m = value(*m)
*m = append(*m, "\r\n"...)
}
func lenUint(i uint64) (count int) {
if i == 0 {
return 1
}
for i != 0 {
i /= 10
count++
}
return
}
func lenInt(i int64) (count int) {
if i < 0 {
return lenUint(uint64(-i)) + 1
}
return lenUint(uint64(i))
}
func stringFromMarshal(marshalFunc func([]byte) []byte, sizeFunc func() int) string {
return string(marshalFunc(make([]byte, 0, sizeFunc())))
}
|