blob: 82bb1b2e2c06dfb4fdb4170a7d3e9e5ec1432d11 [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
Karl Wiberg918f50c2018-07-05 11:40:33 +020017#include "absl/memory/memory.h"
Niels Möller2edab4c2018-10-22 09:48:08 +020018#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/codecs/opus/audio_decoder_opus.h"
kwiberg96d74bb2017-06-30 05:24:56 -070020
21namespace webrtc {
22
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020023bool AudioDecoderOpus::Config::IsOk() const {
24 if (sample_rate_hz != 16000 && sample_rate_hz != 48000) {
25 // Unsupported sample rate. (libopus supports a few other rates as
26 // well; we can add support for them when needed.)
27 return false;
28 }
29 if (num_channels != 1 && num_channels != 2) {
30 return false;
31 }
32 return true;
33}
34
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020035absl::optional<AudioDecoderOpus::Config> AudioDecoderOpus::SdpToConfig(
kwiberg96d74bb2017-06-30 05:24:56 -070036 const SdpAudioFormat& format) {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020037 const auto num_channels = [&]() -> absl::optional<int> {
kwiberg96d74bb2017-06-30 05:24:56 -070038 auto stereo = format.parameters.find("stereo");
39 if (stereo != format.parameters.end()) {
40 if (stereo->second == "0") {
Oskar Sundbom90657302017-11-16 10:52:34 +010041 return 1;
kwiberg96d74bb2017-06-30 05:24:56 -070042 } else if (stereo->second == "1") {
Oskar Sundbom90657302017-11-16 10:52:34 +010043 return 2;
kwiberg96d74bb2017-06-30 05:24:56 -070044 } else {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020045 return absl::nullopt; // Bad stereo parameter.
kwiberg96d74bb2017-06-30 05:24:56 -070046 }
47 }
Oskar Sundbom90657302017-11-16 10:52:34 +010048 return 1; // Default to mono.
kwiberg96d74bb2017-06-30 05:24:56 -070049 }();
Niels Möller2edab4c2018-10-22 09:48:08 +020050 if (absl::EqualsIgnoreCase(format.name, "opus") &&
kwiberg96d74bb2017-06-30 05:24:56 -070051 format.clockrate_hz == 48000 && format.num_channels == 2 &&
52 num_channels) {
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020053 Config config;
54 config.num_channels = *num_channels;
55 RTC_DCHECK(config.IsOk());
56 return config;
kwiberg96d74bb2017-06-30 05:24:56 -070057 } else {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020058 return absl::nullopt;
kwiberg96d74bb2017-06-30 05:24:56 -070059 }
60}
61
62void AudioDecoderOpus::AppendSupportedDecoders(
63 std::vector<AudioCodecSpec>* specs) {
64 AudioCodecInfo opus_info{48000, 1, 64000, 6000, 510000};
65 opus_info.allow_comfort_noise = false;
66 opus_info.supports_network_adaption = true;
67 SdpAudioFormat opus_format(
68 {"opus", 48000, 2, {{"minptime", "10"}, {"useinbandfec", "1"}}});
Mirko Bonadei05cf6be2019-01-31 21:38:12 +010069 specs->push_back({std::move(opus_format), opus_info});
kwiberg96d74bb2017-06-30 05:24:56 -070070}
71
72std::unique_ptr<AudioDecoder> AudioDecoderOpus::MakeAudioDecoder(
Karl Wiberg17668ec2018-03-01 15:13:27 +010073 Config config,
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020074 absl::optional<AudioCodecPairId> /*codec_pair_id*/) {
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020075 RTC_DCHECK(config.IsOk());
76 return absl::make_unique<AudioDecoderOpusImpl>(config.num_channels,
77 config.sample_rate_hz);
kwiberg96d74bb2017-06-30 05:24:56 -070078}
79
80} // namespace webrtc