Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable video when in background #2571

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });
};
}
Loading