提交 c2c3efdf authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

"backup" before removing Peer in worker

上级 1ae26d1c
# BUGS
* `CREATE_PEER` is emitting `newconsumer` but does not emit `newpeer`. Who must do that?
* `then()` of Promises is async so if we create an instance (Peer, Producer, Transport, etc) within the `channel.request().then(....)` and an event happens for that instance immediately, such an event may be lost.
- The solution is creating the instance before calling `channel.request()`.
- DOC: https://stackoverflow.com/questions/36726890/why-are-javascript-promises-asynchronous-when-calling-only-synchronous-functions
+ I must avoid unnecesary `Promise.resolve()`.
- "The callback passed to a Promise constructor is always called synchronously, but the callbacks passed into then are always called asynchronously"
# TODO in mediasoup v2 (server-side)
* If the Producer is paused, should we pass the packet to the BWE?
* It's hard that `join` returns an array of `Peers` since, upon creation of the new `Peer`, it will receive `newconsumer` events which will try to send a `newPeer` mediasoup notification to the client.
* Use "@xxxxx" for private events and `safeEmit("xxxx")` for public events (already done except for Producer and Consumer).
* worker: If a `Producer` is paused and a new `Peer` joins (so a new `Consumer` is generated) such a new `Consumer` does not know that its associated `Producer` is paused. When creating a `Consumer` the `Room` should check `producer->IsPaused()` and call `consumer->setSourcePaused(true)` or similar.
* If the transport of a consumer is closed, and the consumer was "enabled", it should become "disabled" and emit event? I think yes. Enabled should mean "it has a transport". Period.
* If the transport of a consumer is closed, and the consumer was "enabled", it should become "disabled" and emit event (and send `consumerDisabled` notification? I think yes. Enabled should mean "it has a transport and RTP parameters.
- NOTE: We don't need `consumerDisabled` since we already receive `transportClosed` which fires `unhandled` in the client's `Consumer`.
* May have to react on DTLS ALERT CLOSE in the server and make it "really" close the Transport and notify the client. Bufff... I don't like this...
......
......@@ -3,7 +3,6 @@
const Logger = require('./Logger');
const EnhancedEventEmitter = require('./EnhancedEventEmitter');
const errors = require('./errors');
const Transport = require('./Transport');
const logger = new Logger('Consumer');
......@@ -30,28 +29,25 @@ class Consumer extends EnhancedEventEmitter
// - .peer
// - .transport
// - .rtpParameters
// - .associatedProducer
// - .source
this._data = data;
// Channel instance.
this._channel = channel;
// Subscribe to notifications.
this._channel.on(this._internal.consumerId, (event) =>
{
switch (event)
{
case 'close':
{
this.close(); // TODO
// Locally paused flag.
// @type {Boolean}
this._locallyPaused = false;
break;
}
// Remotely paused flag.
// @type {Boolean}
this._remotelyPaused = false;
default:
logger.error('ignoring unknown event "%s"', event);
}
});
// Source paused flag.
// @type {Boolean}
this._sourcePaused = false;
this._handleNotifications();
}
get id()
......@@ -84,24 +80,66 @@ class Consumer extends EnhancedEventEmitter
return this._data.rtpParameters;
}
get associatedProducer()
get source()
{
return this._data.source;
}
/**
* Whether the Consumer is locally paused.
*
* @return {Boolean}
*/
get locallyPaused()
{
return this._locallyPaused;
}
/**
* Whether the Consumer is remotely paused.
*
* @return {Boolean}
*/
get remotelyPaused()
{
return this._remotelyPaused;
}
/**
* Whether the source (Producer) is paused.
*
* @return {Boolean}
*/
get sourcePaused()
{
return this._sourcePaused;
}
/**
* Whether the Consumer is paused.
*
* @return {Boolean}
*/
get paused()
{
return this._data.associatedProducer;
return this._locallyPaused || this._remotelyPaused || this._sourcePaused;
}
/**
* Close the Consumer.
* The app is not supposed to call this method.
*
* @private
*/
close()
{
logger.debug('close()');
if (this._closed)
return;
this._closed = true;
logger.debug('close()');
this.emit('@notify', 'consumerClosed', { id: this.id });
// Remove notification subscriptions.
this._channel.removeAllListeners(this._internal.consumerId);
......@@ -137,125 +175,229 @@ class Consumer extends EnhancedEventEmitter
}
/**
* Assign Transport.
* Start sending media to the remote Consumer.
*
* @param {Transport} transport - Transport instance.
* @param {RTCRtpParameters} rtpParameters - Sending RTP parameters.
*
* @return {Promise} Resolves to this.
*/
setTransport(transport)
enable(transport, rtpParameters)
{
logger.debug('setTransport()');
logger.debug('enable()');
if (this._closed)
return Promise.reject(new errors.InvalidStateError('Consumer closed'));
else if (this.transport)
return Promise.reject(new errors.InvalidStateError('Consumer already enabled'));
// Ensure `transport` is given.
if (!transport)
return Promise.reject(new TypeError('transport not given'));
this._internal.transportId = transport.id;
// Ensure `transport` is a Transport.
if (!(transport instanceof Transport))
const data =
{
return Promise.reject(
new TypeError('transport must be a instance of Transport'));
}
rtpParameters
};
// Ensure `transport` is not closed.
if (transport.closed)
return Promise.reject(new errors.InvalidStateError('transport is closed'));
const previousTransportId = this._internal.transportId;
this._internal.transportId = transport._internal.transportId;
return this._channel.request('consumer.setTransport', this._internal)
return this._channel.request('consumer.enable', this._internal, data)
.then(() =>
{
logger.debug('"consumer.setTransport" request succeeded');
logger.debug('"consumer.enable" request succeeded');
this._data.transport = transport;
this._data.rtpParameters = rtpParameters;
this.emit('transport', transport);
// On Transport closure reset the Consumer.
transport.on('@close', () =>
{
this._internal.transportId = undefined;
});
return this;
})
.catch((error) =>
{
logger.error('"consumer.setTransport" request failed: %s', String(error));
logger.error('"consumer.enable" request failed: %s', String(error));
this._internal.transportId = previousTransportId;
this._internal.transportId = undefined;
throw error;
});
}
/**
* Enable.
* Pauses sending media.
*
* @return {Promise} Resolves to this.
* @param {Any} [appData] - App custom data.
*
* @return {Boolean} true if paused.
*/
enable(options)
pause(appData)
{
logger.debug('enable() [options:%o]', options);
logger.debug('pause()');
if (this._closed)
return Promise.reject(new errors.InvalidStateError('Consumer closed'));
{
logger.error('pause() | Consumer closed');
options = options || {};
return false;
}
else if (this._locallyPaused)
{
return true;
}
if (!this._sourcePaused)
this.emit('@notify', 'consumerPaused', { id: this.id, appData });
this._locallyPaused = true;
// Send Channel request.
return this._channel.request('consumer.disable', this._internal,
{
disabled : false,
emit : Boolean(options.emit)
})
this._channel.request('consumer.pause', this._internal)
.then(() =>
{
logger.debug('"consumer.disable" request succeeded');
return this;
logger.debug('"consumer.pause" request succeeded');
})
.catch((error) =>
{
logger.error('"consumer.disable" request failed: %s', String(error));
throw error;
logger.error('"consumer.pause" request failed: %s', String(error));
});
this.safeEmit('pause', 'local', appData);
// Return true if really paused.
return this.paused;
}
/**
* Disable.
* The remote Consumer was paused.
* Invoked via remote notification.
*
* @return {Promise} Resolves to this.
* @private
*
* @param {Any} [appData] - App custom data.
*/
disable(options)
remotePause(appData)
{
logger.debug('disable() [options:%o]', options);
logger.debug('remotePause()');
if (this._closed)
return Promise.reject(new errors.InvalidStateError('Consumer closed'));
if (this._closed || this._remotelyPaused)
return;
options = options || {};
this._remotelyPaused = true;
// Send Channel request.
return this._channel.request('consumer.disable', this._internal,
{
disabled : true,
emit : Boolean(options.emit)
})
this._channel.request('consumer.pause', this._internal)
.then(() =>
{
logger.debug('"consumer.disable" request succeeded');
return this;
logger.debug('"consumer.pause" request succeeded');
})
.catch((error) =>
{
logger.error('"consumer.disable" request failed: %s', String(error));
throw error;
logger.error('"consumer.pause" request failed: %s', String(error));
});
this.safeEmit('pause', 'remote', appData);
}
/**
* Resumes sending media.
*
* @param {Any} [appData] - App custom data.
*
* @return {Boolean} true if not paused.
*/
resume(appData)
{
logger.debug('resume()');
if (this._closed)
{
logger.error('resume() | Consumer closed');
return false;
}
else if (!this._locallyPaused)
{
return true;
}
if (!this._sourcePaused)
this.emit('@notify', 'consumerResumed', { id: this.id, appData });
this._locallyPaused = false;
if (!this._remotelyPaused)
{
// Send Channel request.
this._channel.request('consumer.resume', this._internal)
.then(() =>
{
logger.debug('"consumer.resume" request succeeded');
})
.catch((error) =>
{
logger.error('"consumer.resume" request failed: %s', String(error));
});
}
this.safeEmit('resume', 'local', appData);
// Return true if not paused.
return !this.paused;
}
/**
* The remote Consumer was resumed.
* Invoked via remote notification.
*
* @private
*
* @param {Any} [appData] - App custom data.
*/
remoteResume(appData)
{
logger.debug('remoteResume()');
if (this._closed || !this._remotelyPaused)
return;
this._remotelyPaused = false;
if (!this._locallyPaused)
{
// Send Channel request.
this._channel.request('consumer.resume', this._internal)
.then(() =>
{
logger.debug('"consumer.resume" request succeeded');
})
.catch((error) =>
{
logger.error('"consumer.resume" request failed: %s', String(error));
});
}
this.safeEmit('resume', 'remote', appData);
}
_handleNotifications()
{
// Subscribe to notifications.
this._channel.on(this._internal.consumerId, (event) =>
{
switch (event)
{
case 'close':
{
this.close();
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
}
}
......
......@@ -14,7 +14,7 @@ const logger = new Logger('Peer');
class Peer extends EnhancedEventEmitter
{
constructor(internal, channel, sandbox)
constructor(internal, data, channel, sandbox)
{
super(logger);
......@@ -31,7 +31,7 @@ class Peer extends EnhancedEventEmitter
// Peer data.
// - .rtpCapabilities
this._data = {};
this._data = data;
// Channel instance.
this._channel = channel;
......@@ -43,6 +43,9 @@ class Peer extends EnhancedEventEmitter
// App data.
this._appData = undefined;
// Whether we are joined so can emit '@notify' events.
this._joined = false;
// Map of Transports indexed by id.
this._transports = new Map();
......@@ -52,55 +55,7 @@ class Peer extends EnhancedEventEmitter
// Map of Consumers indexed by id.
this._consumers = new Map();
// Subscribe to notifications.
this._channel.on(this._internal.peerId, (event, data) =>
{
switch (event)
{
case 'close':
{
this.close(undefined, false);
break;
}
case 'newconsumer':
{
const associatedProducer = this._sandbox.getProducerById(
data.associatedProducerId);
const internal2 =
{
roomId : this._internal.roomId,
peerId : this._internal.peerId,
consumerId : data.consumerId,
transportId : null
};
const data2 =
{
kind : data.kind,
peer : this,
rtpParameters : data.rtpParameters,
associatedProducer : associatedProducer
};
// Create a Consumer instance.
const consumer = new Consumer(internal2, data2, this._channel);
// Store the Consumer instance and remove it when closed.
this._consumers.set(consumer.id, consumer);
consumer.on('@close', () => this._consumers.delete(consumer.id));
this.safeEmit('newconsumer', consumer);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
this._handleNotifications();
}
get id()
......@@ -128,6 +83,11 @@ class Peer extends EnhancedEventEmitter
this._appData = appData;
}
set joined(flag)
{
this._joined = Boolean(joined);
}
get rtpCapabilities()
{
return this._data.rtpCapabilities;
......@@ -332,21 +292,6 @@ class Peer extends EnhancedEventEmitter
});
}
/**
* Set peer RTP capabilities.
*
* @param {RTCRtpCapabilities} [rtpCapabilities]
*/
setRtpCapabilities(rtpCapabilities)
{
logger.debug('setRtpCapabilities()');
if (this._closed)
return Promise.reject(new errors.InvalidStateError('Peer closed'));
this._data.rtpCapabilities = rtpCapabilities;
}
receiveRequest(request)
{
if (this._closed)
......@@ -384,7 +329,7 @@ class Peer extends EnhancedEventEmitter
.then(() => transport)
.catch((error) =>
{
transport.close();
transport.remoteClose();
throw error;
});
......@@ -449,7 +394,7 @@ class Peer extends EnhancedEventEmitter
return producer.receive(rtpParameters, rtpMapping)
.catch((error) =>
{
producer.close();
producer.remoteClose();
throw error;
});
......@@ -531,20 +476,6 @@ class Peer extends EnhancedEventEmitter
break;
}
case 'closeProducer':
{
const { id, appData } = notification;
const producer = this._producers.get(id);
// Just ignore it.
if (!producer)
break;
producer.remoteClose(appData);
break;
}
// TODO: Do it.
case 'updateProducer':
{
......@@ -577,6 +508,20 @@ class Peer extends EnhancedEventEmitter
break;
}
case 'closeProducer':
{
const { id, appData } = notification;
const producer = this._producers.get(id);
// Just ignore it.
if (!producer)
break;
producer.remoteClose(appData);
break;
}
default:
throw new Error(`unknown notification method "${method}"`);
}
......@@ -618,7 +563,7 @@ class Peer extends EnhancedEventEmitter
{
logger.debug('"peer.createTransport" request succeeded');
// Create a Transport instance.
// Create a Transport.
const transport = new Transport(internal, data, this._channel);
// Store the Transport and remove it when closed.
......@@ -666,8 +611,9 @@ class Peer extends EnhancedEventEmitter
const data =
{
kind,
transport
kind : kind,
peer : this,
transport : transport
};
const options =
......@@ -693,7 +639,7 @@ class Peer extends EnhancedEventEmitter
});
// On Transport closure close the Producer.
transport.on('close', () =>
transport.on('@close', () =>
{
if (!producer.closed)
producer.close();
......@@ -711,6 +657,86 @@ class Peer extends EnhancedEventEmitter
throw error;
});
}
_handleNotifications()
{
// Subscribe to notifications.
this._channel.on(this._internal.peerId, (event, data) =>
{
switch (event)
{
case 'close':
{
this.close(undefined, false);
break;
}
// TODO: yes? or get it from data.peerName in 'newconsumer' and retreive
// peers via JS? Then what about `peerClosed`?
case 'newpeer':
{
// TODO
break;
}
case 'newconsumer':
{
// TODO
console.warn('----------------- newconsumer | peer.consumers:%s', this._consumers.size + 1);
// TODO
const source = this._sandbox.getProducerById(data.sourceId);
const consumerInternal =
{
roomId : this._internal.roomId,
peerId : this._internal.peerId,
consumerId : data.consumerId,
transportId : undefined
};
// TODO
const consumerData =
{
kind : data.kind,
peer : this,
rtpParameters : data.rtpParameters,
source : source
};
// Create a Consumer instance.
const consumer =
new Consumer(consumerInternal, consumerData, this._channel);
// Store the Consumer instance and remove it when closed.
this._consumers.set(consumer.id, consumer);
consumer.on('@close', () => this._consumers.delete(consumer.id));
// TODO: Get the Consumer Peer?
if (this._joined)
{
// TODO: Let's see.
// TODO: appData? of the associated Producer/source?
// const foo = consumer.toJson();
const foo = { id: consumer.id, kind: consumer.kind };
// Send a notification.
this._sendNotification('newConsumer', foo);
}
this.safeEmit('newconsumer', consumer);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
}
}
module.exports = Peer;
......@@ -26,9 +26,9 @@ class Producer extends EnhancedEventEmitter
// Producer data.
// - .kind
// - .peer
// - .transport
// - .rtpParameters
// - .rtpMapping
this._data = data;
// Channel instance.
......@@ -42,133 +42,7 @@ class Producer extends EnhancedEventEmitter
// @type {Boolean}
this._remotelyPaused = false;
// Subscribe to notifications.
this._channel.on(this._internal.producerId, (event, data2, buffer) =>
{
switch (event)
{
case 'close':
{
this.close(undefined, false);
break;
}
case 'rtpraw':
{
this.emit('rtpraw', buffer);
break;
}
case 'rtpobject':
{
// Append the binary buffer into `object.payload`.
data2.object.payload = buffer;
this.emit('rtpobject', data2.object);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
// Subscribe to new events.
this.on('newListener', (event) =>
{
switch (event)
{
case 'rtpraw':
{
// Send Channel request.
this._channel.request(
'producer.setRtpRawEvent', this._internal, { enabled: true })
.then(() =>
{
logger.debug('"producer.setRtpRawEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"producer.setRtpRawEvent" request failed: %s', String(error));
});
break;
}
case 'rtpobject':
{
// Send Channel request.
this._channel.request(
'producer.setRtpObjectEvent', this._internal, { enabled: true })
.then(() =>
{
logger.debug('"producer.setRtpObjectEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"producer.setRtpObjectEvent" request failed: %s', String(error));
});
break;
}
}
});
// Subscribe to events removal.
this.on('removeListener', (event) =>
{
switch (event)
{
case 'rtpraw':
{
// Ignore if there are other remaining listeners.
if (this.listenerCount('rtpraw'))
return;
// Send Channel request.
this._channel.request(
'producer.setRtpRawEvent', this._internal, { enabled: false })
.then(() =>
{
logger.debug('"producer.setRtpRawEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"producer.setRtpRawEvent" request failed: %s', String(error));
});
break;
}
case 'rtpobject':
{
// Ignore if there are other remaining listeners.
if (this.listenerCount('rtpobject'))
return;
// Send Channel request.
this._channel.request(
'producer.setRtpObjectEvent', this._internal, { enabled: false })
.then(() =>
{
logger.debug('"producer.setRtpObjectEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"producer.setRtpObjectEvent" request failed: %s', String(error));
});
break;
}
}
});
this._handleNotifications();
}
get id()
......@@ -186,6 +60,11 @@ class Producer extends EnhancedEventEmitter
return this._data.kind;
}
get peer()
{
return this._data.peer;
}
get transport()
{
return this._data.transport;
......@@ -196,11 +75,6 @@ class Producer extends EnhancedEventEmitter
return this._data.rtpParameters;
}
get rtpMapping()
{
return this._data.rtpMapping;
}
/**
* Whether the Producer is locally paused.
*
......@@ -246,8 +120,7 @@ class Producer extends EnhancedEventEmitter
this._closed = true;
this.emit(
'@notify', 'producerClosed', { id: this.id, appData });
this.emit('@notify', 'producerClosed', { id: this.id, appData });
this.emit('@close');
this.safeEmit('close', 'local', appData);
......@@ -354,7 +227,6 @@ class Producer extends EnhancedEventEmitter
logger.debug('"producer.receive" request succeeded');
this._data.rtpParameters = rtpParameters;
this._data.rtpMapping = rtpMapping;
return this;
})
......@@ -388,8 +260,7 @@ class Producer extends EnhancedEventEmitter
return true;
}
this.emit(
'@notify', 'producerPaused', { id: this.id, appData });
this.emit('@notify', 'producerPaused', { id: this.id, appData });
this._locallyPaused = true;
......@@ -463,8 +334,7 @@ class Producer extends EnhancedEventEmitter
return true;
}
this.emit(
'@notify', 'producerResumed', { id: this.id, appData });
this.emit('@notify', 'producerResumed', { id: this.id, appData });
this._locallyPaused = false;
......@@ -521,6 +391,137 @@ class Producer extends EnhancedEventEmitter
this.safeEmit('resume', 'remote', appData);
}
_handleNotifications()
{
// Subscribe to notifications.
this._channel.on(this._internal.producerId, (event, data, buffer) =>
{
switch (event)
{
case 'close':
{
this.close(undefined, false);
break;
}
case 'rtpraw':
{
this.emit('rtpraw', buffer);
break;
}
case 'rtpobject':
{
// Append the binary buffer into `object.payload`.
data.object.payload = buffer;
this.emit('rtpobject', data.object);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
// Subscribe to new events.
this.on('newListener', (event) =>
{
switch (event)
{
case 'rtpraw':
{
// Send Channel request.
this._channel.request(
'producer.setRtpRawEvent', this._internal, { enabled: true })
.then(() =>
{
logger.debug('"producer.setRtpRawEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"producer.setRtpRawEvent" request failed: %s', String(error));
});
break;
}
case 'rtpobject':
{
// Send Channel request.
this._channel.request(
'producer.setRtpObjectEvent', this._internal, { enabled: true })
.then(() =>
{
logger.debug('"producer.setRtpObjectEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"producer.setRtpObjectEvent" request failed: %s', String(error));
});
break;
}
}
});
// Subscribe to events removal.
this.on('removeListener', (event) =>
{
switch (event)
{
case 'rtpraw':
{
// Ignore if there are other remaining listeners.
if (this.listenerCount('rtpraw'))
return;
// Send Channel request.
this._channel.request(
'producer.setRtpRawEvent', this._internal, { enabled: false })
.then(() =>
{
logger.debug('"producer.setRtpRawEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"producer.setRtpRawEvent" request failed: %s', String(error));
});
break;
}
case 'rtpobject':
{
// Ignore if there are other remaining listeners.
if (this.listenerCount('rtpobject'))
return;
// Send Channel request.
this._channel.request(
'producer.setRtpObjectEvent', this._internal, { enabled: false })
.then(() =>
{
logger.debug('"producer.setRtpObjectEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"producer.setRtpObjectEvent" request failed: %s', String(error));
});
break;
}
}
});
}
}
module.exports = Producer;
......@@ -38,117 +38,7 @@ class Room extends EnhancedEventEmitter
// Map of Producer instances indexed by `producerId`.
this._producers = new Map();
// Subscribe to notifications.
this._channel.on(this._internal.roomId, (event, data2) =>
{
switch (event)
{
case 'close':
{
this.close(undefined, false);
break;
}
case 'audiolevels':
{
const entries = data2.entries;
const data3 = [];
for (const entry of entries)
{
const producerId = entry[0];
const audioLevel = entry[1];
const producer = this._producers.get(producerId);
if (!producer)
continue;
const peer = producer.peer;
if (!peer)
continue;
data3.push({ peer, producer, audioLevel });
}
const orderedEntries = data3.sort((a, b) =>
{
if (a.audioLevel > b.audioLevel)
return -1;
else if (a.audioLevel < b.audioLevel)
return 1;
else
return 0;
});
this.safeEmit('audiolevels', orderedEntries);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
// Subscribe to new events.
this.on('newListener', (event) =>
{
switch (event)
{
case 'audiolevels':
{
// Ignore if there are listeners already.
if (this.listenerCount('audiolevels'))
return;
// Send Channel request.
this._channel.request(
'room.setAudioLevelsEvent', this._internal, { enabled: true })
.then(() =>
{
logger.debug('"room.setAudioLevelsEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"room.setAudioLevelsEvent" request failed: %s', String(error));
});
break;
}
}
});
// Subscribe to events removal.
this.on('removeListener', (event) =>
{
switch (event)
{
case 'audiolevels':
{
// Ignore if there are other remaining listeners.
if (this.listenerCount('audiolevels'))
return;
// Send Channel request.
this._channel.request(
'room.setAudioLevelsEvent', this._internal, { enabled: false })
.then(() =>
{
logger.debug('"room.setAudioLevelsEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"room.setAudioLevelsEvent" request failed: %s', String(error));
});
break;
}
}
});
this._handleNotifications();
}
get id()
......@@ -292,39 +182,20 @@ class Room extends EnhancedEventEmitter
{
const { peerName, rtpCapabilities, appData } = request;
return Promise.resolve()
.then(() =>
{
const peer = this._createPeer(peerName);
peer.appData = appData;
try
{
// Ensure that the given capabilties are a subset of the room
// capabilities.
// NOTE: This may throw.
ortc.assertCapabilitiesSubset(
rtpCapabilities, this.rtpCapabilities);
peer.setRtpCapabilities(rtpCapabilities);
}
catch (error)
{
peer.close(undefined, false);
throw error;
}
})
.then(() =>
{
const response =
{
peers : [] // TODO
};
return response;
});
// Ensure that the given capabilties are a subset of the Room
// capabilities.
// NOTE: This may throw.
ortc.assertCapabilitiesSubset(
rtpCapabilities, this.rtpCapabilities);
const peer = this._createPeer(peerName, rtpCapabilities, appData);
const response =
{
peers : [] // TODO: remove...
};
return response;
}
default:
......@@ -339,21 +210,14 @@ class Room extends EnhancedEventEmitter
});
}
/**
* Create a Peer instance.
*
* @param {String} peerName - Peer identificator.
*
* @return {Peer}
*/
_createPeer(peerName)
_createPeer(peerName, rtpCapabilities, appData)
{
logger.debug('_createPeer() [peerName:"%s]', peerName);
if (!peerName || typeof peerName !== 'string')
throw new TypeError('peerName must be a string');
return Promise.reject(new TypeError('peerName must be a string'));
else if (this._peers.has(peerName))
throw new Error(`Peer already exists [peerName:"${peerName}"]`);
return Promise.reject(new Error(`duplicated Peer [peerName:"${peerName}"]`));
const internal =
{
......@@ -362,7 +226,11 @@ class Room extends EnhancedEventEmitter
peerName : peerName
};
// TODO: yes?
const data =
{
rtpCapabilities
};
const sandbox =
{
getProducerById : (producerId) =>
......@@ -379,18 +247,20 @@ class Room extends EnhancedEventEmitter
};
// Create a Peer.
const peer = new Peer(internal, this._channel, sandbox);
const peer = new Peer(internal, data, this._channel, sandbox);
peer.appData = appData;
// Store the Peer and remove it when closed.
this._peers.set(peer.name, peer);
peer.on('close', () => this._peers.delete(peer.name));
peer.on('@close', () => this._peers.delete(peer.name));
// Listen for new Producers so we can associate new Consumers.
peer.on('@newproducer', (producer) =>
{
// Store the Producer and remove it when closed.
this._producers.set(producer.id, producer);
producer.on('close', () => this._producers.delete(producer.id));
producer.on('@close', () => this._producers.delete(producer.id));
});
this._channel.request('room.createPeer', internal)
......@@ -398,17 +268,131 @@ class Room extends EnhancedEventEmitter
{
logger.debug('"room.createPeer" request succeeded');
this.safeEmit('newpeer', peer);
peer.joined = true;
})
.catch((error) =>
{
logger.error('"room.createPeer" request failed: %s', String(error));
peer.close(undefined, false);
peer.close();
});
this.safeEmit('newpeer', peer);
return peer;
}
_handleNotifications()
{
// Subscribe to notifications.
this._channel.on(this._internal.roomId, (event, data) =>
{
switch (event)
{
case 'close':
{
this.close(undefined, false);
break;
}
case 'audiolevels':
{
const entries = data.entries;
const entries2 = [];
for (const entry of entries)
{
const producerId = entry[0];
const audioLevel = entry[1];
const producer = this._producers.get(producerId);
if (!producer)
continue;
const peer = producer.peer;
entries2.push({ peer, producer, audioLevel });
}
const orderedEntries = entries2.sort((a, b) =>
{
if (a.audioLevel > b.audioLevel)
return -1;
else if (a.audioLevel < b.audioLevel)
return 1;
else
return 0;
});
this.safeEmit('audiolevels', orderedEntries);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
// Subscribe to new events.
this.on('newListener', (event) =>
{
switch (event)
{
case 'audiolevels':
{
// Ignore if there are listeners already.
if (this.listenerCount('audiolevels'))
return;
// Send Channel request.
this._channel.request(
'room.setAudioLevelsEvent', this._internal, { enabled: true })
.then(() =>
{
logger.debug('"room.setAudioLevelsEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"room.setAudioLevelsEvent" request failed: %s', String(error));
});
break;
}
}
});
// Subscribe to events removal.
this.on('removeListener', (event) =>
{
switch (event)
{
case 'audiolevels':
{
// Ignore if there are other remaining listeners.
if (this.listenerCount('audiolevels'))
return;
// Send Channel request.
this._channel.request(
'room.setAudioLevelsEvent', this._internal, { enabled: false })
.then(() =>
{
logger.debug('"room.setAudioLevelsEvent" request succeeded');
})
.catch((error) =>
{
logger.error(
'"room.setAudioLevelsEvent" request failed: %s', String(error));
});
break;
}
}
});
}
}
module.exports = Room;
......@@ -65,55 +65,7 @@ class Transport extends EnhancedEventEmitter
// App data.
this._appData = undefined;
// Subscribe to notifications.
this._channel.on(this._internal.transportId, (event, data2) =>
{
switch (event)
{
case 'close':
{
this.close(undefined, false);
break;
}
case 'iceselectedtuplechange':
{
this._data.iceSelectedTuple = data2.iceSelectedTuple;
// Emit it to the app.
this.safeEmit('iceselectedtuplechange', data2.iceSelectedTuple);
break;
}
case 'icestatechange':
{
this._data.iceState = data2.iceState;
// Emit it to the app.
this.safeEmit('icestatechange', data2.iceState);
break;
}
case 'dtlsstatechange':
{
this._data.dtlsState = data2.dtlsState;
if (data2.dtlsState === 'connected')
this._data.dtlsRemoteCert = data2.dtlsRemoteCert;
// Emit it to the app.
this.safeEmit('dtlsstatechange', data2.dtlsState);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
this._handleNotifications();
}
get id()
......@@ -377,6 +329,59 @@ class Transport extends EnhancedEventEmitter
throw error;
});
}
_handleNotifications()
{
// Subscribe to notifications.
this._channel.on(this._internal.transportId, (event, data) =>
{
switch (event)
{
case 'close':
{
this.close(undefined, false);
break;
}
case 'iceselectedtuplechange':
{
this._data.iceSelectedTuple = data.iceSelectedTuple;
// Emit it to the app.
this.safeEmit('iceselectedtuplechange', data.iceSelectedTuple);
break;
}
case 'icestatechange':
{
this._data.iceState = data.iceState;
// Emit it to the app.
this.safeEmit('icestatechange', data.iceState);
break;
}
case 'dtlsstatechange':
{
this._data.dtlsState = data.dtlsState;
if (data.dtlsState === 'connected')
this._data.dtlsRemoteCert = data.dtlsRemoteCert;
// Emit it to the app.
this.safeEmit('dtlsstatechange', data.dtlsState);
break;
}
default:
logger.error('ignoring unknown event "%s"', event);
}
});
}
}
module.exports = Transport;
......@@ -18,9 +18,14 @@ namespace Channel
void EmitWithBinary(
uint32_t targetId,
const std::string& event,
Json::Value& data,
const uint8_t* binaryData,
size_t binaryLen);
void EmitWithBinary(
uint32_t targetId,
const std::string& event,
const uint8_t* binaryData,
size_t binaryLen,
Json::Value& data);
public:
// Passed by argument.
......
......@@ -42,8 +42,9 @@ namespace Channel
PRODUCER_SET_RTP_RAW_EVENT,
PRODUCER_SET_RTP_OBJECT_EVENT,
CONSUMER_DUMP,
CONSUMER_SET_TRANSPORT,
CONSUMER_DISABLE
CONSUMER_ENABLE,
CONSUMER_PAUSE,
CONSUMER_RESUME
};
private:
......
......@@ -71,7 +71,8 @@ namespace RTC
RTC::RtpParameters* rtpParameters{ nullptr };
RTC::RtpStreamSend* rtpStream{ nullptr };
// Others.
std::unordered_set<uint8_t> supportedPayloadTypes;
bool paused{ false };
std::unordered_set<uint8_t> supportedPayloadTypes; // TODO: NO
// Timestamp when last RTCP was sent.
uint64_t lastRtcpSentTime{ 0 };
uint16_t maxRtcpInterval{ 0 };
......@@ -96,8 +97,11 @@ namespace RTC
{
if (this->transport == transport)
this->transport = nullptr;
// TODO: More. Remove rtpParameters.
}
// TODO: What for?
inline RTC::RtpParameters* Consumer::GetParameters() const
{
return this->rtpParameters;
......
......@@ -64,6 +64,7 @@ namespace RTC
RTC::Transport* GetTransport() const;
void RemoveTransport(RTC::Transport* transport);
RTC::RtpParameters* GetParameters() const;
bool IsPaused() const;
void ReceiveRtpPacket(RTC::RtpPacket* packet);
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now);
......@@ -95,11 +96,11 @@ namespace RTC
// Allocated by this.
RTC::RtpParameters* rtpParameters{ nullptr };
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
std::map<uint32_t, RTC::RtpStreamRecv*> rtxStreamMap;
bool paused{ false };
std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxStreams;
// Others.
struct RtpMapping rtpMapping;
struct KnownHeaderExtensions knownHeaderExtensions;
bool paused{ false };
bool rtpRawEventEnabled{ false };
bool rtpObjectEventEnabled{ false };
// Timestamp when last RTCP was sent.
......@@ -130,6 +131,11 @@ namespace RTC
return this->rtpParameters;
}
inline bool Producer::IsPaused() const
{
return this->paused;
}
inline void Producer::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report)
{
auto it = this->rtpStreams.find(report->GetSsrc());
......
......@@ -43,6 +43,7 @@ namespace RTC
private:
RTC::Peer* GetPeerFromRequest(Channel::Request* request, uint32_t* peerId = nullptr) const;
void AddConsumerForProducer(RTC::Peer* consumerPeer, const RTC::Producer* producer);
/* Pure virtual methods inherited from RTC::Peer::Listener. */
public:
......
......@@ -48,12 +48,34 @@ namespace Channel
void Notifier::EmitWithBinary(
uint32_t targetId,
const std::string& event,
Json::Value& data,
const uint8_t* binaryData,
size_t binaryLen)
{
MS_TRACE();
static const Json::StaticString JsonStringTargetId{ "targetId" };
static const Json::StaticString JsonStringEvent{ "event" };
static const Json::StaticString JsonStringBinary{ "binary" };
Json::Value json(Json::objectValue);
json[JsonStringTargetId] = Json::UInt{ targetId };
json[JsonStringEvent] = event;
json[JsonStringBinary] = true;
this->channel->Send(json);
this->channel->SendBinary(binaryData, binaryLen);
}
void Notifier::EmitWithBinary(
uint32_t targetId,
const std::string& event,
const uint8_t* binaryData,
size_t binaryLen,
Json::Value& data)
{
MS_TRACE();
static const Json::StaticString JsonStringTargetId{ "targetId" };
static const Json::StaticString JsonStringEvent{ "event" };
static const Json::StaticString JsonStringData{ "data" };
......@@ -63,8 +85,8 @@ namespace Channel
json[JsonStringTargetId] = Json::UInt{ targetId };
json[JsonStringEvent] = event;
json[JsonStringData] = data;
json[JsonStringBinary] = true;
json[JsonStringData] = data;
this->channel->Send(json);
this->channel->SendBinary(binaryData, binaryLen);
......
......@@ -37,8 +37,9 @@ namespace Channel
{ "producer.setRtpRawEvent", Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT },
{ "producer.setRtpObjectEvent", Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT },
{ "consumer.dump", Request::MethodId::CONSUMER_DUMP },
{ "consumer.setTransport", Request::MethodId::CONSUMER_SET_TRANSPORT },
{ "consumer.disable", Request::MethodId::CONSUMER_DISABLE }
{ "consumer.enable", Request::MethodId::CONSUMER_ENABLE },
{ "consumer.pause", Request::MethodId::CONSUMER_PAUSE },
{ "consumer.resume", Request::MethodId::CONSUMER_RESUME }
};
// clang-format on
......
......@@ -230,8 +230,9 @@ void Loop::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::Req
case Channel::Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT:
case Channel::Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT:
case Channel::Request::MethodId::CONSUMER_DUMP:
case Channel::Request::MethodId::CONSUMER_SET_TRANSPORT:
case Channel::Request::MethodId::CONSUMER_DISABLE:
case Channel::Request::MethodId::CONSUMER_ENABLE:
case Channel::Request::MethodId::CONSUMER_PAUSE:
case Channel::Request::MethodId::CONSUMER_RESUME:
{
RTC::Room* room;
......
......@@ -41,12 +41,7 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
Json::Value eventData(Json::objectValue);
eventData[JsonStringClass] = "Consumer";
this->notifier->Emit(this->consumerId, "close", eventData);
this->notifier->Emit(this->consumerId, "close");
// Notify the listener.
this->listener->OnConsumerClosed(this);
......@@ -109,6 +104,29 @@ namespace RTC
break;
}
case Channel::Request::MethodId::CONSUMER_PAUSE:
{
this->paused = true;
request->Accept();
break;
}
case Channel::Request::MethodId::CONSUMER_RESUME:
{
bool wasPaused = this->paused;
this->paused = false;
request->Accept();
if (wasPaused)
this->listener->OnConsumerFullFrameRequired(this);
break;
}
default:
{
MS_ERROR("unknown method");
......@@ -122,10 +140,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" };
static const Json::StaticString JsonStringEnabled{ "enabled" };
MS_ASSERT(rtpParameters, "no RTP parameters given");
// Free the previous RTP parameters.
......
......@@ -38,10 +38,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
Json::Value eventData(Json::objectValue);
// Close all the Producers.
for (auto it = this->producers.begin(); it != this->producers.end();)
{
......@@ -72,8 +68,7 @@ namespace RTC
}
// Notify.
eventData[JsonStringClass] = "Peer";
this->notifier->Emit(this->peerId, "close", eventData);
this->notifier->Emit(this->peerId, "close");
// Notify the listener.
this->listener->OnPeerClosed(this);
......@@ -346,83 +341,58 @@ namespace RTC
break;
}
case Channel::Request::MethodId::CONSUMER_DUMP:
{
RTC::Consumer* consumer;
try
{
consumer = GetConsumerFromRequest(request);
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
return;
}
if (consumer == nullptr)
{
request->Reject("Consumer does not exist");
return;
}
consumer->HandleRequest(request);
break;
}
case Channel::Request::MethodId::CONSUMER_SET_TRANSPORT:
case Channel::Request::MethodId::CONSUMER_ENABLE:
{
RTC::Consumer* consumer;
// RTC::Consumer* consumer;
try
{
consumer = GetConsumerFromRequest(request);
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
// try
// {
// consumer = GetConsumerFromRequest(request);
// }
// catch (const MediaSoupError& error)
// {
// request->Reject(error.what());
return;
}
// return;
// }
if (consumer == nullptr)
{
request->Reject("Consumer does not exist");
// if (consumer == nullptr)
// {
// request->Reject("Consumer does not exist");
return;
}
// return;
// }
RTC::Transport* transport;
// RTC::Transport* transport;
try
{
transport = GetTransportFromRequest(request);
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
// try
// {
// transport = GetTransportFromRequest(request);
// }
// catch (const MediaSoupError& error)
// {
// request->Reject(error.what());
return;
}
// return;
// }
if (transport == nullptr)
{
request->Reject("Transport does not exist");
// if (transport == nullptr)
// {
// request->Reject("Transport does not exist");
return;
}
// return;
// }
consumer->SetTransport(transport);
// consumer->SetTransport(transport);
request->Accept();
break;
}
case Channel::Request::MethodId::CONSUMER_DISABLE:
case Channel::Request::MethodId::CONSUMER_DUMP:
case Channel::Request::MethodId::CONSUMER_PAUSE:
case Channel::Request::MethodId::CONSUMER_RESUME:
{
RTC::Consumer* consumer;
......@@ -463,7 +433,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringConsumerId{ "consumerId" };
static const Json::StaticString JsonStringKind{ "kind" };
static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" };
......@@ -483,7 +452,6 @@ namespace RTC
// Notify.
Json::Value eventData = consumer->ToJson();
eventData[JsonStringClass] = "Peer";
eventData[JsonStringConsumerId] = Json::UInt{ consumer->consumerId };
eventData[JsonStringKind] = RTC::Media::GetJsonString(consumer->kind);
eventData[JsonStringRtpParameters] = consumer->GetParameters()->ToJson();
......
......@@ -38,13 +38,8 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
Json::Value eventData(Json::objectValue);
// Notify.
eventData[JsonStringClass] = "Producer";
this->notifier->Emit(this->producerId, "close", eventData);
this->notifier->Emit(this->producerId, "close");
// Notify the listener.
this->listener->OnProducerClosed(this);
......@@ -286,7 +281,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringObject{ "object" };
static const Json::StaticString JsonStringPayloadType{ "payloadType" };
static const Json::StaticString JsonStringMarker{ "marker" };
......@@ -307,9 +301,9 @@ namespace RTC
if (!rtpStream->ReceivePacket(packet))
return;
}
else if (this->rtxStreamMap.find(ssrc) != this->rtxStreamMap.end())
else if (this->mapRtxStreams.find(ssrc) != this->mapRtxStreams.end())
{
rtpStream = this->rtxStreamMap[ssrc];
rtpStream = this->mapRtxStreams[ssrc];
// Process the packet.
if (!rtpStream->ReceiveRtxPacket(packet))
......@@ -329,12 +323,8 @@ namespace RTC
// Emit "rtpraw" if enabled.
if (this->rtpRawEventEnabled)
{
Json::Value eventData(Json::objectValue);
eventData[JsonStringClass] = "Producer";
this->notifier->EmitWithBinary(
this->producerId, "rtpraw", eventData, packet->GetData(), packet->GetSize());
this->producerId, "rtpraw", packet->GetData(), packet->GetSize());
}
// Emit "rtpobject" is enabled.
......@@ -343,8 +333,6 @@ namespace RTC
Json::Value eventData(Json::objectValue);
Json::Value jsonObject(Json::objectValue);
eventData[JsonStringClass] = "Producer";
jsonObject[JsonStringPayloadType] = Json::UInt{ packet->GetPayloadType() };
jsonObject[JsonStringMarker] = packet->HasMarker();
jsonObject[JsonStringSequenceNumber] = Json::UInt{ packet->GetSequenceNumber() };
......@@ -354,7 +342,7 @@ namespace RTC
eventData[JsonStringObject] = jsonObject;
this->notifier->EmitWithBinary(
this->producerId, "rtpobject", eventData, packet->GetPayload(), packet->GetPayloadLength());
this->producerId, "rtpobject", packet->GetPayload(), packet->GetPayloadLength(), eventData);
}
// Apply the Producer RTP mapping before dispatching the packet to the Room.
......@@ -562,14 +550,14 @@ namespace RTC
// Check rtx capabilities.
if (encoding.hasRtx && encoding.rtx.ssrc != 0u)
{
if (this->rtxStreamMap.find(encoding.rtx.ssrc) != this->rtxStreamMap.end())
if (this->mapRtxStreams.find(encoding.rtx.ssrc) != this->mapRtxStreams.end())
return;
auto& codec = this->rtpParameters->GetRtxCodecForEncoding(encoding);
auto rtpStream = this->rtpStreams[ssrc];
rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc);
this->rtxStreamMap[encoding.rtx.ssrc] = rtpStream;
this->mapRtxStreams[encoding.rtx.ssrc] = rtpStream;
}
}
......
......@@ -36,10 +36,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
Json::Value eventData(Json::objectValue);
// Close all the Peers.
// NOTE: Upon Peer closure the onPeerClosed() method is called which
// removes it from the map, so this is the safe way to iterate the map
......@@ -56,8 +52,7 @@ namespace RTC
this->audioLevelsTimer->Destroy();
// Notify.
eventData[JsonStringClass] = "Room";
this->notifier->Emit(this->roomId, "close", eventData);
this->notifier->Emit(this->roomId, "close");
// Notify the listener.
this->listener->OnRoomClosed(this);
......@@ -207,6 +202,27 @@ namespace RTC
request->Accept();
for (auto& kv : this->peers)
{
auto* otherPeer = kv.second;
// Skip new Peer.
if (otherPeer == peer)
continue;
// Get all the ready Producers of the others Peers in the Room and create
// Consumers for this new Peer.
for (auto producer : otherPeer->GetProducers())
{
// Skip if the Producer has not RTP parameters.
if (producer->GetParameters() == nullptr)
continue;
AddConsumerForProducer(peer, producer);
}
}
break;
}
......@@ -259,8 +275,9 @@ namespace RTC
case Channel::Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT:
case Channel::Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT:
case Channel::Request::MethodId::CONSUMER_DUMP:
case Channel::Request::MethodId::CONSUMER_SET_TRANSPORT:
case Channel::Request::MethodId::CONSUMER_DISABLE:
case Channel::Request::MethodId::CONSUMER_ENABLE:
case Channel::Request::MethodId::CONSUMER_PAUSE:
case Channel::Request::MethodId::CONSUMER_RESUME:
{
RTC::Peer* peer;
......@@ -323,6 +340,30 @@ namespace RTC
return nullptr;
}
inline void Room::AddConsumerForProducer(RTC::Peer* consumerPeer, const RTC::Producer* producer)
{
MS_TRACE();
MS_ASSERT(producer->GetParameters(), "Producer has no parameters");
uint32_t consumerId = Utils::Crypto::GetRandomUInt(10000000, 99999999);
auto consumer = new RTC::Consumer(consumerPeer, this->notifier, consumerId, producer->kind);
// Store into the maps.
this->mapProducerConsumers[producer].insert(consumer);
this->mapConsumerProducer[consumer] = producer;
// TODO: NO
auto rtpParameters = producer->GetParameters();
// TODO: Maybe, not sure
auto associatedProducerId = producer->producerId;
// TODO: Check producer->IsPaused() and, if so, set consumer->SetSourcePaused(true).
// Attach the Consumer to the peer.
consumerPeer->AddConsumer(consumer, rtpParameters, associatedProducerId);
}
void Room::OnPeerClosed(const RTC::Peer* peer)
{
MS_TRACE();
......@@ -358,14 +399,14 @@ namespace RTC
{
MS_TRACE();
// Iterate all the peers but this one and create a Consumer associated to this
// Producer for each Peer.
MS_ASSERT(producer->GetParameters(), "Producer has no parameters");
MS_ASSERT(
this->mapProducerConsumers.find(producer) == this->mapProducerConsumers.end(),
"Producer already in mapProducerConsumers");
// Iterate all the peers but this one and create a Consumer associated to this
// Producer for each Peer.
// Ensure the entry will exist even with an empty array.
this->mapProducerConsumers[producer];
......@@ -377,18 +418,7 @@ namespace RTC
if (consumerPeer == peer)
continue;
uint32_t consumerId = Utils::Crypto::GetRandomUInt(10000000, 99999999);
auto consumer = new RTC::Consumer(consumerPeer, this->notifier, consumerId, producer->kind);
// Store into the maps.
this->mapProducerConsumers[producer].insert(consumer);
this->mapConsumerProducer[consumer] = producer;
auto rtpParameters = producer->GetParameters();
auto associatedProducerId = producer->producerId;
// Attach the Consumer to the peer.
consumerPeer->AddConsumer(consumer, rtpParameters, associatedProducerId);
AddConsumerForProducer(consumerPeer, producer);
}
}
......@@ -523,7 +553,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringEntries{ "entries" };
// Audio levels timer.
......@@ -558,7 +587,6 @@ namespace RTC
// Emit event.
Json::Value eventData(Json::objectValue);
eventData[JsonStringClass] = "Room";
eventData[JsonStringEntries] = Json::arrayValue;
for (auto& kv : mapProducerAudioLevel)
......
......@@ -211,10 +211,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
Json::Value eventData(Json::objectValue);
if (this->srtpRecvSession != nullptr)
this->srtpRecvSession->Destroy();
......@@ -238,8 +234,7 @@ namespace RTC
this->selectedTuple = nullptr;
// Notify.
eventData[JsonStringClass] = "Transport";
this->notifier->Emit(this->transportId, "close", eventData);
this->notifier->Emit(this->transportId, "close");
// If this was allocated (it did not throw in the constructor) notify the
// listener and delete it.
......@@ -999,7 +994,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringIceSelectedTuple{ "iceSelectedTuple" };
Json::Value eventData(Json::objectValue);
......@@ -1015,7 +1009,6 @@ namespace RTC
this->selectedTuple = tuple;
// Notify.
eventData[JsonStringClass] = "Transport";
eventData[JsonStringIceSelectedTuple] = tuple->ToJson();
this->notifier->Emit(this->transportId, "iceselectedtuplechange", eventData);
}
......@@ -1024,7 +1017,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringIceState{ "iceState" };
static const Json::StaticString JsonStringConnected{ "connected" };
......@@ -1033,7 +1025,6 @@ namespace RTC
MS_DEBUG_TAG(ice, "ICE connected");
// Notify.
eventData[JsonStringClass] = "Transport";
eventData[JsonStringIceState] = JsonStringConnected;
this->notifier->Emit(this->transportId, "icestatechange", eventData);
......@@ -1045,7 +1036,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringIceState{ "iceState" };
static const Json::StaticString JsonStringCompleted{ "completed" };
......@@ -1054,7 +1044,6 @@ namespace RTC
MS_DEBUG_TAG(ice, "ICE completed");
// Notify.
eventData[JsonStringClass] = "Transport";
eventData[JsonStringIceState] = JsonStringCompleted;
this->notifier->Emit(this->transportId, "icestatechange", eventData);
......@@ -1066,7 +1055,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringIceState{ "iceState" };
static const Json::StaticString JsonStringDisconnected{ "disconnected" };
......@@ -1078,7 +1066,6 @@ namespace RTC
this->selectedTuple = nullptr;
// Notify.
eventData[JsonStringClass] = "Transport";
eventData[JsonStringIceState] = JsonStringDisconnected;
this->notifier->Emit(this->transportId, "icestatechange", eventData);
......@@ -1090,7 +1077,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringDtlsState{ "dtlsState" };
static const Json::StaticString JsonStringConnecting{ "connecting" };
......@@ -1099,7 +1085,6 @@ namespace RTC
MS_DEBUG_TAG(dtls, "DTLS connecting");
// Notify.
eventData[JsonStringClass] = "Transport";
eventData[JsonStringDtlsState] = JsonStringConnecting;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
}
......@@ -1115,7 +1100,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringDtlsState{ "dtlsState" };
static const Json::StaticString JsonStringConnected{ "connected" };
static const Json::StaticString JsonStringDtlsRemoteCert{ "dtlsRemoteCert" };
......@@ -1160,7 +1144,6 @@ namespace RTC
}
// Notify.
eventData[JsonStringClass] = "Transport";
eventData[JsonStringDtlsState] = JsonStringConnected;
eventData[JsonStringDtlsRemoteCert] = remoteCert;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
......@@ -1172,7 +1155,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringDtlsState{ "dtlsState" };
static const Json::StaticString JsonStringFailed{ "failed" };
......@@ -1181,7 +1163,6 @@ namespace RTC
MS_WARN_TAG(dtls, "DTLS failed");
// Notify.
eventData[JsonStringClass] = "Transport";
eventData[JsonStringDtlsState] = JsonStringFailed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
......@@ -1193,7 +1174,6 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString JsonStringClass{ "class" };
static const Json::StaticString JsonStringDtlsState{ "dtlsState" };
static const Json::StaticString JsonStringClosed{ "closed" };
......@@ -1202,7 +1182,6 @@ namespace RTC
MS_DEBUG_TAG(dtls, "DTLS remotely closed");
// Notify.
eventData[JsonStringClass] = "Transport";
eventData[JsonStringDtlsState] = JsonStringClosed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论