Skip to content

Commit

Permalink
settings: Allow users to disable receiving audio and/or video
Browse files Browse the repository at this point in the history
Not receiving video may help for users with limited CPU or bandwidth.
  • Loading branch information
bekriebel committed Sep 16, 2021
1 parent 12e457a commit 0fe52c2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"LIVEKITAVCLIENT.connectError": "Error connecting to LiveKit Server",
"LIVEKITAVCLIENT.debug": "Enable debug logging",
"LIVEKITAVCLIENT.debugHint": "Enables CONFIG.debug.av and CONFIG.debug.avclient for extra logging",
"LIVEKITAVCLIENT.disableReceivingAudio": "Disable receiving audio",
"LIVEKITAVCLIENT.disableReceivingAudioHint": "Don't subscribe to other users' audio tracks",
"LIVEKITAVCLIENT.disableReceivingAudioWarning": "Receiving audio is disabled in LiveKit AVClient Module Settings",
"LIVEKITAVCLIENT.disableReceivingVideo": "Disable receiving video",
"LIVEKITAVCLIENT.disableReceivingVideoHint": "Don't subscribe to other users' video tracks. May be helpful if your bandwidth or CPU is limited",
"LIVEKITAVCLIENT.disableReceivingVideoWarning": "Receiving video is disabled in LiveKit AVClient Module Settings",
"LIVEKITAVCLIENT.disconnect": "Disconnect",
"LIVEKITAVCLIENT.indicatorMuted": "Audio Muted",
"LIVEKITAVCLIENT.indicatorHidden": "Video Hidden",
Expand Down
34 changes: 33 additions & 1 deletion src/LiveKitAVClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,43 @@ export default class LiveKitAVClient extends AVClient {

// Set the livekit connection options
const livekitConnectionOptions: ConnectOptions = {
tracks: localTracks,
autoSubscribe: true,
simulcast: getGame().settings.get(MODULE_NAME, "simulcast") === true,
tracks: localTracks,
videoEncoding: VideoPresets43.vga.encoding,
};

// Get disable audio/video settings
const disableReceivingAudio = getGame().settings.get(
MODULE_NAME,
"disableReceivingAudio"
);
const disableReceivingVideo = getGame().settings.get(
MODULE_NAME,
"disableReceivingVideo"
);

// Don't auto subscribe to tracks if either video or audio is disabled
if (disableReceivingAudio || disableReceivingVideo) {
livekitConnectionOptions.autoSubscribe = false;

// Send UI notifications
if (disableReceivingAudio) {
ui.notifications?.info(
`${getGame().i18n.localize(
`${LANG_NAME}.disableReceivingAudioWarning`
)}`
);
}
if (disableReceivingVideo) {
ui.notifications?.info(
`${getGame().i18n.localize(
`${LANG_NAME}.disableReceivingVideoWarning`
)}`
);
}
}

if (
getGame().settings.get(MODULE_NAME, "debug") &&
getGame().settings.get(MODULE_NAME, "livekitTrace")
Expand Down
49 changes: 46 additions & 3 deletions src/LiveKitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ export default class LiveKitClient {
// Set up remote participant callbacks
this.setRemoteParticipantCallbacks(participant);

participant.tracks.forEach((publication) => {
this.onTrackPublished(publication, participant);
});

// Call a debounced render
this.render();
}
Expand Down Expand Up @@ -587,6 +591,47 @@ export default class LiveKitClient {
this.addConnectionButtons(element);
}

onTrackPublished(
publication: RemoteTrackPublication,
participant: RemoteParticipant
): void {
log.debug("onTrackPublished:", publication, participant);

// Get configured settings
const disableReceivingAudio = getGame().settings.get(
MODULE_NAME,
"disableReceivingAudio"
);
const disableReceivingVideo = getGame().settings.get(
MODULE_NAME,
"disableReceivingVideo"
);

// Skip if neither video or audio are disabled; tracks will be auto-subscribed
if (!disableReceivingAudio && !disableReceivingVideo) {
return;
}

// Subscribe to a track if its type hasn't been disabled
if (publication.kind === Track.Kind.Audio && !disableReceivingAudio) {
publication.setSubscribed(true);
} else if (
publication.kind === Track.Kind.Video &&
!disableReceivingVideo
) {
publication.setSubscribed(true);
} else {
log.info(
"Not subscribing to",
publication.kind,
"track",
publication,
"for participant",
participant
);
}
}

async onTrackSubscribed(
track: RemoteTrack,
publication: RemoteTrackPublication,
Expand Down Expand Up @@ -775,9 +820,7 @@ export default class LiveKitClient {
RoomEvent.ParticipantDisconnected,
this.onParticipantDisconnected.bind(this)
)
.on(RoomEvent.TrackPublished, (...args) => {
log.debug("RoomEvent TrackPublished:", args);
})
.on(RoomEvent.TrackPublished, this.onTrackPublished.bind(this))
.on(RoomEvent.TrackSubscribed, this.onTrackSubscribed.bind(this))
.on(RoomEvent.TrackUnpublished, (...args) => {
log.debug("RoomEvent TrackUnpublished:", args);
Expand Down
18 changes: 18 additions & 0 deletions src/utils/registerModuleSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ import { delayReload, getGame, registerModuleSetting } from "./helpers";
import * as log from "./logging";

export default function registerModuleSettings(): void {
registerModuleSetting({
name: "disableReceivingAudio",
scope: "client",
config: true,
default: false,
type: Boolean,
onChange: () => getGame().webrtc?.connect(),
});

registerModuleSetting({
name: "disableReceivingVideo",
scope: "client",
config: true,
default: false,
type: Boolean,
onChange: () => getGame().webrtc?.connect(),
});

registerModuleSetting({
name: "simulcast",
scope: "world",
Expand Down

0 comments on commit 0fe52c2

Please sign in to comment.