| 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/g711/include/audio_encoder_pcm.h" | 11 #include "webrtc/modules/audio_coding/codecs/g711/include/audio_encoder_pcm.h" |
| 12 | 12 |
| 13 #include <limits> | 13 #include <limits> |
| 14 | 14 |
| 15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/common_types.h" | 16 #include "webrtc/common_types.h" |
| 17 #include "webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h" | 17 #include "webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h" |
| 18 | 18 |
| 19 namespace webrtc { | 19 namespace webrtc { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 int16_t NumSamplesPerFrame(int num_channels, | 23 int16_t NumSamplesPerFrame(int num_channels, |
| 24 int frame_size_ms, | 24 int frame_size_ms, |
| 25 int sample_rate_hz) { | 25 int sample_rate_hz) { |
| 26 int samples_per_frame = num_channels * frame_size_ms * sample_rate_hz / 1000; | 26 int samples_per_frame = num_channels * frame_size_ms * sample_rate_hz / 1000; |
| 27 CHECK_LE(samples_per_frame, std::numeric_limits<int16_t>::max()) | 27 RTC_CHECK_LE(samples_per_frame, std::numeric_limits<int16_t>::max()) |
| 28 << "Frame size too large."; | 28 << "Frame size too large."; |
| 29 return static_cast<int16_t>(samples_per_frame); | 29 return static_cast<int16_t>(samples_per_frame); |
| 30 } | 30 } |
| 31 | 31 |
| 32 template <typename T> | 32 template <typename T> |
| 33 typename T::Config CreateConfig(const CodecInst& codec_inst) { | 33 typename T::Config CreateConfig(const CodecInst& codec_inst) { |
| 34 typename T::Config config; | 34 typename T::Config config; |
| 35 config.frame_size_ms = codec_inst.pacsize / 8; | 35 config.frame_size_ms = codec_inst.pacsize / 8; |
| 36 config.num_channels = codec_inst.channels; | 36 config.num_channels = codec_inst.channels; |
| 37 config.payload_type = codec_inst.pltype; | 37 config.payload_type = codec_inst.pltype; |
| 38 return config; | 38 return config; |
| 39 } | 39 } |
| 40 | 40 |
| 41 } // namespace | 41 } // namespace |
| 42 | 42 |
| 43 bool AudioEncoderPcm::Config::IsOk() const { | 43 bool AudioEncoderPcm::Config::IsOk() const { |
| 44 return (frame_size_ms % 10 == 0) && (num_channels >= 1); | 44 return (frame_size_ms % 10 == 0) && (num_channels >= 1); |
| 45 } | 45 } |
| 46 | 46 |
| 47 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) | 47 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) |
| 48 : sample_rate_hz_(sample_rate_hz), | 48 : sample_rate_hz_(sample_rate_hz), |
| 49 num_channels_(config.num_channels), | 49 num_channels_(config.num_channels), |
| 50 payload_type_(config.payload_type), | 50 payload_type_(config.payload_type), |
| 51 num_10ms_frames_per_packet_( | 51 num_10ms_frames_per_packet_( |
| 52 static_cast<size_t>(config.frame_size_ms / 10)), | 52 static_cast<size_t>(config.frame_size_ms / 10)), |
| 53 full_frame_samples_(NumSamplesPerFrame(config.num_channels, | 53 full_frame_samples_(NumSamplesPerFrame(config.num_channels, |
| 54 config.frame_size_ms, | 54 config.frame_size_ms, |
| 55 sample_rate_hz_)), | 55 sample_rate_hz_)), |
| 56 first_timestamp_in_buffer_(0) { | 56 first_timestamp_in_buffer_(0) { |
| 57 CHECK_GT(sample_rate_hz, 0) << "Sample rate must be larger than 0 Hz"; | 57 RTC_CHECK_GT(sample_rate_hz, 0) << "Sample rate must be larger than 0 Hz"; |
| 58 CHECK_EQ(config.frame_size_ms % 10, 0) | 58 RTC_CHECK_EQ(config.frame_size_ms % 10, 0) |
| 59 << "Frame size must be an integer multiple of 10 ms."; | 59 << "Frame size must be an integer multiple of 10 ms."; |
| 60 speech_buffer_.reserve(full_frame_samples_); | 60 speech_buffer_.reserve(full_frame_samples_); |
| 61 } | 61 } |
| 62 | 62 |
| 63 AudioEncoderPcm::~AudioEncoderPcm() = default; | 63 AudioEncoderPcm::~AudioEncoderPcm() = default; |
| 64 | 64 |
| 65 size_t AudioEncoderPcm::MaxEncodedBytes() const { | 65 size_t AudioEncoderPcm::MaxEncodedBytes() const { |
| 66 return full_frame_samples_ * BytesPerSample(); | 66 return full_frame_samples_ * BytesPerSample(); |
| 67 } | 67 } |
| 68 | 68 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 94 const int num_samples = SampleRateHz() / 100 * NumChannels(); | 94 const int num_samples = SampleRateHz() / 100 * NumChannels(); |
| 95 if (speech_buffer_.empty()) { | 95 if (speech_buffer_.empty()) { |
| 96 first_timestamp_in_buffer_ = rtp_timestamp; | 96 first_timestamp_in_buffer_ = rtp_timestamp; |
| 97 } | 97 } |
| 98 for (int i = 0; i < num_samples; ++i) { | 98 for (int i = 0; i < num_samples; ++i) { |
| 99 speech_buffer_.push_back(audio[i]); | 99 speech_buffer_.push_back(audio[i]); |
| 100 } | 100 } |
| 101 if (speech_buffer_.size() < full_frame_samples_) { | 101 if (speech_buffer_.size() < full_frame_samples_) { |
| 102 return EncodedInfo(); | 102 return EncodedInfo(); |
| 103 } | 103 } |
| 104 CHECK_EQ(speech_buffer_.size(), full_frame_samples_); | 104 RTC_CHECK_EQ(speech_buffer_.size(), full_frame_samples_); |
| 105 CHECK_GE(max_encoded_bytes, full_frame_samples_); | 105 RTC_CHECK_GE(max_encoded_bytes, full_frame_samples_); |
| 106 EncodedInfo info; | 106 EncodedInfo info; |
| 107 info.encoded_timestamp = first_timestamp_in_buffer_; | 107 info.encoded_timestamp = first_timestamp_in_buffer_; |
| 108 info.payload_type = payload_type_; | 108 info.payload_type = payload_type_; |
| 109 info.encoded_bytes = | 109 info.encoded_bytes = |
| 110 EncodeCall(&speech_buffer_[0], full_frame_samples_, encoded); | 110 EncodeCall(&speech_buffer_[0], full_frame_samples_, encoded); |
| 111 speech_buffer_.clear(); | 111 speech_buffer_.clear(); |
| 112 return info; | 112 return info; |
| 113 } | 113 } |
| 114 | 114 |
| 115 void AudioEncoderPcm::Reset() { | 115 void AudioEncoderPcm::Reset() { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 136 size_t input_len, | 136 size_t input_len, |
| 137 uint8_t* encoded) { | 137 uint8_t* encoded) { |
| 138 return WebRtcG711_EncodeU(audio, input_len, encoded); | 138 return WebRtcG711_EncodeU(audio, input_len, encoded); |
| 139 } | 139 } |
| 140 | 140 |
| 141 int AudioEncoderPcmU::BytesPerSample() const { | 141 int AudioEncoderPcmU::BytesPerSample() const { |
| 142 return 1; | 142 return 1; |
| 143 } | 143 } |
| 144 | 144 |
| 145 } // namespace webrtc | 145 } // namespace webrtc |
| OLD | NEW |