blob: a1629fd00646b92a96f02f5b95d0be277f0f32a2 [file] [log] [blame]
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/test/PacketLossTest.h"
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000012
kwiberg37478382016-02-14 20:40:57 -080013#include <memory>
14
Karl Wiberg5817d3d2018-04-06 10:06:42 +020015#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020016#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "test/gtest.h"
18#include "test/testsupport/fileutils.h"
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000019
20namespace webrtc {
21
22ReceiverWithPacketLoss::ReceiverWithPacketLoss()
23 : loss_rate_(0),
24 burst_length_(1),
25 packet_counter_(0),
26 lost_packet_counter_(0),
Yves Gerey665174f2018-06-19 15:03:05 +020027 burst_lost_counter_(burst_length_) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000028
Yves Gerey665174f2018-06-19 15:03:05 +020029void ReceiverWithPacketLoss::Setup(AudioCodingModule* acm,
30 RTPStream* rtpStream,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000031 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.
Jonas Olsson366a50c2018-09-06 13:41:30 +020038 rtc::StringBuilder ss;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000039 ss << out_file_name << "_" << loss_rate_ << "_" << burst_length_ << "_";
40 Receiver::Setup(acm, rtpStream, ss.str(), channels);
41}
42
43bool 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
72bool 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
Yves Gerey665174f2018-06-19 15:03:05 +020087SenderWithFEC::SenderWithFEC() : expected_loss_rate_(0) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000088
Yves Gerey665174f2018-06-19 15:03:05 +020089void SenderWithFEC::Setup(AudioCodingModule* acm,
90 RTPStream* rtpStream,
91 std::string in_file_name,
92 int sample_rate,
93 int channels,
94 int expected_loss_rate) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000095 Sender::Setup(acm, rtpStream, in_file_name, sample_rate, channels);
96 EXPECT_TRUE(SetFEC(true));
97 EXPECT_TRUE(SetPacketLossRate(expected_loss_rate));
98}
99
100bool SenderWithFEC::SetFEC(bool enable_fec) {
Karl Wiberg658a5522018-08-15 15:20:49 +0200101 bool success = false;
102 _acm->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
103 if (*enc && (*enc)->SetFec(enable_fec)) {
104 success = true;
105 }
106 });
107 return success;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000108}
109
110bool SenderWithFEC::SetPacketLossRate(int expected_loss_rate) {
111 if (_acm->SetPacketLossRate(expected_loss_rate) == 0) {
112 expected_loss_rate_ = expected_loss_rate;
113 return true;
114 }
115 return false;
116}
117
Yves Gerey665174f2018-06-19 15:03:05 +0200118PacketLossTest::PacketLossTest(int channels,
119 int expected_loss_rate,
120 int actual_loss_rate,
121 int burst_length)
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000122 : channels_(channels),
Yves Gerey665174f2018-06-19 15:03:05 +0200123 in_file_name_(channels_ == 1 ? "audio_coding/testfile32kHz"
124 : "audio_coding/teststereo32kHz"),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000125 sample_rate_hz_(32000),
126 sender_(new SenderWithFEC),
127 receiver_(new ReceiverWithPacketLoss),
128 expected_loss_rate_(expected_loss_rate),
129 actual_loss_rate_(actual_loss_rate),
Yves Gerey665174f2018-06-19 15:03:05 +0200130 burst_length_(burst_length) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000131
132void PacketLossTest::Perform() {
133#ifndef WEBRTC_CODEC_OPUS
134 return;
135#else
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200136 AudioCodingModule::Config config;
137 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
138 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(config));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000139
140 int codec_id = acm->Codec("opus", 48000, channels_);
141
142 RTPFile rtpFile;
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000143 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
144 "packet_loss_test");
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000145
146 // Encode to file
147 rtpFile.Open(fileName.c_str(), "wb+");
148 rtpFile.WriteHeader();
149
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000150 sender_->codeId = codec_id;
151
152 sender_->Setup(acm.get(), &rtpFile, in_file_name_, sample_rate_hz_, channels_,
153 expected_loss_rate_);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800154 if (acm->SendCodec()) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000155 sender_->Run();
156 }
157 sender_->Teardown();
158 rtpFile.Close();
159
160 // Decode to file
161 rtpFile.Open(fileName.c_str(), "rb");
162 rtpFile.ReadHeader();
163
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000164 receiver_->codeId = codec_id;
165
166 receiver_->Setup(acm.get(), &rtpFile, "packetLoss_out", channels_,
167 actual_loss_rate_, burst_length_);
168 receiver_->Run();
169 receiver_->Teardown();
170 rtpFile.Close();
171#endif
172}
173
174} // namespace webrtc