Skip to content

Commit

Permalink
fix: support ice gathering already being complete in getLocalDescript…
Browse files Browse the repository at this point in the history
…ionWithIceCandidates (#40)
  • Loading branch information
bbaldino authored and GitHub Enterprise committed Sep 9, 2021
1 parent 8614994 commit e22429a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
25 changes: 18 additions & 7 deletions src/peer-connection-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { mocked } from './mocks/mock';

jest.mock('./peer-connection');

const dummyLocalDesc = {
type: 'offer',
sdp: 'sdp with candidates',
toJSON: () => undefined,
} as RTCSessionDescription;

describe('getLocalDescriptionWithIceCandidates', () => {
const mockPc = mocked(new PeerConnection(), true);
test('return the correct offer after ice gathering has finished', (done) => {
const localDesc = {
type: 'offer',
sdp: 'sdp with candidates',
toJSON: () => undefined,
} as RTCSessionDescription;
mockPc.getLocalDescription.mockReturnValueOnce(localDesc);
mockPc.getLocalDescription.mockReturnValueOnce(dummyLocalDesc);

const promise = new PromiseHelper(
getLocalDescriptionWithIceCandidates(mockPc as unknown as PeerConnection)
Expand Down Expand Up @@ -46,11 +47,12 @@ describe('getLocalDescriptionWithIceCandidates', () => {
Promise.resolve().then(() => {
expect(mockPc.getLocalDescription.mock.calls).toHaveLength(1);
expect(promise.isFinished()).toBe(true);
expect(promise.resolvedValue).toBe(localDesc);
expect(promise.resolvedValue).toBe(dummyLocalDesc);
done();
});
});
});

test('rejects if the local description is null', async () => {
mockPc.getLocalDescription.mockReturnValueOnce(null);
const promise = getLocalDescriptionWithIceCandidates(mockPc as unknown as PeerConnection);
Expand All @@ -62,6 +64,15 @@ describe('getLocalDescriptionWithIceCandidates', () => {
});
await expect(promise).rejects.toStrictEqual(expect.any(Error));
});

test('resolves immediately if the ICE candidates have already been gathered', () => {
mockPc.getLocalDescription.mockReturnValueOnce(dummyLocalDesc);
Object.defineProperty(mockPc, 'iceGatheringState', {
get: () => 'complete',
});

return getLocalDescriptionWithIceCandidates(mockPc as unknown as PeerConnection);
});
});

/**
Expand Down
23 changes: 17 additions & 6 deletions src/peer-connection-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@ export function getLocalDescriptionWithIceCandidates(
peerConnection: PeerConnection
): Promise<RTCSessionDescription> {
return new Promise((resolve, reject) => {
/**
* A helper method to retrieve the local description and resolve, if one is found, or reject
* with an error if it's not.
*/
const getLocalDescAndResolve = () => {
const localDesc = peerConnection.getLocalDescription();
if (localDesc) {
resolve(localDesc);
} else {
reject(new Error('Local description was null'));
}
};
peerConnection.on(PeerConnection.Events.IceGatheringStateChange, (e) => {
if (e.target.iceGatheringState === 'complete') {
const localDesc = peerConnection.getLocalDescription();
if (localDesc) {
resolve(localDesc);
} else {
reject(new Error('Local description was null'));
}
getLocalDescAndResolve();
}
// TODO(brian): throw an error if we see an error iceGatheringState
});
// It's possible ICE gathering is already done
if (peerConnection.iceGatheringState === 'complete') {
getLocalDescAndResolve();
}
});
}
9 changes: 9 additions & 0 deletions src/peer-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ class PeerConnection extends EventEmitter {
getTransceivers(): RTCRtpTransceiver[] {
return this.pc.getTransceivers();
}

/**
* Returns a string that describes the connections' ICE gathering state.
*
* @returns - The ICE gathering state.
*/
get iceGatheringState(): RTCIceGathererState {
return this.pc.iceGatheringState;
}
}

export { MediaStreamTrackKind, PeerConnection };

0 comments on commit e22429a

Please sign in to comment.