Reland Use CopyOnWriteBuffer instead of Buffer to avoid unnecessary copies.
This CL removes copy and assign support from Buffer and changes various
parameters from Buffer to CopyOnWriteBuffer so they can be passed along
and copied without actually copying the underlying data.
With this changed some parameters to be "const" and fixed an issue when
creating a CopyOnWriteBuffer with empty data.
BUG=webrtc:5155
Review URL: https://codereview.webrtc.org/1823503002
Cr-Commit-Position: refs/heads/master@{#12062}
diff --git a/webrtc/api/sctputils.cc b/webrtc/api/sctputils.cc
index 1a2c282..f2d1b0f 100644
--- a/webrtc/api/sctputils.cc
+++ b/webrtc/api/sctputils.cc
@@ -10,8 +10,8 @@
#include "webrtc/api/sctputils.h"
-#include "webrtc/base/buffer.h"
#include "webrtc/base/bytebuffer.h"
+#include "webrtc/base/copyonwritebuffer.h"
#include "webrtc/base/logging.h"
namespace webrtc {
@@ -31,26 +31,27 @@
DCOMCT_UNORDERED_PARTIAL_TIME = 0x82,
};
-bool IsOpenMessage(const rtc::Buffer& payload) {
+bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) {
// Format defined at
// http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
-
- rtc::ByteBuffer buffer(payload);
- uint8_t message_type;
- if (!buffer.ReadUInt8(&message_type)) {
+ if (payload.size() < 1) {
LOG(LS_WARNING) << "Could not read OPEN message type.";
return false;
}
+
+ uint8_t message_type = payload[0];
return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
}
-bool ParseDataChannelOpenMessage(const rtc::Buffer& payload,
+bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload,
std::string* label,
DataChannelInit* config) {
// Format defined at
// http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
- rtc::ByteBuffer buffer(payload);
+ // TODO(jbauch): avoid copying the payload data into the ByteBuffer, see
+ // https://bugs.chromium.org/p/webrtc/issues/detail?id=5670
+ rtc::ByteBuffer buffer(payload.data<char>(), payload.size());
uint8_t message_type;
if (!buffer.ReadUInt8(&message_type)) {
LOG(LS_WARNING) << "Could not read OPEN message type.";
@@ -120,13 +121,13 @@
return true;
}
-bool ParseDataChannelOpenAckMessage(const rtc::Buffer& payload) {
- rtc::ByteBuffer buffer(payload);
- uint8_t message_type;
- if (!buffer.ReadUInt8(&message_type)) {
+bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) {
+ if (payload.size() < 1) {
LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
return false;
}
+
+ uint8_t message_type = payload[0];
if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
<< message_type;
@@ -137,7 +138,7 @@
bool WriteDataChannelOpenMessage(const std::string& label,
const DataChannelInit& config,
- rtc::Buffer* payload) {
+ rtc::CopyOnWriteBuffer* payload) {
// Format defined at
// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1
uint8_t channel_type = 0;
@@ -168,6 +169,7 @@
rtc::ByteBuffer buffer(
NULL, 20 + label.length() + config.protocol.length(),
rtc::ByteBuffer::ORDER_NETWORK);
+ // TODO(tommi): Add error handling and check resulting length.
buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
buffer.WriteUInt8(channel_type);
buffer.WriteUInt16(priority);
@@ -180,9 +182,9 @@
return true;
}
-void WriteDataChannelOpenAckMessage(rtc::Buffer* payload) {
- rtc::ByteBuffer buffer(rtc::ByteBuffer::ORDER_NETWORK);
- buffer.WriteUInt8(DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE);
- payload->SetData(buffer.Data(), buffer.Length());
+void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) {
+ uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE;
+ payload->SetData(&data, sizeof(data));
}
+
} // namespace webrtc