OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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/ilbc/audio_decoder_ilbc.h" | 11 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_decoder_ilbc.h" |
12 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h" | 12 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h" |
13 #include "webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.h" | 13 #include "webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.h" |
14 #include "webrtc/test/gtest.h" | 14 #include "webrtc/test/gtest.h" |
15 | 15 |
16 namespace webrtc { | 16 namespace webrtc { |
17 | 17 |
18 TEST(IlbcTest, BadPacket) { | 18 TEST(IlbcTest, BadPacket) { |
19 // Get a good packet. | 19 // Get a good packet. |
20 AudioEncoderIlbc::Config config; | 20 AudioEncoderIlbcConfig config; |
21 config.frame_size_ms = 20; // We need 20 ms rather than the default 30 ms; | 21 config.frame_size_ms = 20; // We need 20 ms rather than the default 30 ms; |
22 // otherwise, all possible values of cb_index[2] | 22 // otherwise, all possible values of cb_index[2] |
23 // are valid. | 23 // are valid. |
24 AudioEncoderIlbc encoder(config); | 24 AudioEncoderIlbcImpl encoder(config, 102); |
25 std::vector<int16_t> samples(encoder.SampleRateHz() / 100, 4711); | 25 std::vector<int16_t> samples(encoder.SampleRateHz() / 100, 4711); |
26 rtc::Buffer packet; | 26 rtc::Buffer packet; |
27 int num_10ms_chunks = 0; | 27 int num_10ms_chunks = 0; |
28 while (packet.size() == 0) { | 28 while (packet.size() == 0) { |
29 encoder.Encode(0, samples, &packet); | 29 encoder.Encode(0, samples, &packet); |
30 num_10ms_chunks += 1; | 30 num_10ms_chunks += 1; |
31 } | 31 } |
32 | 32 |
33 // Break the packet by setting all bits of the unsigned 7-bit number | 33 // Break the packet by setting all bits of the unsigned 7-bit number |
34 // cb_index[2] to 1, giving it a value of 127. For a 20 ms packet, this is | 34 // cb_index[2] to 1, giving it a value of 127. For a 20 ms packet, this is |
35 // too large. | 35 // too large. |
36 EXPECT_EQ(38u, packet.size()); | 36 EXPECT_EQ(38u, packet.size()); |
37 rtc::Buffer bad_packet(packet.data(), packet.size()); | 37 rtc::Buffer bad_packet(packet.data(), packet.size()); |
38 bad_packet[29] |= 0x3f; // Bits 1-6. | 38 bad_packet[29] |= 0x3f; // Bits 1-6. |
39 bad_packet[30] |= 0x80; // Bit 0. | 39 bad_packet[30] |= 0x80; // Bit 0. |
40 | 40 |
41 // Decode the bad packet. We expect the decoder to respond by returning -1. | 41 // Decode the bad packet. We expect the decoder to respond by returning -1. |
42 AudioDecoderIlbc decoder; | 42 AudioDecoderIlbcImpl decoder; |
43 std::vector<int16_t> decoded_samples(num_10ms_chunks * samples.size()); | 43 std::vector<int16_t> decoded_samples(num_10ms_chunks * samples.size()); |
44 AudioDecoder::SpeechType speech_type; | 44 AudioDecoder::SpeechType speech_type; |
45 EXPECT_EQ(-1, decoder.Decode(bad_packet.data(), bad_packet.size(), | 45 EXPECT_EQ(-1, decoder.Decode(bad_packet.data(), bad_packet.size(), |
46 encoder.SampleRateHz(), | 46 encoder.SampleRateHz(), |
47 sizeof(int16_t) * decoded_samples.size(), | 47 sizeof(int16_t) * decoded_samples.size(), |
48 decoded_samples.data(), &speech_type)); | 48 decoded_samples.data(), &speech_type)); |
49 | 49 |
50 // Decode the good packet. This should work, because the failed decoding | 50 // Decode the good packet. This should work, because the failed decoding |
51 // should not have left the decoder in a broken state. | 51 // should not have left the decoder in a broken state. |
52 EXPECT_EQ(static_cast<int>(decoded_samples.size()), | 52 EXPECT_EQ(static_cast<int>(decoded_samples.size()), |
53 decoder.Decode(packet.data(), packet.size(), encoder.SampleRateHz(), | 53 decoder.Decode(packet.data(), packet.size(), encoder.SampleRateHz(), |
54 sizeof(int16_t) * decoded_samples.size(), | 54 sizeof(int16_t) * decoded_samples.size(), |
55 decoded_samples.data(), &speech_type)); | 55 decoded_samples.data(), &speech_type)); |
56 } | 56 } |
57 | 57 |
58 class SplitIlbcTest : public ::testing::TestWithParam<std::pair<int, int> > { | 58 class SplitIlbcTest : public ::testing::TestWithParam<std::pair<int, int> > { |
59 protected: | 59 protected: |
60 virtual void SetUp() { | 60 virtual void SetUp() { |
61 const std::pair<int, int> parameters = GetParam(); | 61 const std::pair<int, int> parameters = GetParam(); |
62 num_frames_ = parameters.first; | 62 num_frames_ = parameters.first; |
63 frame_length_ms_ = parameters.second; | 63 frame_length_ms_ = parameters.second; |
64 frame_length_bytes_ = (frame_length_ms_ == 20) ? 38 : 50; | 64 frame_length_bytes_ = (frame_length_ms_ == 20) ? 38 : 50; |
65 } | 65 } |
66 size_t num_frames_; | 66 size_t num_frames_; |
67 int frame_length_ms_; | 67 int frame_length_ms_; |
68 size_t frame_length_bytes_; | 68 size_t frame_length_bytes_; |
69 }; | 69 }; |
70 | 70 |
71 TEST_P(SplitIlbcTest, NumFrames) { | 71 TEST_P(SplitIlbcTest, NumFrames) { |
72 AudioDecoderIlbc decoder; | 72 AudioDecoderIlbcImpl decoder; |
73 const size_t frame_length_samples = frame_length_ms_ * 8; | 73 const size_t frame_length_samples = frame_length_ms_ * 8; |
74 const auto generate_payload = [] (size_t payload_length_bytes) { | 74 const auto generate_payload = [] (size_t payload_length_bytes) { |
75 rtc::Buffer payload(payload_length_bytes); | 75 rtc::Buffer payload(payload_length_bytes); |
76 // Fill payload with increasing integers {0, 1, 2, ...}. | 76 // Fill payload with increasing integers {0, 1, 2, ...}. |
77 for (size_t i = 0; i < payload.size(); ++i) { | 77 for (size_t i = 0; i < payload.size(); ++i) { |
78 payload[i] = static_cast<uint8_t>(i); | 78 payload[i] = static_cast<uint8_t>(i); |
79 } | 79 } |
80 return payload; | 80 return payload; |
81 }; | 81 }; |
82 | 82 |
(...skipping 30 matching lines...) Expand all Loading... |
113 std::pair<int, int>(24, 20), | 113 std::pair<int, int>(24, 20), |
114 std::pair<int, int>(1, 30), | 114 std::pair<int, int>(1, 30), |
115 std::pair<int, int>(2, 30), | 115 std::pair<int, int>(2, 30), |
116 std::pair<int, int>(3, 30), | 116 std::pair<int, int>(3, 30), |
117 std::pair<int, int>(4, 30), | 117 std::pair<int, int>(4, 30), |
118 std::pair<int, int>(5, 30), | 118 std::pair<int, int>(5, 30), |
119 std::pair<int, int>(18, 30))); | 119 std::pair<int, int>(18, 30))); |
120 | 120 |
121 // Test too large payload size. | 121 // Test too large payload size. |
122 TEST(IlbcTest, SplitTooLargePayload) { | 122 TEST(IlbcTest, SplitTooLargePayload) { |
123 AudioDecoderIlbc decoder; | 123 AudioDecoderIlbcImpl decoder; |
124 constexpr size_t kPayloadLengthBytes = 950; | 124 constexpr size_t kPayloadLengthBytes = 950; |
125 const auto results = | 125 const auto results = |
126 decoder.ParsePayload(rtc::Buffer(kPayloadLengthBytes), 0); | 126 decoder.ParsePayload(rtc::Buffer(kPayloadLengthBytes), 0); |
127 EXPECT_TRUE(results.empty()); | 127 EXPECT_TRUE(results.empty()); |
128 } | 128 } |
129 | 129 |
130 // Payload not an integer number of frames. | 130 // Payload not an integer number of frames. |
131 TEST(IlbcTest, SplitUnevenPayload) { | 131 TEST(IlbcTest, SplitUnevenPayload) { |
132 AudioDecoderIlbc decoder; | 132 AudioDecoderIlbcImpl decoder; |
133 constexpr size_t kPayloadLengthBytes = 39; // Not an even number of frames. | 133 constexpr size_t kPayloadLengthBytes = 39; // Not an even number of frames. |
134 const auto results = | 134 const auto results = |
135 decoder.ParsePayload(rtc::Buffer(kPayloadLengthBytes), 0); | 135 decoder.ParsePayload(rtc::Buffer(kPayloadLengthBytes), 0); |
136 EXPECT_TRUE(results.empty()); | 136 EXPECT_TRUE(results.empty()); |
137 } | 137 } |
138 | 138 |
139 } // namespace webrtc | 139 } // namespace webrtc |
OLD | NEW |