@decartai/sdk 0.0.66 → 0.0.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -0
- package/dist/index.d.ts +2 -2
- package/dist/realtime/client.d.ts +7 -2
- package/dist/realtime/client.js +48 -119
- package/dist/realtime/mirror-stream.js +116 -0
- package/dist/realtime/{diagnostics.d.ts → observability/diagnostics.d.ts} +1 -1
- package/dist/realtime/observability/realtime-observability.js +109 -0
- package/dist/realtime/{telemetry-reporter.js → observability/telemetry-reporter.js} +3 -3
- package/dist/realtime/observability/webrtc-stats.d.ts +147 -0
- package/dist/realtime/observability/webrtc-stats.js +278 -0
- package/dist/realtime/subscribe-client.d.ts +3 -1
- package/dist/realtime/webrtc-connection.js +18 -19
- package/dist/realtime/webrtc-manager.js +24 -3
- package/package.json +1 -1
- package/dist/realtime/webrtc-stats.d.ts +0 -59
- package/dist/realtime/webrtc-stats.js +0 -154
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
//#region src/realtime/observability/webrtc-stats.d.ts
|
|
2
|
+
type WebRTCStats = {
|
|
3
|
+
timestamp: number;
|
|
4
|
+
video: {
|
|
5
|
+
framesDecoded: number;
|
|
6
|
+
framesDropped: number;
|
|
7
|
+
framesReceived: number;
|
|
8
|
+
keyFramesDecoded: number;
|
|
9
|
+
framesPerSecond: number;
|
|
10
|
+
frameWidth: number;
|
|
11
|
+
frameHeight: number;
|
|
12
|
+
bytesReceived: number;
|
|
13
|
+
packetsReceived: number;
|
|
14
|
+
packetsLost: number;
|
|
15
|
+
jitter: number;
|
|
16
|
+
/** Estimated inbound bitrate in bits/sec, computed from bytesReceived delta. */
|
|
17
|
+
bitrate: number;
|
|
18
|
+
freezeCount: number;
|
|
19
|
+
totalFreezesDuration: number;
|
|
20
|
+
/** Delta: packets lost since previous sample. */
|
|
21
|
+
packetsLostDelta: number;
|
|
22
|
+
/** Delta: frames dropped since previous sample. */
|
|
23
|
+
framesDroppedDelta: number;
|
|
24
|
+
/** Delta: freeze count since previous sample. */
|
|
25
|
+
freezeCountDelta: number;
|
|
26
|
+
/** Delta: freeze duration (seconds) since previous sample. */
|
|
27
|
+
freezeDurationDelta: number;
|
|
28
|
+
/** NACKs sent to the sender (requesting packet retransmission). */
|
|
29
|
+
nackCount: number;
|
|
30
|
+
nackCountDelta: number;
|
|
31
|
+
/** PLIs sent to the sender (full frame retransmission request). */
|
|
32
|
+
pliCount: number;
|
|
33
|
+
/** FIRs sent to the sender (forced intra-refresh request). */
|
|
34
|
+
firCount: number;
|
|
35
|
+
/**
|
|
36
|
+
* Average decode time (ms/frame), cumulative since stream start.
|
|
37
|
+
* Derived from totalDecodeTime/framesDecoded. `null` if the browser
|
|
38
|
+
* hasn't produced the underlying counters yet.
|
|
39
|
+
*/
|
|
40
|
+
avgDecodeTimeMs: number | null;
|
|
41
|
+
/** Average jitter-buffer time (ms/frame emitted). Cumulative. */
|
|
42
|
+
avgJitterBufferMs: number | null;
|
|
43
|
+
/**
|
|
44
|
+
* Average total processing delay (ms/frame decoded) — from network
|
|
45
|
+
* receive to decoder output. Cumulative.
|
|
46
|
+
*/
|
|
47
|
+
avgProcessingDelayMs: number | null;
|
|
48
|
+
/** Average inter-frame delay at the decoder (ms). */
|
|
49
|
+
avgInterFrameDelayMs: number | null;
|
|
50
|
+
/**
|
|
51
|
+
* Std-dev of inter-frame delay (ms), computed from
|
|
52
|
+
* totalInterFrameDelay + totalSquaredInterFrameDelay.
|
|
53
|
+
*/
|
|
54
|
+
interFrameDelayStdDevMs: number | null;
|
|
55
|
+
/** Current target delay of the jitter buffer (ms). */
|
|
56
|
+
jitterBufferTargetDelayMs: number | null;
|
|
57
|
+
/** Current minimum delay of the jitter buffer (ms). */
|
|
58
|
+
jitterBufferMinimumDelayMs: number | null;
|
|
59
|
+
/** Which decoder the browser picked (e.g. "libvpx", "ExternalDecoder"). */
|
|
60
|
+
decoderImplementation: string;
|
|
61
|
+
} | null;
|
|
62
|
+
audio: {
|
|
63
|
+
bytesReceived: number;
|
|
64
|
+
packetsReceived: number;
|
|
65
|
+
packetsLost: number;
|
|
66
|
+
jitter: number;
|
|
67
|
+
/** Estimated inbound bitrate in bits/sec, computed from bytesReceived delta. */
|
|
68
|
+
bitrate: number;
|
|
69
|
+
/** Delta: packets lost since previous sample. */
|
|
70
|
+
packetsLostDelta: number;
|
|
71
|
+
} | null;
|
|
72
|
+
/** Outbound video track stats (from the local camera/screen share being sent). */
|
|
73
|
+
outboundVideo: {
|
|
74
|
+
/** Why the encoder is limiting quality: "none", "bandwidth", "cpu", or "other". */
|
|
75
|
+
qualityLimitationReason: string;
|
|
76
|
+
/** Cumulative time (seconds) spent in each quality limitation state. */
|
|
77
|
+
qualityLimitationDurations: Record<string, number>;
|
|
78
|
+
bytesSent: number;
|
|
79
|
+
packetsSent: number;
|
|
80
|
+
framesPerSecond: number;
|
|
81
|
+
frameWidth: number;
|
|
82
|
+
frameHeight: number;
|
|
83
|
+
/** Estimated outbound bitrate in bits/sec, computed from bytesSent delta. */
|
|
84
|
+
bitrate: number;
|
|
85
|
+
/** Encoder's current target bitrate in kbps (BWE output). */
|
|
86
|
+
targetBitrateKbps: number | null;
|
|
87
|
+
/** Average encode time per frame (ms), cumulative. */
|
|
88
|
+
avgEncodeTimeMs: number | null;
|
|
89
|
+
/** Average packet send delay (ms), cumulative. */
|
|
90
|
+
avgPacketSendDelayMs: number | null;
|
|
91
|
+
/** Average quantization parameter across encoded frames (lower is better). */
|
|
92
|
+
avgQp: number | null;
|
|
93
|
+
/** NACKs received from receiver (retransmission requests). */
|
|
94
|
+
nackCount: number;
|
|
95
|
+
/** PLIs received from receiver. */
|
|
96
|
+
pliCount: number;
|
|
97
|
+
/** FIRs received from receiver. */
|
|
98
|
+
firCount: number;
|
|
99
|
+
retransmittedBytesSent: number;
|
|
100
|
+
retransmittedPacketsSent: number;
|
|
101
|
+
/** Which encoder the browser picked (e.g. "libvpx", "SimulcastEncoderAdapter"). */
|
|
102
|
+
encoderImplementation: string;
|
|
103
|
+
} | null;
|
|
104
|
+
/**
|
|
105
|
+
* Remote-inbound stats — what the far end reports *about its reception
|
|
106
|
+
* of our outbound stream*. Answers "does the server think we're lossy?"
|
|
107
|
+
* independently of what we see locally. Populated from
|
|
108
|
+
* `remote-inbound-rtp` reports.
|
|
109
|
+
*/
|
|
110
|
+
remoteInbound: {
|
|
111
|
+
fractionLost: number | null;
|
|
112
|
+
/** In seconds. */
|
|
113
|
+
jitter: number | null;
|
|
114
|
+
/** In seconds. Often more accurate than connection.currentRoundTripTime. */
|
|
115
|
+
roundTripTime: number | null;
|
|
116
|
+
} | null;
|
|
117
|
+
connection: {
|
|
118
|
+
/** Current round-trip time in seconds, or null if unavailable. */
|
|
119
|
+
currentRoundTripTime: number | null;
|
|
120
|
+
/** Available outgoing bitrate estimate in bits/sec, or null if unavailable. */
|
|
121
|
+
availableOutgoingBitrate: number | null;
|
|
122
|
+
/**
|
|
123
|
+
* Selected ICE candidate pairs (usually one per PC). Populated from
|
|
124
|
+
* the `candidate-pair` report with state="succeeded" plus the matching
|
|
125
|
+
* `local-candidate` / `remote-candidate` lookups. Lets diagnostic tools
|
|
126
|
+
* tell direct-UDP sessions from TURN-relayed ones — the path affects
|
|
127
|
+
* jitter and failure modes, so this is essential signal for
|
|
128
|
+
* benchmarking and incident triage.
|
|
129
|
+
*/
|
|
130
|
+
selectedCandidatePairs: Array<{
|
|
131
|
+
local: IceCandidateInfo;
|
|
132
|
+
remote: IceCandidateInfo;
|
|
133
|
+
}>;
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
/** One side of an ICE candidate pair (sender or receiver). */
|
|
137
|
+
type IceCandidateInfo = {
|
|
138
|
+
/** "host" | "srflx" | "prflx" | "relay" */
|
|
139
|
+
candidateType: string;
|
|
140
|
+
/** IP (v4 or v6). May be `""` for mDNS-obfuscated host candidates. */
|
|
141
|
+
address: string;
|
|
142
|
+
port: number;
|
|
143
|
+
/** "udp" | "tcp" */
|
|
144
|
+
protocol: string;
|
|
145
|
+
};
|
|
146
|
+
//#endregion
|
|
147
|
+
export { WebRTCStats };
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
//#region src/realtime/observability/webrtc-stats.ts
|
|
2
|
+
const DEFAULT_INTERVAL_MS = 1e3;
|
|
3
|
+
const MIN_INTERVAL_MS = 500;
|
|
4
|
+
var WebRTCStatsCollector = class {
|
|
5
|
+
source = null;
|
|
6
|
+
intervalId = null;
|
|
7
|
+
prevBytesVideo = 0;
|
|
8
|
+
prevBytesAudio = 0;
|
|
9
|
+
prevBytesSentVideo = 0;
|
|
10
|
+
prevTimestamp = 0;
|
|
11
|
+
prevPacketsLostVideo = 0;
|
|
12
|
+
prevFramesDropped = 0;
|
|
13
|
+
prevFreezeCount = 0;
|
|
14
|
+
prevFreezeDuration = 0;
|
|
15
|
+
prevPacketsLostAudio = 0;
|
|
16
|
+
prevNackCountInbound = 0;
|
|
17
|
+
onStats = null;
|
|
18
|
+
intervalMs;
|
|
19
|
+
constructor(options = {}) {
|
|
20
|
+
this.intervalMs = Math.max(options.intervalMs ?? DEFAULT_INTERVAL_MS, MIN_INTERVAL_MS);
|
|
21
|
+
}
|
|
22
|
+
/** Attach to a stats provider and start polling. */
|
|
23
|
+
start(source, onStats) {
|
|
24
|
+
this.stop();
|
|
25
|
+
this.source = source;
|
|
26
|
+
this.onStats = onStats;
|
|
27
|
+
this.prevBytesVideo = 0;
|
|
28
|
+
this.prevBytesAudio = 0;
|
|
29
|
+
this.prevBytesSentVideo = 0;
|
|
30
|
+
this.prevTimestamp = 0;
|
|
31
|
+
this.prevPacketsLostVideo = 0;
|
|
32
|
+
this.prevFramesDropped = 0;
|
|
33
|
+
this.prevFreezeCount = 0;
|
|
34
|
+
this.prevFreezeDuration = 0;
|
|
35
|
+
this.prevPacketsLostAudio = 0;
|
|
36
|
+
this.prevNackCountInbound = 0;
|
|
37
|
+
this.intervalId = setInterval(() => this.collect(), this.intervalMs);
|
|
38
|
+
}
|
|
39
|
+
/** Stop polling and release resources. */
|
|
40
|
+
stop() {
|
|
41
|
+
if (this.intervalId !== null) {
|
|
42
|
+
clearInterval(this.intervalId);
|
|
43
|
+
this.intervalId = null;
|
|
44
|
+
}
|
|
45
|
+
this.source = null;
|
|
46
|
+
this.onStats = null;
|
|
47
|
+
}
|
|
48
|
+
isRunning() {
|
|
49
|
+
return this.intervalId !== null;
|
|
50
|
+
}
|
|
51
|
+
async collect() {
|
|
52
|
+
if (!this.source || !this.onStats) return;
|
|
53
|
+
try {
|
|
54
|
+
const rawStats = await this.source.getStats();
|
|
55
|
+
const stats = this.parse(rawStats);
|
|
56
|
+
this.onStats(stats);
|
|
57
|
+
} catch {
|
|
58
|
+
this.stop();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
parse(rawStats) {
|
|
62
|
+
const now = performance.now();
|
|
63
|
+
const elapsed = this.prevTimestamp > 0 ? (now - this.prevTimestamp) / 1e3 : 0;
|
|
64
|
+
let video = null;
|
|
65
|
+
let audio = null;
|
|
66
|
+
let outboundVideo = null;
|
|
67
|
+
let remoteInbound = null;
|
|
68
|
+
const connection = {
|
|
69
|
+
currentRoundTripTime: null,
|
|
70
|
+
availableOutgoingBitrate: null,
|
|
71
|
+
selectedCandidatePairs: []
|
|
72
|
+
};
|
|
73
|
+
const succeededPairs = [];
|
|
74
|
+
rawStats.forEach((report) => {
|
|
75
|
+
if (report.type === "inbound-rtp" && report.kind === "video") {
|
|
76
|
+
const bytesReceived = report.bytesReceived ?? 0;
|
|
77
|
+
const bitrate = elapsed > 0 ? (bytesReceived - this.prevBytesVideo) * 8 / elapsed : 0;
|
|
78
|
+
this.prevBytesVideo = bytesReceived;
|
|
79
|
+
const r = report;
|
|
80
|
+
const packetsLost = r.packetsLost ?? 0;
|
|
81
|
+
const framesDropped = r.framesDropped ?? 0;
|
|
82
|
+
const freezeCount = r.freezeCount ?? 0;
|
|
83
|
+
const freezeDuration = r.totalFreezesDuration ?? 0;
|
|
84
|
+
const framesDecoded = r.framesDecoded ?? 0;
|
|
85
|
+
const nackCount = r.nackCount ?? 0;
|
|
86
|
+
const jbEmitted = r.jitterBufferEmittedCount ?? 0;
|
|
87
|
+
const totalDecodeTime = r.totalDecodeTime ?? 0;
|
|
88
|
+
const totalProcessingDelay = r.totalProcessingDelay ?? 0;
|
|
89
|
+
const totalInterFrameDelay = r.totalInterFrameDelay ?? 0;
|
|
90
|
+
const totalSquaredInterFrameDelay = r.totalSquaredInterFrameDelay ?? 0;
|
|
91
|
+
const jitterBufferDelay = r.jitterBufferDelay ?? 0;
|
|
92
|
+
const jitterBufferTargetDelay = r.jitterBufferTargetDelay ?? 0;
|
|
93
|
+
const jitterBufferMinimumDelay = r.jitterBufferMinimumDelay ?? 0;
|
|
94
|
+
const avgDecodeTimeMs = framesDecoded > 0 ? totalDecodeTime / framesDecoded * 1e3 : null;
|
|
95
|
+
const avgProcessingDelayMs = framesDecoded > 0 ? totalProcessingDelay / framesDecoded * 1e3 : null;
|
|
96
|
+
const avgInterFrameDelayMs = framesDecoded > 0 ? totalInterFrameDelay / framesDecoded * 1e3 : null;
|
|
97
|
+
const interFrameDelayStdDevMs = framesDecoded > 0 ? Math.sqrt(Math.max(0, totalSquaredInterFrameDelay / framesDecoded - (totalInterFrameDelay / framesDecoded) ** 2)) * 1e3 : null;
|
|
98
|
+
const avgJitterBufferMs = jbEmitted > 0 ? jitterBufferDelay / jbEmitted * 1e3 : null;
|
|
99
|
+
const jitterBufferTargetDelayMs = jbEmitted > 0 ? jitterBufferTargetDelay / jbEmitted * 1e3 : null;
|
|
100
|
+
const jitterBufferMinimumDelayMs = jbEmitted > 0 ? jitterBufferMinimumDelay / jbEmitted * 1e3 : null;
|
|
101
|
+
video = {
|
|
102
|
+
framesDecoded,
|
|
103
|
+
framesDropped,
|
|
104
|
+
framesReceived: r.framesReceived ?? 0,
|
|
105
|
+
keyFramesDecoded: r.keyFramesDecoded ?? 0,
|
|
106
|
+
framesPerSecond: r.framesPerSecond ?? 0,
|
|
107
|
+
frameWidth: r.frameWidth ?? 0,
|
|
108
|
+
frameHeight: r.frameHeight ?? 0,
|
|
109
|
+
bytesReceived,
|
|
110
|
+
packetsReceived: r.packetsReceived ?? 0,
|
|
111
|
+
packetsLost,
|
|
112
|
+
jitter: r.jitter ?? 0,
|
|
113
|
+
bitrate: Math.round(bitrate),
|
|
114
|
+
freezeCount,
|
|
115
|
+
totalFreezesDuration: freezeDuration,
|
|
116
|
+
packetsLostDelta: Math.max(0, packetsLost - this.prevPacketsLostVideo),
|
|
117
|
+
framesDroppedDelta: Math.max(0, framesDropped - this.prevFramesDropped),
|
|
118
|
+
freezeCountDelta: Math.max(0, freezeCount - this.prevFreezeCount),
|
|
119
|
+
freezeDurationDelta: Math.max(0, freezeDuration - this.prevFreezeDuration),
|
|
120
|
+
nackCount,
|
|
121
|
+
nackCountDelta: Math.max(0, nackCount - this.prevNackCountInbound),
|
|
122
|
+
pliCount: r.pliCount ?? 0,
|
|
123
|
+
firCount: r.firCount ?? 0,
|
|
124
|
+
avgDecodeTimeMs,
|
|
125
|
+
avgJitterBufferMs,
|
|
126
|
+
avgProcessingDelayMs,
|
|
127
|
+
avgInterFrameDelayMs,
|
|
128
|
+
interFrameDelayStdDevMs,
|
|
129
|
+
jitterBufferTargetDelayMs,
|
|
130
|
+
jitterBufferMinimumDelayMs,
|
|
131
|
+
decoderImplementation: r.decoderImplementation ?? ""
|
|
132
|
+
};
|
|
133
|
+
this.prevPacketsLostVideo = packetsLost;
|
|
134
|
+
this.prevFramesDropped = framesDropped;
|
|
135
|
+
this.prevFreezeCount = freezeCount;
|
|
136
|
+
this.prevFreezeDuration = freezeDuration;
|
|
137
|
+
this.prevNackCountInbound = nackCount;
|
|
138
|
+
}
|
|
139
|
+
if (report.type === "outbound-rtp" && report.kind === "video") {
|
|
140
|
+
const r = report;
|
|
141
|
+
const bytesSent = r.bytesSent ?? 0;
|
|
142
|
+
const packetsSent = r.packetsSent ?? 0;
|
|
143
|
+
const frameWidth = r.frameWidth ?? 0;
|
|
144
|
+
const frameHeight = r.frameHeight ?? 0;
|
|
145
|
+
const pixels = frameWidth * frameHeight;
|
|
146
|
+
const framesEncoded = r.framesEncoded ?? 0;
|
|
147
|
+
const totalEncodeTime = r.totalEncodeTime ?? 0;
|
|
148
|
+
const totalPacketSendDelay = r.totalPacketSendDelay ?? 0;
|
|
149
|
+
const qpSum = r.qpSum ?? 0;
|
|
150
|
+
const nackCount = r.nackCount ?? 0;
|
|
151
|
+
const pliCount = r.pliCount ?? 0;
|
|
152
|
+
const firCount = r.firCount ?? 0;
|
|
153
|
+
const retransmittedBytesSent = r.retransmittedBytesSent ?? 0;
|
|
154
|
+
const retransmittedPacketsSent = r.retransmittedPacketsSent ?? 0;
|
|
155
|
+
const targetBitrate = r.targetBitrate ?? null;
|
|
156
|
+
const avgEncodeTimeMs = framesEncoded > 0 ? totalEncodeTime / framesEncoded * 1e3 : null;
|
|
157
|
+
const avgPacketSendDelayMs = packetsSent > 0 ? totalPacketSendDelay / packetsSent * 1e3 : null;
|
|
158
|
+
const avgQp = framesEncoded > 0 ? qpSum / framesEncoded : null;
|
|
159
|
+
if (outboundVideo === null) outboundVideo = {
|
|
160
|
+
qualityLimitationReason: r.qualityLimitationReason ?? "none",
|
|
161
|
+
qualityLimitationDurations: r.qualityLimitationDurations ?? {},
|
|
162
|
+
bytesSent,
|
|
163
|
+
packetsSent,
|
|
164
|
+
framesPerSecond: r.framesPerSecond ?? 0,
|
|
165
|
+
frameWidth,
|
|
166
|
+
frameHeight,
|
|
167
|
+
bitrate: 0,
|
|
168
|
+
targetBitrateKbps: targetBitrate != null ? Math.round(targetBitrate / 1e3) : null,
|
|
169
|
+
avgEncodeTimeMs,
|
|
170
|
+
avgPacketSendDelayMs,
|
|
171
|
+
avgQp,
|
|
172
|
+
nackCount,
|
|
173
|
+
pliCount,
|
|
174
|
+
firCount,
|
|
175
|
+
retransmittedBytesSent,
|
|
176
|
+
retransmittedPacketsSent,
|
|
177
|
+
encoderImplementation: r.encoderImplementation ?? ""
|
|
178
|
+
};
|
|
179
|
+
else {
|
|
180
|
+
outboundVideo.bytesSent += bytesSent;
|
|
181
|
+
outboundVideo.packetsSent += packetsSent;
|
|
182
|
+
outboundVideo.nackCount += nackCount;
|
|
183
|
+
outboundVideo.pliCount += pliCount;
|
|
184
|
+
outboundVideo.firCount += firCount;
|
|
185
|
+
outboundVideo.retransmittedBytesSent += retransmittedBytesSent;
|
|
186
|
+
outboundVideo.retransmittedPacketsSent += retransmittedPacketsSent;
|
|
187
|
+
if (pixels > outboundVideo.frameWidth * outboundVideo.frameHeight) {
|
|
188
|
+
outboundVideo.frameWidth = frameWidth;
|
|
189
|
+
outboundVideo.frameHeight = frameHeight;
|
|
190
|
+
outboundVideo.framesPerSecond = r.framesPerSecond ?? 0;
|
|
191
|
+
outboundVideo.qualityLimitationReason = r.qualityLimitationReason ?? "none";
|
|
192
|
+
outboundVideo.qualityLimitationDurations = r.qualityLimitationDurations ?? {};
|
|
193
|
+
outboundVideo.targetBitrateKbps = targetBitrate != null ? Math.round(targetBitrate / 1e3) : null;
|
|
194
|
+
outboundVideo.avgEncodeTimeMs = avgEncodeTimeMs;
|
|
195
|
+
outboundVideo.avgPacketSendDelayMs = avgPacketSendDelayMs;
|
|
196
|
+
outboundVideo.avgQp = avgQp;
|
|
197
|
+
outboundVideo.encoderImplementation = r.encoderImplementation ?? "";
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (report.type === "remote-inbound-rtp" && report.kind === "video") {
|
|
202
|
+
const r = report;
|
|
203
|
+
remoteInbound = {
|
|
204
|
+
fractionLost: r.fractionLost ?? null,
|
|
205
|
+
jitter: r.jitter ?? null,
|
|
206
|
+
roundTripTime: r.roundTripTime ?? null
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
if (report.type === "inbound-rtp" && report.kind === "audio") {
|
|
210
|
+
const bytesReceived = report.bytesReceived ?? 0;
|
|
211
|
+
const bitrate = elapsed > 0 ? (bytesReceived - this.prevBytesAudio) * 8 / elapsed : 0;
|
|
212
|
+
this.prevBytesAudio = bytesReceived;
|
|
213
|
+
const r = report;
|
|
214
|
+
const audioPacketsLost = r.packetsLost ?? 0;
|
|
215
|
+
audio = {
|
|
216
|
+
bytesReceived,
|
|
217
|
+
packetsReceived: r.packetsReceived ?? 0,
|
|
218
|
+
packetsLost: audioPacketsLost,
|
|
219
|
+
jitter: r.jitter ?? 0,
|
|
220
|
+
bitrate: Math.round(bitrate),
|
|
221
|
+
packetsLostDelta: Math.max(0, audioPacketsLost - this.prevPacketsLostAudio)
|
|
222
|
+
};
|
|
223
|
+
this.prevPacketsLostAudio = audioPacketsLost;
|
|
224
|
+
}
|
|
225
|
+
if (report.type === "candidate-pair") {
|
|
226
|
+
const r = report;
|
|
227
|
+
if (r.state === "succeeded") {
|
|
228
|
+
connection.currentRoundTripTime = r.currentRoundTripTime ?? null;
|
|
229
|
+
connection.availableOutgoingBitrate = r.availableOutgoingBitrate ?? null;
|
|
230
|
+
const localId = r.localCandidateId;
|
|
231
|
+
const remoteId = r.remoteCandidateId;
|
|
232
|
+
if (localId && remoteId) succeededPairs.push({
|
|
233
|
+
localId,
|
|
234
|
+
remoteId
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
if (succeededPairs.length > 0) {
|
|
240
|
+
const toInfo = (id) => {
|
|
241
|
+
const c = rawStats.get(id);
|
|
242
|
+
if (!c) return null;
|
|
243
|
+
return {
|
|
244
|
+
candidateType: c.candidateType ?? "",
|
|
245
|
+
address: c.address ?? c.ip ?? "",
|
|
246
|
+
port: c.port ?? 0,
|
|
247
|
+
protocol: c.protocol ?? ""
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
for (const { localId, remoteId } of succeededPairs) {
|
|
251
|
+
const local = toInfo(localId);
|
|
252
|
+
const remote = toInfo(remoteId);
|
|
253
|
+
if (local && remote) connection.selectedCandidatePairs.push({
|
|
254
|
+
local,
|
|
255
|
+
remote
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const ov = outboundVideo;
|
|
260
|
+
if (ov !== null) {
|
|
261
|
+
const outBitrate = elapsed > 0 ? (ov.bytesSent - this.prevBytesSentVideo) * 8 / elapsed : 0;
|
|
262
|
+
ov.bitrate = Math.max(0, Math.round(outBitrate));
|
|
263
|
+
this.prevBytesSentVideo = ov.bytesSent;
|
|
264
|
+
}
|
|
265
|
+
this.prevTimestamp = now;
|
|
266
|
+
return {
|
|
267
|
+
timestamp: Date.now(),
|
|
268
|
+
video,
|
|
269
|
+
audio,
|
|
270
|
+
outboundVideo,
|
|
271
|
+
connection,
|
|
272
|
+
remoteInbound
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
//#endregion
|
|
278
|
+
export { WebRTCStatsCollector };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DecartSDKError } from "../utils/errors.js";
|
|
2
|
-
import { DiagnosticEvent } from "./diagnostics.js";
|
|
3
2
|
import { ConnectionState } from "./types.js";
|
|
3
|
+
import { DiagnosticEvent } from "./observability/diagnostics.js";
|
|
4
|
+
import { WebRTCStats } from "./observability/webrtc-stats.js";
|
|
4
5
|
|
|
5
6
|
//#region src/realtime/subscribe-client.d.ts
|
|
6
7
|
|
|
@@ -8,6 +9,7 @@ type SubscribeEvents = {
|
|
|
8
9
|
connectionChange: ConnectionState;
|
|
9
10
|
error: DecartSDKError;
|
|
10
11
|
diagnostic: DiagnosticEvent;
|
|
12
|
+
stats: WebRTCStats;
|
|
11
13
|
};
|
|
12
14
|
type RealTimeSubscribeClient = {
|
|
13
15
|
isConnected: () => boolean;
|
|
@@ -4,17 +4,16 @@ import mitt from "mitt";
|
|
|
4
4
|
//#region src/realtime/webrtc-connection.ts
|
|
5
5
|
const ICE_SERVERS = [{ urls: "stun:stun.l.google.com:19302" }];
|
|
6
6
|
const SETUP_TIMEOUT_MS = 3e4;
|
|
7
|
-
const noopDiagnostic = () => {};
|
|
8
7
|
var WebRTCConnection = class {
|
|
9
8
|
pc = null;
|
|
10
9
|
ws = null;
|
|
11
10
|
localStream = null;
|
|
12
11
|
connectionReject = null;
|
|
13
12
|
logger;
|
|
14
|
-
|
|
13
|
+
observability;
|
|
15
14
|
state = "disconnected";
|
|
16
15
|
websocketMessagesEmitter = mitt();
|
|
17
|
-
constructor(callbacks
|
|
16
|
+
constructor(callbacks) {
|
|
18
17
|
this.callbacks = callbacks;
|
|
19
18
|
this.logger = callbacks.logger ?? {
|
|
20
19
|
debug() {},
|
|
@@ -22,7 +21,7 @@ var WebRTCConnection = class {
|
|
|
22
21
|
warn() {},
|
|
23
22
|
error() {}
|
|
24
23
|
};
|
|
25
|
-
this.
|
|
24
|
+
this.observability = callbacks.observability;
|
|
26
25
|
}
|
|
27
26
|
getPeerConnection() {
|
|
28
27
|
return this.pc;
|
|
@@ -46,7 +45,7 @@ var WebRTCConnection = class {
|
|
|
46
45
|
this.ws = new WebSocket(wsUrl);
|
|
47
46
|
this.ws.onopen = () => {
|
|
48
47
|
clearTimeout(timer);
|
|
49
|
-
this.
|
|
48
|
+
this.observability.diagnostic("phaseTiming", {
|
|
50
49
|
phase: "websocket",
|
|
51
50
|
durationMs: performance.now() - wsStart,
|
|
52
51
|
success: true
|
|
@@ -63,7 +62,7 @@ var WebRTCConnection = class {
|
|
|
63
62
|
this.ws.onerror = () => {
|
|
64
63
|
clearTimeout(timer);
|
|
65
64
|
const error = /* @__PURE__ */ new Error("WebSocket error");
|
|
66
|
-
this.
|
|
65
|
+
this.observability.diagnostic("phaseTiming", {
|
|
67
66
|
phase: "websocket",
|
|
68
67
|
durationMs: performance.now() - wsStart,
|
|
69
68
|
success: false,
|
|
@@ -85,7 +84,7 @@ var WebRTCConnection = class {
|
|
|
85
84
|
prompt: this.callbacks.initialPrompt?.text,
|
|
86
85
|
enhance: this.callbacks.initialPrompt?.enhance
|
|
87
86
|
}), connectAbort]);
|
|
88
|
-
this.
|
|
87
|
+
this.observability.diagnostic("phaseTiming", {
|
|
89
88
|
phase: "avatar-image",
|
|
90
89
|
durationMs: performance.now() - imageStart,
|
|
91
90
|
success: true
|
|
@@ -93,7 +92,7 @@ var WebRTCConnection = class {
|
|
|
93
92
|
} else if (this.callbacks.initialPrompt) {
|
|
94
93
|
const promptStart = performance.now();
|
|
95
94
|
await Promise.race([this.sendInitialPrompt(this.callbacks.initialPrompt), connectAbort]);
|
|
96
|
-
this.
|
|
95
|
+
this.observability.diagnostic("phaseTiming", {
|
|
97
96
|
phase: "initial-prompt",
|
|
98
97
|
durationMs: performance.now() - promptStart,
|
|
99
98
|
success: true
|
|
@@ -101,7 +100,7 @@ var WebRTCConnection = class {
|
|
|
101
100
|
} else if (localStream) {
|
|
102
101
|
const nullStart = performance.now();
|
|
103
102
|
await Promise.race([this.setImageBase64(null, { prompt: null }), connectAbort]);
|
|
104
|
-
this.
|
|
103
|
+
this.observability.diagnostic("phaseTiming", {
|
|
105
104
|
phase: "initial-prompt",
|
|
106
105
|
durationMs: performance.now() - nullStart,
|
|
107
106
|
success: true
|
|
@@ -113,7 +112,7 @@ var WebRTCConnection = class {
|
|
|
113
112
|
const checkConnection = setInterval(() => {
|
|
114
113
|
if (this.state === "connected" || this.state === "generating") {
|
|
115
114
|
clearInterval(checkConnection);
|
|
116
|
-
this.
|
|
115
|
+
this.observability.diagnostic("phaseTiming", {
|
|
117
116
|
phase: "webrtc-handshake",
|
|
118
117
|
durationMs: performance.now() - handshakeStart,
|
|
119
118
|
success: true
|
|
@@ -121,7 +120,7 @@ var WebRTCConnection = class {
|
|
|
121
120
|
resolve();
|
|
122
121
|
} else if (this.state === "disconnected") {
|
|
123
122
|
clearInterval(checkConnection);
|
|
124
|
-
this.
|
|
123
|
+
this.observability.diagnostic("phaseTiming", {
|
|
125
124
|
phase: "webrtc-handshake",
|
|
126
125
|
durationMs: performance.now() - handshakeStart,
|
|
127
126
|
success: false,
|
|
@@ -130,7 +129,7 @@ var WebRTCConnection = class {
|
|
|
130
129
|
reject(/* @__PURE__ */ new Error("Connection lost during WebRTC handshake"));
|
|
131
130
|
} else if (Date.now() >= deadline) {
|
|
132
131
|
clearInterval(checkConnection);
|
|
133
|
-
this.
|
|
132
|
+
this.observability.diagnostic("phaseTiming", {
|
|
134
133
|
phase: "webrtc-handshake",
|
|
135
134
|
durationMs: performance.now() - handshakeStart,
|
|
136
135
|
success: false,
|
|
@@ -141,7 +140,7 @@ var WebRTCConnection = class {
|
|
|
141
140
|
}, 100);
|
|
142
141
|
connectAbort.catch(() => clearInterval(checkConnection));
|
|
143
142
|
}), connectAbort]);
|
|
144
|
-
this.
|
|
143
|
+
this.observability.diagnostic("phaseTiming", {
|
|
145
144
|
phase: "total",
|
|
146
145
|
durationMs: performance.now() - totalStart,
|
|
147
146
|
success: true
|
|
@@ -219,7 +218,7 @@ var WebRTCConnection = class {
|
|
|
219
218
|
case "ice-candidate":
|
|
220
219
|
if (msg.candidate) {
|
|
221
220
|
await this.pc.addIceCandidate(msg.candidate);
|
|
222
|
-
this.
|
|
221
|
+
this.observability.diagnostic("iceCandidate", {
|
|
223
222
|
source: "remote",
|
|
224
223
|
candidateType: msg.candidate.candidate?.match(/typ (\w+)/)?.[1] ?? "unknown",
|
|
225
224
|
protocol: msg.candidate.candidate?.match(/udp|tcp/i)?.[0]?.toLowerCase() ?? "unknown"
|
|
@@ -332,7 +331,7 @@ var WebRTCConnection = class {
|
|
|
332
331
|
type: "ice-candidate",
|
|
333
332
|
candidate: e.candidate
|
|
334
333
|
});
|
|
335
|
-
if (e.candidate) this.
|
|
334
|
+
if (e.candidate) this.observability.diagnostic("iceCandidate", {
|
|
336
335
|
source: "local",
|
|
337
336
|
candidateType: e.candidate.type ?? "unknown",
|
|
338
337
|
protocol: e.candidate.protocol ?? "unknown",
|
|
@@ -344,7 +343,7 @@ var WebRTCConnection = class {
|
|
|
344
343
|
this.pc.onconnectionstatechange = () => {
|
|
345
344
|
if (!this.pc) return;
|
|
346
345
|
const s = this.pc.connectionState;
|
|
347
|
-
this.
|
|
346
|
+
this.observability.diagnostic("peerConnectionStateChange", {
|
|
348
347
|
state: s,
|
|
349
348
|
previousState: prevPcState,
|
|
350
349
|
timestampMs: performance.now()
|
|
@@ -359,7 +358,7 @@ var WebRTCConnection = class {
|
|
|
359
358
|
this.pc.oniceconnectionstatechange = () => {
|
|
360
359
|
if (!this.pc) return;
|
|
361
360
|
const newIceState = this.pc.iceConnectionState;
|
|
362
|
-
this.
|
|
361
|
+
this.observability.diagnostic("iceStateChange", {
|
|
363
362
|
state: newIceState,
|
|
364
363
|
previousState: prevIceState,
|
|
365
364
|
timestampMs: performance.now()
|
|
@@ -374,7 +373,7 @@ var WebRTCConnection = class {
|
|
|
374
373
|
this.pc.onsignalingstatechange = () => {
|
|
375
374
|
if (!this.pc) return;
|
|
376
375
|
const newState = this.pc.signalingState;
|
|
377
|
-
this.
|
|
376
|
+
this.observability.diagnostic("signalingStateChange", {
|
|
378
377
|
state: newState,
|
|
379
378
|
previousState: prevSignalingState,
|
|
380
379
|
timestampMs: performance.now()
|
|
@@ -398,7 +397,7 @@ var WebRTCConnection = class {
|
|
|
398
397
|
if (r.id === report.localCandidateId) localCandidate = r;
|
|
399
398
|
if (r.id === report.remoteCandidateId) remoteCandidate = r;
|
|
400
399
|
});
|
|
401
|
-
if (localCandidate && remoteCandidate) this.
|
|
400
|
+
if (localCandidate && remoteCandidate) this.observability.diagnostic("selectedCandidatePair", {
|
|
402
401
|
local: {
|
|
403
402
|
candidateType: String(localCandidate.candidateType ?? "unknown"),
|
|
404
403
|
protocol: String(localCandidate.protocol ?? "unknown"),
|