blob: c80615a36d44b5d0e3d6e3972d7e97533964ef19 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "test/gtest.h"
16#include "test/testsupport/fileutils.h"
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000017
18namespace webrtc {
19
20ReceiverWithPacketLoss::ReceiverWithPacketLoss()
21 : loss_rate_(0),
22 burst_length_(1),
23 packet_counter_(0),
24 lost_packet_counter_(0),
25 burst_lost_counter_(burst_length_) {
26}
27
28void ReceiverWithPacketLoss::Setup(AudioCodingModule *acm,
29 RTPStream *rtpStream,
30 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
86SenderWithFEC::SenderWithFEC()
87 : expected_loss_rate_(0) {
88}
89
90void SenderWithFEC::Setup(AudioCodingModule *acm, RTPStream *rtpStream,
91 std::string in_file_name, int sample_rate,
92 int channels, int expected_loss_rate) {
93 Sender::Setup(acm, rtpStream, in_file_name, sample_rate, channels);
94 EXPECT_TRUE(SetFEC(true));
95 EXPECT_TRUE(SetPacketLossRate(expected_loss_rate));
96}
97
98bool SenderWithFEC::SetFEC(bool enable_fec) {
99 if (_acm->SetCodecFEC(enable_fec) == 0) {
100 return true;
101 }
102 return false;
103}
104
105bool SenderWithFEC::SetPacketLossRate(int expected_loss_rate) {
106 if (_acm->SetPacketLossRate(expected_loss_rate) == 0) {
107 expected_loss_rate_ = expected_loss_rate;
108 return true;
109 }
110 return false;
111}
112
113PacketLossTest::PacketLossTest(int channels, int expected_loss_rate,
114 int actual_loss_rate, int burst_length)
115 : channels_(channels),
116 in_file_name_(channels_ == 1 ? "audio_coding/testfile32kHz" :
117 "audio_coding/teststereo32kHz"),
118 sample_rate_hz_(32000),
119 sender_(new SenderWithFEC),
120 receiver_(new ReceiverWithPacketLoss),
121 expected_loss_rate_(expected_loss_rate),
122 actual_loss_rate_(actual_loss_rate),
123 burst_length_(burst_length) {
124}
125
126void PacketLossTest::Perform() {
127#ifndef WEBRTC_CODEC_OPUS
128 return;
129#else
kwiberg37478382016-02-14 20:40:57 -0800130 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(0));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000131
132 int codec_id = acm->Codec("opus", 48000, channels_);
133
134 RTPFile rtpFile;
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000135 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
136 "packet_loss_test");
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000137
138 // Encode to file
139 rtpFile.Open(fileName.c_str(), "wb+");
140 rtpFile.WriteHeader();
141
142 sender_->testMode = 0;
143 sender_->codeId = codec_id;
144
145 sender_->Setup(acm.get(), &rtpFile, in_file_name_, sample_rate_hz_, channels_,
146 expected_loss_rate_);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800147 if (acm->SendCodec()) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000148 sender_->Run();
149 }
150 sender_->Teardown();
151 rtpFile.Close();
152
153 // Decode to file
154 rtpFile.Open(fileName.c_str(), "rb");
155 rtpFile.ReadHeader();
156
157 receiver_->testMode = 0;
158 receiver_->codeId = codec_id;
159
160 receiver_->Setup(acm.get(), &rtpFile, "packetLoss_out", channels_,
161 actual_loss_rate_, burst_length_);
162 receiver_->Run();
163 receiver_->Teardown();
164 rtpFile.Close();
165#endif
166}
167
168} // namespace webrtc