Bläddra i källkod

Always enable RTP MID header extension

Paul-Louis Ageneau 1 år sedan
förälder
incheckning
330acf9eab
4 ändrade filer med 38 tillägg och 4 borttagningar
  1. 3 1
      include/rtc/description.hpp
  2. 20 1
      src/description.cpp
  3. 14 2
      src/impl/track.cpp
  4. 1 0
      src/impl/track.hpp

+ 3 - 1
include/rtc/description.hpp

@@ -118,7 +118,9 @@ public:
 			Direction direction = Direction::Unknown;
 		};
 
-		std::vector<int> extIds();
+		std::vector<int> extIds() const;
+		optional<int> findExtId(string_view uri) const;
+		int nextExtId() const;
 		ExtMap *extMap(int id);
 		const ExtMap *extMap(int id) const;
 		void addExtMap(ExtMap map);

+ 20 - 1
src/description.cpp

@@ -583,7 +583,7 @@ void Description::removeAttribute(const string &attr) {
 	    mAttributes.end());
 }
 
-std::vector<int> Description::Entry::extIds() {
+std::vector<int> Description::Entry::extIds() const {
 	std::vector<int> result;
 	for (auto it = mExtMaps.begin(); it != mExtMaps.end(); ++it)
 		result.push_back(it->first);
@@ -591,6 +591,25 @@ std::vector<int> Description::Entry::extIds() {
 	return result;
 }
 
+int Description::Entry::nextExtId() const {
+	// RFC 8285: In the signaling section, the range 1-256 is referred to as the valid range, with
+	// the values 1-255 referring to extension elements and the value 256 referring to the 4-bit
+	// appbits field.
+	for (int i = 1; i < 255; ++i)
+		if (mExtMaps.find(i) == mExtMaps.end())
+			return i;
+
+	throw std::out_of_range("No more valid extension ID available");
+}
+
+optional<int> Description::Entry::findExtId(string_view uri) const {
+	for (auto it = mExtMaps.begin(); it != mExtMaps.end(); ++it)
+		if(it->second.uri == uri)
+			return std::make_optional(it->first);
+
+	return nullopt;
+}
+
 Description::Entry::ExtMap *Description::Entry::extMap(int id) {
 	auto it = mExtMaps.find(id);
 	if (it == mExtMaps.end())

+ 14 - 2
src/impl/track.cpp

@@ -20,9 +20,11 @@ static LogCounter COUNTER_QUEUE_FULL(plog::warning,
                                      "Number of media packets dropped due to a full queue");
 
 Track::Track(weak_ptr<PeerConnection> pc, Description::Media desc)
-    : mPeerConnection(pc), mMediaDescription(std::move(desc)),
+    : mPeerConnection(pc),
       mRecvQueue(RECV_QUEUE_LIMIT, [](const message_ptr &m) { return m->size(); }) {
 
+	setDescription(std::move(desc));
+
 	// Discard messages by default if track is send only
 	if (mMediaDescription.direction() == Description::Direction::SendOnly)
 		messageCallback = [](message_variant) {};
@@ -58,6 +60,16 @@ void Track::setDescription(Description::Media desc) {
 		if (desc.mid() != mMediaDescription.mid())
 			throw std::logic_error("Media description mid does not match track mid");
 
+		// RFC 8843: The RTP MID header extension MUST be enabled, by including an SDP 'extmap'
+		// attribute [RFC8285], with a 'urn:ietf:params:rtp-hdrext:sdes:mid' URI value, in each
+		// bundled RTP-based "m=" section in every offer and answer.
+		const string sdesMidExtUri = "urn:ietf:params:rtp-hdrext:sdes:mid";
+		mSdesMidExtId = desc.findExtId(sdesMidExtUri);
+		if (!mSdesMidExtId) {
+			mSdesMidExtId.emplace(desc.nextExtId());
+			desc.addExtMap({*mSdesMidExtId, sdesMidExtUri});
+		}
+
 		mMediaDescription = std::move(desc);
 	}
 
@@ -217,7 +229,7 @@ void Track::setMediaHandler(shared_ptr<MediaHandler> handler) {
 		mMediaHandler = handler;
 	}
 
-	if(handler)
+	if (handler)
 		handler->media(description());
 }
 

+ 1 - 0
src/impl/track.hpp

@@ -64,6 +64,7 @@ private:
 #endif
 
 	Description::Media mMediaDescription;
+	optional<int> mSdesMidExtId;
 	shared_ptr<MediaHandler> mMediaHandler;
 
 	mutable std::shared_mutex mMutex;