Skip to content

Commit

Permalink
Feat/use track method (#22)
Browse files Browse the repository at this point in the history
* feat: setUnderlyingTrack method

* feat: useTrack method

* feat: track emits event to be handled by peer connection for new track

* fix: change method name to 'replaceUnderlyingTrack'

* fix: changed 'trackId' to 'oldTrackId'

* style: alphabetize local-track methods
  • Loading branch information
mccarthytyler authored and GitHub Enterprise committed Aug 23, 2021
1 parent 3fdf38c commit d0510f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/local-track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import { Track } from './track';
* A wrapper around MediaStreamTrack.
*/
export class LocalTrack extends Track {
/**
* Exchange the underlying track with a new track. Emit the `track-update` event to alert
* listeners to an update on a new track.
*
* @param track - New underlying track.
* @fires LocalTrack#track-update
*/
replaceUnderlyingTrack(track: MediaStreamTrack): void {
this.emit('track-update', this.getUnderlyingTrack().id, track);
this.getUnderlyingTrack().stop();
this.setUnderlyingTrack(track);
}

/**
* Sets the track to be enabled or disabled.
*
Expand Down
16 changes: 16 additions & 0 deletions src/peer-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class PeerConnection {
log('PeerConnection init');

this.pc = new RTCPeerConnection();

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

/**
Expand All @@ -23,8 +26,10 @@ class PeerConnection {
* @param streams - (Optional) One or more local MediaStream objects to which the track should be
* added.
* @returns The RTCRtpSender object which will be used to transmit the media data.
* @listens LocalTrack#track-update
*/
addTrack(track: LocalTrack, ...streams: MediaStream[]): RTCRtpSender {
track.on('track-update', this.handleTrackUpdate);
return this.pc.addTrack(track.getUnderlyingTrack(), ...streams);
}

Expand Down Expand Up @@ -91,6 +96,17 @@ class PeerConnection {
close(): void {
this.pc.close();
}

/**
* Handles `track-update` event and replaces track on sender.
*
* @param oldTrackId - Id of the existing track.
* @param newTrack - New LocalTrack.
*/
handleTrackUpdate(oldTrackId: string, newTrack: MediaStreamTrack): void {
const sender = this.pc.getSenders().find((s: RTCRtpSender) => s.track?.id === oldTrackId);
sender?.replaceTrack(newTrack);
}
}

export { PeerConnection };
9 changes: 9 additions & 0 deletions src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ export abstract class Track extends EventEmitter {
getUnderlyingTrack(): MediaStreamTrack {
return this.track;
}

/**
* Sets the underlying track.
*
* @param track - The new underlying track.
*/
setUnderlyingTrack(track: MediaStreamTrack): void {
this.track = track;
}
}

0 comments on commit d0510f8

Please sign in to comment.