blob: 0fe3367a64f53cd0ffa261b4bc2747c982e9f5b8 [file] [log] [blame]
kwiberg96d74bb2017-06-30 05:24:56 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/audio_codecs/opus/audio_decoder_opus.h"
kwiberg96d74bb2017-06-30 05:24:56 -070012
13#include <memory>
14#include <utility>
15#include <vector>
16
Niels Möller2edab4c2018-10-22 09:48:08 +020017#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/codecs/opus/audio_decoder_opus.h"
kwiberg96d74bb2017-06-30 05:24:56 -070019
20namespace webrtc {
21
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020022bool 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
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020034absl::optional<AudioDecoderOpus::Config> AudioDecoderOpus::SdpToConfig(
kwiberg96d74bb2017-06-30 05:24:56 -070035 const SdpAudioFormat& format) {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020036 const auto num_channels = [&]() -> absl::optional<int> {
kwiberg96d74bb2017-06-30 05:24:56 -070037 auto stereo = format.parameters.find("stereo");
38 if (stereo != format.parameters.end()) {
39 if (stereo->second == "0") {
Oskar Sundbom90657302017-11-16 10:52:34 +010040 return 1;
kwiberg96d74bb2017-06-30 05:24:56 -070041 } else if (stereo->second == "1") {
Oskar Sundbom90657302017-11-16 10:52:34 +010042 return 2;
kwiberg96d74bb2017-06-30 05:24:56 -070043 } else {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020044 return absl::nullopt; // Bad stereo parameter.
kwiberg96d74bb2017-06-30 05:24:56 -070045 }
46 }
Oskar Sundbom90657302017-11-16 10:52:34 +010047 return 1; // Default to mono.
kwiberg96d74bb2017-06-30 05:24:56 -070048 }();
Niels Möller2edab4c2018-10-22 09:48:08 +020049 if (absl::EqualsIgnoreCase(format.name, "opus") &&
kwiberg96d74bb2017-06-30 05:24:56 -070050 format.clockrate_hz == 48000 && format.num_channels == 2 &&
51 num_channels) {
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020052 Config config;
53 config.num_channels = *num_channels;
Ivo Creusendeb1b1b2021-11-24 19:29:10 +000054 if (!config.IsOk()) {
55 RTC_DCHECK_NOTREACHED();
56 return absl::nullopt;
57 }
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020058 return config;
kwiberg96d74bb2017-06-30 05:24:56 -070059 } else {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020060 return absl::nullopt;
kwiberg96d74bb2017-06-30 05:24:56 -070061 }
62}
63
64void 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"}}});
Mirko Bonadei05cf6be2019-01-31 21:38:12 +010071 specs->push_back({std::move(opus_format), opus_info});
kwiberg96d74bb2017-06-30 05:24:56 -070072}
73
74std::unique_ptr<AudioDecoder> AudioDecoderOpus::MakeAudioDecoder(
Karl Wiberg17668ec2018-03-01 15:13:27 +010075 Config config,
Jonas Oreland6e2b9e22022-03-15 14:29:00 +010076 absl::optional<AudioCodecPairId> /*codec_pair_id*/,
77 const WebRtcKeyValueConfig* field_trials) {
Ivo Creusendeb1b1b2021-11-24 19:29:10 +000078 if (!config.IsOk()) {
79 RTC_DCHECK_NOTREACHED();
80 return nullptr;
81 }
Mirko Bonadei317a1f02019-09-17 17:06:18 +020082 return std::make_unique<AudioDecoderOpusImpl>(config.num_channels,
83 config.sample_rate_hz);
kwiberg96d74bb2017-06-30 05:24:56 -070084}
85
86} // namespace webrtc