-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrtpmap-line.ts
More file actions
99 lines (88 loc) · 3.04 KB
/
rtpmap-line.ts
File metadata and controls
99 lines (88 loc) · 3.04 KB
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
/*
* Copyright 2022 Cisco
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NUM } from '../regex-helpers';
import { Line } from './line';
import { PayloadTypeRef } from './payload-type-ref';
/**
* Definition of an rtpmap attribute line as defined in https://datatracker.ietf.org/doc/html/rfc4566#section-6.
*
* @example
* a=rtpmap:96 VP8/90000
*/
export class RtpMapLine extends Line {
payloadType: PayloadTypeRef;
encodingName: string;
clockRate: number;
encodingParams?: string;
// Note: RtpMap params are separated by a slash ('/'). We can't use a 'TOKEN' to capture
// the codec name, since that will capture the slash as well. Even changing this to a 'lazy' match won't
// work, because then it won't grab enough.
// Instead, we define a special 'NON_SLASH_TOKEN' helper here, which matches everything but whitespace and
// a slash.
private static NON_SLASH_TOKEN = '[^\\s/]+';
private static regex = new RegExp(
`^rtpmap:(${NUM}) (${this.NON_SLASH_TOKEN})/(${this.NON_SLASH_TOKEN})(?:/(${this.NON_SLASH_TOKEN}))?`
);
/**
* Create an RtpMapLine from the given values.
*
* @param payloadType - The payload type.
* @param encodingName - The encoding name.
* @param clockRate - The encoding clockrate.
* @param encodingParams - Optional additional encoding parameters.
*/
constructor(
payloadType: PayloadTypeRef,
encodingName: string,
clockRate: number,
encodingParams?: string
) {
super();
this.payloadType = payloadType;
this.encodingName = encodingName;
this.clockRate = clockRate;
this.encodingParams = encodingParams;
}
/**
* Create an RtpMapLine from the given string.
*
* @param line - The line to parse.
* @returns An RtpMapLine instance or undefined if parsing failed.
*/
static fromSdpLine(line: string): RtpMapLine | undefined {
if (!RtpMapLine.regex.test(line)) {
return undefined;
}
const tokens = line.match(RtpMapLine.regex) as RegExpMatchArray;
const payloadType = new PayloadTypeRef(parseInt(tokens[1], 10));
const encodingName = tokens[2];
const clockRate = parseInt(tokens[3], 10);
// encodingParams, if present, will be in index 4
const encodingParams = tokens[4];
return new RtpMapLine(payloadType, encodingName, clockRate, encodingParams);
}
/**
* @inheritdoc
*/
toSdpLine(): string {
let str = '';
str += `a=rtpmap:${this.payloadType} ${this.encodingName}/${this.clockRate}`;
if (this.encodingParams) {
str += `/${this.encodingParams}`;
}
return str;
}
}