-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsmallWebRTCTransport.ts
More file actions
1111 lines (973 loc) · 33.1 KB
/
smallWebRTCTransport.ts
File metadata and controls
1111 lines (973 loc) · 33.1 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cloneDeep from "lodash/cloneDeep";
import {
APIRequest,
isAPIRequest,
logger,
makeRequest,
MessageTooLargeError,
RTVIError,
RTVIMessage,
PipecatClientOptions,
Tracks,
Transport,
TransportStartError,
TransportState,
UnsupportedFeatureError,
} from "@pipecat-ai/client-js";
import { MediaManager } from "../../../lib/media-mgmt/mediaManager";
import { DailyMediaManager } from "../../../lib/media-mgmt/dailyMediaManager";
class TrackStatusMessage {
type = "trackStatus";
receiver_index: number;
enabled: boolean;
constructor(receiver_index: number, enabled: boolean) {
this.receiver_index = receiver_index;
this.enabled = enabled;
}
}
class WebRTCTrack {
track: MediaStreamTrack;
status: "new" | "muted" | "unmuted" | "ended";
constructor(track: MediaStreamTrack) {
this.track = track;
this.status = "new";
}
}
export type IceConfig = {
iceServers?: RTCIceServer[];
};
export type SmallWebRTCTransportConnectionOptions = {
/** @deprecated Use webrtcRequestParams instead */
connectionUrl?: string;
/** @deprecated Use webrtcRequestParams instead */
webrtcUrl?: string;
webrtcRequestParams?: APIRequest;
iceConfig?: IceConfig;
};
export interface SmallWebRTCTransportConstructorOptions
extends SmallWebRTCTransportConnectionOptions {
iceServers?: RTCIceServer[];
waitForICEGathering?: boolean;
audioCodec?: string;
videoCodec?: string;
mediaManager?: MediaManager;
offerUrlTemplate?: string;
}
const RENEGOTIATE_TYPE = "renegotiate";
class RenegotiateMessage {
type = RENEGOTIATE_TYPE;
}
const PEER_LEFT_TYPE = "peerLeft";
class PeerLeftMessageMessage {
type = PEER_LEFT_TYPE;
}
type OutboundSignallingMessage = TrackStatusMessage;
type InboundSignallingMessage = RenegotiateMessage | PeerLeftMessageMessage;
// Interface for the structure of the signalling message
const SIGNALLING_TYPE = "signalling";
class SignallingMessageObject {
type: typeof SIGNALLING_TYPE = SIGNALLING_TYPE;
message: InboundSignallingMessage | OutboundSignallingMessage;
constructor(message: InboundSignallingMessage | OutboundSignallingMessage) {
this.message = message;
}
}
const AUDIO_TRANSCEIVER_INDEX = 0;
const VIDEO_TRANSCEIVER_INDEX = 1;
const SCREEN_VIDEO_TRANSCEIVER_INDEX = 2;
/**
* SmallWebRTCTransport is a class that provides a client-side
* interface for connecting to the SmallWebRTCTransport provided by Pipecat
*/
export class SmallWebRTCTransport extends Transport {
public static SERVICE_NAME = "small-webrtc-transport";
private _webrtcRequest: APIRequest | null = null;
// Trigger when the peer connection is finally ready or in case it has failed all the attempts to connect
private _connectResolved: ((value: PromiseLike<void> | void) => void) | null =
null;
private _connectFailed: ((reason?: any) => void) | null = null;
// Utilities for audio.
declare private mediaManager: MediaManager;
private pc: RTCPeerConnection | null = null;
private dc: RTCDataChannel | null = null;
private audioCodec: string | null | "default" = null;
private videoCodec: string | null | "default" = null;
private pc_id: string | null = null;
private offerUrlTemplate: string | null = null;
private reconnectionAttempts = 0;
private maxReconnectionAttempts = 3;
private isReconnecting = false;
private keepAliveInterval: number | null = null;
private _iceServers: RTCIceServer[] = [];
private readonly _waitForICEGathering: boolean;
private _incomingTracks: Map<string, WebRTCTrack> = new Map();
private _canSendIceCandidates: boolean = false;
private _candidateQueue: RTCIceCandidate[] = [];
private __flushTimeout: ReturnType<typeof setTimeout> | null = null;
private _flushDelay = 200;
constructor(opts: SmallWebRTCTransportConstructorOptions = {}) {
super();
this._iceServers = opts.iceServers ?? [];
this._waitForICEGathering = opts.waitForICEGathering ?? false;
this.audioCodec = opts.audioCodec ?? null;
this.videoCodec = opts.videoCodec ?? null;
this.offerUrlTemplate = opts.offerUrlTemplate ?? null;
this._webrtcRequest = this._resolveRequestInfo(opts);
this.mediaManager =
opts.mediaManager ||
new DailyMediaManager(
false,
false,
async (event) => {
if (!this.pc) {
return;
}
if (event.type == "audio") {
logger.info("SmallWebRTCMediaManager replacing audio track");
await this.getAudioTransceiver().sender.replaceTrack(event.track);
} else if (event.type == "video") {
logger.info("SmallWebRTCMediaManager replacing video track");
await this.getVideoTransceiver().sender.replaceTrack(event.track);
} else if (event.type == "screenVideo") {
logger.info("SmallWebRTCMediaManager replacing screen video track");
await this.getScreenVideoTransceiver().sender.replaceTrack(
event.track,
);
} else if (event.type == "screenAudio") {
logger.info(
"SmallWebRTCMediaManager does not yet support screen audio. Track is ignored.",
);
}
},
(event) =>
logger.debug("SmallWebRTCMediaManager Track stopped:", event),
);
}
public initialize(
options: PipecatClientOptions,
messageHandler: (ev: RTVIMessage) => void,
): void {
this._options = options;
this._callbacks = options.callbacks ?? {};
this._onMessage = messageHandler;
this.mediaManager.setClientOptions(options);
this.state = "disconnected";
logger.debug("[RTVI Transport] Initialized");
}
async initDevices() {
this.state = "initializing";
await this.mediaManager.initialize();
this.state = "initialized";
}
setAudioCodec(audioCodec: string | null): void {
this.audioCodec = audioCodec;
}
setVideoCodec(videoCodec: string | null): void {
this.videoCodec = videoCodec;
}
_resolveRequestInfo(
params:
| SmallWebRTCTransportConstructorOptions
| SmallWebRTCTransportConnectionOptions,
): APIRequest | null {
let requestInfo: APIRequest | null = null;
const _webrtcUrl = params.webrtcUrl ?? params.connectionUrl ?? null;
if (_webrtcUrl) {
const key = params.webrtcUrl ? "webrtcUrl" : "connectionUrl";
logger.warn(`${key} is deprecated. Use webrtcRequestParams instead.`);
if (params.webrtcRequestParams) {
logger.warn(
`Both ${key} and webrtcRequestParams provided. Using webrtcRequestParams.`,
);
} else {
if (typeof _webrtcUrl === "string") {
requestInfo = { endpoint: _webrtcUrl };
} else {
logger.error(`Invalid ${key} provided in params. Ignoring.`);
}
}
}
if (params.webrtcRequestParams) {
if (isAPIRequest(params.webrtcRequestParams)) {
// Override any previous request set in the constructor, do not try to merge
requestInfo = params.webrtcRequestParams;
} else {
logger.error(
`Invalid webrtcRequestParams provided in params. Ignoring.`,
);
}
}
return requestInfo ?? this._webrtcRequest;
}
_getStartEndpointAsString(): string | undefined {
const startEndpoint = this.startBotParams?.endpoint;
switch (typeof startEndpoint) {
case "string":
return startEndpoint;
case "object":
if (startEndpoint instanceof URL) {
return startEndpoint.toString();
}
if (startEndpoint instanceof Request) {
return startEndpoint.url;
}
}
return;
}
private _isValidObject(value: unknown): value is object {
if (value === null || value === undefined) return false;
if (typeof value !== "object") {
throw new RTVIError("Invalid connection parameters");
}
return true;
}
private _fixConnectionOptionsParams(
params: Record<string, any>,
supportedKeys: string[],
): SmallWebRTCTransportConnectionOptions {
const snakeToCamel = (snakeCaseString: string) => {
return snakeCaseString.replace(/_([a-z,A-Z])/g, (_, letter) =>
letter.toUpperCase(),
);
};
let result: SmallWebRTCTransportConnectionOptions = {};
let sessionId;
for (const [key, val] of Object.entries(params)) {
const camelKey = snakeToCamel(key);
if (camelKey === "sessionId") {
sessionId = val;
continue;
}
if (!supportedKeys.includes(camelKey)) {
logger.warn(`Unrecognized connection parameter: ${key}. Ignored.`);
continue;
}
result[camelKey as keyof SmallWebRTCTransportConnectionOptions] =
val as any;
}
if (sessionId && this._shouldUseStartBotFallback(result)) {
result.webrtcRequestParams =
this._buildRequestParamsBasedOnStartBotParams(sessionId);
}
return result;
}
private _shouldUseStartBotFallback(
options: SmallWebRTCTransportConnectionOptions,
): boolean {
const hasStartEndpoint = !!this._getStartEndpointAsString();
const hasNoConnectionParams =
!options.webrtcUrl &&
!options.connectionUrl &&
!options.webrtcRequestParams;
return hasStartEndpoint && hasNoConnectionParams;
}
private _buildRequestParamsBasedOnStartBotParams(
sessionId: string,
): APIRequest {
const startEndpoint = this._getStartEndpointAsString()!;
const offerUrl = this.offerUrlTemplate
? this.offerUrlTemplate.replace(":sessionId", sessionId)
: startEndpoint.replace("/start", `/sessions/${sessionId}/api/offer`);
const offerRequestData = this.startBotParams!.requestData
? (this.startBotParams!.requestData as any).body
: undefined;
return {
endpoint: offerUrl,
headers: this.startBotParams!.headers,
requestData: offerRequestData,
};
}
_validateConnectionParams(
connectParams: unknown,
): SmallWebRTCTransportConnectionOptions | undefined {
if (!this._isValidObject(connectParams)) return undefined;
const params = connectParams as Record<string, any>;
const supportedKeys = [
"webrtcUrl",
"connectionUrl",
"webrtcRequestParams",
"iceConfig",
];
const fixedParams = this._fixConnectionOptionsParams(params, supportedKeys);
const webrtcRequestParams = this._resolveRequestInfo(fixedParams);
if (webrtcRequestParams) {
fixedParams.webrtcRequestParams = webrtcRequestParams;
}
delete fixedParams.connectionUrl;
delete fixedParams.webrtcUrl;
if (Object.keys(fixedParams).length === 0) {
return undefined;
}
return fixedParams;
}
async _connect(
connectParams?: SmallWebRTCTransportConnectionOptions,
): Promise<void> {
if (this._abortController?.signal.aborted) return;
this.state = "connecting";
if (connectParams?.iceConfig?.iceServers) {
this._iceServers = connectParams?.iceConfig?.iceServers;
}
// Note: There is no need to validate the params here, as they were already
// validated and fixed in the parent class's connect() method (which calls
// _validateConnectionParams() and passes the result to _connect()).
this._webrtcRequest =
connectParams?.webrtcRequestParams ?? this._webrtcRequest;
if (!this._webrtcRequest) {
logger.error("No request details provided for WebRTC connection");
this.state = "error";
throw new TransportStartError();
}
await this.mediaManager.connect();
await this.startNewPeerConnection();
if (this._abortController?.signal.aborted) return;
if (this.dc?.readyState !== "open") {
// Wait until we are actually connected and the data channel is ready
await new Promise<void>((resolve, reject) => {
this._connectResolved = resolve;
this._connectFailed = reject;
});
}
this.state = "connected";
this._callbacks.onConnected?.();
}
private syncTrackStatus() {
// Sending the current status from the tracks to Pipecat
this.sendSignallingMessage(
new TrackStatusMessage(
AUDIO_TRANSCEIVER_INDEX,
this.mediaManager.isMicEnabled,
),
);
this.sendSignallingMessage(
new TrackStatusMessage(
VIDEO_TRANSCEIVER_INDEX,
this.mediaManager.isCamEnabled,
),
);
if (this.mediaManager.supportsScreenShare) {
this.sendSignallingMessage(
new TrackStatusMessage(
SCREEN_VIDEO_TRANSCEIVER_INDEX,
this.mediaManager.isSharingScreen &&
!!this.mediaManager.tracks().local.screenVideo,
),
);
}
}
sendReadyMessage() {
this.state = "ready";
// Sending message that the client is ready, just for testing
//this.dc?.send(JSON.stringify({id: 'clientReady', label: 'rtvi-ai', type:'client-ready'}))
this.sendMessage(RTVIMessage.clientReady());
}
sendMessage(message: RTVIMessage) {
if (!this.dc || this.dc.readyState !== "open") {
logger.warn(`Datachannel is not ready. Message not sent: ${message}`);
return;
}
const getSizeInBytes = (obj: any) => {
const jsonString = JSON.stringify(obj);
const encoder = new TextEncoder();
const bytes = encoder.encode(jsonString);
return bytes.length;
};
const objectSize = getSizeInBytes(message);
const maxSize = this.pc?.sctp?.maxMessageSize ?? 64 * 1024;
if (objectSize > maxSize) {
throw new MessageTooLargeError(
"Message data too large. Max size is " + maxSize,
);
}
this.dc?.send(JSON.stringify(message));
}
private sendSignallingMessage(message: OutboundSignallingMessage) {
if (!this.dc || this.dc.readyState !== "open") {
logger.warn(`Datachannel is not ready. Message not sent: ${message}`);
return;
}
const signallingMessage = new SignallingMessageObject(message);
this.dc?.send(JSON.stringify(signallingMessage));
}
async _disconnect(): Promise<void> {
this.state = "disconnecting";
await this.stop();
this.state = "disconnected";
}
private createPeerConnection(): RTCPeerConnection {
const config: RTCConfiguration = {
iceServers: this._iceServers,
};
let pc = new RTCPeerConnection(config);
pc.onicecandidate = async (event) => {
if (event.candidate) {
logger.debug("New ICE candidate:", event.candidate);
await this.sendIceCandidate(event.candidate);
} else {
logger.info("All ICE candidates have been sent.");
}
};
pc.addEventListener("icegatheringstatechange", () => {
if (
pc.iceGatheringState === "complete" &&
pc.iceConnectionState === "checking" &&
this._waitForICEGathering
) {
logger.info(
"Ice gathering completed and connection is still checking. Trying to reconnect.",
);
// If ICE gathering has completed and the previous connection was still in the "checking" state,
// we will reconnect to use all the new ICE candidates.
void this.attemptReconnection(false);
}
});
pc.addEventListener("iceconnectionstatechange", () =>
this.handleICEConnectionStateChange(),
);
logger.debug(`iceConnectionState: ${pc.iceConnectionState}`);
pc.addEventListener("signalingstatechange", () => {
logger.debug(`signalingState: ${this.pc!.signalingState}`);
if (this.pc!.signalingState == "stable") {
this.handleReconnectionCompleted();
}
});
logger.debug(`signalingState: ${pc.signalingState}`);
pc.addEventListener("track", (evt: RTCTrackEvent) => {
const streamType = evt.transceiver
? evt.transceiver.mid === "0"
? "microphone"
: evt.transceiver.mid === "1"
? "camera"
: "screenVideo"
: null;
if (!streamType) {
logger.warn("Received track without transceiver mid", evt);
return;
}
logger.debug(`Received new remote track for ${streamType}`);
this._incomingTracks.set(streamType, new WebRTCTrack(evt.track));
evt.track.addEventListener("unmute", () => {
const t = this._incomingTracks.get(streamType);
if (!t) return;
logger.debug(`Remote track unmuted: ${streamType}`);
t.status = "unmuted";
this._callbacks.onTrackStarted?.(evt.track);
});
evt.track.addEventListener("mute", () => {
const t = this._incomingTracks.get(streamType);
if (!t || t.status !== "unmuted") return;
logger.debug(`Remote track muted: ${streamType}`);
t.status = "muted";
this._callbacks.onTrackStopped?.(evt.track);
});
evt.track.addEventListener("ended", () => {
logger.debug(`Remote track ended: ${streamType}`);
this._callbacks.onTrackStopped?.(evt.track);
this._incomingTracks.delete(streamType);
});
});
return pc;
}
private handleICEConnectionStateChange(): void {
if (!this.pc) return;
logger.debug(`ICE Connection State: ${this.pc.iceConnectionState}`);
if (this.pc.iceConnectionState === "failed") {
logger.debug("ICE connection failed, attempting restart.");
void this.attemptReconnection(true);
} else if (this.pc.iceConnectionState === "disconnected") {
// Waiting before trying to reconnect to see if it handles it automatically
setTimeout(() => {
if (this.pc?.iceConnectionState === "disconnected") {
logger.debug("Still disconnected, attempting reconnection.");
void this.attemptReconnection(true);
}
}, 5000);
}
}
private handleReconnectionCompleted() {
this.reconnectionAttempts = 0;
this.isReconnecting = false;
}
private async attemptReconnection(
recreatePeerConnection: boolean = false,
): Promise<void> {
if (this.isReconnecting) {
logger.debug("Reconnection already in progress, skipping.");
return;
}
if (this.reconnectionAttempts >= this.maxReconnectionAttempts) {
logger.debug("Max reconnection attempts reached. Stopping transport.");
await this.stop();
return;
}
this.isReconnecting = true;
this.reconnectionAttempts++;
logger.debug(`Reconnection attempt ${this.reconnectionAttempts}...`);
// aiortc does not seem to work when just trying to restart the ice
// so for this case we create a new peer connection on both sides
if (recreatePeerConnection) {
const oldPC = this.pc;
await this.startNewPeerConnection(recreatePeerConnection);
if (oldPC) {
logger.debug("closing old peer connection");
this.closePeerConnection(oldPC);
}
} else {
await this.negotiate();
}
}
private async waitForIceGatheringComplete(timeoutMs = 2000): Promise<void> {
const pc = this.pc!;
if (pc.iceGatheringState === "complete") return;
logger.info(
"Waiting for ICE gathering to complete. Current state:",
pc.iceGatheringState,
);
return new Promise<void>((resolve) => {
let timeoutId: ReturnType<typeof setTimeout>;
const cleanup = () => {
pc.removeEventListener("icegatheringstatechange", checkState);
clearTimeout(timeoutId);
};
const checkState = () => {
logger.debug("icegatheringstatechange:", pc.iceGatheringState);
if (pc.iceGatheringState === "complete") {
cleanup();
resolve();
}
};
const onTimeout = () => {
logger.debug(`ICE gathering timed out after ${timeoutMs} ms.`);
cleanup();
resolve();
};
pc.addEventListener("icegatheringstatechange", checkState);
timeoutId = setTimeout(onTimeout, timeoutMs);
// Checking the state again to avoid race conditions
checkState();
});
}
private async sendIceCandidate(candidate: RTCIceCandidate): Promise<void> {
if (!this._webrtcRequest) {
logger.error("No request details provided for WebRTC connection");
return;
}
this._candidateQueue.push(candidate);
// We are sending all the ice candidates each 200ms
if (!this.__flushTimeout) {
this.__flushTimeout = setTimeout(
() => this.flushIceCandidates(),
this._flushDelay,
);
}
}
private async flushIceCandidates(): Promise<void> {
this.__flushTimeout = null;
if (
!this._webrtcRequest ||
this._candidateQueue.length === 0 ||
!this._canSendIceCandidates
)
return;
// Drain queue
const candidates = this._candidateQueue.splice(
0,
this._candidateQueue.length,
);
try {
const headers = new Headers({
"Content-Type": "application/json",
...Object.fromEntries(
(this._webrtcRequest.headers ?? new Headers()).entries(),
),
});
const payload = {
pc_id: this.pc_id,
candidates: candidates.map((c) => ({
candidate: c.candidate,
sdp_mid: c.sdpMid,
sdp_mline_index: c.sdpMLineIndex,
})),
};
await fetch(this._webrtcRequest.endpoint, {
method: "PATCH",
headers,
body: JSON.stringify(payload),
});
} catch (e) {
logger.error(`Failed to send ICE candidate: ${e}`);
}
}
private async negotiate(
recreatePeerConnection: boolean = false,
): Promise<void> {
if (!this.pc) {
return Promise.reject("Peer connection is not initialized");
}
if (!this._webrtcRequest) {
logger.error("No request details provided for WebRTC connection");
this.state = "error";
throw new TransportStartError();
}
try {
// Create offer
const offer = await this.pc.createOffer();
await this.pc.setLocalDescription(offer);
// Wait for ICE gathering to complete
if (this._waitForICEGathering) {
await this.waitForIceGatheringComplete();
}
let offerSdp = this.pc!.localDescription!;
// Filter audio codec
if (this.audioCodec && this.audioCodec !== "default") {
// @ts-ignore
offerSdp.sdp = this.sdpFilterCodec(
"audio",
this.audioCodec,
offerSdp.sdp,
);
}
// Filter video codec
if (this.videoCodec && this.videoCodec !== "default") {
// @ts-ignore
offerSdp.sdp = this.sdpFilterCodec(
"video",
this.videoCodec,
offerSdp.sdp,
);
}
logger.debug(`Will create offer for peerId: ${this.pc_id}`);
// Send offer to server
const request = cloneDeep(this._webrtcRequest);
const requestData: {
sdp: string;
type: string;
pc_id: string | null;
restart_pc: boolean;
requestData?: any;
} = {
sdp: offerSdp.sdp,
type: offerSdp.type as string,
pc_id: this.pc_id,
restart_pc: recreatePeerConnection,
};
if (this._webrtcRequest.requestData) {
requestData.requestData = this._webrtcRequest.requestData;
}
request.requestData = requestData;
const answer: RTCSessionDescriptionInit = (await makeRequest(
request,
)) as RTCSessionDescriptionInit;
// @ts-ignore
this.pc_id = answer.pc_id;
// @ts-ignore
logger.debug(`Received answer for peer connection id ${answer.pc_id}`);
await this.pc!.setRemoteDescription(answer);
} catch (e) {
logger.debug(
`Reconnection attempt ${this.reconnectionAttempts} failed: ${e}`,
);
this.isReconnecting = false;
setTimeout(() => this.attemptReconnection(true), 2000);
}
}
private addInitialTransceivers() {
// Transceivers always appear in creation-order for both peers
// For now we support 3 transceivers meant to hold the following
// tracks in the given order:
// audio, video, screenVideo
this.pc!.addTransceiver("audio", { direction: "sendrecv" });
this.pc!.addTransceiver("video", { direction: "sendrecv" });
if (this.mediaManager.supportsScreenShare) {
// For now, we only support receiving a single video track
this.pc!.addTransceiver("video", { direction: "sendonly" });
}
}
private getAudioTransceiver() {
// Transceivers always appear in creation-order for both peers
// Look at addInitialTransceivers
return this.pc!.getTransceivers()[AUDIO_TRANSCEIVER_INDEX];
}
private getVideoTransceiver() {
// Transceivers always appear in creation-order for both peers
// Look at addInitialTransceivers
return this.pc!.getTransceivers()[VIDEO_TRANSCEIVER_INDEX];
}
private getScreenVideoTransceiver() {
// Transceivers always appear in creation-order for both peers
// Look at addInitialTransceivers
return this.pc!.getTransceivers()[SCREEN_VIDEO_TRANSCEIVER_INDEX];
}
private async startNewPeerConnection(
recreatePeerConnection: boolean = false,
) {
this.pc = this.createPeerConnection();
this.addInitialTransceivers();
this.dc = this.createDataChannel("chat", { ordered: true });
await this.addUserMedia();
await this.negotiate(recreatePeerConnection);
// Sending the ice candidates
this._canSendIceCandidates = true;
await this.flushIceCandidates();
}
private async addUserMedia(): Promise<void> {
logger.debug(`addUserMedia this.tracks(): ${this.tracks()}`);
let audioTrack = this.tracks().local.audio;
logger.debug(`addUserMedia audioTrack: ${audioTrack}`);
if (audioTrack) {
await this.getAudioTransceiver().sender.replaceTrack(audioTrack);
}
let videoTrack = this.tracks().local.video;
logger.debug(`addUserMedia videoTrack: ${videoTrack}`);
if (videoTrack) {
await this.getVideoTransceiver().sender.replaceTrack(videoTrack);
}
if (this.mediaManager.supportsScreenShare) {
videoTrack = this.tracks().local.screenVideo;
logger.debug(`addUserMedia screenVideoTrack: ${videoTrack}`);
if (videoTrack) {
await this.getScreenVideoTransceiver().sender.replaceTrack(videoTrack);
}
}
}
// Method to handle a general message (this can be expanded for other types of messages)
handleMessage(message: string): void {
try {
const messageObj = JSON.parse(message); // Type is `any` initially
logger.debug("received message:", messageObj);
// Check if it's a signalling message
if (messageObj.type === SIGNALLING_TYPE) {
void this.handleSignallingMessage(
messageObj as SignallingMessageObject,
); // Delegate to handleSignallingMessage
} else {
// Bubble any messages with rtvi-ai label
if (messageObj.label === "rtvi-ai") {
this._onMessage({
id: messageObj.id,
type: messageObj.type,
data: messageObj.data,
} as RTVIMessage);
}
}
} catch (error) {
logger.error("Failed to parse JSON message:", error);
}
}
// Method to handle signalling messages specifically
async handleSignallingMessage(
messageObj: SignallingMessageObject,
): Promise<void> {
// Cast the object to the correct type after verification
const signallingMessage = messageObj as SignallingMessageObject;
// Handle different signalling message types
switch (signallingMessage.message.type) {
case RENEGOTIATE_TYPE:
void this.attemptReconnection(false);
break;
case PEER_LEFT_TYPE:
void this.disconnect();
break;
default:
logger.warn("Unknown signalling message:", signallingMessage.message);
}
}
private createDataChannel(
label: string,
options: RTCDataChannelInit,
): RTCDataChannel {
const dc = this.pc!.createDataChannel(label, options);
dc.addEventListener("close", () => {
logger.debug("datachannel closed");
if (this.keepAliveInterval) {
clearInterval(this.keepAliveInterval);
this.keepAliveInterval = null;
}
});
dc.addEventListener("open", () => {
logger.debug("datachannel opened");
if (this._connectResolved) {
this.syncTrackStatus();
this._connectResolved();
this._connectResolved = null;
this._connectFailed = null;
}
// @ts-ignore
this.keepAliveInterval = setInterval(() => {
const message = "ping: " + new Date().getTime();
dc.send(message);
}, 1000);
});
dc.addEventListener("message", (evt: MessageEvent) => {
let message = evt.data;
this.handleMessage(message);
});
return dc;
}
private closePeerConnection(pc: RTCPeerConnection) {
pc.getTransceivers().forEach((transceiver) => {
if (transceiver.stop) {
transceiver.stop();
}
});
pc.getSenders().forEach((sender) => {
sender.track?.stop();
});
pc.close();
}
private async stop(): Promise<void> {
if (!this.pc) {
logger.debug("Peer connection is already closed or null.");
return;
}
if (this.dc) {
this.dc.close();
}
this.closePeerConnection(this.pc);
this.pc = null;
await this.mediaManager.disconnect();
// For some reason after we close the peer connection, it is not triggering the listeners
this.pc_id = null;
this.reconnectionAttempts = 0;
this.isReconnecting = false;
this._callbacks.onDisconnected?.();
this._candidateQueue = [];
this._canSendIceCandidates = false;
if (this._connectFailed) {
this._connectFailed();
}
this._connectFailed = null;
this._connectResolved = null;
}
getAllMics(): Promise<MediaDeviceInfo[]> {
return this.mediaManager.getAllMics();
}
getAllCams(): Promise<MediaDeviceInfo[]> {
return this.mediaManager.getAllCams();
}
getAllSpeakers(): Promise<MediaDeviceInfo[]> {
return this.mediaManager.getAllSpeakers();
}
async updateMic(micId: string): Promise<void> {
return this.mediaManager.updateMic(micId);
}
updateCam(camId: string): void {
return this.mediaManager.updateCam(camId);
}
updateSpeaker(speakerId: string): void {
return this.mediaManager.updateSpeaker(speakerId);
}
get selectedMic(): MediaDeviceInfo | Record<string, never> {
return this.mediaManager.selectedMic;
}
get selectedCam(): MediaDeviceInfo | Record<string, never> {
return this.mediaManager.selectedCam;
}
get selectedSpeaker(): MediaDeviceInfo | Record<string, never> {
return this.mediaManager.selectedSpeaker;
}
set iceServers(iceServers: RTCIceServer[]) {
this._iceServers = iceServers;
}
get iceServers() {
return this._iceServers;
}
enableMic(enable: boolean): void {
this.mediaManager.enableMic(enable);
this.sendSignallingMessage(