OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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 |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 WriteAt(0, (data()[0] & 0xF0) | csrcs.size()); | 267 WriteAt(0, (data()[0] & 0xF0) | csrcs.size()); |
268 size_t offset = kFixedHeaderSize; | 268 size_t offset = kFixedHeaderSize; |
269 for (uint32_t csrc : csrcs) { | 269 for (uint32_t csrc : csrcs) { |
270 ByteWriter<uint32_t>::WriteBigEndian(WriteAt(offset), csrc); | 270 ByteWriter<uint32_t>::WriteBigEndian(WriteAt(offset), csrc); |
271 offset += 4; | 271 offset += 4; |
272 } | 272 } |
273 buffer_.SetSize(payload_offset_); | 273 buffer_.SetSize(payload_offset_); |
274 } | 274 } |
275 | 275 |
276 uint8_t* Packet::AllocatePayload(size_t size_bytes) { | 276 uint8_t* Packet::AllocatePayload(size_t size_bytes) { |
| 277 // Reset payload size to 0. If CopyOnWrite buffer_ was shared, this will cause |
| 278 // reallocation and memcpy. Keeping just header reduces memcpy size. |
| 279 SetPayloadSize(0); |
| 280 return SetPayloadSize(size_bytes); |
| 281 } |
| 282 |
| 283 uint8_t* Packet::SetPayloadSize(size_t size_bytes) { |
277 RTC_DCHECK_EQ(padding_size_, 0); | 284 RTC_DCHECK_EQ(padding_size_, 0); |
278 if (payload_offset_ + size_bytes > capacity()) { | 285 if (payload_offset_ + size_bytes > capacity()) { |
279 LOG(LS_WARNING) << "Cannot set payload, not enough space in buffer."; | 286 LOG(LS_WARNING) << "Cannot set payload, not enough space in buffer."; |
280 return nullptr; | 287 return nullptr; |
281 } | 288 } |
282 // Reset payload size to 0. If CopyOnWrite buffer_ was shared, this will cause | |
283 // reallocation and memcpy. Setting size to just headers reduces memcpy size. | |
284 buffer_.SetSize(payload_offset_); | |
285 payload_size_ = size_bytes; | 289 payload_size_ = size_bytes; |
286 buffer_.SetSize(payload_offset_ + payload_size_); | 290 buffer_.SetSize(payload_offset_ + payload_size_); |
287 return WriteAt(payload_offset_); | 291 return WriteAt(payload_offset_); |
288 } | 292 } |
289 | 293 |
290 void Packet::SetPayloadSize(size_t size_bytes) { | |
291 RTC_DCHECK_EQ(padding_size_, 0); | |
292 RTC_DCHECK_LE(size_bytes, payload_size_); | |
293 payload_size_ = size_bytes; | |
294 buffer_.SetSize(payload_offset_ + payload_size_); | |
295 } | |
296 | |
297 bool Packet::SetPadding(uint8_t size_bytes, Random* random) { | 294 bool Packet::SetPadding(uint8_t size_bytes, Random* random) { |
298 RTC_DCHECK(random); | 295 RTC_DCHECK(random); |
299 if (payload_offset_ + payload_size_ + size_bytes > capacity()) { | 296 if (payload_offset_ + payload_size_ + size_bytes > capacity()) { |
300 LOG(LS_WARNING) << "Cannot set padding size " << size_bytes << ", only " | 297 LOG(LS_WARNING) << "Cannot set padding size " << size_bytes << ", only " |
301 << (capacity() - payload_offset_ - payload_size_) | 298 << (capacity() - payload_offset_ - payload_size_) |
302 << " bytes left in buffer."; | 299 << " bytes left in buffer."; |
303 return false; | 300 return false; |
304 } | 301 } |
305 padding_size_ = size_bytes; | 302 padding_size_ = size_bytes; |
306 buffer_.SetSize(payload_offset_ + payload_size_ + padding_size_); | 303 buffer_.SetSize(payload_offset_ + payload_size_ + padding_size_); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 uint8_t* Packet::WriteAt(size_t offset) { | 545 uint8_t* Packet::WriteAt(size_t offset) { |
549 return buffer_.data() + offset; | 546 return buffer_.data() + offset; |
550 } | 547 } |
551 | 548 |
552 void Packet::WriteAt(size_t offset, uint8_t byte) { | 549 void Packet::WriteAt(size_t offset, uint8_t byte) { |
553 buffer_.data()[offset] = byte; | 550 buffer_.data()[offset] = byte; |
554 } | 551 } |
555 | 552 |
556 } // namespace rtp | 553 } // namespace rtp |
557 } // namespace webrtc | 554 } // namespace webrtc |
OLD | NEW |