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/cng/audio_encoder_cng.h" | 11 #include "webrtc/modules/audio_coding/codecs/cng/audio_encoder_cng.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <memory> | 14 #include <memory> |
15 #include <limits> | 15 #include <limits> |
| 16 #include <utility> |
16 | 17 |
17 namespace webrtc { | 18 namespace webrtc { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const int kMaxFrameSizeMs = 60; | 22 const int kMaxFrameSizeMs = 60; |
22 | 23 |
23 std::unique_ptr<CNG_enc_inst, CngInstDeleter> CreateCngInst( | 24 std::unique_ptr<CNG_enc_inst, CngInstDeleter> CreateCngInst( |
24 int sample_rate_hz, | 25 int sample_rate_hz, |
25 int sid_frame_interval_ms, | 26 int sid_frame_interval_ms, |
26 int num_cng_coefficients) { | 27 int num_cng_coefficients) { |
27 CNG_enc_inst* ci; | 28 CNG_enc_inst* ci; |
28 RTC_CHECK_EQ(0, WebRtcCng_CreateEnc(&ci)); | 29 RTC_CHECK_EQ(0, WebRtcCng_CreateEnc(&ci)); |
29 std::unique_ptr<CNG_enc_inst, CngInstDeleter> cng_inst(ci); | 30 std::unique_ptr<CNG_enc_inst, CngInstDeleter> cng_inst(ci); |
30 RTC_CHECK_EQ(0, | 31 RTC_CHECK_EQ(0, |
31 WebRtcCng_InitEnc(cng_inst.get(), sample_rate_hz, | 32 WebRtcCng_InitEnc(cng_inst.get(), sample_rate_hz, |
32 sid_frame_interval_ms, num_cng_coefficients)); | 33 sid_frame_interval_ms, num_cng_coefficients)); |
33 return cng_inst; | 34 return cng_inst; |
34 } | 35 } |
35 | 36 |
36 } // namespace | 37 } // namespace |
37 | 38 |
| 39 AudioEncoderCng::Config::Config() = default; |
| 40 |
| 41 // TODO(kwiberg): =default this when Visual Studio learns to handle it. |
| 42 AudioEncoderCng::Config::Config(Config&& c) |
| 43 : num_channels(c.num_channels), |
| 44 payload_type(c.payload_type), |
| 45 speech_encoder(std::move(c.speech_encoder)), |
| 46 vad_mode(c.vad_mode), |
| 47 sid_frame_interval_ms(c.sid_frame_interval_ms), |
| 48 num_cng_coefficients(c.num_cng_coefficients), |
| 49 vad(c.vad) {} |
| 50 |
| 51 AudioEncoderCng::Config::~Config() = default; |
| 52 |
38 bool AudioEncoderCng::Config::IsOk() const { | 53 bool AudioEncoderCng::Config::IsOk() const { |
39 if (num_channels != 1) | 54 if (num_channels != 1) |
40 return false; | 55 return false; |
41 if (!speech_encoder) | 56 if (!speech_encoder) |
42 return false; | 57 return false; |
43 if (num_channels != speech_encoder->NumChannels()) | 58 if (num_channels != speech_encoder->NumChannels()) |
44 return false; | 59 return false; |
45 if (sid_frame_interval_ms < | 60 if (sid_frame_interval_ms < |
46 static_cast<int>(speech_encoder->Max10MsFramesInAPacket() * 10)) | 61 static_cast<int>(speech_encoder->Max10MsFramesInAPacket() * 10)) |
47 return false; | 62 return false; |
48 if (num_cng_coefficients > WEBRTC_CNG_MAX_LPC_ORDER || | 63 if (num_cng_coefficients > WEBRTC_CNG_MAX_LPC_ORDER || |
49 num_cng_coefficients <= 0) | 64 num_cng_coefficients <= 0) |
50 return false; | 65 return false; |
51 return true; | 66 return true; |
52 } | 67 } |
53 | 68 |
54 AudioEncoderCng::AudioEncoderCng(const Config& config) | 69 AudioEncoderCng::AudioEncoderCng(Config&& config) |
55 : speech_encoder_(config.speech_encoder), | 70 : speech_encoder_( |
| 71 ([&] { RTC_CHECK(config.IsOk()) << "Invalid configuration."; }(), |
| 72 std::move(config.speech_encoder))), |
56 cng_payload_type_(config.payload_type), | 73 cng_payload_type_(config.payload_type), |
57 num_cng_coefficients_(config.num_cng_coefficients), | 74 num_cng_coefficients_(config.num_cng_coefficients), |
58 sid_frame_interval_ms_(config.sid_frame_interval_ms), | 75 sid_frame_interval_ms_(config.sid_frame_interval_ms), |
59 last_frame_active_(true), | 76 last_frame_active_(true), |
60 vad_(config.vad ? std::unique_ptr<Vad>(config.vad) | 77 vad_(config.vad ? std::unique_ptr<Vad>(config.vad) |
61 : CreateVad(config.vad_mode)) { | 78 : CreateVad(config.vad_mode)) { |
62 RTC_CHECK(config.IsOk()) << "Invalid configuration."; | |
63 cng_inst_ = CreateCngInst(SampleRateHz(), sid_frame_interval_ms_, | 79 cng_inst_ = CreateCngInst(SampleRateHz(), sid_frame_interval_ms_, |
64 num_cng_coefficients_); | 80 num_cng_coefficients_); |
65 } | 81 } |
66 | 82 |
67 AudioEncoderCng::~AudioEncoderCng() = default; | 83 AudioEncoderCng::~AudioEncoderCng() = default; |
68 | 84 |
69 size_t AudioEncoderCng::MaxEncodedBytes() const { | 85 size_t AudioEncoderCng::MaxEncodedBytes() const { |
70 const size_t max_encoded_bytes_active = speech_encoder_->MaxEncodedBytes(); | 86 const size_t max_encoded_bytes_active = speech_encoder_->MaxEncodedBytes(); |
71 const size_t max_encoded_bytes_passive = | 87 const size_t max_encoded_bytes_passive = |
72 rtc::CheckedDivExact(kMaxFrameSizeMs, 10) * SamplesPer10msFrame(); | 88 rtc::CheckedDivExact(kMaxFrameSizeMs, 10) * SamplesPer10msFrame(); |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 } | 275 } |
260 } | 276 } |
261 return info; | 277 return info; |
262 } | 278 } |
263 | 279 |
264 size_t AudioEncoderCng::SamplesPer10msFrame() const { | 280 size_t AudioEncoderCng::SamplesPer10msFrame() const { |
265 return rtc::CheckedDivExact(10 * SampleRateHz(), 1000); | 281 return rtc::CheckedDivExact(10 * SampleRateHz(), 1000); |
266 } | 282 } |
267 | 283 |
268 } // namespace webrtc | 284 } // namespace webrtc |
OLD | NEW |