Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: webrtc/api/sctputils.cc

Issue 1823503002: Reland Use CopyOnWriteBuffer instead of Buffer to avoid unnecessary copies. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/api/sctputils.h" 11 #include "webrtc/api/sctputils.h"
12 12
13 #include "webrtc/base/buffer.h"
14 #include "webrtc/base/bytebuffer.h" 13 #include "webrtc/base/bytebuffer.h"
14 #include "webrtc/base/copyonwritebuffer.h"
15 #include "webrtc/base/logging.h" 15 #include "webrtc/base/logging.h"
16 16
17 namespace webrtc { 17 namespace webrtc {
18 18
19 // Format defined at 19 // Format defined at
20 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section 20 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
21 21
22 static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03; 22 static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03;
23 static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02; 23 static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02;
24 24
25 enum DataChannelOpenMessageChannelType { 25 enum DataChannelOpenMessageChannelType {
26 DCOMCT_ORDERED_RELIABLE = 0x00, 26 DCOMCT_ORDERED_RELIABLE = 0x00,
27 DCOMCT_ORDERED_PARTIAL_RTXS = 0x01, 27 DCOMCT_ORDERED_PARTIAL_RTXS = 0x01,
28 DCOMCT_ORDERED_PARTIAL_TIME = 0x02, 28 DCOMCT_ORDERED_PARTIAL_TIME = 0x02,
29 DCOMCT_UNORDERED_RELIABLE = 0x80, 29 DCOMCT_UNORDERED_RELIABLE = 0x80,
30 DCOMCT_UNORDERED_PARTIAL_RTXS = 0x81, 30 DCOMCT_UNORDERED_PARTIAL_RTXS = 0x81,
31 DCOMCT_UNORDERED_PARTIAL_TIME = 0x82, 31 DCOMCT_UNORDERED_PARTIAL_TIME = 0x82,
32 }; 32 };
33 33
34 bool IsOpenMessage(const rtc::Buffer& payload) { 34 bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) {
35 // Format defined at 35 // Format defined at
36 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 36 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
37 37 if (payload.size() < 1) {
38 rtc::ByteBuffer buffer(payload);
39 uint8_t message_type;
40 if (!buffer.ReadUInt8(&message_type)) {
41 LOG(LS_WARNING) << "Could not read OPEN message type."; 38 LOG(LS_WARNING) << "Could not read OPEN message type.";
42 return false; 39 return false;
43 } 40 }
41
42 uint8_t message_type = payload[0];
44 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE; 43 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
45 } 44 }
46 45
47 bool ParseDataChannelOpenMessage(const rtc::Buffer& payload, 46 bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload,
48 std::string* label, 47 std::string* label,
49 DataChannelInit* config) { 48 DataChannelInit* config) {
50 // Format defined at 49 // Format defined at
51 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 50 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
52 51
53 rtc::ByteBuffer buffer(payload); 52 // TODO(jbauch): avoid copying the payload data into the ByteBuffer, see
53 // https://bugs.chromium.org/p/webrtc/issues/detail?id=5670
54 rtc::ByteBuffer buffer(payload.data<char>(), payload.size());
54 uint8_t message_type; 55 uint8_t message_type;
55 if (!buffer.ReadUInt8(&message_type)) { 56 if (!buffer.ReadUInt8(&message_type)) {
56 LOG(LS_WARNING) << "Could not read OPEN message type."; 57 LOG(LS_WARNING) << "Could not read OPEN message type.";
57 return false; 58 return false;
58 } 59 }
59 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) { 60 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) {
60 LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: " 61 LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: "
61 << message_type; 62 << message_type;
62 return false; 63 return false;
63 } 64 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 config->maxRetransmits = reliability_param; 114 config->maxRetransmits = reliability_param;
114 break; 115 break;
115 case DCOMCT_ORDERED_PARTIAL_TIME: 116 case DCOMCT_ORDERED_PARTIAL_TIME:
116 case DCOMCT_UNORDERED_PARTIAL_TIME: 117 case DCOMCT_UNORDERED_PARTIAL_TIME:
117 config->maxRetransmitTime = reliability_param; 118 config->maxRetransmitTime = reliability_param;
118 break; 119 break;
119 } 120 }
120 return true; 121 return true;
121 } 122 }
122 123
123 bool ParseDataChannelOpenAckMessage(const rtc::Buffer& payload) { 124 bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) {
124 rtc::ByteBuffer buffer(payload); 125 if (payload.size() < 1) {
125 uint8_t message_type;
126 if (!buffer.ReadUInt8(&message_type)) {
127 LOG(LS_WARNING) << "Could not read OPEN_ACK message type."; 126 LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
128 return false; 127 return false;
129 } 128 }
129
130 uint8_t message_type = payload[0];
130 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) { 131 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
131 LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: " 132 LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
132 << message_type; 133 << message_type;
133 return false; 134 return false;
134 } 135 }
135 return true; 136 return true;
136 } 137 }
137 138
138 bool WriteDataChannelOpenMessage(const std::string& label, 139 bool WriteDataChannelOpenMessage(const std::string& label,
139 const DataChannelInit& config, 140 const DataChannelInit& config,
140 rtc::Buffer* payload) { 141 rtc::CopyOnWriteBuffer* payload) {
141 // Format defined at 142 // Format defined at
142 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1 143 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1
143 uint8_t channel_type = 0; 144 uint8_t channel_type = 0;
144 uint32_t reliability_param = 0; 145 uint32_t reliability_param = 0;
145 uint16_t priority = 0; 146 uint16_t priority = 0;
146 if (config.ordered) { 147 if (config.ordered) {
147 if (config.maxRetransmits > -1) { 148 if (config.maxRetransmits > -1) {
148 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS; 149 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
149 reliability_param = config.maxRetransmits; 150 reliability_param = config.maxRetransmits;
150 } else if (config.maxRetransmitTime > -1) { 151 } else if (config.maxRetransmitTime > -1) {
(...skipping 10 matching lines...) Expand all
161 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME; 162 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
162 reliability_param = config.maxRetransmitTime; 163 reliability_param = config.maxRetransmitTime;
163 } else { 164 } else {
164 channel_type = DCOMCT_UNORDERED_RELIABLE; 165 channel_type = DCOMCT_UNORDERED_RELIABLE;
165 } 166 }
166 } 167 }
167 168
168 rtc::ByteBuffer buffer( 169 rtc::ByteBuffer buffer(
169 NULL, 20 + label.length() + config.protocol.length(), 170 NULL, 20 + label.length() + config.protocol.length(),
170 rtc::ByteBuffer::ORDER_NETWORK); 171 rtc::ByteBuffer::ORDER_NETWORK);
172 // TODO(tommi): Add error handling and check resulting length.
171 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE); 173 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
172 buffer.WriteUInt8(channel_type); 174 buffer.WriteUInt8(channel_type);
173 buffer.WriteUInt16(priority); 175 buffer.WriteUInt16(priority);
174 buffer.WriteUInt32(reliability_param); 176 buffer.WriteUInt32(reliability_param);
175 buffer.WriteUInt16(static_cast<uint16_t>(label.length())); 177 buffer.WriteUInt16(static_cast<uint16_t>(label.length()));
176 buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length())); 178 buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length()));
177 buffer.WriteString(label); 179 buffer.WriteString(label);
178 buffer.WriteString(config.protocol); 180 buffer.WriteString(config.protocol);
179 payload->SetData(buffer.Data(), buffer.Length()); 181 payload->SetData(buffer.Data(), buffer.Length());
180 return true; 182 return true;
181 } 183 }
182 184
183 void WriteDataChannelOpenAckMessage(rtc::Buffer* payload) { 185 void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) {
184 rtc::ByteBuffer buffer(rtc::ByteBuffer::ORDER_NETWORK); 186 uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE;
185 buffer.WriteUInt8(DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE); 187 payload->SetData(&data, sizeof(data));
186 payload->SetData(buffer.Data(), buffer.Length());
187 } 188 }
189
188 } // namespace webrtc 190 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698