blob: fd7622404f5e1d9cbe6045cae9f1cb3d7179eed9 [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"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "test/testsupport/file_utils.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,
Fredrik Solenberg657b2962018-12-05 10:30:25 +010033 int file_num,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000034 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.
Jonas Olsson366a50c2018-09-06 13:41:30 +020039 rtc::StringBuilder ss;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000040 ss << out_file_name << "_" << loss_rate_ << "_" << burst_length_ << "_";
Fredrik Solenberg657b2962018-12-05 10:30:25 +010041 Receiver::Setup(acm, rtpStream, ss.str(), channels, file_num);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000042}
43
44bool ReceiverWithPacketLoss::IncomingPacket() {
45 if (!_rtpStream->EndOfFile()) {
46 if (packet_counter_ == 0) {
47 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _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()) {
Niels Möllerafb5dbb2019-02-15 15:21:47 +010060 _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
61 _rtpInfo.header);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000062 }
63 packet_counter_++;
64 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _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
74bool 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
Yves Gerey665174f2018-06-19 15:03:05 +020089SenderWithFEC::SenderWithFEC() : expected_loss_rate_(0) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000090
Yves Gerey665174f2018-06-19 15:03:05 +020091void SenderWithFEC::Setup(AudioCodingModule* acm,
92 RTPStream* rtpStream,
93 std::string in_file_name,
Fredrik Solenberg657b2962018-12-05 10:30:25 +010094 int payload_type,
95 SdpAudioFormat format,
Yves Gerey665174f2018-06-19 15:03:05 +020096 int expected_loss_rate) {
Fredrik Solenberg657b2962018-12-05 10:30:25 +010097 Sender::Setup(acm, rtpStream, in_file_name, format.clockrate_hz, payload_type,
98 format);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000099 EXPECT_TRUE(SetFEC(true));
100 EXPECT_TRUE(SetPacketLossRate(expected_loss_rate));
101}
102
103bool SenderWithFEC::SetFEC(bool enable_fec) {
Karl Wiberg658a5522018-08-15 15:20:49 +0200104 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;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000111}
112
113bool 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
Yves Gerey665174f2018-06-19 15:03:05 +0200121PacketLossTest::PacketLossTest(int channels,
122 int expected_loss_rate,
123 int actual_loss_rate,
124 int burst_length)
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000125 : channels_(channels),
Yves Gerey665174f2018-06-19 15:03:05 +0200126 in_file_name_(channels_ == 1 ? "audio_coding/testfile32kHz"
127 : "audio_coding/teststereo32kHz"),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000128 sample_rate_hz_(32000),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000129 expected_loss_rate_(expected_loss_rate),
130 actual_loss_rate_(actual_loss_rate),
Yves Gerey665174f2018-06-19 15:03:05 +0200131 burst_length_(burst_length) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000132
133void PacketLossTest::Perform() {
134#ifndef WEBRTC_CODEC_OPUS
135 return;
136#else
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000137 RTPFile rtpFile;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100138 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
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000145 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
146 "packet_loss_test");
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000147 rtpFile.Open(fileName.c_str(), "wb+");
148 rtpFile.WriteHeader();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100149 SenderWithFEC sender;
150 sender.Setup(acm.get(), &rtpFile, in_file_name_, 120, send_format,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000151 expected_loss_rate_);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100152 sender.Run();
153 sender.Teardown();
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000154 rtpFile.Close();
155
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000156 rtpFile.Open(fileName.c_str(), "rb");
157 rtpFile.ReadHeader();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100158 ReceiverWithPacketLoss receiver;
159 receiver.Setup(acm.get(), &rtpFile, "packetLoss_out", channels_, 15,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000160 actual_loss_rate_, burst_length_);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100161 receiver.Run();
162 receiver.Teardown();
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000163 rtpFile.Close();
164#endif
165}
166
167} // namespace webrtc