asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" |
| 12 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 13 | #include "webrtc/base/checks.h" |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | namespace rtcp { |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 17 | constexpr size_t RtcpPacket::kHeaderLength; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 18 | |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 19 | rtc::Buffer RtcpPacket::Build() const { |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 20 | rtc::Buffer packet(BlockLength()); |
| 21 | |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 22 | size_t length = 0; |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 23 | bool created = Create(packet.data(), &length, packet.capacity(), nullptr); |
| 24 | RTC_DCHECK(created) << "Invalid packet is not supported."; |
| 25 | RTC_DCHECK_EQ(length, packet.size()) |
| 26 | << "BlockLength mispredicted size used by Create"; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 27 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 28 | return packet; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 31 | bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer, |
| 32 | size_t max_length, |
| 33 | PacketReadyCallback* callback) const { |
| 34 | size_t index = 0; |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 35 | if (!Create(buffer, &index, max_length, callback)) |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 36 | return false; |
| 37 | return OnBufferFull(buffer, &index, callback); |
| 38 | } |
| 39 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 40 | bool RtcpPacket::OnBufferFull(uint8_t* packet, |
| 41 | size_t* index, |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 42 | PacketReadyCallback* callback) const { |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 43 | if (*index == 0) |
| 44 | return false; |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 45 | RTC_DCHECK(callback) << "Fragmentation not supported."; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 46 | callback->OnPacketReady(packet, *index); |
| 47 | *index = 0; |
| 48 | return true; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 51 | size_t RtcpPacket::HeaderLength() const { |
| 52 | size_t length_in_bytes = BlockLength(); |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 53 | RTC_DCHECK_GT(length_in_bytes, 0u); |
| 54 | RTC_DCHECK_EQ(length_in_bytes % 4, 0u) << "Padding not supported"; |
| 55 | // Length in 32-bit words without common header. |
| 56 | return (length_in_bytes - kHeaderLength) / 4; |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // From RFC 3550, RTP: A Transport Protocol for Real-Time Applications. |
| 60 | // |
| 61 | // RTP header format. |
| 62 | // 0 1 2 3 |
| 63 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 64 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 65 | // |V=2|P| RC/FMT | PT | length | |
| 66 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 67 | void RtcpPacket::CreateHeader( |
| 68 | uint8_t count_or_format, // Depends on packet type. |
| 69 | uint8_t packet_type, |
| 70 | size_t length, |
| 71 | uint8_t* buffer, |
sprang | 73a93e8 | 2015-09-14 12:50:39 -0700 | [diff] [blame] | 72 | size_t* pos) { |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 73 | RTC_DCHECK_LE(length, 0xffffU); |
| 74 | RTC_DCHECK_LE(count_or_format, 0x1f); |
| 75 | constexpr uint8_t kVersionBits = 2 << 6; |
| 76 | constexpr uint8_t kNoPaddingBit = 0 << 5; |
| 77 | buffer[*pos + 0] = kVersionBits | kNoPaddingBit | count_or_format; |
| 78 | buffer[*pos + 1] = packet_type; |
| 79 | buffer[*pos + 2] = (length >> 8) & 0xff; |
| 80 | buffer[*pos + 3] = length & 0xff; |
| 81 | *pos += kHeaderLength; |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 82 | } |
| 83 | |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 84 | } // namespace rtcp |
| 85 | } // namespace webrtc |