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
11 #include "modules/audio_coding/test/PacketLossTest.h"
12
13 #include <memory>
14
15 #include "absl/strings/string_view.h"
16 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
17 #include "rtc_base/strings/string_builder.h"
18 #include "test/gtest.h"
19 #include "test/testsupport/file_utils.h"
20
21 namespace webrtc {
22
ReceiverWithPacketLoss()23 ReceiverWithPacketLoss::ReceiverWithPacketLoss()
24 : loss_rate_(0),
25 burst_length_(1),
26 packet_counter_(0),
27 lost_packet_counter_(0),
28 burst_lost_counter_(burst_length_) {}
29
Setup(AudioCodingModule * acm,RTPStream * rtpStream,absl::string_view out_file_name,int channels,int file_num,int loss_rate,int burst_length)30 void ReceiverWithPacketLoss::Setup(AudioCodingModule* acm,
31 RTPStream* rtpStream,
32 absl::string_view out_file_name,
33 int channels,
34 int file_num,
35 int loss_rate,
36 int burst_length) {
37 loss_rate_ = loss_rate;
38 burst_length_ = burst_length;
39 burst_lost_counter_ = burst_length_; // To prevent first packet gets lost.
40 rtc::StringBuilder ss;
41 ss << out_file_name << "_" << loss_rate_ << "_" << burst_length_ << "_";
42 Receiver::Setup(acm, rtpStream, ss.str(), channels, file_num);
43 }
44
IncomingPacket()45 bool ReceiverWithPacketLoss::IncomingPacket() {
46 if (!_rtpStream->EndOfFile()) {
47 if (packet_counter_ == 0) {
48 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
49 _payloadSizeBytes, &_nextTime);
50 if (_realPayloadSizeBytes == 0) {
51 if (_rtpStream->EndOfFile()) {
52 packet_counter_ = 0;
53 return true;
54 } else {
55 return false;
56 }
57 }
58 }
59
60 if (!PacketLost()) {
61 _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes, _rtpHeader);
62 }
63 packet_counter_++;
64 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
65 _payloadSizeBytes, &_nextTime);
66 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
67 packet_counter_ = 0;
68 lost_packet_counter_ = 0;
69 }
70 }
71 return true;
72 }
73
PacketLost()74 bool ReceiverWithPacketLoss::PacketLost() {
75 if (burst_lost_counter_ < burst_length_) {
76 lost_packet_counter_++;
77 burst_lost_counter_++;
78 return true;
79 }
80
81 if (lost_packet_counter_ * 100 < loss_rate_ * packet_counter_) {
82 lost_packet_counter_++;
83 burst_lost_counter_ = 1;
84 return true;
85 }
86 return false;
87 }
88
SenderWithFEC()89 SenderWithFEC::SenderWithFEC() : expected_loss_rate_(0) {}
90
Setup(AudioCodingModule * acm,RTPStream * rtpStream,absl::string_view in_file_name,int payload_type,SdpAudioFormat format,int expected_loss_rate)91 void SenderWithFEC::Setup(AudioCodingModule* acm,
92 RTPStream* rtpStream,
93 absl::string_view in_file_name,
94 int payload_type,
95 SdpAudioFormat format,
96 int expected_loss_rate) {
97 Sender::Setup(acm, rtpStream, in_file_name, format.clockrate_hz, payload_type,
98 format);
99 EXPECT_TRUE(SetFEC(true));
100 EXPECT_TRUE(SetPacketLossRate(expected_loss_rate));
101 }
102
SetFEC(bool enable_fec)103 bool SenderWithFEC::SetFEC(bool enable_fec) {
104 bool success = false;
105 _acm->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
106 if (*enc && (*enc)->SetFec(enable_fec)) {
107 success = true;
108 }
109 });
110 return success;
111 }
112
SetPacketLossRate(int expected_loss_rate)113 bool SenderWithFEC::SetPacketLossRate(int expected_loss_rate) {
114 if (_acm->SetPacketLossRate(expected_loss_rate) == 0) {
115 expected_loss_rate_ = expected_loss_rate;
116 return true;
117 }
118 return false;
119 }
120
PacketLossTest(int channels,int expected_loss_rate,int actual_loss_rate,int burst_length)121 PacketLossTest::PacketLossTest(int channels,
122 int expected_loss_rate,
123 int actual_loss_rate,
124 int burst_length)
125 : channels_(channels),
126 in_file_name_(channels_ == 1 ? "audio_coding/testfile32kHz"
127 : "audio_coding/teststereo32kHz"),
128 sample_rate_hz_(32000),
129 expected_loss_rate_(expected_loss_rate),
130 actual_loss_rate_(actual_loss_rate),
131 burst_length_(burst_length) {}
132
Perform()133 void PacketLossTest::Perform() {
134 #ifndef WEBRTC_CODEC_OPUS
135 return;
136 #else
137 RTPFile rtpFile;
138 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
139 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
140 SdpAudioFormat send_format = SdpAudioFormat("opus", 48000, 2);
141 if (channels_ == 2) {
142 send_format.parameters = {{"stereo", "1"}};
143 }
144
145 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
146 "packet_loss_test");
147 rtpFile.Open(fileName.c_str(), "wb+");
148 rtpFile.WriteHeader();
149 SenderWithFEC sender;
150 sender.Setup(acm.get(), &rtpFile, in_file_name_, 120, send_format,
151 expected_loss_rate_);
152 sender.Run();
153 sender.Teardown();
154 rtpFile.Close();
155
156 rtpFile.Open(fileName.c_str(), "rb");
157 rtpFile.ReadHeader();
158 ReceiverWithPacketLoss receiver;
159 receiver.Setup(acm.get(), &rtpFile, "packetLoss_out", channels_, 15,
160 actual_loss_rate_, burst_length_);
161 receiver.Run();
162 receiver.Teardown();
163 rtpFile.Close();
164 #endif
165 }
166
167 } // namespace webrtc
168