Skip to content

Commit

Permalink
fix: firefox does not support permission api
Browse files Browse the repository at this point in the history
close #11
  • Loading branch information
Patrick Porto committed Mar 4, 2023
1 parent 5f02171 commit 3812918
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions src/avsettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,15 @@ export class AVQOLSettings extends FormApplication {
}

async getData() {
const microphoneStatus = await navigator.permissions.query({
// @ts-ignore
name: "microphone",
});
const cameraStatus = await navigator.permissions.query({
// @ts-ignore
name: "camera",
});
const microphoneStatus = await this.getMicrophoneStatus();
const cameraStatus = await this.getCameraStatus();
const rtcWorldSettings = getRTCWorldSettings();
const avqol = getAVQOLAPI();
debug("AVQOLSettings.getData", microphoneStatus, cameraStatus)
return {
avatar: (game as Game).user?.avatar ?? DEFAULT_AVATAR,
microphoneStatus: microphoneStatus.state,
cameraStatus: cameraStatus.state,
microphoneStatus: microphoneStatus,
cameraStatus: cameraStatus,
audioDep: [
AVSettings.AV_MODES.AUDIO,
AVSettings.AV_MODES.AUDIO_VIDEO,
Expand Down Expand Up @@ -158,23 +153,8 @@ export class AVQOLSettings extends FormApplication {

async activateListeners(html: JQuery<HTMLElement>) {
super.activateListeners(html);
// await this.checkPermissions();
await this.checkPermissions();

const microphoneStatus = await navigator.permissions.query({
// @ts-ignore
name: "microphone",
});
microphoneStatus.addEventListener("change", () => {
this.render(true);
});
const cameraStatus = await navigator.permissions.query({
// @ts-ignore
name: "camera",
});
cameraStatus.addEventListener("change", async () => {
this.render(true);
await this.changeVideoPreviewSource(html);
});
$(html)
.find("#virtualBackground")
.on("change", async () => {
Expand All @@ -201,6 +181,32 @@ export class AVQOLSettings extends FormApplication {
await this.checkVideoEffectAvailability(html);
}

private async getCameraStatus() {
try {
const status = await navigator.permissions.query({
// @ts-ignore
name: "camera",
});
return status.state;
} catch (e) {
debug("Check camera status not supported.");
return 'granted';
}
}

private async getMicrophoneStatus() {
try {
const status = await navigator.permissions.query({
// @ts-ignore
name: "microphone",
});
return status.state;
} catch (e) {
debug("Check camera status not supported.");
return 'granted';
}
}

async checkVideoEffectAvailability(html: JQuery<HTMLElement>) {
if (!cameraEffectsIsSupported()) {
$(html)
Expand All @@ -219,19 +225,19 @@ export class AVQOLSettings extends FormApplication {
$(html).find("#virtualBackground").removeAttr("disabled");
}

// private async checkPermissions() {
// const data = await this.getData();
// const audioPermission =
// data.microphoneStatus == "prompt" && data.audioDep;
// const videoPermission = data.cameraStatus == "prompt" && data.videoDep;
// if (audioPermission || videoPermission) {
// await this.requestPermissions();
// }
// }
private async checkPermissions() {
const data = await this.getData();
const audioPermission =
data.microphoneStatus == "prompt" && data.audioDep;
const videoPermission = data.cameraStatus == "prompt" && data.videoDep;
if (audioPermission || videoPermission) {
await this.requestPermissions();
}
}

async _updateObject(event: Event, formData: any) {
debug("Updating RTC Client settings", formData);
this.disableHearMyself()
this.disableHearMyself();
const avqol = getAVQOLAPI();
if (!avqol.allowPlay) {
avqol.allowPlay = true;
Expand Down Expand Up @@ -453,7 +459,7 @@ export class AVQOLSettings extends FormApplication {

private disableHearMyself() {
if (!this.hearMyselfAudio) {
return
return;
}
this.hearMyselfAudio.srcObject = null;
this.hearMyselfAudio.remove();
Expand Down

0 comments on commit 3812918

Please sign in to comment.