diff --git a/src/GleapMarkerManager.js b/src/GleapMarkerManager.js index 774bd236..9d1fe2da 100644 --- a/src/GleapMarkerManager.js +++ b/src/GleapMarkerManager.js @@ -2,6 +2,7 @@ import Gleap, { GleapTranslationManager, GleapFrameManager, GleapCaptureManager import { ScreenDrawer } from './ScreenDrawer'; import { GleapScrollStopper } from './GleapScrollStopper'; import { GleapScreenRecorder } from './GleapScreenRecorder'; +import { renderMicButton } from './GleapScreenRecorderMicButton'; import { captureTabScreenshot, requestTabStream } from './GleapCaptureScreenshot'; import { canCaptureTab } from './GleapCaptureSettings'; import { loadIcon } from './UI'; @@ -645,20 +646,12 @@ export default class GleapMarkerManager { const type = toolbarItem.getAttribute('data-type'); switch (type) { case 'mic': - if (this.screenRecorder.audioAvailable && this.screenRecorder.available) { - toolbarItem.classList.remove(itemInactiveClass); - if (!this.screenRecorder.audioMuted) { - toolbarItem.classList.remove('bb-capture-toolbar-item--inactivecross'); - audioRecordingTooltip.innerHTML = GleapTranslationManager.translateText('mute'); - } else { - toolbarItem.classList.add('bb-capture-toolbar-item--inactivecross'); - audioRecordingTooltip.innerHTML = GleapTranslationManager.translateText('unmute'); - } - } else { - toolbarItem.classList.add(itemInactiveClass); - toolbarItem.classList.add('bb-capture-toolbar-item--inactivecross'); - audioRecordingTooltip.innerHTML = GleapTranslationManager.translateText('browserNotSupported'); - } + renderMicButton(toolbarItem, { + state: this.screenRecorder.micButtonState(), + tooltip: audioRecordingTooltip, + inactiveClass: itemInactiveClass, + translate: (key) => GleapTranslationManager.translateText(key), + }); break; case 'recording': diff --git a/src/GleapScreenRecorder.js b/src/GleapScreenRecorder.js index fe6fb900..3d061503 100644 --- a/src/GleapScreenRecorder.js +++ b/src/GleapScreenRecorder.js @@ -1,7 +1,10 @@ import GleapSession from './GleapSession'; import { DisplayRecording, requestDisplayStream } from './GleapCaptureRecorder'; import { stopStream } from './GleapCaptureScreenshot'; -import { hasDisplayMedia, isDesktopChromium } from './GleapCaptureSettings'; +import { getCaptureConfig, hasDisplayMedia, hasUserMedia, isDesktopChromium } from './GleapCaptureSettings'; +import { MIC_BUTTON } from './GleapScreenRecorderMicButton'; + +export { MIC_BUTTON }; // The feedback form's screen recording ("Record screen" in the form's capture step). It records the // same way capture requests do (GleapCaptureRecorder): Chromium offers the current tab with one @@ -21,8 +24,9 @@ export class GleapScreenRecorder { rerender; // The running DisplayRecording, from the Start click until it stopped. recording = null; - audioMuted = false; - audioAvailable = true; + // Opt-in: off until the user turns it on, and never used unless the project allows it. + audioMuted = true; + audioAvailable = false; available = true; isRecording = false; file = null; @@ -32,12 +36,40 @@ export class GleapScreenRecorder { this.rerender = rerender; this.permissionErrorText = permissionErrorText; this.available = hasDisplayMedia(); + this.audioAvailable = this.microphoneAllowed(); setTimeout(() => { this.rerender(); }, GleapScreenRecorder.RERENDER_DELAY); } + /** + * Whether this recording may use the microphone: Developer options → Microphone is on and the + * browser has getUserMedia. Read on every use, as the project's config can arrive after the form. + * @returns {boolean} + */ + microphoneAllowed() { + return getCaptureConfig().allowMicrophone === true && hasUserMedia(); + } + + /** + * What the toolbar's mic button shows. A recording that started without the microphone can't + * add it, so the button is hidden until it ends. + * @returns {string} one of MIC_BUTTON + */ + micButtonState() { + if (!this.available || !this.microphoneAllowed()) { + return MIC_BUTTON.HIDDEN; + } + if (this.isRecording && this.audioMuted && !this.audioAvailable) { + return MIC_BUTTON.HIDDEN; + } + if (!this.audioAvailable) { + return MIC_BUTTON.UNAVAILABLE; + } + return this.audioMuted ? MIC_BUTTON.OFF : MIC_BUTTON.ON; + } + formatTime(s) { return (s - (s %= 60)) / 60 + (9 < s ? ':' : ':0') + s; } @@ -83,7 +115,7 @@ export class GleapScreenRecorder { stopStream(stream); return null; } - return recording.start(stream, !this.audioMuted).then(({ hasMicrophone }) => { + return recording.start(stream, this.microphoneAllowed() && !this.audioMuted).then(({ hasMicrophone }) => { this.audioAvailable = hasMicrophone; this.isRecording = true; this.setTimerLabel(GleapScreenRecorder.MAX_RECORD_TIME); @@ -110,6 +142,8 @@ export class GleapScreenRecorder { } this.recording = null; this.isRecording = false; + // The next recording may ask for the microphone again. + this.audioAvailable = this.microphoneAllowed(); this.setTimerLabel(GleapScreenRecorder.MAX_RECORD_TIME); if (result) { @@ -135,6 +169,9 @@ export class GleapScreenRecorder { } toggleAudio() { + if (!this.microphoneAllowed() || !this.audioAvailable) { + return; + } this.audioMuted = !this.audioMuted; if (this.recording) { this.recording.setMicrophoneEnabled(!this.audioMuted); diff --git a/src/GleapScreenRecorder.test.js b/src/GleapScreenRecorder.test.js index 1dece9ac..626edf12 100644 --- a/src/GleapScreenRecorder.test.js +++ b/src/GleapScreenRecorder.test.js @@ -2,9 +2,9 @@ * @vitest-environment jsdom */ import { test, expect, vi, beforeEach } from 'vitest'; -import { GleapScreenRecorder } from './GleapScreenRecorder'; +import { GleapScreenRecorder, MIC_BUTTON } from './GleapScreenRecorder'; import { requestDisplayStream } from './GleapCaptureRecorder'; -import { hasDisplayMedia, isDesktopChromium } from './GleapCaptureSettings'; +import { getCaptureConfig, hasDisplayMedia, hasUserMedia, isDesktopChromium } from './GleapCaptureSettings'; import { stopStream } from './GleapCaptureScreenshot'; // The feedback form's recorder runs on the capture requests' recording engine (#228450). @@ -13,7 +13,9 @@ const mockRecordings = []; vi.mock('./GleapSession', () => ({ __esModule: true, default: { getInstance: () => ({}) } })); vi.mock('./GleapCaptureScreenshot', () => ({ stopStream: vi.fn() })); vi.mock('./GleapCaptureSettings', () => ({ + getCaptureConfig: vi.fn(() => ({})), hasDisplayMedia: vi.fn(() => true), + hasUserMedia: vi.fn(() => true), isDesktopChromium: vi.fn(() => true), })); vi.mock('./GleapCaptureRecorder', () => ({ @@ -21,7 +23,8 @@ vi.mock('./GleapCaptureRecorder', () => ({ DisplayRecording: class { constructor(options) { this.options = options; - this.start = vi.fn(() => Promise.resolve({ hasMicrophone: true })); + // Like the real engine: a microphone track only when one was asked for. + this.start = vi.fn((stream, withMicrophone) => Promise.resolve({ hasMicrophone: !!withMicrophone })); this.stop = vi.fn(); this.cancel = vi.fn(); this.setMicrophoneEnabled = vi.fn(); @@ -40,6 +43,9 @@ beforeEach(() => { mockRecordings.length = 0; rerender = vi.fn(); hasDisplayMedia.mockReturnValue(true); + hasUserMedia.mockReturnValue(true); + // Developer options → Microphone is on in most tests; the setting's own tests turn it off. + getCaptureConfig.mockReturnValue({ allowMicrophone: true }); isDesktopChromium.mockReturnValue(true); requestDisplayStream.mockReset().mockReturnValue(Promise.resolve(STREAM)); stopStream.mockClear(); @@ -50,8 +56,9 @@ beforeEach(() => { '
'; }); -test('Start asks for the current tab (Chromium) inside the click and records with the microphone', async () => { +test('Start asks for the current tab (Chromium) inside the click and records with the microphone the user turned on', async () => { const recorder = createRecorder(); + recorder.toggleAudio(); recorder.startScreenRecording(); // Synchronously: the share dialog needs the click's user activation. @@ -64,10 +71,10 @@ test('Start asks for the current tab (Chromium) inside the click and records wit expect(recorder.audioAvailable).toBe(true); }); -test('other browsers get their own picker; a muted start records without the microphone', async () => { +test('other browsers get their own picker; the microphone is off until the user turns it on', async () => { isDesktopChromium.mockReturnValue(false); const recorder = createRecorder(); - recorder.toggleAudio(); + expect(recorder.audioMuted).toBe(true); recorder.startScreenRecording(); await flush(); @@ -75,6 +82,40 @@ test('other browsers get their own picker; a muted start records without the mic expect(mockRecordings[0].start).toHaveBeenCalledWith(STREAM, false); }); +// A site that already holds microphone permission gets no browser prompt, so the setting is the +// only thing that keeps sound out of the recording. +test('with the Microphone setting off the recording never asks for the microphone', async () => { + getCaptureConfig.mockReturnValue({}); + const recorder = createRecorder(); + expect(recorder.microphoneAllowed()).toBe(false); + expect(recorder.audioAvailable).toBe(false); + + // The mic button is hidden then; a click on it still changes nothing. + recorder.toggleAudio(); + expect(recorder.audioMuted).toBe(true); + recorder.startScreenRecording(); + await flush(); + + expect(mockRecordings[0].start).toHaveBeenCalledWith(STREAM, false); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).not.toHaveBeenCalled(); + expect(recorder.audioAvailable).toBe(false); +}); + +test('allowMicrophone must be exactly true, and the browser must have getUserMedia', async () => { + getCaptureConfig.mockReturnValue({ allowMicrophone: 'true' }); + expect(createRecorder().microphoneAllowed()).toBe(false); + + getCaptureConfig.mockReturnValue({ allowMicrophone: true }); + hasUserMedia.mockReturnValue(false); + const recorder = createRecorder(); + expect(recorder.microphoneAllowed()).toBe(false); + recorder.toggleAudio(); + recorder.startScreenRecording(); + await flush(); + expect(mockRecordings[0].start).toHaveBeenCalledWith(STREAM, false); +}); + test('the timer counts down and the stopped recording becomes the file and the preview', async () => { const recorder = createRecorder(); recorder.startScreenRecording(); @@ -131,3 +172,53 @@ test('without getDisplayMedia the recorder is unavailable', () => { expect(recorder.available).toBe(false); expect(requestDisplayStream).not.toHaveBeenCalled(); }); + +test('the mic button: hidden without the setting, off by default, on once turned on', () => { + getCaptureConfig.mockReturnValue({}); + expect(createRecorder().micButtonState()).toBe(MIC_BUTTON.HIDDEN); + + getCaptureConfig.mockReturnValue({ allowMicrophone: true }); + const recorder = createRecorder(); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.OFF); + recorder.toggleAudio(); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.ON); + recorder.toggleAudio(); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.OFF); +}); + +test('started without the microphone: the button goes away for that recording and is back after it', async () => { + const recorder = createRecorder(); + recorder.startScreenRecording(); + await flush(); + + expect(recorder.micButtonState()).toBe(MIC_BUTTON.HIDDEN); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).not.toHaveBeenCalled(); + + mockRecordings[0].options.onStop(null); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.OFF); +}); + +test('turned on but the browser gave no microphone: shown as unavailable, mute does nothing', async () => { + const recorder = createRecorder(); + recorder.toggleAudio(); + recorder.startScreenRecording(); + mockRecordings[0].start.mockReturnValue(Promise.resolve({ hasMicrophone: false })); + await flush(); + + expect(recorder.micButtonState()).toBe(MIC_BUTTON.UNAVAILABLE); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).not.toHaveBeenCalled(); +}); + +test('muting while recording with the microphone turns its track off', async () => { + const recorder = createRecorder(); + recorder.toggleAudio(); + recorder.startScreenRecording(); + await flush(); + + expect(recorder.micButtonState()).toBe(MIC_BUTTON.ON); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).toHaveBeenCalledWith(false); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.OFF); +}); diff --git a/src/GleapScreenRecorderMicButton.js b/src/GleapScreenRecorderMicButton.js new file mode 100644 index 00000000..9819fa24 --- /dev/null +++ b/src/GleapScreenRecorderMicButton.js @@ -0,0 +1,38 @@ +/** + * What the toolbar's mic button shows (GleapScreenRecorder.micButtonState). + */ +export const MIC_BUTTON = Object.freeze({ + // The project doesn't allow the microphone, or this recording started without it. + HIDDEN: 'hidden', + // Asked for, but the browser gave none. + UNAVAILABLE: 'unavailable', + ON: 'on', + OFF: 'off', +}); + +const CROSSED_CLASS = 'bb-capture-toolbar-item--inactivecross'; + +const TOOLTIP_KEYS = Object.freeze({ + [MIC_BUTTON.ON]: 'mute', + [MIC_BUTTON.OFF]: 'unmute', + [MIC_BUTTON.UNAVAILABLE]: 'browserNotSupported', +}); + +/** + * Shows the feedback form toolbar's mic button for one of MIC_BUTTON's states. + * @param {HTMLElement} item the mic button + * @param {{state: string, tooltip: HTMLElement|null, inactiveClass: string, translate: function(string): string}} options + * @returns {void} + */ +export const renderMicButton = (item, { state, tooltip, inactiveClass, translate }) => { + item.style.display = state === MIC_BUTTON.HIDDEN ? 'none' : ''; + if (state === MIC_BUTTON.HIDDEN) { + return; + } + const usable = state === MIC_BUTTON.ON || state === MIC_BUTTON.OFF; + item.classList.toggle(inactiveClass, !usable); + item.classList.toggle(CROSSED_CLASS, state !== MIC_BUTTON.ON); + if (tooltip) { + tooltip.textContent = translate(TOOLTIP_KEYS[state] || TOOLTIP_KEYS[MIC_BUTTON.UNAVAILABLE]); + } +}; diff --git a/src/GleapScreenRecorderMicButton.test.js b/src/GleapScreenRecorderMicButton.test.js new file mode 100644 index 00000000..d0466f46 --- /dev/null +++ b/src/GleapScreenRecorderMicButton.test.js @@ -0,0 +1,60 @@ +/** + * @vitest-environment jsdom + */ +import { test, expect, beforeEach } from 'vitest'; +import { MIC_BUTTON, renderMicButton } from './GleapScreenRecorderMicButton'; + +const INACTIVE = 'bb-capture-editor-item-inactive'; +const CROSSED = 'bb-capture-toolbar-item--inactivecross'; + +let item; +let tooltip; +const render = (state) => + renderMicButton(item, { state, tooltip, inactiveClass: INACTIVE, translate: (key) => `t:${key}` }); + +beforeEach(() => { + document.body.innerHTML = + ''; + item = document.querySelector('[data-type="mic"]'); + tooltip = document.querySelector('.bb-tooltip-audio-recording'); +}); + +test('hidden: the button is not shown at all', () => { + render(MIC_BUTTON.HIDDEN); + expect(item.style.display).toBe('none'); +}); + +test('off: shown crossed out, the tooltip offers to unmute', () => { + render(MIC_BUTTON.OFF); + expect(item.style.display).toBe(''); + expect(item.classList.contains(INACTIVE)).toBe(false); + expect(item.classList.contains(CROSSED)).toBe(true); + expect(tooltip.textContent).toBe('t:unmute'); +}); + +test('on: shown without the cross, the tooltip offers to mute', () => { + render(MIC_BUTTON.OFF); + render(MIC_BUTTON.ON); + expect(item.classList.contains(INACTIVE)).toBe(false); + expect(item.classList.contains(CROSSED)).toBe(false); + expect(tooltip.textContent).toBe('t:mute'); +}); + +test('unavailable: shown inactive and crossed out', () => { + render(MIC_BUTTON.UNAVAILABLE); + expect(item.classList.contains(INACTIVE)).toBe(true); + expect(item.classList.contains(CROSSED)).toBe(true); + expect(tooltip.textContent).toBe('t:browserNotSupported'); +}); + +test('shown again after being hidden', () => { + render(MIC_BUTTON.HIDDEN); + render(MIC_BUTTON.OFF); + expect(item.style.display).toBe(''); +}); + +test('a tooltip that is not there is skipped', () => { + tooltip = null; + expect(() => render(MIC_BUTTON.ON)).not.toThrow(); + expect(item.classList.contains(CROSSED)).toBe(false); +});