Add interfaces for using MediaTransport as the transport for data channels.

Adds the types and methods required for sending and receiving data
channel messages over the media transport.  These are:
 - A DataMessageType to distinguish between text, binary, and control
 messages
 - A parameters struct for sending data messages, which specifies the
 channel id, type, and ordering/reliability parameters
 - A sink for data-channel related callbacks (receive data, begin
 closing procedure, and end closing procedure)
 - A method to set the sink for data channels
 - Methods to open, close, and send on data channels

These methods, combined with the state sink, allow PeerConnection to
implement the DataChannelProviderInterface using MediaTransport as the
underlying transport.

Change-Id: Iccb2ba374594762a5b4f995564e2a1ff7d8805f5
Bug: webrtc:9719
Reviewed-on: https://webrtc-review.googlesource.com/c/108541
Reviewed-by: Anton Sukhanov <sukhanov@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Seth Hampson <shampson@webrtc.org>
Reviewed-by: Peter Slatala <psla@webrtc.org>
Commit-Queue: Bjorn Mellem <mellem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25454}
diff --git a/api/DEPS b/api/DEPS
index 3fe2bff..def36c7 100644
--- a/api/DEPS
+++ b/api/DEPS
@@ -96,6 +96,10 @@
     "+rtc_base/scoped_ref_ptr.h",
   ],
 
+  "media_transport_interface\.h": [
+    "+rtc_base/copyonwritebuffer.h",  # As used by datachannelinterface.h
+  ],
+
   "peerconnectionfactoryproxy\.h": [
     "+rtc_base/bind.h",
   ],
diff --git a/api/media_transport_interface.cc b/api/media_transport_interface.cc
index c583fb4..a97b789 100644
--- a/api/media_transport_interface.cc
+++ b/api/media_transport_interface.cc
@@ -83,6 +83,21 @@
 MediaTransportEncodedVideoFrame::MediaTransportEncodedVideoFrame(
     MediaTransportEncodedVideoFrame&&) = default;
 
+SendDataParams::SendDataParams() = default;
+
+RTCError MediaTransportInterface::SendData(
+    int channel_id,
+    const SendDataParams& params,
+    const rtc::CopyOnWriteBuffer& buffer) {
+  RTC_NOTREACHED();
+  return RTCError(RTCErrorType::UNSUPPORTED_OPERATION, "Not implemented");
+}
+
+RTCError MediaTransportInterface::CloseChannel(int channel_id) {
+  RTC_NOTREACHED();
+  return RTCError(RTCErrorType::UNSUPPORTED_OPERATION, "Not implemented");
+}
+
 RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
 MediaTransportFactory::CreateMediaTransport(
     rtc::PacketTransportInternal* packet_transport,
diff --git a/api/media_transport_interface.h b/api/media_transport_interface.h
index 1323635..e0e7453 100644
--- a/api/media_transport_interface.h
+++ b/api/media_transport_interface.h
@@ -28,6 +28,7 @@
 #include "api/rtcerror.h"
 #include "api/video/encoded_image.h"
 #include "common_types.h"  // NOLINT(build/include)
+#include "rtc_base/copyonwritebuffer.h"
 
 namespace rtc {
 class PacketTransportInternal;
@@ -230,6 +231,66 @@
   virtual void OnStateChanged(MediaTransportState state) = 0;
 };
 
+// Supported types of application data messages.
+enum class DataMessageType {
+  // Application data buffer with the binary bit unset.
+  kText,
+
+  // Application data buffer with the binary bit set.
+  kBinary,
+
+  // Transport-agnostic control messages, such as open or open-ack messages.
+  kControl,
+};
+
+// Parameters for sending data.  The parameters may change from message to
+// message, even within a single channel.  For example, control messages may be
+// sent reliably and in-order, even if the data channel is configured for
+// unreliable delivery.
+struct SendDataParams {
+  SendDataParams();
+
+  DataMessageType type = DataMessageType::kText;
+
+  // Whether to deliver the message in order with respect to other ordered
+  // messages with the same channel_id.
+  bool ordered = false;
+
+  // If set, the maximum number of times this message may be
+  // retransmitted by the transport before it is dropped.
+  // Setting this value to zero disables retransmission.
+  // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
+  // simultaneously.
+  absl::optional<int> max_rtx_count;
+
+  // If set, the maximum number of milliseconds for which the transport
+  // may retransmit this message before it is dropped.
+  // Setting this value to zero disables retransmission.
+  // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
+  // simultaneously.
+  absl::optional<int> max_rtx_ms;
+};
+
+// Sink for callbacks related to a data channel.
+class DataChannelSink {
+ public:
+  virtual ~DataChannelSink() = default;
+
+  // Callback issued when data is received by the transport.
+  virtual void OnDataReceived(int channel_id,
+                              DataMessageType type,
+                              const rtc::CopyOnWriteBuffer& buffer) = 0;
+
+  // Callback issued when a remote data channel begins the closing procedure.
+  // Messages sent after the closing procedure begins will not be transmitted.
+  virtual void OnChannelClosing(int channel_id) = 0;
+
+  // Callback issued when a (remote or local) data channel completes the closing
+  // procedure.  Closing channels become closed after all pending data has been
+  // transmitted.
+  virtual void OnChannelClosed(int channel_id) = 0;
+};
+
 // Media transport interface for sending / receiving encoded audio/video frames
 // and receiving bandwidth estimate update from congestion control.
 class MediaTransportInterface {
@@ -279,6 +340,26 @@
   virtual void SetMediaTransportStateCallback(
       MediaTransportStateCallback* callback) {}
 
+  // Sends a data buffer to the remote endpoint using the given send parameters.
+  // |buffer| may not be larger than 256 KiB. Returns an error if the send
+  // fails.
+  // TODO(mellem): Make this pure virtual once all implementations support it.
+  virtual RTCError SendData(int channel_id,
+                            const SendDataParams& params,
+                            const rtc::CopyOnWriteBuffer& buffer);
+
+  // Closes |channel_id| gracefully.  Returns an error if |channel_id| is not
+  // open.  Data sent after the closing procedure begins will not be
+  // transmitted. The channel becomes closed after pending data is transmitted.
+  // TODO(mellem): Make this pure virtual once all implementations support it.
+  virtual RTCError CloseChannel(int channel_id);
+
+  // Sets a sink for data messages and channel state callbacks. Before media
+  // transport is destroyed, the sink must be unregistered by setting it to
+  // nullptr.
+  // TODO(mellem): Make this pure virtual once all implementations support it.
+  virtual void SetDataSink(DataChannelSink* sink) {}
+
   // TODO(sukhanov): RtcEventLogs.
 };