OLD | NEW |
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" |
13 #include "webrtc/base/bytebuffer.h" | 14 #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::CopyOnWriteBuffer& payload) { | 34 bool IsOpenMessage(const rtc::Buffer& 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 if (payload.size() < 1) { | 37 |
| 38 rtc::ByteBuffer buffer(payload); |
| 39 uint8_t message_type; |
| 40 if (!buffer.ReadUInt8(&message_type)) { |
38 LOG(LS_WARNING) << "Could not read OPEN message type."; | 41 LOG(LS_WARNING) << "Could not read OPEN message type."; |
39 return false; | 42 return false; |
40 } | 43 } |
41 | |
42 uint8_t message_type = payload[0]; | |
43 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE; | 44 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE; |
44 } | 45 } |
45 | 46 |
46 bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload, | 47 bool ParseDataChannelOpenMessage(const rtc::Buffer& payload, |
47 std::string* label, | 48 std::string* label, |
48 DataChannelInit* config) { | 49 DataChannelInit* config) { |
49 // Format defined at | 50 // Format defined at |
50 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 | 51 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 |
51 | 52 |
52 // TODO(jbauch): avoid copying the payload data into the ByteBuffer, see | 53 rtc::ByteBuffer buffer(payload); |
53 // https://bugs.chromium.org/p/webrtc/issues/detail?id=5670 | |
54 rtc::ByteBuffer buffer(payload.data<char>(), payload.size()); | |
55 uint8_t message_type; | 54 uint8_t message_type; |
56 if (!buffer.ReadUInt8(&message_type)) { | 55 if (!buffer.ReadUInt8(&message_type)) { |
57 LOG(LS_WARNING) << "Could not read OPEN message type."; | 56 LOG(LS_WARNING) << "Could not read OPEN message type."; |
58 return false; | 57 return false; |
59 } | 58 } |
60 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) { | 59 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) { |
61 LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: " | 60 LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: " |
62 << message_type; | 61 << message_type; |
63 return false; | 62 return false; |
64 } | 63 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 config->maxRetransmits = reliability_param; | 113 config->maxRetransmits = reliability_param; |
115 break; | 114 break; |
116 case DCOMCT_ORDERED_PARTIAL_TIME: | 115 case DCOMCT_ORDERED_PARTIAL_TIME: |
117 case DCOMCT_UNORDERED_PARTIAL_TIME: | 116 case DCOMCT_UNORDERED_PARTIAL_TIME: |
118 config->maxRetransmitTime = reliability_param; | 117 config->maxRetransmitTime = reliability_param; |
119 break; | 118 break; |
120 } | 119 } |
121 return true; | 120 return true; |
122 } | 121 } |
123 | 122 |
124 bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) { | 123 bool ParseDataChannelOpenAckMessage(const rtc::Buffer& payload) { |
125 if (payload.size() < 1) { | 124 rtc::ByteBuffer buffer(payload); |
| 125 uint8_t message_type; |
| 126 if (!buffer.ReadUInt8(&message_type)) { |
126 LOG(LS_WARNING) << "Could not read OPEN_ACK message type."; | 127 LOG(LS_WARNING) << "Could not read OPEN_ACK message type."; |
127 return false; | 128 return false; |
128 } | 129 } |
129 | |
130 uint8_t message_type = payload[0]; | |
131 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) { | 130 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) { |
132 LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: " | 131 LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: " |
133 << message_type; | 132 << message_type; |
134 return false; | 133 return false; |
135 } | 134 } |
136 return true; | 135 return true; |
137 } | 136 } |
138 | 137 |
139 bool WriteDataChannelOpenMessage(const std::string& label, | 138 bool WriteDataChannelOpenMessage(const std::string& label, |
140 const DataChannelInit& config, | 139 const DataChannelInit& config, |
141 rtc::CopyOnWriteBuffer* payload) { | 140 rtc::Buffer* payload) { |
142 // Format defined at | 141 // Format defined at |
143 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1 | 142 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1 |
144 uint8_t channel_type = 0; | 143 uint8_t channel_type = 0; |
145 uint32_t reliability_param = 0; | 144 uint32_t reliability_param = 0; |
146 uint16_t priority = 0; | 145 uint16_t priority = 0; |
147 if (config.ordered) { | 146 if (config.ordered) { |
148 if (config.maxRetransmits > -1) { | 147 if (config.maxRetransmits > -1) { |
149 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS; | 148 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS; |
150 reliability_param = config.maxRetransmits; | 149 reliability_param = config.maxRetransmits; |
151 } else if (config.maxRetransmitTime > -1) { | 150 } else if (config.maxRetransmitTime > -1) { |
(...skipping 10 matching lines...) Expand all Loading... |
162 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME; | 161 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME; |
163 reliability_param = config.maxRetransmitTime; | 162 reliability_param = config.maxRetransmitTime; |
164 } else { | 163 } else { |
165 channel_type = DCOMCT_UNORDERED_RELIABLE; | 164 channel_type = DCOMCT_UNORDERED_RELIABLE; |
166 } | 165 } |
167 } | 166 } |
168 | 167 |
169 rtc::ByteBuffer buffer( | 168 rtc::ByteBuffer buffer( |
170 NULL, 20 + label.length() + config.protocol.length(), | 169 NULL, 20 + label.length() + config.protocol.length(), |
171 rtc::ByteBuffer::ORDER_NETWORK); | 170 rtc::ByteBuffer::ORDER_NETWORK); |
172 // TODO(tommi): Add error handling and check resulting length. | |
173 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE); | 171 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE); |
174 buffer.WriteUInt8(channel_type); | 172 buffer.WriteUInt8(channel_type); |
175 buffer.WriteUInt16(priority); | 173 buffer.WriteUInt16(priority); |
176 buffer.WriteUInt32(reliability_param); | 174 buffer.WriteUInt32(reliability_param); |
177 buffer.WriteUInt16(static_cast<uint16_t>(label.length())); | 175 buffer.WriteUInt16(static_cast<uint16_t>(label.length())); |
178 buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length())); | 176 buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length())); |
179 buffer.WriteString(label); | 177 buffer.WriteString(label); |
180 buffer.WriteString(config.protocol); | 178 buffer.WriteString(config.protocol); |
181 payload->SetData(buffer.Data(), buffer.Length()); | 179 payload->SetData(buffer.Data(), buffer.Length()); |
182 return true; | 180 return true; |
183 } | 181 } |
184 | 182 |
185 void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) { | 183 void WriteDataChannelOpenAckMessage(rtc::Buffer* payload) { |
186 uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE; | 184 rtc::ByteBuffer buffer(rtc::ByteBuffer::ORDER_NETWORK); |
187 payload->SetData(&data, sizeof(data)); | 185 buffer.WriteUInt8(DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE); |
| 186 payload->SetData(buffer.Data(), buffer.Length()); |
188 } | 187 } |
189 | |
190 } // namespace webrtc | 188 } // namespace webrtc |
OLD | NEW |