blob: 53dfdd31eafbd2c67a6976d5a5c04eeb793cffc2 [file] [log] [blame]
solenbergdb3c9b02017-06-28 02:05:04 -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/ilbc/audio_encoder_ilbc.h"
solenbergdb3c9b02017-06-28 02:05:04 -070012
13#include <memory>
14#include <vector>
15
Mirko Bonadei71207422017-09-15 13:58:09 +020016#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010018#include "rtc_base/numerics/safe_conversions.h"
19#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/string_to_number.h"
solenbergdb3c9b02017-06-28 02:05:04 -070022
23namespace webrtc {
24namespace {
25int GetIlbcBitrate(int ptime) {
26 switch (ptime) {
27 case 20:
28 case 40:
29 // 38 bytes per frame of 20 ms => 15200 bits/s.
30 return 15200;
31 case 30:
32 case 60:
33 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s.
34 return 13333;
35 default:
36 FATAL();
37 }
38}
39} // namespace
40
41rtc::Optional<AudioEncoderIlbcConfig> AudioEncoderIlbc::SdpToConfig(
42 const SdpAudioFormat& format) {
kwibergd1d79f62017-08-25 22:22:42 -070043 if (STR_CASE_CMP(format.name.c_str(), "ILBC") != 0 ||
44 format.clockrate_hz != 8000 || format.num_channels != 1) {
Oskar Sundbom90657302017-11-16 10:52:34 +010045 return rtc::nullopt;
kwibergd1d79f62017-08-25 22:22:42 -070046 }
47
48 AudioEncoderIlbcConfig config;
49 auto ptime_iter = format.parameters.find("ptime");
50 if (ptime_iter != format.parameters.end()) {
51 auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
52 if (ptime && *ptime > 0) {
53 const int whole_packets = *ptime / 10;
54 config.frame_size_ms = rtc::SafeClamp<int>(whole_packets * 10, 20, 60);
55 }
56 }
57 return config.IsOk() ? rtc::Optional<AudioEncoderIlbcConfig>(config)
Oskar Sundbom90657302017-11-16 10:52:34 +010058 : rtc::nullopt;
solenbergdb3c9b02017-06-28 02:05:04 -070059}
60
61void AudioEncoderIlbc::AppendSupportedEncoders(
62 std::vector<AudioCodecSpec>* specs) {
63 const SdpAudioFormat fmt = {"ILBC", 8000, 1};
64 const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt));
65 specs->push_back({fmt, info});
66}
67
68AudioCodecInfo AudioEncoderIlbc::QueryAudioEncoder(
69 const AudioEncoderIlbcConfig& config) {
70 RTC_DCHECK(config.IsOk());
71 return {8000, 1, GetIlbcBitrate(config.frame_size_ms)};
72}
73
74std::unique_ptr<AudioEncoder> AudioEncoderIlbc::MakeAudioEncoder(
75 const AudioEncoderIlbcConfig& config,
Karl Wiberg17668ec2018-03-01 15:13:27 +010076 int payload_type,
77 rtc::Optional<AudioCodecPairId> /*codec_pair_id*/) {
solenbergdb3c9b02017-06-28 02:05:04 -070078 RTC_DCHECK(config.IsOk());
79 return rtc::MakeUnique<AudioEncoderIlbcImpl>(config, payload_type);
80}
81
82} // namespace webrtc