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/audio_encoder_pcm.h" | 11 #include "webrtc/modules/audio_coding/codecs/g711/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/g711_interface.h" | 17 #include "webrtc/modules/audio_coding/codecs/g711/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, | |
24 int frame_size_ms, | |
25 int sample_rate_hz) { | |
26 int samples_per_frame = num_channels * frame_size_ms * sample_rate_hz / 1000; | |
27 RTC_CHECK_LE(samples_per_frame, std::numeric_limits<int16_t>::max()) | |
28 << "Frame size too large."; | |
29 return static_cast<int16_t>(samples_per_frame); | |
30 } | |
31 | |
32 template <typename T> | 23 template <typename T> |
33 typename T::Config CreateConfig(const CodecInst& codec_inst) { | 24 typename T::Config CreateConfig(const CodecInst& codec_inst) { |
34 typename T::Config config; | 25 typename T::Config config; |
35 config.frame_size_ms = codec_inst.pacsize / 8; | 26 config.frame_size_ms = codec_inst.pacsize / 8; |
36 config.num_channels = codec_inst.channels; | 27 config.num_channels = codec_inst.channels; |
37 config.payload_type = codec_inst.pltype; | 28 config.payload_type = codec_inst.pltype; |
38 return config; | 29 return config; |
39 } | 30 } |
40 | 31 |
41 } // namespace | 32 } // namespace |
42 | 33 |
43 bool AudioEncoderPcm::Config::IsOk() const { | 34 bool AudioEncoderPcm::Config::IsOk() const { |
44 return (frame_size_ms % 10 == 0) && (num_channels >= 1); | 35 return (frame_size_ms % 10 == 0) && (num_channels >= 1); |
45 } | 36 } |
46 | 37 |
47 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) | 38 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) |
48 : sample_rate_hz_(sample_rate_hz), | 39 : sample_rate_hz_(sample_rate_hz), |
49 num_channels_(config.num_channels), | 40 num_channels_(config.num_channels), |
50 payload_type_(config.payload_type), | 41 payload_type_(config.payload_type), |
51 num_10ms_frames_per_packet_( | 42 num_10ms_frames_per_packet_( |
52 static_cast<size_t>(config.frame_size_ms / 10)), | 43 static_cast<size_t>(config.frame_size_ms / 10)), |
53 full_frame_samples_(NumSamplesPerFrame(config.num_channels, | 44 full_frame_samples_( |
54 config.frame_size_ms, | 45 config.num_channels * config.frame_size_ms * sample_rate_hz / 1000), |
55 sample_rate_hz_)), | |
56 first_timestamp_in_buffer_(0) { | 46 first_timestamp_in_buffer_(0) { |
57 RTC_CHECK_GT(sample_rate_hz, 0) << "Sample rate must be larger than 0 Hz"; | 47 RTC_CHECK_GT(sample_rate_hz, 0) << "Sample rate must be larger than 0 Hz"; |
58 RTC_CHECK_EQ(config.frame_size_ms % 10, 0) | 48 RTC_CHECK_EQ(config.frame_size_ms % 10, 0) |
59 << "Frame size must be an integer multiple of 10 ms."; | 49 << "Frame size must be an integer multiple of 10 ms."; |
60 speech_buffer_.reserve(full_frame_samples_); | 50 speech_buffer_.reserve(full_frame_samples_); |
61 } | 51 } |
62 | 52 |
63 AudioEncoderPcm::~AudioEncoderPcm() = default; | 53 AudioEncoderPcm::~AudioEncoderPcm() = default; |
64 | 54 |
65 size_t AudioEncoderPcm::MaxEncodedBytes() const { | 55 size_t AudioEncoderPcm::MaxEncodedBytes() const { |
66 return full_frame_samples_ * BytesPerSample(); | 56 return full_frame_samples_ * BytesPerSample(); |
67 } | 57 } |
68 | 58 |
69 int AudioEncoderPcm::SampleRateHz() const { | 59 int AudioEncoderPcm::SampleRateHz() const { |
70 return sample_rate_hz_; | 60 return sample_rate_hz_; |
71 } | 61 } |
72 | 62 |
73 int AudioEncoderPcm::NumChannels() const { | 63 size_t AudioEncoderPcm::NumChannels() const { |
74 return num_channels_; | 64 return num_channels_; |
75 } | 65 } |
76 | 66 |
77 size_t AudioEncoderPcm::Num10MsFramesInNextPacket() const { | 67 size_t AudioEncoderPcm::Num10MsFramesInNextPacket() const { |
78 return num_10ms_frames_per_packet_; | 68 return num_10ms_frames_per_packet_; |
79 } | 69 } |
80 | 70 |
81 size_t AudioEncoderPcm::Max10MsFramesInAPacket() const { | 71 size_t AudioEncoderPcm::Max10MsFramesInAPacket() const { |
82 return num_10ms_frames_per_packet_; | 72 return num_10ms_frames_per_packet_; |
83 } | 73 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 size_t input_len, | 124 size_t input_len, |
135 uint8_t* encoded) { | 125 uint8_t* encoded) { |
136 return WebRtcG711_EncodeU(audio, input_len, encoded); | 126 return WebRtcG711_EncodeU(audio, input_len, encoded); |
137 } | 127 } |
138 | 128 |
139 size_t AudioEncoderPcmU::BytesPerSample() const { | 129 size_t AudioEncoderPcmU::BytesPerSample() const { |
140 return 1; | 130 return 1; |
141 } | 131 } |
142 | 132 |
143 } // namespace webrtc | 133 } // namespace webrtc |
OLD | NEW |