@convai/web-sdk 2.0.0 → 2.0.1
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/vanilla/AudioRenderer.d.ts +11 -0
- package/dist/vanilla/AudioRenderer.js +44 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -13,6 +13,8 @@ export declare class AudioRenderer {
|
|
|
13
13
|
private boundHandleTrackSubscribed;
|
|
14
14
|
private boundHandleTrackUnsubscribed;
|
|
15
15
|
private boundHandleParticipantDisconnected;
|
|
16
|
+
private boundUnlockAudio;
|
|
17
|
+
private unlockArmed;
|
|
16
18
|
constructor(room: Room, options?: {
|
|
17
19
|
container?: HTMLElement;
|
|
18
20
|
});
|
|
@@ -49,6 +51,15 @@ export declare class AudioRenderer {
|
|
|
49
51
|
* Attach a MediaStreamTrack from WebSocket transport for bot audio playback.
|
|
50
52
|
*/
|
|
51
53
|
attachWebSocketTrack(track: MediaStreamTrack): void;
|
|
54
|
+
/**
|
|
55
|
+
* Browsers block audio that starts before the visitor has clicked, tapped or
|
|
56
|
+
* pressed a key on the page, and an `autoplay` element that was blocked stays
|
|
57
|
+
* paused. When playback is refused, wait for the first gesture and retry.
|
|
58
|
+
*/
|
|
59
|
+
private playOrDefer;
|
|
60
|
+
private armUnlock;
|
|
61
|
+
private disarmUnlock;
|
|
62
|
+
private unlockAudio;
|
|
52
63
|
detachWebSocketTrack(): void;
|
|
53
64
|
/**
|
|
54
65
|
* Cleanup and destroy the audio renderer
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { RoomEvent } from 'livekit-client';
|
|
2
2
|
import { logger } from '../utils/logger.js';
|
|
3
|
+
/** Gestures that let a page start audio. */
|
|
4
|
+
const UNLOCK_EVENTS = ['pointerdown', 'keydown', 'touchstart'];
|
|
3
5
|
/**
|
|
4
6
|
* AudioRenderer - Manages audio playback for remote participants
|
|
5
7
|
* Automatically creates and attaches audio elements for remote audio tracks
|
|
@@ -9,6 +11,8 @@ export class AudioRenderer {
|
|
|
9
11
|
constructor(room, options) {
|
|
10
12
|
this.audioElements = new Map();
|
|
11
13
|
this.livekitTracks = new Map(); // Store track references
|
|
14
|
+
this.boundUnlockAudio = () => this.unlockAudio();
|
|
15
|
+
this.unlockArmed = false;
|
|
12
16
|
this._wsAudioElement = null;
|
|
13
17
|
this.room = room;
|
|
14
18
|
this.rendererId = `renderer-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
@@ -96,6 +100,7 @@ export class AudioRenderer {
|
|
|
96
100
|
this.container.appendChild(audioElement);
|
|
97
101
|
this.audioElements.set(trackSid, audioElement);
|
|
98
102
|
this.livekitTracks.set(trackSid, track);
|
|
103
|
+
this.playOrDefer(audioElement);
|
|
99
104
|
}
|
|
100
105
|
catch (error) {
|
|
101
106
|
logger.error('Failed to attach audio track:', error);
|
|
@@ -130,6 +135,44 @@ export class AudioRenderer {
|
|
|
130
135
|
el.srcObject = new MediaStream([track]);
|
|
131
136
|
this.container.appendChild(el);
|
|
132
137
|
this._wsAudioElement = el;
|
|
138
|
+
this.playOrDefer(el);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Browsers block audio that starts before the visitor has clicked, tapped or
|
|
142
|
+
* pressed a key on the page, and an `autoplay` element that was blocked stays
|
|
143
|
+
* paused. When playback is refused, wait for the first gesture and retry.
|
|
144
|
+
*/
|
|
145
|
+
playOrDefer(element) {
|
|
146
|
+
const playing = element.play?.();
|
|
147
|
+
if (playing && typeof playing.catch === 'function') {
|
|
148
|
+
playing.catch((error) => {
|
|
149
|
+
if (error?.name === 'NotAllowedError')
|
|
150
|
+
this.armUnlock();
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
armUnlock() {
|
|
155
|
+
if (this.unlockArmed || typeof document === 'undefined')
|
|
156
|
+
return;
|
|
157
|
+
this.unlockArmed = true;
|
|
158
|
+
for (const type of UNLOCK_EVENTS)
|
|
159
|
+
document.addEventListener(type, this.boundUnlockAudio, true);
|
|
160
|
+
}
|
|
161
|
+
disarmUnlock() {
|
|
162
|
+
if (!this.unlockArmed)
|
|
163
|
+
return;
|
|
164
|
+
this.unlockArmed = false;
|
|
165
|
+
for (const type of UNLOCK_EVENTS)
|
|
166
|
+
document.removeEventListener(type, this.boundUnlockAudio, true);
|
|
167
|
+
}
|
|
168
|
+
unlockAudio() {
|
|
169
|
+
this.disarmUnlock();
|
|
170
|
+
void this.room.startAudio?.().catch(() => { });
|
|
171
|
+
const elements = [...this.audioElements.values(), ...(this._wsAudioElement ? [this._wsAudioElement] : [])];
|
|
172
|
+
for (const element of elements) {
|
|
173
|
+
if (element.paused)
|
|
174
|
+
this.playOrDefer(element);
|
|
175
|
+
}
|
|
133
176
|
}
|
|
134
177
|
detachWebSocketTrack() {
|
|
135
178
|
if (this._wsAudioElement) {
|
|
@@ -143,6 +186,7 @@ export class AudioRenderer {
|
|
|
143
186
|
* Cleanup and destroy the audio renderer
|
|
144
187
|
*/
|
|
145
188
|
destroy() {
|
|
189
|
+
this.disarmUnlock();
|
|
146
190
|
this.detachWebSocketTrack();
|
|
147
191
|
// Remove all audio elements
|
|
148
192
|
this.audioElements.forEach((element, trackSid) => {
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "2.0.
|
|
1
|
+
export declare const SDK_VERSION = "2.0.1";
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@convai/web-sdk",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Build web apps with lifelike AI characters. The Convai Web SDK gives you real-time voice, lipsync, emotions, and dynamic context — with first-class support for React and vanilla TypeScript.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|