Skip to content

Commit

Permalink
feat: dtmf support
Browse files Browse the repository at this point in the history
  • Loading branch information
farhat-ha committed Apr 3, 2024
1 parent 4dbe549 commit 312978d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
14 changes: 12 additions & 2 deletions packages/janus/src/Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SwEvent } from "./constants";
import { SIPAnswerTransaction } from "./transactions/SIPAnswer";
import { SIPHangupTransaction } from "./transactions/SIPHangup";
import { CallState, ICall, ICallOptions } from "./types";
import { SIPDTMFTransaction } from "./transactions/SIPDTMF";

type CallDirection = "inbound" | "outbound";
type TelnyxIds = {
Expand Down Expand Up @@ -138,8 +139,17 @@ export default class Call implements ICall {
deaf(): void {
throw new Error("Method not implemented.");
}
dtmf(_dtmf: string): Promise<void> {
throw new Error("Method not implemented.");
async dtmf(digit: string): Promise<void> {
if (!connection.gatewayHandleId || !connection.gatewaySessionId) {
throw new Error("Gateway handle or session id not found");
}
await transactionManager.execute(
new SIPDTMFTransaction({
digit,
gatewayHandleId: connection.gatewayHandleId,
gatewaySessionId: connection.gatewaySessionId,
})
);
}
hold(): Promise<void> {
throw new Error("Method not implemented.");
Expand Down
4 changes: 2 additions & 2 deletions packages/janus/src/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export default class Peer {

private _setupLocalStream = async () => {
const constrains: MediaStreamConstraints = {
audio: this._callOptions.audio ?? true,
video: this._callOptions.video ?? false,
audio: true,
video: false,
};

const stream = await navigator.mediaDevices
Expand Down
16 changes: 14 additions & 2 deletions packages/janus/src/messages/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export type JanusSIPAnswerRequest = {
autoaccept_reinvites: false;
};
transaction?: string;
jsep: RTCSessionDescriptionInit
jsep: RTCSessionDescriptionInit;
session_id: number;
handle_id: number;
};
Expand All @@ -92,5 +92,17 @@ export type JanusSIPHangupRequest = {
};

export type JanusSIPUnregisterRequest = {
request: "unregister";
janus: Janus.message;
session_id: number;
handle_id: number;
body: {
request: "unregister";
};
};

export type JanusSIPDTMFRequest = {
janus: Janus.message;
session_id: number;
handle_id: number;
body: { request: "dtmf_info"; digit: string };
};
34 changes: 34 additions & 0 deletions packages/janus/src/transactions/SIPDTMF.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Janus } from "../messages/janus";
import { JanusSIPDTMFRequest } from "../messages/request";
import { JanusResponse } from "../messages/response";
import { BaseTransaction } from "./BaseTransaction";

type DTMFConstructorParams = {
digit: string;
gatewaySessionId: number;
gatewayHandleId: number;
};

export class SIPDTMFTransaction extends BaseTransaction<
boolean,
JanusSIPDTMFRequest
> {
constructor({
digit,
gatewayHandleId,
gatewaySessionId,
}: DTMFConstructorParams) {
super({
janus: Janus.message,
body: {
request: "dtmf_info",
digit,
},
session_id: gatewayHandleId,
handle_id: gatewaySessionId,
});
}
public onMessage(_msg: JanusResponse): void {
this._resolve(true);
}
}

0 comments on commit 312978d

Please sign in to comment.