Implement max-channels for SCTP datachannels.

This involves catching another callback from usrsctp.
It also moves the definition of "connected" a little later
in the sequence: From "ready to send data" to the reception
of the SCTP_COMM_UP event.

Bug: chromium:943976
Change-Id: Ib9e1b17d0cc356f19cdfa675159b29bf1efdcb55
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/137435
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28004}
diff --git a/pc/sctp_transport.cc b/pc/sctp_transport.cc
index 1c0289a9..6c4a8be 100644
--- a/pc/sctp_transport.cc
+++ b/pc/sctp_transport.cc
@@ -10,6 +10,7 @@
 
 #include "pc/sctp_transport.h"
 
+#include <algorithm>
 #include <utility>
 
 namespace webrtc {
@@ -20,8 +21,8 @@
       info_(SctpTransportState::kNew),
       internal_sctp_transport_(std::move(internal)) {
   RTC_DCHECK(internal_sctp_transport_.get());
-  internal_sctp_transport_->SignalReadyToSendData.connect(
-      this, &SctpTransport::OnInternalReadyToSendData);
+  internal_sctp_transport_->SignalAssociationChangeCommunicationUp.connect(
+      this, &SctpTransport::OnAssociationChangeCommunicationUp);
   // TODO(https://bugs.webrtc.org/10360): Add handlers for transport closing.
 
   if (dtls_transport_) {
@@ -143,7 +144,21 @@
   }
 }
 
-void SctpTransport::OnInternalReadyToSendData() {
+void SctpTransport::OnAssociationChangeCommunicationUp() {
+  RTC_DCHECK_RUN_ON(owner_thread_);
+  {
+    rtc::CritScope scope(&lock_);
+    RTC_DCHECK(internal_sctp_transport_);
+    if (internal_sctp_transport_->max_outbound_streams() &&
+        internal_sctp_transport_->max_inbound_streams()) {
+      int max_channels =
+          std::min(*(internal_sctp_transport_->max_outbound_streams()),
+                   *(internal_sctp_transport_->max_inbound_streams()));
+      // Record max channels.
+      info_ = SctpTransportInformation(info_.state(), info_.dtls_transport(),
+                                       info_.MaxMessageSize(), max_channels);
+    }
+  }
   UpdateInformation(SctpTransportState::kConnected);
 }