Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1778)

Side by Side Diff: webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.cc

Issue 2695243005: Injectable audio encoders: BuiltinAudioEncoderFactory (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h" 11 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 #include <limits> 14 #include <limits>
15 #include "webrtc/base/checks.h" 15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/string_to_number.h"
16 #include "webrtc/common_types.h" 17 #include "webrtc/common_types.h"
17 #include "webrtc/modules/audio_coding/codecs/ilbc/ilbc.h" 18 #include "webrtc/modules/audio_coding/codecs/ilbc/ilbc.h"
18 19
19 namespace webrtc { 20 namespace webrtc {
20 21
21 namespace { 22 namespace {
22 23
23 const int kSampleRateHz = 8000; 24 const int kSampleRateHz = 8000;
24 25
25 AudioEncoderIlbc::Config CreateConfig(const CodecInst& codec_inst) { 26 AudioEncoderIlbc::Config CreateConfig(const CodecInst& codec_inst) {
26 AudioEncoderIlbc::Config config; 27 AudioEncoderIlbc::Config config;
27 config.frame_size_ms = codec_inst.pacsize / 8; 28 config.frame_size_ms = codec_inst.pacsize / 8;
28 config.payload_type = codec_inst.pltype; 29 config.payload_type = codec_inst.pltype;
29 return config; 30 return config;
30 } 31 }
31 32
33 AudioEncoderIlbc::Config CreateConfig(int payload_type,
34 const SdpAudioFormat& format) {
35 AudioEncoderIlbc::Config config;
36 config.payload_type = payload_type;
37 auto ptime_iter = format.parameters.find("ptime");
38 if (ptime_iter != format.parameters.end()) {
39 auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
40 if (ptime && *ptime > 0) {
41 config.frame_size_ms = *ptime;
42 }
43 }
44 return config;
45 }
46
47 int GetIlbcBitrate(int ptime) {
48 switch (ptime) {
49 case 20: case 40:
50 // 38 bytes per frame of 20 ms => 15200 bits/s.
51 return 15200;
52 case 30: case 60:
53 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s.
54 return 13333;
55 default:
56 FATAL();
57 }
58 }
59
32 } // namespace 60 } // namespace
33 61
34 // static 62 // static
35 const size_t AudioEncoderIlbc::kMaxSamplesPerPacket; 63 const size_t AudioEncoderIlbc::kMaxSamplesPerPacket;
36 64
37 bool AudioEncoderIlbc::Config::IsOk() const { 65 bool AudioEncoderIlbc::Config::IsOk() const {
38 return (frame_size_ms == 20 || frame_size_ms == 30 || frame_size_ms == 40 || 66 return (frame_size_ms == 20 || frame_size_ms == 30 || frame_size_ms == 40 ||
39 frame_size_ms == 60) && 67 frame_size_ms == 60) &&
40 static_cast<size_t>(kSampleRateHz / 100 * (frame_size_ms / 10)) <= 68 static_cast<size_t>(kSampleRateHz / 100 * (frame_size_ms / 10)) <=
41 kMaxSamplesPerPacket; 69 kMaxSamplesPerPacket;
42 } 70 }
43 71
44 AudioEncoderIlbc::AudioEncoderIlbc(const Config& config) 72 AudioEncoderIlbc::AudioEncoderIlbc(const Config& config)
45 : config_(config), 73 : config_(config),
46 num_10ms_frames_per_packet_( 74 num_10ms_frames_per_packet_(
47 static_cast<size_t>(config.frame_size_ms / 10)), 75 static_cast<size_t>(config.frame_size_ms / 10)),
48 encoder_(nullptr) { 76 encoder_(nullptr) {
49 Reset(); 77 Reset();
50 } 78 }
51 79
52 AudioEncoderIlbc::AudioEncoderIlbc(const CodecInst& codec_inst) 80 AudioEncoderIlbc::AudioEncoderIlbc(const CodecInst& codec_inst)
53 : AudioEncoderIlbc(CreateConfig(codec_inst)) {} 81 : AudioEncoderIlbc(CreateConfig(codec_inst)) {}
54 82
83 AudioEncoderIlbc::AudioEncoderIlbc(int payload_type,
84 const SdpAudioFormat& format)
85 : AudioEncoderIlbc(CreateConfig(payload_type, format)) {}
86
87 rtc::Optional<AudioFormatInfo> AudioEncoderIlbc::QueryAudioFormat(
88 const SdpAudioFormat& format) {
89 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 &&
90 format.clockrate_hz == 8000 && format.num_channels == 1) {
91 Config config = CreateConfig(0, format);
92 if (config.IsOk()) {
93 return rtc::Optional<AudioFormatInfo>(
94 {kSampleRateHz, 1, GetIlbcBitrate(config.frame_size_ms)});
95 }
96 }
97
98 return rtc::Optional<AudioFormatInfo>();
99 }
100
101
55 AudioEncoderIlbc::~AudioEncoderIlbc() { 102 AudioEncoderIlbc::~AudioEncoderIlbc() {
56 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_)); 103 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_));
57 } 104 }
58 105
59 int AudioEncoderIlbc::SampleRateHz() const { 106 int AudioEncoderIlbc::SampleRateHz() const {
60 return kSampleRateHz; 107 return kSampleRateHz;
61 } 108 }
62 109
63 size_t AudioEncoderIlbc::NumChannels() const { 110 size_t AudioEncoderIlbc::NumChannels() const {
64 return 1; 111 return 1;
65 } 112 }
66 113
67 size_t AudioEncoderIlbc::Num10MsFramesInNextPacket() const { 114 size_t AudioEncoderIlbc::Num10MsFramesInNextPacket() const {
68 return num_10ms_frames_per_packet_; 115 return num_10ms_frames_per_packet_;
69 } 116 }
70 117
71 size_t AudioEncoderIlbc::Max10MsFramesInAPacket() const { 118 size_t AudioEncoderIlbc::Max10MsFramesInAPacket() const {
72 return num_10ms_frames_per_packet_; 119 return num_10ms_frames_per_packet_;
73 } 120 }
74 121
75 int AudioEncoderIlbc::GetTargetBitrate() const { 122 int AudioEncoderIlbc::GetTargetBitrate() const {
76 switch (num_10ms_frames_per_packet_) { 123 return GetIlbcBitrate(num_10ms_frames_per_packet_ * 10);
77 case 2: case 4:
78 // 38 bytes per frame of 20 ms => 15200 bits/s.
79 return 15200;
80 case 3: case 6:
81 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s.
82 return 13333;
83 default:
84 FATAL();
85 }
86 } 124 }
87 125
88 AudioEncoder::EncodedInfo AudioEncoderIlbc::EncodeImpl( 126 AudioEncoder::EncodedInfo AudioEncoderIlbc::EncodeImpl(
89 uint32_t rtp_timestamp, 127 uint32_t rtp_timestamp,
90 rtc::ArrayView<const int16_t> audio, 128 rtc::ArrayView<const int16_t> audio,
91 rtc::Buffer* encoded) { 129 rtc::Buffer* encoded) {
92 130
93 // Save timestamp if starting a new packet. 131 // Save timestamp if starting a new packet.
94 if (num_10ms_frames_buffered_ == 0) 132 if (num_10ms_frames_buffered_ == 0)
95 first_timestamp_in_buffer_ = rtp_timestamp; 133 first_timestamp_in_buffer_ = rtp_timestamp;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 switch (num_10ms_frames_per_packet_) { 185 switch (num_10ms_frames_per_packet_) {
148 case 2: return 38; 186 case 2: return 38;
149 case 3: return 50; 187 case 3: return 50;
150 case 4: return 2 * 38; 188 case 4: return 2 * 38;
151 case 6: return 2 * 50; 189 case 6: return 2 * 50;
152 default: FATAL(); 190 default: FATAL();
153 } 191 }
154 } 192 }
155 193
156 } // namespace webrtc 194 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698