2014-02-21 08:14:45 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
|
|
|
|
|
|
2015-06-08 09:54:14 +02:00
|
|
|
#include "webrtc/base/checks.h"
|
2015-10-28 16:39:33 +01:00
|
|
|
#include "webrtc/base/logging.h"
|
2015-03-17 16:42:49 +00:00
|
|
|
#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
|
2014-02-21 08:14:45 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace rtcp {
|
|
|
|
|
namespace {
|
2014-06-16 14:09:28 +00:00
|
|
|
void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) {
|
2014-02-21 08:14:45 +00:00
|
|
|
buffer[(*offset)++] = value;
|
|
|
|
|
}
|
2014-06-16 14:09:28 +00:00
|
|
|
void AssignUWord16(uint8_t* buffer, size_t* offset, uint16_t value) {
|
2015-03-17 16:42:49 +00:00
|
|
|
ByteWriter<uint16_t>::WriteBigEndian(buffer + *offset, value);
|
2014-02-21 08:14:45 +00:00
|
|
|
*offset += 2;
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void RtcpPacket::Append(RtcpPacket* packet) {
|
|
|
|
|
assert(packet);
|
|
|
|
|
appended_packets_.push_back(packet);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 03:11:42 -08:00
|
|
|
rtc::Buffer RtcpPacket::Build() const {
|
2014-06-16 14:09:28 +00:00
|
|
|
size_t length = 0;
|
2016-02-17 03:11:42 -08:00
|
|
|
rtc::Buffer packet(IP_PACKET_SIZE);
|
2015-06-08 09:54:14 +02:00
|
|
|
|
|
|
|
|
class PacketVerifier : public PacketReadyCallback {
|
|
|
|
|
public:
|
2016-02-17 03:11:42 -08:00
|
|
|
explicit PacketVerifier(rtc::Buffer* packet)
|
2015-06-08 09:54:14 +02:00
|
|
|
: called_(false), packet_(packet) {}
|
|
|
|
|
virtual ~PacketVerifier() {}
|
|
|
|
|
void OnPacketReady(uint8_t* data, size_t length) override {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(!called_) << "Fragmentation not supported.";
|
2015-06-08 09:54:14 +02:00
|
|
|
called_ = true;
|
2016-02-17 03:11:42 -08:00
|
|
|
packet_->SetSize(length);
|
2015-06-08 09:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool called_;
|
2016-02-17 03:11:42 -08:00
|
|
|
rtc::Buffer* const packet_;
|
|
|
|
|
} verifier(&packet);
|
|
|
|
|
CreateAndAddAppended(packet.data(), &length, packet.capacity(), &verifier);
|
|
|
|
|
OnBufferFull(packet.data(), &length, &verifier);
|
2015-06-08 09:54:14 +02:00
|
|
|
return packet;
|
2014-02-21 08:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-08 09:54:14 +02:00
|
|
|
bool RtcpPacket::Build(PacketReadyCallback* callback) const {
|
|
|
|
|
uint8_t buffer[IP_PACKET_SIZE];
|
|
|
|
|
return BuildExternalBuffer(buffer, IP_PACKET_SIZE, callback);
|
2014-02-21 08:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-08 09:54:14 +02:00
|
|
|
bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer,
|
|
|
|
|
size_t max_length,
|
|
|
|
|
PacketReadyCallback* callback) const {
|
|
|
|
|
size_t index = 0;
|
|
|
|
|
if (!CreateAndAddAppended(buffer, &index, max_length, callback))
|
|
|
|
|
return false;
|
|
|
|
|
return OnBufferFull(buffer, &index, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RtcpPacket::CreateAndAddAppended(uint8_t* packet,
|
|
|
|
|
size_t* index,
|
|
|
|
|
size_t max_length,
|
|
|
|
|
PacketReadyCallback* callback) const {
|
|
|
|
|
if (!Create(packet, index, max_length, callback))
|
|
|
|
|
return false;
|
|
|
|
|
for (RtcpPacket* appended : appended_packets_) {
|
|
|
|
|
if (!appended->CreateAndAddAppended(packet, index, max_length, callback))
|
|
|
|
|
return false;
|
2014-02-21 08:14:45 +00:00
|
|
|
}
|
2015-06-08 09:54:14 +02:00
|
|
|
return true;
|
2014-02-21 08:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-08 09:54:14 +02:00
|
|
|
bool RtcpPacket::OnBufferFull(uint8_t* packet,
|
|
|
|
|
size_t* index,
|
|
|
|
|
RtcpPacket::PacketReadyCallback* callback) const {
|
|
|
|
|
if (*index == 0)
|
|
|
|
|
return false;
|
|
|
|
|
callback->OnPacketReady(packet, *index);
|
|
|
|
|
*index = 0;
|
|
|
|
|
return true;
|
2014-02-21 08:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 10:46:54 +02:00
|
|
|
size_t RtcpPacket::HeaderLength() const {
|
|
|
|
|
size_t length_in_bytes = BlockLength();
|
|
|
|
|
// Length in 32-bit words minus 1.
|
|
|
|
|
assert(length_in_bytes > 0);
|
|
|
|
|
return ((length_in_bytes + 3) / 4) - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// From RFC 3550, RTP: A Transport Protocol for Real-Time Applications.
|
|
|
|
|
//
|
|
|
|
|
// RTP header format.
|
|
|
|
|
// 0 1 2 3
|
|
|
|
|
// 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
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
// |V=2|P| RC/FMT | PT | length |
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
|
|
|
|
|
void RtcpPacket::CreateHeader(
|
|
|
|
|
uint8_t count_or_format, // Depends on packet type.
|
|
|
|
|
uint8_t packet_type,
|
|
|
|
|
size_t length,
|
|
|
|
|
uint8_t* buffer,
|
2015-09-14 12:50:39 -07:00
|
|
|
size_t* pos) {
|
2015-07-29 10:46:54 +02:00
|
|
|
assert(length <= 0xffff);
|
|
|
|
|
const uint8_t kVersion = 2;
|
|
|
|
|
AssignUWord8(buffer, pos, (kVersion << 6) + count_or_format);
|
|
|
|
|
AssignUWord8(buffer, pos, packet_type);
|
|
|
|
|
AssignUWord16(buffer, pos, length);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-21 08:14:45 +00:00
|
|
|
} // namespace rtcp
|
|
|
|
|
} // namespace webrtc
|