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

worker: Producers and Consumer have Transport and rtpParameters from the *beginning*. Period.

上级 120b09c0
# TODO in mediasoup v2 (server-side)
* Redo the compile_commands_template.json.
* Remove DEPTH stuff.
* worker: All the RTC emthods in Producer and Consumer do a `MS_ASSERT(Enabled();` (meaning that they already have `transport` and `rtpParameters`). Producers/Consumers without them are not present in the Router `mapProducerConsumers`/`mapConsumerProducer` maps. However *THEY DO EXIST* in the Sets of `producers` and `consumers` of the Transport.
- So: before calling any RTC method on a Producer/Consumer, the Transport must check `producer/consumer->IsEnabled()` and ignore if not.
* Redo the compile_commands_template.json.
* Remove all the "close" events in C++. May be leave the `transport.on('close')`.
* Rethink the auto close of the worker `Transport`. Yes/no?
* Move the RTCP timer from Peer to Transport.
......@@ -18,18 +17,10 @@
* 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.
- NOTE: Won't happen with the new design since that's done in JS land.
* 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...
* Must be ready for the case in which the client closes a `Transport` on which a `Producer` was running. Isn't it?
* Upon a `updateProducer` notification, map the new SSRCs of the `Producer` and send a PLI back to the browser (otherwise, consumers will get frozen video).
* Since a `Producer` may receive RTP (and RTCP) with ssrc, pt and headerExtensionId different that those in the room, we must ready to also map consumer/generated/relayed RTCP.
## TODO per JS class
......
......@@ -35,6 +35,9 @@ class Consumer extends EnhancedEventEmitter
// Channel instance.
this._channel = channel;
// App data.
this._appData = undefined;
// Locally paused flag.
// @type {Boolean}
this._locallyPaused = false;
......@@ -60,6 +63,16 @@ class Consumer extends EnhancedEventEmitter
return this._closed;
}
get appData()
{
return this._appData;
}
set appData(appData)
{
this._appData = appData;
}
get kind()
{
return this._data.kind;
......@@ -85,11 +98,6 @@ class Consumer extends EnhancedEventEmitter
return this._data.source;
}
get enabled()
{
return Boolean(this->transport) && Boolean(this->rtpParameters);
}
/**
* Whether the Consumer is locally paused.
*
......
......@@ -385,26 +385,12 @@ class Peer extends EnhancedEventEmitter
appData
} = request;
// NOTE: This may throw.
const rtpMapping =
this._sandbox.getProducerRtpParametersMapping(rtpParameters);
return this._createProducer(id, kind, transportId, appData)
return this._createProducer(id, kind, transportId, rtpParameters, appData)
.then((producer) =>
{
if (paused)
producer.remotePause();
return producer.receive(rtpParameters, rtpMapping)
.catch((error) =>
{
producer.remoteClose();
throw error;
});
})
.then(() =>
{
return;
});
}
......@@ -648,7 +634,7 @@ class Peer extends EnhancedEventEmitter
});
}
_createProducer(id, kind, transportId, appData)
_createProducer(id, kind, transportId, rtpParameters, appData)
{
logger.debug('_createProducer() [id:%s, kind:%s]', id, kind);
......@@ -657,6 +643,18 @@ class Peer extends EnhancedEventEmitter
else if (!KINDS.has(kind))
return Promise.reject(new TypeError(`unsupported kind: ${kind}`));
let rtpMapping;
try
{
// NOTE: This may throw.
rtpMapping = this._sandbox.getProducerRtpParametersMapping(rtpParameters);
}
catch (error)
{
return Promise.reject(new Error(`wrong RTP parameters: ${error}`));
}
const transport = this._transports.get(transportId);
if (!transport)
......@@ -671,22 +669,22 @@ class Peer extends EnhancedEventEmitter
const data =
{
kind : kind,
peer : this,
transport : transport
};
const options =
{
kind
kind : kind,
peer : this,
transport : transport,
rtpParameters : rtpParameters
};
return this._channel.request('router.createProducer', internal, options)
return this._channel.request('router.createProducer', internal,
{
kind,
rtpParameters,
rtpMapping
})
.then(() =>
{
logger.debug('"router.createProducer" request succeeded');
// Create a Producer instance.
const producer = new Producer(internal, data, this._channel);
producer.appData = appData;
......@@ -700,14 +698,6 @@ class Peer extends EnhancedEventEmitter
this._sendNotification(method, data2);
});
// On closure, the worker Transport closes all its Producers and the client
// side gets producer.on('unhandled').
transport.on('@close', () =>
{
if (!producer.closed)
producer.close(undefined, false);
});
this.emit('@newproducer', producer);
this.safeEmit('newproducer', producer);
......
......@@ -33,6 +33,9 @@ class Producer extends EnhancedEventEmitter
// Channel instance.
this._channel = channel;
// App data.
this._appData = undefined;
// Locally paused flag.
// @type {Boolean}
this._locallyPaused = false;
......@@ -41,6 +44,8 @@ class Producer extends EnhancedEventEmitter
// @type {Boolean}
this._remotelyPaused = false;
this._handleTransportEvents();
this._handleWorkerNotifications();
}
......@@ -54,6 +59,16 @@ class Producer extends EnhancedEventEmitter
return this._closed;
}
get appData()
{
return this._appData;
}
set appData(appData)
{
this._appData = appData;
}
get kind()
{
return this._data.kind;
......@@ -74,11 +89,6 @@ class Producer extends EnhancedEventEmitter
return this._data.rtpParameters;
}
get enabled()
{
return Boolean(this->transport) && Boolean(this->rtpParameters);
}
/**
* Whether the Producer is locally paused.
*
......@@ -400,6 +410,19 @@ class Producer extends EnhancedEventEmitter
this.safeEmit('resume', 'remote', appData);
}
_handleTransportEvents()
{
// On closure, the worker Transport closes all its Producers and the client
// side gets producer.on('unhandled').
this.transport.on('@close', () =>
{
if (this._closed)
return;
this.close(undefined, false);
});
}
_handleWorkerNotifications()
{
// Subscribe to notifications.
......
......@@ -33,14 +33,12 @@ namespace Channel
TRANSPORT_CHANGE_UFRAG_PWD,
PRODUCER_CLOSE,
PRODUCER_DUMP,
PRODUCER_RECEIVE,
PRODUCER_PAUSE,
PRODUCER_RESUME,
PRODUCER_SET_RTP_RAW_EVENT,
PRODUCER_SET_RTP_OBJECT_EVENT,
CONSUMER_CLOSE,
CONSUMER_DUMP,
CONSUMER_SEND,
CONSUMER_PAUSE,
CONSUMER_RESUME
};
......
......@@ -26,8 +26,9 @@ namespace RTC
Channel::Notifier* notifier,
uint32_t consumerId,
RTC::Media::Kind kind,
uint32_t sourceProducerId,
RTC::Transport* transport);
RTC::Transport* transport,
RTC::RtpParameters& rtpParameters,
uint32_t sourceProducerId);
private:
virtual ~Consumer();
......@@ -38,8 +39,7 @@ namespace RTC
void HandleRequest(Channel::Request* request);
void AddListener(RTC::ConsumerListener* listener);
void RemoveListener(RTC::ConsumerListener* listener);
bool IsEnabled() const;
RTC::RtpParameters* GetParameters() const;
const RTC::RtpParameters& GetParameters() const;
void SendRtpPacket(RTC::RtpPacket* packet);
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now);
void ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket);
......@@ -48,6 +48,7 @@ namespace RTC
void RequestFullFrame();
private:
void FillSupportedCodecPayloadTypes();
void CreateRtpStream(RTC::RtpEncodingParameters& encoding);
void RetransmitRtpPacket(RTC::RtpPacket* packet);
......@@ -59,11 +60,11 @@ namespace RTC
private:
// Passed by argument.
std::unordered_set<RTC::ConsumerListener*> listeners;
Channel::Notifier* notifier{ nullptr };
RTC::Transport* transport{ nullptr };
RTC::RtpParameters rtpParameters;
std::unordered_set<RTC::ConsumerListener*> listeners;
// Allocated by this.
RTC::RtpParameters* rtpParameters{ nullptr };
RTC::RtpStreamSend* rtpStream{ nullptr };
// Others.
std::unordered_set<uint8_t> supportedCodecPayloadTypes;
......@@ -88,12 +89,7 @@ namespace RTC
this->listeners.erase(listener);
}
inline bool Consumer::IsEnabled() const
{
return this->transport && this->rtpParameters && this->rtpStream;
}
inline RTC::RtpParameters* Consumer::GetParameters() const
inline const RTC::RtpParameters& Consumer::GetParameters() const
{
return this->rtpParameters;
}
......
......@@ -13,7 +13,6 @@ namespace RTC
{
public:
virtual void OnConsumerClosed(RTC::Consumer* consumer) = 0;
virtual void OnConsumerRtpParameters(RTC::Consumer* consumer) = 0;
virtual void OnConsumerFullFrameRequired(RTC::Consumer* consumer) = 0;
};
} // namespace RTC
......
......@@ -21,7 +21,7 @@ namespace RTC
{
class Producer : public RtpStreamRecv::Listener
{
private:
public:
struct RtpMapping
{
std::map<uint8_t, uint8_t> codecPayloadTypes;
......@@ -40,7 +40,9 @@ namespace RTC
Channel::Notifier* notifier,
uint32_t producerId,
RTC::Media::Kind kind,
RTC::Transport* transport);
RTC::Transport* transport,
RTC::RtpParameters& rtpParameters,
struct RtpMapping& rtpMapping);
public:
// Must be public because Router needs to call it.
......@@ -52,8 +54,7 @@ namespace RTC
void HandleRequest(Channel::Request* request);
void AddListener(RTC::ProducerListener* listener);
void RemoveListener(RTC::ProducerListener* listener);
bool IsEnabled() const;
RTC::RtpParameters* GetParameters() const;
const RTC::RtpParameters& GetParameters() const;
void ReceiveRtpPacket(RTC::RtpPacket* packet);
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now);
......@@ -62,7 +63,7 @@ namespace RTC
void RequestFullFrame() const;
private:
void CreateRtpMapping(Json::Value& rtpMapping);
void FillKnownHeaderExtensions();
void CreateRtpStream(RTC::RtpEncodingParameters& encoding);
void ApplyRtpMapping(RTC::RtpPacket* packet);
......@@ -78,15 +79,15 @@ namespace RTC
private:
// Passed by argument.
std::unordered_set<RTC::ProducerListener*> listeners;
Channel::Notifier* notifier{ nullptr };
RTC::Transport* transport{ nullptr };
RTC::RtpParameters rtpParameters;
struct RtpMapping rtpMapping;
std::unordered_set<RTC::ProducerListener*> listeners;
// Allocated by this.
RTC::RtpParameters* rtpParameters{ nullptr };
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxStreams;
// Others.
struct RtpMapping rtpMapping;
struct KnownHeaderExtensions knownHeaderExtensions;
bool paused{ false };
bool rtpRawEventEnabled{ false };
......@@ -108,12 +109,7 @@ namespace RTC
this->listeners.erase(listener);
}
inline bool Producer::IsEnabled() const
{
return this->transport && this->rtpParameters && this->rtpStreams.size();
}
inline RTC::RtpParameters* Producer::GetParameters() const
inline const RTC::RtpParameters& Producer::GetParameters() const
{
return this->rtpParameters;
}
......
......@@ -13,7 +13,6 @@ namespace RTC
{
public:
virtual void OnProducerClosed(RTC::Producer* producer) = 0;
virtual void OnProducerRtpParameters(RTC::Producer* producer) = 0;
virtual void OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet) = 0;
};
} // namespace RTC
......
......@@ -66,13 +66,11 @@ namespace RTC
/* Pure virtual methods inherited from RTC::ProducerListener. */
public:
void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerRtpParameters(RTC::Producer* producer) override;
void OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet) override;
/* Pure virtual methods inherited from RTC::ConsumerListener. */
public:
void OnConsumerClosed(RTC::Consumer* consumer) override;
void OnConsumerRtpParameters(RTC::Consumer* consumer) override;
void OnConsumerFullFrameRequired(RTC::Consumer* consumer) override;
/* Pure virtual methods inherited from Timer::Listener. */
......
......@@ -260,6 +260,7 @@ namespace RTC
class RtpParameters
{
public:
RtpParameters(){};
explicit RtpParameters(Json::Value& data);
explicit RtpParameters(const RtpParameters* rtpParameters);
......
......@@ -18,7 +18,7 @@ namespace RTC
public:
Json::Value ToJson() const;
bool HasSsrc(uint32_t ssrc, const RTC::Producer* producer) const;
bool HasMuxId(std::string& muxId, const RTC::Producer* producer) const;
bool HasMuxId(const std::string& muxId, const RTC::Producer* producer) const;
bool HasPayloadType(uint8_t payloadType, const RTC::Producer* producer) const;
void AddProducer(RTC::Producer* producer);
void RemoveProducer(const RTC::Producer* producer);
......@@ -57,7 +57,7 @@ namespace RTC
}
}
inline bool RtpListener::HasMuxId(std::string& muxId, const RTC::Producer* producer) const
inline bool RtpListener::HasMuxId(const std::string& muxId, const RTC::Producer* producer) const
{
auto it = this->muxIdTable.find(muxId);
......
......@@ -63,7 +63,6 @@ namespace RTC
void SendRtpPacket(RTC::RtpPacket* packet);
void SendRtcpPacket(RTC::RTCP::Packet* packet);
void SendRtcpCompoundPacket(RTC::RTCP::CompoundPacket* packet);
RTC::Producer* GetProducer(uint32_t ssrc);
bool IsConnected() const;
void EnableRemb();
bool HasRemb();
......@@ -132,13 +131,11 @@ namespace RTC
/* Pure virtual methods inherited from RTC::ProducerListener. */
public:
void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerRtpParameters(RTC::Producer* producer) override;
void OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet) override;
/* Pure virtual methods inherited from RTC::ConsumerListener. */
public:
void OnConsumerClosed(RTC::Consumer* consumer) override;
void OnConsumerRtpParameters(RTC::Consumer* consumer) override;
void OnConsumerFullFrameRequired(RTC::Consumer* consumer) override;
public:
......@@ -178,11 +175,7 @@ namespace RTC
/* Inline instance methods. */
inline RTC::Producer* Transport::GetProducer(uint32_t ssrc)
{
return this->rtpListener.GetProducer(ssrc);
}
// TODO: Used?
inline bool Transport::IsConnected() const
{
return this->dtlsTransport->GetState() == RTC::DtlsTransport::DtlsState::CONNECTED;
......
......@@ -28,14 +28,12 @@ namespace Channel
{ "transport.changeUfragPwd", Request::MethodId::TRANSPORT_CHANGE_UFRAG_PWD },
{ "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.close", Request::MethodId::CONSUMER_CLOSE },
{ "consumer.dump", Request::MethodId::CONSUMER_DUMP },
{ "consumer.send", Request::MethodId::CONSUMER_SEND },
{ "consumer.pause", Request::MethodId::CONSUMER_PAUSE },
{ "consumer.resume", Request::MethodId::CONSUMER_RESUME }
};
......
......@@ -21,13 +21,19 @@ namespace RTC
Channel::Notifier* notifier,
uint32_t consumerId,
RTC::Media::Kind kind,
uint32_t sourceProducerId,
RTC::Transport* transport)
: consumerId(consumerId), kind(kind), sourceProducerId(sourceProducerId), notifier(notifier),
transport(transport)
RTC::Transport* transport,
RTC::RtpParameters& rtpParameters,
uint32_t sourceProducerId)
: consumerId(consumerId), kind(kind), sourceProducerId(sourceProducerId), notifier(notifier), transport(transport), rtpParameters(rtpParameters)
{
MS_TRACE();
FillSupportedCodecPayloadTypes();
// Create RtpStreamSend instance.
CreateRtpStream(this->rtpParameters.encodings[0]);
// Set the RTCP report generation interval.
if (this->kind == RTC::Media::Kind::AUDIO)
this->maxRtcpInterval = RTC::RTCP::MaxAudioIntervalMs;
else
......@@ -38,7 +44,6 @@ namespace RTC
{
MS_TRACE();
delete this->rtpParameters;
delete this->rtpStream;
}
......@@ -77,13 +82,9 @@ namespace RTC
json[JsonStringSourceProducerId] = Json::UInt{ this->sourceProducerId };
if (this->rtpParameters != nullptr)
json[JsonStringRtpParameters] = this->rtpParameters->ToJson();
else
json[JsonStringRtpParameters] = Json::nullValue;
json[JsonStringRtpParameters] = this->rtpParameters.ToJson();
if (this->rtpStream != nullptr)
json[JsonStringRtpStream] = this->rtpStream->ToJson();
json[JsonStringRtpStream] = this->rtpStream->ToJson();
json[JsonStringSupportedCodecPayloadTypes] = Json::arrayValue;
......@@ -112,49 +113,6 @@ namespace RTC
break;
}
case Channel::Request::MethodId::CONSUMER_SEND:
{
static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" };
if (!request->data[JsonStringRtpParameters].isObject())
{
request->Reject("missing data.rtpParameters");
return;
}
if (this->rtpParameters)
{
request->Reject("Consumer already has RTP parameters");
return;
}
try
{
// NOTE: This may throw.
this->rtpParameters = new RTC::RtpParameters(request->data[JsonStringRtpParameters]);
for (auto& listener : this->listeners)
{
listener->OnConsumerRtpParameters(this);
}
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
return;
}
// Create RtpStreamSend instance.
CreateRtpStream(this->rtpParameters->encodings[0]);
request->Accept();
break;
}
case Channel::Request::MethodId::CONSUMER_PAUSE:
{
this->paused = true;
......@@ -196,12 +154,10 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Consumer not enabled");
// TODO: Must refactor for simulcast.
// Ignore the packet if the SSRC is not the single one in the sender
// RTP parameters.
if (packet->GetSsrc() != this->rtpParameters->encodings[0].ssrc)
if (packet->GetSsrc() != this->rtpParameters.encodings[0].ssrc)
{
MS_WARN_TAG(rtp, "ignoring packet with unknown SSRC [ssrc:%" PRIu32 "]", packet->GetSsrc());
......@@ -209,15 +165,13 @@ namespace RTC
}
// Map the payload type.
uint8_t payloadType = packet->GetPayloadType();
auto it = this->supportedCodecPayloadTypes.find(payloadType);
auto payloadType = packet->GetPayloadType();
// NOTE: This may happen if this peer supports just some codecs from the
// given RtpParameters.
// TODO: We should just ignore those packets as they have non been negotiated.
if (it == this->supportedCodecPayloadTypes.end())
// NOTE: This may happen if this Consumer supports just some codecs of those
// in the corresponding Producer.
if (this->supportedCodecPayloadTypes.find(payloadType) == this->supportedCodecPayloadTypes.end())
{
MS_WARN_DEV("payload type not supported [payloadType:%" PRIu8 "]", payloadType);
MS_DEBUG_DEV("payload type not supported [payloadType:%" PRIu8 "]", payloadType);
return;
}
......@@ -237,18 +191,17 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Consumer not enabled");
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) < this->maxRtcpInterval)
return;
auto* report = this->rtpStream->GetRtcpSenderReport(now);
if (report == nullptr)
return;
// NOTE: This assumes a single stream.
uint32_t ssrc = this->rtpParameters->encodings[0].ssrc;
std::string cname = this->rtpParameters->rtcp.cname;
uint32_t ssrc = this->rtpParameters.encodings[0].ssrc;
std::string cname = this->rtpParameters.rtcp.cname;
report->SetSsrc(ssrc);
packet->AddSenderReport(report);
......@@ -260,7 +213,6 @@ namespace RTC
sdesChunk->AddItem(sdesItem);
packet->AddSdesChunk(sdesChunk);
this->lastRtcpSentTime = now;
}
......@@ -268,8 +220,6 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Consumer not enabled");
for (auto it = nackPacket->Begin(); it != nackPacket->End(); ++it)
{
RTC::RTCP::FeedbackRtpNackItem* item = *it;
......@@ -294,8 +244,6 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Consumer not enabled");
this->rtpStream->ReceiveRtcpReceiverReport(report);
}
......@@ -303,21 +251,29 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Consumer not enabled");
for (auto& listener : this->listeners)
{
listener->OnConsumerFullFrameRequired(this);
}
}
void Consumer::FillSupportedCodecPayloadTypes()
{
MS_TRACE();
for (auto& codec : this->rtpParameters.codecs)
{
this->supportedCodecPayloadTypes.insert(codec.payloadType);
}
}
void Consumer::CreateRtpStream(RTC::RtpEncodingParameters& encoding)
{
MS_TRACE();
uint32_t ssrc = encoding.ssrc;
// Get the codec of the stream/encoding.
auto& codec = this->rtpParameters->GetCodecForEncoding(encoding);
auto& codec = this->rtpParameters.GetCodecForEncoding(encoding);
bool useNack{ false };
bool usePli{ false };
......@@ -355,7 +311,7 @@ namespace RTC
if (encoding.hasRtx && encoding.rtx.ssrc != 0u)
{
auto& codec = this->rtpParameters->GetRtxCodecForEncoding(encoding);
auto& codec = this->rtpParameters.GetRtxCodecForEncoding(encoding);
this->rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc);
}
......@@ -365,8 +321,6 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Consumer not enabled");
RTC::RtpPacket* rtxPacket{ nullptr };
if (this->rtpStream->HasRtx())
......@@ -385,7 +339,6 @@ namespace RTC
packet->GetSsrc(),
packet->GetSequenceNumber());
}
else
{
rtxPacket = packet;
......
......@@ -13,11 +13,28 @@ namespace RTC
/* Instance methods. */
Producer::Producer(
Channel::Notifier* notifier, uint32_t producerId, RTC::Media::Kind kind, RTC::Transport* transport)
: producerId(producerId), kind(kind), notifier(notifier), transport(transport)
Channel::Notifier* notifier,
uint32_t producerId,
RTC::Media::Kind kind,
RTC::Transport* transport,
RTC::RtpParameters& rtpParameters,
struct RtpMapping& rtpMapping)
: producerId(producerId), kind(kind), notifier(notifier), transport(transport), rtpParameters(rtpParameters)
{
MS_TRACE();
this->rtpMapping = rtpMapping;
// Fill ids of well known RTP header extensions with the mapped ids (if any).
FillKnownHeaderExtensions();
// Create RtpStreamRecv instances.
for (auto& encoding : this->rtpParameters.encodings)
{
CreateRtpStream(encoding);
}
// Set the RTCP report generation interval.
if (this->kind == RTC::Media::Kind::AUDIO)
this->maxRtcpInterval = RTC::RTCP::MaxAudioIntervalMs;
else
......@@ -28,8 +45,6 @@ namespace RTC
{
MS_TRACE();
delete this->rtpParameters;
for (auto& kv : this->rtpStreams)
{
auto rtpStream = kv.second;
......@@ -74,10 +89,7 @@ namespace RTC
json[JsonStringKind] = RTC::Media::GetJsonString(this->kind);
if (this->rtpParameters != nullptr)
json[JsonStringRtpParameters] = this->rtpParameters->ToJson();
else
json[JsonStringRtpParameters] = Json::nullValue;
json[JsonStringRtpParameters] = this->rtpParameters.ToJson();
for (auto& kv : this->rtpStreams)
{
......@@ -117,69 +129,6 @@ namespace RTC
break;
}
case Channel::Request::MethodId::PRODUCER_RECEIVE:
{
static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" };
static const Json::StaticString JsonStringRtpMapping{ "rtpMapping" };
if (!request->data[JsonStringRtpParameters].isObject())
{
request->Reject("missing data.rtpParameters");
return;
}
else if (!request->data[JsonStringRtpMapping].isObject())
{
request->Reject("missing data.rtpMapping");
return;
}
if (this->rtpParameters)
{
request->Reject("Producer already has RTP parameters");
return;
}
try
{
// NOTE: This may throw.
this->rtpParameters = new RTC::RtpParameters(request->data[JsonStringRtpParameters]);
// NOTE: This may throw.
CreateRtpMapping(request->data[JsonStringRtpMapping]);
// NOTE: This may throw (due RtpListener->AddProducer()).
for (auto& listener : this->listeners)
{
listener->OnProducerRtpParameters(this);
}
}
catch (const MediaSoupError& error)
{
if (this->rtpParameters)
{
delete this->rtpParameters;
this->rtpParameters = nullptr;
}
request->Reject(error.what());
return;
}
// Create RtpStreamRecv instances.
for (auto& encoding : this->rtpParameters->encodings)
{
CreateRtpStream(encoding);
}
request->Accept();
break;
}
case Channel::Request::MethodId::PRODUCER_PAUSE:
{
this->paused = true;
......@@ -260,8 +209,6 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Producer not enabled");
static const Json::StaticString JsonStringObject{ "object" };
static const Json::StaticString JsonStringPayloadType{ "payloadType" };
static const Json::StaticString JsonStringMarker{ "marker" };
......@@ -356,11 +303,6 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Producer not enabled");
if (this->transport == nullptr)
return;
// Ensure that the RTCP packet fits into the RTCP buffer.
if (packet->GetSize() > RTC::RTCP::BufferSize)
{
......@@ -377,11 +319,6 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Producer not enabled");
if (this->transport == nullptr)
return;
// Ensure that the RTCP packet fits into the RTCP buffer.
if (packet->GetSize() > RTC::RTCP::BufferSize)
{
......@@ -398,8 +335,6 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Producer not enabled");
for (auto& kv : this->rtpStreams)
{
auto rtpStream = kv.second;
......@@ -408,48 +343,15 @@ namespace RTC
}
}
void Producer::CreateRtpMapping(Json::Value& rtpMapping)
void Producer::FillKnownHeaderExtensions()
{
MS_TRACE();
static const Json::StaticString JsonStringCodecPayloadTypes{ "codecPayloadTypes" };
static const Json::StaticString JsonStringHeaderExtensionIds{ "headerExtensionIds" };
if (!rtpMapping[JsonStringCodecPayloadTypes].isArray())
MS_THROW_ERROR("missing rtpMapping.codecPayloadTypes");
else if (!rtpMapping[JsonStringHeaderExtensionIds].isArray())
MS_THROW_ERROR("missing rtpMapping.headerExtensionIds");
for (auto& pair : rtpMapping[JsonStringCodecPayloadTypes])
{
if (!pair.isArray() || pair.size() != 2 || !pair[0].isUInt() || !pair[1].isUInt())
MS_THROW_ERROR("wrong rtpMapping entry");
auto sourcePayloadType = static_cast<uint8_t>(pair[0].asUInt());
auto mappedPayloadType = static_cast<uint8_t>(pair[1].asUInt());
this->rtpMapping.codecPayloadTypes[sourcePayloadType] = mappedPayloadType;
}
for (auto& pair : rtpMapping[JsonStringHeaderExtensionIds])
{
if (!pair.isArray() || pair.size() != 2 || !pair[0].isUInt() || !pair[1].isUInt())
MS_THROW_ERROR("wrong rtpMapping entry");
auto sourceHeaderExtensionId = static_cast<uint8_t>(pair[0].asUInt());
auto mappedHeaderExtensionId = static_cast<uint8_t>(pair[1].asUInt());
this->rtpMapping.headerExtensionIds[sourceHeaderExtensionId] = mappedHeaderExtensionId;
}
// Also, fill the id of well known RTP header extensions with the mapped ids
// (if any).
auto& idMapping = this->rtpMapping.headerExtensionIds;
uint8_t ssrcAudioLevelId{ 0 };
uint8_t absSendTimeId{ 0 };
for (auto& exten : this->rtpParameters->headerExtensions)
for (auto& exten : this->rtpParameters.headerExtensions)
{
if (this->kind == RTC::Media::Kind::AUDIO && (ssrcAudioLevelId == 0u) &&
exten.type == RTC::RtpHeaderExtensionUri::Type::SSRC_AUDIO_LEVEL)
......@@ -492,7 +394,7 @@ namespace RTC
return;
// Get the codec of the stream/encoding.
auto& codec = this->rtpParameters->GetCodecForEncoding(encoding);
auto& codec = this->rtpParameters.GetCodecForEncoding(encoding);
bool useNack{ false };
bool usePli{ false };
bool useRemb{ false };
......@@ -542,7 +444,7 @@ namespace RTC
if (this->mapRtxStreams.find(encoding.rtx.ssrc) != this->mapRtxStreams.end())
return;
auto& codec = this->rtpParameters->GetRtxCodecForEncoding(encoding);
auto& codec = this->rtpParameters.GetRtxCodecForEncoding(encoding);
auto rtpStream = this->rtpStreams[ssrc];
rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc);
......@@ -587,11 +489,6 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Producer not enabled");
if (this->transport == nullptr)
return;
RTC::RTCP::FeedbackRtpNackPacket packet(0, rtpStream->GetSsrc());
auto it = seqNumbers.begin();
const auto end = seqNumbers.end();
......@@ -640,8 +537,6 @@ namespace RTC
{
MS_TRACE();
MS_ASSERT(IsEnabled(), "Producer not enabled");
RTC::RTCP::FeedbackPsPliPacket packet(0, rtpStream->GetSsrc());
packet.Serialize(RTC::RTCP::Buffer);
......
......@@ -219,6 +219,10 @@ namespace RTC
case Channel::Request::MethodId::ROUTER_CREATE_PRODUCER:
{
static const Json::StaticString JsonStringKind{ "kind" };
static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" };
static const Json::StaticString JsonStringRtpMapping{ "rtpMapping" };
static const Json::StaticString JsonStringCodecPayloadTypes{ "codecPayloadTypes" };
static const Json::StaticString JsonStringHeaderExtensionIds{ "headerExtensionIds" };
RTC::Producer* producer;
RTC::Transport* transport;
......@@ -260,17 +264,36 @@ namespace RTC
return;
}
// kind is mandatory.
if (!request->data[JsonStringKind].isString())
MS_THROW_ERROR("missing kind");
{
request->Reject("missing data.kind");
return;
}
else if (!request->data[JsonStringRtpParameters].isObject())
{
request->Reject("missing data.rtpParameters");
return;
}
else if (!request->data[JsonStringRtpMapping].isObject())
{
request->Reject("missing data.rtpMapping");
return;
}
RTC::Media::Kind kind;
std::string kindStr = request->data[JsonStringKind].asString();
auto kind = RTC::Media::GetKind(kindStr);
RTC::RtpParameters rtpParameters;
// Create a Producer instance.
try
{
producer = new RTC::Producer(this->notifier, producerId, kind, transport);
// NOTE: This may throw.
kind = RTC::Media::GetKind(kindStr);
// NOTE: This may throw.
rtpParameters = RTC::RtpParameters(request->data[JsonStringRtpParameters]);
}
catch (const MediaSoupError& error)
{
......@@ -279,25 +302,74 @@ namespace RTC
return;
}
if (kind == RTC::Media::Kind::ALL)
MS_THROW_ERROR("invalid empty kind");
RTC::Producer::RtpMapping rtpMapping;
try
{
auto& jsonRtpMapping = request->data[JsonStringRtpMapping];
if (!jsonRtpMapping[JsonStringCodecPayloadTypes].isArray())
MS_THROW_ERROR("missing rtpMapping.codecPayloadTypes");
else if (!jsonRtpMapping[JsonStringHeaderExtensionIds].isArray())
MS_THROW_ERROR("missing rtpMapping.headerExtensionIds");
for (auto& pair : jsonRtpMapping[JsonStringCodecPayloadTypes])
{
if (!pair.isArray() || pair.size() != 2 || !pair[0].isUInt() || !pair[1].isUInt())
MS_THROW_ERROR("wrong rtpMapping.codecPayloadTypes entry");
auto sourcePayloadType = static_cast<uint8_t>(pair[0].asUInt());
auto mappedPayloadType = static_cast<uint8_t>(pair[1].asUInt());
rtpMapping.codecPayloadTypes[sourcePayloadType] = mappedPayloadType;
}
for (auto& pair : jsonRtpMapping[JsonStringHeaderExtensionIds])
{
if (!pair.isArray() || pair.size() != 2 || !pair[0].isUInt() || !pair[1].isUInt())
MS_THROW_ERROR("wrong rtpMapping entry");
auto sourceHeaderExtensionId = static_cast<uint8_t>(pair[0].asUInt());
auto mappedHeaderExtensionId = static_cast<uint8_t>(pair[1].asUInt());
rtpMapping.headerExtensionIds[sourceHeaderExtensionId] = mappedHeaderExtensionId;
}
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
return;
}
// Create a Producer instance.
producer = new RTC::Producer(
this->notifier, producerId, kind, transport, rtpParameters, rtpMapping);
// Add us as listener.
producer->AddListener(this);
// Tell the Transport to handle the new Producer.
try
{
// Tell the Transport to handle the new Producer.
// NOTE: This may throw.
transport->HandleProducer(producer);
}
catch (const MediaSoupError& error)
{
delete producer;
request->Reject(error.what());
return;
}
// Insert into the maps.
this->producers[producerId] = producer;
// Ensure the entry will exist even with an empty array.
this->mapProducerConsumers[producer];
MS_DEBUG_DEV("Producer created [producerId:%" PRIu32 "]", producerId);
......@@ -309,6 +381,7 @@ namespace RTC
case Channel::Request::MethodId::ROUTER_CREATE_CONSUMER:
{
static const Json::StaticString JsonStringKind{ "kind" };
static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" };
RTC::Consumer* consumer;
RTC::Transport* transport;
......@@ -368,31 +441,31 @@ namespace RTC
return;
}
else if (!producer->GetParameters())
if (!request->data[JsonStringKind].isString())
{
request->Reject("Producer has no parameters");
request->Reject("missing data.kind");
return;
}
else if (!request->data[JsonStringRtpParameters].isObject())
{
request->Reject("missing data.rtpParameters");
// kind is mandatory.
if (!request->data[JsonStringKind].isString())
MS_THROW_ERROR("missing kind");
return;
}
RTC::Media::Kind kind;
std::string kindStr = request->data[JsonStringKind].asString();
auto kind = RTC::Media::GetKind(kindStr);
if (kind != producer->kind)
MS_THROW_ERROR("not matching kind");
RTC::RtpParameters rtpParameters;
// Create a Consumer instance.
try
{
consumer =
new RTC::Consumer(this->notifier, consumerId, kind, producer->producerId, transport);
// NOTE: This may throw.
kind = RTC::Media::GetKind(kindStr);
// Add us as listener.
consumer->AddListener(this);
// NOTE: This may throw.
rtpParameters = RTC::RtpParameters(request->data[JsonStringRtpParameters]);
}
catch (const MediaSoupError& error)
{
......@@ -401,10 +474,24 @@ namespace RTC
return;
}
if (kind == RTC::Media::Kind::ALL)
MS_THROW_ERROR("invalid empty kind");
else if (kind != producer->kind)
MS_THROW_ERROR("not matching kind");
consumer = new RTC::Consumer(
this->notifier, consumerId, kind, transport, rtpParameters, producer->producerId);
// Add us as listener.
consumer->AddListener(this);
// Tell the Transport to handle the new Consumer.
transport->HandleConsumer(consumer);
// Insert into the maps.
this->consumers[consumerId] = consumer;
this->mapProducerConsumers[producer].insert(consumer);
this->mapConsumerProducer[consumer] = producer;
MS_DEBUG_DEV("Consumer created [consumerId:%" PRIu32 "]", consumerId);
......@@ -532,7 +619,6 @@ namespace RTC
}
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:
......@@ -592,7 +678,6 @@ namespace RTC
}
case Channel::Request::MethodId::CONSUMER_DUMP:
case Channel::Request::MethodId::CONSUMER_SEND:
case Channel::Request::MethodId::CONSUMER_PAUSE:
case Channel::Request::MethodId::CONSUMER_RESUME:
{
......@@ -745,16 +830,6 @@ namespace RTC
this->mapProducerAudioLevelContainer.erase(producer);
}
void Router::OnProducerRtpParameters(RTC::Producer* producer)
{
MS_TRACE();
MS_ASSERT(producer->GetParameters(), "Producer has no parameters");
// Ensure the entry will exist even with an empty array.
this->mapProducerConsumers[producer];
}
void Router::OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet)
{
MS_TRACE();
......@@ -811,28 +886,6 @@ namespace RTC
this->mapConsumerProducer.erase(consumer);
}
void Router::OnConsumerRtpParameters(RTC::Consumer* consumer)
{
MS_TRACE();
MS_ASSERT(consumer->GetParameters(), "Consumer has no parameters");
// TODO: Fill the mapConsumerProducer, so we need the associated Producer id.
auto producerId = consumer->sourceProducerId;
MS_ASSERT(this->producers.find(producerId) != this->producers.end(), "Producer not found");
auto* producer = this->producers[producerId];
MS_ASSERT(
this->mapProducerConsumers.find(producer) != this->mapProducerConsumers.end(),
"Producer not present in mapProducerConsumers");
this->mapProducerConsumers[producer].insert(consumer);
this->mapConsumerProducer[consumer] = producer;
}
void Router::OnConsumerFullFrameRequired(RTC::Consumer* consumer)
{
MS_TRACE();
......
......@@ -60,9 +60,7 @@ namespace RTC
{
MS_TRACE();
auto rtpParameters = producer->GetParameters();
MS_ASSERT(rtpParameters, "no RtpParameters");
auto& rtpParameters = producer->GetParameters();
// Keep a copy of the previous entries so we can rollback.
......@@ -106,7 +104,7 @@ namespace RTC
// Add entries into the ssrcTable.
{
for (auto& encoding : rtpParameters->encodings)
for (auto& encoding : rtpParameters.encodings)
{
uint32_t ssrc;
......@@ -171,9 +169,9 @@ namespace RTC
// Add entries into muxIdTable.
{
if (!rtpParameters->muxId.empty())
if (!rtpParameters.muxId.empty())
{
auto& muxId = rtpParameters->muxId;
auto& muxId = rtpParameters.muxId;
if (!this->HasMuxId(muxId, producer))
{
......@@ -194,9 +192,9 @@ namespace RTC
// - Not all the encoding.rtx.ssrc are given, or
// - Not all the encoding.fec.ssrc are given.
{
auto it = rtpParameters->encodings.begin();
auto it = rtpParameters.encodings.begin();
for (; it != rtpParameters->encodings.end(); ++it)
for (; it != rtpParameters.encodings.end(); ++it)
{
auto& encoding = *it;
......@@ -207,9 +205,9 @@ namespace RTC
}
}
if (it != rtpParameters->encodings.end())
if (it != rtpParameters.encodings.end())
{
for (auto& codec : rtpParameters->codecs)
for (auto& codec : rtpParameters.codecs)
{
uint8_t payloadType = codec.payloadType;
......@@ -272,11 +270,11 @@ namespace RTC
if (it != this->ssrcTable.end())
{
auto producer = it->second;
auto rtpParameters = producer->GetParameters();
auto producer = it->second;
auto& rtpParameters = producer->GetParameters();
// Ensure the RTP PT is present in RtpParameters.
for (auto& codec : rtpParameters->codecs)
for (auto& codec : rtpParameters.codecs)
{
// Check payloads.
if (codec.payloadType == packet->GetPayloadType())
......
......@@ -601,6 +601,9 @@ namespace RTC
// Add to the map.
this->producers.insert(producer);
// Pass it to the RtpListener.
this->rtpListener.AddProducer(producer);
// Add us as listener.
producer->AddListener(this);
}
......@@ -1313,21 +1316,11 @@ namespace RTC
{
MS_TRACE();
// Remove from the map.
// Remove it from the map.
this->producers.erase(producer);
// If it was handled by the RtpListener, remove it.
if (producer->GetParameters())
this->rtpListener.RemoveProducer(producer);
}
void Transport::OnProducerRtpParameters(RTC::Producer* producer)
{
MS_ASSERT(producer->GetParameters(), "Producer has no parameters");
// Pass it to the RtpListener.
if (producer->GetParameters())
this->rtpListener.AddProducer(producer);
// Remove it from the RtpListener.
this->rtpListener.RemoveProducer(producer);
}
void Transport::OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet)
......@@ -1343,11 +1336,6 @@ namespace RTC
this->consumers.erase(consumer);
}
void Transport::OnConsumerRtpParameters(RTC::Consumer* consumer)
{
// Do nothing.
}
void Transport::OnConsumerFullFrameRequired(RTC::Consumer* consumer)
{
// Do nothing.
......
......@@ -249,7 +249,6 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R
case Channel::Request::MethodId::TRANSPORT_CHANGE_UFRAG_PWD:
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:
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论