File: audio_decoder_opus_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (112 lines) | stat: -rw-r--r-- 3,588 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 *  Copyright (c) 2024 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 "api/audio_codecs/opus/audio_decoder_opus.h"

#include <cstddef>
#include <memory>
#include <optional>

#include "api/audio_codecs/audio_format.h"
#include "api/environment/environment.h"
#include "api/environment/environment_factory.h"
#include "api/field_trials.h"
#include "test/create_test_field_trials.h"
#include "test/gmock.h"
#include "test/gtest.h"

namespace webrtc {
namespace {

using ::testing::Field;
using ::testing::Optional;
using Config = AudioDecoderOpus::Config;

enum class StereoParam { kUnset, kMono, kStereo };

SdpAudioFormat GetSdpAudioFormat(StereoParam param) {
  SdpAudioFormat format("opus", 48000, 2);
  switch (param) {
    case StereoParam::kUnset:
      // Do nothing.
      break;
    case StereoParam::kMono:
      format.parameters.emplace("stereo", "0");
      break;
    case StereoParam::kStereo:
      format.parameters.emplace("stereo", "1");
      break;
  }
  return format;
}

constexpr int kDefaultNumChannels = 1;
constexpr int kAlternativeNumChannels = 2;

TEST(AudioDecoderOpusTest, SdpToConfigDoesNotSetNumChannels) {
  const std::optional<Config> config =
      AudioDecoderOpus::SdpToConfig(GetSdpAudioFormat(StereoParam::kUnset));

  EXPECT_THAT(config, Optional(Field(&Config::num_channels, std::nullopt)));
}

TEST(AudioDecoderOpusTest, SdpToConfigForcesMono) {
  const std::optional<Config> config =
      AudioDecoderOpus::SdpToConfig(GetSdpAudioFormat(StereoParam::kMono));

  EXPECT_THAT(config, Optional(Field(&Config::num_channels, 1)));
}

TEST(AudioDecoderOpusTest, SdpToConfigForcesStereo) {
  const std::optional<Config> config =
      AudioDecoderOpus::SdpToConfig(GetSdpAudioFormat(StereoParam::kStereo));

  EXPECT_THAT(config, Optional(Field(&Config::num_channels, 2)));
}

TEST(AudioDecoderOpusTest, MakeAudioDecoderForcesDefaultNumChannels) {
  const Environment env = CreateEnvironment();
  auto decoder = AudioDecoderOpus::MakeAudioDecoder(
      env, /*config=*/{.num_channels = std::nullopt});

  EXPECT_EQ(decoder->Channels(), static_cast<size_t>(kDefaultNumChannels));
}

TEST(AudioDecoderOpusTest, MakeAudioDecoderCannotForceDefaultNumChannels) {
  const Environment env = CreateEnvironment();
  auto decoder = AudioDecoderOpus::MakeAudioDecoder(
      env, /*config=*/{.num_channels = kAlternativeNumChannels});

  EXPECT_EQ(decoder->Channels(), static_cast<size_t>(kAlternativeNumChannels));
}

TEST(AudioDecoderOpusTest, MakeAudioDecoderForcesStereo) {
  const Environment env =
      CreateEnvironment(std::make_unique<FieldTrials>(CreateTestFieldTrials(
          "WebRTC-Audio-OpusDecodeStereoByDefault/Enabled/")));
  auto decoder = AudioDecoderOpus::MakeAudioDecoder(
      env,
      /*config=*/{.num_channels = std::nullopt});

  EXPECT_EQ(decoder->Channels(), 2u);
}

TEST(AudioDecoderOpusTest, MakeAudioDecoderCannotForceStereo) {
  const Environment env =
      CreateEnvironment(std::make_unique<FieldTrials>(CreateTestFieldTrials(
          "WebRTC-Audio-OpusDecodeStereoByDefault/Enabled/")));
  auto decoder =
      AudioDecoderOpus::MakeAudioDecoder(env, /*config=*/{.num_channels = 1});

  EXPECT_EQ(decoder->Channels(), 1u);
}

}  // namespace
}  // namespace webrtc