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

Consider device key '' when evaluating for 'default' device #239

Closed
Closed
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions lib/twilio/rtc/peerconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,9 @@ PeerConnection.prototype._reassignMasterOutput = function reassignMasterOutput(p
pc.outputs.delete(masterId);

const self = this;
const idToReplace = Array.from(pc.outputs.keys())[0] || 'default';
const activeDevice = Array.from(pc.outputs.keys())[0];
// The audio device key could also be '' on Chrome if no media device permissions are allowed
const idToReplace = activeDevice !== null && activeDevice !== undefined ? activeDevice : 'default';
Copy link
Author

Choose a reason for hiding this comment

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

Checking against undefined might be enough if guarding against outputs being empty?

return masterOutput.audio.setSinkId(idToReplace).then(() => {
self._disableOutput(pc, idToReplace);

Expand Down Expand Up @@ -586,7 +588,9 @@ PeerConnection.prototype._onAddTrack = function onAddTrack(pc, stream) {
audio.play();

// Assign the initial master audio element to a random active output device
const deviceId = Array.from(pc.outputs.keys())[0] || 'default';
const activeDevice = Array.from(pc.outputs.keys())[0];
// The audio device key could also be '' on Chrome if no media device permissions are allowed
const deviceId = activeDevice !== null && activeDevice !== undefined ? activeDevice : 'default';
pc._masterAudioDeviceId = deviceId;
pc.outputs.set(deviceId, { audio });

Expand Down
17 changes: 17 additions & 0 deletions tests/peerconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,23 @@ describe('PeerConnection', () => {
}).then(done).catch(done);
});

it('Should call setSinkId with \'\' if empty string is first device', done => {
output.audio.setSinkId.returns(Promise.resolve());
pc.outputs.delete('a');
pc.outputs.delete('b');
pc.outputs.delete('c');
pc.outputs.set('', '1');
toTest().then(() => {
assert(pc.outputs.has(''));
assert.equal(pc.outputs.has(MASTER_ID), false);
assert(output.audio.setSinkId.calledWithExactly(''));
assert(context._disableOutput.calledWithExactly(pc, ''));
assert.deepStrictEqual(pc.outputs.get(''), output);
assert.deepStrictEqual(pc._masterAudioDeviceId, '');
assert(output.audio.setSinkId.calledBefore(context._disableOutput));
}).then(done).catch(done);
});

it('Should add back pc output when setSinkId rejects', () => {
output.audio.setSinkId.returns(Promise.reject(ERROR));
return toTest().then(() => {
Expand Down