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}
This commit is contained in:
Harald Alvestrand 2023-08-04 09:50:36 +00:00 committed by WebRTC LUCI CQ
parent cad3aed5fc
commit b38d9d2b6f
5 changed files with 40 additions and 2 deletions

View file

@ -796,6 +796,7 @@ rtc_library("transport_api") {
"call/transport.h",
]
deps = [
":array_view",
":refcountedbase",
":scoped_refptr",
]

View file

@ -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 @@ struct PacketOptions {
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;

View file

@ -201,8 +201,13 @@ MediaChannelUtil::TransportForMediaChannels::~TransportForMediaChannels() {
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 @@ bool MediaChannelUtil::TransportForMediaChannels::SendRtp(
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()) {

View file

@ -140,6 +140,9 @@ class MediaChannelUtil {
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);

View file

@ -83,6 +83,17 @@ class RTC_EXPORT CopyOnWriteBuffer {
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*,