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

RTP works...

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