packet_manipulator_unittest.cc revision 2557b86e7648ffebc5781df9f093ca5a84efc219
1/* 2 * Copyright (c) 2012 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/video_coding/codecs/test/packet_manipulator.h" 12 13#include <queue> 14 15#include "testing/gtest/include/gtest/gtest.h" 16#include "webrtc/modules/video_coding/include/video_codec_interface.h" 17#include "webrtc/modules/video_coding/codecs/test/predictive_packet_manipulator.h" 18#include "webrtc/test/testsupport/unittest_utils.h" 19#include "webrtc/typedefs.h" 20 21namespace webrtc { 22namespace test { 23 24const double kNeverDropProbability = 0.0; 25const double kAlwaysDropProbability = 1.0; 26const int kBurstLength = 1; 27 28class PacketManipulatorTest: public PacketRelatedTest { 29 protected: 30 PacketReader packet_reader_; 31 EncodedImage image_; 32 NetworkingConfig drop_config_; 33 NetworkingConfig no_drop_config_; 34 35 PacketManipulatorTest() { 36 image_._buffer = packet_data_; 37 image_._length = kPacketDataLength; 38 image_._size = kPacketDataLength; 39 40 drop_config_.packet_size_in_bytes = kPacketSizeInBytes; 41 drop_config_.packet_loss_probability = kAlwaysDropProbability; 42 drop_config_.packet_loss_burst_length = kBurstLength; 43 drop_config_.packet_loss_mode = kUniform; 44 45 no_drop_config_.packet_size_in_bytes = kPacketSizeInBytes; 46 no_drop_config_.packet_loss_probability = kNeverDropProbability; 47 no_drop_config_.packet_loss_burst_length = kBurstLength; 48 no_drop_config_.packet_loss_mode = kUniform; 49 } 50 51 virtual ~PacketManipulatorTest() {} 52 53 void SetUp() { 54 PacketRelatedTest::SetUp(); 55 } 56 57 void TearDown() { 58 PacketRelatedTest::TearDown(); 59 } 60 61 void VerifyPacketLoss(int expected_nbr_packets_dropped, 62 int actual_nbr_packets_dropped, 63 size_t expected_packet_data_length, 64 uint8_t* expected_packet_data, 65 EncodedImage& actual_image) { 66 EXPECT_EQ(expected_nbr_packets_dropped, actual_nbr_packets_dropped); 67 EXPECT_EQ(expected_packet_data_length, image_._length); 68 EXPECT_EQ(0, memcmp(expected_packet_data, actual_image._buffer, 69 expected_packet_data_length)); 70 } 71}; 72 73TEST_F(PacketManipulatorTest, Constructor) { 74 PacketManipulatorImpl manipulator(&packet_reader_, no_drop_config_, false); 75} 76 77TEST_F(PacketManipulatorTest, DropNone) { 78 PacketManipulatorImpl manipulator(&packet_reader_, no_drop_config_, false); 79 int nbr_packets_dropped = manipulator.ManipulatePackets(&image_); 80 VerifyPacketLoss(0, nbr_packets_dropped, kPacketDataLength, 81 packet_data_, image_); 82} 83 84TEST_F(PacketManipulatorTest, UniformDropNoneSmallFrame) { 85 size_t data_length = 400; // smaller than the packet size 86 image_._length = data_length; 87 PacketManipulatorImpl manipulator(&packet_reader_, no_drop_config_, false); 88 int nbr_packets_dropped = manipulator.ManipulatePackets(&image_); 89 90 VerifyPacketLoss(0, nbr_packets_dropped, data_length, 91 packet_data_, image_); 92} 93 94TEST_F(PacketManipulatorTest, UniformDropAll) { 95 PacketManipulatorImpl manipulator(&packet_reader_, drop_config_, false); 96 int nbr_packets_dropped = manipulator.ManipulatePackets(&image_); 97 VerifyPacketLoss(kPacketDataNumberOfPackets, nbr_packets_dropped, 98 0, packet_data_, image_); 99} 100 101// Use our customized test class to make the second packet being lost 102TEST_F(PacketManipulatorTest, UniformDropSinglePacket) { 103 drop_config_.packet_loss_probability = 0.5; 104 PredictivePacketManipulator manipulator(&packet_reader_, drop_config_); 105 manipulator.AddRandomResult(1.0); 106 manipulator.AddRandomResult(0.3); // less than 0.5 will cause packet loss 107 manipulator.AddRandomResult(1.0); 108 109 // Execute the test target method: 110 int nbr_packets_dropped = manipulator.ManipulatePackets(&image_); 111 112 // Since we setup the predictive packet manipulator, it will throw away the 113 // second packet. The third packet is also lost because when we have lost one, 114 // the remains shall also be discarded (in the current implementation). 115 VerifyPacketLoss(2, nbr_packets_dropped, kPacketSizeInBytes, packet1_, 116 image_); 117} 118 119// Use our customized test class to make the second packet being lost 120TEST_F(PacketManipulatorTest, BurstDropNinePackets) { 121 // Create a longer packet data structure (10 packets) 122 const int kNbrPackets = 10; 123 const size_t kDataLength = kPacketSizeInBytes * kNbrPackets; 124 uint8_t data[kDataLength]; 125 uint8_t* data_pointer = data; 126 // Fill with 0s, 1s and so on to be able to easily verify which were dropped: 127 for (int i = 0; i < kNbrPackets; ++i) { 128 memset(data_pointer + i * kPacketSizeInBytes, i, kPacketSizeInBytes); 129 } 130 // Overwrite the defaults from the test fixture: 131 image_._buffer = data; 132 image_._length = kDataLength; 133 image_._size = kDataLength; 134 135 drop_config_.packet_loss_probability = 0.5; 136 drop_config_.packet_loss_burst_length = 5; 137 drop_config_.packet_loss_mode = kBurst; 138 PredictivePacketManipulator manipulator(&packet_reader_, drop_config_); 139 manipulator.AddRandomResult(1.0); 140 manipulator.AddRandomResult(0.3); // less than 0.5 will cause packet loss 141 for (int i = 0; i < kNbrPackets - 2; ++i) { 142 manipulator.AddRandomResult(1.0); 143 } 144 145 // Execute the test target method: 146 int nbr_packets_dropped = manipulator.ManipulatePackets(&image_); 147 148 // Should discard every packet after the first one. 149 VerifyPacketLoss(9, nbr_packets_dropped, kPacketSizeInBytes, data, image_); 150} 151 152} // namespace test 153} // namespace webrtc 154