Skip to content

Commit

Permalink
Refactor: replace ugly transportRemoteParameters, `consumerRemotePa…
Browse files Browse the repository at this point in the history
…rameters`, `transportLocalParameters`, `producerLocalParameters`, etc. with destructuring Objects with proper and well known keys.
  • Loading branch information
ibc committed Feb 4, 2019
1 parent 7063573 commit 9c61f48
Show file tree
Hide file tree
Showing 17 changed files with 540 additions and 373 deletions.
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import mySignaling from './my-signaling'; // Our own signaling stuff.
const device = new Device();

// Communicate with our server app to retrieve router RTP capabilities.
const routerRtpCapabilities =
await mySignaling.request('getRouterCapabilities');
const routerRtpCapabilities = await mySignaling.request('getRouterCapabilities');

// Load the device with the router RTP capabilities.
await device.load({ routerRtpCapabilities });
Expand All @@ -42,27 +41,28 @@ if (!device.canProduce('video'))
}

// Create a transport in the server for sending our media through it.
const sendTransportRemoteParameters =
await mySignaling.request('createTransport');
const {
id,
iceParameters,
iceCandidates,
dtlsParameters
} = await mySignaling.request('createTransport');

// Create the local representation of our server-side transport.
const sendTransport = device.createTransport(
{
transportRemoteParameters : sendTransportRemoteParameters,
direction : 'send'
});
const sendTransport =
device.createSendTransport({ id, iceParameters, iceCandidates, dtlsParameters });

// Set transport "connect" event handler.
sendTransport.on('connect', async (transportLocalParameters, callback, errback) =>
sendTransport.on('connect', async ({ dtlsParameters }, callback, errback) =>
{
// Here we must communicate our local parameters to our remote transport.
try
{
await mySignaling.request(
'transport-connect',
{
transportId : sendTransport.id,
transportParameters : transportLocalParameters
{
transportId: sendTransport.id,
dtlsParameters
});

// Done in the server, tell our transport.
Expand All @@ -76,20 +76,22 @@ sendTransport.on('connect', async (transportLocalParameters, callback, errback)
});

// Set transport "produce" event handler.
sendTransport.on('produce', async (producerLocalParameters, callback, errback) =>
sendTransport.on('produce', async ({ kind, rtpParameters, appData }, callback, errback) =>
{
// Here we must communicate our local parameters to our remote transport.
try
{
const producerRemoteParameters = await mySignaling.request(
const { id } = await mySignaling.request(
'produce',
{
transportId : sendTransport.id,
producerParameters : producerLocalParameters
transportId : sendTransport.id,
kind,
rtpParameters,
appData
});

// Done in the server, pass the response to our transport.
callback(producerRemoteParameters);
callback({ id });
}
catch (error)
{
Expand Down
52 changes: 40 additions & 12 deletions lib/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ class Device
/**
* Creates a Transport for sending media.
*
* @param {Object} transportRemoteParameters - Server-side transport parameters.
* @param {String} - Server-side Transport id.
* @param {RTCIceParameters} iceParameters - Server-side Transport ICE parameters.
* @param {Array<RTCIceCandidate>} [iceCandidates] - Server-side Transport ICE candidates.
* @param {RTCDtlsParameters} dtlsParameters - Server-side Transport DTLS parameters.
* @param {Array<RTCIceServer>} [iceServers] - Array of ICE servers.
* @param {RTCIceTransportPolicy} [iceTransportPolicy] - ICE transport
* policy.
Expand All @@ -165,7 +168,10 @@ class Device
*/
createSendTransport(
{
transportRemoteParameters,
id,
iceParameters,
iceCandidates,
dtlsParameters,
iceServers,
iceTransportPolicy,
proprietaryConstraints,
Expand All @@ -178,7 +184,10 @@ class Device
return this._createTransport(
{
direction : 'send',
transportRemoteParameters,
id,
iceParameters,
iceCandidates,
dtlsParameters,
iceServers,
iceTransportPolicy,
proprietaryConstraints,
Expand All @@ -189,7 +198,10 @@ class Device
/**
* Creates a Transport for receiving media.
*
* @param {Object} transportRemoteParameters - Server-side transport parameters.
* @param {String} - Server-side Transport id.
* @param {RTCIceParameters} iceParameters - Server-side Transport ICE parameters.
* @param {Array<RTCIceCandidate>} [iceCandidates] - Server-side Transport ICE candidates.
* @param {RTCDtlsParameters} dtlsParameters - Server-side Transport DTLS parameters.
* @param {Array<RTCIceServer>} [iceServers] - Array of ICE servers.
* @param {RTCIceTransportPolicy} [iceTransportPolicy] - ICE transport
* policy.
Expand All @@ -202,7 +214,10 @@ class Device
*/
createRecvTransport(
{
transportRemoteParameters,
id,
iceParameters,
iceCandidates,
dtlsParameters,
iceServers,
iceTransportPolicy,
proprietaryConstraints,
Expand All @@ -215,7 +230,10 @@ class Device
return this._createTransport(
{
direction : 'recv',
transportRemoteParameters,
id,
iceParameters,
iceCandidates,
dtlsParameters,
iceServers,
iceTransportPolicy,
proprietaryConstraints,
Expand All @@ -229,7 +247,10 @@ class Device
_createTransport(
{
direction,
transportRemoteParameters,
id,
iceParameters,
iceCandidates,
dtlsParameters,
iceServers,
iceTransportPolicy,
proprietaryConstraints,
Expand All @@ -241,18 +262,25 @@ class Device

if (!this._loaded)
throw new InvalidStateError('not loaded');
else if (typeof transportRemoteParameters !== 'object')
throw new TypeError('missing transportRemoteParameters');
else if (!transportRemoteParameters.id)
throw new TypeError('missing transportRemoteParameters.id');
else if (typeof id !== 'string')
throw new TypeError('missing id');
else if (typeof iceParameters !== 'object')
throw new TypeError('missing iceParameters');
else if (!Array.isArray(iceCandidates))
throw new TypeError('missing iceCandidates');
else if (typeof dtlsParameters !== 'object')
throw new TypeError('missing dtlsParameters');
else if (appData && typeof appData !== 'object')
throw new TypeError('if given, appData must be an object');

// Create a new Transport.
const transport = new Transport(
{
direction,
transportRemoteParameters,
id,
iceParameters,
iceCandidates,
dtlsParameters,
iceServers,
iceTransportPolicy,
proprietaryConstraints,
Expand Down
88 changes: 41 additions & 47 deletions lib/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class Transport extends EnhancedEventEmitter
constructor(
{
direction,
transportRemoteParameters,
id,
iceParameters,
iceCandidates,
dtlsParameters,
iceServers,
iceTransportPolicy,
proprietaryConstraints,
Expand All @@ -33,13 +36,11 @@ class Transport extends EnhancedEventEmitter
{
super(logger);

logger.debug(
'constructor() [id:%s, direction:%s]',
transportRemoteParameters.id, direction);
logger.debug('constructor() [id:%s, direction:%s]', id, direction);

// Id.
// @type {String}
this._id = transportRemoteParameters.id;
this._id = id;

// Closed flag.
// @type {Boolean}
Expand All @@ -62,8 +63,10 @@ class Transport extends EnhancedEventEmitter
// @type {Handler}
this._handler = new Handler(
{
transportRemoteParameters,
direction,
iceParameters,
iceCandidates,
dtlsParameters,
iceServers,
iceTransportPolicy,
proprietaryConstraints,
Expand Down Expand Up @@ -213,24 +216,24 @@ class Transport extends EnhancedEventEmitter
/**
* Restart ICE connection.
*
* @param {RTCIceParameters} remoteIceParameters
* @param {RTCIceParameters} iceParameters - New Server-side Transport ICE parameters.
*
* @async
* @throws {InvalidStateError} if Transport closed.
* @throws {TypeError} if wrong arguments.
*/
async restartIce({ remoteIceParameters } = {})
async restartIce({ iceParameters } = {})
{
logger.debug('restartIce()');

if (this._closed)
throw new InvalidStateError('closed');
else if (!remoteIceParameters)
throw new TypeError('missing remoteIceParameters');
else if (!iceParameters)
throw new TypeError('missing iceParameters');

// Enqueue command.
return this._awaitQueue.push(
async () => this._handler.restartIce({ remoteIceParameters }));
async () => this._handler.restartIce({ iceParameters }));
}

/**
Expand Down Expand Up @@ -326,22 +329,15 @@ class Transport extends EnhancedEventEmitter

try
{
const producerRemoteParameters = await this.safeEmitAsPromise(
const { id } = await this.safeEmitAsPromise(
'produce',
// producerLocalParameters.
{
kind : track.kind,
rtpParameters,
appData
});

const producer = new Producer(
{
id : producerRemoteParameters.id,
track,
rtpParameters,
appData
});
const producer = new Producer({ id, track, rtpParameters, appData });

this._producers.set(producer.id, producer);
this._handleProducer(producer);
Expand Down Expand Up @@ -370,7 +366,10 @@ class Transport extends EnhancedEventEmitter
/**
* Consume a remote Producer.
*
* @param {String} consumerRemoteParameters - Server-side Consumer parameters.
* @param {String} id - Server-side Consumer id.
* @param {String} producerId - Server-side Producer id.
* @param {String} kind - 'audio' or 'video'.
* @param {RTCRtpParameters} rtpParameters - Server-side Consumer RTP parameters.
* @param {Object} [appData={}] - Custom app data.
*
* @async
Expand All @@ -379,20 +378,29 @@ class Transport extends EnhancedEventEmitter
* @throws {TypeError} if wrong arguments.
* @throws {UnsupportedError} if Transport direction is incompatible.
*/
async consume({ consumerRemoteParameters, appData = {} } = {})
async consume(
{
id,
producerId,
kind,
rtpParameters,
appData = {}
} = {})
{
logger.debug('consume()');

if (this._closed)
throw new InvalidStateError('closed');
else if (this._direction !== 'recv')
throw new UnsupportedError('not a receiving Transport');
else if (typeof consumerRemoteParameters !== 'object')
throw new TypeError('missing consumerRemoteParameters');
else if (!consumerRemoteParameters.id)
throw new TypeError('missing consumerRemoteParameters.id');
else if (!consumerRemoteParameters.producerId)
throw new TypeError('missing consumerRemoteParameters.producerId');
else if (typeof id !== 'string')
throw new TypeError('missing id');
else if (typeof producerId !== 'string')
throw new TypeError('missing producerId');
else if (kind !== 'audio' && kind !== 'video')
throw new TypeError(`invalid kind "${kind}"`);
else if (typeof rtpParameters !== 'object')
throw new TypeError('missing rtpParameters');
else if (appData && typeof appData !== 'object')
throw new TypeError('if given, appData must be an object');

Expand All @@ -402,26 +410,14 @@ class Transport extends EnhancedEventEmitter
{
// Ensure the device can consume it.
const canConsume = ortc.canReceive(
consumerRemoteParameters.rtpParameters, this._extendedRtpCapabilities);
rtpParameters, this._extendedRtpCapabilities);

if (!canConsume)
throw new UnsupportedError('cannot consume this Producer');

const track = await this._handler.receive(
{
id : consumerRemoteParameters.id,
kind : consumerRemoteParameters.kind,
rtpParameters : consumerRemoteParameters.rtpParameters
});

const track = await this._handler.receive({ id, kind, rtpParameters });
const consumer = new Consumer(
{
id : consumerRemoteParameters.id,
producerId : consumerRemoteParameters.producerId,
track,
rtpParameters : consumerRemoteParameters.rtpParameters,
appData
});
{ id, producerId, track, rtpParameters, appData });

this._consumers.set(consumer.id, consumer);
this._handleConsumer(consumer);
Expand All @@ -434,7 +430,7 @@ class Transport extends EnhancedEventEmitter
{
const handler = this._handler;

handler.on('@connect', (transportLocalParameters, callback, errback) =>
handler.on('@connect', ({ dtlsParameters }, callback, errback) =>
{
if (this._closed)
{
Expand All @@ -443,9 +439,7 @@ class Transport extends EnhancedEventEmitter
return;
}

transportLocalParameters.id = this._id;

this.safeEmit('connect', transportLocalParameters, callback, errback);
this.safeEmit('connect', { dtlsParameters }, callback, errback);
});

handler.on('@connectionstatechange', (connectionState) =>
Expand Down
Loading

0 comments on commit 9c61f48

Please sign in to comment.