| 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/safe_conversions.h" |
| 18 #include "webrtc/base/string_to_number.h" |
| 15 #include "webrtc/common_types.h" | 19 #include "webrtc/common_types.h" |
| 16 #include "webrtc/modules/audio_coding/codecs/g722/g722_interface.h" | 20 #include "webrtc/modules/audio_coding/codecs/g722/g722_interface.h" |
| 17 | 21 |
| 18 namespace webrtc { | 22 namespace webrtc { |
| 19 | 23 |
| 20 namespace { | 24 namespace { |
| 21 | 25 |
| 22 const size_t kSampleRateHz = 16000; | 26 const size_t kSampleRateHz = 16000; |
| 23 | 27 |
| 24 AudioEncoderG722::Config CreateConfig(const CodecInst& codec_inst) { | 28 AudioEncoderG722::Config CreateConfig(const CodecInst& codec_inst) { |
| 25 AudioEncoderG722::Config config; | 29 AudioEncoderG722::Config config; |
| 26 config.num_channels = codec_inst.channels; | 30 config.num_channels = codec_inst.channels; |
| 27 config.frame_size_ms = codec_inst.pacsize / 16; | 31 config.frame_size_ms = codec_inst.pacsize / 16; |
| 28 config.payload_type = codec_inst.pltype; | 32 config.payload_type = codec_inst.pltype; |
| 29 return config; | 33 return config; |
| 30 } | 34 } |
| 31 | 35 |
| 36 AudioEncoderG722::Config CreateConfig(int payload_type, |
| 37 const SdpAudioFormat& format) { |
| 38 AudioEncoderG722::Config config; |
| 39 config.payload_type = payload_type; |
| 40 config.num_channels = format.num_channels; |
| 41 auto ptime_iter = format.parameters.find("ptime"); |
| 42 if (ptime_iter != format.parameters.end()) { |
| 43 auto ptime = rtc::StringToNumber<int>(ptime_iter->second); |
| 44 if (ptime && *ptime > 0) { |
| 45 const int whole_packets = *ptime / 10; |
| 46 config.frame_size_ms = std::max(10, std::min(whole_packets * 10, 60)); |
| 47 } |
| 48 } |
| 49 return config; |
| 50 } |
| 51 |
| 32 } // namespace | 52 } // namespace |
| 33 | 53 |
| 34 bool AudioEncoderG722::Config::IsOk() const { | 54 bool AudioEncoderG722::Config::IsOk() const { |
| 35 return (frame_size_ms > 0) && (frame_size_ms % 10 == 0) && | 55 return (frame_size_ms > 0) && (frame_size_ms % 10 == 0) && |
| 36 (num_channels >= 1); | 56 (num_channels >= 1); |
| 37 } | 57 } |
| 38 | 58 |
| 39 AudioEncoderG722::AudioEncoderG722(const Config& config) | 59 AudioEncoderG722::AudioEncoderG722(const Config& config) |
| 40 : num_channels_(config.num_channels), | 60 : num_channels_(config.num_channels), |
| 41 payload_type_(config.payload_type), | 61 payload_type_(config.payload_type), |
| 42 num_10ms_frames_per_packet_( | 62 num_10ms_frames_per_packet_( |
| 43 static_cast<size_t>(config.frame_size_ms / 10)), | 63 static_cast<size_t>(config.frame_size_ms / 10)), |
| 44 num_10ms_frames_buffered_(0), | 64 num_10ms_frames_buffered_(0), |
| 45 first_timestamp_in_buffer_(0), | 65 first_timestamp_in_buffer_(0), |
| 46 encoders_(new EncoderState[num_channels_]), | 66 encoders_(new EncoderState[num_channels_]), |
| 47 interleave_buffer_(2 * num_channels_) { | 67 interleave_buffer_(2 * num_channels_) { |
| 48 RTC_CHECK(config.IsOk()); | 68 RTC_CHECK(config.IsOk()); |
| 49 const size_t samples_per_channel = | 69 const size_t samples_per_channel = |
| 50 kSampleRateHz / 100 * num_10ms_frames_per_packet_; | 70 kSampleRateHz / 100 * num_10ms_frames_per_packet_; |
| 51 for (size_t i = 0; i < num_channels_; ++i) { | 71 for (size_t i = 0; i < num_channels_; ++i) { |
| 52 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]); | 72 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]); |
| 53 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2); | 73 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2); |
| 54 } | 74 } |
| 55 Reset(); | 75 Reset(); |
| 56 } | 76 } |
| 57 | 77 |
| 58 AudioEncoderG722::AudioEncoderG722(const CodecInst& codec_inst) | 78 AudioEncoderG722::AudioEncoderG722(const CodecInst& codec_inst) |
| 59 : AudioEncoderG722(CreateConfig(codec_inst)) {} | 79 : AudioEncoderG722(CreateConfig(codec_inst)) {} |
| 60 | 80 |
| 81 AudioEncoderG722::AudioEncoderG722(int payload_type, |
| 82 const SdpAudioFormat& format) |
| 83 : AudioEncoderG722(CreateConfig(payload_type, format)) {} |
| 84 |
| 61 AudioEncoderG722::~AudioEncoderG722() = default; | 85 AudioEncoderG722::~AudioEncoderG722() = default; |
| 62 | 86 |
| 87 rtc::Optional<AudioCodecInfo> AudioEncoderG722::QueryAudioEncoder( |
| 88 const SdpAudioFormat& format) { |
| 89 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0) { |
| 90 Config config = CreateConfig(0, format); |
| 91 if (format.clockrate_hz == 8000 && config.IsOk()) { |
| 92 return rtc::Optional<AudioCodecInfo>( |
| 93 {rtc::dchecked_cast<int>(kSampleRateHz), config.num_channels, 64000}); |
| 94 } |
| 95 } |
| 96 return rtc::Optional<AudioCodecInfo>(); |
| 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 |