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

RTP works...

上级 f0db9781
{ {
"name": "mediasoup", "name": "mediasoup",
"version": "0.1.0-pre9", "version": "0.1.0-pre10",
"description": "Powerful WebRTC SFU for Node.js", "description": "Powerful WebRTC SFU for Node.js",
"main": "lib/mediasoup.js", "main": "lib/mediasoup.js",
"repository": { "repository": {
......
...@@ -147,18 +147,20 @@ tap.test('rtpReceiver.receive() with full rtpParameters must succeed', { timeout ...@@ -147,18 +147,20 @@ tap.test('rtpReceiver.receive() with full rtpParameters must succeed', { timeout
maxptime : 80, maxptime : 80,
ptime : 60, ptime : 60,
numChannels : 2, numChannels : 2,
rtx : // TODO: uncomment when RTX and RTCP implemented
{ rtcpFeedback : [],
payloadType : 96, // rtx :
rtxTime : 500 // {
}, // payloadType : 96,
rtcpFeedback : // rtxTime : 500
[ // },
{ type: 'ccm', parameter: 'fir' }, // rtcpFeedback :
{ type: 'nack', parameter: '' }, // [
{ type: 'nack', parameter: 'pli' }, // { type: 'ccm', parameter: 'fir' },
{ type: 'google-remb', parameter: '' } // { type: 'nack', parameter: '' },
], // { type: 'nack', parameter: 'pli' },
// { type: 'google-remb', parameter: '' }
// ],
parameters : parameters :
{ {
profileLevelId : 2, profileLevelId : 2,
...@@ -448,26 +450,27 @@ tap.test('rtpReceiver.receive() with wrong codecs must fail', { timeout: 2000 }, ...@@ -448,26 +450,27 @@ tap.test('rtpReceiver.receive() with wrong codecs must fail', { timeout: 2000 },
}); });
}); });
funcs.push(function() // TODO: uncomment when RTX implemented
{ // funcs.push(function()
return rtpReceiver.receive( // {
{ // return rtpReceiver.receive(
codecs : // {
[ // codecs :
{ // [
name : 'audio/opus', // {
payloadType : 100, // name : 'audio/opus',
clockRate : 48000, // payloadType : 100,
rtx : {} // clockRate : 48000,
} // rtx : {}
] // }
}) // ]
.then(() => t.fail('rtpReceiver.receive() succeeded')) // })
.catch((error) => // .then(() => t.fail('rtpReceiver.receive() succeeded'))
{ // .catch((error) =>
t.pass(`rtpReceiver.receive() without codec.rtx.payloadType failed: ${error}`); // {
}); // t.pass(`rtpReceiver.receive() without codec.rtx.payloadType failed: ${error}`);
}); // });
// });
funcs.push(function() funcs.push(function()
{ {
...@@ -494,34 +497,35 @@ tap.test('rtpReceiver.receive() with wrong codecs must fail', { timeout: 2000 }, ...@@ -494,34 +497,35 @@ tap.test('rtpReceiver.receive() with wrong codecs must fail', { timeout: 2000 },
}); });
}); });
funcs.push(function() // TODO: uncomment when RTX implemented
{ // funcs.push(function()
return rtpReceiver.receive( // {
{ // return rtpReceiver.receive(
codecs : // {
[ // codecs :
{ // [
name : 'audio/opus', // {
payloadType : 100, // name : 'audio/opus',
clockRate : 48000, // payloadType : 100,
rtx : // clockRate : 48000,
{ // rtx :
payloadType : 0 // {
} // payloadType : 0
}, // }
{ // },
name : 'audio/PCMU', // {
payloadType : 0, // name : 'audio/PCMU',
clockRate : 8000 // payloadType : 0,
} // clockRate : 8000
] // }
}) // ]
.then(() => t.fail('rtpReceiver.receive() succeeded')) // })
.catch((error) => // .then(() => t.fail('rtpReceiver.receive() succeeded'))
{ // .catch((error) =>
t.pass(`rtpReceiver.receive() with duplicated RTX payloadType failed: ${error}`); // {
}); // t.pass(`rtpReceiver.receive() with duplicated RTX payloadType failed: ${error}`);
}); // });
// });
return promiseSeries(funcs); return promiseSeries(funcs);
}); });
......
...@@ -49,7 +49,7 @@ namespace RTC ...@@ -49,7 +49,7 @@ namespace RTC
* @param rtpSender Instance of RtpSender. * @param rtpSender Instance of RtpSender.
* @param peerName Name of the receiver Peer. * @param peerName Name of the receiver Peer.
*/ */
void AddRtpSender(RTC::RtpSender* rtpSender, std::string& peerName); void AddRtpSender(RTC::RtpSender* rtpSender, std::string& peerName, RTC::RtpParameters* rtpParameters);
private: private:
RTC::Transport* GetTransportFromRequest(Channel::Request* request, uint32_t* transportId = nullptr); RTC::Transport* GetTransportFromRequest(Channel::Request* request, uint32_t* transportId = nullptr);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "RTC/RtcpPacket.h" #include "RTC/RtcpPacket.h"
#include "Channel/Request.h" #include "Channel/Request.h"
#include "Channel/Notifier.h" #include "Channel/Notifier.h"
#include <unordered_map>
#include <json/json.h> #include <json/json.h>
namespace RTC namespace RTC
...@@ -38,6 +39,9 @@ namespace RTC ...@@ -38,6 +39,9 @@ namespace RTC
RTC::RtpParameters* GetParameters(); RTC::RtpParameters* GetParameters();
void SendRtpPacket(RTC::RtpPacket* packet); void SendRtpPacket(RTC::RtpPacket* packet);
private:
void SetPayloadTypesMapping();
public: public:
// Passed by argument. // Passed by argument.
uint32_t rtpSenderId; uint32_t rtpSenderId;
...@@ -52,6 +56,7 @@ namespace RTC ...@@ -52,6 +56,7 @@ namespace RTC
// Allocated by this. // Allocated by this.
RTC::RtpParameters* rtpParameters = nullptr; RTC::RtpParameters* rtpParameters = nullptr;
// Others. // Others.
std::unordered_map<uint8_t, uint8_t> mapPayloadTypes;
// Whether this RtpSender is valid according to Peer capabilities. // Whether this RtpSender is valid according to Peer capabilities.
bool available = false; bool available = false;
}; };
...@@ -76,13 +81,6 @@ namespace RTC ...@@ -76,13 +81,6 @@ namespace RTC
{ {
return this->rtpParameters; return this->rtpParameters;
} }
inline
void RtpSender::SendRtpPacket(RTC::RtpPacket* packet)
{
if (this->available && this->transport)
this->transport->SendRtpPacket(packet);
}
} }
#endif #endif
...@@ -438,7 +438,7 @@ namespace RTC ...@@ -438,7 +438,7 @@ namespace RTC
} }
} }
void Peer::AddRtpSender(RTC::RtpSender* rtpSender, std::string& peerName) void Peer::AddRtpSender(RTC::RtpSender* rtpSender, std::string& peerName, RTC::RtpParameters* rtpParameters)
{ {
MS_TRACE(); MS_TRACE();
...@@ -448,11 +448,12 @@ namespace RTC ...@@ -448,11 +448,12 @@ namespace RTC
MS_ASSERT(this->rtpSenders.find(rtpSender->rtpSenderId) == this->rtpSenders.end(), MS_ASSERT(this->rtpSenders.find(rtpSender->rtpSenderId) == this->rtpSenders.end(),
"given RtpSender already exists in this Peer"); "given RtpSender already exists in this Peer");
MS_ASSERT(rtpSender->GetParameters(), "given RtpSender does not have RtpParameters");
// Provide the RtpSender with peer's capabilities. // Provide the RtpSender with peer's capabilities.
rtpSender->SetPeerCapabilities(std::addressof(this->capabilities)); rtpSender->SetPeerCapabilities(std::addressof(this->capabilities));
// Provide the RtpSender with the received RTP parameters.
rtpSender->Send(rtpParameters);
// Store it. // Store it.
this->rtpSenders[rtpSender->rtpSenderId] = rtpSender; this->rtpSenders[rtpSender->rtpSenderId] = rtpSender;
......
...@@ -438,11 +438,11 @@ namespace RTC ...@@ -438,11 +438,11 @@ namespace RTC
// Store into the map. // Store into the map.
this->mapRtpReceiverRtpSenders[rtpReceiver].insert(rtpSender); this->mapRtpReceiverRtpSenders[rtpReceiver].insert(rtpSender);
// Attach the RtpSender to peer.
peer->AddRtpSender(rtpSender, receiver_peer->peerName, rtpReceiver->GetParameters());
// Provide the RtpSender with the parameters of the RtpReceiver. // Provide the RtpSender with the parameters of the RtpReceiver.
rtpSender->Send(rtpReceiver->GetParameters()); rtpSender->Send(rtpReceiver->GetParameters());
// Attach the RtpSender to peer.
peer->AddRtpSender(rtpSender, receiver_peer->peerName);
} }
} }
} }
...@@ -506,11 +506,8 @@ namespace RTC ...@@ -506,11 +506,8 @@ namespace RTC
// Store into the map. // Store into the map.
this->mapRtpReceiverRtpSenders[rtpReceiver].insert(rtpSender); this->mapRtpReceiverRtpSenders[rtpReceiver].insert(rtpSender);
// Provide the RtpSender with the parameters of the RtpReceiver.
rtpSender->Send(rtpReceiver->GetParameters());
// Attach the RtpSender to sender_peer. // Attach the RtpSender to sender_peer.
sender_peer->AddRtpSender(rtpSender, peer->peerName); sender_peer->AddRtpSender(rtpSender, peer->peerName, rtpReceiver->GetParameters());
} }
} }
// If this is not a new RtpReceiver let's retrieve its updated parameters // If this is not a new RtpReceiver let's retrieve its updated parameters
......
...@@ -85,33 +85,33 @@ namespace RTC ...@@ -85,33 +85,33 @@ namespace RTC
if (data[k_parameters].isObject()) if (data[k_parameters].isObject())
this->parameters.Set(data[k_parameters]); this->parameters.Set(data[k_parameters]);
if (this->scope == RTC::Scope::RECEIVE) // if (this->scope == RTC::Scope::RECEIVE)
{ // {
// `rtx` is optional. // // `rtx` is optional.
if (data[k_rtx].isObject()) // if (data[k_rtx].isObject())
{ // {
this->rtx = RTC::RTCRtpCodecRtxParameters(data[k_rtx]); // this->rtx = RTC::RTCRtpCodecRtxParameters(data[k_rtx]);
this->hasRtx = true; // this->hasRtx = true;
} // }
} // }
if (this->scope == RTC::Scope::PEER_CAPABILITY || // if (this->scope == RTC::Scope::PEER_CAPABILITY ||
this->scope == RTC::Scope::RECEIVE) // this->scope == RTC::Scope::RECEIVE)
{ // {
// `rtcpFeedback` is optional. // // `rtcpFeedback` is optional.
if (data[k_rtcpFeedback].isArray()) // if (data[k_rtcpFeedback].isArray())
{ // {
auto& json_rtcpFeedback = data[k_rtcpFeedback]; // auto& json_rtcpFeedback = data[k_rtcpFeedback];
for (Json::UInt i = 0; i < json_rtcpFeedback.size(); i++) // for (Json::UInt i = 0; i < json_rtcpFeedback.size(); i++)
{ // {
RTC::RtcpFeedback rtcpFeedback(json_rtcpFeedback[i]); // RTC::RtcpFeedback rtcpFeedback(json_rtcpFeedback[i]);
// Append to the rtcpFeedback vector. // // Append to the rtcpFeedback vector.
this->rtcpFeedback.push_back(rtcpFeedback); // this->rtcpFeedback.push_back(rtcpFeedback);
} // }
} // }
} // }
// Check codec. // Check codec.
CheckCodec(); CheckCodec();
......
...@@ -52,6 +52,8 @@ namespace RTC ...@@ -52,6 +52,8 @@ namespace RTC
static const Json::StaticString k_rtpParameters("rtpParameters"); static const Json::StaticString k_rtpParameters("rtpParameters");
static const Json::StaticString k_hasTransport("hasTransport"); static const Json::StaticString k_hasTransport("hasTransport");
static const Json::StaticString k_available("available"); static const Json::StaticString k_available("available");
// TODO: TMP
static const Json::StaticString k_mapPayloadTypes("mapPayloadTypes");
Json::Value json(Json::objectValue); Json::Value json(Json::objectValue);
...@@ -68,6 +70,13 @@ namespace RTC ...@@ -68,6 +70,13 @@ namespace RTC
json[k_available] = this->available; json[k_available] = this->available;
// TODO: TMP
json[k_mapPayloadTypes] = Json::objectValue;
for (auto& kv : this->mapPayloadTypes)
{
json[k_mapPayloadTypes][std::to_string(kv.first)] = kv.second;
}
return json; return json;
} }
...@@ -99,6 +108,8 @@ namespace RTC ...@@ -99,6 +108,8 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
MS_ASSERT(peerCapabilities, "given peerCapabilities are null");
this->peerCapabilities = peerCapabilities; this->peerCapabilities = peerCapabilities;
} }
...@@ -110,6 +121,8 @@ namespace RTC ...@@ -110,6 +121,8 @@ namespace RTC
static const Json::StaticString k_rtpParameters("rtpParameters"); static const Json::StaticString k_rtpParameters("rtpParameters");
static const Json::StaticString k_available("available"); static const Json::StaticString k_available("available");
MS_ASSERT(rtpParameters, "no RTP parameters given");
auto previousRtpParameters = this->rtpParameters; auto previousRtpParameters = this->rtpParameters;
// Free the previous rtpParameters. // Free the previous rtpParameters.
...@@ -119,6 +132,9 @@ namespace RTC ...@@ -119,6 +132,9 @@ namespace RTC
// Clone given RTP parameters so we manage our own sender parameters. // Clone given RTP parameters so we manage our own sender parameters.
this->rtpParameters = new RTC::RtpParameters(rtpParameters); this->rtpParameters = new RTC::RtpParameters(rtpParameters);
// Build the payload types map.
SetPayloadTypesMapping();
// TODO: Must check new parameters and: // TODO: Must check new parameters and:
// - remove unsuported capabilities, // - remove unsuported capabilities,
// - set this->available if at least there is a capable encoding. // - set this->available if at least there is a capable encoding.
...@@ -137,4 +153,88 @@ namespace RTC ...@@ -137,4 +153,88 @@ namespace RTC
this->notifier->Emit(this->rtpSenderId, "parameterschange", event_data); this->notifier->Emit(this->rtpSenderId, "parameterschange", event_data);
} }
} }
void RtpSender::SendRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
if (!this->available || !this->transport)
return;
// Map the payload type.
uint8_t originalPayloadType = packet->GetPayloadType();
uint8_t mappedPayloadType;
auto it = this->mapPayloadTypes.find(originalPayloadType);
// TODO: This should never happen.
if (it == this->mapPayloadTypes.end())
{
MS_ERROR("payload type not mapped [payloadType:%" PRIu8 "]",
originalPayloadType);
return;
}
mappedPayloadType = this->mapPayloadTypes[originalPayloadType];
// Map the packet payload type.
packet->SetPayloadType(mappedPayloadType);
// Send the packet.
this->transport->SendRtpPacket(packet);
// Recover the original packet payload type.
packet->SetPayloadType(originalPayloadType);
}
void RtpSender::SetPayloadTypesMapping()
{
MS_TRACE();
MS_ASSERT(this->peerCapabilities, "peer RTP capabilities are null");
this->mapPayloadTypes.clear();
for (auto& codec : this->rtpParameters->codecs)
{
auto it = this->peerCapabilities->codecs.begin();
for (; it != this->peerCapabilities->codecs.end(); ++it)
{
auto& codecCapability = *it;
if (codecCapability.Matches(codec))
{
auto originalPayloadType = codec.payloadType;
auto mappedPayloadType = codecCapability.payloadType;
// Set the mapping.
this->mapPayloadTypes[originalPayloadType] = mappedPayloadType;
// Override the codec payload type.
codec.payloadType = mappedPayloadType;
// Override encoding.codecPayloadType.
for (auto& encoding : this->rtpParameters->encodings)
{
// TODO: remove when confirmed
MS_ASSERT(encoding.hasCodecPayloadType, "encoding without codecPayloadType");
if (encoding.codecPayloadType == originalPayloadType)
encoding.codecPayloadType = mappedPayloadType;
}
break;
}
}
if (it == this->peerCapabilities->codecs.end())
{
// TODO: Let's see. Theoretically unsupported codecs has been
// already removed in Send(), but it's not done yet.
// Once done, this should be a MS_THROW.
MS_ERROR("no matching room codec found [payloadType:%" PRIu8 "]",
codec.payloadType);
}
}
}
} }
...@@ -138,6 +138,7 @@ namespace RTC ...@@ -138,6 +138,7 @@ namespace RTC
err = srtp_protect(this->session, (void*)SrtpSession::encryptBuffer, (int*)len); err = srtp_protect(this->session, (void*)SrtpSession::encryptBuffer, (int*)len);
if (DepLibSRTP::IsError(err)) if (DepLibSRTP::IsError(err))
{ {
// TODO
MS_WARN("srtp_protect() failed: %s", DepLibSRTP::GetErrorString(err)); MS_WARN("srtp_protect() failed: %s", DepLibSRTP::GetErrorString(err));
return false; return false;
...@@ -158,6 +159,7 @@ namespace RTC ...@@ -158,6 +159,7 @@ namespace RTC
err = srtp_unprotect(this->session, (void*)data, (int*)len); err = srtp_unprotect(this->session, (void*)data, (int*)len);
if (DepLibSRTP::IsError(err)) if (DepLibSRTP::IsError(err))
{ {
// TODO
MS_WARN("srtp_unprotect() failed: %s", DepLibSRTP::GetErrorString(err)); MS_WARN("srtp_unprotect() failed: %s", DepLibSRTP::GetErrorString(err));
return false; return false;
...@@ -185,6 +187,7 @@ namespace RTC ...@@ -185,6 +187,7 @@ namespace RTC
err = srtp_protect_rtcp(this->session, (void*)SrtpSession::encryptBuffer, (int*)len); err = srtp_protect_rtcp(this->session, (void*)SrtpSession::encryptBuffer, (int*)len);
if (DepLibSRTP::IsError(err)) if (DepLibSRTP::IsError(err))
{ {
// TODO
MS_WARN("srtp_protect_rtcp() failed: %s", DepLibSRTP::GetErrorString(err)); MS_WARN("srtp_protect_rtcp() failed: %s", DepLibSRTP::GetErrorString(err));
return false; return false;
...@@ -205,6 +208,7 @@ namespace RTC ...@@ -205,6 +208,7 @@ namespace RTC
err = srtp_unprotect_rtcp(this->session, (void*)data, (int*)len); err = srtp_unprotect_rtcp(this->session, (void*)data, (int*)len);
if (DepLibSRTP::IsError(err)) if (DepLibSRTP::IsError(err))
{ {
// TODO
MS_WARN("srtp_unprotect_rtcp() failed: %s", DepLibSRTP::GetErrorString(err)); MS_WARN("srtp_unprotect_rtcp() failed: %s", DepLibSRTP::GetErrorString(err));
return false; return false;
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论