Uniformized DataChannel stream type to uint16_t

This commit is contained in:
Paul-Louis Ageneau
2020-11-12 09:30:58 +01:00
parent 7a388bdffe
commit e7d45db210
4 changed files with 21 additions and 17 deletions

View File

@ -38,12 +38,12 @@ class PeerConnection;
class DataChannel : public std::enable_shared_from_this<DataChannel>, public Channel { class DataChannel : public std::enable_shared_from_this<DataChannel>, public Channel {
public: public:
DataChannel(std::weak_ptr<PeerConnection> pc, unsigned int stream, string label, DataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream, string label,
string protocol, Reliability reliability); string protocol, Reliability reliability);
virtual ~DataChannel(); virtual ~DataChannel();
uint16_t stream() const;
uint16_t id() const; uint16_t id() const;
unsigned int stream() const;
string label() const; string label() const;
string protocol() const; string protocol() const;
Reliability reliability() const; Reliability reliability() const;
@ -73,7 +73,7 @@ protected:
const std::weak_ptr<PeerConnection> mPeerConnection; const std::weak_ptr<PeerConnection> mPeerConnection;
std::weak_ptr<SctpTransport> mSctpTransport; std::weak_ptr<SctpTransport> mSctpTransport;
unsigned int mStream; uint16_t mStream;
string mLabel; string mLabel;
string mProtocol; string mProtocol;
std::shared_ptr<Reliability> mReliability; std::shared_ptr<Reliability> mReliability;
@ -89,10 +89,10 @@ private:
class NegociatedDataChannel final : public DataChannel { class NegociatedDataChannel final : public DataChannel {
public: public:
NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, unsigned int stream, string label, NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream, string label,
string protocol, Reliability reliability); string protocol, Reliability reliability);
NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, std::weak_ptr<SctpTransport> transport, NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, std::weak_ptr<SctpTransport> transport,
unsigned int stream); uint16_t stream);
~NegociatedDataChannel(); ~NegociatedDataChannel();
private: private:

View File

@ -178,7 +178,7 @@ private:
std::shared_ptr<DtlsTransport> mDtlsTransport; std::shared_ptr<DtlsTransport> mDtlsTransport;
std::shared_ptr<SctpTransport> mSctpTransport; std::shared_ptr<SctpTransport> mSctpTransport;
std::unordered_map<unsigned int, std::weak_ptr<DataChannel>> mDataChannels; // by stream ID std::unordered_map<uint16_t, std::weak_ptr<DataChannel>> mDataChannels; // by stream ID
std::unordered_map<string, std::weak_ptr<Track>> mTracks; // by mid std::unordered_map<string, std::weak_ptr<Track>> mTracks; // by mid
std::shared_mutex mDataChannelsMutex, mTracksMutex; std::shared_mutex mDataChannelsMutex, mTracksMutex;

View File

@ -72,7 +72,7 @@ struct CloseMessage {
}; };
#pragma pack(pop) #pragma pack(pop)
DataChannel::DataChannel(weak_ptr<PeerConnection> pc, unsigned int stream, string label, DataChannel::DataChannel(weak_ptr<PeerConnection> pc, uint16_t stream, string label,
string protocol, Reliability reliability) string protocol, Reliability reliability)
: mPeerConnection(pc), mStream(stream), mLabel(std::move(label)), : mPeerConnection(pc), mStream(stream), mLabel(std::move(label)),
mProtocol(std::move(protocol)), mProtocol(std::move(protocol)),
@ -81,9 +81,9 @@ DataChannel::DataChannel(weak_ptr<PeerConnection> pc, unsigned int stream, strin
DataChannel::~DataChannel() { close(); } DataChannel::~DataChannel() { close(); }
uint16_t DataChannel::id() const { return uint16_t(mStream); } uint16_t DataChannel::stream() const { return mStream; }
unsigned int DataChannel::stream() const { return mStream; } uint16_t DataChannel::id() const { return uint16_t(mStream); }
string DataChannel::label() const { return mLabel; } string DataChannel::label() const { return mLabel; }
@ -230,13 +230,13 @@ void DataChannel::incoming(message_ptr message) {
} }
} }
NegociatedDataChannel::NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, unsigned int stream, NegociatedDataChannel::NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream,
string label, string protocol, Reliability reliability) string label, string protocol, Reliability reliability)
: DataChannel(pc, stream, std::move(label), std::move(protocol), std::move(reliability)) {} : DataChannel(pc, stream, std::move(label), std::move(protocol), std::move(reliability)) {}
NegociatedDataChannel::NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, NegociatedDataChannel::NegociatedDataChannel(std::weak_ptr<PeerConnection> pc,
std::weak_ptr<SctpTransport> transport, std::weak_ptr<SctpTransport> transport,
unsigned int stream) uint16_t stream)
: DataChannel(pc, stream, "", "", {}) { : DataChannel(pc, stream, "", "", {}) {
mSctpTransport = transport; mSctpTransport = transport;
} }

View File

@ -645,7 +645,8 @@ void PeerConnection::forwardMessage(message_ptr message) {
return; return;
} }
auto channel = findDataChannel(uint16_t(message->stream)); uint16_t stream = uint16_t(message->stream);
auto channel = findDataChannel(stream);
if (!channel) { if (!channel) {
auto iceTransport = std::atomic_load(&mIceTransport); auto iceTransport = std::atomic_load(&mIceTransport);
auto sctpTransport = std::atomic_load(&mSctpTransport); auto sctpTransport = std::atomic_load(&mSctpTransport);
@ -653,9 +654,9 @@ void PeerConnection::forwardMessage(message_ptr message) {
return; return;
const byte dataChannelOpenMessage{0x03}; const byte dataChannelOpenMessage{0x03};
unsigned int remoteParity = (iceTransport->role() == Description::Role::Active) ? 1 : 0; uint16_t remoteParity = (iceTransport->role() == Description::Role::Active) ? 1 : 0;
if (message->type == Message::Control && *message->data() == dataChannelOpenMessage && if (message->type == Message::Control && *message->data() == dataChannelOpenMessage &&
message->stream % 2 == remoteParity) { stream % 2 == remoteParity) {
channel = std::make_shared<NegociatedDataChannel>(shared_from_this(), sctpTransport, channel = std::make_shared<NegociatedDataChannel>(shared_from_this(), sctpTransport,
message->stream); message->stream);
@ -733,18 +734,21 @@ void PeerConnection::forwardBufferedAmount(uint16_t stream, size_t amount) {
shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(Description::Role role, string label, shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(Description::Role role, string label,
DataChannelInit init) { DataChannelInit init) {
std::unique_lock lock(mDataChannelsMutex); // we are going to emplace std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
unsigned int stream; uint16_t stream;
if (init.id) { if (init.id) {
stream = *init.id; stream = *init.id;
if (stream == 65535)
throw std::runtime_error("Invalid DataChannel id");
} else { } else {
// The active side must use streams with even identifiers, whereas the passive side must use // The active side must use streams with even identifiers, whereas the passive side must use
// streams with odd identifiers. // streams with odd identifiers.
// See https://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-6 // See https://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-6
stream = (role == Description::Role::Active) ? 0 : 1; stream = (role == Description::Role::Active) ? 0 : 1;
while (mDataChannels.find(stream) != mDataChannels.end()) { while (mDataChannels.find(stream) != mDataChannels.end()) {
stream += 2; if (stream >= 65535 - 2)
if (stream >= 65535)
throw std::runtime_error("Too many DataChannels"); throw std::runtime_error("Too many DataChannels");
stream += 2;
} }
} }
// If the DataChannel is user-negociated, do not negociate it here // If the DataChannel is user-negociated, do not negociate it here