andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/acm2/acm_receiver.h" |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> // std::min |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 14 | #include <memory> |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 17 | #include "modules/audio_coding/acm2/rent_a_codec.h" |
| 18 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 19 | #include "modules/audio_coding/neteq/tools/rtp_generator.h" |
Fredrik Solenberg | bbf21a3 | 2018-04-12 22:44:09 +0200 | [diff] [blame] | 20 | #include "modules/include/module_common_types.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 22 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "system_wrappers/include/clock.h" |
| 24 | #include "test/gtest.h" |
| 25 | #include "test/testsupport/fileutils.h" |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | namespace acm2 { |
| 30 | namespace { |
| 31 | |
| 32 | bool CodecsEqual(const CodecInst& codec_a, const CodecInst& codec_b) { |
| 33 | if (strcmp(codec_a.plname, codec_b.plname) != 0 || |
| 34 | codec_a.plfreq != codec_b.plfreq || |
| 35 | codec_a.pltype != codec_b.pltype || |
| 36 | codec_b.channels != codec_a.channels) |
| 37 | return false; |
| 38 | return true; |
| 39 | } |
| 40 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 41 | struct CodecIdInst { |
| 42 | explicit CodecIdInst(RentACodec::CodecId codec_id) { |
| 43 | const auto codec_ix = RentACodec::CodecIndexFromId(codec_id); |
| 44 | EXPECT_TRUE(codec_ix); |
| 45 | id = *codec_ix; |
| 46 | const auto codec_inst = RentACodec::CodecInstById(codec_id); |
| 47 | EXPECT_TRUE(codec_inst); |
| 48 | inst = *codec_inst; |
| 49 | } |
| 50 | int id; |
| 51 | CodecInst inst; |
| 52 | }; |
| 53 | |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 54 | } // namespace |
| 55 | |
| 56 | class AcmReceiverTestOldApi : public AudioPacketizationCallback, |
| 57 | public ::testing::Test { |
| 58 | protected: |
| 59 | AcmReceiverTestOldApi() |
| 60 | : timestamp_(0), |
| 61 | packet_sent_(false), |
| 62 | last_packet_send_timestamp_(timestamp_), |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 63 | last_frame_type_(kEmptyFrame) { |
ossu | e352578 | 2016-05-25 07:37:43 -0700 | [diff] [blame] | 64 | config_.decoder_factory = CreateBuiltinAudioDecoderFactory(); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | ~AcmReceiverTestOldApi() {} |
| 68 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 69 | void SetUp() override { |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 70 | acm_.reset(AudioCodingModule::Create(config_)); |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 71 | receiver_.reset(new AcmReceiver(config_)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 72 | ASSERT_TRUE(receiver_.get() != NULL); |
| 73 | ASSERT_TRUE(acm_.get() != NULL); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 74 | codecs_ = RentACodec::Database(); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 75 | |
| 76 | acm_->InitializeReceiver(); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 77 | acm_->RegisterTransportCallback(this); |
| 78 | |
| 79 | rtp_header_.header.sequenceNumber = 0; |
| 80 | rtp_header_.header.timestamp = 0; |
| 81 | rtp_header_.header.markerBit = false; |
| 82 | rtp_header_.header.ssrc = 0x12345678; // Arbitrary. |
| 83 | rtp_header_.header.numCSRCs = 0; |
| 84 | rtp_header_.header.payloadType = 0; |
| 85 | rtp_header_.frameType = kAudioFrameSpeech; |
| 86 | rtp_header_.type.Audio.isCNG = false; |
| 87 | } |
| 88 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 89 | void TearDown() override {} |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 90 | |
| 91 | void InsertOnePacketOfSilence(int codec_id) { |
kwiberg | d6c0f8c | 2015-11-06 14:28:00 -0800 | [diff] [blame] | 92 | CodecInst codec = |
| 93 | *RentACodec::CodecInstById(*RentACodec::CodecIdFromIndex(codec_id)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 94 | if (timestamp_ == 0) { // This is the first time inserting audio. |
| 95 | ASSERT_EQ(0, acm_->RegisterSendCodec(codec)); |
| 96 | } else { |
kwiberg | 1fd4a4a | 2015-11-03 11:20:50 -0800 | [diff] [blame] | 97 | auto current_codec = acm_->SendCodec(); |
| 98 | ASSERT_TRUE(current_codec); |
| 99 | if (!CodecsEqual(codec, *current_codec)) |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 100 | ASSERT_EQ(0, acm_->RegisterSendCodec(codec)); |
| 101 | } |
| 102 | AudioFrame frame; |
| 103 | // Frame setup according to the codec. |
| 104 | frame.sample_rate_hz_ = codec.plfreq; |
| 105 | frame.samples_per_channel_ = codec.plfreq / 100; // 10 ms. |
| 106 | frame.num_channels_ = codec.channels; |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 107 | frame.Mute(); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 108 | packet_sent_ = false; |
| 109 | last_packet_send_timestamp_ = timestamp_; |
henrik.lundin@webrtc.org | f56c162 | 2015-03-02 12:29:30 +0000 | [diff] [blame] | 110 | while (!packet_sent_) { |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 111 | frame.timestamp_ = timestamp_; |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 112 | timestamp_ += rtc::checked_cast<uint32_t>(frame.samples_per_channel_); |
henrik.lundin@webrtc.org | f56c162 | 2015-03-02 12:29:30 +0000 | [diff] [blame] | 113 | ASSERT_GE(acm_->Add10MsData(frame), 0); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 114 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 115 | } |
| 116 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 117 | template <size_t N> |
| 118 | void AddSetOfCodecs(const RentACodec::CodecId(&ids)[N]) { |
| 119 | for (auto id : ids) { |
| 120 | const auto i = RentACodec::CodecIndexFromId(id); |
| 121 | ASSERT_TRUE(i); |
kwiberg | 6f0f616 | 2016-09-20 03:07:46 -0700 | [diff] [blame] | 122 | ASSERT_EQ(0, receiver_->AddCodec(*i, codecs_[*i].pltype, |
| 123 | codecs_[*i].channels, codecs_[*i].plfreq, |
| 124 | nullptr, codecs_[*i].plname)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 128 | int SendData(FrameType frame_type, |
| 129 | uint8_t payload_type, |
| 130 | uint32_t timestamp, |
| 131 | const uint8_t* payload_data, |
| 132 | size_t payload_len_bytes, |
| 133 | const RTPFragmentationHeader* fragmentation) override { |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 134 | if (frame_type == kEmptyFrame) |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 135 | return 0; |
| 136 | |
| 137 | rtp_header_.header.payloadType = payload_type; |
| 138 | rtp_header_.frameType = frame_type; |
| 139 | if (frame_type == kAudioFrameSpeech) |
| 140 | rtp_header_.type.Audio.isCNG = false; |
| 141 | else |
| 142 | rtp_header_.type.Audio.isCNG = true; |
| 143 | rtp_header_.header.timestamp = timestamp; |
| 144 | |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 145 | int ret_val = receiver_->InsertPacket( |
| 146 | rtp_header_, |
| 147 | rtc::ArrayView<const uint8_t>(payload_data, payload_len_bytes)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 148 | if (ret_val < 0) { |
| 149 | assert(false); |
| 150 | return -1; |
| 151 | } |
| 152 | rtp_header_.header.sequenceNumber++; |
| 153 | packet_sent_ = true; |
| 154 | last_frame_type_ = frame_type; |
| 155 | return 0; |
| 156 | } |
| 157 | |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 158 | AudioCodingModule::Config config_; |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 159 | std::unique_ptr<AcmReceiver> receiver_; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 160 | rtc::ArrayView<const CodecInst> codecs_; |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 161 | std::unique_ptr<AudioCodingModule> acm_; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 162 | WebRtcRTPHeader rtp_header_; |
| 163 | uint32_t timestamp_; |
| 164 | bool packet_sent_; // Set when SendData is called reset when inserting audio. |
| 165 | uint32_t last_packet_send_timestamp_; |
| 166 | FrameType last_frame_type_; |
| 167 | }; |
| 168 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 169 | #if defined(WEBRTC_ANDROID) |
| 170 | #define MAYBE_AddCodecGetCodec DISABLED_AddCodecGetCodec |
| 171 | #else |
| 172 | #define MAYBE_AddCodecGetCodec AddCodecGetCodec |
| 173 | #endif |
| 174 | TEST_F(AcmReceiverTestOldApi, MAYBE_AddCodecGetCodec) { |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 175 | // Add codec. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 176 | for (size_t n = 0; n < codecs_.size(); ++n) { |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 177 | if (n & 0x1) { // Just add codecs with odd index. |
| 178 | EXPECT_EQ( |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 179 | 0, receiver_->AddCodec(rtc::checked_cast<int>(n), codecs_[n].pltype, |
| 180 | codecs_[n].channels, codecs_[n].plfreq, NULL, |
| 181 | codecs_[n].plname)); |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 182 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 183 | } |
| 184 | // Get codec and compare. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 185 | for (size_t n = 0; n < codecs_.size(); ++n) { |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 186 | CodecInst my_codec; |
| 187 | if (n & 0x1) { |
| 188 | // Codecs with odd index should match the reference. |
| 189 | EXPECT_EQ(0, receiver_->DecoderByPayloadType(codecs_[n].pltype, |
| 190 | &my_codec)); |
| 191 | EXPECT_TRUE(CodecsEqual(codecs_[n], my_codec)); |
| 192 | } else { |
| 193 | // Codecs with even index are not registered. |
| 194 | EXPECT_EQ(-1, receiver_->DecoderByPayloadType(codecs_[n].pltype, |
| 195 | &my_codec)); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 200 | #if defined(WEBRTC_ANDROID) |
| 201 | #define MAYBE_AddCodecChangePayloadType DISABLED_AddCodecChangePayloadType |
| 202 | #else |
| 203 | #define MAYBE_AddCodecChangePayloadType AddCodecChangePayloadType |
| 204 | #endif |
| 205 | TEST_F(AcmReceiverTestOldApi, MAYBE_AddCodecChangePayloadType) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 206 | const CodecIdInst codec1(RentACodec::CodecId::kPCMA); |
| 207 | CodecInst codec2 = codec1.inst; |
| 208 | ++codec2.pltype; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 209 | CodecInst test_codec; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 210 | |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 211 | // Register the same codec with different payloads. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 212 | EXPECT_EQ(0, receiver_->AddCodec(codec1.id, codec1.inst.pltype, |
| 213 | codec1.inst.channels, codec1.inst.plfreq, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 214 | nullptr, codec1.inst.plname)); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 215 | EXPECT_EQ(0, receiver_->AddCodec(codec1.id, codec2.pltype, codec2.channels, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 216 | codec2.plfreq, NULL, codec2.plname)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 217 | |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 218 | // Both payload types should exist. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 219 | EXPECT_EQ(0, |
| 220 | receiver_->DecoderByPayloadType(codec1.inst.pltype, &test_codec)); |
| 221 | EXPECT_EQ(true, CodecsEqual(codec1.inst, test_codec)); |
| 222 | EXPECT_EQ(0, receiver_->DecoderByPayloadType(codec2.pltype, &test_codec)); |
| 223 | EXPECT_EQ(true, CodecsEqual(codec2, test_codec)); |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 224 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 225 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 226 | #if defined(WEBRTC_ANDROID) |
| 227 | #define MAYBE_AddCodecChangeCodecId DISABLED_AddCodecChangeCodecId |
| 228 | #else |
| 229 | #define MAYBE_AddCodecChangeCodecId AddCodecChangeCodecId |
| 230 | #endif |
| 231 | TEST_F(AcmReceiverTestOldApi, AddCodecChangeCodecId) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 232 | const CodecIdInst codec1(RentACodec::CodecId::kPCMU); |
| 233 | CodecIdInst codec2(RentACodec::CodecId::kPCMA); |
| 234 | codec2.inst.pltype = codec1.inst.pltype; |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 235 | CodecInst test_codec; |
| 236 | |
| 237 | // Register the same payload type with different codec ID. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 238 | EXPECT_EQ(0, receiver_->AddCodec(codec1.id, codec1.inst.pltype, |
| 239 | codec1.inst.channels, codec1.inst.plfreq, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 240 | nullptr, codec1.inst.plname)); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 241 | EXPECT_EQ(0, receiver_->AddCodec(codec2.id, codec2.inst.pltype, |
| 242 | codec2.inst.channels, codec2.inst.plfreq, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 243 | nullptr, codec2.inst.plname)); |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 244 | |
| 245 | // Make sure that the last codec is used. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 246 | EXPECT_EQ(0, |
| 247 | receiver_->DecoderByPayloadType(codec2.inst.pltype, &test_codec)); |
| 248 | EXPECT_EQ(true, CodecsEqual(codec2.inst, test_codec)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 251 | #if defined(WEBRTC_ANDROID) |
| 252 | #define MAYBE_AddCodecRemoveCodec DISABLED_AddCodecRemoveCodec |
| 253 | #else |
| 254 | #define MAYBE_AddCodecRemoveCodec AddCodecRemoveCodec |
| 255 | #endif |
| 256 | TEST_F(AcmReceiverTestOldApi, MAYBE_AddCodecRemoveCodec) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 257 | const CodecIdInst codec(RentACodec::CodecId::kPCMA); |
| 258 | const int payload_type = codec.inst.pltype; |
| 259 | EXPECT_EQ( |
| 260 | 0, receiver_->AddCodec(codec.id, codec.inst.pltype, codec.inst.channels, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 261 | codec.inst.plfreq, nullptr, codec.inst.plname)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 262 | |
| 263 | // Remove non-existing codec should not fail. ACM1 legacy. |
| 264 | EXPECT_EQ(0, receiver_->RemoveCodec(payload_type + 1)); |
| 265 | |
| 266 | // Remove an existing codec. |
| 267 | EXPECT_EQ(0, receiver_->RemoveCodec(payload_type)); |
| 268 | |
| 269 | // Ask for the removed codec, must fail. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 270 | CodecInst ci; |
| 271 | EXPECT_EQ(-1, receiver_->DecoderByPayloadType(payload_type, &ci)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 274 | #if defined(WEBRTC_ANDROID) |
| 275 | #define MAYBE_SampleRate DISABLED_SampleRate |
| 276 | #else |
| 277 | #define MAYBE_SampleRate SampleRate |
| 278 | #endif |
| 279 | TEST_F(AcmReceiverTestOldApi, MAYBE_SampleRate) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 280 | const RentACodec::CodecId kCodecId[] = {RentACodec::CodecId::kISAC, |
| 281 | RentACodec::CodecId::kISACSWB}; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 282 | AddSetOfCodecs(kCodecId); |
| 283 | |
| 284 | AudioFrame frame; |
| 285 | const int kOutSampleRateHz = 8000; // Different than codec sample rate. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 286 | for (const auto codec_id : kCodecId) { |
| 287 | const CodecIdInst codec(codec_id); |
| 288 | const int num_10ms_frames = codec.inst.pacsize / (codec.inst.plfreq / 100); |
| 289 | InsertOnePacketOfSilence(codec.id); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 290 | for (int k = 0; k < num_10ms_frames; ++k) { |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 291 | bool muted; |
| 292 | EXPECT_EQ(0, receiver_->GetAudio(kOutSampleRateHz, &frame, &muted)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 293 | } |
henrik.lundin | d89814b | 2015-11-23 06:49:25 -0800 | [diff] [blame] | 294 | EXPECT_EQ(codec.inst.plfreq, receiver_->last_output_sample_rate_hz()); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 298 | class AcmReceiverTestFaxModeOldApi : public AcmReceiverTestOldApi { |
| 299 | protected: |
| 300 | AcmReceiverTestFaxModeOldApi() { |
| 301 | config_.neteq_config.playout_mode = kPlayoutFax; |
| 302 | } |
| 303 | |
| 304 | void RunVerifyAudioFrame(RentACodec::CodecId codec_id) { |
| 305 | // Make sure "fax mode" is enabled. This will avoid delay changes unless the |
| 306 | // packet-loss concealment is made. We do this in order to make the |
| 307 | // timestamp increments predictable; in normal mode, NetEq may decide to do |
| 308 | // accelerate or pre-emptive expand operations after some time, offsetting |
| 309 | // the timestamp. |
| 310 | EXPECT_EQ(kPlayoutFax, config_.neteq_config.playout_mode); |
| 311 | |
| 312 | const RentACodec::CodecId kCodecId[] = {codec_id}; |
| 313 | AddSetOfCodecs(kCodecId); |
| 314 | |
| 315 | const CodecIdInst codec(codec_id); |
| 316 | const int output_sample_rate_hz = codec.inst.plfreq; |
| 317 | const size_t output_channels = codec.inst.channels; |
| 318 | const size_t samples_per_ms = rtc::checked_cast<size_t>( |
| 319 | rtc::CheckedDivExact(output_sample_rate_hz, 1000)); |
| 320 | const int num_10ms_frames = rtc::CheckedDivExact( |
| 321 | codec.inst.pacsize, rtc::checked_cast<int>(10 * samples_per_ms)); |
| 322 | const AudioFrame::VADActivity expected_vad_activity = |
| 323 | output_sample_rate_hz > 16000 ? AudioFrame::kVadActive |
| 324 | : AudioFrame::kVadPassive; |
| 325 | |
| 326 | // Expect the first output timestamp to be 5*fs/8000 samples before the |
| 327 | // first inserted timestamp (because of NetEq's look-ahead). (This value is |
| 328 | // defined in Expand::overlap_length_.) |
| 329 | uint32_t expected_output_ts = last_packet_send_timestamp_ - |
| 330 | rtc::CheckedDivExact(5 * output_sample_rate_hz, 8000); |
| 331 | |
| 332 | AudioFrame frame; |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 333 | bool muted; |
| 334 | EXPECT_EQ(0, receiver_->GetAudio(output_sample_rate_hz, &frame, &muted)); |
henrik.lundin | 15c51e3 | 2016-04-06 08:38:56 -0700 | [diff] [blame] | 335 | // Expect timestamp = 0 before first packet is inserted. |
| 336 | EXPECT_EQ(0u, frame.timestamp_); |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 337 | for (int i = 0; i < 5; ++i) { |
| 338 | InsertOnePacketOfSilence(codec.id); |
| 339 | for (int k = 0; k < num_10ms_frames; ++k) { |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 340 | EXPECT_EQ(0, |
| 341 | receiver_->GetAudio(output_sample_rate_hz, &frame, &muted)); |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 342 | EXPECT_EQ(expected_output_ts, frame.timestamp_); |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 343 | expected_output_ts += rtc::checked_cast<uint32_t>(10 * samples_per_ms); |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 344 | EXPECT_EQ(10 * samples_per_ms, frame.samples_per_channel_); |
| 345 | EXPECT_EQ(output_sample_rate_hz, frame.sample_rate_hz_); |
| 346 | EXPECT_EQ(output_channels, frame.num_channels_); |
| 347 | EXPECT_EQ(AudioFrame::kNormalSpeech, frame.speech_type_); |
| 348 | EXPECT_EQ(expected_vad_activity, frame.vad_activity_); |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 349 | EXPECT_FALSE(muted); |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | }; |
| 354 | |
| 355 | #if defined(WEBRTC_ANDROID) |
| 356 | #define MAYBE_VerifyAudioFramePCMU DISABLED_VerifyAudioFramePCMU |
| 357 | #else |
| 358 | #define MAYBE_VerifyAudioFramePCMU VerifyAudioFramePCMU |
| 359 | #endif |
| 360 | TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFramePCMU) { |
| 361 | RunVerifyAudioFrame(RentACodec::CodecId::kPCMU); |
| 362 | } |
| 363 | |
| 364 | #if defined(WEBRTC_ANDROID) |
| 365 | #define MAYBE_VerifyAudioFrameISAC DISABLED_VerifyAudioFrameISAC |
| 366 | #else |
| 367 | #define MAYBE_VerifyAudioFrameISAC VerifyAudioFrameISAC |
| 368 | #endif |
| 369 | TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameISAC) { |
| 370 | RunVerifyAudioFrame(RentACodec::CodecId::kISAC); |
| 371 | } |
| 372 | |
| 373 | #if defined(WEBRTC_ANDROID) |
| 374 | #define MAYBE_VerifyAudioFrameOpus DISABLED_VerifyAudioFrameOpus |
| 375 | #else |
| 376 | #define MAYBE_VerifyAudioFrameOpus VerifyAudioFrameOpus |
| 377 | #endif |
| 378 | TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameOpus) { |
| 379 | RunVerifyAudioFrame(RentACodec::CodecId::kOpus); |
| 380 | } |
| 381 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 382 | #if defined(WEBRTC_ANDROID) |
| 383 | #define MAYBE_PostdecodingVad DISABLED_PostdecodingVad |
| 384 | #else |
| 385 | #define MAYBE_PostdecodingVad PostdecodingVad |
| 386 | #endif |
| 387 | TEST_F(AcmReceiverTestOldApi, MAYBE_PostdecodingVad) { |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 388 | EXPECT_TRUE(config_.neteq_config.enable_post_decode_vad); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 389 | const CodecIdInst codec(RentACodec::CodecId::kPCM16Bwb); |
| 390 | ASSERT_EQ( |
| 391 | 0, receiver_->AddCodec(codec.id, codec.inst.pltype, codec.inst.channels, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 392 | codec.inst.plfreq, nullptr, "")); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 393 | const int kNumPackets = 5; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 394 | const int num_10ms_frames = codec.inst.pacsize / (codec.inst.plfreq / 100); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 395 | AudioFrame frame; |
| 396 | for (int n = 0; n < kNumPackets; ++n) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 397 | InsertOnePacketOfSilence(codec.id); |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 398 | for (int k = 0; k < num_10ms_frames; ++k) { |
| 399 | bool muted; |
| 400 | ASSERT_EQ(0, receiver_->GetAudio(codec.inst.plfreq, &frame, &muted)); |
| 401 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 402 | } |
| 403 | EXPECT_EQ(AudioFrame::kVadPassive, frame.vad_activity_); |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 404 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 405 | |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 406 | class AcmReceiverTestPostDecodeVadPassiveOldApi : public AcmReceiverTestOldApi { |
| 407 | protected: |
| 408 | AcmReceiverTestPostDecodeVadPassiveOldApi() { |
| 409 | config_.neteq_config.enable_post_decode_vad = false; |
| 410 | } |
| 411 | }; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 412 | |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 413 | #if defined(WEBRTC_ANDROID) |
| 414 | #define MAYBE_PostdecodingVad DISABLED_PostdecodingVad |
| 415 | #else |
| 416 | #define MAYBE_PostdecodingVad PostdecodingVad |
| 417 | #endif |
| 418 | TEST_F(AcmReceiverTestPostDecodeVadPassiveOldApi, MAYBE_PostdecodingVad) { |
| 419 | EXPECT_FALSE(config_.neteq_config.enable_post_decode_vad); |
| 420 | const CodecIdInst codec(RentACodec::CodecId::kPCM16Bwb); |
| 421 | ASSERT_EQ( |
| 422 | 0, receiver_->AddCodec(codec.id, codec.inst.pltype, codec.inst.channels, |
| 423 | codec.inst.plfreq, nullptr, "")); |
| 424 | const int kNumPackets = 5; |
| 425 | const int num_10ms_frames = codec.inst.pacsize / (codec.inst.plfreq / 100); |
| 426 | AudioFrame frame; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 427 | for (int n = 0; n < kNumPackets; ++n) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 428 | InsertOnePacketOfSilence(codec.id); |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 429 | for (int k = 0; k < num_10ms_frames; ++k) { |
| 430 | bool muted; |
| 431 | ASSERT_EQ(0, receiver_->GetAudio(codec.inst.plfreq, &frame, &muted)); |
| 432 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 433 | } |
| 434 | EXPECT_EQ(AudioFrame::kVadUnknown, frame.vad_activity_); |
| 435 | } |
| 436 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 437 | #if defined(WEBRTC_ANDROID) |
| 438 | #define MAYBE_LastAudioCodec DISABLED_LastAudioCodec |
kwiberg | 98ab3a4 | 2015-09-30 21:54:21 -0700 | [diff] [blame] | 439 | #else |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 440 | #define MAYBE_LastAudioCodec LastAudioCodec |
kwiberg | 98ab3a4 | 2015-09-30 21:54:21 -0700 | [diff] [blame] | 441 | #endif |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 442 | #if defined(WEBRTC_CODEC_ISAC) |
| 443 | TEST_F(AcmReceiverTestOldApi, MAYBE_LastAudioCodec) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 444 | const RentACodec::CodecId kCodecId[] = { |
| 445 | RentACodec::CodecId::kISAC, RentACodec::CodecId::kPCMA, |
| 446 | RentACodec::CodecId::kISACSWB, RentACodec::CodecId::kPCM16Bswb32kHz}; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 447 | AddSetOfCodecs(kCodecId); |
| 448 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 449 | const RentACodec::CodecId kCngId[] = { |
| 450 | // Not including full-band. |
| 451 | RentACodec::CodecId::kCNNB, RentACodec::CodecId::kCNWB, |
| 452 | RentACodec::CodecId::kCNSWB}; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 453 | AddSetOfCodecs(kCngId); |
| 454 | |
| 455 | // Register CNG at sender side. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 456 | for (auto id : kCngId) |
| 457 | ASSERT_EQ(0, acm_->RegisterSendCodec(CodecIdInst(id).inst)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 458 | |
| 459 | CodecInst codec; |
| 460 | // No audio payload is received. |
| 461 | EXPECT_EQ(-1, receiver_->LastAudioCodec(&codec)); |
| 462 | |
| 463 | // Start with sending DTX. |
| 464 | ASSERT_EQ(0, acm_->SetVAD(true, true, VADVeryAggr)); |
| 465 | packet_sent_ = false; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 466 | InsertOnePacketOfSilence(CodecIdInst(kCodecId[0]).id); // Enough to test |
| 467 | // with one codec. |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 468 | ASSERT_TRUE(packet_sent_); |
| 469 | EXPECT_EQ(kAudioFrameCN, last_frame_type_); |
| 470 | |
| 471 | // Has received, only, DTX. Last Audio codec is undefined. |
| 472 | EXPECT_EQ(-1, receiver_->LastAudioCodec(&codec)); |
henrik.lundin | 057fb89 | 2015-11-23 08:19:52 -0800 | [diff] [blame] | 473 | EXPECT_FALSE(receiver_->last_packet_sample_rate_hz()); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 474 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 475 | for (auto id : kCodecId) { |
| 476 | const CodecIdInst c(id); |
| 477 | |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 478 | // Set DTX off to send audio payload. |
| 479 | acm_->SetVAD(false, false, VADAggr); |
| 480 | packet_sent_ = false; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 481 | InsertOnePacketOfSilence(c.id); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 482 | |
| 483 | // Sanity check if Actually an audio payload received, and it should be |
| 484 | // of type "speech." |
| 485 | ASSERT_TRUE(packet_sent_); |
| 486 | ASSERT_EQ(kAudioFrameSpeech, last_frame_type_); |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 487 | EXPECT_EQ(c.inst.plfreq, receiver_->last_packet_sample_rate_hz()); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 488 | |
| 489 | // Set VAD on to send DTX. Then check if the "Last Audio codec" returns |
| 490 | // the expected codec. |
| 491 | acm_->SetVAD(true, true, VADAggr); |
| 492 | |
| 493 | // Do as many encoding until a DTX is sent. |
| 494 | while (last_frame_type_ != kAudioFrameCN) { |
| 495 | packet_sent_ = false; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 496 | InsertOnePacketOfSilence(c.id); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 497 | ASSERT_TRUE(packet_sent_); |
| 498 | } |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 499 | EXPECT_EQ(c.inst.plfreq, receiver_->last_packet_sample_rate_hz()); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 500 | EXPECT_EQ(0, receiver_->LastAudioCodec(&codec)); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 501 | EXPECT_TRUE(CodecsEqual(c.inst, codec)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 504 | #endif |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 505 | |
| 506 | } // namespace acm2 |
| 507 | |
| 508 | } // namespace webrtc |