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