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

Side by Side Diff: webrtc/modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.cc

Issue 2695243005: Injectable audio encoders: BuiltinAudioEncoderFactory (Closed)
Patch Set: Cleaned up Opus code a bit. kHz -> Hz in comment picked a bunch of lint Created 3 years, 9 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/pcm16b/audio_encoder_pcm16b.h" 11 #include "webrtc/modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h"
12 12
13 #include <algorithm>
14
13 #include "webrtc/base/checks.h" 15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/string_to_number.h"
14 #include "webrtc/common_types.h" 17 #include "webrtc/common_types.h"
15 #include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h" 18 #include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
16 19
17 namespace webrtc { 20 namespace webrtc {
18 21
19 size_t AudioEncoderPcm16B::EncodeCall(const int16_t* audio, 22 size_t AudioEncoderPcm16B::EncodeCall(const int16_t* audio,
20 size_t input_len, 23 size_t input_len,
21 uint8_t* encoded) { 24 uint8_t* encoded) {
22 return WebRtcPcm16b_Encode(audio, input_len, encoded); 25 return WebRtcPcm16b_Encode(audio, input_len, encoded);
23 } 26 }
24 27
25 size_t AudioEncoderPcm16B::BytesPerSample() const { 28 size_t AudioEncoderPcm16B::BytesPerSample() const {
26 return 2; 29 return 2;
27 } 30 }
28 31
29 AudioEncoder::CodecType AudioEncoderPcm16B::GetCodecType() const { 32 AudioEncoder::CodecType AudioEncoderPcm16B::GetCodecType() const {
30 return CodecType::kOther; 33 return CodecType::kOther;
31 } 34 }
32 35
33 namespace { 36 namespace {
34 AudioEncoderPcm16B::Config CreateConfig(const CodecInst& codec_inst) { 37 AudioEncoderPcm16B::Config CreateConfig(const CodecInst& codec_inst) {
35 AudioEncoderPcm16B::Config config; 38 AudioEncoderPcm16B::Config config;
36 config.num_channels = codec_inst.channels; 39 config.num_channels = codec_inst.channels;
37 config.sample_rate_hz = codec_inst.plfreq; 40 config.sample_rate_hz = codec_inst.plfreq;
38 config.frame_size_ms = rtc::CheckedDivExact( 41 config.frame_size_ms = rtc::CheckedDivExact(
39 codec_inst.pacsize, rtc::CheckedDivExact(config.sample_rate_hz, 1000)); 42 codec_inst.pacsize, rtc::CheckedDivExact(config.sample_rate_hz, 1000));
40 config.payload_type = codec_inst.pltype; 43 config.payload_type = codec_inst.pltype;
41 return config; 44 return config;
42 } 45 }
46
47 AudioEncoderPcm16B::Config CreateConfig(int payload_type,
48 const SdpAudioFormat& format) {
49 AudioEncoderPcm16B::Config config;
50 config.num_channels = format.num_channels;
51 config.sample_rate_hz = format.clockrate_hz;
52 config.frame_size_ms = 10;
53 auto ptime_iter = format.parameters.find("ptime");
54 if (ptime_iter != format.parameters.end()) {
55 auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
56 if (ptime && *ptime > 0) {
57 const int whole_packets = *ptime / 10;
58 config.frame_size_ms = std::max(10, std::min(whole_packets * 10, 60));
59 }
60 }
61 config.payload_type = payload_type;
62 return config;
63 }
43 } // namespace 64 } // namespace
44 65
45 bool AudioEncoderPcm16B::Config::IsOk() const { 66 bool AudioEncoderPcm16B::Config::IsOk() const {
46 if ((sample_rate_hz != 8000) && (sample_rate_hz != 16000) && 67 if ((sample_rate_hz != 8000) && (sample_rate_hz != 16000) &&
47 (sample_rate_hz != 32000) && (sample_rate_hz != 48000)) 68 (sample_rate_hz != 32000) && (sample_rate_hz != 48000))
48 return false; 69 return false;
49 return AudioEncoderPcm::Config::IsOk(); 70 return AudioEncoderPcm::Config::IsOk();
50 } 71 }
51 72
52 AudioEncoderPcm16B::AudioEncoderPcm16B(const CodecInst& codec_inst) 73 AudioEncoderPcm16B::AudioEncoderPcm16B(const CodecInst& codec_inst)
53 : AudioEncoderPcm16B(CreateConfig(codec_inst)) {} 74 : AudioEncoderPcm16B(CreateConfig(codec_inst)) {}
54 75
76 AudioEncoderPcm16B::AudioEncoderPcm16B(int payload_type,
77 const SdpAudioFormat& format)
78 : AudioEncoderPcm16B(CreateConfig(payload_type, format)) {}
79
80 rtc::Optional<AudioCodecInfo> AudioEncoderPcm16B::QueryAudioEncoder(
81 const SdpAudioFormat& format) {
82 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 &&
83 format.num_channels >= 1) {
84 Config config = CreateConfig(0, format);
85 if (config.IsOk()) {
86 return rtc::Optional<AudioCodecInfo>(
87 {config.sample_rate_hz, config.num_channels,
88 config.sample_rate_hz * 2 * config.num_channels});
89 }
90 }
91 return rtc::Optional<AudioCodecInfo>();
92 }
93
55 } // namespace webrtc 94 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698