summaryrefslogtreecommitdiff
path: root/vendor/github.com/pion/sdp/v3/marshal.go
blob: f029c4e9968484f39eac21ee510444447232d141 (plain)
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
package sdp

import (
	"strings"
)

// 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) {
	m := make(marshaller, 0, 1024)

	m.addKeyValue("v=", s.Version.String())
	m.addKeyValue("o=", s.Origin.String())
	m.addKeyValue("s=", s.SessionName.String())

	if s.SessionInformation != nil {
		m.addKeyValue("i=", s.SessionInformation.String())
	}

	if s.URI != nil {
		m.addKeyValue("u=", s.URI.String())
	}

	if s.EmailAddress != nil {
		m.addKeyValue("e=", s.EmailAddress.String())
	}

	if s.PhoneNumber != nil {
		m.addKeyValue("p=", s.PhoneNumber.String())
	}

	if s.ConnectionInformation != nil {
		m.addKeyValue("c=", s.ConnectionInformation.String())
	}

	for _, b := range s.Bandwidth {
		m.addKeyValue("b=", b.String())
	}

	for _, td := range s.TimeDescriptions {
		m.addKeyValue("t=", td.Timing.String())
		for _, r := range td.RepeatTimes {
			m.addKeyValue("r=", r.String())
		}
	}

	if len(s.TimeZones) > 0 {
		var b strings.Builder
		for i, z := range s.TimeZones {
			if i > 0 {
				b.WriteString(" ")
			}
			b.WriteString(z.String())
		}
		m.addKeyValue("z=", b.String())
	}

	if s.EncryptionKey != nil {
		m.addKeyValue("k=", s.EncryptionKey.String())
	}

	for _, a := range s.Attributes {
		m.addKeyValue("a=", a.String())
	}

	for _, md := range s.MediaDescriptions {
		m.addKeyValue("m=", md.MediaName.String())

		if md.MediaTitle != nil {
			m.addKeyValue("i=", md.MediaTitle.String())
		}

		if md.ConnectionInformation != nil {
			m.addKeyValue("c=", md.ConnectionInformation.String())
		}

		for _, b := range md.Bandwidth {
			m.addKeyValue("b=", b.String())
		}

		if md.EncryptionKey != nil {
			m.addKeyValue("k=", md.EncryptionKey.String())
		}

		for _, a := range md.Attributes {
			m.addKeyValue("a=", a.String())
		}
	}

	return m.bytes(), nil
}

// marshaller contains state during marshaling.
type marshaller []byte

func (m *marshaller) addKeyValue(key, value string) {
	if value == "" {
		return
	}
	*m = append(*m, key...)
	*m = append(*m, value...)
	*m = append(*m, "\r\n"...)
}

func (m *marshaller) bytes() []byte {
	return *m
}