Skip to content

Commit

Permalink
feat: support caller codec preference (#361)
Browse files Browse the repository at this point in the history
* feat: support caller codec preference

* feat: set codec preferences on answer
  • Loading branch information
farhat-ha authored Jun 10, 2024
1 parent e04435b commit cac97ac
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
3 changes: 3 additions & 0 deletions packages/js/src/Modules/Verto/webrtc/BaseCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ export default abstract class BaseCall implements IWebRTCCall {
customHeaders: params.customHeaders,
};
}
if (params.preferred_codecs?.length > 0) {
this.options.preferred_codecs = params.preferred_codecs;
}

this.peer = new Peer(PeerType.Answer, this.options, this.session);
await this.peer.iceGatheringComplete.promise;
Expand Down
35 changes: 24 additions & 11 deletions packages/js/src/Modules/Verto/webrtc/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ export default class Peer {
}

private handleIceCandidate = (event: RTCPeerConnectionIceEvent) => {
if (event.candidate && ['relay', 'srflx'].includes(event.candidate.type)) {
if (
event.candidate &&
['relay', 'srflx', 'prflx'].includes(event.candidate.type)
) {
// Found enough candidates to establish a connection
// This is a workaround for the issue where iceGatheringState is always 'gathering'
this.iceGatheringComplete.resolve(true);
Expand Down Expand Up @@ -219,20 +222,20 @@ export default class Peer {
// FIXME: use transceivers way only for offer - when answer gotta match mid from the ones from SRD
if (this.isOffer && typeof this.instance.addTransceiver === 'function') {
// Use addTransceiver

audioTracks.forEach((track) => {
this.options.userVariables.microphoneLabel = track.label;
this.instance.addTransceiver(track, {
direction: 'sendrecv',
streams: [localStream],
});
});

const transceiverParams: RTCRtpTransceiverInit = {
direction: 'sendrecv',
streams: [localStream],
};

audioTracks.forEach((track) => {
this.options.userVariables.microphoneLabel = track.label;
const transceiver = this.instance.addTransceiver(
track,
transceiverParams
);
this._setAudioCodec(transceiver);
});

console.debug('Applying video transceiverParams', transceiverParams);
videoTracks.forEach((track) => {
this.options.userVariables.cameraLabel = track.label;
Expand All @@ -245,6 +248,9 @@ export default class Peer {
this.options.userVariables.microphoneLabel = track.label;
this.instance.addTrack(track, localStream);
});
this.instance
.getTransceivers()
.forEach((trans) => this._setAudioCodec(trans));

videoTracks.forEach((track) => {
this.options.userVariables.cameraLabel = track.label;
Expand Down Expand Up @@ -391,6 +397,14 @@ export default class Peer {
return this.instance.setLocalDescription(sessionDescription);
}

private _setAudioCodec = (transceiver: RTCRtpTransceiver) => {
if (!this.options.preferred_codecs || this.options.preferred_codecs.length === 0) {
return;
}
if (transceiver.setCodecPreferences) {
return transceiver.setCodecPreferences(this.options.preferred_codecs);
}
};
/** Workaround for ReactNative: first time SDP has no candidates */
private _sdpReady(): void {
if (isFunction(this.onSdpReadyTwice)) {
Expand Down Expand Up @@ -438,7 +452,6 @@ export default class Peer {
if (!this.options.debug) {
return;
}
debugger

if (this.options.debugOutput === 'file') {
const timeline = this._webrtcStatsReporter.debuggerInstance.getTimeline();
Expand Down
2 changes: 2 additions & 0 deletions packages/js/src/Modules/Verto/webrtc/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface IVertoCallOptions {
customHeaders?: Array<{ name: string; value: string }>;
debug?: boolean;
debugOutput?: 'socket' | 'file';
preferred_codecs?: RTCRtpCodecCapability[];
}

export interface IStatsBinding {
Expand All @@ -56,6 +57,7 @@ export interface IStatsBinding {

export interface AnswerParams {
customHeaders?: Array<{ name: string; value: string }>;
preferred_codecs?: Array<RTCRtpCodecCapability>;
}

export interface IWebRTCCall {
Expand Down
15 changes: 15 additions & 0 deletions packages/js/src/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ export interface ICallOptions {
* Add custom headers to the INVITE and ANSWER request.
*/
customHeaders?: { name: string; value: string }[];

/**
* Enable debug mode for this call.
*/
debug?: boolean;

/**
* Output debug logs to a file.
*/
debugOutput?: 'socket' | 'file';

/**
* Preferred codecs for the call.
*/
preferred_codecs?: RTCRtpCodecCapability[];
}

/**
Expand Down

0 comments on commit cac97ac

Please sign in to comment.