Skip to content

Commit

Permalink
feat: apply policy settings to tracks added via addTrack
Browse files Browse the repository at this point in the history
  • Loading branch information
raviteja83 authored Sep 18, 2024
1 parent b1bdfdb commit c094ee2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
46 changes: 44 additions & 2 deletions packages/hms-video-store/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { HMSAction } from '../error/HMSAction';
import { HMSException } from '../error/HMSException';
import { EventBus } from '../events/EventBus';
import {
HMSAudioCodec,
HMSChangeMultiTrackStateParams,
HMSConfig,
HMSConnectionQualityListener,
Expand All @@ -37,6 +38,7 @@ import {
HMSRole,
HMSRoleChangeRequest,
HMSScreenShareConfig,
HMSVideoCodec,
TokenRequest,
TokenRequestOptions,
} from '../interfaces';
Expand All @@ -51,6 +53,7 @@ import { RTMPRecordingConfig } from '../interfaces/rtmp-recording-config';
import InitialSettings from '../interfaces/settings';
import { HMSAudioListener, HMSPeerUpdate, HMSTrackUpdate, HMSUpdateListener } from '../interfaces/update-listener';
import { PlaylistManager, TranscriptionConfig } from '../internal';
import { HMSAudioTrackSettingsBuilder, HMSVideoTrackSettingsBuilder } from '../media/settings';
import { HMSLocalStream } from '../media/streams/HMSLocalStream';
import {
HMSLocalAudioTrack,
Expand Down Expand Up @@ -95,7 +98,7 @@ import HMSLogger, { HMSLogLevel } from '../utils/logger';
import { HMSAudioContextHandler } from '../utils/media';
import { isNode } from '../utils/support';
import { workerSleep } from '../utils/timer-utils';
import { validateMediaDevicesExistence, validateRTCPeerConnection } from '../utils/validations';
import { validateMediaDevicesExistence, validatePublishParams, validateRTCPeerConnection } from '../utils/validations';

const INITIAL_STATE = {
published: false,
Expand Down Expand Up @@ -954,7 +957,8 @@ export class HMSSdk implements HMSInterface {

const TrackKlass = type === 'audio' ? HMSLocalAudioTrack : HMSLocalVideoTrack;
const hmsTrack = new TrackKlass(stream, track, source, this.eventBus);
this.setPlaylistSettings({
await this.applySettings(hmsTrack);
await this.setPlaylistSettings({
track,
hmsTrack,
source,
Expand Down Expand Up @@ -1603,4 +1607,42 @@ export class HMSSdk implements HMSInterface {
this.playlistManager.stop(HMSPlaylistType.video);
}
}

// eslint-disable-next-line complexity
private async applySettings(track: HMSLocalTrack) {
validatePublishParams(this.store);
const publishParams = this.store.getPublishParams();
// this is not needed but added for avoiding ? later
if (!publishParams) {
return;
}
if (track instanceof HMSLocalVideoTrack) {
const publishKey = track.source === 'regular' ? 'video' : track.source === 'screen' ? 'screen' : '';
if (!publishKey || !publishParams.allowed.includes(publishKey)) {
return;
}
const video = publishParams[publishKey];
if (!video) {
return;
}
const settings = new HMSVideoTrackSettingsBuilder()
.codec(video.codec as HMSVideoCodec)
.maxBitrate(video.bitRate)
.maxFramerate(video.frameRate)
.setWidth(video.width)
.setHeight(video.height)
.build();

await track.setSettings(settings);
} else if (track instanceof HMSLocalAudioTrack) {
if (!publishParams.allowed.includes('audio')) {
return;
}
const settings = new HMSAudioTrackSettingsBuilder()
.codec(publishParams.audio.codec as HMSAudioCodec)
.maxBitrate(publishParams.audio.bitRate)
.build();
await track.setSettings(settings);
}
}
}
12 changes: 12 additions & 0 deletions packages/hms-video-store/src/utils/validations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import HMSLogger from './logger';
import { ErrorFactory } from '../error/ErrorFactory';
import { HMSAction } from '../error/HMSAction';
import { Store } from '../sdk/store';

const TAG = `[VALIDATIONS]`;

Expand Down Expand Up @@ -32,3 +34,13 @@ export const validateMediaDevicesExistence = () => {
throw error;
}
};

export const validatePublishParams = (store: Store) => {
const publishParams = store.getPublishParams();
if (!publishParams) {
throw ErrorFactory.GenericErrors.NotConnected(
HMSAction.VALIDATION,
'call addTrack after preview or join is successful',
);
}
};

0 comments on commit c094ee2

Please sign in to comment.