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