Browse Source

Merge pull request #586 from paullouisageneau/fix-local-mid-collision

Prevent mid collision with local tracks when adding application mid
Paul-Louis Ageneau 3 years ago
parent
commit
977c660b51
1 changed files with 19 additions and 18 deletions
  1. 19 18
      src/impl/peerconnection.cpp

+ 19 - 18
src/impl/peerconnection.cpp

@@ -863,24 +863,6 @@ void PeerConnection::processLocalDescription(Description description) {
 
 
 	if (description.type() == Description::Type::Offer) {
 	if (description.type() == Description::Type::Offer) {
 		// This is an offer, add locally created data channels and tracks
 		// This is an offer, add locally created data channels and tracks
-		// Add application for data channels
-		if (!description.hasApplication()) {
-			std::shared_lock lock(mDataChannelsMutex);
-			if (!mDataChannels.empty()) {
-				unsigned int m = 0;
-				while (description.hasMid(std::to_string(m)))
-					++m;
-				Description::Application app(std::to_string(m));
-				app.setSctpPort(localSctpPort);
-				app.setMaxMessageSize(localMaxMessageSize);
-
-				PLOG_DEBUG << "Adding application to local description, mid=\"" << app.mid()
-				           << "\"";
-
-				description.addMedia(std::move(app));
-			}
-		}
-
 		// Add media for local tracks
 		// Add media for local tracks
 		std::shared_lock lock(mTracksMutex);
 		std::shared_lock lock(mTracksMutex);
 		for (auto it = mTrackLines.begin(); it != mTrackLines.end(); ++it) {
 		for (auto it = mTrackLines.begin(); it != mTrackLines.end(); ++it) {
@@ -901,6 +883,25 @@ void PeerConnection::processLocalDescription(Description description) {
 			}
 			}
 		}
 		}
 
 
+		// Add application for data channels
+		if (!description.hasApplication()) {
+			std::shared_lock lock(mDataChannelsMutex);
+			if (!mDataChannels.empty()) {
+				// Prevents mid collision with remote or local tracks
+				unsigned int m = 0;
+				while (description.hasMid(std::to_string(m)))
+					++m;
+				Description::Application app(std::to_string(m));
+				app.setSctpPort(localSctpPort);
+				app.setMaxMessageSize(localMaxMessageSize);
+
+				PLOG_DEBUG << "Adding application to local description, mid=\"" << app.mid()
+				           << "\"";
+
+				description.addMedia(std::move(app));
+			}
+		}
+
 		// There might be no media at this point if the user created a Track, deleted it,
 		// There might be no media at this point if the user created a Track, deleted it,
 		// then called setLocalDescription().
 		// then called setLocalDescription().
 		if (description.mediaCount() == 0)
 		if (description.mediaCount() == 0)