@relaymessenger/livekit 0.1.0-staging.0 → 0.1.0-staging.10

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/dist/livekit.js CHANGED
@@ -1,12 +1,28 @@
1
- import { AudioInput, AudioOutput } from "@livekit/agents";
1
+ import { AudioInput, AudioOutput, Future } from "@livekit/agents";
2
2
  import { AudioFrame } from "@livekit/rtc-node";
3
3
  import { TransformStream } from "node:stream/web";
4
4
  import { RelayCallTransport, } from "./transport.js";
5
+ /**
6
+ * The format LiveKit's own room input hands an AgentSession:
7
+ * `@livekit/agents` dist/voice/room_io/room_io.js:46-48
8
+ * `DEFAULT_ROOM_INPUT_OPTIONS = { audioSampleRate: 24e3, audioNumChannels: 1 }`,
9
+ * passed to the participant `AudioStream` (room_io.js:373-374). Plugins
10
+ * resample assuming the channel count they construct with
11
+ * (`@livekit/agents-plugin-google` 1.9.0 realtime_api.js:1307-1329
12
+ * `new AudioResampler(frame.sampleRate, 16000, 1)`; rtc-node's resampler reads
13
+ * raw bytes and ignores `frame.channels`), so the session must receive mono.
14
+ */
15
+ export const LIVEKIT_ROOM_INPUT_AUDIO = Object.freeze({
16
+ sampleRate: 24_000,
17
+ channelCount: 1,
18
+ });
5
19
  /** LiveKit Agents input backed by the remote Relay participant's WebRTC audio. */
6
20
  export class RelayAudioInput extends AudioInput {
7
21
  #transport;
8
22
  #writer;
9
23
  #onAudio;
24
+ /** Serial write chain: each frame waits for the writer, as `pipeTo` does in LiveKit's input. */
25
+ #writes = Promise.resolve();
10
26
  #attached = false;
11
27
  #closed = false;
12
28
  constructor(transport) {
@@ -19,7 +35,16 @@ export class RelayAudioInput extends AudioInput {
19
35
  if (this.#closed || !this.#attached)
20
36
  return;
21
37
  const audio = new AudioFrame(frame.samples, frame.sampleRate, frame.channelCount, frame.samples.length / frame.channelCount);
22
- void this.#writer.write(audio).catch(() => undefined);
38
+ // LiveKit's ParticipantAudioInputStream pipes its AudioStream into the
39
+ // session with `pipeTo(output.writable)` (room_io/_input.js:161-162),
40
+ // which awaits the writer before each chunk. The transport pushes and
41
+ // cannot be paused, so the frames wait here in order instead.
42
+ this.#writes = this.#writes
43
+ .then(async () => {
44
+ await this.#writer.ready;
45
+ await this.#writer.write(audio);
46
+ })
47
+ .catch(() => undefined);
23
48
  };
24
49
  this.#transport.on("audio", this.#onAudio);
25
50
  }
@@ -31,16 +56,30 @@ export class RelayAudioInput extends AudioInput {
31
56
  return;
32
57
  this.#closed = true;
33
58
  this.#transport.off("audio", this.#onAudio);
59
+ await this.#writes;
34
60
  await this.#writer.close().catch(() => undefined);
35
61
  await super.close();
36
62
  }
37
63
  }
38
- /** LiveKit Agents output that publishes TTS PCM to the Relay participant. */
64
+ /**
65
+ * LiveKit Agents output that publishes TTS PCM to the Relay participant.
66
+ *
67
+ * Shape copied from `@livekit/agents` `ParticipantAudioOutput`
68
+ * (dist/voice/room_io/_output.js): `captureFrame` hands the frame to the
69
+ * transport and returns without waiting for playout, `flush()` starts a playout
70
+ * task that reports `onPlaybackFinished` only once the transport has drained,
71
+ * and `clearBuffer()` resolves the interruption future so that task reports
72
+ * `interrupted: true` with the position actually played.
73
+ */
39
74
  export class RelayAudioOutput extends AudioOutput {
40
75
  #transport;
41
- #segmentDuration = 0;
42
- #segmentStarted = false;
43
- #segmentStartedAt = 0;
76
+ /** Seconds pushed to the transport in the open segment. */
77
+ #pushedDuration = 0;
78
+ #firstFrameEmitted = false;
79
+ #flushTask;
80
+ #flushDone = true;
81
+ /** Resolved by `clearBuffer()` with the milliseconds still queued at that moment. */
82
+ #interruptedFuture = new Future();
44
83
  #closed = false;
45
84
  constructor(transport, sampleRate = 48_000) {
46
85
  super(sampleRate, undefined, { pause: false });
@@ -49,38 +88,47 @@ export class RelayAudioOutput extends AudioOutput {
49
88
  async captureFrame(frame) {
50
89
  if (this.#closed)
51
90
  throw new Error("Relay LiveKit audio output is closed.");
91
+ if (this.#flushTask && !this.#flushDone) {
92
+ this.logger.error("captureFrame called while flush is in progress");
93
+ await this.#flushTask;
94
+ }
52
95
  await super.captureFrame(frame);
53
- if (!this.#segmentStarted) {
54
- this.#segmentStarted = true;
55
- this.#segmentStartedAt = Date.now();
56
- this.onPlaybackStarted(this.#segmentStartedAt);
96
+ if (!this.#firstFrameEmitted) {
97
+ this.#firstFrameEmitted = true;
98
+ this.onPlaybackStarted(Date.now());
57
99
  }
58
- this.#segmentDuration += frame.samplesPerChannel / frame.sampleRate;
100
+ this.#pushedDuration += frame.samplesPerChannel / frame.sampleRate;
101
+ // Resolves once the slices are queued; the engine's pump paces the wire.
59
102
  await this.#transport.writeAudio({
60
103
  samples: frame.data,
61
104
  sampleRate: frame.sampleRate,
62
105
  channelCount: frame.channels,
63
106
  });
64
107
  }
108
+ /** Mark the segment complete; `onPlaybackFinished` fires once the transport has drained. */
65
109
  flush() {
66
110
  super.flush();
67
- if (!this.#segmentStarted)
111
+ if (!this.#pushedDuration)
112
+ return;
113
+ if (this.#flushTask && !this.#flushDone)
68
114
  return;
69
- this.onPlaybackFinished({
70
- playbackPosition: this.#segmentDuration,
71
- interrupted: false,
115
+ this.#flushDone = false;
116
+ this.#flushTask = this.#waitForPlayoutTask().finally(() => {
117
+ this.#flushDone = true;
72
118
  });
73
- this.#resetSegment();
119
+ void this.#flushTask.catch(() => undefined);
74
120
  }
75
121
  clearBuffer() {
122
+ const queuedMs = this.#transport.queuedAudioMs();
76
123
  this.#transport.clearAudio();
77
- if (!this.#segmentStarted)
124
+ if (this.#interruptedFuture.done)
78
125
  return;
79
- this.onPlaybackFinished({
80
- playbackPosition: Math.min(this.#segmentDuration, Math.max(0, (Date.now() - this.#segmentStartedAt) / 1_000)),
81
- interrupted: true,
82
- });
83
- this.#resetSegment();
126
+ if (this.#pushedDuration === 0 && this.pendingPlayoutSegments === 0)
127
+ return;
128
+ if (!this.#flushTask || this.#flushDone)
129
+ this.flush();
130
+ if (!this.#interruptedFuture.done)
131
+ this.#interruptedFuture.resolve(queuedMs);
84
132
  }
85
133
  close() {
86
134
  if (this.#closed)
@@ -88,10 +136,19 @@ export class RelayAudioOutput extends AudioOutput {
88
136
  this.#closed = true;
89
137
  this.clearBuffer();
90
138
  }
91
- #resetSegment() {
92
- this.#segmentDuration = 0;
93
- this.#segmentStarted = false;
94
- this.#segmentStartedAt = 0;
139
+ async #waitForPlayoutTask() {
140
+ const interruptedFuture = this.#interruptedFuture;
141
+ await Promise.race([this.#transport.waitForPlayout(), interruptedFuture.await]);
142
+ const interrupted = interruptedFuture.done;
143
+ let playbackPosition = this.#pushedDuration;
144
+ if (interrupted) {
145
+ playbackPosition = Math.max(0, playbackPosition - interruptedFuture.result / 1_000);
146
+ }
147
+ this.#pushedDuration = 0;
148
+ this.#firstFrameEmitted = false;
149
+ if (this.#interruptedFuture === interruptedFuture)
150
+ this.#interruptedFuture = new Future();
151
+ this.onPlaybackFinished({ playbackPosition, interrupted });
95
152
  }
96
153
  }
97
154
  /** Build LiveKit Agents audio IO around an already-connected Relay transport. */
@@ -119,17 +176,27 @@ export class RelayLiveKitCall {
119
176
  const transportOptions = {
120
177
  relay: options.relay,
121
178
  callId: options.callId,
179
+ inboundAudio: LIVEKIT_ROOM_INPUT_AUDIO,
122
180
  ...(options.room ? { room: options.room } : {}),
181
+ ...(options.roomClient ? { roomClient: options.roomClient } : {}),
123
182
  ...(options.webRTC ? { webRTC: options.webRTC } : {}),
183
+ ...(options.iceServers ? { iceServers: options.iceServers } : {}),
184
+ ...(options.iceTransportPolicy ? { iceTransportPolicy: options.iceTransportPolicy } : {}),
124
185
  ...(options.iceGatheringTimeoutMs === undefined
125
186
  ? {}
126
187
  : { iceGatheringTimeoutMs: options.iceGatheringTimeoutMs }),
188
+ ...(options.sessionConnectTimeoutMs === undefined
189
+ ? {}
190
+ : { sessionConnectTimeoutMs: options.sessionConnectTimeoutMs }),
191
+ ...(options.mediaConnectTimeoutMs === undefined
192
+ ? {}
193
+ : { mediaConnectTimeoutMs: options.mediaConnectTimeoutMs }),
127
194
  ...(options.connectionTimeoutMs === undefined
128
195
  ? {}
129
196
  : { connectionTimeoutMs: options.connectionTimeoutMs }),
130
197
  };
131
198
  const transport = new RelayCallTransport(transportOptions);
132
- await transport.connect();
199
+ await transport.connect(options.signal ? { signal: options.signal } : {});
133
200
  return new RelayLiveKitCall(transport, createRelayLiveKitAudio(transport, options));
134
201
  }
135
202
  attach(session) {
@@ -150,6 +217,18 @@ export class RelayLiveKitCall {
150
217
  this.#session.output.audio = null;
151
218
  this.#session = undefined;
152
219
  }
220
+ /**
221
+ * Resolves once the person's audio has arrived and the room shows them
222
+ * connected (the transport's `peerAudio`). Await it before starting the
223
+ * AgentSession so the greeting is heard.
224
+ */
225
+ waitForPeerAudio(timeoutMs) {
226
+ return this.transport.waitForPeerAudio(timeoutMs);
227
+ }
228
+ /** ICE candidates and state transitions for this call, with a one-line summary for logs. */
229
+ diagnostics() {
230
+ return this.transport.diagnostics();
231
+ }
153
232
  setMuted(muted) {
154
233
  this.transport.setMuted(muted);
155
234
  }
@@ -1 +1 @@
1
- {"version":3,"file":"livekit.js","sourceRoot":"","sources":["../src/livekit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAqB,MAAM,iBAAiB,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAoC,MAAM,iBAAiB,CAAC;AAGpF,OAAO,EACL,kBAAkB,GAInB,MAAM,gBAAgB,CAAC;AAExB,kFAAkF;AAClF,MAAM,OAAO,eAAgB,SAAQ,UAAU;IACpC,UAAU,CAAqB;IAC/B,OAAO,CAA0C;IACjD,QAAQ,CAAmC;IACpD,SAAS,GAAG,KAAK,CAAC;IAClB,OAAO,GAAG,KAAK,CAAC;IAEhB,YAAY,SAA6B;QACvC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,MAAM,OAAO,GAAG,IAAI,eAAe,EAA0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,KAAc;QACrB,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,GAAG;QACD,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,IAAI,CAAC,OAAO;YAAE,OAAO;QACzB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,MAAM,EAAE,CAAC;QACd,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACpB,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QACzB,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;CACF"}
@@ -5,6 +5,46 @@ export interface RelayAudioFrame {
5
5
  sampleRate: number;
6
6
  channelCount: number;
7
7
  }
8
+ /** Standard `RTCIceServer` shape: STUN or TURN URLs with optional credentials. */
9
+ export interface RelayIceServer {
10
+ urls: string | string[];
11
+ username?: string;
12
+ credential?: string;
13
+ }
14
+ export type RelayIceTransportPolicy = "all" | "relay";
15
+ /**
16
+ * Returns the ICE servers for one peer connection. Called before the first
17
+ * peer and again before every restart, so short-lived TURN credentials can be
18
+ * minted per attempt (PartyTracks: a reconnect "will trigger new sessionId,
19
+ * new ice server credentials and a new peerConnection").
20
+ */
21
+ export type RelayIceServersProvider = (attempt: {
22
+ /** 0 for the first peer, then 1, 2, ... for each restart. */
23
+ restarts: number;
24
+ }) => RelayIceServer[] | Promise<RelayIceServer[]>;
25
+ /** Why the transport replaced its peer connection with a new SFU session. */
26
+ export type RelayCallRestartReason = "timeout" | "failed" | "disconnected" | "error";
27
+ export interface RelayCallRestartEvent {
28
+ reason: RelayCallRestartReason;
29
+ /** `diagnostics().summary` of the session being replaced, taken as it was given up. */
30
+ summary: string;
31
+ /** Restarts since `connect()`, this one included. */
32
+ restarts: number;
33
+ /** Backoff waited before the new peer was built. */
34
+ delayMs: number;
35
+ }
36
+ /**
37
+ * PCM format the engine decodes the remote participant's Opus into. The values
38
+ * are the ones libopus decodes to (`@evan/opus` lib.d.ts: `channels?: 1 | 2`,
39
+ * `sample_rate?: 8000 | 12000 | 16000 | 24000 | 48000`); the decoder itself
40
+ * resamples and downmixes, so no PCM is converted by hand.
41
+ */
42
+ export interface RelayInboundAudioFormat {
43
+ sampleRate: 8000 | 12000 | 16000 | 24000 | 48000;
44
+ channelCount: 1 | 2;
45
+ }
46
+ /** Opus's native rate and Relay's wire channel count. */
47
+ export declare const DEFAULT_INBOUND_AUDIO: Readonly<RelayInboundAudioFormat>;
8
48
  export type RelayCallEngine = "werift" | "wrtc";
9
49
  export interface RelayCallTransportOptions {
10
50
  relay: Relay;
@@ -15,8 +55,110 @@ export interface RelayCallTransportOptions {
15
55
  * and loads no native binding. `"wrtc"` loads `@roamhq/wrtc` on demand.
16
56
  */
17
57
  engine?: RelayCallEngine;
18
- /** Maximum wait for the WebRTC peer to become connected. Defaults to 15 seconds. */
58
+ /**
59
+ * STUN and TURN servers handed to the engine's `RTCPeerConnection`. Defaults
60
+ * to none: Cloudflare's SFU answers with its own host candidates. Set TURN
61
+ * servers when the agent runs behind a NAT or firewall that blocks UDP. A
62
+ * function is called before every peer connection, restarts included, so
63
+ * it can mint fresh TURN credentials each time.
64
+ */
65
+ iceServers?: RelayIceServer[] | RelayIceServersProvider;
66
+ /** `"relay"` forces every candidate through TURN. Defaults to `"all"`. */
67
+ iceTransportPolicy?: RelayIceTransportPolicy;
68
+ /**
69
+ * How long one SFU session has, after its answer is applied, to reach
70
+ * `connected` before the transport restarts onto a new session. Defaults to
71
+ * 5 seconds. `connect()` itself has no deadline: it waits through restarts
72
+ * until media connects, the Call ends, `close()` is called, or its signal aborts.
73
+ */
74
+ sessionConnectTimeoutMs?: number;
75
+ /** @deprecated Use `sessionConnectTimeoutMs`; now the per-session wait, not a `connect()` deadline. */
76
+ mediaConnectTimeoutMs?: number;
77
+ /** @deprecated Use `sessionConnectTimeoutMs`. */
19
78
  connectionTimeoutMs?: number;
79
+ /**
80
+ * PCM format of the `audio` events: the Opus decoder decodes the remote
81
+ * track straight to this rate and channel count. Defaults to 48 kHz stereo.
82
+ * The `"wrtc"` engine delivers libwebrtc's own format and accepts only the default.
83
+ */
84
+ inboundAudio?: RelayInboundAudioFormat;
85
+ /**
86
+ * Called once per call when outbound audio is queued but no RTP packet has
87
+ * been written for 2 s while media is connected. Receives the diagnostics
88
+ * summary. Defaults to a no-op; nothing is restarted.
89
+ */
90
+ onWarning?: (message: string) => void;
91
+ }
92
+ /** Packet counts for the other participant's track, as seen by this participant. */
93
+ export interface RelayCallInboundDiagnostics {
94
+ /** RTP packets received on the subscribed track. */
95
+ rtpPackets: number;
96
+ /** Opus packets the decoder rejected. */
97
+ decodeFailures: number;
98
+ /** PCM frames handed to the `audio` listeners. */
99
+ frames: number;
100
+ /** ms since `connect()` for the first and last RTP packet; undefined until one arrives. */
101
+ firstPacketAtMs: number | undefined;
102
+ lastPacketAtMs: number | undefined;
103
+ /** RTP packets received in the 5 s before `diagnostics()` was called. */
104
+ recentRtpPackets: number;
105
+ }
106
+ /** Packet counts for this participant's published track. */
107
+ export interface RelayCallOutboundDiagnostics {
108
+ /** PCM slices accepted from the caller and handed to the engine source. */
109
+ frames: number;
110
+ opusPackets: number;
111
+ /** RTP packets carrying the caller's audio. */
112
+ rtpPackets: number;
113
+ /** RTP packets carrying Opus silence, written while nothing was queued. */
114
+ silencePackets: number;
115
+ /** ms since `connect()` for the first and last RTP packet of either kind. */
116
+ firstPacketAtMs: number | undefined;
117
+ lastPacketAtMs: number | undefined;
118
+ /** RTP packets of either kind written in the 5 s before `diagnostics()` was called. */
119
+ recentRtpPackets: number;
120
+ /** Encoded packets waiting for the 20 ms pacer. */
121
+ queued: number;
122
+ /** `undefined` when the engine does not expose its pacer (`wrtc`). */
123
+ pacerAlive: boolean | undefined;
124
+ }
125
+ /** Room signaling frames counted since `connect()`. */
126
+ export interface RelayCallRoomDiagnostics {
127
+ roomStates: number;
128
+ /** Subscription (pull) offers received from the room. */
129
+ offers: number;
130
+ endedReason: string | undefined;
131
+ /** `error` frame messages, in order. */
132
+ errors: string[];
133
+ }
134
+ /** ICE facts recorded for one call, for logs and for the connect timeout error. */
135
+ export interface RelayCallIceDiagnostics {
136
+ /** Local candidates gathered, counted by type. */
137
+ local: {
138
+ host: number;
139
+ srflx: number;
140
+ relay: number;
141
+ other: number;
142
+ };
143
+ /** Remote candidates from the SFU answer: transport and port only, never the address. */
144
+ remote: Array<{
145
+ transport: string;
146
+ port: number;
147
+ }>;
148
+ /** ICE gathering, ICE connection and peer connection state changes since `connect()`. */
149
+ transitions: Array<{
150
+ kind: "gathering" | "ice" | "connection";
151
+ state: string;
152
+ atMs: number;
153
+ }>;
154
+ connected: boolean;
155
+ inbound: RelayCallInboundDiagnostics;
156
+ outbound: RelayCallOutboundDiagnostics;
157
+ room: RelayCallRoomDiagnostics;
158
+ /** Peer connections replaced by a new SFU session since `connect()`. */
159
+ restarts: number;
160
+ /** One-line rendering of the fields above. */
161
+ summary: string;
20
162
  }
21
163
  export interface RelayCallTransportCloseEvent {
22
164
  code: number;
@@ -26,13 +168,47 @@ export interface RelayCallTransportCloseEvent {
26
168
  type TransportEventMap = {
27
169
  audio: [RelayAudioFrame];
28
170
  connected: [];
171
+ /** A new peer connection published on a new SFU session; audio continues on it. */
172
+ restarted: [RelayCallRestartEvent];
29
173
  roomState: [CallRoomStateFrame];
174
+ /** The person's camera started (`true`) or stopped (`false`) sending, from `roomState`. */
175
+ remoteVideo: [boolean];
176
+ /**
177
+ * Once per call: the person's audio has reached this peer (first inbound
178
+ * frame on the pulled track) and `roomState` shows the person connected. The
179
+ * room pulls both directions together, so from here the person hears what is
180
+ * written; start speaking after this, as LiveKit Agents start a session once
181
+ * the participant is in the room.
182
+ */
183
+ peerAudio: [];
30
184
  ended: [CallRoomEndedFrame];
31
185
  error: [Error];
32
186
  close: [RelayCallTransportCloseEvent];
33
187
  };
34
188
  type TransportEvent = keyof TransportEventMap;
35
189
  type TransportListener<K extends TransportEvent> = (...args: TransportEventMap[K]) => void;
190
+ /**
191
+ * Restart rule, copied from PartyTracks and Cloudflare (PROTOCOL.md section 4):
192
+ * not `connected` 5 s after the SFU answer (Cloudflare's echo example waits
193
+ * 5000 ms; SFU operations block up to 5 s awaiting `connected`), `failed`, or
194
+ * `disconnected` for 7 s (PartyTracks.ts `timeoutSeconds = 7`). Backoff 250 ms
195
+ * x1.1 per attempt, capped at 10 s (PartyTracks `retryWithBackoff`
196
+ * `backoffFactor: 1.1`, rxjs-helpers.ts defaults). werift never reports
197
+ * `failed` on a session whose checks go unanswered (ice.js:983-986), so the
198
+ * 5 s timer is the trigger that fires in practice.
199
+ */
200
+ export declare const RESTART_CONNECT_TIMEOUT_MS = 5000;
201
+ export declare const RESTART_DISCONNECTED_MS = 7000;
202
+ export declare const RESTART_INITIAL_DELAY_MS = 250;
203
+ export declare const RESTART_BACKOFF_FACTOR = 1.1;
204
+ export declare const RESTART_MAX_DELAY_MS = 10000;
205
+ /**
206
+ * Backoff before restart number `attempt` (1-based) counted since the last
207
+ * session that connected. PartyTracks passes `resetOnSuccess: true` to rxjs
208
+ * `retry` (rxjs-helpers.ts:18, :39); this transport reads "success" as a
209
+ * session reaching `connected`.
210
+ */
211
+ export declare const restartDelayMs: (attempt: number) => number;
36
212
  export declare class RelayCallTransportError extends Error {
37
213
  readonly code?: string;
38
214
  constructor(message: string, code?: string);
@@ -41,23 +217,56 @@ export declare class RelayCallTransportError extends Error {
41
217
  * Provider-neutral Node WebRTC bridge for Relay Call rooms.
42
218
  *
43
219
  * The class owns Relay media negotiation internally. Higher-level adapters only
44
- * exchange PCM16 frames and call lifecycle events.
220
+ * exchange PCM16 frames and call lifecycle events. When an SFU session never
221
+ * connects or dies, the transport builds a new peer connection on a new
222
+ * session (PROTOCOL.md section 4); the audio source and the `audio` events
223
+ * carry on across the swap, so adapters only hear silence.
45
224
  */
46
225
  export declare class RelayCallTransport {
47
226
  #private;
48
227
  constructor(options: RelayCallTransportOptions);
49
228
  on<K extends TransportEvent>(event: K, listener: TransportListener<K>): this;
50
229
  off<K extends TransportEvent>(event: K, listener: TransportListener<K>): this;
51
- connect(): Promise<void>;
230
+ /**
231
+ * Join the room, publish, and resolve on the first `connected`. Dead SFU
232
+ * sessions are replaced as they are found (PROTOCOL.md section 4), with no
233
+ * overall deadline: this rejects only when the Call ends, the room or
234
+ * transport closes, the room reports an error, or `signal` aborts (which
235
+ * also closes the transport).
236
+ */
237
+ connect(options?: {
238
+ signal?: AbortSignal;
239
+ }): Promise<void>;
52
240
  /** Replace only the room signaling socket and replay the identical publication. */
53
241
  reconnect(): Promise<void>;
242
+ /**
243
+ * ICE candidates, state transitions, packet counts in both directions,
244
+ * room frame counts and restarts recorded for this call, with a one-line summary.
245
+ */
246
+ diagnostics(): RelayCallIceDiagnostics;
54
247
  /**
55
248
  * Feed interleaved PCM16 audio into Relay. Frames are split into 10 ms WebRTC
56
- * source slices and paced in real time, so adapters may push larger chunks.
249
+ * source slices and handed to the engine at once; the engine's 20 ms pump
250
+ * paces the wire, so adapters may push faster than real time (LiveKit's
251
+ * `AudioSource.captureFrame` shape). Resolves once the slices are queued;
252
+ * `waitForPlayout()` tells when they have left.
57
253
  */
58
254
  writeAudio(frame: RelayAudioFrame): Promise<void>;
59
- /** Drop queued outgoing PCM. At most one already-submitted 10 ms slice remains. */
255
+ /** Milliseconds of audio accepted by `writeAudio` but not yet written to RTP. */
256
+ queuedAudioMs(): number;
257
+ /**
258
+ * Resolves when every accepted slice has been written to RTP and the engine's
259
+ * pump is idle; immediately when nothing is queued; early on `clearAudio()`
260
+ * or `close()` (the caller reads `queuedAudioMs()` to learn what was dropped).
261
+ */
262
+ waitForPlayout(): Promise<void>;
263
+ /** Drop outgoing PCM that has not reached RTP and release `waitForPlayout()` callers. */
60
264
  clearAudio(): void;
265
+ /**
266
+ * Resolves once `peerAudio` has fired (at once if it already has). Rejects
267
+ * after `timeoutMs`, or when the Call ends or the transport closes first.
268
+ */
269
+ waitForPeerAudio(timeoutMs: number): Promise<void>;
61
270
  setMuted(muted: boolean): void;
62
271
  end(): void;
63
272
  close(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,kBAAkB,EAElB,eAAe,EAGf,kBAAkB,EAElB,KAAK,EACN,MAAM,qBAAqB,CAAC;AAE7B,MAAM,WAAW,eAAe;IAC9B,wCAAwC;IACxC,OAAO,EAAE,UAAU,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;CACtB;AAkED,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,MAAM,CAAC;AAEhD,MAAM,WAAW,yBAAyB;IACxC,KAAK,EAAE,KAAK,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,eAAe,CAAC;IAGvB;;;OAGG;IACH,MAAM,CAAC,EAAE,eAAe,CAAC;IAKzB,oFAAoF;IACpF,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED,MAAM,WAAW,4BAA4B;IAC3C,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,KAAK,iBAAiB,GAAG;IACvB,KAAK,EAAE,CAAC,eAAe,CAAC,CAAC;IACzB,SAAS,EAAE,EAAE,CAAC;IACd,SAAS,EAAE,CAAC,kBAAkB,CAAC,CAAC;IAChC,KAAK,EAAE,CAAC,kBAAkB,CAAC,CAAC;IAC5B,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC;IACf,KAAK,EAAE,CAAC,4BAA4B,CAAC,CAAC;CACvC,CAAC;AAEF,KAAK,cAAc,GAAG,MAAM,iBAAiB,CAAC;AAC9C,KAAK,iBAAiB,CAAC,CAAC,SAAS,cAAc,IAAI,CAAC,GAAG,IAAI,EAA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