From cf7efeba374b2197ed6e869b8577fe3800a74e5e Mon Sep 17 00:00:00 2001 From: "henrik.lundin@webrtc.org" Date: Wed, 4 Feb 2015 15:34:05 +0000 Subject: [PATCH] Add new AudioEncoderOpusTest This test will replace AcmOpusTest when ACMOpus is removed. The old AcmOpusTest also contains tests for setting and updating the "application" setting in Opus. However, in the new AudioEncoderOpus class, the application is trivially set in the Config struct at construction, wherefore a test is no longer needed. BUG=3926 R=minyue@webrtc.org Review URL: https://webrtc-codereview.appspot.com/37929004 Cr-Commit-Position: refs/heads/master@{#8244} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8244 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../opus/audio_encoder_opus_unittest.cc | 80 +++++++++++++++++++ .../opus/interface/audio_encoder_opus.h | 1 + webrtc/modules/modules.gyp | 1 + 3 files changed, 82 insertions(+) create mode 100644 webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc diff --git a/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc b/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc new file mode 100644 index 0000000000..9b2f07fe3c --- /dev/null +++ b/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "testing/gtest/include/gtest/gtest.h" +#include "webrtc/modules/audio_coding/codecs/opus/interface/audio_encoder_opus.h" +#include "webrtc/system_wrappers/interface/scoped_ptr.h" + +namespace webrtc { + +class AudioEncoderOpusTest : public ::testing::Test { + protected: + // The constructor simply creates an Opus encoder with default configuration. + AudioEncoderOpusTest() + : opus_(new AudioEncoderOpus(AudioEncoderOpus::Config())) {} + + // Repeatedly sets packet loss rates in the range [from, to], increasing by + // 0.01 in each step. The function verifies that the actual loss rate is + // |expected_return|. + void TestSetPacketLossRate(double from, double to, double expected_return) { + ASSERT_TRUE(opus_); + for (double loss = from; loss <= to; + (to >= from) ? loss += 0.01 : loss -= 0.01) { + opus_->SetProjectedPacketLossRate(loss); + EXPECT_DOUBLE_EQ(expected_return, opus_->packet_loss_rate()); + } + } + + scoped_ptr opus_; +}; + +namespace { +// These constants correspond to those used in +// AudioEncoderOpus::SetProjectedPacketLossRate. +const double kPacketLossRate20 = 0.20; +const double kPacketLossRate10 = 0.10; +const double kPacketLossRate5 = 0.05; +const double kPacketLossRate1 = 0.01; +const double kLossRate20Margin = 0.02; +const double kLossRate10Margin = 0.01; +const double kLossRate5Margin = 0.01; +} // namespace + +TEST_F(AudioEncoderOpusTest, PacketLossRateOptimized) { + // Note that the order of the following calls is critical. + TestSetPacketLossRate(0.0, 0.0, 0.0); + TestSetPacketLossRate(kPacketLossRate1, + kPacketLossRate5 + kLossRate5Margin - 0.01, + kPacketLossRate1); + TestSetPacketLossRate(kPacketLossRate5 + kLossRate5Margin, + kPacketLossRate10 + kLossRate10Margin - 0.01, + kPacketLossRate5); + TestSetPacketLossRate(kPacketLossRate10 + kLossRate10Margin, + kPacketLossRate20 + kLossRate20Margin - 0.01, + kPacketLossRate10); + TestSetPacketLossRate(kPacketLossRate20 + kLossRate20Margin, + 1.0, + kPacketLossRate20); + TestSetPacketLossRate(kPacketLossRate20 + kLossRate20Margin, + kPacketLossRate20 - kLossRate20Margin, + kPacketLossRate20); + TestSetPacketLossRate(kPacketLossRate20 - kLossRate20Margin - 0.01, + kPacketLossRate10 - kLossRate10Margin, + kPacketLossRate10); + TestSetPacketLossRate(kPacketLossRate10 - kLossRate10Margin - 0.01, + kPacketLossRate5 - kLossRate5Margin, + kPacketLossRate5); + TestSetPacketLossRate(kPacketLossRate5 - kLossRate5Margin - 0.01, + kPacketLossRate1, + kPacketLossRate1); + TestSetPacketLossRate(0.0, 0.0, 0.0); +} + +} // namespace webrtc diff --git a/webrtc/modules/audio_coding/codecs/opus/interface/audio_encoder_opus.h b/webrtc/modules/audio_coding/codecs/opus/interface/audio_encoder_opus.h index 477ee8fff1..c45388b8f7 100644 --- a/webrtc/modules/audio_coding/codecs/opus/interface/audio_encoder_opus.h +++ b/webrtc/modules/audio_coding/codecs/opus/interface/audio_encoder_opus.h @@ -49,6 +49,7 @@ class AudioEncoderOpus final : public AudioEncoder { virtual int Max10MsFramesInAPacket() const OVERRIDE; void SetTargetBitrate(int bits_per_second) override; void SetProjectedPacketLossRate(double fraction) override; + double packet_loss_rate() const { return packet_loss_rate_; } protected: virtual bool EncodeInternal(uint32_t rtp_timestamp, diff --git a/webrtc/modules/modules.gyp b/webrtc/modules/modules.gyp index 23ee29f431..376e6c5b45 100644 --- a/webrtc/modules/modules.gyp +++ b/webrtc/modules/modules.gyp @@ -105,6 +105,7 @@ 'audio_coding/codecs/isac/fix/source/transform_unittest.cc', 'audio_coding/codecs/isac/main/source/isac_unittest.cc', 'audio_coding/codecs/isac/main/source/audio_encoder_isac_red_unittest.cc', + 'audio_coding/codecs/opus/audio_encoder_opus_unittest.cc', 'audio_coding/codecs/opus/opus_unittest.cc', 'audio_coding/codecs/red/audio_encoder_copy_red_unittest.cc', 'audio_coding/neteq/audio_classifier_unittest.cc',