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