| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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/modules/rtp_rtcp/source/rtcp_packet.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" |
| 12 | 12 |
| 13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 16 | 16 |
| 17 namespace webrtc { | 17 namespace webrtc { |
| 18 namespace rtcp { | 18 namespace rtcp { |
| 19 namespace { | 19 namespace { |
| 20 void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) { | 20 void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) { |
| 21 buffer[(*offset)++] = value; | 21 buffer[(*offset)++] = value; |
| 22 } | 22 } |
| 23 void AssignUWord16(uint8_t* buffer, size_t* offset, uint16_t value) { | 23 void AssignUWord16(uint8_t* buffer, size_t* offset, uint16_t value) { |
| 24 ByteWriter<uint16_t>::WriteBigEndian(buffer + *offset, value); | 24 ByteWriter<uint16_t>::WriteBigEndian(buffer + *offset, value); |
| 25 *offset += 2; | 25 *offset += 2; |
| 26 } | 26 } |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 void RtcpPacket::Append(RtcpPacket* packet) { | |
| 30 assert(packet); | |
| 31 appended_packets_.push_back(packet); | |
| 32 } | |
| 33 | |
| 34 rtc::Buffer RtcpPacket::Build() const { | 29 rtc::Buffer RtcpPacket::Build() const { |
| 35 size_t length = 0; | 30 size_t length = 0; |
| 36 rtc::Buffer packet(IP_PACKET_SIZE); | 31 rtc::Buffer packet(IP_PACKET_SIZE); |
| 37 | 32 |
| 38 class PacketVerifier : public PacketReadyCallback { | 33 class PacketVerifier : public PacketReadyCallback { |
| 39 public: | 34 public: |
| 40 explicit PacketVerifier(rtc::Buffer* packet) | 35 explicit PacketVerifier(rtc::Buffer* packet) |
| 41 : called_(false), packet_(packet) {} | 36 : called_(false), packet_(packet) {} |
| 42 virtual ~PacketVerifier() {} | 37 virtual ~PacketVerifier() {} |
| 43 void OnPacketReady(uint8_t* data, size_t length) override { | 38 void OnPacketReady(uint8_t* data, size_t length) override { |
| 44 RTC_CHECK(!called_) << "Fragmentation not supported."; | 39 RTC_CHECK(!called_) << "Fragmentation not supported."; |
| 45 called_ = true; | 40 called_ = true; |
| 46 packet_->SetSize(length); | 41 packet_->SetSize(length); |
| 47 } | 42 } |
| 48 | 43 |
| 49 private: | 44 private: |
| 50 bool called_; | 45 bool called_; |
| 51 rtc::Buffer* const packet_; | 46 rtc::Buffer* const packet_; |
| 52 } verifier(&packet); | 47 } verifier(&packet); |
| 53 CreateAndAddAppended(packet.data(), &length, packet.capacity(), &verifier); | 48 Create(packet.data(), &length, packet.capacity(), &verifier); |
| 54 OnBufferFull(packet.data(), &length, &verifier); | 49 OnBufferFull(packet.data(), &length, &verifier); |
| 55 return packet; | 50 return packet; |
| 56 } | 51 } |
| 57 | 52 |
| 58 bool RtcpPacket::Build(PacketReadyCallback* callback) const { | 53 bool RtcpPacket::Build(PacketReadyCallback* callback) const { |
| 59 uint8_t buffer[IP_PACKET_SIZE]; | 54 uint8_t buffer[IP_PACKET_SIZE]; |
| 60 return BuildExternalBuffer(buffer, IP_PACKET_SIZE, callback); | 55 return BuildExternalBuffer(buffer, IP_PACKET_SIZE, callback); |
| 61 } | 56 } |
| 62 | 57 |
| 63 bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer, | 58 bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer, |
| 64 size_t max_length, | 59 size_t max_length, |
| 65 PacketReadyCallback* callback) const { | 60 PacketReadyCallback* callback) const { |
| 66 size_t index = 0; | 61 size_t index = 0; |
| 67 if (!CreateAndAddAppended(buffer, &index, max_length, callback)) | 62 if (!Create(buffer, &index, max_length, callback)) |
| 68 return false; | 63 return false; |
| 69 return OnBufferFull(buffer, &index, callback); | 64 return OnBufferFull(buffer, &index, callback); |
| 70 } | 65 } |
| 71 | 66 |
| 72 bool RtcpPacket::CreateAndAddAppended(uint8_t* packet, | |
| 73 size_t* index, | |
| 74 size_t max_length, | |
| 75 PacketReadyCallback* callback) const { | |
| 76 if (!Create(packet, index, max_length, callback)) | |
| 77 return false; | |
| 78 for (RtcpPacket* appended : appended_packets_) { | |
| 79 if (!appended->CreateAndAddAppended(packet, index, max_length, callback)) | |
| 80 return false; | |
| 81 } | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 bool RtcpPacket::OnBufferFull(uint8_t* packet, | 67 bool RtcpPacket::OnBufferFull(uint8_t* packet, |
| 86 size_t* index, | 68 size_t* index, |
| 87 RtcpPacket::PacketReadyCallback* callback) const { | 69 RtcpPacket::PacketReadyCallback* callback) const { |
| 88 if (*index == 0) | 70 if (*index == 0) |
| 89 return false; | 71 return false; |
| 90 callback->OnPacketReady(packet, *index); | 72 callback->OnPacketReady(packet, *index); |
| 91 *index = 0; | 73 *index = 0; |
| 92 return true; | 74 return true; |
| 93 } | 75 } |
| 94 | 76 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 116 size_t* pos) { | 98 size_t* pos) { |
| 117 assert(length <= 0xffff); | 99 assert(length <= 0xffff); |
| 118 const uint8_t kVersion = 2; | 100 const uint8_t kVersion = 2; |
| 119 AssignUWord8(buffer, pos, (kVersion << 6) + count_or_format); | 101 AssignUWord8(buffer, pos, (kVersion << 6) + count_or_format); |
| 120 AssignUWord8(buffer, pos, packet_type); | 102 AssignUWord8(buffer, pos, packet_type); |
| 121 AssignUWord16(buffer, pos, length); | 103 AssignUWord16(buffer, pos, length); |
| 122 } | 104 } |
| 123 | 105 |
| 124 } // namespace rtcp | 106 } // namespace rtcp |
| 125 } // namespace webrtc | 107 } // namespace webrtc |
| OLD | NEW |