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

fix(incoming-sound): stop incoming sound when the call gets disconnected #134

Merged
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
6 changes: 6 additions & 0 deletions lib/twilio/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,12 @@ class Device extends EventEmitter {
}
this._removeCall(call);
maybeUnsetPreferredUri();
/**
* NOTE(kamalbennani): We need to stop the incoming sound when the call is
* disconnected right after the user has accepted the call (activeCall.accept()), and before
* the call has been fully connected (i.e. before the `pstream.answer` event)
*/
this._maybeStopIncomingSound();
});

call.once('reject', () => {
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,11 @@ describe('Device', function() {
});

describe('with a pending incoming call', () => {
let spyIncomingSound: any;
beforeEach(async () => {
spyIncomingSound = { play: sinon.spy(), stop: sinon.spy() };
device['_soundcache'].set(Device.SoundName.Incoming, spyIncomingSound);

const incomingPromise = new Promise(resolve =>
device.once(Device.EventName.Incoming, () => {
device.emit = sinon.spy();
Expand Down Expand Up @@ -762,19 +766,29 @@ describe('Device', function() {
});

it('should not play outgoing sound', () => {
sinon.assert.calledOnce(spyIncomingSound.play);
const spy: any = { play: sinon.spy() };
device['_soundcache'].set(Device.SoundName.Outgoing, spy);
device.calls[0].emit('accept');
sinon.assert.notCalled(spy.play);
sinon.assert.calledOnce(spyIncomingSound.stop);
});

it('should update the preferred uri', () => {
sinon.assert.calledOnce(spyIncomingSound.play);
pstream.emit('connected', { edge: 'sydney' });
const spy: any = device['_stream'].updatePreferredURI =
sinon.spy(device['_stream'].updatePreferredURI);
const call = device.calls[0];
call.emit('accept');
sinon.assert.calledOnce(spy);
sinon.assert.calledOnce(spyIncomingSound.stop);
});

it('should stop playing incoming sound', () => {
sinon.assert.calledOnce(spyIncomingSound.play);
device.calls[0].emit("disconnect");
sinon.assert.calledOnce(spyIncomingSound.stop);
});
});

Expand All @@ -792,6 +806,13 @@ describe('Device', function() {
device.calls[0].emit('error');
sinon.assert.calledOnceWithExactly(spy, null);
});

it('should stop playing incoming sound', () => {
sinon.assert.calledOnce(spyIncomingSound.play);
device.calls[0].status = () => CallType.State.Closed;
device.calls[0].emit('error');
sinon.assert.calledOnce(spyIncomingSound.stop);
});
});

describe('on call.transportClose', () => {
Expand All @@ -805,6 +826,12 @@ describe('Device', function() {
device.calls[0].emit('transportClose');
assert.equal(device.calls.length, 1);
});
it('should stop playing incoming sound', () => {
sinon.assert.calledOnce(spyIncomingSound.play);
device.calls[0].status = () => CallType.State.Pending;
device.calls[0].emit('transportClose');
sinon.assert.calledOnce(spyIncomingSound.stop);
});
});

describe('on call.disconnect', () => {
Expand All @@ -824,6 +851,12 @@ describe('Device', function() {
device.calls[0].emit('disconnect');
sinon.assert.calledOnceWithExactly(spy, null);
});

it('should stop playing incoming sound', () => {
sinon.assert.calledOnce(spyIncomingSound.play);
device.calls[0].emit('disconnect');
sinon.assert.calledOnce(spyIncomingSound.stop);
});
});

describe('on call.reject', () => {
Expand All @@ -838,6 +871,12 @@ describe('Device', function() {
device.calls[0].emit('reject');
sinon.assert.calledOnceWithExactly(spy, null);
});

it('should stop playing incoming sound', () => {
sinon.assert.calledOnce(spyIncomingSound.play);
device.calls[0].emit('reject');
sinon.assert.calledOnce(spyIncomingSound.stop);
});
});
});

Expand Down