diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..6320840 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,299 @@ +export type ConnectionState = "connected" | "disconnected"; +export type SessionState = "alive" | "dying" | "dead"; + +export interface ClientOptions { + url?: string; + logger?: any; + requestTimeout?: number; + WebSocket?: any; + connectionTimeout?: number; + reconnect?: boolean; + token?: string | null; + apiSecret?: string | null; + handshakeTimeout?: number; +} + +export interface RequestOptions { + ack?: boolean; +} + +export interface TransactionLike { + getId(): string; + getRequest(): any; + getState(): string; + start(): this; + onSent(listener: (req: any) => void): this; + onAck(listener: (res: ClientResponse) => void): this; + onResponse(listener: (res: ClientResponse) => void): this; + onEnd(listener: () => void): this; + onError(listener: (err: any) => void): this; + getTimeout(): number; +} + +export interface ClientResponse { + getRequest(): any; + getResponse(): any; + getType(): string | null; + getJsep(): any; + isError(): boolean; + isAck(): boolean; + isSuccess(): boolean; +} + +export interface PluginResponse extends ClientResponse { + getName(): string | null; + getData(): any; +} + +export interface Jsep { + type: "offer" | "answer"; + sdp: string; +} + +export interface VideoRoomCreateOptions { + [key: string]: any; +} + +export interface VideoRoomDestroyOptions { + room: number | string; + [key: string]: any; +} + +export interface VideoRoomExistsOptions { + room: number | string; + [key: string]: any; +} + +export interface VideoRoomListParticipantsOptions { + room: number | string; + [key: string]: any; +} + +export interface VideoRoomJoinOptions { + room: number | string; + ptype: "publisher" | "subscriber" | "listener"; + [key: string]: any; +} + +export interface VideoRoomJoinPublisherOptions { + room: number | string; + [key: string]: any; +} + +export interface VideoRoomJoinListenerOptions { + room: number | string; + feed: number | string; + [key: string]: any; +} + +export interface VideoRoomConfigureOptions { + audio?: boolean; + video?: boolean; + data?: boolean; + [key: string]: any; +} + +export interface VideoRoomJoinAndConfigureOptions extends VideoRoomConfigureOptions { + room: number | string; + jsep: Jsep; + [key: string]: any; +} + +export interface VideoRoomPublishOptions { + jsep: Jsep; + [key: string]: any; +} + +export interface VideoRoomStartOptions { + room: number | string; + jsep: Jsep; + [key: string]: any; +} + +export interface VideoRoomGenericOptions { + [key: string]: any; +} + +export interface VideoRoomCreateResult { + room: number; + response: PluginResponse; +} + +export interface VideoRoomDestroyResult { + response: PluginResponse; +} + +export interface VideoRoomExistsResult { + exists: boolean; + response: PluginResponse; +} + +export interface VideoRoomListResult { + list: any[]; + response: PluginResponse; +} + +export interface VideoRoomListParticipantsResult { + participants: any[]; + response: PluginResponse; +} + +export interface VideoRoomJoinResult { + id: number; + jsep: any; + response: PluginResponse; +} + +export interface VideoRoomJoinAndConfigureResult { + id: number; + jsep: any; + publishers: any; + response: PluginResponse; +} + +export interface VideoRoomPublishResult { + response: PluginResponse; +} + +export interface VideoRoomSimpleResult { + response: PluginResponse; +} + +export class PluginHandle { + constructor(options: { id: number | string; opaqueId?: string; plugin: VideoRoomPlugin }); + getId(): number | string; + getSession(): Session; + getPlugin(): VideoRoomPlugin; + isConnected(): ConnectionState; + isDisposed(): boolean; + detach(): Promise; + hangup(): Promise; + trickle(candidate: any): Promise; + trickles(candidates: any[]): Promise; + trickleCompleted(): Promise; + event(event: any): void; + onWebrtcUp(listener: (event: any) => void): void; + onMedia(listener: (event: any) => void): void; + onHangup(listener: (event: any) => void): void; + onSlowlink(listener: (event: any) => void): void; + onDetached(listener: (event: any) => void): void; + onEvent(listener: (event: any) => void): void; + onTrickle(listener: (event: any) => void): void; + request(obj: any, options?: RequestOptions): Promise; + requestMessage(body: any, options?: RequestOptions): Promise; + dispose(): Promise; +} + +export class VideoRoomHandle extends PluginHandle { + create(options?: VideoRoomCreateOptions): Promise; + destroy(options: VideoRoomDestroyOptions): Promise; + exists(options: VideoRoomExistsOptions): Promise; + list(): Promise; + listParticipants(options: VideoRoomListParticipantsOptions): Promise; + join(options: VideoRoomJoinOptions): Promise; + joinPublisher(options: VideoRoomJoinPublisherOptions): Promise; + joinListener(options: VideoRoomJoinListenerOptions): Promise; + configure(options?: VideoRoomConfigureOptions): Promise; + joinAndConfigure(options: VideoRoomJoinAndConfigureOptions): Promise; + publish(options: VideoRoomPublishOptions): Promise; + unpublish(options?: VideoRoomGenericOptions): Promise; + start(options: VideoRoomStartOptions): Promise; + pause(options?: VideoRoomGenericOptions): Promise; + switch(options?: VideoRoomGenericOptions): Promise; + stop(options?: VideoRoomGenericOptions): Promise; + add(options?: VideoRoomGenericOptions): Promise; + remove(options?: VideoRoomGenericOptions): Promise; + leave(options?: VideoRoomGenericOptions): Promise; + publishFeed(options: VideoRoomJoinAndConfigureOptions): Promise; + listenFeed(options: VideoRoomJoinListenerOptions): Promise; +} + +export class VideoRoomPublisher extends VideoRoomHandle { + getPublisherId(): number | null; + getRoom(): number | string; + getAnswer(): string | null; + createAnswer(offer: string): Promise; +} + +export class VideoRoomListener extends VideoRoomHandle { + getRoom(): number | string; + getFeed(): number | string; + getOffer(): string | null; + createOffer(): Promise; + setRemoteAnswer(answer: string): Promise; +} + +export class VideoRoomPlugin { + name: string; + fullName: string; + session: Session; + constructor(options: { session: Session }); + getName(): string | undefined; + getFullName(): string | undefined; + getSession(): Session | undefined; + defaultHandle(options?: { opaqueId?: string }): Promise; + createVideoRoomHandle(options?: { opaqueId?: string }): Promise; + attachVideoRoomHandle(handleId: number | string, opaqueId?: string): Promise; + createPublisherHandle(room: number | string, opaqueId?: string): Promise; + attachPublisherHandle(handleId: number | string, room: number | string, opaqueId?: string): Promise; + createListenerHandle(room: number | string, feed: number | string, opaqueId?: string): Promise; + attachListenerHandle(handleId: number | string, room: number | string, feed: number | string, opaqueId?: string): Promise; + publishFeed(room: number | string, offer: string, opaqueId?: string): Promise; + listenFeed(room: number | string, feed: number | string, opaqueId?: string): Promise; + getFeeds(room: number | string): Promise; + getFeedsExclude(room: number | string, feed: number | string): Promise; +} + +export class Session { + constructor(id: number | string, janus: Janus); + keepAlive(): Promise; + startKeepAlive(): void; + stopKeepAlive(): void; + request(obj: any, options?: RequestOptions): Promise; + createPluginHandle(plugin: string, options?: { opaqueId?: string }): Promise; + getId(): number | string; + getState(): SessionState; + isAlive(): boolean; + timeout(): void; + onTimeout(listener: () => void): void; + onKeepAlive(listener: (ok: boolean) => void): void; + onError(listener: (err: any) => void): void; + onEvent(listener: (event: any) => void): void; + event(event: any): void; + destroy(): Promise; + videoRoom(): VideoRoomPlugin; +} + +export class Janus { + constructor(options?: ClientOptions); + getVersion(): string; + isConnected(): boolean; + isConnecting(): boolean; + isClosing(): boolean; + connect(): void; + disconnect(): void; + open(): void; + close(options?: { connect?: boolean }): void; + message(message: { data: any }): void; + error(err: any): void; + getConnectionState(): ConnectionState; + setConnectionTimeout(timeout: number): void; + startConnectionTimeout(): void; + stopConnectionTimeout(): void; + dispatchObject(obj: any): void; + delegateEvent(event: any): void; + sendObject(obj: any): void; + createTransaction(options: { request: any; ack?: boolean; timeout?: number }): TransactionLike; + request(req: any, options?: RequestOptions): Promise; + hasSession(id: number | string): boolean; + addSession(session: Session): void; + deleteSession(id: number | string): void; + createSession(): Promise; + claimSession(sessionId: number | string): Promise; + destroySession(id: number | string): Promise; + getInfo(): Promise; + onConnected(listener: () => void): void; + onDisconnected(listener: () => void): void; + onError(listener: (err: any) => void): void; + onEvent(listener: (event: any) => void): void; +} diff --git a/package.json b/package.json index f044c3a..ccb527f 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "janus-videoroom-client", "version": "4.2.1", "main": "src/janus.js", + "types": "index.d.ts", "scripts": { "test": "mocha -b --exit -R spec --full-trace test/*-spec.js" },