blob: 5ec1219b3b58fb061aff2105f2ca1109dcd5d987 [file] [log] [blame]
kwiberg619a2112016-08-24 02:46:44 -07001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/codecs/ilbc/audio_decoder_ilbc.h"
12#include "modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h"
13#include "modules/audio_coding/codecs/legacy_encoded_audio_frame.h"
14#include "test/gtest.h"
kwiberg619a2112016-08-24 02:46:44 -070015
16namespace webrtc {
17
18TEST(IlbcTest, BadPacket) {
19 // Get a good packet.
solenbergdb3c9b02017-06-28 02:05:04 -070020 AudioEncoderIlbcConfig config;
kwiberg619a2112016-08-24 02:46:44 -070021 config.frame_size_ms = 20; // We need 20 ms rather than the default 30 ms;
22 // otherwise, all possible values of cb_index[2]
23 // are valid.
solenbergdb3c9b02017-06-28 02:05:04 -070024 AudioEncoderIlbcImpl encoder(config, 102);
kwiberg619a2112016-08-24 02:46:44 -070025 std::vector<int16_t> samples(encoder.SampleRateHz() / 100, 4711);
26 rtc::Buffer packet;
27 int num_10ms_chunks = 0;
28 while (packet.size() == 0) {
29 encoder.Encode(0, samples, &packet);
30 num_10ms_chunks += 1;
31 }
32
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
35 // too large.
36 EXPECT_EQ(38u, packet.size());
37 rtc::Buffer bad_packet(packet.data(), packet.size());
38 bad_packet[29] |= 0x3f; // Bits 1-6.
39 bad_packet[30] |= 0x80; // Bit 0.
40
41 // Decode the bad packet. We expect the decoder to respond by returning -1.
solenbergdb3c9b02017-06-28 02:05:04 -070042 AudioDecoderIlbcImpl decoder;
kwiberg619a2112016-08-24 02:46:44 -070043 std::vector<int16_t> decoded_samples(num_10ms_chunks * samples.size());
44 AudioDecoder::SpeechType speech_type;
45 EXPECT_EQ(-1, decoder.Decode(bad_packet.data(), bad_packet.size(),
46 encoder.SampleRateHz(),
47 sizeof(int16_t) * decoded_samples.size(),
48 decoded_samples.data(), &speech_type));
49
50 // Decode the good packet. This should work, because the failed decoding
51 // should not have left the decoder in a broken state.
52 EXPECT_EQ(static_cast<int>(decoded_samples.size()),
53 decoder.Decode(packet.data(), packet.size(), encoder.SampleRateHz(),
54 sizeof(int16_t) * decoded_samples.size(),
55 decoded_samples.data(), &speech_type));
56}
57
ossu0d526d52016-09-21 01:57:31 -070058class SplitIlbcTest : public ::testing::TestWithParam<std::pair<int, int> > {
59 protected:
60 virtual void SetUp() {
61 const std::pair<int, int> parameters = GetParam();
62 num_frames_ = parameters.first;
63 frame_length_ms_ = parameters.second;
64 frame_length_bytes_ = (frame_length_ms_ == 20) ? 38 : 50;
65 }
66 size_t num_frames_;
67 int frame_length_ms_;
68 size_t frame_length_bytes_;
69};
70
71TEST_P(SplitIlbcTest, NumFrames) {
solenbergdb3c9b02017-06-28 02:05:04 -070072 AudioDecoderIlbcImpl decoder;
ossu0d526d52016-09-21 01:57:31 -070073 const size_t frame_length_samples = frame_length_ms_ * 8;
Yves Gerey665174f2018-06-19 15:03:05 +020074 const auto generate_payload = [](size_t payload_length_bytes) {
ossu0d526d52016-09-21 01:57:31 -070075 rtc::Buffer payload(payload_length_bytes);
76 // Fill payload with increasing integers {0, 1, 2, ...}.
77 for (size_t i = 0; i < payload.size(); ++i) {
78 payload[i] = static_cast<uint8_t>(i);
79 }
80 return payload;
81 };
82
83 const auto results = decoder.ParsePayload(
ossua70695a2016-09-22 02:06:28 -070084 generate_payload(frame_length_bytes_ * num_frames_), 0);
ossu0d526d52016-09-21 01:57:31 -070085 EXPECT_EQ(num_frames_, results.size());
86
87 size_t frame_num = 0;
88 uint8_t payload_value = 0;
89 for (const auto& result : results) {
90 EXPECT_EQ(frame_length_samples * frame_num, result.timestamp);
91 const LegacyEncodedAudioFrame* frame =
92 static_cast<const LegacyEncodedAudioFrame*>(result.frame.get());
93 const rtc::Buffer& payload = frame->payload();
94 EXPECT_EQ(frame_length_bytes_, payload.size());
95 for (size_t i = 0; i < payload.size(); ++i, ++payload_value) {
96 EXPECT_EQ(payload_value, payload[i]);
97 }
98 ++frame_num;
99 }
100}
101
102// Test 1 through 5 frames of 20 and 30 ms size.
103// Also test the maximum number of frames in one packet for 20 and 30 ms.
104// The maximum is defined by the largest payload length that can be uniquely
105// resolved to a frame size of either 38 bytes (20 ms) or 50 bytes (30 ms).
106INSTANTIATE_TEST_CASE_P(
Yves Gerey665174f2018-06-19 15:03:05 +0200107 IlbcTest,
108 SplitIlbcTest,
ossu0d526d52016-09-21 01:57:31 -0700109 ::testing::Values(std::pair<int, int>(1, 20), // 1 frame, 20 ms.
110 std::pair<int, int>(2, 20), // 2 frames, 20 ms.
111 std::pair<int, int>(3, 20), // And so on.
112 std::pair<int, int>(4, 20),
113 std::pair<int, int>(5, 20),
114 std::pair<int, int>(24, 20),
115 std::pair<int, int>(1, 30),
116 std::pair<int, int>(2, 30),
117 std::pair<int, int>(3, 30),
118 std::pair<int, int>(4, 30),
119 std::pair<int, int>(5, 30),
120 std::pair<int, int>(18, 30)));
121
122// Test too large payload size.
123TEST(IlbcTest, SplitTooLargePayload) {
solenbergdb3c9b02017-06-28 02:05:04 -0700124 AudioDecoderIlbcImpl decoder;
ossu0d526d52016-09-21 01:57:31 -0700125 constexpr size_t kPayloadLengthBytes = 950;
126 const auto results =
ossua70695a2016-09-22 02:06:28 -0700127 decoder.ParsePayload(rtc::Buffer(kPayloadLengthBytes), 0);
ossu0d526d52016-09-21 01:57:31 -0700128 EXPECT_TRUE(results.empty());
129}
130
131// Payload not an integer number of frames.
132TEST(IlbcTest, SplitUnevenPayload) {
solenbergdb3c9b02017-06-28 02:05:04 -0700133 AudioDecoderIlbcImpl decoder;
ossu0d526d52016-09-21 01:57:31 -0700134 constexpr size_t kPayloadLengthBytes = 39; // Not an even number of frames.
135 const auto results =
ossua70695a2016-09-22 02:06:28 -0700136 decoder.ParsePayload(rtc::Buffer(kPayloadLengthBytes), 0);
ossu0d526d52016-09-21 01:57:31 -0700137 EXPECT_TRUE(results.empty());
138}
139
kwiberg619a2112016-08-24 02:46:44 -0700140} // namespace webrtc