blob: c5cb3963b3a9eb3f2100488c7ba9c6ca31a558ed [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "test/gtest.h"
17#include "test/testsupport/fileutils.h"
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000018
19namespace webrtc {
20
21ReceiverWithPacketLoss::ReceiverWithPacketLoss()
22 : loss_rate_(0),
23 burst_length_(1),
24 packet_counter_(0),
25 lost_packet_counter_(0),
Yves Gerey665174f2018-06-19 15:03:05 +020026 burst_lost_counter_(burst_length_) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000027
Yves Gerey665174f2018-06-19 15:03:05 +020028void ReceiverWithPacketLoss::Setup(AudioCodingModule* acm,
29 RTPStream* rtpStream,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000030 std::string out_file_name,
31 int channels,
32 int loss_rate,
33 int burst_length) {
34 loss_rate_ = loss_rate;
35 burst_length_ = burst_length;
36 burst_lost_counter_ = burst_length_; // To prevent first packet gets lost.
37 std::stringstream ss;
38 ss << out_file_name << "_" << loss_rate_ << "_" << burst_length_ << "_";
39 Receiver::Setup(acm, rtpStream, ss.str(), channels);
40}
41
42bool ReceiverWithPacketLoss::IncomingPacket() {
43 if (!_rtpStream->EndOfFile()) {
44 if (packet_counter_ == 0) {
45 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
46 _payloadSizeBytes, &_nextTime);
47 if (_realPayloadSizeBytes == 0) {
48 if (_rtpStream->EndOfFile()) {
49 packet_counter_ = 0;
50 return true;
51 } else {
52 return false;
53 }
54 }
55 }
56
57 if (!PacketLost()) {
58 _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes, _rtpInfo);
59 }
60 packet_counter_++;
61 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
62 _payloadSizeBytes, &_nextTime);
63 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
64 packet_counter_ = 0;
65 lost_packet_counter_ = 0;
66 }
67 }
68 return true;
69}
70
71bool ReceiverWithPacketLoss::PacketLost() {
72 if (burst_lost_counter_ < burst_length_) {
73 lost_packet_counter_++;
74 burst_lost_counter_++;
75 return true;
76 }
77
78 if (lost_packet_counter_ * 100 < loss_rate_ * packet_counter_) {
79 lost_packet_counter_++;
80 burst_lost_counter_ = 1;
81 return true;
82 }
83 return false;
84}
85
Yves Gerey665174f2018-06-19 15:03:05 +020086SenderWithFEC::SenderWithFEC() : expected_loss_rate_(0) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000087
Yves Gerey665174f2018-06-19 15:03:05 +020088void SenderWithFEC::Setup(AudioCodingModule* acm,
89 RTPStream* rtpStream,
90 std::string in_file_name,
91 int sample_rate,
92 int channels,
93 int expected_loss_rate) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000094 Sender::Setup(acm, rtpStream, in_file_name, sample_rate, channels);
95 EXPECT_TRUE(SetFEC(true));
96 EXPECT_TRUE(SetPacketLossRate(expected_loss_rate));
97}
98
99bool SenderWithFEC::SetFEC(bool enable_fec) {
100 if (_acm->SetCodecFEC(enable_fec) == 0) {
101 return true;
102 }
103 return false;
104}
105
106bool SenderWithFEC::SetPacketLossRate(int expected_loss_rate) {
107 if (_acm->SetPacketLossRate(expected_loss_rate) == 0) {
108 expected_loss_rate_ = expected_loss_rate;
109 return true;
110 }
111 return false;
112}
113
Yves Gerey665174f2018-06-19 15:03:05 +0200114PacketLossTest::PacketLossTest(int channels,
115 int expected_loss_rate,
116 int actual_loss_rate,
117 int burst_length)
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000118 : channels_(channels),
Yves Gerey665174f2018-06-19 15:03:05 +0200119 in_file_name_(channels_ == 1 ? "audio_coding/testfile32kHz"
120 : "audio_coding/teststereo32kHz"),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000121 sample_rate_hz_(32000),
122 sender_(new SenderWithFEC),
123 receiver_(new ReceiverWithPacketLoss),
124 expected_loss_rate_(expected_loss_rate),
125 actual_loss_rate_(actual_loss_rate),
Yves Gerey665174f2018-06-19 15:03:05 +0200126 burst_length_(burst_length) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000127
128void PacketLossTest::Perform() {
129#ifndef WEBRTC_CODEC_OPUS
130 return;
131#else
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200132 AudioCodingModule::Config config;
133 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
134 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(config));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000135
136 int codec_id = acm->Codec("opus", 48000, channels_);
137
138 RTPFile rtpFile;
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000139 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
140 "packet_loss_test");
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000141
142 // Encode to file
143 rtpFile.Open(fileName.c_str(), "wb+");
144 rtpFile.WriteHeader();
145
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000146 sender_->codeId = codec_id;
147
148 sender_->Setup(acm.get(), &rtpFile, in_file_name_, sample_rate_hz_, channels_,
149 expected_loss_rate_);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800150 if (acm->SendCodec()) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000151 sender_->Run();
152 }
153 sender_->Teardown();
154 rtpFile.Close();
155
156 // Decode to file
157 rtpFile.Open(fileName.c_str(), "rb");
158 rtpFile.ReadHeader();
159
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000160 receiver_->codeId = codec_id;
161
162 receiver_->Setup(acm.get(), &rtpFile, "packetLoss_out", channels_,
163 actual_loss_rate_, burst_length_);
164 receiver_->Run();
165 receiver_->Teardown();
166 rtpFile.Close();
167#endif
168}
169
170} // namespace webrtc