Skip to content

Commit

Permalink
Add ArrayView versions of SendRtp and SendRtcp
Browse files Browse the repository at this point in the history
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 <[email protected]>
Commit-Queue: Harald Alvestrand <[email protected]>
Cr-Commit-Position: refs/heads/main@{#40512}
  • Loading branch information
Harald Alvestrand authored and WebRTC LUCI CQ committed Aug 4, 2023
1 parent cad3aed commit b38d9d2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions api/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ rtc_library("transport_api") {
"call/transport.h",
]
deps = [
":array_view",
":refcountedbase",
":scoped_refptr",
]
Expand Down
12 changes: 12 additions & 0 deletions api/call/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
Expand Down
15 changes: 13 additions & 2 deletions media/base/media_channel_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()) {
Expand Down
3 changes: 3 additions & 0 deletions media/base/media_channel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions rtc_base/copy_on_write_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*,
Expand Down

0 comments on commit b38d9d2

Please sign in to comment.