forked from pion/sdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
342 lines (281 loc) · 7.55 KB
/
util.go
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package sdp
import (
"bufio"
"errors"
"fmt"
"io"
"sort"
"strconv"
"strings"
"github.com/pion/randutil"
)
const (
attributeKey = "a="
)
// ConnectionRole indicates which of the end points should initiate the connection establishment
type ConnectionRole int
const (
// ConnectionRoleActive indicates the endpoint will initiate an outgoing connection.
ConnectionRoleActive ConnectionRole = iota + 1
// ConnectionRolePassive indicates the endpoint will accept an incoming connection.
ConnectionRolePassive
// ConnectionRoleActpass indicates the endpoint is willing to accept an incoming connection or to initiate an outgoing connection.
ConnectionRoleActpass
// ConnectionRoleHoldconn indicates the endpoint does not want the connection to be established for the time being.
ConnectionRoleHoldconn
)
func (t ConnectionRole) String() string {
switch t {
case ConnectionRoleActive:
return "active"
case ConnectionRolePassive:
return "passive"
case ConnectionRoleActpass:
return "actpass"
case ConnectionRoleHoldconn:
return "holdconn"
default:
return "Unknown"
}
}
func newSessionID() (uint64, error) {
// https://tools.ietf.org/html/draft-ietf-rtcweb-jsep-26#section-5.2.1
// Session ID is recommended to be constructed by generating a 64-bit
// quantity with the highest bit set to zero and the remaining 63-bits
// being cryptographically random.
id, err := randutil.CryptoUint64()
return id & (^(uint64(1) << 63)), err
}
// Codec represents a codec
type Codec struct {
PayloadType uint8
Name string
ClockRate uint32
EncodingParameters string
Fmtp string
RTCPFeedback []string
}
const (
unknown = iota
)
func (c Codec) String() string {
return fmt.Sprintf("%d %s/%d/%s (%s) [%s]", c.PayloadType, c.Name, c.ClockRate, c.EncodingParameters, c.Fmtp, strings.Join(c.RTCPFeedback, ", "))
}
func parseRtpmap(rtpmap string) (Codec, error) {
var codec Codec
parsingFailed := errors.New("could not extract codec from rtpmap")
// a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encoding parameters>]
split := strings.Split(rtpmap, " ")
if len(split) != 2 {
return codec, parsingFailed
}
ptSplit := strings.Split(split[0], ":")
if len(ptSplit) != 2 {
return codec, parsingFailed
}
ptInt, err := strconv.Atoi(ptSplit[1])
if err != nil {
return codec, parsingFailed
}
codec.PayloadType = uint8(ptInt)
split = strings.Split(split[1], "/")
codec.Name = split[0]
parts := len(split)
if parts > 1 {
rate, err := strconv.Atoi(split[1])
if err != nil {
return codec, parsingFailed
}
codec.ClockRate = uint32(rate)
}
if parts > 2 {
codec.EncodingParameters = split[2]
}
return codec, nil
}
func parseFmtp(fmtp string) (Codec, error) {
var codec Codec
parsingFailed := errors.New("could not extract codec from fmtp")
// a=fmtp:<format> <format specific parameters>
split := strings.Split(fmtp, " ")
if len(split) != 2 {
return codec, parsingFailed
}
formatParams := split[1]
split = strings.Split(split[0], ":")
if len(split) != 2 {
return codec, parsingFailed
}
ptInt, err := strconv.Atoi(split[1])
if err != nil {
return codec, parsingFailed
}
codec.PayloadType = uint8(ptInt)
codec.Fmtp = formatParams
return codec, nil
}
func parseRtcpFb(rtcpFb string) (Codec, error) {
var codec Codec
parsingFailed := errors.New("could not extract codec from rtcp-fb")
// a=ftcp-fb:<payload type> <RTCP feedback type> [<RTCP feedback parameter>]
split := strings.SplitN(rtcpFb, " ", 2)
if len(split) != 2 {
return codec, parsingFailed
}
ptSplit := strings.Split(split[0], ":")
if len(ptSplit) != 2 {
return codec, parsingFailed
}
ptInt, err := strconv.Atoi(ptSplit[1])
if err != nil {
return codec, parsingFailed
}
codec.PayloadType = uint8(ptInt)
codec.RTCPFeedback = append(codec.RTCPFeedback, split[1])
return codec, nil
}
func mergeCodecs(codec Codec, codecs map[uint8]Codec) {
savedCodec := codecs[codec.PayloadType]
if savedCodec.PayloadType == 0 {
savedCodec.PayloadType = codec.PayloadType
}
if savedCodec.Name == "" {
savedCodec.Name = codec.Name
}
if savedCodec.ClockRate == 0 {
savedCodec.ClockRate = codec.ClockRate
}
if savedCodec.EncodingParameters == "" {
savedCodec.EncodingParameters = codec.EncodingParameters
}
if savedCodec.Fmtp == "" {
savedCodec.Fmtp = codec.Fmtp
}
savedCodec.RTCPFeedback = append(savedCodec.RTCPFeedback, codec.RTCPFeedback...)
codecs[savedCodec.PayloadType] = savedCodec
}
func (s *SessionDescription) buildCodecMap() map[uint8]Codec {
codecs := make(map[uint8]Codec)
for _, m := range s.MediaDescriptions {
for _, a := range m.Attributes {
attr := *a.String()
if strings.HasPrefix(attr, "rtpmap:") {
codec, err := parseRtpmap(attr)
if err == nil {
mergeCodecs(codec, codecs)
}
} else if strings.HasPrefix(attr, "fmtp:") {
codec, err := parseFmtp(attr)
if err == nil {
mergeCodecs(codec, codecs)
}
} else if strings.HasPrefix(attr, "rtcp-fb:") {
codec, err := parseRtcpFb(attr)
if err == nil {
mergeCodecs(codec, codecs)
}
}
}
}
return codecs
}
func equivalentFmtp(want, got string) bool {
wantSplit := strings.Split(want, ";")
gotSplit := strings.Split(got, ";")
if len(wantSplit) != len(gotSplit) {
return false
}
sort.Strings(wantSplit)
sort.Strings(gotSplit)
for i, wantPart := range wantSplit {
wantPart = strings.TrimSpace(wantPart)
gotPart := strings.TrimSpace(gotSplit[i])
if gotPart != wantPart {
return false
}
}
return true
}
func codecsMatch(wanted, got Codec) bool {
if wanted.Name != "" && !strings.EqualFold(wanted.Name, got.Name) {
return false
}
if wanted.ClockRate != 0 && wanted.ClockRate != got.ClockRate {
return false
}
if wanted.EncodingParameters != "" && wanted.EncodingParameters != got.EncodingParameters {
return false
}
if wanted.Fmtp != "" && !equivalentFmtp(wanted.Fmtp, got.Fmtp) {
return false
}
return true
}
// GetCodecForPayloadType scans the SessionDescription for the given payload type and returns the codec
func (s *SessionDescription) GetCodecForPayloadType(payloadType uint8) (Codec, error) {
codecs := s.buildCodecMap()
codec, ok := codecs[payloadType]
if ok {
return codec, nil
}
return codec, errors.New("payload type not found")
}
// GetPayloadTypeForCodec scans the SessionDescription for a codec that matches the provided codec
// as closely as possible and returns its payload type
func (s *SessionDescription) GetPayloadTypeForCodec(wanted Codec) (uint8, error) {
codecs := s.buildCodecMap()
for payloadType, codec := range codecs {
if codecsMatch(wanted, codec) {
return payloadType, nil
}
}
return 0, errors.New("codec not found")
}
type lexer struct {
desc *SessionDescription
input *bufio.Reader
}
type stateFn func(*lexer) (stateFn, error)
func readType(input *bufio.Reader) (string, error) {
for {
b, err := input.ReadByte()
if err != nil {
return "", err
}
if b == '\n' || b == '\r' {
continue
}
if err = input.UnreadByte(); err != nil {
return "", err
}
key, err := input.ReadString('=')
if err != nil {
return key, err
}
switch len(key) {
case 2:
return key, nil
default:
return key, fmt.Errorf("SyntaxError: %v", strconv.Quote(key))
}
}
}
func readValue(input *bufio.Reader) (string, error) {
lineBytes, _, err := input.ReadLine()
line := string(lineBytes)
if err != nil && err != io.EOF {
return line, err
}
if len(line) == 0 {
return line, io.EOF
}
return line, nil
}
func indexOf(element string, data []string) int {
for k, v := range data {
if element == v {
return k
}
}
return -1
}