@volcengine/avatar-web-sdk 1.0.1-beta.1 → 1.0.1-beta.2
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/avatar-web-sdk.es.js +19248 -0
- package/core/AvatarError.d.ts +70 -0
- package/core/AvatarSDK.d.ts +28 -0
- package/core/AvatarSession.d.ts +89 -0
- package/index.d.ts +11 -0
- package/logging/LogManager.d.ts +39 -0
- package/package.json +12 -2
- package/protocol/Protocol.d.ts +30 -0
- package/services/Connection.d.ts +36 -0
- package/services/RTCService.d.ts +51 -0
- package/services/WebRecorder.d.ts +67 -0
- package/types/errors.d.ts +67 -0
- package/types/index.d.ts +109 -0
- package/types/internal.d.ts +25 -0
- package/utils/CryptoFallback.d.ts +7 -0
- package/utils/URLSigner.d.ts +24 -0
- package/version.d.ts +8 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { AvatarErrorCode } from "../types/errors";
|
|
2
|
+
/**
|
|
3
|
+
* AvatarError - Custom error class for Avatar SDK
|
|
4
|
+
*
|
|
5
|
+
* @note Use the static factory methods to create errors for consistency
|
|
6
|
+
*/
|
|
7
|
+
export declare class AvatarError extends Error {
|
|
8
|
+
/** Error domain */
|
|
9
|
+
readonly domain: string;
|
|
10
|
+
/** Error code */
|
|
11
|
+
readonly code: AvatarErrorCode;
|
|
12
|
+
/** Underlying error (if any) */
|
|
13
|
+
readonly cause?: Error;
|
|
14
|
+
/** Additional details (if any) */
|
|
15
|
+
readonly details?: Record<string, unknown>;
|
|
16
|
+
/**
|
|
17
|
+
* Create an AvatarError instance
|
|
18
|
+
* @param code - Error code
|
|
19
|
+
* @param message - Error message (optional, uses default if not provided)
|
|
20
|
+
* @param cause - Underlying error (optional)
|
|
21
|
+
* @param details - Additional details (optional)
|
|
22
|
+
*/
|
|
23
|
+
constructor(code: AvatarErrorCode, message?: string, cause?: Error, details?: Record<string, unknown>);
|
|
24
|
+
/**
|
|
25
|
+
* Create an error with default domain, code and description
|
|
26
|
+
* @param code - Error code
|
|
27
|
+
* @param message - Error message (optional)
|
|
28
|
+
* @returns AvatarError instance
|
|
29
|
+
*/
|
|
30
|
+
static withCode(code: AvatarErrorCode, message?: string): AvatarError;
|
|
31
|
+
/**
|
|
32
|
+
* Create an error with default domain, code, description and underlying error
|
|
33
|
+
* @param code - Error code
|
|
34
|
+
* @param message - Error message (optional)
|
|
35
|
+
* @param cause - Underlying error
|
|
36
|
+
* @returns AvatarError instance
|
|
37
|
+
*/
|
|
38
|
+
static withCause(code: AvatarErrorCode, message: string | undefined, cause: Error): AvatarError;
|
|
39
|
+
/**
|
|
40
|
+
* Create an error with default domain, code, description and additional details
|
|
41
|
+
* @param code - Error code
|
|
42
|
+
* @param message - Error message (optional)
|
|
43
|
+
* @param details - Additional details (optional)
|
|
44
|
+
* @returns AvatarError instance
|
|
45
|
+
*/
|
|
46
|
+
static withDetails(code: AvatarErrorCode, message: string | undefined, details?: Record<string, unknown>): AvatarError;
|
|
47
|
+
/**
|
|
48
|
+
* Create an error with default domain, code, description, underlying error and additional details
|
|
49
|
+
* @param code - Error code
|
|
50
|
+
* @param message - Error message (optional)
|
|
51
|
+
* @param cause - Underlying error (optional)
|
|
52
|
+
* @param details - Additional details (optional)
|
|
53
|
+
* @returns AvatarError instance
|
|
54
|
+
*/
|
|
55
|
+
static withFullDetails(code: AvatarErrorCode, message: string | undefined, cause?: Error, details?: Record<string, unknown>): AvatarError;
|
|
56
|
+
/**
|
|
57
|
+
* Convert a plain Error to AvatarError (if not already an AvatarError)
|
|
58
|
+
* @param error - Error to convert
|
|
59
|
+
* @param defaultCode - Default error code if converting a plain Error
|
|
60
|
+
* @returns AvatarError instance
|
|
61
|
+
*/
|
|
62
|
+
static from(error: Error, defaultCode?: AvatarErrorCode): AvatarError;
|
|
63
|
+
/**
|
|
64
|
+
* Check if an error is an AvatarError with a specific code
|
|
65
|
+
* @param error - Error to check
|
|
66
|
+
* @param code - Error code to match
|
|
67
|
+
* @returns true if the error is an AvatarError with the specified code
|
|
68
|
+
*/
|
|
69
|
+
static is(error: unknown, code?: AvatarErrorCode): error is AvatarError;
|
|
70
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AvatarSDKConfig, AvatarSessionConfig } from "../types";
|
|
2
|
+
import { AvatarSession } from "./AvatarSession";
|
|
3
|
+
import { LogManager } from "../logging/LogManager";
|
|
4
|
+
/**
|
|
5
|
+
* AvatarSDK main class
|
|
6
|
+
*
|
|
7
|
+
* @note Best practice: Create one instance per application process lifecycle and use it as a singleton.
|
|
8
|
+
* @warning The config is immutable after initialization. Any subsequent changes to the config object will not take effect.
|
|
9
|
+
*/
|
|
10
|
+
export declare class AvatarSDK {
|
|
11
|
+
/** Current version of the Avatar Web SDK, populated at build time from package.json. */
|
|
12
|
+
static readonly version: string;
|
|
13
|
+
/** Configuration for AvatarSDK (readonly) */
|
|
14
|
+
readonly config: Readonly<AvatarSDKConfig>;
|
|
15
|
+
readonly logger: LogManager;
|
|
16
|
+
/**
|
|
17
|
+
* Initialize AvatarSDK with configuration
|
|
18
|
+
* @param config - Configuration for AvatarSDK
|
|
19
|
+
* @throws Error if appKey or secretKey is missing
|
|
20
|
+
*/
|
|
21
|
+
constructor(config: AvatarSDKConfig);
|
|
22
|
+
/**
|
|
23
|
+
* Create AvatarSession instance with config
|
|
24
|
+
* @param config - Session configuration
|
|
25
|
+
* @returns AvatarSession instance
|
|
26
|
+
*/
|
|
27
|
+
createSession(config: AvatarSessionConfig): AvatarSession;
|
|
28
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
import { AvatarSDK } from "./AvatarSDK";
|
|
3
|
+
import { AvatarSessionConfig, AvatarSessionState, RenderMode, AvatarSessionEvents } from "../types";
|
|
4
|
+
/**
|
|
5
|
+
* AvatarSession class manages the lifecycle of an avatar session
|
|
6
|
+
*
|
|
7
|
+
* @note Use `AvatarSDK.createSession()` to create an instance instead of using the constructor directly.
|
|
8
|
+
* @warning Once the session ends (either normally or due to an error), the session object becomes invalid
|
|
9
|
+
* and cannot be reused. To start a new session, you must create a new AvatarSession instance
|
|
10
|
+
* using `AvatarSDK.createSession()`. All resources are automatically cleaned up when the session ends.
|
|
11
|
+
*/
|
|
12
|
+
export declare class AvatarSession extends EventEmitter<AvatarSessionEvents> {
|
|
13
|
+
private readonly _config;
|
|
14
|
+
private readonly _avatarSDK;
|
|
15
|
+
private _connection;
|
|
16
|
+
private _protocol;
|
|
17
|
+
private _state;
|
|
18
|
+
private _rtcService;
|
|
19
|
+
private _webRecorder;
|
|
20
|
+
private readonly _localSessionId;
|
|
21
|
+
private _sessionId;
|
|
22
|
+
/**
|
|
23
|
+
* Create AvatarSession instance
|
|
24
|
+
* @param config - Session configuration
|
|
25
|
+
* @param avatarSDK - AvatarSDK instance
|
|
26
|
+
*/
|
|
27
|
+
constructor(config: AvatarSessionConfig, avatarSDK: AvatarSDK);
|
|
28
|
+
/** Current state of the session */
|
|
29
|
+
get state(): AvatarSessionState;
|
|
30
|
+
/** Local session identifier */
|
|
31
|
+
get localSessionId(): string;
|
|
32
|
+
/**
|
|
33
|
+
* Start the avatar session
|
|
34
|
+
* @returns Promise that resolves when the session starts
|
|
35
|
+
* @note If the session is already starting or started, this method will ignore the request
|
|
36
|
+
*/
|
|
37
|
+
start(): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* End the avatar session
|
|
40
|
+
* @returns Promise that resolves when the session ends
|
|
41
|
+
* @note If the session is already idle, ending, or ended, this method will ignore the request
|
|
42
|
+
* @warning After calling this method, the session object becomes invalid and cannot be reused.
|
|
43
|
+
* All resources (WebRecorder, RTCService, Connection) will be automatically cleaned up.
|
|
44
|
+
* The 'end' event will be emitted before cleanup. To start a new session, create a new
|
|
45
|
+
* AvatarSession instance using `AvatarSDK.createSession()`.
|
|
46
|
+
*/
|
|
47
|
+
end(): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Set avatar video canvas
|
|
50
|
+
* @param container - HTMLDivElement to render avatar video. Pass null to unset the canvas.
|
|
51
|
+
* @param mode - Render mode
|
|
52
|
+
*/
|
|
53
|
+
setAvatarVideoCanvas(container: HTMLDivElement | null, mode: RenderMode): void;
|
|
54
|
+
/**
|
|
55
|
+
* Start local microphone recording
|
|
56
|
+
* @returns Promise that resolves when audio capture starts successfully
|
|
57
|
+
* @throws Error if session is not started or audio capture fails to start
|
|
58
|
+
* @note Audio format: mono channel, 16000Hz sample rate, int16 sample format, little-endian byte order
|
|
59
|
+
*/
|
|
60
|
+
startAudioCapture(): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Stop local microphone recording
|
|
63
|
+
* @returns Promise that resolves when audio capture stops successfully
|
|
64
|
+
* @throws Error if audio capture fails to stop
|
|
65
|
+
*/
|
|
66
|
+
stopAudioCapture(): Promise<void>;
|
|
67
|
+
/** Destroy the session and release all resources */
|
|
68
|
+
destroy(): void;
|
|
69
|
+
private _generateUUID;
|
|
70
|
+
private _buildEventParams;
|
|
71
|
+
private _trackEvent;
|
|
72
|
+
private _sendCreateSessionMessage;
|
|
73
|
+
private _sendEndSessionMessage;
|
|
74
|
+
private _sendAudioData;
|
|
75
|
+
private _generateServerConfig;
|
|
76
|
+
private _onConnectionOpen;
|
|
77
|
+
private _onConnectionError;
|
|
78
|
+
private _onConnectionClose;
|
|
79
|
+
private _onConnectionMessage;
|
|
80
|
+
private _startRTCService;
|
|
81
|
+
private _onRTCReady;
|
|
82
|
+
private _onRTCError;
|
|
83
|
+
private _onAudioCaptureStart;
|
|
84
|
+
private _onAudioCaptureStop;
|
|
85
|
+
private _onAudioCaptureFail;
|
|
86
|
+
private _onAudioFrame;
|
|
87
|
+
private _bytesToHex;
|
|
88
|
+
private _handleSessionError;
|
|
89
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { AvatarSDK } from "./core/AvatarSDK";
|
|
2
|
+
export { AvatarSession } from "./core/AvatarSession";
|
|
3
|
+
export { VERSION, getBuildInfo, getRtcVariant } from "./version";
|
|
4
|
+
export type { AvatarSDKConfig } from "./types";
|
|
5
|
+
export type { AvatarSessionConfig } from "./types";
|
|
6
|
+
export type { AvatarSessionState } from "./types";
|
|
7
|
+
export type { RenderMode } from "./types";
|
|
8
|
+
export type { AudioFrame } from "./types";
|
|
9
|
+
export type { AvatarSessionEvents } from "./types";
|
|
10
|
+
export type { LogLevel } from "./types";
|
|
11
|
+
export type { BuildInfo, RtcVariant } from "./version";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { LogLevel } from "../types";
|
|
2
|
+
interface LogEvents {
|
|
3
|
+
verbose: [message: string];
|
|
4
|
+
debug: [message: string];
|
|
5
|
+
info: [message: string];
|
|
6
|
+
warn: [message: string];
|
|
7
|
+
error: [message: string];
|
|
8
|
+
}
|
|
9
|
+
export declare class LogManager {
|
|
10
|
+
private logEnabled;
|
|
11
|
+
private logLevel;
|
|
12
|
+
private readonly moduleName;
|
|
13
|
+
private readonly emitter;
|
|
14
|
+
constructor(config: {
|
|
15
|
+
logEnabled?: boolean;
|
|
16
|
+
logLevel?: LogLevel;
|
|
17
|
+
moduleName?: string;
|
|
18
|
+
});
|
|
19
|
+
private shouldLog;
|
|
20
|
+
private _pad2;
|
|
21
|
+
private _pad3;
|
|
22
|
+
private _formatTimestamp;
|
|
23
|
+
private formatMessage;
|
|
24
|
+
private emitLog;
|
|
25
|
+
verbose(message: string, ..._args: unknown[]): void;
|
|
26
|
+
debug(message: string, ..._args: unknown[]): void;
|
|
27
|
+
info(message: string, ..._args: unknown[]): void;
|
|
28
|
+
warn(message: string, ..._args: unknown[]): void;
|
|
29
|
+
error(message: string, ..._args: unknown[]): void;
|
|
30
|
+
on<K extends keyof LogEvents>(event: K, handler: (...args: LogEvents[K]) => void): void;
|
|
31
|
+
off<K extends keyof LogEvents>(event: K, handler: (...args: LogEvents[K]) => void): void;
|
|
32
|
+
once<K extends keyof LogEvents>(event: K, handler: (...args: LogEvents[K]) => void): void;
|
|
33
|
+
removeAllListeners(event?: keyof LogEvents): void;
|
|
34
|
+
setLogLevel(level: LogLevel): void;
|
|
35
|
+
getLogLevel(): LogLevel;
|
|
36
|
+
setLogEnabled(enabled: boolean): void;
|
|
37
|
+
getLogEnabled(): boolean;
|
|
38
|
+
}
|
|
39
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@volcengine/avatar-web-sdk",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./avatar-web-sdk.es.js",
|
|
6
6
|
"module": "./avatar-web-sdk.es.js",
|
|
@@ -16,7 +16,17 @@
|
|
|
16
16
|
"eventemitter3": "^5.0.4"
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
|
-
"."
|
|
19
|
+
"avatar-web-sdk.es.js",
|
|
20
|
+
"index.d.ts",
|
|
21
|
+
"version.d.ts",
|
|
22
|
+
"core/",
|
|
23
|
+
"logging/",
|
|
24
|
+
"protocol/",
|
|
25
|
+
"services/",
|
|
26
|
+
"types/",
|
|
27
|
+
"utils/",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
20
30
|
],
|
|
21
31
|
"publishConfig": {
|
|
22
32
|
"access": "public",
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
interface ConnectServerMessage {
|
|
2
|
+
type: "connect";
|
|
3
|
+
code?: number;
|
|
4
|
+
message?: string;
|
|
5
|
+
rtcAppId?: string;
|
|
6
|
+
rtcRoomId?: string;
|
|
7
|
+
rtcAvatarId?: string;
|
|
8
|
+
rtcUserId?: string;
|
|
9
|
+
rtcUserToken?: string;
|
|
10
|
+
sessionId?: string;
|
|
11
|
+
}
|
|
12
|
+
interface ErrorServerMessage {
|
|
13
|
+
type: "error";
|
|
14
|
+
code?: number;
|
|
15
|
+
message?: string;
|
|
16
|
+
sessionId?: string;
|
|
17
|
+
}
|
|
18
|
+
interface HeartbeatServerMessage {
|
|
19
|
+
type: "heartbeat";
|
|
20
|
+
}
|
|
21
|
+
type ServerMessage = ConnectServerMessage | ErrorServerMessage | HeartbeatServerMessage;
|
|
22
|
+
export declare class Protocol {
|
|
23
|
+
serializeStartSession(config: Record<string, unknown>): string;
|
|
24
|
+
serializeEndSession(): string;
|
|
25
|
+
serializeAudio(pcmData: Uint8Array): Uint8Array;
|
|
26
|
+
deserialize(data: unknown): ServerMessage;
|
|
27
|
+
private _parseServerMessage;
|
|
28
|
+
private _buildServerMessage;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
import { AvatarSDK } from "../core/AvatarSDK";
|
|
3
|
+
type ConnectionReadyState = "connecting" | "open" | "closing" | "closed";
|
|
4
|
+
interface ConnectionEvents {
|
|
5
|
+
open: [];
|
|
6
|
+
error: [error: Error];
|
|
7
|
+
close: [code: number, reason: string, wasClean: boolean];
|
|
8
|
+
message: [message: unknown];
|
|
9
|
+
}
|
|
10
|
+
export declare class Connection extends EventEmitter<ConnectionEvents> {
|
|
11
|
+
private readonly _avatarSDK;
|
|
12
|
+
private _ws;
|
|
13
|
+
private _readyState;
|
|
14
|
+
private _sessionId;
|
|
15
|
+
private _xTtLogid;
|
|
16
|
+
private _lastHeartbeatTimestamp;
|
|
17
|
+
private _heartbeatTimer;
|
|
18
|
+
private _disconnectCompleted;
|
|
19
|
+
private _disconnectTimeout;
|
|
20
|
+
constructor(avatarSDK: AvatarSDK);
|
|
21
|
+
get readyState(): ConnectionReadyState;
|
|
22
|
+
get sessionId(): string | null;
|
|
23
|
+
get xTtLogid(): string | null;
|
|
24
|
+
private _getWsStateDescription;
|
|
25
|
+
connect(): Promise<void>;
|
|
26
|
+
disconnect(): Promise<void>;
|
|
27
|
+
sendText(text: string): Promise<void>;
|
|
28
|
+
sendBinary(data: Uint8Array): Promise<void>;
|
|
29
|
+
private _buildWebSocketUrl;
|
|
30
|
+
private _handleMessage;
|
|
31
|
+
private _startHeartbeatCheck;
|
|
32
|
+
private _stopHeartbeatCheck;
|
|
33
|
+
private _completeDisconnect;
|
|
34
|
+
destroy(): void;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
import { AvatarSDK } from "../core/AvatarSDK";
|
|
3
|
+
import { RenderMode, AudioFrame } from "../types";
|
|
4
|
+
import { RTCServiceConfig } from "../types/internal";
|
|
5
|
+
interface RTCServiceEvents {
|
|
6
|
+
ready: [];
|
|
7
|
+
error: [error: Error];
|
|
8
|
+
audioCaptureStart: [];
|
|
9
|
+
audioCaptureStop: [];
|
|
10
|
+
audioCaptureFail: [error: Error];
|
|
11
|
+
audioFrame: [frame: AudioFrame];
|
|
12
|
+
}
|
|
13
|
+
export declare class RTCService extends EventEmitter<RTCServiceEvents> {
|
|
14
|
+
private readonly _config;
|
|
15
|
+
private readonly _avatarSDK;
|
|
16
|
+
private _ready;
|
|
17
|
+
private _audioCapturing;
|
|
18
|
+
private _startAudioCaptureRequested;
|
|
19
|
+
private _audioStreamState;
|
|
20
|
+
private _localUserReady;
|
|
21
|
+
private _avatarUserAudioReady;
|
|
22
|
+
private _avatarUserVideoReady;
|
|
23
|
+
private _engine;
|
|
24
|
+
constructor(config: RTCServiceConfig, avatarSDK: AvatarSDK);
|
|
25
|
+
get ready(): boolean;
|
|
26
|
+
start(): Promise<void>;
|
|
27
|
+
stop(): Promise<void>;
|
|
28
|
+
setAvatarVideoCanvas(container: HTMLDivElement | null, mode: RenderMode): void;
|
|
29
|
+
startAudioCapture(): Promise<void>;
|
|
30
|
+
stopAudioCapture(): Promise<void>;
|
|
31
|
+
private _initializeRTC;
|
|
32
|
+
private _bindEngineEvents;
|
|
33
|
+
private _unbindEngineEvents;
|
|
34
|
+
private _joinRoom;
|
|
35
|
+
private _leaveRoom;
|
|
36
|
+
private _destroyRTC;
|
|
37
|
+
private _setupRemoteVideo;
|
|
38
|
+
private _convertRenderMode;
|
|
39
|
+
private _startAudioCapture;
|
|
40
|
+
private _stopAudioCapture;
|
|
41
|
+
private _updateReadyState;
|
|
42
|
+
private _handleEngineError;
|
|
43
|
+
private _handleConnectionStateChanged;
|
|
44
|
+
private _handleAudioDeviceStateChanged;
|
|
45
|
+
private _handleUserJoined;
|
|
46
|
+
private _handleUserLeave;
|
|
47
|
+
private _handleUserPublishStream;
|
|
48
|
+
private _handleUserUnpublishStream;
|
|
49
|
+
destroy(): void;
|
|
50
|
+
}
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
import { AvatarSDK } from "../core/AvatarSDK";
|
|
3
|
+
import { AudioFrame } from "../types";
|
|
4
|
+
import { AudioRecorderConfig } from "../types/internal";
|
|
5
|
+
interface WebRecorderEvents {
|
|
6
|
+
start: [];
|
|
7
|
+
stop: [];
|
|
8
|
+
pause: [];
|
|
9
|
+
resume: [];
|
|
10
|
+
error: [error: Error];
|
|
11
|
+
audioFrame: [frame: AudioFrame];
|
|
12
|
+
volumeChange: [volume: {
|
|
13
|
+
spl: number;
|
|
14
|
+
percent: number;
|
|
15
|
+
}];
|
|
16
|
+
durationChange: [duration: number];
|
|
17
|
+
permissionChange: [state: PermissionState | undefined];
|
|
18
|
+
}
|
|
19
|
+
declare enum WebRecorderState {
|
|
20
|
+
Uninitialized = "Uninitialized",
|
|
21
|
+
Initializing = "Initializing",
|
|
22
|
+
Initialized = "Initialized",
|
|
23
|
+
Recording = "Recording",
|
|
24
|
+
Paused = "Paused",
|
|
25
|
+
Destroyed = "Destroyed"
|
|
26
|
+
}
|
|
27
|
+
type WebRecorderStateType = keyof typeof WebRecorderState;
|
|
28
|
+
declare class WebRecorder extends EventEmitter<WebRecorderEvents> {
|
|
29
|
+
private readonly _config;
|
|
30
|
+
private readonly _avatarSDK;
|
|
31
|
+
private _audioContext;
|
|
32
|
+
private _mediaStream;
|
|
33
|
+
private _audioWorklet;
|
|
34
|
+
private _analyserNode;
|
|
35
|
+
private _state;
|
|
36
|
+
private _totalDataLength;
|
|
37
|
+
private _fftSize;
|
|
38
|
+
private _volumeTimer;
|
|
39
|
+
private readonly _channel;
|
|
40
|
+
private readonly _sampleRate;
|
|
41
|
+
private readonly _sampleFormat;
|
|
42
|
+
private readonly _byteOrder;
|
|
43
|
+
constructor(config: AudioRecorderConfig, avatarSDK: AvatarSDK);
|
|
44
|
+
get state(): WebRecorderStateType;
|
|
45
|
+
get recording(): boolean;
|
|
46
|
+
get paused(): boolean;
|
|
47
|
+
checkMicrophonePermission(): Promise<PermissionState | undefined>;
|
|
48
|
+
start(): Promise<void>;
|
|
49
|
+
pause(): Promise<void>;
|
|
50
|
+
resume(): Promise<void>;
|
|
51
|
+
stop(): Promise<void>;
|
|
52
|
+
clearData(): void;
|
|
53
|
+
private _initialize;
|
|
54
|
+
private _setupAudioWorklet;
|
|
55
|
+
private _setupAnalyser;
|
|
56
|
+
private _processAudioFrame;
|
|
57
|
+
private _getBytesPerSample;
|
|
58
|
+
private _writeSample;
|
|
59
|
+
private _startVolumeMonitoring;
|
|
60
|
+
private _stopVolumeMonitoring;
|
|
61
|
+
private _postMessageToWorklet;
|
|
62
|
+
private _changeState;
|
|
63
|
+
private _setupDeviceWatcher;
|
|
64
|
+
private _cleanup;
|
|
65
|
+
destroy(): void;
|
|
66
|
+
}
|
|
67
|
+
export { WebRecorder, WebRecorderState };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Avatar error codes
|
|
3
|
+
* Error code range:
|
|
4
|
+
* - -1: Unknown error
|
|
5
|
+
* - 0xxx: Common errors
|
|
6
|
+
* - 1xxx: Connection/WebSocket errors
|
|
7
|
+
* - 2xxx: Message/Protocol errors
|
|
8
|
+
* - 3xxx: RTC errors
|
|
9
|
+
* - 4xxx: Server errors
|
|
10
|
+
* - 5xxx: Audio/Permission errors
|
|
11
|
+
*/
|
|
12
|
+
export declare enum AvatarErrorCode {
|
|
13
|
+
/** -1: Unknown error */
|
|
14
|
+
Unknown = -1,
|
|
15
|
+
/** 0xxx: Common errors */
|
|
16
|
+
InvalidParameter = 1,
|
|
17
|
+
NotInitialized = 2,
|
|
18
|
+
InvalidState = 3,
|
|
19
|
+
/** 1xxx: Connection/WebSocket errors */
|
|
20
|
+
ConnectionLost = 1001,
|
|
21
|
+
ConnectionFailed = 1002,
|
|
22
|
+
ConnectionClosed = 1003,
|
|
23
|
+
ConnectTimeout = 1004,
|
|
24
|
+
DisconnectTimeout = 1005,
|
|
25
|
+
SendFailed = 1006,
|
|
26
|
+
SocketError = 1007,
|
|
27
|
+
/** 2xxx: Message/Protocol errors */
|
|
28
|
+
InvalidMessageType = 2001,
|
|
29
|
+
MessageParseFailed = 2002,
|
|
30
|
+
MessageSerializeFailed = 2003,
|
|
31
|
+
InvalidMessageFormat = 2004,
|
|
32
|
+
JSONParseFailed = 2005,
|
|
33
|
+
InvalidBinaryData = 2006,
|
|
34
|
+
/** 3xxx: RTC errors */
|
|
35
|
+
RTCEngineCreateFailed = 3001,
|
|
36
|
+
RTCEngineNotInitialized = 3002,
|
|
37
|
+
RTCRoomCreateFailed = 3003,
|
|
38
|
+
RTCJoinRoomFailed = 3004,
|
|
39
|
+
RTCAvatarUserLeft = 3005,
|
|
40
|
+
RTCAvatarAudioStopped = 3006,
|
|
41
|
+
RTCAvatarVideoStopped = 3007,
|
|
42
|
+
RTCAudioCaptureFailed = 3008,
|
|
43
|
+
RTCAudioDeviceError = 3009,
|
|
44
|
+
RTCSetCanvasFailed = 3010,
|
|
45
|
+
RTCEngineError = 3011,
|
|
46
|
+
RTCStreamNotAvailable = 3012,
|
|
47
|
+
/** 4xxx: Server errors */
|
|
48
|
+
ServerError = 4001,
|
|
49
|
+
ServerConnectFailed = 4002,
|
|
50
|
+
ServerErrorMessage = 4003,
|
|
51
|
+
SignFailed = 4004,
|
|
52
|
+
/** 5xxx: Audio/Permission errors */
|
|
53
|
+
MicrophonePermissionDenied = 5001,
|
|
54
|
+
AudioRecorderInitFailed = 5002,
|
|
55
|
+
AudioWorkletError = 5003,
|
|
56
|
+
AudioContextError = 5004
|
|
57
|
+
}
|
|
58
|
+
/** Error domain for Avatar SDK */
|
|
59
|
+
export declare const AVATAR_ERROR_DOMAIN = "com.bytedance.bdavatar.error";
|
|
60
|
+
/** Default error description map */
|
|
61
|
+
export declare const errorDescriptionMap: Record<AvatarErrorCode, string>;
|
|
62
|
+
/**
|
|
63
|
+
* Get default error description for error code
|
|
64
|
+
* @param code - Error code
|
|
65
|
+
* @returns Default error description
|
|
66
|
+
*/
|
|
67
|
+
export declare function getDefaultErrorDescription(code: AvatarErrorCode): string;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export * from "./errors";
|
|
2
|
+
export { AvatarError } from "../core/AvatarError";
|
|
3
|
+
/**
|
|
4
|
+
* Log level for SDK logging
|
|
5
|
+
*/
|
|
6
|
+
export type LogLevel = "verbose" | "debug" | "info" | "warn" | "error";
|
|
7
|
+
/**
|
|
8
|
+
* Environment type
|
|
9
|
+
*/
|
|
10
|
+
export type Environment = "cn" | "overseas";
|
|
11
|
+
/**
|
|
12
|
+
* AvatarSDK configuration interface
|
|
13
|
+
*/
|
|
14
|
+
export interface AvatarSDKConfig {
|
|
15
|
+
/** App Key (Required) */
|
|
16
|
+
appKey: string;
|
|
17
|
+
/** Secret Key (Required) */
|
|
18
|
+
secretKey: string;
|
|
19
|
+
/** STS Token (Optional) */
|
|
20
|
+
stsToken?: string;
|
|
21
|
+
/** Enable logging (Optional), default is true */
|
|
22
|
+
logEnabled?: boolean;
|
|
23
|
+
/** Log level (Optional), default is "info" */
|
|
24
|
+
logLevel?: LogLevel;
|
|
25
|
+
/** Environment type (Optional), default is "cn" */
|
|
26
|
+
environment?: Environment;
|
|
27
|
+
/** Extra parameters dictionary (Optional) */
|
|
28
|
+
extraDict?: Record<string, unknown>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* AvatarSession configuration interface
|
|
32
|
+
*/
|
|
33
|
+
export interface AvatarSessionConfig {
|
|
34
|
+
/** Avatar image URL (Required). Photo URL of the avatar */
|
|
35
|
+
avatarImageUrl: string;
|
|
36
|
+
/** Speaker (Required). The speaker ID for TTS */
|
|
37
|
+
speaker: string;
|
|
38
|
+
/** User prompt (Optional). Custom prompt for the avatar */
|
|
39
|
+
userPrompt?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Speech rate (Optional). Controls the speed of output speech,
|
|
42
|
+
* value range [-50, 100], default is 0
|
|
43
|
+
*/
|
|
44
|
+
speechRate?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Loudness rate (Optional). Controls the volume of output speech,
|
|
47
|
+
* value range [-50, 100], default is 0
|
|
48
|
+
*/
|
|
49
|
+
loudnessRate?: number;
|
|
50
|
+
/** Enable web search (Optional), default is false */
|
|
51
|
+
enableWebsearch?: boolean;
|
|
52
|
+
/** Extra parameters dictionary (Optional) */
|
|
53
|
+
extraParams?: Record<string, unknown>;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Avatar session state
|
|
57
|
+
*/
|
|
58
|
+
export type AvatarSessionState = "idle" | "starting" | "started" | "ending" | "ended";
|
|
59
|
+
/**
|
|
60
|
+
* Image or video stream scaling mode
|
|
61
|
+
*/
|
|
62
|
+
export type RenderMode =
|
|
63
|
+
/**
|
|
64
|
+
* Fit the view first, default value.
|
|
65
|
+
* Video scales proportionally until it fills the view. When video and view dimensions don't match,
|
|
66
|
+
* the extra part of the video will be cropped.
|
|
67
|
+
*/
|
|
68
|
+
"hidden"
|
|
69
|
+
/**
|
|
70
|
+
* Show all video content first.
|
|
71
|
+
* Video scales proportionally to ensure all content is visible. When video and view dimensions don't match,
|
|
72
|
+
* the unfilled area of the view will be filled with background color.
|
|
73
|
+
*/
|
|
74
|
+
| "fit"
|
|
75
|
+
/**
|
|
76
|
+
* Video adapts to canvas.
|
|
77
|
+
* Video scales non-proportionally to fill the view. During this process, the aspect ratio of the video may change.
|
|
78
|
+
*/
|
|
79
|
+
| "fill";
|
|
80
|
+
/**
|
|
81
|
+
* Audio frame data structure
|
|
82
|
+
*/
|
|
83
|
+
export interface AudioFrame {
|
|
84
|
+
/** Sample rate of the audio */
|
|
85
|
+
sampleRate: number;
|
|
86
|
+
/** Number of audio channels */
|
|
87
|
+
channel: number;
|
|
88
|
+
/** Number of samples in the frame */
|
|
89
|
+
samples: number;
|
|
90
|
+
/** PCM audio buffer */
|
|
91
|
+
buffer: Uint8Array;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* AvatarSession event types and their argument signatures
|
|
95
|
+
*/
|
|
96
|
+
export interface AvatarSessionEvents {
|
|
97
|
+
/** Session did start successfully */
|
|
98
|
+
start: [];
|
|
99
|
+
/** Called when an Avatar session has ended, either by request or due to an error */
|
|
100
|
+
end: [error?: Error];
|
|
101
|
+
/** Audio capture started successfully */
|
|
102
|
+
audioCaptureStart: [];
|
|
103
|
+
/** Audio capture stopped */
|
|
104
|
+
audioCaptureStop: [];
|
|
105
|
+
/** Audio capture error (either start failed or runtime error) */
|
|
106
|
+
audioCaptureFail: [error: Error];
|
|
107
|
+
/** Audio frame callback from local microphone recording */
|
|
108
|
+
audioFrame: [frame: AudioFrame];
|
|
109
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface RTCServiceConfig {
|
|
2
|
+
rtcAppId: string;
|
|
3
|
+
rtcRoomId: string;
|
|
4
|
+
rtcAvatarId: string;
|
|
5
|
+
rtcUserId: string;
|
|
6
|
+
rtcUserToken: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Audio sample format (internal use only)
|
|
10
|
+
*/
|
|
11
|
+
export type AudioSampleFormat = "int8" | "int16" | "int32" | "float32";
|
|
12
|
+
/**
|
|
13
|
+
* Audio byte order (internal use only)
|
|
14
|
+
*/
|
|
15
|
+
export type AudioByteOrder = "little-endian" | "big-endian";
|
|
16
|
+
/**
|
|
17
|
+
* Audio recorder configuration (internal use only)
|
|
18
|
+
*/
|
|
19
|
+
export interface AudioRecorderConfig {
|
|
20
|
+
channel?: number;
|
|
21
|
+
sampleRate?: number;
|
|
22
|
+
sampleFormat?: AudioSampleFormat;
|
|
23
|
+
byteOrder?: AudioByteOrder;
|
|
24
|
+
bufferSizeMs?: number;
|
|
25
|
+
}
|