diff --git a/api/BUILD.gn b/api/BUILD.gn index ffaa4aac90..54529ab5b8 100644 --- a/api/BUILD.gn +++ b/api/BUILD.gn @@ -114,6 +114,8 @@ rtc_static_library("libjingle_peerconnection_api") { "crypto_params.h", "data_channel_interface.cc", "data_channel_interface.h", + "data_channel_transport_interface.cc", + "data_channel_transport_interface.h", "datagram_transport_interface.h", "dtls_transport_interface.cc", "dtls_transport_interface.h", diff --git a/api/DEPS b/api/DEPS index 5b3267e993..f7210d803b 100644 --- a/api/DEPS +++ b/api/DEPS @@ -72,6 +72,10 @@ specific_include_rules = { "+rtc_base/ref_count.h", ], + "data_channel_transport_interface\.h": [ + "+rtc_base/copy_on_write_buffer.h", + ], + "dtls_transport_interface\.h": [ "+rtc_base/ref_count.h", "+rtc_base/ssl_certificate.h", diff --git a/api/data_channel_transport_interface.cc b/api/data_channel_transport_interface.cc new file mode 100644 index 0000000000..e5d8fdda9d --- /dev/null +++ b/api/data_channel_transport_interface.cc @@ -0,0 +1,34 @@ +/* Copyright 2019 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "api/data_channel_transport_interface.h" + +namespace webrtc { + +// TODO(mellem): Delete these default implementations and make these functions +// pure virtual as soon as downstream implementations override them. + +RTCError DataChannelTransportInterface::OpenChannel(int channel_id) { + return RTCError(RTCErrorType::UNSUPPORTED_OPERATION); +} + +RTCError DataChannelTransportInterface::SendData( + int channel_id, + const SendDataParams& params, + const rtc::CopyOnWriteBuffer& buffer) { + return RTCError(RTCErrorType::UNSUPPORTED_OPERATION); +} + +RTCError DataChannelTransportInterface::CloseChannel(int channel_id) { + return RTCError(RTCErrorType::UNSUPPORTED_OPERATION); +} + +void DataChannelTransportInterface::SetDataSink(DataChannelSink* /*sink*/) {} + +} // namespace webrtc diff --git a/api/data_channel_transport_interface.h b/api/data_channel_transport_interface.h new file mode 100644 index 0000000000..a63abe0d89 --- /dev/null +++ b/api/data_channel_transport_interface.h @@ -0,0 +1,111 @@ +/* Copyright 2019 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +// This is an experimental interface and is subject to change without notice. + +#ifndef API_DATA_CHANNEL_TRANSPORT_INTERFACE_H_ +#define API_DATA_CHANNEL_TRANSPORT_INTERFACE_H_ + +#include "absl/types/optional.h" +#include "api/rtc_error.h" +#include "rtc_base/copy_on_write_buffer.h" + +namespace webrtc { + +// 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(); + SendDataParams(const 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 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 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; +}; + +// Transport for data channels. +class DataChannelTransportInterface { + public: + virtual ~DataChannelTransportInterface() = default; + + // Opens a data |channel_id| for sending. May return an error if the + // specified |channel_id| is unusable. Must be called before |SendData|. + virtual RTCError OpenChannel(int channel_id); + + // 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. + 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. + 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. + virtual void SetDataSink(DataChannelSink* sink); +}; + +} // namespace webrtc + +#endif // API_DATA_CHANNEL_TRANSPORT_INTERFACE_H_ diff --git a/api/datagram_transport_interface.h b/api/datagram_transport_interface.h index d84090a0c4..38d6dd5791 100644 --- a/api/datagram_transport_interface.h +++ b/api/datagram_transport_interface.h @@ -19,6 +19,7 @@ #include "absl/types/optional.h" #include "api/array_view.h" #include "api/congestion_control_interface.h" +#include "api/data_channel_transport_interface.h" #include "api/media_transport_interface.h" #include "api/rtc_error.h" #include "api/units/data_rate.h" @@ -68,7 +69,7 @@ class DatagramSinkInterface { // CongestionControlInterface). The idea is to send RTP packets as datagrams and // have underlying implementation of datagram transport to use QUIC datagram // protocol. -class DatagramTransportInterface { +class DatagramTransportInterface : public DataChannelTransportInterface { public: virtual ~DatagramTransportInterface() = default; diff --git a/api/media_transport_interface.h b/api/media_transport_interface.h index 3f6fcc0d2c..609ae2c5c1 100644 --- a/api/media_transport_interface.h +++ b/api/media_transport_interface.h @@ -23,6 +23,7 @@ #include "absl/types/optional.h" #include "api/array_view.h" +#include "api/data_channel_transport_interface.h" #include "api/rtc_error.h" #include "api/transport/media/audio_transport.h" #include "api/transport/media/video_transport.h" @@ -135,70 +136,9 @@ class MediaTransportRttObserver { virtual void OnRttUpdated(int64_t rtt_ms) = 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(); - SendDataParams(const 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 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 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 { +class MediaTransportInterface : public DataChannelTransportInterface { public: MediaTransportInterface(); virtual ~MediaTransportInterface(); @@ -331,27 +271,6 @@ class MediaTransportInterface { virtual void SetTargetBitrateLimits( const MediaTransportTargetRateConstraints& target_rate_constraints) {} - // Opens a data |channel_id| for sending. May return an error if the - // specified |channel_id| is unusable. Must be called before |SendData|. - virtual RTCError OpenChannel(int channel_id) = 0; - - // 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. - virtual RTCError SendData(int channel_id, - const SendDataParams& params, - const rtc::CopyOnWriteBuffer& buffer) = 0; - - // 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. - virtual RTCError CloseChannel(int channel_id) = 0; - - // 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. - virtual void SetDataSink(DataChannelSink* sink) = 0; - // TODO(sukhanov): RtcEventLogs. };