blob: 727f692b76bf31e0f885e2cd76f7094677186237 [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) {
Niels Möllerbf474952019-02-18 12:00:06 +010047 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000048 _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öllerbf474952019-02-18 12:00:06 +010060 _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes, _rtpHeader);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000061 }
62 packet_counter_++;
Niels Möllerbf474952019-02-18 12:00:06 +010063 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000064 _payloadSizeBytes, &_nextTime);
65 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
66 packet_counter_ = 0;
67 lost_packet_counter_ = 0;
68 }
69 }
70 return true;
71}
72
73bool 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
Yves Gerey665174f2018-06-19 15:03:05 +020088SenderWithFEC::SenderWithFEC() : expected_loss_rate_(0) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000089
Yves Gerey665174f2018-06-19 15:03:05 +020090void SenderWithFEC::Setup(AudioCodingModule* acm,
91 RTPStream* rtpStream,
92 std::string in_file_name,
Fredrik Solenberg657b2962018-12-05 10:30:25 +010093 int payload_type,
94 SdpAudioFormat format,
Yves Gerey665174f2018-06-19 15:03:05 +020095 int expected_loss_rate) {
Fredrik Solenberg657b2962018-12-05 10:30:25 +010096 Sender::Setup(acm, rtpStream, in_file_name, format.clockrate_hz, payload_type,
97 format);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000098 EXPECT_TRUE(SetFEC(true));
99 EXPECT_TRUE(SetPacketLossRate(expected_loss_rate));
100}
101
102bool SenderWithFEC::SetFEC(bool enable_fec) {
Karl Wiberg658a5522018-08-15 15:20:49 +0200103 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;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000110}
111
112bool 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
Yves Gerey665174f2018-06-19 15:03:05 +0200120PacketLossTest::PacketLossTest(int channels,
121 int expected_loss_rate,
122 int actual_loss_rate,
123 int burst_length)
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000124 : channels_(channels),
Yves Gerey665174f2018-06-19 15:03:05 +0200125 in_file_name_(channels_ == 1 ? "audio_coding/testfile32kHz"
126 : "audio_coding/teststereo32kHz"),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000127 sample_rate_hz_(32000),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000128 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
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000136 RTPFile rtpFile;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100137 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
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000144 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
145 "packet_loss_test");
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000146 rtpFile.Open(fileName.c_str(), "wb+");
147 rtpFile.WriteHeader();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100148 SenderWithFEC sender;
149 sender.Setup(acm.get(), &rtpFile, in_file_name_, 120, send_format,
Jonas Olssona4d87372019-07-05 19:08:33 +0200150 expected_loss_rate_);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100151 sender.Run();
152 sender.Teardown();
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000153 rtpFile.Close();
154
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000155 rtpFile.Open(fileName.c_str(), "rb");
156 rtpFile.ReadHeader();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100157 ReceiverWithPacketLoss receiver;
158 receiver.Setup(acm.get(), &rtpFile, "packetLoss_out", channels_, 15,
Jonas Olssona4d87372019-07-05 19:08:33 +0200159 actual_loss_rate_, burst_length_);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100160 receiver.Run();
161 receiver.Teardown();
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000162 rtpFile.Close();
163#endif
164}
165
166} // namespace webrtc