Parcourir la source

Uniformized DataChannel stream type to uint16_t

Paul-Louis Ageneau il y a 4 ans
Parent
commit
e7d45db210
4 fichiers modifiés avec 21 ajouts et 17 suppressions
  1. 5 5
      include/rtc/datachannel.hpp
  2. 1 1
      include/rtc/peerconnection.hpp
  3. 5 5
      src/datachannel.cpp
  4. 10 6
      src/peerconnection.cpp

+ 5 - 5
include/rtc/datachannel.hpp

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

+ 1 - 1
include/rtc/peerconnection.hpp

@@ -178,7 +178,7 @@ private:
 	std::shared_ptr<DtlsTransport> mDtlsTransport;
 	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::shared_mutex mDataChannelsMutex, mTracksMutex;
 

+ 5 - 5
src/datachannel.cpp

@@ -72,7 +72,7 @@ struct CloseMessage {
 };
 #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)
     : mPeerConnection(pc), mStream(stream), mLabel(std::move(label)),
       mProtocol(std::move(protocol)),
@@ -81,9 +81,9 @@ DataChannel::DataChannel(weak_ptr<PeerConnection> pc, unsigned int stream, strin
 
 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; }
 
@@ -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)
     : DataChannel(pc, stream, std::move(label), std::move(protocol), std::move(reliability)) {}
 
 NegociatedDataChannel::NegociatedDataChannel(std::weak_ptr<PeerConnection> pc,
                                              std::weak_ptr<SctpTransport> transport,
-                                             unsigned int stream)
+                                             uint16_t stream)
     : DataChannel(pc, stream, "", "", {}) {
 	mSctpTransport = transport;
 }

+ 10 - 6
src/peerconnection.cpp

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