Skip to content

Commit

Permalink
feat: implement janus-client class for core sip client functionality
Browse files Browse the repository at this point in the history
* Introduces JanusClient class as the central component for SIP calls.
* Manages connection to Janus server, plugin attachment, and SIP
registration.
* Handles call creation and management through CallAgent.
* Provides methods for connecting, making calls, getting calls, and setting
remote media element.
* Includes event handling for listening to call events.
* Implements stubs for enabling/disabling webcam and disconnection (to be
completed).

BREAKING CHANGE: Introduces a new class for core functionality, potentially
affecting code organization and usage.
  • Loading branch information
farhat-ha committed Mar 11, 2024
1 parent aab56da commit dd64f2f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
108 changes: 108 additions & 0 deletions packages/js/src/Modules/Janus/Client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Call } from './Call';
import { Connection } from './Connection';
import { deRegister, register } from './Handler';
import KeepAliveAgent from './KeepAliveAgent';
import { SIPRegistrationAgent } from './SIPRegistrationAgent';
import { transactionManager } from './TransactionManager';
import { Environment } from './constants';
import { IClientOptions } from './interfaces';
import { AttachSipPluginTransaction } from './transactions/AttachSIPPlugin';
import { CreateSessionTransaction } from './transactions/CreateSession';
import CallAgent from './CallAgent';
import { ICallOptions } from 'src/utils/interfaces';

export default class JanusClient {
private _gatewaySessionId: number | null = null;
private _gatewayHandleId: number | null = null;
private _connection: Connection;
private _options: IClientOptions;
private _sipRegistrationAgent: SIPRegistrationAgent;
private _keepAliveAgent: KeepAliveAgent;
private _callAgent: CallAgent;

static telnyxStateCall(call: Call) {
return call;
}
constructor(options: IClientOptions) {
this._connection = new Connection({
environment: Environment.production,
});
this._options = options;
}

public async connect() {
this._connection.connect();

const { sessionId } = await transactionManager.execute(
new CreateSessionTransaction()
);

this._gatewaySessionId = sessionId;

const { handleId } = await transactionManager.execute(
new AttachSipPluginTransaction({
sessionId: this._gatewaySessionId,
})
);
this._gatewayHandleId = handleId;

this._keepAliveAgent = new KeepAliveAgent({
connection: this._connection,
gatewaySessionId: this._gatewaySessionId,
});

this._sipRegistrationAgent = new SIPRegistrationAgent({
connection: this._connection,
options: this._options,
sessionId: this._gatewaySessionId,
handleId: this._gatewayHandleId,
});

this._callAgent = new CallAgent({
gatewayHandleId: this._gatewayHandleId,
gatewaySessionId: this._gatewaySessionId,
connection: this._connection,
});

await this._sipRegistrationAgent.register();
this._keepAliveAgent.start();
}

async newCall(options: ICallOptions) {
const call = await this._callAgent.Outbound({
...options,
handleId: this._gatewayHandleId,
sessionId: this._gatewaySessionId,
});
return call;
}

get calls() {
return this._callAgent.calls;
}
public disconnect() {
// TODO - implement
}

public enableWebcam() {
// TODO - implement
return true;
}
public disableWebcam() {
// TODO - implement
return true;
}

set remoteElement(
element: HTMLMediaElement | string | (() => HTMLMediaElement)
) {
this._callAgent.remoteElement = element;
}

on(eventName: string, callback: Function) {
register(eventName, callback);
}
off(eventName: string, callback: Function) {
deRegister(eventName, callback);
}
}
1 change: 1 addition & 0 deletions packages/js/src/Modules/Janus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Client'
6 changes: 3 additions & 3 deletions packages/js/src/TelnyxRTC.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-useless-constructor */
import TelnyxRTCClient from './Modules/Verto';
import { ICallOptions, IClientOptions } from './utils/interfaces';
import {
getWebRTCInfo,
Expand All @@ -11,6 +10,7 @@ import {
} from './Modules/Verto/webrtc/interfaces';

import * as pkg from '../package.json';
import JanusClient from './Modules/Janus/Client';

/**
* The `TelnyxRTC` client connects your application to the Telnyx backend,
Expand Down Expand Up @@ -49,7 +49,7 @@ import * as pkg from '../package.json';
*
* @category Client
*/
export class TelnyxRTC extends TelnyxRTCClient {
export class TelnyxRTC extends JanusClient {
/**
* Creates a new `TelnyxRTC` instance with the provided options.
*
Expand Down Expand Up @@ -153,7 +153,7 @@ export class TelnyxRTC extends TelnyxRTCClient {
* // => `destinationNumber is required`
* ```
*/
newCall(options: ICallOptions) {
newCall = (options: ICallOptions) => {
return super.newCall(options);
}

Expand Down

0 comments on commit dd64f2f

Please sign in to comment.