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

[VBLOCKS-3256] Feat: listen for navigator permissions changes #283

Merged
merged 8 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
2.11.3 (In Progress)
======================

Bug Fixes
---------

- Fixed an issue where `PreflightTest` throws an error when `RTCIceCandidateStatsReport` is not available. Thanks @phi-line for your [contribution](https://github.com/twilio/twilio-voice.js/pull/280).

Improvements
------------

- The SDK now updates its internal device list when the microphone permission changes.

2.11.2 (June 26, 2024)
======================

Expand Down
33 changes: 33 additions & 0 deletions lib/twilio/audiohelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,21 @@ class AudioHelper extends EventEmitter {
*/
private _mediaDevices: AudioHelper.MediaDevicesLike | null;

/**
* The microphone permission status
*/
private _microphonePermissionStatus: PermissionStatus | null;

/**
* Called with the new input stream when the active input is changed.
*/
private _onActiveInputChanged: (stream: MediaStream | null) => Promise<void>;

/**
* Handler for microphone permission status change
*/
private _onMicrophonePermissionStatusChanged: () => void;

/**
* Internal reference to the processed stream
*/
Expand Down Expand Up @@ -275,6 +285,19 @@ class AudioHelper extends EventEmitter {
if (isEnumerationSupported) {
this._initializeEnumeration();
}

kpchoy marked this conversation as resolved.
Show resolved Hide resolved
// NOTE (kchoy): Currently microphone permissions are not supported in firefox.
// https://github.com/mozilla/standards-positions/issues/19#issuecomment-370158947
navigator.permissions.query({ name: 'microphone' }).then((microphonePermissionStatus) => {
if (microphonePermissionStatus.state !== 'granted') {
const handleStateChange = () => {
this._updateAvailableDevices();
kpchoy marked this conversation as resolved.
Show resolved Hide resolved
};
microphonePermissionStatus.addEventListener('change', handleStateChange);
this._microphonePermissionStatus = microphonePermissionStatus;
this._onMicrophonePermissionStatusChanged = handleStateChange;
}
}).catch((reason) => this._log.warn(`Warning: unable to listen for microphone permission changes. ${reason}`));
}

/**
Expand All @@ -287,6 +310,7 @@ class AudioHelper extends EventEmitter {
this._destroyProcessedStream();
this._maybeStopPollingVolume();
this.removeAllListeners();
this._stopMicrophonePermissionListener();
this._unbind();
}

Expand Down Expand Up @@ -799,6 +823,15 @@ class AudioHelper extends EventEmitter {
});
}

/**
* Remove event listener for microphone permissions
*/
private _stopMicrophonePermissionListener(): void {
if (this._microphonePermissionStatus?.removeEventListener) {
this._microphonePermissionStatus.removeEventListener('change', this._onMicrophonePermissionStatusChanged);
}
}

/**
* Stop the selected audio stream
*/
Expand Down
55 changes: 53 additions & 2 deletions tests/audiohelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ function getUserMedia() {
return Promise.resolve({ id: 'default', getTracks: () => [] });
}

const defaultNavigator = {
permissions: {
query: function() {
return Promise.resolve('prompt');
}
},
mediaDevices: {
enumerateDevices: function(){
return Promise.resolve([]);
}
}
};

describe('AudioHelper', () => {
context('when enumerateDevices is not supported', () => {
const noop = () => {};
Expand Down Expand Up @@ -34,7 +47,7 @@ describe('AudioHelper', () => {
? navigator
: undefined;
HTMLAudioElement = undefined;
navigator = { };
navigator = defaultNavigator;
});

after(() => {
Expand Down Expand Up @@ -68,6 +81,8 @@ describe('AudioHelper', () => {
let availableDevices;
let handlers;
let mediaDevices;
let oldHTMLAudioElement;
let oldNavigator;

beforeEach(() => {
eventObserver = new AudioProcessorEventObserver();
Expand Down Expand Up @@ -100,6 +115,23 @@ describe('AudioHelper', () => {
});
});

before(() => {
oldHTMLAudioElement = typeof HTMLAudioElement !== 'undefined'
? HTMLAudioElement
: undefined;
oldNavigator = typeof navigator !== 'undefined'
? navigator
: undefined;
HTMLAudioElement = undefined;
navigator = defaultNavigator
});

after(() => {
HTMLAudioElement = oldHTMLAudioElement;
navigator = oldNavigator;
});


describe('constructor', () => {
it('should set .isOutputSelectionSupported to true', () => {
assert.equal(audio.isOutputSelectionSupported, true);
Expand All @@ -124,20 +156,39 @@ describe('AudioHelper', () => {
});
});

describe('navigator.permissions', () => {
it('should listen for microphone state changes', () => {
let onChangeHandler = sinon.stub();
let microphonePermissionStatus = {
state: 'prompt',
addEventListener: (eventName, listener) => {
if (eventName === 'change') {
listener()
}
}
};
navigator.permissions.query = () => Promise.resolve(microphonePermissionStatus);
audio = new AudioHelper(onActiveOutputsChanged, onActiveInputChanged, {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this test might not going through SDK codepaths, unless my understanding is incorrect. If you remove these two lines, I don't think much changes? It seems like you're creating the microphonePermissionStatus object above and then lines 172~173 test that code path?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point :( . I am having a hard time testing this feature correctly. Any suggestions?
cc: @charliesantos

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get the implementation approved then I'll review the tests 😄
That way, we don't have to keep updating the tests while we review the PR.

microphonePermissionStatus.addEventListener('change', onChangeHandler);
sinon.assert.calledOnce(onChangeHandler);
})
})

describe('._destroy', () => {
it('should properly dispose the audio instance', () => {
audio._stopDefaultInputDeviceStream = sinon.stub();
audio._stopSelectedInputDeviceStream = sinon.stub();
audio._destroyProcessedStream = sinon.stub();
audio._maybeStopPollingVolume = sinon.stub();
audio._stopMicrophonePermissionListener = sinon.stub();
kpchoy marked this conversation as resolved.
Show resolved Hide resolved
audio._destroy();
assert.strictEqual(audio.eventNames().length, 0);
sinon.assert.calledOnce(audio._stopDefaultInputDeviceStream);
sinon.assert.calledOnce(audio._stopSelectedInputDeviceStream);
sinon.assert.calledOnce(audio._destroyProcessedStream);
sinon.assert.calledOnce(audio._maybeStopPollingVolume);
sinon.assert.calledOnce(mediaDevices.removeEventListener);

sinon.assert.calledOnce(audio._stopMicrophonePermissionListener);
kpchoy marked this conversation as resolved.
Show resolved Hide resolved
});
});

Expand Down
5 changes: 5 additions & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ root.navigator = {
},
platform: 'platform',
userAgent: 'userAgent',
permissions: {
query: function() {
return Promise.resolve('prompt');
}
}
kpchoy marked this conversation as resolved.
Show resolved Hide resolved
};

root.RTCPeerConnection = root.window.RTCPeerConnection = function() { };
Expand Down