kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/audio_codecs/opus/audio_decoder_opus.h" |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | #include <vector> |
| 16 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 18 | #include "absl/strings/match.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_coding/codecs/opus/audio_decoder_opus.h" |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 23 | absl::optional<AudioDecoderOpus::Config> AudioDecoderOpus::SdpToConfig( |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 24 | const SdpAudioFormat& format) { |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 25 | const auto num_channels = [&]() -> absl::optional<int> { |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 26 | auto stereo = format.parameters.find("stereo"); |
| 27 | if (stereo != format.parameters.end()) { |
| 28 | if (stereo->second == "0") { |
Oskar Sundbom | 9065730 | 2017-11-16 10:52:34 +0100 | [diff] [blame] | 29 | return 1; |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 30 | } else if (stereo->second == "1") { |
Oskar Sundbom | 9065730 | 2017-11-16 10:52:34 +0100 | [diff] [blame] | 31 | return 2; |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 32 | } else { |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 33 | return absl::nullopt; // Bad stereo parameter. |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 34 | } |
| 35 | } |
Oskar Sundbom | 9065730 | 2017-11-16 10:52:34 +0100 | [diff] [blame] | 36 | return 1; // Default to mono. |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 37 | }(); |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 38 | if (absl::EqualsIgnoreCase(format.name, "opus") && |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 39 | format.clockrate_hz == 48000 && format.num_channels == 2 && |
| 40 | num_channels) { |
Oskar Sundbom | 9065730 | 2017-11-16 10:52:34 +0100 | [diff] [blame] | 41 | return Config{*num_channels}; |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 42 | } else { |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 43 | return absl::nullopt; |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | void AudioDecoderOpus::AppendSupportedDecoders( |
| 48 | std::vector<AudioCodecSpec>* specs) { |
| 49 | AudioCodecInfo opus_info{48000, 1, 64000, 6000, 510000}; |
| 50 | opus_info.allow_comfort_noise = false; |
| 51 | opus_info.supports_network_adaption = true; |
| 52 | SdpAudioFormat opus_format( |
| 53 | {"opus", 48000, 2, {{"minptime", "10"}, {"useinbandfec", "1"}}}); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 54 | specs->push_back({std::move(opus_format), opus_info}); |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | std::unique_ptr<AudioDecoder> AudioDecoderOpus::MakeAudioDecoder( |
Karl Wiberg | 17668ec | 2018-03-01 15:13:27 +0100 | [diff] [blame] | 58 | Config config, |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 59 | absl::optional<AudioCodecPairId> /*codec_pair_id*/) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 60 | return absl::make_unique<AudioDecoderOpusImpl>(config.num_channels); |
kwiberg | 96d74bb | 2017-06-30 05:24:56 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | } // namespace webrtc |