Skip to content

Commit

Permalink
Merge branch 'main' into feat/ts-pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
raviteja83 committed Feb 24, 2024
2 parents 92a7eab + 3a1dde7 commit e9898e9
Show file tree
Hide file tree
Showing 30 changed files with 121 additions and 85 deletions.
2 changes: 1 addition & 1 deletion examples/prebuilt-react-integration/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/hls-player/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/hls-stats/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/hms-video-store/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/hms-video-store/src/analytics/AnalyticsEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export default class AnalyticsEvent implements ISignalParamsProvider<SignalEvent
user_name?: string;
user_data?: string;
};
userAgent?: string;
userAgent: string;
} = {
peer: {},
userAgent: createUserAgent(),
};
timestamp: number;
event_id: string;
Expand All @@ -54,7 +55,6 @@ export default class AnalyticsEvent implements ISignalParamsProvider<SignalEvent
this.timestamp = timestamp || new Date().getTime(); // Timestamp of generating the event
this.event_id = uuid();
this.device_id = getAnalyticsDeviceId();
createUserAgent().then(userAgent => (this.metadata.userAgent = userAgent));
}

toSignalParams() {
Expand Down
13 changes: 5 additions & 8 deletions packages/hms-video-store/src/analytics/HTTPAnalyticsTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,13 @@ class ClientAnalyticsTransport implements IAnalyticsTransportProvider {
},
};
const url = this.env === ENV.PROD ? CLIENT_ANAYLTICS_PROD_ENDPOINT : CLIENT_ANAYLTICS_QA_ENDPOINT;
const headers = new Headers({
'Content-Type': 'application/json',
Authorization: `Bearer ${event.metadata.token}`,
});
if (event.metadata.userAgent) {
headers.set('user_agent_v2', event.metadata.userAgent);
}
fetch(url, {
method: 'POST',
headers,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${event.metadata.token}`,
user_agent_v2: event.metadata.userAgent,
},
body: JSON.stringify(requestBody),
})
.then(response => {
Expand Down
2 changes: 1 addition & 1 deletion packages/hms-video-store/src/interfaces/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface HMSRoom {
* @alpha
*/
effectsKey?: string;
isHippaEnabled?: boolean;
isHipaaEnabled?: boolean;
isNoiseCancellationEnabled?: boolean;
}

Expand Down
20 changes: 20 additions & 0 deletions packages/hms-video-store/src/media/tracks/HMSLocalVideoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { HMSMediaStreamPlugin, HMSVideoPluginsManager } from '../../plugins/vide
import { HMSMediaStreamPluginsManager } from '../../plugins/video/HMSMediaStreamPluginsManager';
import { LocalTrackManager } from '../../sdk/LocalTrackManager';
import HMSLogger from '../../utils/logger';
import { isBrowser, isMobile } from '../../utils/support';
import { getVideoTrack, isEmptyTrack } from '../../utils/track';
import { HMSVideoTrackSettings, HMSVideoTrackSettingsBuilder } from '../settings';
import { HMSLocalStream } from '../streams';
Expand All @@ -36,6 +37,7 @@ export class HMSLocalVideoTrack extends HMSVideoTrack {
private processedTrack?: MediaStreamTrack;
private _layerDefinitions: HMSSimulcastLayerDefinition[] = [];
private TAG = '[HMSLocalVideoTrack]';
private enabledStateBeforeBackground = false;

/**
* true if it's screenshare and current tab is what is being shared. Browser dependent, Chromium only
Expand Down Expand Up @@ -80,6 +82,9 @@ export class HMSLocalVideoTrack extends HMSVideoTrack {
this.pluginsManager = new HMSVideoPluginsManager(this, eventBus);
this.mediaStreamPluginsManager = new HMSMediaStreamPluginsManager(eventBus);
this.setFirstTrackId(this.trackId);
if (isBrowser && isMobile()) {
document.addEventListener('visibilitychange', this.handleVisibilityChange);
}
}

/** @internal */
Expand Down Expand Up @@ -232,6 +237,9 @@ export class HMSLocalVideoTrack extends HMSVideoTrack {
await this.pluginsManager.cleanup();
this.processedTrack?.stop();
this.isPublished = false;
if (isBrowser && isMobile()) {
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
}
}

/**
Expand Down Expand Up @@ -477,4 +485,16 @@ export class HMSLocalVideoTrack extends HMSVideoTrack {
}
await this.replaceSenderTrack(this.processedTrack || this.nativeTrack);
};

private handleVisibilityChange = async () => {
if (document.visibilityState === 'hidden' && this.source === 'regular') {
this.enabledStateBeforeBackground = this.enabled;
this.nativeTrack.enabled = false;
this.replaceSenderTrack(this.nativeTrack);
} else {
this.nativeTrack.enabled = this.enabledStateBeforeBackground;
this.replaceSenderTrack(this.nativeTrack);
}
this.eventBus.localVideoEnabled.publish({ enabled: this.nativeTrack.enabled, track: this });
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { HMSAction } from '../../error/HMSAction';
import { EventBus } from '../../events/EventBus';
import { HMSLocalVideoTrack } from '../../media/tracks';
import HMSLogger from '../../utils/logger';
import { workerSleep } from '../../utils/timer-utils';
import { reusableWorker, workerSleep } from '../../utils/timer-utils';
import { HMSPluginUnsupportedTypes } from '../audio';

const DEFAULT_FRAME_RATE = 24;
Expand Down Expand Up @@ -57,6 +57,7 @@ export class HMSVideoPluginsManager {
private pluginNumFramesToSkip: Record<string, number>;
private pluginNumFramesSkipped: Record<string, number>;
private canvases: Array<CanvasElement>; //array of canvases to store intermediate result
private reusableWorker = reusableWorker();

constructor(track: HMSLocalVideoTrack, eventBus: EventBus) {
this.hmsTrack = track;
Expand Down Expand Up @@ -288,7 +289,7 @@ export class HMSVideoPluginsManager {
this.resetCanvases();
}
this.pluginsLoopState = 'paused';
await workerSleep(sleepTimeMs);
await this.reusableWorker.sleep(sleepTimeMs);
continue;
}
let processingTime = 0;
Expand All @@ -306,7 +307,7 @@ export class HMSVideoPluginsManager {
}
this.pluginsLoopState = 'running';
// take into account processing time to decide time to wait for the next loop
await workerSleep(sleepTimeMs - processingTime);
await this.reusableWorker.sleep(sleepTimeMs - processingTime);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/hms-video-store/src/reactive-store/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class SDKToHMS {
isLargeRoom: sdkRoom.large_room_optimization,
isEffectsEnabled: sdkRoom.isEffectsEnabled,
effectsKey: sdkRoom.effectsKey,
isHippaEnabled: sdkRoom.isHippaEnabled,
isHipaaEnabled: sdkRoom.isHipaaEnabled,
isNoiseCancellationEnabled: sdkRoom.isNoiseCancellationEnabled,
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hms-video-store/src/schema/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ export interface HMSRoom {
isLargeRoom?: boolean;
isEffectsEnabled?: boolean;
effectsKey?: string;
isHippaEnabled?: boolean;
isHipaaEnabled?: boolean;
isNoiseCancellationEnabled?: boolean;
}
8 changes: 4 additions & 4 deletions packages/hms-video-store/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export class HMSSdk implements HMSInterface {
}

this.analyticsTimer.start(TimedEvent.PREVIEW);
await this.setUpPreview(config, listener);
this.setUpPreview(config, listener);

// Request permissions and populate devices before waiting for policy
if (config.alwaysRequestPermissions) {
Expand Down Expand Up @@ -517,7 +517,7 @@ export class HMSSdk implements HMSInterface {
this.removeDevicesFromConfig(config);
this.store.setConfig(config);
/** set after config since we need config to get env for user agent */
await this.store.createAndSetUserAgent(this.frameworkInfo);
this.store.createAndSetUserAgent(this.frameworkInfo);
HMSAudioContextHandler.resumeContext();
// acquire screen lock to stay awake while in call
const storeConfig = this.store.getConfig();
Expand Down Expand Up @@ -1239,15 +1239,15 @@ export class HMSSdk implements HMSInterface {
* @param {HMSConfig} config
* @param {HMSPreviewListener} listener
*/
private async setUpPreview(config: HMSPreviewConfig, listener: HMSPreviewListener) {
private setUpPreview(config: HMSPreviewConfig, listener: HMSPreviewListener) {
this.listener = listener as unknown as HMSUpdateListener;
this.sdkState.isPreviewCalled = true;
this.sdkState.isPreviewInProgress = true;
const { roomId, userId, role } = decodeJWT(config.authToken);
this.commonSetup(config, roomId, listener);
this.store.setConfig(config);
/** set after config since we need config to get env for user agent */
await this.store.createAndSetUserAgent(this.frameworkInfo);
this.store.createAndSetUserAgent(this.frameworkInfo);
this.createAndAddLocalPeerToStore(config, role, userId, config.asRole);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/hms-video-store/src/sdk/models/HMSRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class Room implements HMSRoom {
* @alpha
*/
effectsKey?: string;
isHippaEnabled?: boolean;
isHipaaEnabled?: boolean;
isNoiseCancellationEnabled?: boolean;

constructor(id: string) {
Expand Down
6 changes: 3 additions & 3 deletions packages/hms-video-store/src/sdk/store/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Store {
private roleDetailsArrived = false;
private env: ENV = ENV.PROD;
private simulcastEnabled = false;
private userAgent?: string;
private userAgent: string = createUserAgent(this.env);
private polls = new Map<string, HMSPoll>();
private whiteboards = new Map<string, HMSWhiteboard>();

Expand Down Expand Up @@ -188,8 +188,8 @@ class Store {
return this.userAgent;
}

async createAndSetUserAgent(frameworkInfo?: HMSFrameworkInfo) {
this.userAgent = await createUserAgent(this.env, frameworkInfo);
createAndSetUserAgent(frameworkInfo?: HMSFrameworkInfo) {
this.userAgent = createUserAgent(this.env, frameworkInfo);
}

setRoom(room: HMSRoom) {
Expand Down
8 changes: 4 additions & 4 deletions packages/hms-video-store/src/signal/init/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { HMSException } from '../../error/HMSException';
import { ENV } from '../../utils/support';
import { createUserAgent } from '../../utils/user-agent';

describe('getUrl', async () => {
const userAgent = await createUserAgent(ENV.PROD);
describe('getUrl', () => {
const userAgent = createUserAgent(ENV.PROD);
const userAgentQueryParam = new URLSearchParams(`user_agent_v2=${userAgent}`).toString();
const peerId = '1234';
it('should return the URL even if unnecesary params are passed to the endpoint', () => {
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('transformInit', () => {
});
});

describe('init API call', async () => {
describe('init API call', () => {
const peerId = '2e26acc7-d2c8-4235-883e-812695ff1e7d';
const correctToken =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2Nlc3Nfa2V5IjoiNjEwY2Q5Y2JmMzBlNzczZjQ3NTc3YjBkIiwicm9vbV9pZCI6IjYxOGU5NGY1YWYzMTg4ZGYzM2U2N2Q0NiIsInVzZXJfaWQiOiJiZTM5MzQwZC04ZDgzLTQ5ZjQtOTNhMy00ZjRmMTgwZTVkZWUiLCJyb2xlIjoiaG9zdCIsImp0aSI6IjY0ZTRjMTgzLWZkNTktNGE2OS1hOGY2LWNkNGE5MzBmOTYzZSIsInR5cGUiOiJhcHAiLCJ2ZXJzaW9uIjoyLCJleHAiOjE2NTIyNjUyNzV9.t1Wvwl0tXyMzi386LwfDACvUeWibZYIzSf20DTwjqJU';
Expand All @@ -76,7 +76,7 @@ describe('init API call', async () => {
const wrongToken =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2Nlc3Nfa2V5IjoiNjEwY2Q5Y2JmMzBlNzczZjQ3NTc3YjBkIiwicm9vbV9pZCI6IjYxOGU5NGY1YWYzMTg4ZGYzM2U2N2Q0NyIsInVzZXJfaWQiOiJiZTM5MzQwZC04ZDgzLTQ5ZjQtOTNhMy00ZjRmMTgwZTVkZWUiLCJyb2xlIjoiaG9zdCIsImp0aSI6IjY0ZTRjMTgzLWZkNTktNGE2OS1hOGY2LWNkNGE5MzBmOTYzZSIsInR5cGUiOiJhcHAiLCJ2ZXJzaW9uIjoyLCJleHAiOjE2NTIyNjUyNzV9.tX4BZllTjOuA5L3bgItoDYKQa6J3d-L2cayvQiEntHY';

const userAgent = await createUserAgent(ENV.PROD);
const userAgent = createUserAgent(ENV.PROD);

const mockResponse = (init: RequestInit | undefined): Promise<Response> => {
const headers = init?.headers as Record<string, string>;
Expand Down
2 changes: 1 addition & 1 deletion packages/hms-video-store/src/signal/init/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ export enum InitFlags {
FLAG_DISABLE_VIDEO_TRACK_AUTO_UNSUBSCRIBE = 'disableVideoTrackAutoUnsubscribe',
FLAG_WHITEBOARD_ENABLED = 'whiteboardEnabled',
FLAG_EFFECTS_SDK_ENABLED = 'effectsSDKEnabled',
FLAG_HIPPA_ENABLED = 'hippa',
FLAG_HIPAA_ENABLED = 'hipaa',
FLAG_NOISE_CANCELLATION = 'noiseCancellation',
}
14 changes: 3 additions & 11 deletions packages/hms-video-store/src/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,22 +896,18 @@ export default class HMSTransport {
HMSLogger.d(TAG, 'connect: started ⏰');
const connectRequestedAt = new Date();
try {
const userAgent = this.store.getUserAgent();
if (!userAgent) {
throw ErrorFactory.GenericErrors.PeerMetadataMissing(HMSAction.INIT, 'User Agent not available');
}
this.analyticsTimer.start(TimedEvent.INIT);
this.initConfig = await InitService.fetchInitConfig({
token,
peerId,
userAgent,
userAgent: this.store.getUserAgent(),
initEndpoint,
});
const room = this.store.getRoom();
if (room) {
room.effectsKey = this.initConfig.config.vb?.effectsKey;
room.isEffectsEnabled = this.isFlagEnabled(InitFlags.FLAG_EFFECTS_SDK_ENABLED);
room.isHippaEnabled = this.isFlagEnabled(InitFlags.FLAG_HIPPA_ENABLED);
room.isHipaaEnabled = this.isFlagEnabled(InitFlags.FLAG_HIPAA_ENABLED);
room.isNoiseCancellationEnabled = this.isFlagEnabled(InitFlags.FLAG_NOISE_CANCELLATION);
}
this.analyticsTimer.end(TimedEvent.INIT);
Expand Down Expand Up @@ -955,16 +951,12 @@ export default class HMSTransport {
if (!this.initConfig) {
throw ErrorFactory.APIErrors.InitConfigNotAvailable(HMSAction.INIT, 'Init Config not found');
}
const userAgent = this.store.getUserAgent();
if (!userAgent) {
throw ErrorFactory.GenericErrors.PeerMetadataMissing(HMSAction.INIT, 'User Agent not available');
}

HMSLogger.d(TAG, '⏳ internal connect: connecting to ws endpoint', this.initConfig.endpoint);
const url = new URL(this.initConfig.endpoint);
url.searchParams.set('peer', peerId);
url.searchParams.set('token', token);
url.searchParams.set('user_agent_v2', userAgent);
url.searchParams.set('user_agent_v2', this.store.getUserAgent());
url.searchParams.set('protocol_version', PROTOCOL_VERSION);
url.searchParams.set('protocol_spec', PROTOCOL_SPEC);

Expand Down
2 changes: 1 addition & 1 deletion packages/hms-video-store/src/utils/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export const isPageHidden = () => typeof document !== 'undefined' && document.hi

export const isIOS = () => parsedUserAgent.getOS().name?.toLowerCase() === 'ios';

export const isFirefox = parsedUserAgent.getBrowser()?.name?.toLowerCase().includes('firefox');
export const isFirefox = parsedUserAgent.getBrowser()?.name?.toLowerCase() === 'firefox';
23 changes: 22 additions & 1 deletion packages/hms-video-store/src/utils/timer-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const worker = `(function metronomeWorkerSetup() {
export const worker = `(function workerSetup() {
function ticker() {
self.postMessage('tick');
}
Expand Down Expand Up @@ -47,6 +47,27 @@ export function workerSleep(ms: number): Promise<void> {
});
}

export function reusableWorker() {
if (typeof Worker === 'undefined') {
return {
sleep: (ms: number) => sleep(ms),
};
}
const WorkerThread = new Worker(URL.createObjectURL(new Blob([worker], { type: 'application/javascript' })));
return {
sleep: (ms: number) => {
WorkerThread.postMessage(['start', ms]);
return new Promise<void>(resolve => {
WorkerThread.onmessage = event => {
if (event.data === 'tick') {
resolve();
}
};
});
},
};
}

/**
* Debounce Fn - Function to limit the number of executions of the passed in
* function in a given time duration
Expand Down
Loading

0 comments on commit e9898e9

Please sign in to comment.