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