audio_decoder_opus_unittest.cc (3482B)
1 /* 2 * Copyright (c) 2024 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 11 #include "api/audio_codecs/opus/audio_decoder_opus.h" 12 13 #include <cstddef> 14 #include <memory> 15 #include <optional> 16 17 #include "api/audio_codecs/audio_format.h" 18 #include "api/environment/environment.h" 19 #include "api/environment/environment_factory.h" 20 #include "test/create_test_field_trials.h" 21 #include "test/gmock.h" 22 #include "test/gtest.h" 23 24 namespace webrtc { 25 namespace { 26 27 using ::testing::Field; 28 using ::testing::Optional; 29 using Config = AudioDecoderOpus::Config; 30 31 enum class StereoParam { kUnset, kMono, kStereo }; 32 33 SdpAudioFormat GetSdpAudioFormat(StereoParam param) { 34 SdpAudioFormat format("opus", 48000, 2); 35 switch (param) { 36 case StereoParam::kUnset: 37 // Do nothing. 38 break; 39 case StereoParam::kMono: 40 format.parameters.emplace("stereo", "0"); 41 break; 42 case StereoParam::kStereo: 43 format.parameters.emplace("stereo", "1"); 44 break; 45 } 46 return format; 47 } 48 49 constexpr int kDefaultNumChannels = 1; 50 constexpr int kAlternativeNumChannels = 2; 51 52 TEST(AudioDecoderOpusTest, SdpToConfigDoesNotSetNumChannels) { 53 const std::optional<Config> config = 54 AudioDecoderOpus::SdpToConfig(GetSdpAudioFormat(StereoParam::kUnset)); 55 56 EXPECT_THAT(config, Optional(Field(&Config::num_channels, std::nullopt))); 57 } 58 59 TEST(AudioDecoderOpusTest, SdpToConfigForcesMono) { 60 const std::optional<Config> config = 61 AudioDecoderOpus::SdpToConfig(GetSdpAudioFormat(StereoParam::kMono)); 62 63 EXPECT_THAT(config, Optional(Field(&Config::num_channels, 1))); 64 } 65 66 TEST(AudioDecoderOpusTest, SdpToConfigForcesStereo) { 67 const std::optional<Config> config = 68 AudioDecoderOpus::SdpToConfig(GetSdpAudioFormat(StereoParam::kStereo)); 69 70 EXPECT_THAT(config, Optional(Field(&Config::num_channels, 2))); 71 } 72 73 TEST(AudioDecoderOpusTest, MakeAudioDecoderForcesDefaultNumChannels) { 74 const Environment env = CreateEnvironment(); 75 auto decoder = AudioDecoderOpus::MakeAudioDecoder( 76 env, /*config=*/{.num_channels = std::nullopt}); 77 78 EXPECT_EQ(decoder->Channels(), static_cast<size_t>(kDefaultNumChannels)); 79 } 80 81 TEST(AudioDecoderOpusTest, MakeAudioDecoderCannotForceDefaultNumChannels) { 82 const Environment env = CreateEnvironment(); 83 auto decoder = AudioDecoderOpus::MakeAudioDecoder( 84 env, /*config=*/{.num_channels = kAlternativeNumChannels}); 85 86 EXPECT_EQ(decoder->Channels(), static_cast<size_t>(kAlternativeNumChannels)); 87 } 88 89 TEST(AudioDecoderOpusTest, MakeAudioDecoderForcesStereo) { 90 const Environment env = CreateEnvironment(CreateTestFieldTrialsPtr( 91 "WebRTC-Audio-OpusDecodeStereoByDefault/Enabled/")); 92 auto decoder = AudioDecoderOpus::MakeAudioDecoder( 93 env, 94 /*config=*/{.num_channels = std::nullopt}); 95 96 EXPECT_EQ(decoder->Channels(), 2u); 97 } 98 99 TEST(AudioDecoderOpusTest, MakeAudioDecoderCannotForceStereo) { 100 const Environment env = CreateEnvironment(CreateTestFieldTrialsPtr( 101 "WebRTC-Audio-OpusDecodeStereoByDefault/Enabled/")); 102 auto decoder = 103 AudioDecoderOpus::MakeAudioDecoder(env, /*config=*/{.num_channels = 1}); 104 105 EXPECT_EQ(decoder->Channels(), 1u); 106 } 107 108 } // namespace 109 } // namespace webrtc