blob: 891471dce572c2321eb4fda41184cf652beec364 [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
kjellander3e6db232015-11-26 04:44:54 -080011#include "webrtc/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
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000015#include "testing/gtest/include/gtest/gtest.h"
16#include "webrtc/common.h"
17#include "webrtc/test/testsupport/fileutils.h"
18
19namespace webrtc {
20
21ReceiverWithPacketLoss::ReceiverWithPacketLoss()
22 : loss_rate_(0),
23 burst_length_(1),
24 packet_counter_(0),
25 lost_packet_counter_(0),
26 burst_lost_counter_(burst_length_) {
27}
28
29void ReceiverWithPacketLoss::Setup(AudioCodingModule *acm,
30 RTPStream *rtpStream,
31 std::string out_file_name,
32 int channels,
33 int loss_rate,
34 int burst_length) {
35 loss_rate_ = loss_rate;
36 burst_length_ = burst_length;
37 burst_lost_counter_ = burst_length_; // To prevent first packet gets lost.
38 std::stringstream ss;
39 ss << out_file_name << "_" << loss_rate_ << "_" << burst_length_ << "_";
40 Receiver::Setup(acm, rtpStream, ss.str(), channels);
41}
42
43bool ReceiverWithPacketLoss::IncomingPacket() {
44 if (!_rtpStream->EndOfFile()) {
45 if (packet_counter_ == 0) {
46 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
47 _payloadSizeBytes, &_nextTime);
48 if (_realPayloadSizeBytes == 0) {
49 if (_rtpStream->EndOfFile()) {
50 packet_counter_ = 0;
51 return true;
52 } else {
53 return false;
54 }
55 }
56 }
57
58 if (!PacketLost()) {
59 _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes, _rtpInfo);
60 }
61 packet_counter_++;
62 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
63 _payloadSizeBytes, &_nextTime);
64 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
65 packet_counter_ = 0;
66 lost_packet_counter_ = 0;
67 }
68 }
69 return true;
70}
71
72bool ReceiverWithPacketLoss::PacketLost() {
73 if (burst_lost_counter_ < burst_length_) {
74 lost_packet_counter_++;
75 burst_lost_counter_++;
76 return true;
77 }
78
79 if (lost_packet_counter_ * 100 < loss_rate_ * packet_counter_) {
80 lost_packet_counter_++;
81 burst_lost_counter_ = 1;
82 return true;
83 }
84 return false;
85}
86
87SenderWithFEC::SenderWithFEC()
88 : expected_loss_rate_(0) {
89}
90
91void SenderWithFEC::Setup(AudioCodingModule *acm, RTPStream *rtpStream,
92 std::string in_file_name, int sample_rate,
93 int channels, int expected_loss_rate) {
94 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
114PacketLossTest::PacketLossTest(int channels, int expected_loss_rate,
115 int actual_loss_rate, int burst_length)
116 : channels_(channels),
117 in_file_name_(channels_ == 1 ? "audio_coding/testfile32kHz" :
118 "audio_coding/teststereo32kHz"),
119 sample_rate_hz_(32000),
120 sender_(new SenderWithFEC),
121 receiver_(new ReceiverWithPacketLoss),
122 expected_loss_rate_(expected_loss_rate),
123 actual_loss_rate_(actual_loss_rate),
124 burst_length_(burst_length) {
125}
126
127void PacketLossTest::Perform() {
128#ifndef WEBRTC_CODEC_OPUS
129 return;
130#else
kwiberg37478382016-02-14 20:40:57 -0800131 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(0));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000132
133 int codec_id = acm->Codec("opus", 48000, channels_);
134
135 RTPFile rtpFile;
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000136 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
137 "packet_loss_test");
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000138
139 // Encode to file
140 rtpFile.Open(fileName.c_str(), "wb+");
141 rtpFile.WriteHeader();
142
143 sender_->testMode = 0;
144 sender_->codeId = codec_id;
145
146 sender_->Setup(acm.get(), &rtpFile, in_file_name_, sample_rate_hz_, channels_,
147 expected_loss_rate_);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800148 if (acm->SendCodec()) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000149 sender_->Run();
150 }
151 sender_->Teardown();
152 rtpFile.Close();
153
154 // Decode to file
155 rtpFile.Open(fileName.c_str(), "rb");
156 rtpFile.ReadHeader();
157
158 receiver_->testMode = 0;
159 receiver_->codeId = codec_id;
160
161 receiver_->Setup(acm.get(), &rtpFile, "packetLoss_out", channels_,
162 actual_loss_rate_, burst_length_);
163 receiver_->Run();
164 receiver_->Teardown();
165 rtpFile.Close();
166#endif
167}
168
169} // namespace webrtc