Skip to content

Commit

Permalink
fix: fix eslint ts errors
Browse files Browse the repository at this point in the history
  • Loading branch information
farhat-ha committed Apr 2, 2024
1 parent 67e1133 commit 4dbe549
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 53 deletions.
11 changes: 6 additions & 5 deletions packages/janus/src/Call.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v4 as uuid } from "uuid";
import { connection } from "./Connection";
import { trigger } from "./Handler";
import Peer from "./Peer";
Expand All @@ -6,7 +7,6 @@ import { SwEvent } from "./constants";
import { SIPAnswerTransaction } from "./transactions/SIPAnswer";
import { SIPHangupTransaction } from "./transactions/SIPHangup";
import { CallState, ICall, ICallOptions } from "./types";
import { v4 as uuid } from "uuid";

type CallDirection = "inbound" | "outbound";
type TelnyxIds = {
Expand Down Expand Up @@ -107,6 +107,7 @@ export default class Call implements ICall {
};

answer = async (): Promise<void> => {
debugger;
if (this.direction === "outbound") {
throw new Error("Cannot answer an outbound call");
}
Expand Down Expand Up @@ -137,7 +138,7 @@ export default class Call implements ICall {
deaf(): void {
throw new Error("Method not implemented.");
}
dtmf(dtmf: string): Promise<void> {
dtmf(_dtmf: string): Promise<void> {
throw new Error("Method not implemented.");
}
hold(): Promise<void> {
Expand All @@ -149,13 +150,13 @@ export default class Call implements ICall {
muteVideo(): Promise<void> {
throw new Error("Method not implemented.");
}
setAudioInDevice(deviceId: string): Promise<void> {
setAudioInDevice(_deviceId: string): Promise<void> {
throw new Error("Method not implemented.");
}
setAudioOutDevice(deviceId: string): Promise<void> {
setAudioOutDevice(_deviceId: string): Promise<void> {
throw new Error("Method not implemented.");
}
setVideoDevice(deviceId: string): Promise<void> {
setVideoDevice(_deviceId: string): Promise<void> {
throw new Error("Method not implemented.");
}
toggleAudioMute(): void {
Expand Down
8 changes: 1 addition & 7 deletions packages/janus/src/CallAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
isSIPIncomingCallMessage,
isSipHangupEvent,
} from "./util/janus";
import { createAudioElement } from "./util/media";

type CallAgentConstructorOptions = {
ringbackFile?: string;
Expand All @@ -27,23 +26,18 @@ type CallAgentConstructorOptions = {
};

export default class CallAgent {
private ringbackAudio: HTMLAudioElement;
private ringtoneAudio: HTMLAudioElement;
public localElement: HTMLMediaElement | null;
public remoteElement: HTMLMediaElement | null;
public defaultMediaConstrains: MediaStreamConstraints;
public calls: Record<string, Call>;

constructor({
ringbackFile = "",
ringtoneFile = "",
localElement = null,
remoteElement = null,
}: CallAgentConstructorOptions) {
this.localElement = localElement;
this.remoteElement = remoteElement;
this.ringbackAudio = createAudioElement(ringbackFile);
this.ringtoneAudio = createAudioElement(ringtoneFile);

this.calls = {};
this.defaultMediaConstrains = { audio: true, video: true };

Expand Down
12 changes: 7 additions & 5 deletions packages/janus/src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import CallAgent from "./CallAgent";
import { connection } from "./Connection";
import { deRegister, register } from "./Handler";
import KeepAliveAgent from "./KeepAliveAgent";
import { SIPRegistrationAgent } from "./SIPRegistrationAgent";
import {
ICall,
Expand All @@ -15,13 +16,13 @@ export default class JanusClient implements IClient {
private _options: IClientOptions;
private _sipRegistrationAgent: SIPRegistrationAgent;
private _callAgent: CallAgent;
// private _keepAliveAgent: KeepAliveAgent;
private _keepAliveAgent: KeepAliveAgent;

constructor(options: IClientOptions = {}) {
this._options = options;

this._sipRegistrationAgent = new SIPRegistrationAgent();
// this._keepAliveAgent = new KeepAliveAgent();
this._keepAliveAgent = new KeepAliveAgent();
this._callAgent = new CallAgent(this._options);
}

Expand All @@ -35,7 +36,7 @@ export default class JanusClient implements IClient {
// TODO: implement
speaker: string | null = null;

checkPermissions(audio: boolean, video: boolean): Promise<boolean> {
checkPermissions(_audio: boolean, _video: boolean): Promise<boolean> {
throw new Error("Method not implemented.");
}
disableMicrophone(): void {
Expand Down Expand Up @@ -68,7 +69,7 @@ export default class JanusClient implements IClient {
}

getDeviceResolutions(
deviceId: string
_deviceId: string
): { resolution: string; width: number; height: number }[] {
throw new Error("Method not implemented.");
}
Expand Down Expand Up @@ -101,7 +102,7 @@ export default class JanusClient implements IClient {
}
public async disconnect() {
await this._sipRegistrationAgent.unregister();
// this._keepAliveAgent?.stop();
this._keepAliveAgent.stop();
connection.disconnect();
}

Expand All @@ -110,6 +111,7 @@ export default class JanusClient implements IClient {
if (this._options.login || this._options.login_token) {
await this._sipRegistrationAgent.register(this._options);
}
this._keepAliveAgent.start();
}

async newCall(options: ICallOptions) {
Expand Down
14 changes: 1 addition & 13 deletions packages/janus/src/KeepAliveAgent.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { connection } from "./Connection";
import { transactionManager } from "./TransactionManager";
import { ConnectionEvents } from "./constants";
import { KeepAliveTransaction } from "./transactions/KeepAlive";

const KEEP_ALIVE_INTERVAL = 10 * 1000;
const KEEP_ALIVE_INTERVAL = 10 * 5000;
export default class KeepAliveAgent {
private _interval: number | null;
private _failedAttempts: number;

constructor() {
this._interval = null;
this._failedAttempts = 0;
connection.addListener(
ConnectionEvents.StateChange,
this._onConnectionStateChange
);
}

private _onConnectionStateChange = () => {
if (connection.connected) {
this.start();
} else {
this.stop();
}
};
private _tick = async () => {
try {
if (!connection.gatewaySessionId) {
Expand Down
18 changes: 11 additions & 7 deletions packages/janus/src/Peer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { connection } from "./Connection";
import { trigger } from "./Handler";
import { transactionManager } from "./TransactionManager";
import { STUN_SERVER, TURN_SERVER } from "./constants";
import { STUN_SERVER, SwEvent, TURN_SERVER } from "./constants";
import { ICETrickleTransaction } from "./transactions/ICETrickleTransaction";
import { ICallOptions } from "./types";
import { DeferredPromise, deferredPromise } from "./util/promise";
Expand All @@ -20,6 +21,7 @@ export default class Peer {
) => {
const peer = new Peer(options, "answer");
await peer._setupLocalStream();
debugger;
await peer.setRemoteDescription(remoteSDP);
return peer;
};
Expand Down Expand Up @@ -58,9 +60,8 @@ export default class Peer {
return;
}
await this.connection.setRemoteDescription(sdp);
await this.connection.createAnswer().then((answer) => {
this.connection.setLocalDescription(answer);
});
const answer = await this.connection.createAnswer();
await this.connection.setLocalDescription(answer);
};

private _createConnection() {
Expand All @@ -75,7 +76,7 @@ export default class Peer {
}
pc.addEventListener("track", this._onTrack);
pc.addEventListener("icecandidate", this._onIceCandidate);
pc.addEventListener("iceconnectionstatechange", (ev) => {
pc.addEventListener("iceconnectionstatechange", () => {
console.log("iceconnectionstatechange", pc.iceConnectionState);
});
return pc;
Expand All @@ -92,11 +93,14 @@ export default class Peer {
audio: this._callOptions.audio ?? true,
video: this._callOptions.video ?? false,
};
// TODO add support for specific devices

const stream = await navigator.mediaDevices
.getUserMedia(constrains)
.then((stream) => stream)
.catch((err) => null);
.catch((err) => {
trigger(SwEvent.MediaError, err);
return null;
});

const tracks = stream?.getTracks() ?? [];

Expand Down
3 changes: 2 additions & 1 deletion packages/janus/src/SIPRegistrationAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export class SIPRegistrationAgent {

constructor() {
this.state = "unregistered";
connection.addListener(ConnectionEvents.Message, this._onMessage);
}

private _onMessage = (msg: string) => {
Expand All @@ -41,6 +40,7 @@ export class SIPRegistrationAgent {
data.plugindata.data.result.event === "registered"
) {
this._setState("registered");
connection.removeListener(ConnectionEvents.Message, this._onMessage);
trigger(SwEvent.Ready, connection);
}
};
Expand All @@ -62,6 +62,7 @@ export class SIPRegistrationAgent {
) {
return this.state;
}
connection.addListener(ConnectionEvents.Message, this._onMessage);

try {
await transactionManager.execute(
Expand Down
19 changes: 9 additions & 10 deletions packages/janus/src/transactions/SIPCall.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { v4 as uuidV4 } from 'uuid';
import { Janus } from '../messages/janus';
import { JanusSIPCallRequest } from '../messages/request';
import { JanusResponse } from '../messages/response';
import { isSipError, isSipRingingEvent } from '../util/janus';
import { BaseTransaction } from './BaseTransaction';
import { Janus } from "../messages/janus";
import { JanusSIPCallRequest } from "../messages/request";
import { JanusResponse } from "../messages/response";
import { isSipError, isSipRingingEvent } from "../util/janus";
import { BaseTransaction } from "./BaseTransaction";

type SIPCallTransactionOptions = {
callId: string;
Expand All @@ -29,7 +28,7 @@ export class SIPCallTransaction extends BaseTransaction<
handle_id: options.handle_id,
janus: Janus.message,
body: {
request: 'call',
request: "call",
call_id: options.callId,
uri: options.uri,
headers: options.headers,
Expand All @@ -46,11 +45,11 @@ export class SIPCallTransaction extends BaseTransaction<
if (isSipRingingEvent(msg)) {
return this._resolve({
callId: msg.plugindata.data.call_id,
telnyxLegId: msg.plugindata.data.result.headers['X-Telnyx-Leg-ID'],
telnyxLegId: msg.plugindata.data.result.headers["X-Telnyx-Leg-ID"],
telnyxCallControlId:
msg.plugindata.data.result.headers['X-Telnyx-Call-Control-ID'],
msg.plugindata.data.result.headers["X-Telnyx-Call-Control-ID"],
telnyxSessionId:
msg.plugindata.data.result.headers['X-Telnyx-Session-ID'],
msg.plugindata.data.result.headers["X-Telnyx-Session-ID"],
});
}
}
Expand Down
5 changes: 0 additions & 5 deletions packages/janus/src/util/media.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
type CreateAudioOptions = {
src: string;
loop?: Boolean;
};

export function createAudioElement(src: string, loop = true) {
const audio = document.createElement("audio");
audio.autoplay = true;
Expand Down
6 changes: 6 additions & 0 deletions packages/js-sip/src/SipCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export default class SipCall implements ICall {
private session: SIP.InviteClientContext | SIP.InviteServerContext,
private isIncoming: boolean = false
) {}
setAudioInDevice(deviceId: string): Promise<any> {
throw new Error("Method not implemented.");
}
setVideoDevice(deviceId: string): Promise<any> {
throw new Error("Method not implemented.");
}

get state(): CallState {
const C = SIP.Web.Simple.C;
Expand Down
7 changes: 7 additions & 0 deletions packages/js/src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
export type Env = 'production' | 'development';
export type RTCElement = HTMLMediaElement | string | Function;
export type CallState =
| 'new'
| 'ringing'
| 'connecting'
| 'active'
| 'held'
| 'done';

0 comments on commit 4dbe549

Please sign in to comment.