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

producer.pause/resume() works!

上级 93eea21f
# TODO in mediasoup v2 (server-side)
* Before the Producer dispacthes a RTP packet to the room (via `onRtpPacket()`, we call `ApplyRtpMapping(packet)` to mangle RTP payload type and header extension ids. However, we are breaking the previously established Id mapping in the `RtpPacket` (generated via `packet->AddExtensionMapping()`). The fact is that, most probably, we should set the mapping once the `RtpStreamRecv` processes the packet. The problem is that, currently, the information to set the mapping is hold into the `RtpStreamRecv`...
* May be problems with `RtpPacket` RTX stuff due to RTP mapping in Producers? Sure, the decoded RTX packet must also be mapped (once decoded).
* If the Producer is paused, should we pass the packet to the BWE?
* Use "@xxxxx" for private events and `safeEmit("xxxx")` for public events (already done except for Producer and Consumer).
......
......@@ -403,6 +403,27 @@ class Peer extends EnhancedEventEmitter
});
}
case 'restartTransport':
{
const { id } = request;
const transport = this._transports.get(id);
if (!transport)
throw new Error('Transport not found');
return transport.changeUfragPwd()
.then(() =>
{
const response =
{
iceParameters : transport.iceLocalParameters
};
return response;
});
}
case 'createProducer':
{
const {
......@@ -415,9 +436,8 @@ class Peer extends EnhancedEventEmitter
} = request;
// NOTE: This may throw.
const { adaptedRtpParameters, rtpMapping } =
this._sandbox.adaptProducerRtpParametersToRtpCapabilities(
rtpParameters);
const rtpMapping =
this._sandbox.getProducerRtpParametersMapping(rtpParameters);
return this._createProducer(id, kind, transportId)
.then((producer) =>
......@@ -427,8 +447,7 @@ class Peer extends EnhancedEventEmitter
if (paused)
producer.remotePause();
return producer.receive(
rtpParameters, adaptedRtpParameters, rtpMapping)
return producer.receive(rtpParameters, rtpMapping)
.catch((error) =>
{
producer.close();
......@@ -579,7 +598,7 @@ class Peer extends EnhancedEventEmitter
_createTransport(id, options)
{
logger.debug('_createTransport()');
logger.debug('_createTransport() [id:%s]', id);
const internal =
{
......@@ -619,7 +638,7 @@ class Peer extends EnhancedEventEmitter
_createProducer(id, kind, transportId)
{
logger.debug('_createProducer()');
logger.debug('_createProducer() [id:%s, kind:%s]', id, kind);
if (!this.rtpCapabilities)
return Promise.reject(new errors.InvalidStateError('RTP capabilities unset'));
......
......@@ -28,7 +28,6 @@ class Producer extends EnhancedEventEmitter
// - .kind
// - .transport
// - .rtpParameters
// - .adaptedRtpParameters
// - .rtpMapping
this._data = data;
......@@ -197,11 +196,6 @@ class Producer extends EnhancedEventEmitter
return this._data.rtpParameters;
}
get adaptedRtpParameters()
{
return this._data.adaptedRtpParameters;
}
get rtpMapping()
{
return this._data.rtpMapping;
......@@ -336,13 +330,11 @@ class Producer extends EnhancedEventEmitter
* Start receiving media from the remote Producer.
*
* @param {RTCRtpParameters} rtpParameters - Receiving RTP parameters.
* @param {RTCRtpParameters} adaptedRtpParameters - RTP parameters adapted to the
* room RTP capabilities.
* @param {Object} rtpMapping.
*
* @return {Promise} Resolves to this.
*/
receive(rtpParameters, adaptedRtpParameters, rtpMapping)
receive(rtpParameters, rtpMapping)
{
logger.debug('receive()');
......@@ -362,7 +354,6 @@ class Producer extends EnhancedEventEmitter
logger.debug('"producer.receive" request succeeded');
this._data.rtpParameters = rtpParameters;
this._data.adaptedRtpParameters = adaptedRtpParameters;
this._data.rtpMapping = rtpMapping;
return this;
......
......@@ -371,9 +371,9 @@ class Room extends EnhancedEventEmitter
},
// NOTE: This may throw.
adaptProducerRtpParametersToRtpCapabilities : (rtpParameters) =>
getProducerRtpParametersMapping : (rtpParameters) =>
{
return ortc.adaptProducerRtpParametersToRtpCapabilities(
return ortc.getProducerRtpParametersMapping(
rtpParameters, this.rtpCapabilities);
}
};
......
'use strict';
const utils = require('./utils');
const supportedRtpCapabilities = require('./supportedRtpCapabilities');
const DYNAMIC_PAYLOAD_TYPES =
......@@ -163,12 +162,12 @@ exports.assertCapabilitiesSubset = function(capsA, capsB)
* @param {RTCRtpCapabilities} caps
*
* @return {Object}
*
* TODO: This method should also reduce encodings in case of simulcast/SVC
* so the resulting consumableRtpParameters are suitable for a Consumer.
*/
exports.adaptProducerRtpParametersToRtpCapabilities = function(params, caps)
exports.getProducerRtpParametersMapping = function(params, caps)
{
// Clone given parameters.
params = utils.cloneObject(params);
const codecToCapCodec = new Map();
// Match parameters media codecs to capabilities media codecs.
......@@ -191,8 +190,6 @@ exports.adaptProducerRtpParametersToRtpCapabilities = function(params, caps)
codecToCapCodec.set(codec, matchedCapCodec);
}
const aptToCapApt = {};
// Match parameters RTX codecs to capabilities RTX codecs.
for (const codec of params.codecs)
{
......@@ -228,22 +225,17 @@ exports.adaptProducerRtpParametersToRtpCapabilities = function(params, caps)
`[capabilities codec PT:${capMediaCodec.preferredPayloadType}]`);
}
aptToCapApt[codec.parameters.apt] = associatedCapRtxCodec.parameters.apt;
codecToCapCodec.set(codec, associatedCapRtxCodec);
}
const codecPayloadTypeMapping = new Map();
const mapCodecPayloadTypes = new Map();
for (const [ codec, capCodec ] of codecToCapCodec)
{
codecPayloadTypeMapping.set(codec.payloadType, capCodec.preferredPayloadType);
codec.payloadType = capCodec.preferredPayloadType;
if (codec.name.toLowerCase() === 'rtx')
codec.parameters.apt = aptToCapApt[codec.parameters.apt];
mapCodecPayloadTypes.set(codec.payloadType, capCodec.preferredPayloadType);
}
const headerExtensionIdMapping = new Map();
const mapHeaderExtensionIds = new Map();
for (const ext of params.headerExtensions)
{
......@@ -256,17 +248,12 @@ exports.adaptProducerRtpParametersToRtpCapabilities = function(params, caps)
`unsupported header extensions [uri:"${ext.uri}", id:${ext.id}]`);
}
headerExtensionIdMapping.set(ext.id, matchedCapExt.preferredId);
ext.id = matchedCapExt.preferredId;
mapHeaderExtensionIds.set(ext.id, matchedCapExt.preferredId);
}
return {
adaptedRtpParameters : params,
rtpMapping :
{
codecPayloadTypes : Array.from(codecPayloadTypeMapping),
headerExtensionIds : Array.from(headerExtensionIdMapping)
}
codecPayloadTypes : Array.from(mapCodecPayloadTypes),
headerExtensionIds : Array.from(mapHeaderExtensionIds)
};
};
......
......@@ -37,6 +37,8 @@ namespace Channel
PRODUCER_CLOSE,
PRODUCER_DUMP,
PRODUCER_RECEIVE,
PRODUCER_PAUSE,
PRODUCER_RESUME,
PRODUCER_SET_RTP_RAW_EVENT,
PRODUCER_SET_RTP_OBJECT_EVENT,
CONSUMER_DUMP,
......
......@@ -94,6 +94,7 @@ namespace RTC
RTC::RtpParameters* rtpParameters{ nullptr };
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
std::map<uint32_t, RTC::RtpStreamRecv*> rtxStreamMap;
bool paused{ false };
// Others.
struct RtpMapping rtpMapping;
struct KnownHeaderExtensions knownHeaderExtensions;
......
......@@ -32,6 +32,8 @@ namespace Channel
{ "producer.close", Request::MethodId::PRODUCER_CLOSE },
{ "producer.dump", Request::MethodId::PRODUCER_DUMP },
{ "producer.receive", Request::MethodId::PRODUCER_RECEIVE },
{ "producer.pause", Request::MethodId::PRODUCER_PAUSE },
{ "producer.resume" , Request::MethodId::PRODUCER_RESUME },
{ "producer.setRtpRawEvent", Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT },
{ "producer.setRtpObjectEvent", Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT },
{ "consumer.dump", Request::MethodId::CONSUMER_DUMP },
......
......@@ -225,6 +225,8 @@ void Loop::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::Req
case Channel::Request::MethodId::PRODUCER_CLOSE:
case Channel::Request::MethodId::PRODUCER_DUMP:
case Channel::Request::MethodId::PRODUCER_RECEIVE:
case Channel::Request::MethodId::PRODUCER_PAUSE:
case Channel::Request::MethodId::PRODUCER_RESUME:
case Channel::Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT:
case Channel::Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT:
case Channel::Request::MethodId::CONSUMER_DUMP:
......
......@@ -316,6 +316,8 @@ namespace RTC
case Channel::Request::MethodId::PRODUCER_CLOSE:
case Channel::Request::MethodId::PRODUCER_DUMP:
case Channel::Request::MethodId::PRODUCER_RECEIVE:
case Channel::Request::MethodId::PRODUCER_PAUSE:
case Channel::Request::MethodId::PRODUCER_RESUME:
case Channel::Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT:
case Channel::Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT:
{
......@@ -737,12 +739,6 @@ namespace RTC
MS_ASSERT(producer->GetParameters(), "Producer has no parameters");
auto transport = producer->GetTransport();
// NOTE: This may throw.
if (transport != nullptr)
transport->AddProducer(producer);
this->listener->OnPeerProducerRtpParameters(this, producer);
}
......
......@@ -159,13 +159,19 @@ namespace RTC
try
{
// This may throw.
// NOTE: This may throw.
this->rtpParameters = new RTC::RtpParameters(request->data[JsonStringRtpParameters]);
// This may throw.
auto transport = GetTransport();
// NOTE: This may throw.
if (transport != nullptr)
transport->AddProducer(this);
// NOTE: This may throw.
CreateRtpMapping(request->data[JsonStringRtpMapping]);
// This may throw.
// NOTE: This may throw.
this->listener->OnProducerRtpParameters(this);
}
catch (const MediaSoupError& error)
......@@ -192,6 +198,29 @@ namespace RTC
break;
}
case Channel::Request::MethodId::PRODUCER_PAUSE:
{
this->paused = true;
request->Accept();
break;
}
case Channel::Request::MethodId::PRODUCER_RESUME:
{
bool wasPaused = this->paused;
this->paused = false;
request->Accept();
if (wasPaused)
RequestFullFrame();
break;
}
case Channel::Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT:
{
static const Json::StaticString JsonStringEnabled{ "enabled" };
......@@ -209,7 +238,7 @@ namespace RTC
// If set, require a full frame.
if (this->rtpRawEventEnabled)
this->RequestFullFrame();
RequestFullFrame();
break;
}
......@@ -231,7 +260,7 @@ namespace RTC
// If set, require a full frame.
if (this->rtpObjectEventEnabled)
this->RequestFullFrame();
RequestFullFrame();
break;
}
......@@ -257,8 +286,6 @@ namespace RTC
static const Json::StaticString JsonStringTimestamp{ "timestamp" };
static const Json::StaticString JsonStringSsrc{ "ssrc" };
// TODO: Check if paused, etc (not yet done).
// Find the corresponding RtpStreamRecv.
uint32_t ssrc = packet->GetSsrc();
......@@ -287,11 +314,9 @@ namespace RTC
return;
}
// Apply the Producer RTP mapping before dispatching the packet to the Room.
ApplyRtpMapping(packet);
// Notify the listener.
this->listener->OnRtpPacket(this, packet);
// If paused stop here.
if (this->paused)
return;
// Emit "rtpraw" if enabled.
if (this->rtpRawEventEnabled)
......@@ -323,6 +348,12 @@ namespace RTC
this->notifier->EmitWithBinary(
this->producerId, "rtpobject", eventData, packet->GetPayload(), packet->GetPayloadLength());
}
// Apply the Producer RTP mapping before dispatching the packet to the Room.
ApplyRtpMapping(packet);
// Notify the listener.
this->listener->OnRtpPacket(this, packet);
}
void Producer::GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now)
......
......@@ -254,6 +254,8 @@ namespace RTC
case Channel::Request::MethodId::PRODUCER_CLOSE:
case Channel::Request::MethodId::PRODUCER_DUMP:
case Channel::Request::MethodId::PRODUCER_RECEIVE:
case Channel::Request::MethodId::PRODUCER_PAUSE:
case Channel::Request::MethodId::PRODUCER_RESUME:
case Channel::Request::MethodId::PRODUCER_SET_RTP_RAW_EVENT:
case Channel::Request::MethodId::PRODUCER_SET_RTP_OBJECT_EVENT:
case Channel::Request::MethodId::CONSUMER_DUMP:
......
......@@ -885,6 +885,9 @@ namespace RTC
// Pass the RTP packet to the corresponding Producer.
producer->ReceiveRtpPacket(packet);
// TODO: If the Producer is paused, should we pass the packet to the
// BWE?
// Feed the remote bitrate estimator (REMB).
if (this->remoteBitrateEstimator)
{
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论