-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathav-media-description.ts
More file actions
251 lines (231 loc) · 6.53 KB
/
av-media-description.ts
File metadata and controls
251 lines (231 loc) · 6.53 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
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
/*
* 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 {
DirectionLine,
ExtMapDirection,
ExtMapLine,
FingerprintLine,
FmtpLine,
Line,
MediaDirection,
MediaLine,
MidLine,
RidLine,
RtcpFbLine,
RtcpMuxLine,
RtpMapLine,
Setup,
SetupLine,
SimulcastLine,
SsrcGroupLine,
SsrcLine,
} from '../lines';
import { CodecInfo } from './codec-info';
import { CodecStore } from './codec-store';
import { MediaDescription } from './media-description';
/**
* Model a media description with type 'audio' or 'video'.
*/
export class AvMediaDescription extends MediaDescription {
pts: Array<number> = [];
extMaps: Map<number, ExtMapLine> = new Map();
rids: Array<RidLine> = [];
simulcast?: SimulcastLine;
codecs: CodecStore = new CodecStore();
direction?: MediaDirection;
rtcpMux = false;
ssrcs: Array<SsrcLine> = [];
ssrcGroups: Array<SsrcGroupLine> = [];
/**
* Create a MediaInfo instance from a MediaLine.
*
* @param mediaLine - The MediaLine to create this MediaInfo from.
*/
constructor(mediaLine: MediaLine) {
super(mediaLine.type, mediaLine.port, mediaLine.protocol);
this.pts = mediaLine.formats.map((fmt) => {
return parseInt(fmt, 10);
});
this.pts.forEach((pt) => this.codecs.set(pt, new CodecInfo(pt)));
}
/**
* @inheritdoc
*/
toLines(): Array<Line> {
const lines: Array<Line> = [];
lines.push(
new MediaLine(
this.type,
this.port,
this.protocol,
this.pts.map((pt) => `${pt}`)
)
);
if (this.connection) {
lines.push(this.connection);
}
if (this.bandwidth) {
lines.push(this.bandwidth);
}
lines.push(...this.iceInfo.toLines());
if (this.fingerprint) {
lines.push(new FingerprintLine(this.fingerprint as string));
}
if (this.setup) {
lines.push(new SetupLine(this.setup as Setup));
}
if (this.mid) {
lines.push(new MidLine(this.mid));
}
if (this.rtcpMux) {
lines.push(new RtcpMuxLine());
}
if (this.content) {
lines.push(this.content);
}
this.extMaps.forEach((extMap) => lines.push(extMap));
this.rids.forEach((rid) => lines.push(rid));
if (this.simulcast) {
lines.push(this.simulcast);
}
if (this.direction) {
lines.push(new DirectionLine(this.direction as MediaDirection));
}
lines.push(...this.codecs.toLines());
lines.push(...this.ssrcs);
lines.push(...this.ssrcGroups);
lines.push(...this.otherLines);
return lines;
}
/**
* @inheritdoc
*/
addLine(line: Line): boolean {
if (super.addLine(line)) {
return true;
}
if (line instanceof MediaLine) {
throw new Error('Error: tried passing a MediaLine to an existing MediaInfo');
}
if (line instanceof DirectionLine) {
this.direction = line.direction;
return true;
}
if (line instanceof ExtMapLine) {
if (this.extMaps.has(line.id)) {
throw new Error(
`Tried to extension with duplicate ID: an extension already exists with ID ${line.id}`
);
}
this.extMaps.set(line.id, line);
return true;
}
if (line instanceof RidLine) {
this.rids.push(line);
return true;
}
if (line instanceof RtcpMuxLine) {
this.rtcpMux = true;
return true;
}
if (line instanceof SimulcastLine) {
this.simulcast = line;
return true;
}
// Lines pertaining to a specific codec
if (line instanceof RtpMapLine || line instanceof FmtpLine || line instanceof RtcpFbLine) {
this.codecs.addLine(line);
return true;
}
if (line instanceof SsrcLine) {
this.ssrcs.push(line);
return true;
}
if (line instanceof SsrcGroupLine) {
this.ssrcGroups.push(line);
return true;
}
this.otherLines.push(line);
return true;
}
/**
* Get the CodecInfo associated with the given payload type, if one exists.
*
* @param pt - The payload type.
* @returns The corresponding CodecInfo, or undefined if none was found.
*/
getCodecByPt(pt: number): CodecInfo | undefined {
return this.codecs.get(pt);
}
/**
* Remove all references to the given payload type. This includes removing any secondary codecs
* that may be associated with this payload type.
*
* @param pt - The payload type of the codec to remove.
*/
removePt(pt: number) {
const associatedPts = [...this.codecs.values()]
.filter((ci: CodecInfo) => ci.primaryCodecPt === pt)
.map((ci: CodecInfo) => ci.pt);
const allPtsToRemove = [pt, ...associatedPts];
allPtsToRemove.forEach((ptToRemove: number) => {
this.codecs.delete(ptToRemove);
});
this.pts = this.pts.filter((existingPt) => allPtsToRemove.indexOf(existingPt) === -1);
}
/**
* Add the given extension to this mline.
*
* @param extInfo - Information related to the extension to be added.
* @param extInfo.uri - The URI of the extension.
* @param extInfo.direction - An optional direction for the extension.
* @param extInfo.attributes - Option attributes for the extension.
* @param extInfo.id - An optional ID to use for the extension. If no ID is passed, the first
* free (not already present in the map) ID will be used, starting at 1. If the ID is
* invalid or already in use, an error is thrown.
*/
addExtension({
uri,
direction,
attributes,
id,
}: {
uri: string;
direction?: ExtMapDirection;
attributes?: string;
id?: number;
}) {
// eslint-disable-next-line jsdoc/require-jsdoc
const getFirstFreeId = (): number => {
let freeId = 1;
for (;;) {
if (!this.extMaps.has(freeId)) {
break;
}
freeId += 1;
}
return freeId;
};
const extId = id || getFirstFreeId();
if (this.extMaps.has(extId)) {
throw new Error(`Extension with ID ${id} already exists`);
}
if (extId === 0) {
throw new Error(`Extension ID 0 is reserved`);
}
this.extMaps.set(extId, new ExtMapLine(extId, uri, direction, attributes));
}
}