Add ArrayView versions of SendRtp and SendRtcp This is part of the long term plan to stop using pointer + length to pass around buffers. Bug: webrtc:14870 Change-Id: Ibaf5258fd326b56132b9b5a8a6b1563a763ef2f8 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/314960 Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Harald Alvestrand <hta@webrtc.org> Cr-Commit-Position: refs/heads/main@{#40512}
diff --git a/api/BUILD.gn b/api/BUILD.gn index a921740..ca5a869 100644 --- a/api/BUILD.gn +++ b/api/BUILD.gn
@@ -796,6 +796,7 @@ "call/transport.h", ] deps = [ + ":array_view", ":refcountedbase", ":scoped_refptr", ]
diff --git a/api/call/transport.h b/api/call/transport.h index 387ce8d..dc3f165 100644 --- a/api/call/transport.h +++ b/api/call/transport.h
@@ -14,6 +14,7 @@ #include <stddef.h> #include <stdint.h> +#include "api/array_view.h" #include "api/ref_counted_base.h" #include "api/scoped_refptr.h" @@ -44,6 +45,17 @@ class Transport { public: + // New style functions. Default implementations are to accomodate + // subclasses that haven't been converted to new style yet. + // TODO(bugs.webrtc.org/14870): Deprecate and remove old functions. + virtual bool SendRtp(rtc::ArrayView<const uint8_t> packet, + const PacketOptions& options) { + return SendRtp(packet.data(), packet.size(), options); + } + virtual bool SendRtcp(rtc::ArrayView<const uint8_t> packet) { + return SendRtcp(packet.data(), packet.size()); + } + // Old style functions. virtual bool SendRtp(const uint8_t* packet, size_t length, const PacketOptions& options) = 0;
diff --git a/media/base/media_channel_impl.cc b/media/base/media_channel_impl.cc index bd5b513..88aa279 100644 --- a/media/base/media_channel_impl.cc +++ b/media/base/media_channel_impl.cc
@@ -201,8 +201,13 @@ bool MediaChannelUtil::TransportForMediaChannels::SendRtcp(const uint8_t* data, size_t len) { + return SendRtcp(rtc::MakeArrayView(data, len)); +} + +bool MediaChannelUtil::TransportForMediaChannels::SendRtcp( + rtc::ArrayView<const uint8_t> packet) { auto send = [this, packet = rtc::CopyOnWriteBuffer( - data, len, kMaxRtpPacketLen)]() mutable { + packet, kMaxRtpPacketLen)]() mutable { rtc::PacketOptions rtc_options; if (DscpEnabled()) { rtc_options.dscp = PreferredDscp(); @@ -222,13 +227,19 @@ const uint8_t* data, size_t len, const webrtc::PacketOptions& options) { + return SendRtp(rtc::ArrayView<const uint8_t>(data, len), options); +} + +bool MediaChannelUtil::TransportForMediaChannels::SendRtp( + rtc::ArrayView<const uint8_t> packet, + const webrtc::PacketOptions& options) { auto send = [this, packet_id = options.packet_id, included_in_feedback = options.included_in_feedback, included_in_allocation = options.included_in_allocation, batchable = options.batchable, last_packet_in_batch = options.last_packet_in_batch, - packet = rtc::CopyOnWriteBuffer(data, len, kMaxRtpPacketLen)]() mutable { + packet = rtc::CopyOnWriteBuffer(packet, kMaxRtpPacketLen)]() mutable { rtc::PacketOptions rtc_options; rtc_options.packet_id = packet_id; if (DscpEnabled()) {
diff --git a/media/base/media_channel_impl.h b/media/base/media_channel_impl.h index 1583e34..1e7fa64 100644 --- a/media/base/media_channel_impl.h +++ b/media/base/media_channel_impl.h
@@ -140,6 +140,9 @@ size_t length, const webrtc::PacketOptions& options) override; bool SendRtcp(const uint8_t* packet, size_t length) override; + bool SendRtp(rtc::ArrayView<const uint8_t> packet, + const webrtc::PacketOptions& options) override; + bool SendRtcp(rtc::ArrayView<const uint8_t> packet) override; // Not implementation of webrtc::Transport void SetInterface(MediaChannelNetworkInterface* iface);
diff --git a/rtc_base/copy_on_write_buffer.h b/rtc_base/copy_on_write_buffer.h index af6e868..8332ee6 100644 --- a/rtc_base/copy_on_write_buffer.h +++ b/rtc_base/copy_on_write_buffer.h
@@ -83,6 +83,17 @@ explicit CopyOnWriteBuffer(const VecT& v) : CopyOnWriteBuffer(v.data(), v.size()) {} + // Construct a buffer from a vector like type and a capacity argument + template <typename VecT, + typename ElemT = typename std::remove_pointer_t< + decltype(std::declval<VecT>().data())>, + typename std::enable_if_t< + !std::is_same<VecT, CopyOnWriteBuffer>::value && + HasDataAndSize<VecT, ElemT>::value && + internal::BufferCompat<uint8_t, ElemT>::value>* = nullptr> + explicit CopyOnWriteBuffer(const VecT& v, size_t capacity) + : CopyOnWriteBuffer(v.data(), v.size(), capacity) {} + ~CopyOnWriteBuffer(); // Get a pointer to the data. Just .data() will give you a (const) uint8_t*,