/
SubscribeAndReceiveSubscribeAckTask.ts
274 lines (242 loc) · 10.9 KB
/
SubscribeAndReceiveSubscribeAckTask.ts
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import AudioVideoControllerState from '../audiovideocontroller/AudioVideoControllerState';
import MeetingSessionStatus from '../meetingsession/MeetingSessionStatus';
import MeetingSessionStatusCode from '../meetingsession/MeetingSessionStatusCode';
import SDP from '../sdp/SDP';
import ZLIBTextCompressor from '../sdp/ZLIBTextCompressor';
import { serverSideNetworkAdaptionIsNoneOrDefault } from '../signalingclient/ServerSideNetworkAdaption';
import SignalingClient from '../signalingclient/SignalingClient';
import SignalingClientEvent from '../signalingclient/SignalingClientEvent';
import SignalingClientEventType from '../signalingclient/SignalingClientEventType';
import SignalingClientSubscribe from '../signalingclient/SignalingClientSubscribe';
import SignalingClientObserver from '../signalingclientobserver/SignalingClientObserver';
import {
SdkSignalFrame,
SdkStreamServiceType,
SdkSubscribeAckFrame,
} from '../signalingprotocol/SignalingProtocol.js';
import TaskCanceler from '../taskcanceler/TaskCanceler';
import { convertVideoPreferencesToSignalingClientVideoSubscriptionConfiguration } from '../videodownlinkbandwidthpolicy/VideoPreferences';
import BaseTask from './BaseTask';
/**
* [[SubscribeAndReceiveSubscribeAckTask]] sends a subscribe frame with the given settings
* and receives SdkSubscribeAckFrame.
*/
export default class SubscribeAndReceiveSubscribeAckTask extends BaseTask {
protected taskName = 'SubscribeAndReceiveSubscribeAckTask';
private taskCanceler: TaskCanceler | null = null;
private textCompressor: ZLIBTextCompressor;
constructor(private context: AudioVideoControllerState) {
super(context.logger);
this.textCompressor = new ZLIBTextCompressor(context.logger);
}
cancel(): void {
if (this.taskCanceler) {
this.taskCanceler.cancel();
this.taskCanceler = null;
}
}
async run(): Promise<void> {
let localSdp = '';
if (this.context.peer && this.context.peer.localDescription) {
localSdp = new SDP(this.context.peer.localDescription.sdp).withUnifiedPlanFormat().sdp;
}
if (!this.context.enableSimulcast) {
// backward compatibility
let frameRate = 0;
let maxEncodeBitrateKbps = 0;
if (this.context.videoCaptureAndEncodeParameter) {
frameRate = this.context.videoCaptureAndEncodeParameter.captureFrameRate();
maxEncodeBitrateKbps = this.context.videoCaptureAndEncodeParameter.encodeBitrates()[0];
}
const param: RTCRtpEncodingParameters = {
rid: 'hi',
maxBitrate: maxEncodeBitrateKbps * 1000,
maxFramerate: frameRate,
active: true,
};
this.context.videoStreamIndex.integrateUplinkPolicyDecision([param]);
}
// This will cache the current index so that we maintain the values over the course of the subscribe.
this.context.videoStreamIndex.subscribeFrameSent();
// See comment above `fixUpSubscriptionOrder`
const videoSubscriptions = this.fixUpSubscriptionOrder(
localSdp,
this.context.videoSubscriptions
);
const isSendingStreams: boolean =
this.context.videoDuplexMode === SdkStreamServiceType.TX ||
this.context.videoDuplexMode === SdkStreamServiceType.DUPLEX;
let compressedSDPOffer: Uint8Array | null;
let localSdpOffer = localSdp;
if (
this.context.videoUplinkBandwidthPolicy.wantsVideoDependencyDescriptorRtpHeaderExtension ===
undefined ||
!this.context.videoUplinkBandwidthPolicy.wantsVideoDependencyDescriptorRtpHeaderExtension()
) {
// See note above similar code in `SetLocalDescriptionTask`.
localSdpOffer = new SDP(localSdpOffer).withoutDependencyDescriptorRtpHeaderExtension().sdp;
}
if (this.context.serverSupportsCompression) {
// If the server supports compression, then send the compressed version of the sdp
// and exclude the original sdp offer.
const prevOffer = this.context.previousSdpOffer ? this.context.previousSdpOffer.sdp : '';
compressedSDPOffer = this.textCompressor.compress(localSdpOffer, prevOffer);
this.context.logger.info(
`Compressed the SDP message from ${localSdpOffer.length} to ${compressedSDPOffer.length} bytes.`
);
localSdp = '';
}
this.context.previousSdpOffer = new SDP(localSdp);
const subscribe = new SignalingClientSubscribe(
this.context.meetingSessionConfiguration.credentials.attendeeId,
localSdp,
this.context.meetingSessionConfiguration.urls.audioHostURL,
this.context.realtimeController.realtimeIsLocalAudioMuted(),
false,
videoSubscriptions,
isSendingStreams,
this.context.videoStreamIndex.localStreamDescriptions(),
// TODO: handle check-in mode, or remove this param
true,
compressedSDPOffer
);
if (
this.context.videoDownlinkBandwidthPolicy.getServerSideNetworkAdaption !== undefined &&
!serverSideNetworkAdaptionIsNoneOrDefault(
this.context.videoDownlinkBandwidthPolicy.getServerSideNetworkAdaption()
) &&
this.context.videoDownlinkBandwidthPolicy.getVideoPreferences !== undefined
) {
// Set initial configuration for the receive streams indicated by the rest of the subscribe
subscribe.videoSubscriptionConfiguration = convertVideoPreferencesToSignalingClientVideoSubscriptionConfiguration(
this.context,
videoSubscriptions.map((streamId: number) => {
return streamId === 0 ? 0 : this.context.videoStreamIndex.groupIdForStreamId(streamId);
}),
this.context.videoDownlinkBandwidthPolicy.getVideoPreferences()
);
}
this.context.logger.info(`sending subscribe: ${JSON.stringify(subscribe)}`);
this.context.signalingClient.subscribe(subscribe);
const subscribeAckFrame = await this.receiveSubscribeAck();
this.context.logger.info(`got subscribe ack: ${JSON.stringify(subscribeAckFrame)}`);
let decompressedText = '';
if (subscribeAckFrame.compressedSdpAnswer && subscribeAckFrame.compressedSdpAnswer.length) {
decompressedText = this.textCompressor.decompress(
subscribeAckFrame.compressedSdpAnswer,
this.context.previousSdpAnswerAsString
);
if (decompressedText.length === 0) {
this.context.sdpAnswer = '';
this.context.previousSdpAnswerAsString = '';
this.logAndThrow(`Error occurred while trying to decompress the SDP answer.`);
}
this.context.logger.info(
`Decompressed the SDP message from ${subscribeAckFrame.compressedSdpAnswer.length} to ${decompressedText.length} bytes.`
);
this.context.sdpAnswer = decompressedText;
} else {
this.context.sdpAnswer = subscribeAckFrame.sdpAnswer;
}
this.context.previousSdpAnswerAsString = this.context.sdpAnswer;
this.context.videoStreamIndex.integrateSubscribeAckFrame(subscribeAckFrame);
}
// Our backends currently expect the video subscriptions passed in subscribe to precisely
// line up with the media sections, with a zero for any video send or inactive section.
//
// Firefox occasionally tosses stopped transceivers at the end of the SDP without reason
// and in general we don't want to be at the mercy of SDP sections not being in the same
// order as `getTransceivers`, so we simply recalculate the array here to enforce that
// expected invarient until we refactor our signaling to simply take a mapping of MID to
// subscription.
//
// This only works on Unified Plan SDPs
private fixUpSubscriptionOrder(sdp: string, videoSubscriptions: number[]): number[] {
if (this.context.transceiverController.getMidForStreamId === undefined) {
return videoSubscriptions;
}
const midsToStreamIds = new Map<string, number>();
for (const streamId of videoSubscriptions) {
// The local description will have been set by the time this task is running, so all
// of the transceivers should have `mid` set by now (see comment above `getMidForStreamId`)
const mid = this.context.transceiverController.getMidForStreamId(streamId);
if (mid === undefined) {
if (streamId !== 0) {
// Send section or inactive section
this.logger.warn(`Could not find MID for stream ID: ${streamId}`);
}
continue;
}
midsToStreamIds.set(mid, streamId);
}
const sections = new SDP(sdp).mediaSections();
const newSubscriptions: number[] = [];
for (const section of sections) {
if (section.mediaType !== 'video') {
continue;
}
if (section.direction === 'recvonly') {
const streamId = midsToStreamIds.get(section.mid);
if (streamId === undefined) {
this.logger.warn(`Could not find stream ID for MID: ${section.mid}`);
continue;
}
newSubscriptions.push(streamId);
} else {
newSubscriptions.push(0);
}
}
this.logger.info(
`Fixed up ${JSON.stringify(videoSubscriptions)} to ${JSON.stringify(
newSubscriptions
)} (may be same))}`
);
return newSubscriptions;
}
private receiveSubscribeAck(): Promise<SdkSubscribeAckFrame> {
return new Promise((resolve, reject) => {
const context = this.context;
class Interceptor implements SignalingClientObserver, TaskCanceler {
constructor(private signalingClient: SignalingClient) {}
cancel(): void {
this.signalingClient.removeObserver(this);
reject(
new Error(
`SubscribeAndReceiveSubscribeAckTask got canceled while waiting for SdkSubscribeAckFrame`
)
);
}
handleSignalingClientEvent(event: SignalingClientEvent): void {
if (event.isConnectionTerminated()) {
const message = `SubscribeAndReceiveSubscribeAckTask connection was terminated with code ${event.closeCode} and reason: ${event.closeReason}`;
context.logger.warn(message);
let statusCode: MeetingSessionStatusCode = MeetingSessionStatusCode.TaskFailed;
if (event.closeCode >= 4500 && event.closeCode < 4600) {
statusCode = MeetingSessionStatusCode.SignalingInternalServerError;
}
context.audioVideoController.handleMeetingSessionStatus(
new MeetingSessionStatus(statusCode),
new Error(message)
);
return;
}
if (
event.type !== SignalingClientEventType.ReceivedSignalFrame ||
event.message.type !== SdkSignalFrame.Type.SUBSCRIBE_ACK
) {
return;
}
this.signalingClient.removeObserver(this);
// @ts-ignore: force cast to SdkSubscribeAckFrame
const subackFrame: SdkSubscribeAckFrame = event.message.suback;
resolve(subackFrame);
}
}
const interceptor = new Interceptor(this.context.signalingClient);
this.context.signalingClient.registerObserver(interceptor);
this.taskCanceler = interceptor;
});
}
}