minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "modules/audio_coding/test/PacketLossTest.h" |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 | [diff] [blame] | 12 | |
kwiberg | 3747838 | 2016-02-15 04:40:57 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Karl Wiberg | 5817d3d | 2018-04-06 08:06:42 | [diff] [blame] | 15 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "test/gtest.h" |
| 17 | #include "test/testsupport/fileutils.h" |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | ReceiverWithPacketLoss::ReceiverWithPacketLoss() |
| 22 | : loss_rate_(0), |
| 23 | burst_length_(1), |
| 24 | packet_counter_(0), |
| 25 | lost_packet_counter_(0), |
| 26 | burst_lost_counter_(burst_length_) { |
| 27 | } |
| 28 | |
| 29 | void ReceiverWithPacketLoss::Setup(AudioCodingModule *acm, |
| 30 | RTPStream *rtpStream, |
| 31 | std::string out_file_name, |
| 32 | int channels, |
| 33 | int loss_rate, |
| 34 | int burst_length) { |
| 35 | loss_rate_ = loss_rate; |
| 36 | burst_length_ = burst_length; |
| 37 | burst_lost_counter_ = burst_length_; // To prevent first packet gets lost. |
| 38 | std::stringstream ss; |
| 39 | ss << out_file_name << "_" << loss_rate_ << "_" << burst_length_ << "_"; |
| 40 | Receiver::Setup(acm, rtpStream, ss.str(), channels); |
| 41 | } |
| 42 | |
| 43 | bool ReceiverWithPacketLoss::IncomingPacket() { |
| 44 | if (!_rtpStream->EndOfFile()) { |
| 45 | if (packet_counter_ == 0) { |
| 46 | _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload, |
| 47 | _payloadSizeBytes, &_nextTime); |
| 48 | if (_realPayloadSizeBytes == 0) { |
| 49 | if (_rtpStream->EndOfFile()) { |
| 50 | packet_counter_ = 0; |
| 51 | return true; |
| 52 | } else { |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (!PacketLost()) { |
| 59 | _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes, _rtpInfo); |
| 60 | } |
| 61 | packet_counter_++; |
| 62 | _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload, |
| 63 | _payloadSizeBytes, &_nextTime); |
| 64 | if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) { |
| 65 | packet_counter_ = 0; |
| 66 | lost_packet_counter_ = 0; |
| 67 | } |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool ReceiverWithPacketLoss::PacketLost() { |
| 73 | if (burst_lost_counter_ < burst_length_) { |
| 74 | lost_packet_counter_++; |
| 75 | burst_lost_counter_++; |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | if (lost_packet_counter_ * 100 < loss_rate_ * packet_counter_) { |
| 80 | lost_packet_counter_++; |
| 81 | burst_lost_counter_ = 1; |
| 82 | return true; |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | SenderWithFEC::SenderWithFEC() |
| 88 | : expected_loss_rate_(0) { |
| 89 | } |
| 90 | |
| 91 | void SenderWithFEC::Setup(AudioCodingModule *acm, RTPStream *rtpStream, |
| 92 | std::string in_file_name, int sample_rate, |
| 93 | int channels, int expected_loss_rate) { |
| 94 | Sender::Setup(acm, rtpStream, in_file_name, sample_rate, channels); |
| 95 | EXPECT_TRUE(SetFEC(true)); |
| 96 | EXPECT_TRUE(SetPacketLossRate(expected_loss_rate)); |
| 97 | } |
| 98 | |
| 99 | bool SenderWithFEC::SetFEC(bool enable_fec) { |
| 100 | if (_acm->SetCodecFEC(enable_fec) == 0) { |
| 101 | return true; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | bool SenderWithFEC::SetPacketLossRate(int expected_loss_rate) { |
| 107 | if (_acm->SetPacketLossRate(expected_loss_rate) == 0) { |
| 108 | expected_loss_rate_ = expected_loss_rate; |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | PacketLossTest::PacketLossTest(int channels, int expected_loss_rate, |
| 115 | int actual_loss_rate, int burst_length) |
| 116 | : channels_(channels), |
| 117 | in_file_name_(channels_ == 1 ? "audio_coding/testfile32kHz" : |
| 118 | "audio_coding/teststereo32kHz"), |
| 119 | sample_rate_hz_(32000), |
| 120 | sender_(new SenderWithFEC), |
| 121 | receiver_(new ReceiverWithPacketLoss), |
| 122 | expected_loss_rate_(expected_loss_rate), |
| 123 | actual_loss_rate_(actual_loss_rate), |
| 124 | burst_length_(burst_length) { |
| 125 | } |
| 126 | |
| 127 | void PacketLossTest::Perform() { |
| 128 | #ifndef WEBRTC_CODEC_OPUS |
| 129 | return; |
| 130 | #else |
Karl Wiberg | 5817d3d | 2018-04-06 08:06:42 | [diff] [blame] | 131 | AudioCodingModule::Config config; |
| 132 | config.decoder_factory = CreateBuiltinAudioDecoderFactory(); |
| 133 | std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(config)); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 | [diff] [blame] | 134 | |
| 135 | int codec_id = acm->Codec("opus", 48000, channels_); |
| 136 | |
| 137 | RTPFile rtpFile; |
pbos@webrtc.org | c86e45d | 2014-10-01 10:05:40 | [diff] [blame] | 138 | std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(), |
| 139 | "packet_loss_test"); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 | [diff] [blame] | 140 | |
| 141 | // Encode to file |
| 142 | rtpFile.Open(fileName.c_str(), "wb+"); |
| 143 | rtpFile.WriteHeader(); |
| 144 | |
| 145 | sender_->testMode = 0; |
| 146 | sender_->codeId = codec_id; |
| 147 | |
| 148 | sender_->Setup(acm.get(), &rtpFile, in_file_name_, sample_rate_hz_, channels_, |
| 149 | expected_loss_rate_); |
kwiberg | 1fd4a4a | 2015-11-03 19:20:50 | [diff] [blame] | 150 | if (acm->SendCodec()) { |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 | [diff] [blame] | 151 | sender_->Run(); |
| 152 | } |
| 153 | sender_->Teardown(); |
| 154 | rtpFile.Close(); |
| 155 | |
| 156 | // Decode to file |
| 157 | rtpFile.Open(fileName.c_str(), "rb"); |
| 158 | rtpFile.ReadHeader(); |
| 159 | |
| 160 | receiver_->testMode = 0; |
| 161 | receiver_->codeId = codec_id; |
| 162 | |
| 163 | receiver_->Setup(acm.get(), &rtpFile, "packetLoss_out", channels_, |
| 164 | actual_loss_rate_, burst_length_); |
| 165 | receiver_->Run(); |
| 166 | receiver_->Teardown(); |
| 167 | rtpFile.Close(); |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | } // namespace webrtc |