blob: 507c8d7d26f78ad746901707a4f481960342d46e [file] [log] [blame]
kwiberg7ea6e592017-08-16 06:12:57 -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/L16/audio_encoder_L16.h"
kwiberg7ea6e592017-08-16 06:12:57 -070012
Mirko Bonadei317a1f02019-09-17 17:06:18 +020013#include <memory>
14
Niels Möller2edab4c2018-10-22 09:48:08 +020015#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h"
17#include "modules/audio_coding/codecs/pcm16b/pcm16b_common.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010018#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberg133cff02018-07-06 15:40:14 +020019#include "rtc_base/numerics/safe_minmax.h"
20#include "rtc_base/string_to_number.h"
kwiberg7ea6e592017-08-16 06:12:57 -070021
22namespace webrtc {
23
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020024absl::optional<AudioEncoderL16::Config> AudioEncoderL16::SdpToConfig(
kwiberg7ea6e592017-08-16 06:12:57 -070025 const SdpAudioFormat& format) {
Karl Wibergeea063f2017-09-14 13:49:13 +020026 if (!rtc::IsValueInRangeForNumericType<int>(format.num_channels)) {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020027 return absl::nullopt;
Karl Wibergeea063f2017-09-14 13:49:13 +020028 }
kwiberg7ea6e592017-08-16 06:12:57 -070029 Config config;
30 config.sample_rate_hz = format.clockrate_hz;
Karl Wibergeea063f2017-09-14 13:49:13 +020031 config.num_channels = rtc::dchecked_cast<int>(format.num_channels);
Karl Wiberg133cff02018-07-06 15:40:14 +020032 auto ptime_iter = format.parameters.find("ptime");
33 if (ptime_iter != format.parameters.end()) {
34 const auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
35 if (ptime && *ptime > 0) {
36 config.frame_size_ms = rtc::SafeClamp(10 * (*ptime / 10), 10, 60);
37 }
38 }
Niels Möller2edab4c2018-10-22 09:48:08 +020039 return absl::EqualsIgnoreCase(format.name, "L16") && config.IsOk()
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020040 ? absl::optional<Config>(config)
41 : absl::nullopt;
kwiberg7ea6e592017-08-16 06:12:57 -070042}
43
44void AudioEncoderL16::AppendSupportedEncoders(
45 std::vector<AudioCodecSpec>* specs) {
46 Pcm16BAppendSupportedCodecSpecs(specs);
47}
48
49AudioCodecInfo AudioEncoderL16::QueryAudioEncoder(
50 const AudioEncoderL16::Config& config) {
51 RTC_DCHECK(config.IsOk());
52 return {config.sample_rate_hz,
53 rtc::dchecked_cast<size_t>(config.num_channels),
54 config.sample_rate_hz * config.num_channels * 16};
55}
56
57std::unique_ptr<AudioEncoder> AudioEncoderL16::MakeAudioEncoder(
58 const AudioEncoderL16::Config& config,
Karl Wiberg17668ec2018-03-01 15:13:27 +010059 int payload_type,
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020060 absl::optional<AudioCodecPairId> /*codec_pair_id*/) {
kwiberg7ea6e592017-08-16 06:12:57 -070061 RTC_DCHECK(config.IsOk());
62 AudioEncoderPcm16B::Config c;
63 c.sample_rate_hz = config.sample_rate_hz;
64 c.num_channels = config.num_channels;
65 c.frame_size_ms = config.frame_size_ms;
66 c.payload_type = payload_type;
Mirko Bonadei317a1f02019-09-17 17:06:18 +020067 return std::make_unique<AudioEncoderPcm16B>(c);
kwiberg7ea6e592017-08-16 06:12:57 -070068}
69
70} // namespace webrtc