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