Fix formatting of rtp_format_vp8*

Sorting out all lint issues and fixing indentation.

Review URL: http://webrtc-codereview.appspot.com/301011

git-svn-id: http://webrtc.googlecode.com/svn/trunk@1111 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
henrik.lundin@webrtc.org 2011-12-06 15:56:18 +00:00
parent c7e2bffb66
commit b6e58eb5a1
3 changed files with 656 additions and 738 deletions

View File

@ -8,10 +8,11 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
#include "rtp_format_vp8.h" #include "modules/rtp_rtcp/source/rtp_format_vp8.h"
#include <cassert> // assert #include <string.h> // memcpy
#include <string.h> // memcpy
#include <cassert> // assert
namespace webrtc { namespace webrtc {
@ -40,9 +41,8 @@ RtpFormatVp8::RtpFormatVp8(const WebRtc_UWord8* payload_data,
balance_(balance_modes_[mode]), balance_(balance_modes_[mode]),
separate_first_(separate_first_modes_[mode]), separate_first_(separate_first_modes_[mode]),
hdr_info_(hdr_info), hdr_info_(hdr_info),
first_partition_in_packet_(0) first_partition_in_packet_(0) {
{ part_info_ = fragmentation;
part_info_ = fragmentation;
} }
RtpFormatVp8::RtpFormatVp8(const WebRtc_UWord8* payload_data, RtpFormatVp8::RtpFormatVp8(const WebRtc_UWord8* payload_data,
@ -60,312 +60,268 @@ RtpFormatVp8::RtpFormatVp8(const WebRtc_UWord8* payload_data,
balance_(balance_modes_[kSloppy]), balance_(balance_modes_[kSloppy]),
separate_first_(separate_first_modes_[kSloppy]), separate_first_(separate_first_modes_[kSloppy]),
hdr_info_(hdr_info), hdr_info_(hdr_info),
first_partition_in_packet_(0) first_partition_in_packet_(0) {
{
part_info_.VerifyAndAllocateFragmentationHeader(1); part_info_.VerifyAndAllocateFragmentationHeader(1);
part_info_.fragmentationLength[0] = payload_size; part_info_.fragmentationLength[0] = payload_size;
part_info_.fragmentationOffset[0] = 0; part_info_.fragmentationOffset[0] = 0;
} }
int RtpFormatVp8::CalcNextSize(int max_payload_len, int remaining_bytes, int RtpFormatVp8::CalcNextSize(int max_payload_len, int remaining_bytes,
bool split_payload) const bool split_payload) const {
{ if (max_payload_len == 0 || remaining_bytes == 0) {
if (max_payload_len == 0 || remaining_bytes == 0) return 0;
{ }
return 0; if (!split_payload) {
} return max_payload_len >= remaining_bytes ? remaining_bytes : 0;
if (!split_payload) }
{
return max_payload_len >= remaining_bytes ? remaining_bytes : 0;
}
if (balance_) if (balance_) {
{ // Balance payload sizes to produce (almost) equal size
// Balance payload sizes to produce (almost) equal size // fragments.
// fragments. // Number of fragments for remaining_bytes:
// Number of fragments for remaining_bytes: int num_frags = remaining_bytes / max_payload_len + 1;
int num_frags = remaining_bytes / max_payload_len + 1; // Number of bytes in this fragment:
// Number of bytes in this fragment: return static_cast<int>(static_cast<double>(remaining_bytes)
return static_cast<int>(static_cast<double>(remaining_bytes) / num_frags + 0.5);
/ num_frags + 0.5); } else {
} return max_payload_len >= remaining_bytes ? remaining_bytes
else : max_payload_len;
{ }
return max_payload_len >= remaining_bytes ? remaining_bytes
: max_payload_len;
}
} }
int RtpFormatVp8::NextPacket(int max_payload_len, WebRtc_UWord8* buffer, int RtpFormatVp8::NextPacket(int max_payload_len, WebRtc_UWord8* buffer,
int* bytes_to_send, bool* last_packet) int* bytes_to_send, bool* last_packet) {
{ if (max_payload_len < vp8_fixed_payload_descriptor_bytes_
if (max_payload_len < vp8_fixed_payload_descriptor_bytes_ + PayloadDescriptorExtraLength() + 1) {
+ PayloadDescriptorExtraLength() + 1) // The provided payload length is not long enough for the payload
{ // descriptor and one payload byte. Return an error.
// The provided payload length is not long enough for the payload return -1;
// descriptor and one payload byte. Return an error. }
return -1; const int num_partitions = part_info_.fragmentationVectorSize;
} int send_bytes = 0; // How much data to send in this packet.
const int num_partitions = part_info_.fragmentationVectorSize; bool split_payload = true; // Splitting of partitions is initially allowed.
int send_bytes = 0; // How much data to send in this packet. int remaining_in_partition = part_info_.fragmentationOffset[part_ix_] -
bool split_payload = true; // Splitting of partitions is initially allowed. payload_bytes_sent_ + part_info_.fragmentationLength[part_ix_] +
int remaining_in_partition = part_info_.fragmentationOffset[part_ix_] - PayloadDescriptorExtraLength();
payload_bytes_sent_ + part_info_.fragmentationLength[part_ix_] + int rem_payload_len = max_payload_len - vp8_fixed_payload_descriptor_bytes_;
PayloadDescriptorExtraLength(); first_partition_in_packet_ = part_ix_;
int rem_payload_len = max_payload_len - vp8_fixed_payload_descriptor_bytes_; if (first_partition_in_packet_ > 8) return -1;
first_partition_in_packet_ = part_ix_;
if (first_partition_in_packet_ > 8) return -1;
while (int next_size = CalcNextSize(rem_payload_len, remaining_in_partition, while (int next_size = CalcNextSize(rem_payload_len, remaining_in_partition,
split_payload)) split_payload)) {
{ send_bytes += next_size;
send_bytes += next_size; rem_payload_len -= next_size;
rem_payload_len -= next_size; remaining_in_partition -= next_size;
remaining_in_partition -= next_size;
if (remaining_in_partition == 0 && !(beginning_ && separate_first_)) if (remaining_in_partition == 0 && !(beginning_ && separate_first_)) {
{ // Advance to next partition?
// Advance to next partition? // Check that there are more partitions; verify that we are either
// Check that there are more partitions; verify that we are either // allowed to aggregate fragments, or that we are allowed to
// allowed to aggregate fragments, or that we are allowed to // aggregate intact partitions and that we started this packet
// aggregate intact partitions and that we started this packet // with an intact partition (indicated by first_fragment_ == true).
// with an intact partition (indicated by first_fragment_ == true). if (part_ix_ + 1 < num_partitions &&
if (part_ix_ + 1 < num_partitions && ((aggr_mode_ == kAggrFragments) ||
((aggr_mode_ == kAggrFragments) || (aggr_mode_ == kAggrPartitions && first_fragment_))) {
(aggr_mode_ == kAggrPartitions && first_fragment_))) remaining_in_partition
{ = part_info_.fragmentationLength[++part_ix_];
remaining_in_partition // Disallow splitting unless kAggrFragments. In kAggrPartitions,
= part_info_.fragmentationLength[++part_ix_]; // we can only aggregate intact partitions.
// Disallow splitting unless kAggrFragments. In kAggrPartitions, split_payload = (aggr_mode_ == kAggrFragments);
// we can only aggregate intact partitions. }
split_payload = (aggr_mode_ == kAggrFragments); } else if (balance_ && remaining_in_partition > 0) {
} break;
}
else if (balance_ && remaining_in_partition > 0)
{
break;
}
}
if (remaining_in_partition == 0)
{
++part_ix_; // Advance to next partition.
} }
}
if (remaining_in_partition == 0) {
++part_ix_; // Advance to next partition.
}
send_bytes -= PayloadDescriptorExtraLength(); // Remove extra length again. send_bytes -= PayloadDescriptorExtraLength(); // Remove extra length again.
assert(send_bytes > 0); assert(send_bytes > 0);
// Write the payload header and the payload to buffer. // Write the payload header and the payload to buffer.
*bytes_to_send = WriteHeaderAndPayload(send_bytes, buffer, max_payload_len); *bytes_to_send = WriteHeaderAndPayload(send_bytes, buffer, max_payload_len);
if (*bytes_to_send < 0) if (*bytes_to_send < 0) {
{ return -1;
return -1; }
}
beginning_ = false; // Next packet cannot be first packet in frame. beginning_ = false; // Next packet cannot be first packet in frame.
// Next packet starts new fragment if this ended one. // Next packet starts new fragment if this ended one.
first_fragment_ = (remaining_in_partition == 0); first_fragment_ = (remaining_in_partition == 0);
*last_packet = (payload_bytes_sent_ >= payload_size_); *last_packet = (payload_bytes_sent_ >= payload_size_);
assert(!*last_packet || (payload_bytes_sent_ == payload_size_)); assert(!*last_packet || (payload_bytes_sent_ == payload_size_));
return first_partition_in_packet_; return first_partition_in_packet_;
} }
int RtpFormatVp8::WriteHeaderAndPayload(int payload_bytes, int RtpFormatVp8::WriteHeaderAndPayload(int payload_bytes,
WebRtc_UWord8* buffer, WebRtc_UWord8* buffer,
int buffer_length) int buffer_length) {
{ // Write the VP8 payload descriptor.
// Write the VP8 payload descriptor. // 0
// 0 // 0 1 2 3 4 5 6 7 8
// 0 1 2 3 4 5 6 7 8 // +-+-+-+-+-+-+-+-+-+
// +-+-+-+-+-+-+-+-+-+ // |X| |N|S| PART_ID |
// |X| |N|S| PART_ID | // +-+-+-+-+-+-+-+-+-+
// +-+-+-+-+-+-+-+-+-+ // X: |I|L|T|K| | (mandatory if any of the below are used)
// X: |I|L|T|K| | (mandatory if any of the below are used) // +-+-+-+-+-+-+-+-+-+
// +-+-+-+-+-+-+-+-+-+ // I: |PictureID (8/16b)| (optional)
// I: |PictureID (8/16b)| (optional) // +-+-+-+-+-+-+-+-+-+
// +-+-+-+-+-+-+-+-+-+ // L: | TL0PIC_IDX | (optional)
// L: | TL0PIC_IDX | (optional) // +-+-+-+-+-+-+-+-+-+
// +-+-+-+-+-+-+-+-+-+ // T/K: | TID | KEYIDX | (optional)
// T/K: | TID | KEYIDX | (optional) // +-+-+-+-+-+-+-+-+-+
// +-+-+-+-+-+-+-+-+-+
assert(payload_bytes > 0); assert(payload_bytes > 0);
assert(payload_bytes_sent_ + payload_bytes <= payload_size_); assert(payload_bytes_sent_ + payload_bytes <= payload_size_);
assert(vp8_fixed_payload_descriptor_bytes_ + PayloadDescriptorExtraLength() assert(vp8_fixed_payload_descriptor_bytes_ + PayloadDescriptorExtraLength()
+ payload_bytes <= buffer_length); + payload_bytes <= buffer_length);
buffer[0] = 0; buffer[0] = 0;
if (XFieldPresent()) buffer[0] |= kXBit; if (XFieldPresent()) buffer[0] |= kXBit;
if (hdr_info_.nonReference) buffer[0] |= kNBit; if (hdr_info_.nonReference) buffer[0] |= kNBit;
if (first_fragment_) buffer[0] |= kSBit; if (first_fragment_) buffer[0] |= kSBit;
buffer[0] |= (first_partition_in_packet_ & kPartIdField); buffer[0] |= (first_partition_in_packet_ & kPartIdField);
const int extension_length = WriteExtensionFields(buffer, buffer_length); const int extension_length = WriteExtensionFields(buffer, buffer_length);
memcpy(&buffer[vp8_fixed_payload_descriptor_bytes_ + extension_length], memcpy(&buffer[vp8_fixed_payload_descriptor_bytes_ + extension_length],
&payload_data_[payload_bytes_sent_], payload_bytes); &payload_data_[payload_bytes_sent_], payload_bytes);
payload_bytes_sent_ += payload_bytes; payload_bytes_sent_ += payload_bytes;
// Return total length of written data. // Return total length of written data.
return payload_bytes + vp8_fixed_payload_descriptor_bytes_ return payload_bytes + vp8_fixed_payload_descriptor_bytes_
+ extension_length; + extension_length;
} }
int RtpFormatVp8::WriteExtensionFields(WebRtc_UWord8* buffer, int buffer_length) int RtpFormatVp8::WriteExtensionFields(WebRtc_UWord8* buffer,
const int buffer_length) const {
{ int extension_length = 0;
int extension_length = 0; if (XFieldPresent()) {
if (XFieldPresent()) WebRtc_UWord8* x_field = buffer + vp8_fixed_payload_descriptor_bytes_;
{ *x_field = 0;
WebRtc_UWord8* x_field = buffer + vp8_fixed_payload_descriptor_bytes_; extension_length = 1; // One octet for the X field.
*x_field = 0; if (PictureIdPresent()) {
extension_length = 1; // One octet for the X field. if (WritePictureIDFields(x_field, buffer, buffer_length,
if (PictureIdPresent()) &extension_length) < 0) {
{ return -1;
if (WritePictureIDFields(x_field, buffer, buffer_length, }
&extension_length) < 0)
{
return -1;
}
}
if (TL0PicIdxFieldPresent())
{
if (WriteTl0PicIdxFields(x_field, buffer, buffer_length,
&extension_length) < 0)
{
return -1;
}
}
if (TIDFieldPresent() || KeyIdxFieldPresent())
{
if (WriteTIDAndKeyIdxFields(x_field, buffer, buffer_length,
&extension_length) < 0)
{
return -1;
}
}
assert(extension_length == PayloadDescriptorExtraLength());
} }
return extension_length; if (TL0PicIdxFieldPresent()) {
if (WriteTl0PicIdxFields(x_field, buffer, buffer_length,
&extension_length) < 0) {
return -1;
}
}
if (TIDFieldPresent() || KeyIdxFieldPresent()) {
if (WriteTIDAndKeyIdxFields(x_field, buffer, buffer_length,
&extension_length) < 0) {
return -1;
}
}
assert(extension_length == PayloadDescriptorExtraLength());
}
return extension_length;
} }
int RtpFormatVp8::WritePictureIDFields(WebRtc_UWord8* x_field, int RtpFormatVp8::WritePictureIDFields(WebRtc_UWord8* x_field,
WebRtc_UWord8* buffer, WebRtc_UWord8* buffer,
int buffer_length, int buffer_length,
int* extension_length) const int* extension_length) const {
{ *x_field |= kIBit;
*x_field |= kIBit; const int pic_id_length = WritePictureID(
const int pic_id_length = WritePictureID( buffer + vp8_fixed_payload_descriptor_bytes_ + *extension_length,
buffer + vp8_fixed_payload_descriptor_bytes_ + *extension_length, buffer_length - vp8_fixed_payload_descriptor_bytes_
buffer_length - vp8_fixed_payload_descriptor_bytes_ - *extension_length);
- *extension_length); if (pic_id_length < 0) return -1;
if (pic_id_length < 0) return -1; *extension_length += pic_id_length;
*extension_length += pic_id_length; return 0;
return 0;
} }
int RtpFormatVp8::WritePictureID(WebRtc_UWord8* buffer, int buffer_length) const int RtpFormatVp8::WritePictureID(WebRtc_UWord8* buffer,
{ int buffer_length) const {
const WebRtc_UWord16 pic_id = const WebRtc_UWord16 pic_id =
static_cast<WebRtc_UWord16> (hdr_info_.pictureId); static_cast<WebRtc_UWord16> (hdr_info_.pictureId);
int picture_id_len = PictureIdLength(); int picture_id_len = PictureIdLength();
if (picture_id_len > buffer_length) return -1; if (picture_id_len > buffer_length) return -1;
if (picture_id_len == 2) if (picture_id_len == 2) {
{ buffer[0] = 0x80 | ((pic_id >> 8) & 0x7F);
buffer[0] = 0x80 | ((pic_id >> 8) & 0x7F); buffer[1] = pic_id & 0xFF;
buffer[1] = pic_id & 0xFF; } else if (picture_id_len == 1) {
} buffer[0] = pic_id & 0x7F;
else if (picture_id_len == 1) }
{ return picture_id_len;
buffer[0] = pic_id & 0x7F;
}
return picture_id_len;
} }
int RtpFormatVp8::WriteTl0PicIdxFields(WebRtc_UWord8* x_field, int RtpFormatVp8::WriteTl0PicIdxFields(WebRtc_UWord8* x_field,
WebRtc_UWord8* buffer, WebRtc_UWord8* buffer,
int buffer_length, int buffer_length,
int* extension_length) const int* extension_length) const {
{ if (buffer_length < vp8_fixed_payload_descriptor_bytes_ + *extension_length
if (buffer_length < vp8_fixed_payload_descriptor_bytes_ + *extension_length + 1) {
+ 1) return -1;
{ }
return -1; *x_field |= kLBit;
} buffer[vp8_fixed_payload_descriptor_bytes_
*x_field |= kLBit; + *extension_length] = hdr_info_.tl0PicIdx;
buffer[vp8_fixed_payload_descriptor_bytes_ ++*extension_length;
+ *extension_length] = hdr_info_.tl0PicIdx; return 0;
++*extension_length;
return 0;
} }
int RtpFormatVp8::WriteTIDAndKeyIdxFields(WebRtc_UWord8* x_field, int RtpFormatVp8::WriteTIDAndKeyIdxFields(WebRtc_UWord8* x_field,
WebRtc_UWord8* buffer, WebRtc_UWord8* buffer,
int buffer_length, int buffer_length,
int* extension_length) const int* extension_length) const {
{ if (buffer_length < vp8_fixed_payload_descriptor_bytes_ + *extension_length
if (buffer_length < vp8_fixed_payload_descriptor_bytes_ + *extension_length + 1) {
+ 1) return -1;
{ }
return -1; WebRtc_UWord8* data_field =
} &buffer[vp8_fixed_payload_descriptor_bytes_ + *extension_length];
WebRtc_UWord8* data_field = *data_field = 0;
&buffer[vp8_fixed_payload_descriptor_bytes_ + *extension_length]; if (TIDFieldPresent()) {
*data_field = 0; *x_field |= kTBit;
if (TIDFieldPresent()) *data_field |= hdr_info_.temporalIdx << 5;
{ }
*x_field |= kTBit; if (KeyIdxFieldPresent()) {
*data_field |= hdr_info_.temporalIdx << 5; *x_field |= kKBit;
} *data_field |= (hdr_info_.keyIdx & kKeyIdxField);
if (KeyIdxFieldPresent()) }
{ ++*extension_length;
*x_field |= kKBit; return 0;
*data_field |= (hdr_info_.keyIdx & kKeyIdxField); }
}
++*extension_length; int RtpFormatVp8::PayloadDescriptorExtraLength() const {
int length_bytes = PictureIdLength();
if (TL0PicIdxFieldPresent()) ++length_bytes;
if (TIDFieldPresent() || KeyIdxFieldPresent()) ++length_bytes;
if (length_bytes > 0) ++length_bytes; // Include the extension field.
return length_bytes;
}
int RtpFormatVp8::PictureIdLength() const {
if (hdr_info_.pictureId == kNoPictureId) {
return 0; return 0;
}
if (hdr_info_.pictureId <= 0x7F) {
return 1;
}
return 2;
} }
int RtpFormatVp8::PayloadDescriptorExtraLength() const bool RtpFormatVp8::XFieldPresent() const {
{ return (TIDFieldPresent() || TL0PicIdxFieldPresent() || PictureIdPresent()
int length_bytes = PictureIdLength(); || KeyIdxFieldPresent());
if (TL0PicIdxFieldPresent()) ++length_bytes;
if (TIDFieldPresent() || KeyIdxFieldPresent()) ++length_bytes;
if (length_bytes > 0) ++length_bytes; // Include the extension field.
return length_bytes;
} }
int RtpFormatVp8::PictureIdLength() const bool RtpFormatVp8::TIDFieldPresent() const {
{ return (hdr_info_.temporalIdx != kNoTemporalIdx);
if (hdr_info_.pictureId == kNoPictureId)
{
return 0;
}
if (hdr_info_.pictureId <= 0x7F)
{
return 1;
}
return 2;
} }
bool RtpFormatVp8::XFieldPresent() const bool RtpFormatVp8::KeyIdxFieldPresent() const {
{ return (hdr_info_.keyIdx != kNoKeyIdx);
return (TIDFieldPresent() || TL0PicIdxFieldPresent() || PictureIdPresent()
|| KeyIdxFieldPresent());
} }
bool RtpFormatVp8::TIDFieldPresent() const bool RtpFormatVp8::TL0PicIdxFieldPresent() const {
{ return (hdr_info_.tl0PicIdx != kNoTl0PicIdx);
return (hdr_info_.temporalIdx != kNoTemporalIdx);
} }
} // namespace webrtc
bool RtpFormatVp8::KeyIdxFieldPresent() const
{
return (hdr_info_.keyIdx != kNoKeyIdx);
}
bool RtpFormatVp8::TL0PicIdxFieldPresent() const
{
return (hdr_info_.tl0PicIdx != kNoTl0PicIdx);
}
} // namespace webrtc

View File

@ -22,143 +22,140 @@
* false as long as there are more packets left to fetch. * false as long as there are more packets left to fetch.
*/ */
#ifndef WEBRTC_MODULES_RTP_RTCP_RTP_FORMAT_VP8_H_ #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_H_
#define WEBRTC_MODULES_RTP_RTCP_RTP_FORMAT_VP8_H_ #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_H_
#include "module_common_types.h" #include "modules/interface/module_common_types.h"
#include "typedefs.h" #include "typedefs.h" // NOLINT(build/include)
namespace webrtc namespace webrtc {
{
enum VP8PacketizerMode enum VP8PacketizerMode {
{ kStrict = 0, // Split partitions if too large;
kStrict = 0, // split partitions if too large; never aggregate, balance size // never aggregate, balance size.
kAggregate, // split partitions if too large; aggregate whole partitions kAggregate, // Split partitions if too large; aggregate whole partitions.
kSloppy, // split entire payload without considering partition limits kSloppy, // Split entire payload without considering partition limits.
kNumModes, kNumModes,
}; };
// Packetizer for VP8. // Packetizer for VP8.
class RtpFormatVp8 class RtpFormatVp8 {
{ public:
public: // Initialize with payload from encoder and fragmentation info.
// Initialize with payload from encoder and fragmentation info. // The payload_data must be exactly one encoded VP8 frame.
// The payload_data must be exactly one encoded VP8 frame. RtpFormatVp8(const WebRtc_UWord8* payload_data,
RtpFormatVp8(const WebRtc_UWord8* payload_data, WebRtc_UWord32 payload_size,
WebRtc_UWord32 payload_size, const RTPVideoHeaderVP8& hdr_info,
const RTPVideoHeaderVP8& hdr_info, const RTPFragmentationHeader& fragmentation,
const RTPFragmentationHeader& fragmentation, VP8PacketizerMode mode);
VP8PacketizerMode mode);
// Initialize without fragmentation info. Mode kSloppy will be used. // Initialize without fragmentation info. Mode kSloppy will be used.
// The payload_data must be exactly one encoded VP8 frame. // The payload_data must be exactly one encoded VP8 frame.
RtpFormatVp8(const WebRtc_UWord8* payload_data, RtpFormatVp8(const WebRtc_UWord8* payload_data,
WebRtc_UWord32 payload_size, WebRtc_UWord32 payload_size,
const RTPVideoHeaderVP8& hdr_info); const RTPVideoHeaderVP8& hdr_info);
// Get the next payload with VP8 payload header. // Get the next payload with VP8 payload header.
// max_payload_len limits the sum length of payload and VP8 payload header. // max_payload_len limits the sum length of payload and VP8 payload header.
// buffer is a pointer to where the output will be written. // buffer is a pointer to where the output will be written.
// bytes_to_send is an output variable that will contain number of bytes // bytes_to_send is an output variable that will contain number of bytes
// written to buffer. Parameter last_packet is true for the last packet of // written to buffer. Parameter last_packet is true for the last packet of
// the frame, false otherwise (i.e., call the function again to get the // the frame, false otherwise (i.e., call the function again to get the
// next packet). Returns the partition index from which the first payload // next packet). Returns the partition index from which the first payload
// byte in the packet is taken, with the first partition having index 0; // byte in the packet is taken, with the first partition having index 0;
// returns negative on error. // returns negative on error.
int NextPacket(int max_payload_len, WebRtc_UWord8* buffer, int NextPacket(int max_payload_len, WebRtc_UWord8* buffer,
int* bytes_to_send, bool* last_packet); int* bytes_to_send, bool* last_packet);
private: private:
enum AggregationMode enum AggregationMode {
{ kAggrNone = 0, // No aggregation.
kAggrNone = 0, // no aggregation kAggrPartitions, // Aggregate intact partitions.
kAggrPartitions, // aggregate intact partitions kAggrFragments // Aggregate intact and fragmented partitions.
kAggrFragments // aggregate intact and fragmented partitions };
};
static const AggregationMode aggr_modes_[kNumModes]; static const AggregationMode aggr_modes_[kNumModes];
static const bool balance_modes_[kNumModes]; static const bool balance_modes_[kNumModes];
static const bool separate_first_modes_[kNumModes]; static const bool separate_first_modes_[kNumModes];
static const int kXBit = 0x80; static const int kXBit = 0x80;
static const int kNBit = 0x20; static const int kNBit = 0x20;
static const int kSBit = 0x10; static const int kSBit = 0x10;
static const int kPartIdField = 0x0F; static const int kPartIdField = 0x0F;
static const int kKeyIdxField = 0x1F; static const int kKeyIdxField = 0x1F;
static const int kIBit = 0x80; static const int kIBit = 0x80;
static const int kLBit = 0x40; static const int kLBit = 0x40;
static const int kTBit = 0x20; static const int kTBit = 0x20;
static const int kKBit = 0x10; static const int kKBit = 0x10;
// Calculate size of next chunk to send. Returns 0 if none can be sent. // Calculate size of next chunk to send. Returns 0 if none can be sent.
int CalcNextSize(int max_payload_len, int remaining_bytes, int CalcNextSize(int max_payload_len, int remaining_bytes,
bool split_payload) const; bool split_payload) const;
// Write the payload header and copy the payload to the buffer. // Write the payload header and copy the payload to the buffer.
// Will copy send_bytes bytes from the current position on the payload data. // Will copy send_bytes bytes from the current position on the payload data.
// last_fragment indicates that this packet ends with the last byte of a // last_fragment indicates that this packet ends with the last byte of a
// partition. // partition.
int WriteHeaderAndPayload(int send_bytes, WebRtc_UWord8* buffer, int WriteHeaderAndPayload(int send_bytes, WebRtc_UWord8* buffer,
int buffer_length); int buffer_length);
// Write the X field and the appropriate extension fields to buffer. // Write the X field and the appropriate extension fields to buffer.
// The function returns the extension length (including X field), or -1 // The function returns the extension length (including X field), or -1
// on error. // on error.
int WriteExtensionFields(WebRtc_UWord8* buffer, int buffer_length) const; int WriteExtensionFields(WebRtc_UWord8* buffer, int buffer_length) const;
// Set the I bit in the x_field, and write PictureID to the appropriate // Set the I bit in the x_field, and write PictureID to the appropriate
// position in buffer. The function returns 0 on success, -1 otherwise. // position in buffer. The function returns 0 on success, -1 otherwise.
int WritePictureIDFields(WebRtc_UWord8* x_field, WebRtc_UWord8* buffer, int WritePictureIDFields(WebRtc_UWord8* x_field, WebRtc_UWord8* buffer,
int buffer_length, int* extension_length) const; int buffer_length, int* extension_length) const;
// Set the L bit in the x_field, and write Tl0PicIdx to the appropriate // Set the L bit in the x_field, and write Tl0PicIdx to the appropriate
// position in buffer. The function returns 0 on success, -1 otherwise. // position in buffer. The function returns 0 on success, -1 otherwise.
int WriteTl0PicIdxFields(WebRtc_UWord8* x_field, WebRtc_UWord8* buffer, int WriteTl0PicIdxFields(WebRtc_UWord8* x_field, WebRtc_UWord8* buffer,
int buffer_length, int* extension_length) const; int buffer_length, int* extension_length) const;
// Set the T and K bits in the x_field, and write TID and KeyIdx to the // Set the T and K bits in the x_field, and write TID and KeyIdx to the
// appropriate position in buffer. The function returns 0 on success, // appropriate position in buffer. The function returns 0 on success,
// -1 otherwise. // -1 otherwise.
int WriteTIDAndKeyIdxFields(WebRtc_UWord8* x_field, WebRtc_UWord8* buffer, int WriteTIDAndKeyIdxFields(WebRtc_UWord8* x_field, WebRtc_UWord8* buffer,
int buffer_length, int* extension_length) const; int buffer_length, int* extension_length) const;
// Write the PictureID from codec_specific_info_ to buffer. One or two // Write the PictureID from codec_specific_info_ to buffer. One or two
// bytes are written, depending on magnitude of PictureID. The function // bytes are written, depending on magnitude of PictureID. The function
// returns the number of bytes written. // returns the number of bytes written.
int WritePictureID(WebRtc_UWord8* buffer, int buffer_length) const; int WritePictureID(WebRtc_UWord8* buffer, int buffer_length) const;
// Calculate and return length (octets) of the variable header fields in // Calculate and return length (octets) of the variable header fields in
// the next header (i.e., header length in addition to vp8_header_bytes_). // the next header (i.e., header length in addition to vp8_header_bytes_).
int PayloadDescriptorExtraLength() const; int PayloadDescriptorExtraLength() const;
// Calculate and return length (octets) of PictureID field in the next // Calculate and return length (octets) of PictureID field in the next
// header. Can be 0, 1, or 2. // header. Can be 0, 1, or 2.
int PictureIdLength() const; int PictureIdLength() const;
// Check whether each of the optional fields will be included in the header. // Check whether each of the optional fields will be included in the header.
bool XFieldPresent() const; bool XFieldPresent() const;
bool TIDFieldPresent() const; bool TIDFieldPresent() const;
bool KeyIdxFieldPresent() const; bool KeyIdxFieldPresent() const;
bool TL0PicIdxFieldPresent() const; bool TL0PicIdxFieldPresent() const;
bool PictureIdPresent() const { return (PictureIdLength() > 0); } bool PictureIdPresent() const { return (PictureIdLength() > 0); }
const WebRtc_UWord8* payload_data_; const WebRtc_UWord8* payload_data_;
const int payload_size_; const int payload_size_;
RTPFragmentationHeader part_info_; RTPFragmentationHeader part_info_;
int payload_bytes_sent_; int payload_bytes_sent_;
int part_ix_; int part_ix_;
bool beginning_; // first partition in this frame bool beginning_; // First partition in this frame.
bool first_fragment_; // first fragment of a partition bool first_fragment_; // First fragment of a partition.
const int vp8_fixed_payload_descriptor_bytes_; // length of VP8 payload const int vp8_fixed_payload_descriptor_bytes_; // Length of VP8 payload
// descriptors's fixed part // descriptors's fixed part.
AggregationMode aggr_mode_; AggregationMode aggr_mode_;
bool balance_; bool balance_;
bool separate_first_; bool separate_first_;
const RTPVideoHeaderVP8 hdr_info_; const RTPVideoHeaderVP8 hdr_info_;
int first_partition_in_packet_; int first_partition_in_packet_;
}; };
} } // namespace
#endif /* WEBRTC_MODULES_RTP_RTCP_RTP_FORMAT_VP8_H_ */ #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_H_

View File

@ -15,17 +15,17 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "typedefs.h" #include "modules/rtp_rtcp/source/rtp_format_vp8.h"
#include "rtp_format_vp8.h" #include "typedefs.h" // NOLINT(build/include)
namespace webrtc { namespace webrtc {
const int kPayloadSize = 30; const int kPayloadSize = 30;
const int kBufferSize = kPayloadSize + 6; // Add space for payload descriptor. const int kBufferSize = kPayloadSize + 6; // Add space for payload descriptor.
class RtpFormatVp8Test : public ::testing::Test { class RtpFormatVp8Test : public ::testing::Test {
protected: protected:
RtpFormatVp8Test() {}; RtpFormatVp8Test() {}
virtual void SetUp(); virtual void SetUp();
virtual void TearDown(); virtual void TearDown();
void CheckHeader(bool first_in_frame, bool frag_start, int part_id); void CheckHeader(bool first_in_frame, bool frag_start, int part_id);
@ -35,7 +35,7 @@ class RtpFormatVp8Test : public ::testing::Test {
void CheckPayload(int payload_end); void CheckPayload(int payload_end);
void CheckLast(bool last) const; void CheckLast(bool last) const;
void CheckPacket(int send_bytes, int expect_bytes, bool last, void CheckPacket(int send_bytes, int expect_bytes, bool last,
bool first_in_frame, bool frag_start); bool first_in_frame, bool frag_start);
void CheckPacketZeroPartId(int send_bytes, int expect_bytes, bool last, void CheckPacketZeroPartId(int send_bytes, int expect_bytes, bool last,
bool first_in_frame, bool frag_start); bool first_in_frame, bool frag_start);
WebRtc_UWord8 payload_data_[kPayloadSize]; WebRtc_UWord8 payload_data_[kPayloadSize];
@ -47,455 +47,420 @@ class RtpFormatVp8Test : public ::testing::Test {
}; };
void RtpFormatVp8Test::SetUp() { void RtpFormatVp8Test::SetUp() {
for (int i = 0; i < kPayloadSize; i++) for (int i = 0; i < kPayloadSize; i++) {
{ payload_data_[i] = i / 10; // Integer division.
payload_data_[i] = i / 10; // integer division }
} data_ptr_ = payload_data_;
data_ptr_ = payload_data_;
fragmentation_ = new RTPFragmentationHeader; fragmentation_ = new RTPFragmentationHeader;
fragmentation_->VerifyAndAllocateFragmentationHeader(3); fragmentation_->VerifyAndAllocateFragmentationHeader(3);
fragmentation_->fragmentationLength[0] = 10; fragmentation_->fragmentationLength[0] = 10;
fragmentation_->fragmentationLength[1] = 10; fragmentation_->fragmentationLength[1] = 10;
fragmentation_->fragmentationLength[2] = 10; fragmentation_->fragmentationLength[2] = 10;
fragmentation_->fragmentationOffset[0] = 0; fragmentation_->fragmentationOffset[0] = 0;
fragmentation_->fragmentationOffset[1] = 10; fragmentation_->fragmentationOffset[1] = 10;
fragmentation_->fragmentationOffset[2] = 20; fragmentation_->fragmentationOffset[2] = 20;
hdr_info_.pictureId = kNoPictureId; hdr_info_.pictureId = kNoPictureId;
hdr_info_.nonReference = false; hdr_info_.nonReference = false;
hdr_info_.temporalIdx = kNoTemporalIdx; hdr_info_.temporalIdx = kNoTemporalIdx;
hdr_info_.tl0PicIdx = kNoTl0PicIdx; hdr_info_.tl0PicIdx = kNoTl0PicIdx;
hdr_info_.keyIdx = kNoKeyIdx; hdr_info_.keyIdx = kNoKeyIdx;
} }
void RtpFormatVp8Test::TearDown() { void RtpFormatVp8Test::TearDown() {
delete fragmentation_; delete fragmentation_;
} }
// First octet tests // First octet tests.
#define EXPECT_BIT_EQ(x,n,a) EXPECT_EQ((((x)>>n)&0x1), a) #define EXPECT_BIT_EQ(x, n, a) EXPECT_EQ((((x) >> (n)) & 0x1), a)
#define EXPECT_RSV_ZERO(x) EXPECT_EQ(((x)&0xE0), 0) #define EXPECT_RSV_ZERO(x) EXPECT_EQ(((x) & 0xE0), 0)
#define EXPECT_BIT_X_EQ(x,a) EXPECT_BIT_EQ(x, 7, a) #define EXPECT_BIT_X_EQ(x, a) EXPECT_BIT_EQ(x, 7, a)
#define EXPECT_BIT_N_EQ(x,a) EXPECT_BIT_EQ(x, 5, a) #define EXPECT_BIT_N_EQ(x, a) EXPECT_BIT_EQ(x, 5, a)
#define EXPECT_BIT_S_EQ(x,a) EXPECT_BIT_EQ(x, 4, a) #define EXPECT_BIT_S_EQ(x, a) EXPECT_BIT_EQ(x, 4, a)
#define EXPECT_PART_ID_EQ(x, a) EXPECT_EQ(((x)&0x0F), a) #define EXPECT_PART_ID_EQ(x, a) EXPECT_EQ(((x) & 0x0F), a)
// Extension fields tests // Extension fields tests
#define EXPECT_BIT_I_EQ(x,a) EXPECT_BIT_EQ(x, 7, a) #define EXPECT_BIT_I_EQ(x, a) EXPECT_BIT_EQ(x, 7, a)
#define EXPECT_BIT_L_EQ(x,a) EXPECT_BIT_EQ(x, 6, a) #define EXPECT_BIT_L_EQ(x, a) EXPECT_BIT_EQ(x, 6, a)
#define EXPECT_BIT_T_EQ(x,a) EXPECT_BIT_EQ(x, 5, a) #define EXPECT_BIT_T_EQ(x, a) EXPECT_BIT_EQ(x, 5, a)
#define EXPECT_BIT_K_EQ(x,a) EXPECT_BIT_EQ(x, 4, a) #define EXPECT_BIT_K_EQ(x, a) EXPECT_BIT_EQ(x, 4, a)
#define EXPECT_TID_EQ(x,a) EXPECT_EQ((((x) & 0xE0) >> 5), a) #define EXPECT_TID_EQ(x, a) EXPECT_EQ((((x) & 0xE0) >> 5), a)
#define EXPECT_KEYIDX_EQ(x,a) EXPECT_EQ(((x) & 0x1F), a) #define EXPECT_KEYIDX_EQ(x, a) EXPECT_EQ(((x) & 0x1F), a)
void RtpFormatVp8Test::CheckHeader(bool first_in_frame, bool frag_start, void RtpFormatVp8Test::CheckHeader(bool first_in_frame, bool frag_start,
int part_id) int part_id) {
{ payload_start_ = 1;
payload_start_ = 1; EXPECT_BIT_EQ(buffer_[0], 6, 0); // Check reserved bit.
EXPECT_BIT_EQ(buffer_[0], 6, 0); // check reserved bit
if (hdr_info_.pictureId != kNoPictureId || if (hdr_info_.pictureId != kNoPictureId ||
hdr_info_.temporalIdx != kNoTemporalIdx || hdr_info_.temporalIdx != kNoTemporalIdx ||
hdr_info_.tl0PicIdx != kNoTl0PicIdx || hdr_info_.tl0PicIdx != kNoTl0PicIdx ||
hdr_info_.keyIdx != kNoKeyIdx) hdr_info_.keyIdx != kNoKeyIdx) {
{ EXPECT_BIT_X_EQ(buffer_[0], 1);
EXPECT_BIT_X_EQ(buffer_[0], 1);
++payload_start_;
CheckPictureID();
CheckTl0PicIdx();
CheckTIDAndKeyIdx();
}
else
{
EXPECT_BIT_X_EQ(buffer_[0], 0);
}
EXPECT_BIT_N_EQ(buffer_[0], 0);
EXPECT_BIT_S_EQ(buffer_[0], frag_start);
// Check partition index.
if (part_id < 0)
{
// (Payload data is the same as the partition index.)
EXPECT_EQ(buffer_[0] & 0x0F, buffer_[payload_start_]);
}
else
{
EXPECT_EQ(buffer_[0] & 0x0F, part_id);
}
}
void RtpFormatVp8Test::CheckPictureID()
{
if (hdr_info_.pictureId != kNoPictureId)
{
EXPECT_BIT_I_EQ(buffer_[1], 1);
if (hdr_info_.pictureId > 0x7F)
{
EXPECT_BIT_EQ(buffer_[payload_start_], 7, 1);
EXPECT_EQ(buffer_[payload_start_] & 0x7F,
(hdr_info_.pictureId >> 8) & 0x7F);
EXPECT_EQ(buffer_[payload_start_ + 1],
hdr_info_.pictureId & 0xFF);
payload_start_ += 2;
}
else
{
EXPECT_BIT_EQ(buffer_[payload_start_], 7, 0);
EXPECT_EQ(buffer_[payload_start_] & 0x7F,
(hdr_info_.pictureId) & 0x7F);
payload_start_ += 1;
}
}
else
{
EXPECT_BIT_I_EQ(buffer_[1], 0);
}
}
void RtpFormatVp8Test::CheckTl0PicIdx()
{
if (hdr_info_.tl0PicIdx != kNoTl0PicIdx)
{
EXPECT_BIT_L_EQ(buffer_[1], 1);
EXPECT_EQ(buffer_[payload_start_], hdr_info_.tl0PicIdx);
++payload_start_;
}
else
{
EXPECT_BIT_L_EQ(buffer_[1], 0);
}
}
void RtpFormatVp8Test::CheckTIDAndKeyIdx()
{
if (hdr_info_.temporalIdx == kNoTemporalIdx &&
hdr_info_.keyIdx == kNoKeyIdx)
{
EXPECT_BIT_T_EQ(buffer_[1], 0);
EXPECT_BIT_K_EQ(buffer_[1], 0);
return;
}
if (hdr_info_.temporalIdx != kNoTemporalIdx)
{
EXPECT_BIT_T_EQ(buffer_[1], 1);
EXPECT_TID_EQ(buffer_[payload_start_], hdr_info_.temporalIdx);
}
else
{
EXPECT_BIT_T_EQ(buffer_[1], 0);
EXPECT_TID_EQ(buffer_[payload_start_], 0);
}
if (hdr_info_.keyIdx != kNoKeyIdx)
{
EXPECT_BIT_K_EQ(buffer_[1], 1);
EXPECT_KEYIDX_EQ(buffer_[payload_start_], hdr_info_.keyIdx);
}
else
{
EXPECT_BIT_K_EQ(buffer_[1], 0);
EXPECT_KEYIDX_EQ(buffer_[payload_start_], 0);
}
++payload_start_; ++payload_start_;
CheckPictureID();
CheckTl0PicIdx();
CheckTIDAndKeyIdx();
} else {
EXPECT_BIT_X_EQ(buffer_[0], 0);
}
EXPECT_BIT_N_EQ(buffer_[0], 0);
EXPECT_BIT_S_EQ(buffer_[0], frag_start);
// Check partition index.
if (part_id < 0) {
// (Payload data is the same as the partition index.)
EXPECT_EQ(buffer_[0] & 0x0F, buffer_[payload_start_]);
} else {
EXPECT_EQ(buffer_[0] & 0x0F, part_id);
}
} }
void RtpFormatVp8Test::CheckPayload(int payload_end) void RtpFormatVp8Test::CheckPictureID() {
{ if (hdr_info_.pictureId != kNoPictureId) {
for (int i = payload_start_; i < payload_end; i++, data_ptr_++) EXPECT_BIT_I_EQ(buffer_[1], 1);
EXPECT_EQ(buffer_[i], *data_ptr_); if (hdr_info_.pictureId > 0x7F) {
EXPECT_BIT_EQ(buffer_[payload_start_], 7, 1);
EXPECT_EQ(buffer_[payload_start_] & 0x7F,
(hdr_info_.pictureId >> 8) & 0x7F);
EXPECT_EQ(buffer_[payload_start_ + 1],
hdr_info_.pictureId & 0xFF);
payload_start_ += 2;
} else {
EXPECT_BIT_EQ(buffer_[payload_start_], 7, 0);
EXPECT_EQ(buffer_[payload_start_] & 0x7F,
(hdr_info_.pictureId) & 0x7F);
payload_start_ += 1;
}
} else {
EXPECT_BIT_I_EQ(buffer_[1], 0);
}
} }
void RtpFormatVp8Test::CheckLast(bool last) const void RtpFormatVp8Test::CheckTl0PicIdx() {
{ if (hdr_info_.tl0PicIdx != kNoTl0PicIdx) {
EXPECT_EQ(last, data_ptr_ == payload_data_ + kPayloadSize); EXPECT_BIT_L_EQ(buffer_[1], 1);
EXPECT_EQ(buffer_[payload_start_], hdr_info_.tl0PicIdx);
++payload_start_;
} else {
EXPECT_BIT_L_EQ(buffer_[1], 0);
}
}
void RtpFormatVp8Test::CheckTIDAndKeyIdx() {
if (hdr_info_.temporalIdx == kNoTemporalIdx &&
hdr_info_.keyIdx == kNoKeyIdx) {
EXPECT_BIT_T_EQ(buffer_[1], 0);
EXPECT_BIT_K_EQ(buffer_[1], 0);
return;
}
if (hdr_info_.temporalIdx != kNoTemporalIdx) {
EXPECT_BIT_T_EQ(buffer_[1], 1);
EXPECT_TID_EQ(buffer_[payload_start_], hdr_info_.temporalIdx);
} else {
EXPECT_BIT_T_EQ(buffer_[1], 0);
EXPECT_TID_EQ(buffer_[payload_start_], 0);
}
if (hdr_info_.keyIdx != kNoKeyIdx) {
EXPECT_BIT_K_EQ(buffer_[1], 1);
EXPECT_KEYIDX_EQ(buffer_[payload_start_], hdr_info_.keyIdx);
} else {
EXPECT_BIT_K_EQ(buffer_[1], 0);
EXPECT_KEYIDX_EQ(buffer_[payload_start_], 0);
}
++payload_start_;
}
void RtpFormatVp8Test::CheckPayload(int payload_end) {
for (int i = payload_start_; i < payload_end; i++, data_ptr_++)
EXPECT_EQ(buffer_[i], *data_ptr_);
}
void RtpFormatVp8Test::CheckLast(bool last) const {
EXPECT_EQ(last, data_ptr_ == payload_data_ + kPayloadSize);
} }
void RtpFormatVp8Test::CheckPacket(int send_bytes, int expect_bytes, bool last, void RtpFormatVp8Test::CheckPacket(int send_bytes, int expect_bytes, bool last,
bool first_in_frame, bool frag_start) bool first_in_frame, bool frag_start) {
{ EXPECT_EQ(send_bytes, expect_bytes);
EXPECT_EQ(send_bytes, expect_bytes); CheckHeader(first_in_frame, frag_start, -1);
CheckHeader(first_in_frame, frag_start, -1); CheckPayload(send_bytes);
CheckPayload(send_bytes); CheckLast(last);
CheckLast(last);
} }
void RtpFormatVp8Test::CheckPacketZeroPartId(int send_bytes, void RtpFormatVp8Test::CheckPacketZeroPartId(int send_bytes,
int expect_bytes, int expect_bytes,
bool last, bool last,
bool first_in_frame, bool first_in_frame,
bool frag_start) bool frag_start) {
{ EXPECT_EQ(send_bytes, expect_bytes);
EXPECT_EQ(send_bytes, expect_bytes); CheckHeader(first_in_frame, frag_start, 0);
CheckHeader(first_in_frame, frag_start, 0); CheckPayload(send_bytes);
CheckPayload(send_bytes); CheckLast(last);
CheckLast(last);
} }
TEST_F(RtpFormatVp8Test, TestStrictMode) TEST_F(RtpFormatVp8Test, TestStrictMode) {
{ int send_bytes = 0;
int send_bytes = 0; bool last;
bool last; bool first_in_frame = true;
bool first_in_frame = true;
hdr_info_.pictureId = 200; // > 0x7F should produce 2-byte PictureID hdr_info_.pictureId = 200; // > 0x7F should produce 2-byte PictureID.
RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize, RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize,
hdr_info_, *fragmentation_, kStrict); hdr_info_, *fragmentation_, kStrict);
// get first packet, expect balanced size ~= same as second packet // Get first packet, expect balanced size ~= same as second packet.
EXPECT_EQ(0, packetizer.NextPacket(13, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(13, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 8, last, CheckPacket(send_bytes, 8, last,
first_in_frame, first_in_frame,
/* frag_start */ true); /* frag_start */ true);
first_in_frame = false; first_in_frame = false;
// get second packet // Get second packet.
EXPECT_EQ(0, packetizer.NextPacket(13, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(13, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 10, last, CheckPacket(send_bytes, 10, last,
first_in_frame, first_in_frame,
/* frag_start */ false); /* frag_start */ false);
// Second partition // Second partition.
// Get first (and only) packet // Get first (and only) packet.
EXPECT_EQ(1, packetizer.NextPacket(20, buffer_, &send_bytes, &last)); EXPECT_EQ(1, packetizer.NextPacket(20, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 14, last, CheckPacket(send_bytes, 14, last,
first_in_frame, first_in_frame,
/* frag_start */ true); /* frag_start */ true);
// Third partition // Third partition.
// Get first packet (of four) // Get first packet (of four).
EXPECT_EQ(2, packetizer.NextPacket(7, buffer_, &send_bytes, &last)); EXPECT_EQ(2, packetizer.NextPacket(7, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 5, last, CheckPacket(send_bytes, 5, last,
first_in_frame, first_in_frame,
/* frag_start */ true); /* frag_start */ true);
// Get second packet (of four) // Get second packet (of four).
EXPECT_EQ(2, packetizer.NextPacket(7, buffer_, &send_bytes, &last)); EXPECT_EQ(2, packetizer.NextPacket(7, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 5, last, CheckPacket(send_bytes, 5, last,
first_in_frame, first_in_frame,
/* frag_start */ false); /* frag_start */ false);
// Get third packet (of four) // Get third packet (of four).
EXPECT_EQ(2, packetizer.NextPacket(7, buffer_, &send_bytes, &last)); EXPECT_EQ(2, packetizer.NextPacket(7, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 7, last, CheckPacket(send_bytes, 7, last,
first_in_frame, first_in_frame,
/* frag_start */ false); /* frag_start */ false);
// Get fourth and last packet
EXPECT_EQ(2, packetizer.NextPacket(7, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 5, last,
first_in_frame,
/* frag_start */ false);
// Get fourth and last packet.
EXPECT_EQ(2, packetizer.NextPacket(7, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 5, last,
first_in_frame,
/* frag_start */ false);
} }
TEST_F(RtpFormatVp8Test, TestAggregateMode) TEST_F(RtpFormatVp8Test, TestAggregateMode) {
{ int send_bytes = 0;
int send_bytes = 0; bool last;
bool last; bool first_in_frame = true;
bool first_in_frame = true;
hdr_info_.pictureId = 20; // <= 0x7F should produce 1-byte PictureID hdr_info_.pictureId = 20; // <= 0x7F should produce 1-byte PictureID.
RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize, RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize,
hdr_info_, *fragmentation_, kAggregate); hdr_info_, *fragmentation_,
kAggregate);
// get first packet // Get first packet.
// first part of first partition (balanced fragments are expected) // First part of first partition (balanced fragments are expected).
EXPECT_EQ(0, packetizer.NextPacket(8, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(8, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 7, last, CheckPacket(send_bytes, 7, last,
first_in_frame, first_in_frame,
/* frag_start */ true); /* frag_start */ true);
first_in_frame = false; first_in_frame = false;
// get second packet // Get second packet.
// second fragment of first partition // Second fragment of first partition.
EXPECT_EQ(0, packetizer.NextPacket(8, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(8, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 5, last, CheckPacket(send_bytes, 5, last,
first_in_frame, first_in_frame,
/* frag_start */ false); /* frag_start */ false);
// get third packet // Get third packet.
// third fragment of first partition // Third fragment of first partition.
EXPECT_EQ(0, packetizer.NextPacket(8, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(8, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 7, last, CheckPacket(send_bytes, 7, last,
first_in_frame, first_in_frame,
/* frag_start */ false); /* frag_start */ false);
// get fourth packet
// last two partitions aggregated
EXPECT_EQ(1, packetizer.NextPacket(25, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 23, last,
first_in_frame,
/* frag_start */ true);
// Get fourth packet.
// Last two partitions aggregated.
EXPECT_EQ(1, packetizer.NextPacket(25, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 23, last,
first_in_frame,
/* frag_start */ true);
} }
TEST_F(RtpFormatVp8Test, TestSloppyMode) TEST_F(RtpFormatVp8Test, TestSloppyMode) {
{ int send_bytes = 0;
int send_bytes = 0; bool last;
bool last; bool first_in_frame = true;
bool first_in_frame = true;
hdr_info_.pictureId = kNoPictureId; // no PictureID hdr_info_.pictureId = kNoPictureId; // No PictureID.
RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize, RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize,
hdr_info_, *fragmentation_, kSloppy); hdr_info_, *fragmentation_, kSloppy);
// get first packet // Get first packet.
EXPECT_EQ(0, packetizer.NextPacket(9, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(9, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 9, last, CheckPacket(send_bytes, 9, last,
first_in_frame, first_in_frame,
/* frag_start */ true); /* frag_start */ true);
first_in_frame = false; first_in_frame = false;
// get second packet // Get second packet.
// fragments of first and second partitions // Fragments of first and second partitions.
EXPECT_EQ(0, packetizer.NextPacket(9, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(9, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 9, last, CheckPacket(send_bytes, 9, last,
first_in_frame, first_in_frame,
/* frag_start */ false); /* frag_start */ false);
// get third packet // Get third packet.
// fragments of second and third partitions // Fragments of second and third partitions.
EXPECT_EQ(1, packetizer.NextPacket(9, buffer_, &send_bytes, &last)); EXPECT_EQ(1, packetizer.NextPacket(9, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 9, last, CheckPacket(send_bytes, 9, last,
first_in_frame, first_in_frame,
/* frag_start */ false); /* frag_start */ false);
// get fourth packet
// second half of last partition
EXPECT_EQ(2, packetizer.NextPacket(9, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 7, last,
first_in_frame,
/* frag_start */ false);
// Get fourth packet.
// Second half of last partition.
EXPECT_EQ(2, packetizer.NextPacket(9, buffer_, &send_bytes, &last));
CheckPacket(send_bytes, 7, last,
first_in_frame,
/* frag_start */ false);
} }
// Verify that sloppy mode is forced if fragmentation info is missing. // Verify that sloppy mode is forced if fragmentation info is missing.
TEST_F(RtpFormatVp8Test, TestSloppyModeFallback) TEST_F(RtpFormatVp8Test, TestSloppyModeFallback) {
{ int send_bytes = 0;
int send_bytes = 0; bool last;
bool last; bool first_in_frame = true;
bool first_in_frame = true;
hdr_info_.pictureId = 200; // > 0x7F should produce 2-byte PictureID hdr_info_.pictureId = 200; // > 0x7F should produce 2-byte PictureID
RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize, RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize,
hdr_info_); hdr_info_);
// get first packet // Get first packet.
EXPECT_EQ(0, packetizer.NextPacket(10, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(10, buffer_, &send_bytes, &last));
CheckPacketZeroPartId(send_bytes, 10, last, CheckPacketZeroPartId(send_bytes, 10, last,
first_in_frame, first_in_frame,
/* frag_start */ true); /* frag_start */ true);
first_in_frame = false; first_in_frame = false;
// get second packet // Get second packet.
// fragments of first and second partitions // Fragments of first and second partitions.
EXPECT_EQ(0, packetizer.NextPacket(10, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(10, buffer_, &send_bytes, &last));
CheckPacketZeroPartId(send_bytes, 10, last, CheckPacketZeroPartId(send_bytes, 10, last,
first_in_frame, first_in_frame,
/* frag_start */ false); /* frag_start */ false);
// get third packet // Get third packet.
// fragments of second and third partitions // Fragments of second and third partitions.
EXPECT_EQ(0, packetizer.NextPacket(10, buffer_, &send_bytes, &last)); EXPECT_EQ(0, packetizer.NextPacket(10, buffer_, &send_bytes, &last));
CheckPacketZeroPartId(send_bytes, 10, last, CheckPacketZeroPartId(send_bytes, 10, last,
first_in_frame, first_in_frame,
/* frag_start */ false); /* frag_start */ false);
// get fourth packet
// second half of last partition
EXPECT_EQ(0, packetizer.NextPacket(7, buffer_, &send_bytes, &last));
CheckPacketZeroPartId(send_bytes, 7, last,
first_in_frame,
/* frag_start */ false);
// Get fourth packet.
// Second half of last partition.
EXPECT_EQ(0, packetizer.NextPacket(7, buffer_, &send_bytes, &last));
CheckPacketZeroPartId(send_bytes, 7, last,
first_in_frame,
/* frag_start */ false);
} }
// Verify that non-reference bit is set. // Verify that non-reference bit is set.
TEST_F(RtpFormatVp8Test, TestNonReferenceBit) { TEST_F(RtpFormatVp8Test, TestNonReferenceBit) {
int send_bytes = 0; int send_bytes = 0;
bool last; bool last;
hdr_info_.nonReference = true; hdr_info_.nonReference = true;
RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize, RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize,
hdr_info_); hdr_info_);
// get first packet // Get first packet.
ASSERT_EQ(0, packetizer.NextPacket(25, buffer_, &send_bytes, &last)); ASSERT_EQ(0, packetizer.NextPacket(25, buffer_, &send_bytes, &last));
ASSERT_FALSE(last); ASSERT_FALSE(last);
EXPECT_BIT_N_EQ(buffer_[0], 1); EXPECT_BIT_N_EQ(buffer_[0], 1);
// get second packet // Get second packet.
ASSERT_EQ(0, packetizer.NextPacket(25, buffer_, &send_bytes, &last)); ASSERT_EQ(0, packetizer.NextPacket(25, buffer_, &send_bytes, &last));
ASSERT_TRUE(last); ASSERT_TRUE(last);
EXPECT_BIT_N_EQ(buffer_[0], 1); EXPECT_BIT_N_EQ(buffer_[0], 1);
} }
// Verify Tl0PicIdx and TID fields // Verify Tl0PicIdx and TID fields
TEST_F(RtpFormatVp8Test, TestTl0PicIdxAndTID) { TEST_F(RtpFormatVp8Test, TestTl0PicIdxAndTID) {
int send_bytes = 0; int send_bytes = 0;
bool last; bool last;
hdr_info_.tl0PicIdx = 117; hdr_info_.tl0PicIdx = 117;
hdr_info_.temporalIdx = 2; hdr_info_.temporalIdx = 2;
RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize, RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize,
hdr_info_, *fragmentation_, kAggregate); hdr_info_, *fragmentation_,
kAggregate);
// get first and only packet // Get first and only packet.
EXPECT_EQ(0, packetizer.NextPacket(kBufferSize, buffer_, &send_bytes, EXPECT_EQ(0, packetizer.NextPacket(kBufferSize, buffer_, &send_bytes,
&last)); &last));
bool first_in_frame = true; bool first_in_frame = true;
CheckPacket(send_bytes, kPayloadSize + 4, last, CheckPacket(send_bytes, kPayloadSize + 4, last,
first_in_frame, first_in_frame,
/* frag_start */ true); /* frag_start */ true);
} }
// Verify KeyIdx field // Verify KeyIdx field.
TEST_F(RtpFormatVp8Test, TestKeyIdx) { TEST_F(RtpFormatVp8Test, TestKeyIdx) {
int send_bytes = 0; int send_bytes = 0;
bool last; bool last;
hdr_info_.keyIdx = 17; hdr_info_.keyIdx = 17;
RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize, RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize,
hdr_info_, *fragmentation_, kAggregate); hdr_info_, *fragmentation_,
kAggregate);
// get first and only packet // Get first and only packet.
EXPECT_EQ(0, packetizer.NextPacket(kBufferSize, buffer_, &send_bytes, EXPECT_EQ(0, packetizer.NextPacket(kBufferSize, buffer_, &send_bytes,
&last)); &last));
bool first_in_frame = true; bool first_in_frame = true;
CheckPacket(send_bytes, kPayloadSize + 3, last, CheckPacket(send_bytes, kPayloadSize + 3, last,
first_in_frame, first_in_frame,
/* frag_start */ true); /* frag_start */ true);
} }
// Verify TID field and KeyIdx field in combination // Verify TID field and KeyIdx field in combination.
TEST_F(RtpFormatVp8Test, TestTIDAndKeyIdx) { TEST_F(RtpFormatVp8Test, TestTIDAndKeyIdx) {
int send_bytes = 0; int send_bytes = 0;
bool last; bool last;
hdr_info_.temporalIdx = 1; hdr_info_.temporalIdx = 1;
hdr_info_.keyIdx = 5; hdr_info_.keyIdx = 5;
RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize, RtpFormatVp8 packetizer = RtpFormatVp8(payload_data_, kPayloadSize,
hdr_info_, *fragmentation_, kAggregate); hdr_info_, *fragmentation_,
kAggregate);
// get first and only packet // Get first and only packet.
EXPECT_EQ(0, packetizer.NextPacket(kBufferSize, buffer_, &send_bytes, EXPECT_EQ(0, packetizer.NextPacket(kBufferSize, buffer_, &send_bytes,
&last)); &last));
bool first_in_frame = true; bool first_in_frame = true;
CheckPacket(send_bytes, kPayloadSize + 3, last, CheckPacket(send_bytes, kPayloadSize + 3, last,
first_in_frame, first_in_frame,
/* frag_start */ true); /* frag_start */ true);
} }
} // namespace } // namespace