xref: /aosp_15_r20/external/webrtc/api/audio_codecs/opus/audio_decoder_opus.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 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 <memory>
14 #include <utility>
15 #include <vector>
16 
17 #include "absl/strings/match.h"
18 #include "modules/audio_coding/codecs/opus/audio_decoder_opus.h"
19 
20 namespace webrtc {
21 
IsOk() const22 bool AudioDecoderOpus::Config::IsOk() const {
23   if (sample_rate_hz != 16000 && sample_rate_hz != 48000) {
24     // Unsupported sample rate. (libopus supports a few other rates as
25     // well; we can add support for them when needed.)
26     return false;
27   }
28   if (num_channels != 1 && num_channels != 2) {
29     return false;
30   }
31   return true;
32 }
33 
SdpToConfig(const SdpAudioFormat & format)34 absl::optional<AudioDecoderOpus::Config> AudioDecoderOpus::SdpToConfig(
35     const SdpAudioFormat& format) {
36   const auto num_channels = [&]() -> absl::optional<int> {
37     auto stereo = format.parameters.find("stereo");
38     if (stereo != format.parameters.end()) {
39       if (stereo->second == "0") {
40         return 1;
41       } else if (stereo->second == "1") {
42         return 2;
43       } else {
44         return absl::nullopt;  // Bad stereo parameter.
45       }
46     }
47     return 1;  // Default to mono.
48   }();
49   if (absl::EqualsIgnoreCase(format.name, "opus") &&
50       format.clockrate_hz == 48000 && format.num_channels == 2 &&
51       num_channels) {
52     Config config;
53     config.num_channels = *num_channels;
54     if (!config.IsOk()) {
55       RTC_DCHECK_NOTREACHED();
56       return absl::nullopt;
57     }
58     return config;
59   } else {
60     return absl::nullopt;
61   }
62 }
63 
AppendSupportedDecoders(std::vector<AudioCodecSpec> * specs)64 void AudioDecoderOpus::AppendSupportedDecoders(
65     std::vector<AudioCodecSpec>* specs) {
66   AudioCodecInfo opus_info{48000, 1, 64000, 6000, 510000};
67   opus_info.allow_comfort_noise = false;
68   opus_info.supports_network_adaption = true;
69   SdpAudioFormat opus_format(
70       {"opus", 48000, 2, {{"minptime", "10"}, {"useinbandfec", "1"}}});
71   specs->push_back({std::move(opus_format), opus_info});
72 }
73 
MakeAudioDecoder(Config config,absl::optional<AudioCodecPairId>,const FieldTrialsView * field_trials)74 std::unique_ptr<AudioDecoder> AudioDecoderOpus::MakeAudioDecoder(
75     Config config,
76     absl::optional<AudioCodecPairId> /*codec_pair_id*/,
77     const FieldTrialsView* field_trials) {
78   if (!config.IsOk()) {
79     RTC_DCHECK_NOTREACHED();
80     return nullptr;
81   }
82   return std::make_unique<AudioDecoderOpusImpl>(config.num_channels,
83                                                 config.sample_rate_hz);
84 }
85 
86 }  // namespace webrtc
87