You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include "modules/audio_coding/test/PacketLossTest.h"
  11. #include <memory>
  12. #include "api/audio_codecs/builtin_audio_decoder_factory.h"
  13. #include "rtc_base/strings/string_builder.h"
  14. #include "test/gtest.h"
  15. #include "test/testsupport/file_utils.h"
  16. namespace webrtc {
  17. ReceiverWithPacketLoss::ReceiverWithPacketLoss()
  18. : loss_rate_(0),
  19. burst_length_(1),
  20. packet_counter_(0),
  21. lost_packet_counter_(0),
  22. burst_lost_counter_(burst_length_) {}
  23. void ReceiverWithPacketLoss::Setup(AudioCodingModule* acm,
  24. RTPStream* rtpStream,
  25. std::string out_file_name,
  26. int channels,
  27. int file_num,
  28. int loss_rate,
  29. int burst_length) {
  30. loss_rate_ = loss_rate;
  31. burst_length_ = burst_length;
  32. burst_lost_counter_ = burst_length_; // To prevent first packet gets lost.
  33. rtc::StringBuilder ss;
  34. ss << out_file_name << "_" << loss_rate_ << "_" << burst_length_ << "_";
  35. Receiver::Setup(acm, rtpStream, ss.str(), channels, file_num);
  36. }
  37. bool ReceiverWithPacketLoss::IncomingPacket() {
  38. if (!_rtpStream->EndOfFile()) {
  39. if (packet_counter_ == 0) {
  40. _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
  41. _payloadSizeBytes, &_nextTime);
  42. if (_realPayloadSizeBytes == 0) {
  43. if (_rtpStream->EndOfFile()) {
  44. packet_counter_ = 0;
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. }
  51. if (!PacketLost()) {
  52. _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes, _rtpHeader);
  53. }
  54. packet_counter_++;
  55. _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
  56. _payloadSizeBytes, &_nextTime);
  57. if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
  58. packet_counter_ = 0;
  59. lost_packet_counter_ = 0;
  60. }
  61. }
  62. return true;
  63. }
  64. bool ReceiverWithPacketLoss::PacketLost() {
  65. if (burst_lost_counter_ < burst_length_) {
  66. lost_packet_counter_++;
  67. burst_lost_counter_++;
  68. return true;
  69. }
  70. if (lost_packet_counter_ * 100 < loss_rate_ * packet_counter_) {
  71. lost_packet_counter_++;
  72. burst_lost_counter_ = 1;
  73. return true;
  74. }
  75. return false;
  76. }
  77. SenderWithFEC::SenderWithFEC() : expected_loss_rate_(0) {}
  78. void SenderWithFEC::Setup(AudioCodingModule* acm,
  79. RTPStream* rtpStream,
  80. std::string in_file_name,
  81. int payload_type,
  82. SdpAudioFormat format,
  83. int expected_loss_rate) {
  84. Sender::Setup(acm, rtpStream, in_file_name, format.clockrate_hz, payload_type,
  85. format);
  86. EXPECT_TRUE(SetFEC(true));
  87. EXPECT_TRUE(SetPacketLossRate(expected_loss_rate));
  88. }
  89. bool SenderWithFEC::SetFEC(bool enable_fec) {
  90. bool success = false;
  91. _acm->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
  92. if (*enc && (*enc)->SetFec(enable_fec)) {
  93. success = true;
  94. }
  95. });
  96. return success;
  97. }
  98. bool SenderWithFEC::SetPacketLossRate(int expected_loss_rate) {
  99. if (_acm->SetPacketLossRate(expected_loss_rate) == 0) {
  100. expected_loss_rate_ = expected_loss_rate;
  101. return true;
  102. }
  103. return false;
  104. }
  105. PacketLossTest::PacketLossTest(int channels,
  106. int expected_loss_rate,
  107. int actual_loss_rate,
  108. int burst_length)
  109. : channels_(channels),
  110. in_file_name_(channels_ == 1 ? "audio_coding/testfile32kHz"
  111. : "audio_coding/teststereo32kHz"),
  112. sample_rate_hz_(32000),
  113. expected_loss_rate_(expected_loss_rate),
  114. actual_loss_rate_(actual_loss_rate),
  115. burst_length_(burst_length) {}
  116. void PacketLossTest::Perform() {
  117. #ifndef WEBRTC_CODEC_OPUS
  118. return;
  119. #else
  120. RTPFile rtpFile;
  121. std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
  122. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
  123. SdpAudioFormat send_format = SdpAudioFormat("opus", 48000, 2);
  124. if (channels_ == 2) {
  125. send_format.parameters = {{"stereo", "1"}};
  126. }
  127. std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
  128. "packet_loss_test");
  129. rtpFile.Open(fileName.c_str(), "wb+");
  130. rtpFile.WriteHeader();
  131. SenderWithFEC sender;
  132. sender.Setup(acm.get(), &rtpFile, in_file_name_, 120, send_format,
  133. expected_loss_rate_);
  134. sender.Run();
  135. sender.Teardown();
  136. rtpFile.Close();
  137. rtpFile.Open(fileName.c_str(), "rb");
  138. rtpFile.ReadHeader();
  139. ReceiverWithPacketLoss receiver;
  140. receiver.Setup(acm.get(), &rtpFile, "packetLoss_out", channels_, 15,
  141. actual_loss_rate_, burst_length_);
  142. receiver.Run();
  143. receiver.Teardown();
  144. rtpFile.Close();
  145. #endif
  146. }
  147. } // namespace webrtc