OLD | NEW |
(Empty) | |
| 1 /* |
| 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 |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "webrtc/modules/audio_coding/acm2/rent_a_codec.h" |
| 13 #include "webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.h" |
| 14 |
| 15 namespace webrtc { |
| 16 |
| 17 using NetEqDecoder = acm2::RentACodec::NetEqDecoder; |
| 18 |
| 19 class SplitBySamplesTest : public ::testing::TestWithParam<NetEqDecoder> { |
| 20 protected: |
| 21 virtual void SetUp() { |
| 22 decoder_type_ = GetParam(); |
| 23 switch (decoder_type_) { |
| 24 case NetEqDecoder::kDecoderPCMu: |
| 25 case NetEqDecoder::kDecoderPCMa: |
| 26 bytes_per_ms_ = 8; |
| 27 samples_per_ms_ = 8; |
| 28 break; |
| 29 case NetEqDecoder::kDecoderPCMu_2ch: |
| 30 case NetEqDecoder::kDecoderPCMa_2ch: |
| 31 bytes_per_ms_ = 2 * 8; |
| 32 samples_per_ms_ = 8; |
| 33 break; |
| 34 case NetEqDecoder::kDecoderG722: |
| 35 bytes_per_ms_ = 8; |
| 36 samples_per_ms_ = 16; |
| 37 break; |
| 38 case NetEqDecoder::kDecoderPCM16B: |
| 39 bytes_per_ms_ = 16; |
| 40 samples_per_ms_ = 8; |
| 41 break; |
| 42 case NetEqDecoder::kDecoderPCM16Bwb: |
| 43 bytes_per_ms_ = 32; |
| 44 samples_per_ms_ = 16; |
| 45 break; |
| 46 case NetEqDecoder::kDecoderPCM16Bswb32kHz: |
| 47 bytes_per_ms_ = 64; |
| 48 samples_per_ms_ = 32; |
| 49 break; |
| 50 case NetEqDecoder::kDecoderPCM16Bswb48kHz: |
| 51 bytes_per_ms_ = 96; |
| 52 samples_per_ms_ = 48; |
| 53 break; |
| 54 case NetEqDecoder::kDecoderPCM16B_2ch: |
| 55 bytes_per_ms_ = 2 * 16; |
| 56 samples_per_ms_ = 8; |
| 57 break; |
| 58 case NetEqDecoder::kDecoderPCM16Bwb_2ch: |
| 59 bytes_per_ms_ = 2 * 32; |
| 60 samples_per_ms_ = 16; |
| 61 break; |
| 62 case NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch: |
| 63 bytes_per_ms_ = 2 * 64; |
| 64 samples_per_ms_ = 32; |
| 65 break; |
| 66 case NetEqDecoder::kDecoderPCM16Bswb48kHz_2ch: |
| 67 bytes_per_ms_ = 2 * 96; |
| 68 samples_per_ms_ = 48; |
| 69 break; |
| 70 case NetEqDecoder::kDecoderPCM16B_5ch: |
| 71 bytes_per_ms_ = 5 * 16; |
| 72 samples_per_ms_ = 8; |
| 73 break; |
| 74 default: |
| 75 assert(false); |
| 76 break; |
| 77 } |
| 78 } |
| 79 size_t bytes_per_ms_; |
| 80 int samples_per_ms_; |
| 81 NetEqDecoder decoder_type_; |
| 82 }; |
| 83 |
| 84 // Test splitting sample-based payloads. |
| 85 TEST_P(SplitBySamplesTest, PayloadSizes) { |
| 86 constexpr uint32_t kBaseTimestamp = 0x12345678; |
| 87 struct ExpectedSplit { |
| 88 size_t payload_size_ms; |
| 89 size_t num_frames; |
| 90 // For simplicity. We only expect up to two packets per split. |
| 91 size_t frame_sizes[2]; |
| 92 }; |
| 93 // The payloads are expected to be split as follows: |
| 94 // 10 ms -> 10 ms |
| 95 // 20 ms -> 20 ms |
| 96 // 30 ms -> 30 ms |
| 97 // 40 ms -> 20 + 20 ms |
| 98 // 50 ms -> 25 + 25 ms |
| 99 // 60 ms -> 30 + 30 ms |
| 100 ExpectedSplit expected_splits[] = { |
| 101 {10, 1, {10}}, |
| 102 {20, 1, {20}}, |
| 103 {30, 1, {30}}, |
| 104 {40, 2, {20, 20}}, |
| 105 {50, 2, {25, 25}}, |
| 106 {60, 2, {30, 30}} |
| 107 }; |
| 108 |
| 109 for (const auto& expected_split : expected_splits) { |
| 110 // The payload values are set to steadily increase (modulo 256), so that the |
| 111 // resulting frames can be checked and we can be reasonably certain no |
| 112 // sample was missed or repeated. |
| 113 const auto generate_payload = [] (size_t num_bytes) { |
| 114 rtc::Buffer payload(num_bytes); |
| 115 uint8_t value = 0; |
| 116 // Allow wrap-around of value in counter below. |
| 117 for (size_t i = 0; i != payload.size(); ++i, ++value) { |
| 118 payload[i] = value; |
| 119 } |
| 120 return payload; |
| 121 }; |
| 122 |
| 123 const auto results = LegacyEncodedAudioFrame::SplitBySamples( |
| 124 nullptr, |
| 125 generate_payload(expected_split.payload_size_ms * bytes_per_ms_), |
| 126 kBaseTimestamp, true, bytes_per_ms_, samples_per_ms_); |
| 127 |
| 128 EXPECT_EQ(expected_split.num_frames, results.size()); |
| 129 uint32_t expected_timestamp = kBaseTimestamp; |
| 130 uint32_t expected_byte_offset = 0; |
| 131 uint8_t value = 0; |
| 132 for (size_t i = 0; i != expected_split.num_frames; ++i) { |
| 133 const auto& result = results[i]; |
| 134 const LegacyEncodedAudioFrame* frame = |
| 135 static_cast<const LegacyEncodedAudioFrame*>(result.frame.get()); |
| 136 const size_t length_bytes = expected_split.frame_sizes[i] * bytes_per_ms_; |
| 137 EXPECT_EQ(length_bytes, frame->payload().size()); |
| 138 EXPECT_EQ(expected_timestamp, result.timestamp); |
| 139 const rtc::Buffer& payload = frame->payload(); |
| 140 // Allow wrap-around of value in counter below. |
| 141 for (size_t i = 0; i != payload.size(); ++i, ++value) { |
| 142 ASSERT_EQ(value, payload[i]); |
| 143 } |
| 144 |
| 145 expected_timestamp += expected_split.frame_sizes[i] * samples_per_ms_; |
| 146 expected_byte_offset += length_bytes; |
| 147 } |
| 148 } |
| 149 } |
| 150 |
| 151 INSTANTIATE_TEST_CASE_P( |
| 152 LegacyEncodedAudioFrame, |
| 153 SplitBySamplesTest, |
| 154 ::testing::Values(NetEqDecoder::kDecoderPCMu, |
| 155 NetEqDecoder::kDecoderPCMa, |
| 156 NetEqDecoder::kDecoderPCMu_2ch, |
| 157 NetEqDecoder::kDecoderPCMa_2ch, |
| 158 NetEqDecoder::kDecoderG722, |
| 159 NetEqDecoder::kDecoderPCM16B, |
| 160 NetEqDecoder::kDecoderPCM16Bwb, |
| 161 NetEqDecoder::kDecoderPCM16Bswb32kHz, |
| 162 NetEqDecoder::kDecoderPCM16Bswb48kHz, |
| 163 NetEqDecoder::kDecoderPCM16B_2ch, |
| 164 NetEqDecoder::kDecoderPCM16Bwb_2ch, |
| 165 NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch, |
| 166 NetEqDecoder::kDecoderPCM16Bswb48kHz_2ch, |
| 167 NetEqDecoder::kDecoderPCM16B_5ch)); |
| 168 |
| 169 } // namespace webrtc |
OLD | NEW |