Skip to content

Commit

Permalink
feat: peer connection events; helper functions (#23)
Browse files Browse the repository at this point in the history
* feat: add 'getLocalDescription' method on PeerConnection

* feat: derive PeerConnection from EventEmitter; proxy icegatheringstatechange event

* feat: add function to wait for description with ice candidates

* fix: fix spelling errors

* refactor: use reject instead of throw
  • Loading branch information
bbaldino authored and GitHub Enterprise committed Aug 26, 2021
1 parent c1c798b commit 7fa6727
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Handles the WebRTC core functionality.

## Developement
## Development

1. `yarn`
1. `yarn watch`

## Usage

This library uses [cspell](https://github.com/streetsidesoftware/cspell) to check spelling throughout the codebase. Any words that need to be ignored (e.g., package names, protocols, etc.), should be added to the `ignoreWords` field in the [cpsell.json](./cspell.json) configuration file.
This library uses [cspell](https://github.com/streetsidesoftware/cspell) to check spelling throughout the codebase. Any words that need to be ignored (e.g., package names, protocols, etc.), should be added to the `ignoreWords` field in the [cspell.json](./cspell.json) configuration file.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ import * as media from './media';
export * from './local-track';
export * from './peer-connection';
export { media };

export * from './peer-connection-utils';
26 changes: 26 additions & 0 deletions src/peer-connection-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PeerConnection } from 'peer-connection';

/**
* Wait until the given peer connection has finished gathering ICE candidates and, when it has,
* return the local description with the candidates.
*
* @param peerConnection - The PeerConnection to use.
* @returns A Promise that resolves with the local description with the ICE candidates in it.
*/
export function getLocalDescriptionWithIceCandidates(
peerConnection: PeerConnection
): Promise<RTCSessionDescription> {
return new Promise((resolve, reject) => {
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'));
}
}
// TODO(brian): throw an error if we see an error iceGatheringState
});
});
}
23 changes: 22 additions & 1 deletion src/peer-connection.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import { LocalTrack } from './local-track';
import { log } from './util/logger';
import { EventEmitter } from './event-emitter';

/**
* Manages a single RTCPeerConnection with the server.
*/
class PeerConnection {
class PeerConnection extends EventEmitter {
static Events = {
IceGatheringStateChange: 'icegatheringstatechange',
};

private pc: RTCPeerConnection;

/**
* Creates an instance of the RTCPeerConnection.
*/
constructor() {
super();
log('PeerConnection init');

this.pc = new RTCPeerConnection();

// Bind event handlers.
this.handleTrackUpdate = this.handleTrackUpdate.bind(this);

// Subscribe to underlying PeerConnection events and emit them via the EventEmitter
/* eslint-disable jsdoc/require-jsdoc */
this.pc.onicegatheringstatechange = (ev: Event) => {
this.emit(PeerConnection.Events.IceGatheringStateChange, ev);
};
}

/**
Expand Down Expand Up @@ -107,6 +119,15 @@ class PeerConnection {
const sender = this.pc.getSenders().find((s: RTCRtpSender) => s.track?.id === oldTrackId);
sender?.replaceTrack(newTrack);
}

/**
* Get the local description from this PeerConnection.
*
* @returns An RTCSessionDescription representing the local description, or null if none has been set.
*/
getLocalDescription(): RTCSessionDescription | null {
return this.pc.localDescription;
}
}

export { PeerConnection };

0 comments on commit 7fa6727

Please sign in to comment.