If MSID is encoded in both ways, make the SSRC-level one take priority.

BUG=webrtc:5264
R=pthatcher@webrtc.org

Review URL: https://codereview.webrtc.org/1762003003 .

Cr-Commit-Position: refs/heads/master@{#11933}
diff --git a/webrtc/api/webrtcsdp.cc b/webrtc/api/webrtcsdp.cc
index 2ee0747..54bb059 100644
--- a/webrtc/api/webrtcsdp.cc
+++ b/webrtc/api/webrtcsdp.cc
@@ -212,11 +212,6 @@
 const int kWildcardPayloadType = -1;
 
 struct SsrcInfo {
-  SsrcInfo()
-      : stream_id(kDefaultMsid),
-        // TODO(ronghuawu): What should we do if the track id doesn't appear?
-        // Create random string (which will be used as track label later)?
-        track_id(rtc::CreateRandomString(8)) {}
   uint32_t ssrc_id;
   std::string cname;
   std::string stream_id;
@@ -573,30 +568,47 @@
       cricket::IsValidRtpPayloadType(*payload_type);
 }
 
+// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
+// "a=msid" attribute, if it exists. They are empty if the attribute does not
+// exist.
 void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
+                               const std::string& msid_stream_id,
+                               const std::string& msid_track_id,
                                StreamParamsVec* tracks) {
   ASSERT(tracks != NULL);
+  ASSERT(msid_stream_id.empty() == msid_track_id.empty());
   for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
        ssrc_info != ssrc_infos.end(); ++ssrc_info) {
     if (ssrc_info->cname.empty()) {
       continue;
     }
 
-    std::string sync_label;
+    std::string stream_id;
     std::string track_id;
-    if (ssrc_info->stream_id == kDefaultMsid && !ssrc_info->mslabel.empty()) {
+    if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
       // If there's no msid and there's mslabel, we consider this is a sdp from
       // a older version of client that doesn't support msid.
       // In that case, we use the mslabel and label to construct the track.
-      sync_label = ssrc_info->mslabel;
+      stream_id = ssrc_info->mslabel;
       track_id = ssrc_info->label;
+    } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
+      // If there's no msid in the SSRC attributes, but there's a global one
+      // (from a=msid), use that. This is the case with unified plan SDP.
+      stream_id = msid_stream_id;
+      track_id = msid_track_id;
     } else {
-      sync_label = ssrc_info->stream_id;
+      stream_id = ssrc_info->stream_id;
       track_id = ssrc_info->track_id;
     }
-    if (sync_label.empty() || track_id.empty()) {
-      ASSERT(false);
-      continue;
+    // If a stream/track ID wasn't populated from the SSRC attributes OR the
+    // msid attribute, use default/random values.
+    if (stream_id.empty()) {
+      stream_id = kDefaultMsid;
+    }
+    if (track_id.empty()) {
+      // TODO(ronghuawu): What should we do if the track id doesn't appear?
+      // Create random string (which will be used as track label later)?
+      track_id = rtc::CreateRandomString(8);
     }
 
     StreamParamsVec::iterator track = tracks->begin();
@@ -612,7 +624,7 @@
     }
     track->add_ssrc(ssrc_info->ssrc_id);
     track->cname = ssrc_info->cname;
-    track->sync_label = sync_label;
+    track->sync_label = stream_id;
     track->id = track_id;
   }
 }
@@ -2670,19 +2682,11 @@
     }
   }
 
-  // Found an msid attribute.
-  // Setting the stream_id/track_id will cause only one StreamParams
-  // to be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
-  // the m= section.
-  if (!stream_id.empty() && !track_id.empty()) {
-    for (SsrcInfo& ssrc_info : ssrc_infos) {
-      ssrc_info.stream_id = stream_id;
-      ssrc_info.track_id = track_id;
-    }
-  }
-
   // Create tracks from the |ssrc_infos|.
-  CreateTracksFromSsrcInfos(ssrc_infos, &tracks);
+  // If the stream_id/track_id for all SSRCS are identical, one StreamParams
+  // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
+  // the m= section.
+  CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
 
   // Add the ssrc group to the track.
   for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
diff --git a/webrtc/api/webrtcsdp_unittest.cc b/webrtc/api/webrtcsdp_unittest.cc
index 39b7cdd..9559701 100644
--- a/webrtc/api/webrtcsdp_unittest.cc
+++ b/webrtc/api/webrtcsdp_unittest.cc
@@ -412,6 +412,91 @@
     "a=ssrc:6 mslabel:local_stream_2\r\n"
     "a=ssrc:6 label:video_track_id_3\r\n";
 
+// Plan B SDP reference string, with 2 streams, 2 audio tracks and 3 video
+// tracks, but with the unified plan "a=msid" attribute.
+static const char kPlanBSdpFullStringWithMsid[] =
+    "v=0\r\n"
+    "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
+    "s=-\r\n"
+    "t=0 0\r\n"
+    "a=msid-semantic: WMS local_stream_1 local_stream_2\r\n"
+    "m=audio 2345 RTP/SAVPF 111 103 104\r\n"
+    "c=IN IP4 74.125.127.126\r\n"
+    "a=rtcp:2347 IN IP4 74.125.127.126\r\n"
+    "a=candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1234 typ host "
+    "generation 2\r\n"
+    "a=candidate:a0+B/1 2 udp 2130706432 192.168.1.5 1235 typ host "
+    "generation 2\r\n"
+    "a=candidate:a0+B/2 1 udp 2130706432 ::1 1238 typ host "
+    "generation 2\r\n"
+    "a=candidate:a0+B/2 2 udp 2130706432 ::1 1239 typ host "
+    "generation 2\r\n"
+    "a=candidate:a0+B/3 1 udp 2130706432 74.125.127.126 2345 typ srflx "
+    "raddr 192.168.1.5 rport 2346 "
+    "generation 2\r\n"
+    "a=candidate:a0+B/3 2 udp 2130706432 74.125.127.126 2347 typ srflx "
+    "raddr 192.168.1.5 rport 2348 "
+    "generation 2\r\n"
+    "a=ice-ufrag:ufrag_voice\r\na=ice-pwd:pwd_voice\r\n"
+    "a=mid:audio_content_name\r\n"
+    "a=msid:local_stream_1 audio_track_id_1\r\n"
+    "a=sendrecv\r\n"
+    "a=rtcp-mux\r\n"
+    "a=rtcp-rsize\r\n"
+    "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
+    "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
+    "dummy_session_params\r\n"
+    "a=rtpmap:111 opus/48000/2\r\n"
+    "a=rtpmap:103 ISAC/16000\r\n"
+    "a=rtpmap:104 ISAC/32000\r\n"
+    "a=ssrc:1 cname:stream_1_cname\r\n"
+    "a=ssrc:1 msid:local_stream_1 audio_track_id_1\r\n"
+    "a=ssrc:1 mslabel:local_stream_1\r\n"
+    "a=ssrc:1 label:audio_track_id_1\r\n"
+    "a=ssrc:4 cname:stream_2_cname\r\n"
+    "a=ssrc:4 msid:local_stream_2 audio_track_id_2\r\n"
+    "a=ssrc:4 mslabel:local_stream_2\r\n"
+    "a=ssrc:4 label:audio_track_id_2\r\n"
+    "m=video 3457 RTP/SAVPF 120\r\n"
+    "c=IN IP4 74.125.224.39\r\n"
+    "a=rtcp:3456 IN IP4 74.125.224.39\r\n"
+    "a=candidate:a0+B/1 2 udp 2130706432 192.168.1.5 1236 typ host "
+    "generation 2\r\n"
+    "a=candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1237 typ host "
+    "generation 2\r\n"
+    "a=candidate:a0+B/2 2 udp 2130706432 ::1 1240 typ host "
+    "generation 2\r\n"
+    "a=candidate:a0+B/2 1 udp 2130706432 ::1 1241 typ host "
+    "generation 2\r\n"
+    "a=candidate:a0+B/4 2 udp 2130706432 74.125.224.39 3456 typ relay "
+    "generation 2\r\n"
+    "a=candidate:a0+B/4 1 udp 2130706432 74.125.224.39 3457 typ relay "
+    "generation 2\r\n"
+    "a=ice-ufrag:ufrag_video\r\na=ice-pwd:pwd_video\r\n"
+    "a=mid:video_content_name\r\n"
+    "a=msid:local_stream_1 video_track_id_1\r\n"
+    "a=sendrecv\r\n"
+    "a=crypto:1 AES_CM_128_HMAC_SHA1_80 "
+    "inline:d0RmdmcmVCspeEc3QGZiNWpVLFJhQX1cfHAwJSoj|2^20|1:32\r\n"
+    "a=rtpmap:120 VP8/90000\r\n"
+    "a=ssrc-group:FEC 2 3\r\n"
+    "a=ssrc:2 cname:stream_1_cname\r\n"
+    "a=ssrc:2 msid:local_stream_1 video_track_id_1\r\n"
+    "a=ssrc:2 mslabel:local_stream_1\r\n"
+    "a=ssrc:2 label:video_track_id_1\r\n"
+    "a=ssrc:3 cname:stream_1_cname\r\n"
+    "a=ssrc:3 msid:local_stream_1 video_track_id_1\r\n"
+    "a=ssrc:3 mslabel:local_stream_1\r\n"
+    "a=ssrc:3 label:video_track_id_1\r\n"
+    "a=ssrc:5 cname:stream_2_cname\r\n"
+    "a=ssrc:5 msid:local_stream_2 video_track_id_2\r\n"
+    "a=ssrc:5 mslabel:local_stream_2\r\n"
+    "a=ssrc:5 label:video_track_id_2\r\n"
+    "a=ssrc:6 cname:stream_2_cname\r\n"
+    "a=ssrc:6 msid:local_stream_2 video_track_id_3\r\n"
+    "a=ssrc:6 mslabel:local_stream_2\r\n"
+    "a=ssrc:6 label:video_track_id_3\r\n";
+
 // Unified Plan SDP reference string, with 2 streams, 2 audio tracks and 3 video
 // tracks.
 static const char kUnifiedPlanSdpFullString[] =
@@ -2976,6 +3061,20 @@
   TestSerialize(jdesc_, false);
 }
 
+// Some WebRTC endpoints include the msid in both the Plan B and Unified Plan
+// ways, to make SDP that's compatible with both Plan B and Unified Plan (to
+// some extent). If we parse this, the Plan B msid attribute (which is more
+// specific, since it's at the SSRC level) should take priority.
+TEST_F(WebRtcSdpTest, DeserializePlanBSessionDescriptionWithMsid) {
+  MakePlanBDescription();
+
+  JsepSessionDescription deserialized_description(kDummyString);
+  EXPECT_TRUE(
+      SdpDeserialize(kPlanBSdpFullStringWithMsid, &deserialized_description));
+
+  EXPECT_TRUE(CompareSessionDescription(jdesc_, deserialized_description));
+}
+
 TEST_F(WebRtcSdpTest, DeserializeUnifiedPlanSessionDescription) {
   MakeUnifiedPlanDescription();