Only generate one CNAME per PeerConnection.

The CNAME is generated in the PeerConnection constructor and is populated through the MediaSessionOptions.
A default cname will be set in the MediaSessionOptions constructor.

BUG=webrtc:3431

Review-Url: https://codereview.webrtc.org/1871993002
Cr-Commit-Position: refs/heads/master@{#12650}
diff --git a/webrtc/api/peerconnection.cc b/webrtc/api/peerconnection.cc
index 7f1f452..506a215 100644
--- a/webrtc/api/peerconnection.cc
+++ b/webrtc/api/peerconnection.cc
@@ -66,6 +66,9 @@
 // NOTE: Must be in the same order as the ServiceType enum.
 static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"};
 
+// The length of RTCP CNAMEs.
+static const int kRtcpCnameLength = 16;
+
 // NOTE: A loop below assumes that the first value of this enum is 0 and all
 // other values are incremental.
 enum ServiceType {
@@ -377,6 +380,16 @@
 
 namespace webrtc {
 
+// Generate a RTCP CNAME when a PeerConnection is created.
+std::string GenerateRtcpCname() {
+  std::string cname;
+  if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) {
+    LOG(LS_ERROR) << "Failed to generate CNAME.";
+    RTC_DCHECK(false);
+  }
+  return cname;
+}
+
 bool ExtractMediaSessionOptions(
     const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
     bool is_offer,
@@ -508,6 +521,7 @@
       ice_state_(kIceNew),
       ice_connection_state_(kIceConnectionNew),
       ice_gathering_state_(kIceGatheringNew),
+      rtcp_cname_(GenerateRtcpCname()),
       local_streams_(StreamCollection::Create()),
       remote_streams_(StreamCollection::Create()) {}
 
@@ -1503,6 +1517,8 @@
   if (session_->data_channel_type() == cricket::DCT_SCTP && HasDataChannels()) {
     session_options->data_channel_type = cricket::DCT_SCTP;
   }
+
+  session_options->rtcp_cname = rtcp_cname_;
   return true;
 }
 
@@ -1540,6 +1556,8 @@
   if (!ParseConstraintsForAnswer(constraints, session_options)) {
     return false;
   }
+  session_options->rtcp_cname = rtcp_cname_;
+
   FinishOptionsForAnswer(session_options);
   return true;
 }
@@ -1552,6 +1570,8 @@
   if (!ExtractMediaSessionOptions(options, false, session_options)) {
     return false;
   }
+  session_options->rtcp_cname = rtcp_cname_;
+
   FinishOptionsForAnswer(session_options);
   return true;
 }