Chromium Code Reviews| Index: webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc |
| diff --git a/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc b/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc |
| index febea2f869fe7ca94897015ac2d35df7219233b7..e0fcdcc4b9c087a73ebc42b5eeadd72fcc9ee0fc 100644 |
| --- a/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc |
| +++ b/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc |
| @@ -40,6 +40,8 @@ class OpusTest : public TestWithParam<::testing::tuple<int, int>> { |
| void TestDtxEffect(bool dtx, int block_length_ms); |
| + void TestCbrEffect(bool dtx, int block_length_ms); |
| + |
| // Prepare |speech_data_| for encoding, read from a hard-coded file. |
| // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a |
| // block of |block_length_ms| milliseconds. The data is looped every |
| @@ -300,6 +302,54 @@ void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) { |
| EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
| } |
| +// Test if CBR does what we expect. |
| +void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) { |
|
minyue-webrtc
2017/03/30 20:36:17
it looks like that TestCbrEffect and TestDtxEffect
|
| + PrepareSpeechData(channels_, block_length_ms, 2000); |
| + const size_t samples = kOpusRateKhz * block_length_ms; |
| + |
| + int32_t max_pkt_size_diff = 0; |
| + int32_t prev_pkt_size = 0; |
| + |
| + // Create encoder memory. |
| + EXPECT_EQ(0, |
| + WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_)); |
| + EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_)); |
| + |
| + // Set bitrate. |
| + EXPECT_EQ( |
| + 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000)); |
| + |
| + // Setting CBR. |
| + EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_) |
| + : WebRtcOpus_DisableCbr(opus_encoder_)); |
| + |
| + int16_t audio_type; |
| + int16_t* output_data_decode = new int16_t[samples * channels_]; |
|
minyue-webrtc
2017/03/30 20:36:17
std::vector<int16_t> output_data_decode(samples *
|
| + |
| + for (int i = 0; i < 100; ++i) { |
| + EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode( |
| + opus_encoder_, speech_data_.GetNextBlock(), |
| + opus_decoder_, output_data_decode, &audio_type))); |
| + |
| + if (prev_pkt_size > 0) { |
| + int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size); |
| + max_pkt_size_diff = std::max(max_pkt_size_diff, diff); |
| + } |
| + prev_pkt_size = encoded_bytes_; |
| + } |
| + |
| + if (cbr) { |
| + EXPECT_EQ(max_pkt_size_diff, 0); |
| + } else { |
| + EXPECT_GT(max_pkt_size_diff, 0); |
| + } |
| + |
| + // Free memory. |
| + delete[] output_data_decode; |
| + EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| + EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
| +} |
| + |
| // Test failing Create. |
| TEST(OpusTest, OpusCreateFail) { |
| WebRtcOpusEncInst* opus_encoder; |
| @@ -525,6 +575,18 @@ TEST_P(OpusTest, OpusDtxOn) { |
| TestDtxEffect(true, 40); |
| } |
| +TEST_P(OpusTest, OpusCbrOff) { |
| + TestCbrEffect(false, 10); |
| + TestCbrEffect(false, 20); |
| + TestCbrEffect(false, 40); |
| +} |
| + |
| +TEST_P(OpusTest, OpusCbrOn) { |
| + TestCbrEffect(true, 10); |
| + TestCbrEffect(true, 20); |
| + TestCbrEffect(true, 40); |
| +} |
| + |
| TEST_P(OpusTest, OpusSetPacketLossRate) { |
| // Test without creating encoder memory. |
| EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50)); |