Fix SDP stream ID mismatch issue when a track's stream changes.

The example that brought up this issue was:
1. Do offer/answer exchange.
2. Later, remove the audio/video stream.
3. Add back a new stream, that contains only the audio track.
4. Do new offer/answer.

The new offer didn't have the new stream ID, but code elsewhere was
expecting one. As a result, the send stream is never hooked up to the
audio track, and audio packets aren't sent.

BUG=chromium:611708

Review-Url: https://codereview.webrtc.org/2810733003
Cr-Commit-Position: refs/heads/master@{#17709}
diff --git a/webrtc/pc/peerconnection_integrationtest.cc b/webrtc/pc/peerconnection_integrationtest.cc
index d818300..49771f6 100644
--- a/webrtc/pc/peerconnection_integrationtest.cc
+++ b/webrtc/pc/peerconnection_integrationtest.cc
@@ -217,6 +217,10 @@
     return client;
   }
 
+  webrtc::PeerConnectionFactoryInterface* pc_factory() const {
+    return peer_connection_factory_.get();
+  }
+
   webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
 
   // If a signaling message receiver is set (via ConnectFakeSignaling), this
@@ -2773,6 +2777,38 @@
             contributing_sources[0].source_id());
 }
 
+// Test that if a track is removed and added again with a different stream ID,
+// the new stream ID is successfully communicated in SDP and media continues to
+// flow end-to-end.
+TEST_F(PeerConnectionIntegrationTest, RemoveAndAddTrackWithNewStreamId) {
+  ASSERT_TRUE(CreatePeerConnectionWrappers());
+  ConnectFakeSignaling();
+
+  rtc::scoped_refptr<MediaStreamInterface> stream_1 =
+      caller()->pc_factory()->CreateLocalMediaStream("stream_1");
+  rtc::scoped_refptr<MediaStreamInterface> stream_2 =
+      caller()->pc_factory()->CreateLocalMediaStream("stream_2");
+
+  // Add track using stream 1, do offer/answer.
+  rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
+      caller()->CreateLocalAudioTrack();
+  rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
+      caller()->pc()->AddTrack(track, {stream_1.get()});
+  caller()->CreateAndSetAndSignalOffer();
+  ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
+  // Wait for one audio frame to be received by the callee.
+  ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
+
+  // Remove the sender, and create a new one with the new stream.
+  caller()->pc()->RemoveTrack(sender);
+  sender = caller()->pc()->AddTrack(track, {stream_2.get()});
+  caller()->CreateAndSetAndSignalOffer();
+  ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
+  // Wait for additional audio frames to be received by the callee.
+  ExpectNewFramesReceivedWithWait(0, 0, kDefaultExpectedAudioFrameCount, 0,
+                                  kMaxWaitForFramesMs);
+}
+
 }  // namespace
 
 #endif  // if !defined(THREAD_SANITIZER)