OLD | NEW |
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/g722/audio_encoder_g722.h" | 11 #include "webrtc/modules/audio_coding/codecs/g722/audio_encoder_g722.h" |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/string_to_number.h" |
15 #include "webrtc/common_types.h" | 16 #include "webrtc/common_types.h" |
16 #include "webrtc/modules/audio_coding/codecs/g722/g722_interface.h" | 17 #include "webrtc/modules/audio_coding/codecs/g722/g722_interface.h" |
17 | 18 |
18 namespace webrtc { | 19 namespace webrtc { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const size_t kSampleRateHz = 16000; | 23 const size_t kSampleRateHz = 16000; |
23 | 24 |
24 AudioEncoderG722::Config CreateConfig(const CodecInst& codec_inst) { | 25 AudioEncoderG722::Config CreateConfig(const CodecInst& codec_inst) { |
25 AudioEncoderG722::Config config; | 26 AudioEncoderG722::Config config; |
26 config.num_channels = codec_inst.channels; | 27 config.num_channels = codec_inst.channels; |
27 config.frame_size_ms = codec_inst.pacsize / 16; | 28 config.frame_size_ms = codec_inst.pacsize / 16; |
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 AudioEncoderG722::Config CreateConfig(int payload_type, |
| 34 const SdpAudioFormat& format) { |
| 35 AudioEncoderG722::Config config; |
| 36 config.payload_type = payload_type; |
| 37 config.num_channels = format.num_channels; |
| 38 config.frame_size_ms = 20; |
| 39 auto ptime_iter = format.parameters.find("ptime"); |
| 40 if (ptime_iter != format.parameters.end()) { |
| 41 auto ptime = rtc::StringToNumber<int>(ptime_iter->second); |
| 42 if (ptime && *ptime > 0) { |
| 43 config.frame_size_ms = *ptime; |
| 44 } |
| 45 } |
| 46 return config; |
| 47 } |
| 48 |
32 } // namespace | 49 } // namespace |
33 | 50 |
34 bool AudioEncoderG722::Config::IsOk() const { | 51 bool AudioEncoderG722::Config::IsOk() const { |
35 return (frame_size_ms > 0) && (frame_size_ms % 10 == 0) && | 52 return (frame_size_ms > 0) && (frame_size_ms % 10 == 0) && |
36 (num_channels >= 1); | 53 (num_channels >= 1); |
37 } | 54 } |
38 | 55 |
39 AudioEncoderG722::AudioEncoderG722(const Config& config) | 56 AudioEncoderG722::AudioEncoderG722(const Config& config) |
40 : num_channels_(config.num_channels), | 57 : num_channels_(config.num_channels), |
41 payload_type_(config.payload_type), | 58 payload_type_(config.payload_type), |
42 num_10ms_frames_per_packet_( | 59 num_10ms_frames_per_packet_( |
43 static_cast<size_t>(config.frame_size_ms / 10)), | 60 static_cast<size_t>(config.frame_size_ms / 10)), |
44 num_10ms_frames_buffered_(0), | 61 num_10ms_frames_buffered_(0), |
45 first_timestamp_in_buffer_(0), | 62 first_timestamp_in_buffer_(0), |
46 encoders_(new EncoderState[num_channels_]), | 63 encoders_(new EncoderState[num_channels_]), |
47 interleave_buffer_(2 * num_channels_) { | 64 interleave_buffer_(2 * num_channels_) { |
48 RTC_CHECK(config.IsOk()); | 65 RTC_CHECK(config.IsOk()); |
49 const size_t samples_per_channel = | 66 const size_t samples_per_channel = |
50 kSampleRateHz / 100 * num_10ms_frames_per_packet_; | 67 kSampleRateHz / 100 * num_10ms_frames_per_packet_; |
51 for (size_t i = 0; i < num_channels_; ++i) { | 68 for (size_t i = 0; i < num_channels_; ++i) { |
52 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]); | 69 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]); |
53 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2); | 70 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2); |
54 } | 71 } |
55 Reset(); | 72 Reset(); |
56 } | 73 } |
57 | 74 |
58 AudioEncoderG722::AudioEncoderG722(const CodecInst& codec_inst) | 75 AudioEncoderG722::AudioEncoderG722(const CodecInst& codec_inst) |
59 : AudioEncoderG722(CreateConfig(codec_inst)) {} | 76 : AudioEncoderG722(CreateConfig(codec_inst)) {} |
60 | 77 |
| 78 AudioEncoderG722::AudioEncoderG722(int payload_type, |
| 79 const SdpAudioFormat& format) |
| 80 : AudioEncoderG722(CreateConfig(payload_type, format)) {} |
| 81 |
61 AudioEncoderG722::~AudioEncoderG722() = default; | 82 AudioEncoderG722::~AudioEncoderG722() = default; |
62 | 83 |
| 84 rtc::Optional<AudioCodecInfo> AudioEncoderG722::QueryAudioEncoder( |
| 85 const SdpAudioFormat& format) { |
| 86 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0) { |
| 87 Config config = CreateConfig(0, format); |
| 88 if (format.clockrate_hz == 8000 && config.IsOk()) { |
| 89 return rtc::Optional<AudioCodecInfo>( |
| 90 {kSampleRateHz, config.num_channels, 64000}); |
| 91 } |
| 92 } |
| 93 return rtc::Optional<AudioCodecInfo>(); |
| 94 } |
| 95 |
| 96 |
63 int AudioEncoderG722::SampleRateHz() const { | 97 int AudioEncoderG722::SampleRateHz() const { |
64 return kSampleRateHz; | 98 return kSampleRateHz; |
65 } | 99 } |
66 | 100 |
67 size_t AudioEncoderG722::NumChannels() const { | 101 size_t AudioEncoderG722::NumChannels() const { |
68 return num_channels_; | 102 return num_channels_; |
69 } | 103 } |
70 | 104 |
71 int AudioEncoderG722::RtpTimestampRateHz() const { | 105 int AudioEncoderG722::RtpTimestampRateHz() const { |
72 // The RTP timestamp rate for G.722 is 8000 Hz, even though it is a 16 kHz | 106 // The RTP timestamp rate for G.722 is 8000 Hz, even though it is a 16 kHz |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 189 |
156 AudioEncoderG722::EncoderState::~EncoderState() { | 190 AudioEncoderG722::EncoderState::~EncoderState() { |
157 RTC_CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder)); | 191 RTC_CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder)); |
158 } | 192 } |
159 | 193 |
160 size_t AudioEncoderG722::SamplesPerChannel() const { | 194 size_t AudioEncoderG722::SamplesPerChannel() const { |
161 return kSampleRateHz / 100 * num_10ms_frames_per_packet_; | 195 return kSampleRateHz / 100 * num_10ms_frames_per_packet_; |
162 } | 196 } |
163 | 197 |
164 } // namespace webrtc | 198 } // namespace webrtc |
OLD | NEW |